i2cdevlib icon indicating copy to clipboard operation
i2cdevlib copied to clipboard

MPU6050 with DMP active hangs indefinitely using Arduino Wire library

Open batata004 opened this issue 10 years ago • 84 comments

Sometimes my Arduino freezes or crashes when using JEFF ROWBERG library to read DMP data from MPU6050. It took me almost a full year to discover the problem and today I had some spare time and I tried to find where exactly arduino hangs/freezes.

I discovered it is usually when executin getFIFOCount() or getIntStatus(). I discovered that inserting many Serial.print with tags along my entire code so I could know exactly which line hanged. So I discovered everytime my arduino hanged/freezes it was at a line with getFIFOCount() or getIntStatus(). So I decided to take a close look at it.

Inside the file MPU6050.cpp there is a code like this:

uint16_t MPU6050::getFIFOCount() { I2Cdev::readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer); return (((uint16_t)buffer[0]) << 8) | buffer[1]; }

I checked the function "readBytes()" inside I2Cdev.cpp and I found this:

I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout)

You see the problem? The function readBytes accepts one last parameter which is "timeout" but the function getFIFOCount uses readBytes without the timeout parameter! That's probably a bug cause sometimes I think the readBytes takes too long to execute and a TIMEOUT SHOULD BE set to avoid the function hanging out forever.

I am pretty sure maaaany MAAAANY other people alterady experienced this problem and spent a lot of time trying to understand what went wrong. Some people mistakenly believe it's related to bufffer overflow in the FIFO or because of some non optimized code. It could be the problem but MY PROBLEM FOR ALMOST A FULL YEAR has been this bug reported here.

I really hope someone fix this and add a timeout so nobody would spent entire months anymore seeking a problem that's is really hard to track an intermitent.

batata004 avatar Jun 24 '16 03:06 batata004

This is my first time using the library and I think I am running into this issue as well. Did you figure out what timeout to give?

Thank You.

bishwa9 avatar Jun 27 '16 23:06 bishwa9

No, it looks like this library support is dead. I think we should move to other alternatives cause this library has already many bugs documented and unresolved so it's really unreliable if you are gonna use this library to anything serious. Jeff Rowberg is not providing the good support he gave to this library in the old days so I think you would be better off this library

batata004 avatar Jun 28 '16 15:06 batata004

Apologies for the lack of support. I've tried to migrate most of the effort to online forums and other willing (and often more capable) contributors as much as possible, since I can't devote the time to this that I used to. The best laid plans of mice and men, as it were...

However, in this case, I strongly doubt that it is the lack of a timeout argument that is causing the underlying issue. Arduino's Wire library does not actually provide timeout functionality. If you dig into the code, you'll see a blocking loop in the TWI calls. See this for example:

https://github.com/arduino/Arduino/issues/1476

Supplying a timeout in I2Cdevlib code would make zero difference, because there's no way to pass it to the underlying Wire/TWI implementation. The only way around this is to use a different timeout-capable I2C implementation. This is possible, but not simple.

I would recommend checking the SDA/SCL lines with a logic analyzer to see when/why the sensor stops responding. The blocking loop inside Arduino's Wire/TWI code should not hang as long as the sensor responds according to the I2C spec.

jrowberg avatar Jun 28 '16 15:06 jrowberg

@jrowberg I never wanted to offend you. I know you did this library for free and you are contributing a lot with the community providing it to general use. This library is really great but there are some bugs with it. When you have sometime I would like you to take a look at this:

http://forum.arduino.cc/index.php?topic=408851.0

For some reason your library is a little "unstable", when you change some lines of code it simply stops working. I also provide you below with a really simple code to use your great library to retrive DMP data from MPU6050 without using interrupt pin. It simply freezes arduino after some time. I already checked with an osciloscope and everything is working fine there is not reason (at the hardware) that could make this freeze happens. Could you please run the code below in your arduino and wait a few seconds? After around 10-30 seconds your arduino will freeze.

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
MPU6050 mpu;
uint16_t packetSize;
uint16_t fifoCount;
uint8_t fifoBuffer[64];
Quaternion q;
VectorFloat gravity;
float ypr[3];

void setup() {

    Wire.begin();
    TWBR = 24;
    mpu.initialize();
    mpu.dmpInitialize();
    mpu.setXAccelOffset(-1343);
    mpu.setYAccelOffset(-1155);
    mpu.setZAccelOffset(1033);
    mpu.setXGyroOffset(19);
    mpu.setYGyroOffset(-27);
    mpu.setZGyroOffset(16);
    mpu.setDMPEnabled(true);
    packetSize = mpu.dmpGetFIFOPacketSize();
    fifoCount = mpu.getFIFOCount();

    Serial.begin(115200);

}

void loop() {

    while (fifoCount < packetSize) {

        //here comes my other code




        fifoCount = mpu.getFIFOCount();

    }


    if (fifoCount == 1024) {

        mpu.resetFIFO();
        Serial.println(F("FIFO overflow!"));

    }
    else{

        if (fifoCount % packetSize != 0) {

            mpu.resetFIFO();

        }
        else{

            while (fifoCount >= packetSize) {

                mpu.getFIFOBytes(fifoBuffer,packetSize);
                fifoCount -= packetSize;

            }    

            mpu.dmpGetQuaternion(&q,fifoBuffer);
            mpu.dmpGetGravity(&gravity,&q);
            mpu.dmpGetYawPitchRoll(ypr,&q,&gravity);            

            Serial.print("ypr\t");
            Serial.print(ypr[0]*180/PI);
            Serial.print("\t");
            Serial.print(ypr[1]*180/PI);
            Serial.print("\t");
            Serial.print(ypr[2]*180/PI);
            Serial.println();

        }

    }

}

batata004 avatar Jun 28 '16 16:06 batata004

Hello,

I used to have the same problems. Try running it without the serial prints.It had worked for me.

cheers and regards

On Tue, Jun 28, 2016 at 9:51 PM, batata004 [email protected] wrote:

@jrowberg https://github.com/jrowberg I never wanted to offend you. I know you did this library for free and you are contributing a lot with the community providing it to general use. This library is really great but there are some bugs with it. When you have sometime I would like you to take a look at this:

http://forum.arduino.cc/index.php?topic=408851.0

For some reason your library is a little "unstable", when you change some lines of code it simply stops working. I also provide you below with a really simple code to use your great library to retrive DMP data from MPU6050 without using interrupt pin. It simply freezes arduino after some time. I already checked with an osciloscope and everything is working fine there is not reason (at the hardware) that could make this freeze happens. Could you please run the code below in your arduino and wait a few seconds? After around 10-30 seconds your arduino will freeze.

#include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" #include "Wire.h" MPU6050 mpu; uint16_t packetSize; uint16_t fifoCount; uint8_t fifoBuffer[64]; Quaternion q; VectorFloat gravity; float ypr[3];

void setup() {

Wire.begin();
TWBR = 24;
mpu.initialize();
mpu.dmpInitialize();
mpu.setXAccelOffset(-1343);
mpu.setYAccelOffset(-1155);
mpu.setZAccelOffset(1033);
mpu.setXGyroOffset(19);
mpu.setYGyroOffset(-27);
mpu.setZGyroOffset(16);
mpu.setDMPEnabled(true);
packetSize = mpu.dmpGetFIFOPacketSize();
fifoCount = mpu.getFIFOCount();

Serial.begin(115200);

}

void loop() {

while (fifoCount < packetSize) {

    //here comes my other code




    fifoCount = mpu.getFIFOCount();

}


if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));

}
else{

    if (fifoCount % packetSize != 0) {

        mpu.resetFIFO();

    }
    else{

        while (fifoCount >= packetSize) {

            mpu.getFIFOBytes(fifoBuffer,packetSize);
            fifoCount -= packetSize;

        }

        mpu.dmpGetQuaternion(&q,fifoBuffer);
        mpu.dmpGetGravity(&gravity,&q);
        mpu.dmpGetYawPitchRoll(ypr,&q,&gravity);

        Serial.print("ypr\t");
        Serial.print(ypr[0]*180/PI);
        Serial.print("\t");
        Serial.print(ypr[1]*180/PI);
        Serial.print("\t");
        Serial.print(ypr[2]*180/PI);
        Serial.println();

    }

}

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jrowberg/i2cdevlib/issues/252#issuecomment-229102224, or mute the thread https://github.com/notifications/unsubscribe/AHU5fba5qUzoaqZYvO3ULoG8vJ2uLeH_ks5qQUoegaJpZM4I9cTT .

hgvk94 avatar Jun 28 '16 16:06 hgvk94

I tried it right now and didnt work :( I even tried changing the baud rate and it also didnt work. I think it's not related to Serial but to something in the library cause there is never a buffer overflow when I run the above code in my arduino so the Serial is not causing a significant delay that could cause a problem/overflow. I am pretty sure is something related to MPU6050 DMP library.

batata004 avatar Jun 28 '16 16:06 batata004

@jrowberg please, when you have a time try the code I gave you in your arduino with MPU6050. I am sure it's gonna freeze and I would like to know from you where is the bug of the library so I can fix it on my end.

batata004 avatar Jun 29 '16 16:06 batata004

:( I wish jeff could find the bug in this code

batata004 avatar Jul 05 '16 21:07 batata004

So the line for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); count++) doesn't work even you specify a value to timeout? Then why write (timeout == 0 || millis() - t1 < timeout) rather than removing it?

yang614 avatar Jul 07 '16 10:07 yang614

@yang614 I changed the line as you said and the bug still happens, arduino freezes. I dont know for sure if it's a timeout problem. Could you please test the code I provide in your arduino amd mpu6050? Other people on forum.arduino.cc could also replicate this bug, that's why I came here to github but unfortunatelly jeff does not look available to help with this

batata004 avatar Jul 07 '16 13:07 batata004

I changed, too. Yes ,it still happens.I am waiting Rowberg to explain it.

yang614 avatar Jul 11 '16 00:07 yang614

yeap, same here. I really hope Rowberg replies us here.

batata004 avatar Jul 11 '16 02:07 batata004

Do you use mag? I use the HMC5883L with mpu6050 to read the mag raw data and read the pitch yaw roll by DMP is fine. But when I use the mpu9150 to read the the mag data by DMP only , it keeps overflow and freezes soon.

You should change your TWBR =24 to =12 or substitute TWBR to Wire.setclock(400000),if only read data without mag ,mine is working fine with this edition by Jeff Rowberg.

yang614 avatar Jul 11 '16 02:07 yang614

I only use MPU6050 without any magnetometer, and I try to read data from DMP (not raw data). It freezes after a while.

You think chaing TWBR from 24 to 12 should help?

batata004 avatar Jul 11 '16 02:07 batata004

Yes, or Wire.setclock(400000).

yang614 avatar Jul 11 '16 02:07 yang614

I tried it just right now. If freezed using "Wire.setClock(400000);" and also when using "TWBR = 12;". Any suggestion?

batata004 avatar Jul 11 '16 13:07 batata004

A while ago I had issues with FIFO overruns on a 9150 but I can't remember the details. Switching to FastWire fixed it for me.

On 11 Jul 2016 14:59, batata004 [email protected] wrote:

I tried it just right now. If freezed using "Wire.setClock(400000);" and also when using "TWBR = 12;". Any suggestion?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/jrowberg/i2cdevlib/issues/252#issuecomment-231742469, or mute the threadhttps://github.com/notifications/unsubscribe/AA9a97SsYhZ-M_bsLXiththQS5P6exTfks5qUkxQgaJpZM4I9cTT.

jaundice avatar Jul 11 '16 17:07 jaundice

Sir, how I switch to fast wire?

batata004 avatar Jul 11 '16 17:07 batata004

@TMcGahee you certainly have no idea what you are talking about and you surely should delete your reply to this bug report. Read all the thread, I am not using any delay and I know much better than you how important it is to keep the loop fast.

batata004 avatar Aug 23 '16 12:08 batata004

@batata004 , any progress on this matter?

ewerlopes avatar Oct 11 '16 08:10 ewerlopes