回転しながら加速度・ジャイロセンサのキャリブレーションをしているため,手直し必要
#include "MPU9250.h"
const int motorA[3] = {13, 4, 25}; // AIN1, AIN2, PWMA
const int motorB[3] = {14, 27, 26}; // BIN1, BIN2, PWMB
const int CHANNEL_A = 0;
const int CHANNEL_B = 1;
const int LEDC_TIMER_BIT = 8;
const int LEDC_BASE_FREQ = 490;
MPU9250 mpu; // You can also use MPU9255 as is
void setup() {
for(int i = 0; i < 3; i++){
pinMode(motorA[i], OUTPUT);
pinMode(motorB[i], OUTPUT);
}
ledcSetup(CHANNEL_A, LEDC_BASE_FREQ, LEDC_TIMER_BIT);
ledcSetup(CHANNEL_B, LEDC_BASE_FREQ, LEDC_TIMER_BIT);
ledcAttachPin(motorA[2], CHANNEL_A);
ledcAttachPin(motorB[2], CHANNEL_B);
Serial.begin(115200);
Wire.begin();
delay(2000);
mpu.setup(0x68); // change to your own address
delay(5000);
// calibrate anytime you want to
mpu.calibrateAccelGyro();
mpu.calibrateMag();
}
void loop() {
if (mpu.update()) {
// 上限,下限
Serial.print(-200); Serial.print(", ");
Serial.print(200); Serial.print(", ");
Serial.print(mpu.getYaw()); Serial.print(", ");
Serial.print(mpu.getPitch()); Serial.print(", ");
Serial.println(mpu.getRoll());
}
// 右モータ(CW,時計回り)
digitalWrite(motorB[1], LOW);
digitalWrite(motorB[0], HIGH);
ledcWrite(CHANNEL_B, 50);
}