MPU6050/MPU6500/MPU9150/MPU9250 library over I2c

This works great, thanks!! :D Took me less than 5 minutes to get everything up and running on a new setup, showing y,r,p values on the serial port. Do you also see some initial 'drifting' of the values right after powerup before the values stabilize? Just as confirmation, I need to set the #define MP6050 in the inv_mpu.h file, and I am using a gy-521 board populated with a mpu6050 and the ardiono uno board.

Thanks for trying! Yes, it is not as difficult as it sounds :)

There is initial drift. In fact there are 2 types of drifts: - initial - yaw

The MPU has an internal calibration mechanism that is triggered after 8 seconds of no motion. You can find more information in mpu.c and google DMP_FEATURE_SEND_CAL_GYRO.

The yaw drift is normal for this MPU and is very marginal.

Hope this clarifies.

I found some nice work done here http://www.i2cdevlib.com/forums/topic/96-arduino-sketch-to-automatically-calculate-mpu6050-offsets/ to calibrate the MPU. I modified his code a bit to write the calibrated offset values to EEPROM during calibration and then read it back and apply it during the setup phase of my sketch. The stability on yaw looks really good, still need to test repeated 360 degrees loops to makes sure the values come back to zero after a few loops. Thanks, Conrad

Hi !

Thanks a lot for sharing AvrCopter.ino !

I recently bought a MPU9250 breakout board from Drotek, and succeeded to access to accelerometer and gyro values.

I want to use the DMP function of the MPU9250, in order to use it as an integrated AHRS.
After downloading your AvrCopter project, as I use the Arduino IDE, I’ve added “#define MPU9250” in “inv_pmu.cpp”, and the compilation and upload is ok, but I get the following error : "READ ERROR! " again and again.
I’ve tryed to find the reason, but after 3 hours yesterday night, I’m a little bit lost !

I’ve seen the founction “dmp_enable_feature” in inv_mpu_dmp_motion_driver.cpp, but it seems it is never called…

If you could help me, that would be great !!!

Regards,

Sylvain

I just forgot to tell you the board I'm using : Arduino mega2560. I also have a Leonardo that I can try...

Sylvain

Hi Sylvain,

READ ERROR message might appear when your wiring is incorrect.

Can you try running i2c scan to see if your device is connected correctly and check what address it uses? http://playground.arduino.cc/Main/I2cScanner

To change i2c address, see: https://github.com/rpicopter/ArduinoMotionSensorExample/blob/master/mpu.cpp#L28 Value 0 = 0x68, Value 1= 0x69

If this does not help please specify #define MPU_DEBUG in mpu.h and recompile. Post the output (MPU Init value).

Hi, Thanks for your answer !

I have uploaded i2c-scanner, and I get that :

Scanning... I2C device found at address 0x68 ! done

So, it's ok, the MPU9250 is present.

I've tried other source codes, basic examples, and I can read accelerometer and gyrometer values (but no magnetometers values at this time).

When I add #define MPU_DEBUG in mpu.h, I get this :

START MPU init: 3 Free mem: 6802

READ ERROR! -1 READ ERROR! -1 READ ERROR! -1 READ ERROR! -1 READ ERROR! -1 READ ERROR! -1

Many thanks for yout help !

Sylvain

Hi again !

I've tried that : in function "mympu_open", with MPU_DEBUG activated, I have suppressed the line "if (ret) return 10+ret;"

Then, the program ouputs that : "Firmware compare failed addr 0 0 0 112 0 0 0 0 36 0 0 33 55 0 118 7 0 1 4 40 1 "

Probably an interesting information... but difficult to understand for me !

Regards,

Sylvain

borenis: Hi, Thanks for your answer !

I have uploaded i2c-scanner, and I get that :

Scanning... I2C device found at address 0x68 ! done

So, it's ok, the MPU9250 is present.

I've tried other source codes, basic examples, and I can read accelerometer and gyrometer values (but no magnetometers values at this time).

When I add #define MPU_DEBUG in mpu.h, I get this :

START MPU init: 3 Free mem: 6802

READ ERROR! -1 READ ERROR! -1 READ ERROR! -1 READ ERROR! -1 READ ERROR! -1 READ ERROR! -1

Many thanks for yout help !

Sylvain

Thanks, this explains quite a bit.

MPU Init: 3 indicates that the following call fails: https://github.com/rpicopter/ArduinoMotionSensorExample/blob/master/mpu.cpp#L31

Following the references, this can be traced to the following call: https://github.com/rpicopter/ArduinoMotionSensorExample/blob/master/inv_mpu.cpp#L634 This above call is a generic driver call and should not fail. The only explanation I have is that one or more parameters are not set properly. Unfortunately, I do not have MPU9250 to test it. The parameters are all set in here: https://github.com/rpicopter/ArduinoMotionSensorExample/blob/master/inv_mpu.cpp#L408

This would have to be compared with other implementations to find any discrepancies. Alternatively, this could also be related to i2c library used.

I will try to look into it once I get some free time.

Hello,

I'm interested in using the MPU-6500 along with this library in my project. As far as hardware is concerned, is this all that's needed aside from the Arduino:

http://store.invensense.com/ProductDetail/EMSENSR6500-Embedded-Masters/495014/

They also have an evaluation board:

http://store.invensense.com/ProductDetail/MPU6500EVB-InvenSense-Inc/488434/

I wanted to make sure I get the correct board.

Thanks!

Hi,

I'm unsure about the evaluation boards. What you need for this library is any of MPU6050, MPU6500, MPU9150 or MPU9250. You can get it off ebay, sparkfun, etc.

Gregory

borenis: Hi again !

I've tried that : in function "mympu_open", with MPU_DEBUG activated, I have suppressed the line "if (ret) return 10+ret;"

Then, the program ouputs that : "Firmware compare failed addr 0 0 0 112 0 0 0 0 36 0 0 33 55 0 118 7 0 1 4 40 1 "

Probably an interesting information... but difficult to understand for me !

Regards,

Sylvain

Hi,

I believe I fixed the issue with MPU9250. Can you pull and update and re-try?

Thanks, Gregory

Hi, I've been having a similar issue with the 9250.

Thanks for attempting a fix! I've just tried the latest commit and it's still resulting in the same error notice. Let me know if there's any debugging I can help with.

And thanks for the great work - it works great with the 6050!

Amit

I can get the sketch to work with the 9250 now, but I had to do two things:

  1. Had to add a line #define MPU9250 in the file inv_mpu.h.
  2. Had to use a Mega. On an Uno, it still spews the error -1's.

Dan

Dan,

If this works on one board but not on the other, this might be down to I2c library.

Can you try changing fastwire speed to 400 ? https://github.com/rpicopter/ArduinoMotionSensorExample/blob/master/AvrCopter.ino#L8

If this does not help you can try to use ArduinoWire library instead of FastWire: - comment line 53 and uncomment line 52: https://github.com/rpicopter/ArduinoMotionSensorExample/blob/5c4bdf2aa464bdfcf903bc166afd3bb8752bf83d/I2Cdev.h#L52

Hi Greg,

I have debugged this issue on the 9250 a bit and it seems to be related to the programming the DMP firmware to the 9250 board. I have logged the bytes written and bytes read back and when the issue happens it seems to be when the bytes read back does not match the bytes written. Furthermore, it seems that the bytes read back is always the same as the previous written buffer. It seems the something in either the i2c state machine or the memory bank/write machine on the mpu gets stuck. I have also added some retry mechanism to try to reprogram the bank if at first it failed. It improved a bit but it still not very stable. Here is an output dump of the bytes written and read back write mem 0 0 112 0 0 0 0 36 0 0 0 2 0 3 0 0 read mem 0 0 112 0 0 0 0 36 0 0 0 2 0 3 0 0 write mem 0 101 0 84 255 239 0 0 250 128 0 11 18 130 0 1 read mem 0 101 0 84 255 239 0 0 250 128 0 11 18 130 0 1 write mem 3 12 48 195 14 140 140 233 20 213 64 2 19 113 15 142 read mem 3 12 48 195 14 140 140 233 20 213 64 2 19 113 15 142 write mem 56 131 248 131 48 0 248 131 37 142 248 131 48 0 248 131 read mem 3 12 48 195 14 140 140 233 20 213 64 2 19 113 15 142 Firmware compare failed addr 48 write mem 56 131 248 131 48 0 248 131 37 142 248 131 48 0 248 131 read mem 3 12 48 195 14 140 140 233 20 213 64 2 19 113 15 142 Firmware compare failed addr 48 write mem 56 131 248 131 48 0 248 131 37 142 248 131 48 0 248 131 read mem 3 12 48 195 14 140 140 233 20 213 64 2 19 113 15 142

Weirdly though if i use some of the other libraries available (for example the one from Jeff for the 9axis motion sensor) the DMP programming works fine and I do not run into the same issue)

I have ordered some 9150 devices now (which has the mpu6050 inside), to see if is exposes the same problem. I like your library since it utilizes the internal sensor fusion of the DMP if you use the quaternion outputs, but I need to correct for yaw drift now and that is why i want to move to either the 9150 or 9250 to be able to use the compass.

Cheers, Conrad

Hi Gregory,

Great library, that is what I was looking for. Got it up and running on Nano + GY-521. And even connected it to famous teapot demo.

I have a few questions.

  1. Why you produced your own version mpu.cpp? Can I use inv_mpu.cpp?
    2.What should I do to change FSR from 2000 to 250 ? (as far as I understand it is much more than just simple #define FSR 250 in mpu.cpp)

Here is what I found on Invensense forum.

Post Re: Wrong Quaternion angle with DMP at 500 DPS This is a know bug on our end. 2000 DPS is the only full scale range the MotionDriver firmware currently supports for the gyroscope.

July 7th, 2014, 3:14 pm

Alexvc14: Hi Gregory,

Great library, that is what I was looking for. Got it up and running on Nano + GY-521. And even connected it to famous teapot demo.

I have a few questions.

  1. Why you produced your own version mpu.cpp? Can I use inv_mpu.cpp? 2.What should I do to change FSR from 2000 to 250 ? (as far as I understand it is much more than just simple #define FSR 250 in mpu.cpp)

Hi,

The mpu.cpp is not a substitution of inv_mpu.cpp. It contains just a common initialisation code that in turn calls for inv_mpu functions.

You should be able to change FSR, just remember to update GYRO_SENS accordingly. See line 6 and 7 in mpu.cpp

conrads1arduino: Hi Greg,

I have debugged this issue on the 9250 a bit and it seems to be related to the programming the DMP firmware to the 9250 board. I have logged the bytes written and bytes read back and when the issue happens it seems to be when the bytes read back does not match the bytes written. Furthermore, it seems that the bytes read back is always the same as the previous written buffer. It seems the something in either the i2c state machine or the memory bank/write machine on the mpu gets stuck. I have also added some retry mechanism to try to reprogram the bank if at first it failed. It improved a bit but it still not very stable. [...]

Hi Conrad,

This really might be down to the i2c library used. Have you tried to use slower transmission speeds? See my previous posts for details.

I'm using myself 9150 on pro mini and this works flawlessly. But again, this might be different on other arduino models.