MPU6050/MPU6500/MPU9150/MPU9250 library over I2c

Hi,

As a subtask for my main project I ported the entire InvenSense MPU/DMP 5.1 library to Arduino. Hopefully this will save time someone in the future.

I am well aware of other implementations of this library, however this implementation is intended to be comprehensive and clean. As such it is not the most space efficient but it features all functions and is easy to debug.

The encapsulating demo does a simple Quaternions calculation and outputs them along with some additional debug data.

Main features:

Thanks,
Gregory

Thanks for sharing

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.

Hi,

These error look like something has changed. Or is not included as it should.

It looks like I2CDev.h is not included correctly.

If you look into I2Cdev.h, on line 53 it specified to use I2CDEV_BUILTIN_FASTWIRE

But the errors reported by you (line 276 - BUFFER_LENGTH) does not execute when I2CDEV_BUILTIN_FASTWIRE is defined but it will execute when I2CDEV_ARDUINO_WIRE is define. See line 220 I2Cdev.cpp.

Hope this helps.

Of course, you can try to use I2CDEV_ARDUINO_WIRE but then you need to include Wire.h. However, I have not tested this extensively.

Hi,

I am getting the exactly same error as Takostyle.

Could you please tell me the information as written below?

  1. Where exactly should we include the #define MPU6050? At the beginning of inv_mpu.cpp? (I am using MPU6050 in my application)
  2. Which version of IDE you are using?
  3. Which Arduino boards you have tested working?

Thanks in advance.

Hi,

The errors are unrelated to the directive #define MPU6050. Placing it at the beginning of in inv_mpu.cpp should be fine. If this is incorrectly defined the compiler will return an error: "Which gyro are you using?"

  1. at the beginning of inv_mpu.cpp is fine
  2. used the official 1.0.5 libraries in connection with Arduino-Makefile GitHub - sudar/Arduino-Makefile: Makefile for Arduino sketches. It defines the workflows for compiling code, flashing it to Arduino and even communicating through Serial.
  3. tested on a few variants of Pro Mini

If you get a chance please attach the full build log.

This works great, thanks!! :smiley:
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 :slight_smile:

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 Arduino Sketch to automatically calculate MPU6050 offsets - MPU-6050 6-axis accelerometer/gyroscope (InvenSense) - I2Cdevlib Forums 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: ArduinoMotionSensorExample/mpu.cpp at master · rpicopter/ArduinoMotionSensorExample · GitHub
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:

Following the references, this can be traced to the following call:

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: ArduinoMotionSensorExample/inv_mpu.cpp at master · rpicopter/ArduinoMotionSensorExample · GitHub

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