Go Down

Topic: MPU6050/MPU6500/MPU9150/MPU9250 library over I2c (Read 63539 times) previous topic - next topic

gregd72002

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:
- uses FastWire and I2Cdev from Jeff Rowberg
- DMP enabled
- calculates and displays gyro and quaternions
- Makefile provided for Arduino-Makefile (https://github.com/sudar/Arduino-Makefile)

https://github.com/rpicopter/ArduinoMotionSensorExample

Thanks,
Gregory

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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.

gregd72002

#3
Jun 26, 2014, 12:20 pm Last Edit: Jun 26, 2014, 12:35 pm by gregd72002 Reason: 1
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.

wswong

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.

gregd72002

#5
Jul 15, 2014, 02:41 pm Last Edit: Jul 23, 2014, 12:29 pm by gregd72002 Reason: 1
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 https://github.com/sudar/Arduino-Makefile
3) tested on a few variants of Pro Mini

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

conrads1arduino

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.

gregd72002

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.

conrads1arduino

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

borenis

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

borenis

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

Sylvain

gregd72002

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).


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




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

gregd72002


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.

Go Up