I just tried to write my own code for an MPU6050 gyroscope/accelerometer using the MPU6050 library found here. Here is my code:
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
MPU6050 mpu; //gyro sensor
float ypr[3]; //yaw, pitch, roll
VectorFloat gravity;
Quaternion q;
uint8_t devStatus;
uint16_t packetSize;
uint8_t fifoBuffer[64];//buffer for inputs from mpu
void dmpDataReady(){ //interrupt from mpu
uint8_t mpuIntStatus = mpu.getIntStatus();
// get current FIFO count
uint16_t fifoCount = mpu.getFIFOCount();
if ((mpuIntStatus & 0x10) || fifoCount == 1024) { //check for overflow
// reset to continue cleanly
mpu.resetFIFO();
Serial.println("FIFO overflow!");
// otherwise, check for DMP data ready interrupt
}
else if(mpuIntStatus & 0x02) {
//wait for data to be available
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
mpu.getFIFOBytes(fifoBuffer, packetSize); //read a packet
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
}
}
void setup() {
// put your setup code here, to run once:
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(115200); //pour a bowl of serial
Serial.println("Initializing mpu6050...");
mpu.initialize(); //start communication with MPU6050
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
Serial.println("Initializing DMP...");
devStatus = mpu.dmpInitialize();
if (devStatus == 0) {
Serial.println("Enabling DMP...");
mpu.setDMPEnabled(true);
attachInterrupt(0, dmpDataReady, RISING);
Serial.println("Ready to read data from IMU.");
packetSize = mpu.dmpGetFIFOPacketSize();
}
else
{
Serial.println("Initialization failed.");
while(1){delay(1);}
}
}
void loop() {
// put your main code here, to run repeatedly:
dmpDataReady();
Serial.print(ypr[0] * 180/M_PI);
Serial.print(",");
Serial.print(ypr[1] * 180/M_PI);
Serial.print(",");
Serial.println(ypr[2] * 180/M_PI);
}
The code compiles and uploads fine. However, the issue I’m having is that when I open the serial monitor, it outputs:
Initializing mpu6050...
MPU6050 connection successful
Initializing DMP...
Enabling
And stops in the middle of the print statement which should print “Enabling DMP”
I can’t seem to figure out why it’s stopping in the middle of the print statement like this. When I replace the text with “Test test test”, it still stops after “Test tes”, so it seems to not be related to what’s printing.
I have no idea what’s going on here, so any help would be appreciated.