jrowberg / i2cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs
http://www.i2cdevlib.com
3.95k stars 7.51k forks source link

complete working code examples for Raspberry Pi (C++) available, e.g. for MPU6050 ? #403

Open dsyleixa opened 6 years ago

dsyleixa commented 6 years ago

hello, are there complete working code examples for Raspberry Pi 2 v1, Pi 2 v2, and Pi 3 (C++) available, e.g. for MPU6050 ? I don't find a Raspberry Pi section, similar to the Arduino section.

ajuancer commented 6 years ago

Sure, here is it! https://github.com/jrowberg/i2cdevlib/tree/master/RaspberryPi_bcm2835/MPU6050/examples

dsyleixa commented 6 years ago

ah, thanks, the bcm2835 perhaps was puzzling me... and does that also provide the fusioned yaw, pitch, and roll (Euler angles), not just the sole gyro and accelerometer values?

jrowberg commented 6 years ago

That example only has the raw accel/gryo data. Nobody as yet (that I know of) has put together an example that integrates any of the DMP code.

dsyleixa commented 6 years ago

for the Arduino it's supposed to be availble, why not for the Pi?

jrowberg commented 6 years ago

The short answer is that porting the DMP code takes effort that nobody has put in for RasPi platforms yet, presumably due to low demand. Most of the interest around this chip and library has been in the small embedded MCU world.

Pluscrafter commented 6 years ago

I have sucessfully used the DMP on my raspi zero, i will provide an example (probably tomorrow), if you want that. But then there is a bug that the mpu6050 hang off when you use two i2c devices. You have to bitbang you other I2c devices then.

dsyleixa commented 6 years ago

@ jrowberg :
:so do you think anyone will take the efforts to port the Arduino code for Euler angles to the Pi (IIRC, that is actually yaw, pitch, and roll) ? In a future that might be called "near"? ;)

2nd, does the Pi also use an interrupt pin? In case yes: which one?

dsyleixa commented 6 years ago

@ Pluscrafter :

I have sucessfully used the DMP on my raspi zero, i will provide an example (probably tomorrow), if you want that. But then there is a bug that the mpu6050 hang off when you use two i2c devices. You have to bitbang you other I2c devices then. Thanks, that was cross-over-posting!

yes, I am interested, but I have always multiple i2c devices attached to my Pi which can't be bitbanged. I have also already i2c-0 in use at 400kHz, i2c-1 is running at 100kHz. So having said that, actually only a code will be working for me if it's fully i2c-compliant to additional different other devices.

Pluscrafter commented 6 years ago

It depends some I2c devices works with the mpu6050dmp, like the BMP180 or HMC1886l(don't have the exact number, is a magnetometer), but others like the pca9685 (DCA) seems not to work with the mpu6050 dmp and cause a freeze of the program.

Pluscrafter commented 6 years ago

Here is a example from Richardghirst, https://github.com/richardghirst/PiBits/tree/master/MPU6050-Pi-Demo there is a dmp example in there.

dsyleixa commented 6 years ago

It depends some I2c devices works with the mpu6050dmp, like the BMP180 or HMC1886l(don't have the exact number, is a magnetometer), but others like the pca9685 (DCA) seems not to work with the mpu6050 dmp and cause a freeze of the program.

Do you know the reason why it fails? I have a GPS (Ublox Neo-Gy-6M), a OLED (http://hallard.me/adafruit-oled-display-driver-for-pi/), an MCP23017, 2 ASD1115 ADC, a PCA9685 Servo board, an Arduino slave (not working yet), a Real Time Clock RTC DS3231 , and some SRF-08 ultrasonic distance sensors.

dsyleixa commented 6 years ago

Here is a example from Richardghirst, https://github.com/richardghirst/PiBits/tree/master/MPU6050-Pi-Demo there is a dmp example in there.

which is the file I need to c+p to my Geany IDE? Do I need extra compiler/linker flags?

edit: https://github.com/richardghirst/PiBits/blob/master/MPU6050-Pi-Demo/demo_dmp.cpp ? how does it retrieve and show yaw, pitch, and roll?

Pluscrafter commented 6 years ago

Do you know the reason why it fails? I have a GPS (Ublox Neo-Gy-6M), a OLED (http://hallard.me/adafruit-oled-display-driver-for-pi/), an MCP23017, 2 ASD1115 ADC, a PCA9685 Servo board, an Arduino slave (not working yet), a Real Time Clock RTC DS3231 , and some SRF-08 ultrasonic distance sensors.

When I get my logic analyzer in a couple days I try to localize the problem, but there were some issues in the past like (https://github.com/jrowberg/i2cdevlib/issues/252) i think this is the same issue, but it could also be the issue, that some libaries don't work together.

Pluscrafter commented 6 years ago

Here is a example from Richardghirst, https://github.com/richardghirst/PiBits/tree/master/MPU6050-Pi-Demo there is a dmp example in there.

which is the file I need to c+p to my Geany IDE? Do I need extra compiler/linker flags?

edit: Rhttps://github.com/richardghirst/PiBits/blob/master/MPU6050-Pi-Demo/demo_dmp.cpp ? how does it retrieve and show yaw, pitch, and roll?

`#ifdef OUTPUT_READABLE_YAWPITCHROLL // display Euler angles in degrees mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); *printf("ypr %7.2f %7.2f %7.2f ", ypr[0] 180/M_PI, ypr[1] 180/M_PI, ypr[2] 180/M_PI);**

endif`

you can read the float ypr[] for the angles. This is more or less the same example like from jrowberg for the Arduino. And thanks to J.Rowberg for the libary, I use it for my quadcopter-flightcontroller, that should work in the future.

dsyleixa commented 6 years ago

as to

#ifdef  OUTPUT_READABLE_YAWPITCHROLL
(...)
printf("ypr %7.2f %7.2f %7.2f ", ypr[0] * 180/M_PI, ypr[1] * 180/M_PI, ypr[2] * 180/M_PI);

thanks - I completely missed the _YAWPITCHROLL part of the ifdef caption :o

I use it for my quadcopter-flightcontroller

very nice, I have 2 projects in progress for the MPU6050, a 7DOF robot arm (adjusting claw orientation+angle by the IMU, currently driven by a Adafruit M4 but which won't compile for that MCU yet), and an autonomous all terrain vehicle (alligning IMU yaw to GPS heading, currently driven by a Pi2).

(Perhaps 1 day also a quadrocopter coming...)

dsyleixa commented 6 years ago

Does the Pi use an interrupt GPIO? https://github.com/richardghirst/PiBits/blob/master/MPU6050-Pi-Demo/demo_dmp.cpp in case yes, which one?

Pluscrafter commented 6 years ago

Does the Pi use an interrupt GPIO? https://github.com/richardghirst/PiBits/blob/master/MPU6050-Pi-Demo/demo_dmp.cpp in case yes, which one?

I think you can use Interrupts on all GPIO(expet for SDA/SDL and PowerPins), but i haven't use yet any interrupts.

dsyleixa commented 6 years ago

No, sorry, you probably misunderstand me. I do not need to use interrupts for my purposes compellingly, but as the Arduino libs/sources are using interrupts (pin 2) I need to know if also the Pi libs/sources need them to be wired to or not.

Pluscrafter commented 6 years ago

no you dont have to wire the Interrupt pin.

Pluscrafter commented 6 years ago

BTW, what does that ......... wrapper mean for printf("ypr %7.2f %7.2f %7.2f ", ypr[0] 180/M_PI, ypr[1] 180/M_PI, ypr[2] * 180/M_PI); ?

http://www.dailyfreecode.com/code/formatted-output-real-numbers-2575.aspx

dsyleixa commented 6 years ago

thanks, but that was just because of the double * around that line in your post - probably a text formatter. **printf("ypr %7.2f %7.2f %7.2f ", ypr[0] * 180/M_PI, ypr[1] * 180/M_PI, ypr[2] * 180/M_PI);** I deleted that post meanwhile because I saw what I was missing before, and printf is actually wellknown to me ;)

Pluscrafter commented 6 years ago

ah because there was a email notification for me.

davidsilvam commented 4 years ago

@Pluscrafter on line 142 of demo_dpm.cpp has "else if (fifoCount> = 42)". I am testing the execution time on the Arduino and Raspberry Pi 3. The Arduino gets each dmp sample in 2ms while the rasp takes 50ms. In the degub, I verified that ficCount> = 42 takes an average of 69 iterations in the loop until you get a sample. Do you have any suggestions on how to reduce this time? Thanks

dsyleixa commented 4 years ago

at which i2c clock speed are you running the Pi? (I always use 400k if possible). Which Arduino board are you trying? Uno, Zero, Due, SAMD51, ESP8266,ESP32 ?

davidsilvam commented 4 years ago

Thanks @dsyleixa, I changed the -DDMP_FIFO_RATE flag to 0 in Makefile and it got a little better, but far from the performance that the Arduino achieves. I managed to catch 10ms. Fortunately, for my problem it is enough. I used the arduino uno.