MPU6050/MPU6500/MPU9150/MPU9250 library over I2c

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 ?

If this does not help you can try to use ArduinoWire library instead of FastWire:

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.

I dug deeper and found the answer on Invensense forum.
The answer to my question is NO. I cant do FSR_250 and use DMP.

http://www.invensense.com/developers/forum/viewtopic.php?f=3&t=1096&p=4631#p4631
"This is a know bug on our end. 2000 DPS is the only full scale range the MotionDriver firmware currently supports for the gyroscope."

Well found! Thanks for sharing Alexvc14

Takostyle:
Hi,

I'm actualy trying to use your library but i've encountered some errors.

I first downloaded the .zip with all the files. I extracted it in my project folder and opened it in Arduino IDE. It didn'T build at first because of the #define i had to put in inv_mpu.cpp.

Once i've put my "#define MPU6050" i tried to build but got some more errors:

C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp: In static member function 'static int8_t I2Cdev::readBytes(uint8_t, uint8_t, uint8_t, uint8_t*, uint16_t)':
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:276: error: 'BUFFER_LENGTH' was not declared in this scope
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:277: error: 'Wire' was not declared in this scope
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp: In static member function 'static int8_t I2Cdev::readWords(uint8_t, uint8_t, uint8_t, uint16_t*, uint16_t)':
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:414: error: 'BUFFER_LENGTH' was not declared in this scope
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:415: error: 'Wire' was not declared in this scope
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp: In static member function 'static bool I2Cdev::writeBytes(uint8_t, uint8_t, uint8_t, uint8_t*)':
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:598: error: 'Wire' was not declared in this scope
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp: In static member function 'static bool I2Cdev::writeWords(uint8_t, uint8_t, uint8_t, uint16_t*)':
C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:653: error: 'Wire' was not declared in this scope

These are quite simple errors but i can't find the origine.

Wire is declared a bit higher in the code:

#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE

#ifdef I2CDEV_IMPLEMENTATION_WARNINGS
#warning Using I2CDEV_BUILTIN_NBWIRE implementation may adversely affect interrupt detection.
#warning This I2Cdev implementation does not support:
#warning - Repeated starts conditions
#endif

// NBWire implementation based heavily on code by Gene Knight Gene@Telobot.com
// Originally posted on the Arduino forum at http://arduino.cc/forum/index.php/topic,70705.0.html
// Originally offered to the i2cdevlib project at http://arduino.cc/forum/index.php/topic,68210.30.html
TwoWire Wire;

#endif

At fisrt I tought that it was because it was in a if condition and the compiler just ignored it, but even if i put just after the if condition, the compiler says:

I2Cdev.cpp:90: error: 'TwoWire' does not name a type

I know for sure that all the code in the if condition is not executed because i wrote some random code and it didn't flaged it.

Once there, i don't realy know what else to do.
For the BUFFER_LENGTH error i can't find the problem either. This constant is used in other functions but the readBytes and readWords don't recognize the BUFFER_LENGTH.

Since i didn't wrote anything in the code, i trust that it should be working fine but it won't on my computer.

If you have anything to help me it would be realy awesome !

Thanks in advance,
Marvin.

I had the same problem as you. I renamed the original library "I2Cdev" in my libraries folder and it compiled fine.
But after the values show up on Serial monitor for about 5s, it froze. When I close and reopen Serial monitor, it shows the values and froze again after 5s. I don't know what's wrong.

Hello,

I'm novice to this library. I'm looking for a way to detect acceleration also to determine the platform speed and acceleration. Is it possible to get using this library?

theinlinaung2010:
I had the same problem as you. I renamed the original library "I2Cdev" in my libraries folder and it compiled fine.
But after the values show up on Serial monitor for about 5s, it froze. When I close and reopen Serial monitor, it shows the values and froze again after 5s. I don't know what's wrong.

Does it report something like 'READ ERROR' when it freezes? If it does, can you try to increase Serial speed to 115200: ArduinoMotionSensorExample/AvrCopter.ino at master · rpicopter/ArduinoMotionSensorExample · GitHub

You could also try to decrese I2C speed on Line 8 to 200 to see if it helps.

kitsooftlk:
Hello,

I'm novice to this library. I'm looking for a way to detect acceleration also to determine the platform speed and acceleration. Is it possible to get using this library?

Platform speed is more complex than just measuring acceleration. Obviously you could derive some kind of velocity but from my experience it will not be accurate. Usually people couple more sensors to derive velocity like GPS and accelerometer or barometer and accelerometer.
However the library does not do anything like that.

For starters thanks for your library!

I have been trying to use your library to get data from an IMU connected to an arduino UNO. Moreover, the sketch on the arduino will work as an ROS node (don't know if you are familiar with ROS).
After some problems I was able to get the IMU to work and I was able to receive the IMU data published by the arduino. However I want to use the same arduino to control some motors using an H-bridge. When I implement the subscriber (basically a function that will get the controls from the computer and execute the necessary motor control) the IMU stopped working.

At that point the output of mympu_open() is zero, while the output of mympu_update() becomes "-1" and I'm unable to read the IMU.
Can you help me understand what might be going on?

Thanks in advance.

Hi Gregory !

Fisrt, I'm sorry for my long silence, I've had urgent problems to solve and my MPU9250 was not my priority since september !
I yesterday downloaded your last version of avrcopter, and... It doesn' work :frowning:

I use an Arduino UNO, just like you.
When using I2C-scanner, I can find a device at I2C adress 0x68.

I do not use the makefile, but the ide compiler... Is that a problem ?
In your code, I've add #define MPU9250 in inv_mpu.h, and #define MPU_DEBUG in all .h

I get this :
MPU init: 3
Free mem: 208

I've searched were the error comes from.

It happens at the very beginning of the program, at the call of ret = mympu_open(200);

I've searched a bit more...
In mpu.cpp, function mympu_open(...), call to mpu_init(NULL) returns -7

Then, I've found that comes from inv_mpu.cpp, function mpu-read_mem, in wich
st->chip_cfg.sensors equal 0

Have you any Idea of the origin of the problem ?

Many thanks !

PS : have you tryed the MPU9250 ? If not, and if you are interested, I'd be happy to send you one on a breakout board ! :wink:

Hi Dan ! Hi Greg !
Dan, you've said you have been able to run the code with an MPU9250 + Arduino MEGA ?
I've just tryed the same, but I'm unable to make it work :frowning:
I've been trying for months now, not every nights, but...
Would you tell me which code you used ? which libraries ?
I'm afraid there's just a little thing different and nothing works...
Tonight, I've tryed an i2c_scanner with the same arduino mega and MPU9250, with a windows 7 computer runing arduino 1.0.6, and with a windows 8.1 computer runing arduino 1.5.7.
In the first case, I get the address 0x68, and in the second case, I get 0x69 !?? Strange thing !
Would you give me your complete configuration so that I can do exactly the same ?
Thanks a lot for your help !
Sylvain

borenis:
Hi Gregory !

Fisrt, I'm sorry for my long silence, I've had urgent problems to solve and my MPU9250 was not my priority since september !
I yesterday downloaded your last version of avrcopter, and... It doesn' work :frowning:

I use an Arduino UNO, just like you.
When using I2C-scanner, I can find a device at I2C adress 0x68.

I do not use the makefile, but the ide compiler... Is that a problem ?
In your code, I've add #define MPU9250 in inv_mpu.h, and #define MPU_DEBUG in all .h

I get this :
MPU init: 3
Free mem: 208

I've searched were the error comes from.

It happens at the very beginning of the program, at the call of ret = mympu_open(200);

I've searched a bit more...
In mpu.cpp, function mympu_open(...), call to mpu_init(NULL) returns -7

Then, I've found that comes from inv_mpu.cpp, function mpu-read_mem, in wich
st->chip_cfg.sensors equal 0

Have you any Idea of the origin of the problem ?

Many thanks !

PS : have you tryed the MPU9250 ? If not, and if you are interested, I'd be happy to send you one on a breakout board ! :wink:

Hi,

The key issue in here is mpu_init which returns -7.

the chip_cfg.sensors is correct to be 0 at this stage. It will change its value after mpu_set_sensor is called: ArduinoMotionSensorExample/mpu.cpp at master · rpicopter/ArduinoMotionSensorExample · GitHub

As you rightly point out the value -7 comes due to an error on mpu_read_mem:

What makes me think that the address (0x17) might not be correct. I do not have the datasheet in front but google tells me that there are devices with revision address of 0x16. Can you please try changing the address?

Thanks,
Gregory

P.S. Alternatively, you might want to just comment out/delete lines 720-729.