This is a basic
control library for using the MPU6050 accelerometer and gyroscope module with Raspberry Pi using i2c protocol It provides functions to read raw accelerometer data and fully
corrected (with complementary filters and some logic) angles on any axis (roll, pitch, yaw)
Installation
The dependencies for this library are libi2c-dev, i2c-tools, and libi2c0. These can be installed with apt. The latest version of this code now works on Raspbian Buster. Note: to run the code, you will need to enable I2C in raspi-config.
Function
Definitions
__constructor__ (MPU6050)
args: int8_t
addr - the address of the MPU6050 (usually 0x68; can find with command "i2cdetect -y 1" (may need to be installed - run "sudo
apt-get install i2c-tools -y") description:
sets up i2c device and starts loop to read the angle
getAccelRaw
args:
float *x - pointer to the variable where the X axis results should
be stored float *y - pointer to the variable where the Y axis
results should be stored float *z - pointer to the variable
where the Z axis results should be stored description:
gets the raw accelerometer values from the MPU6050 registers
getAccel
args:
float *x - pointer to the variable where the X axis results
should be stored float *y - pointer to the variable where the
Y axis results should be stored &nbpfloat *z - pointer to the
variable where the Z axis results should be stored description:
gets the accelerometer values (in g), rounded to three decimal
places
getGyroRaw
args:
float *roll - pointer to the variable where the roll (X) axis
results should be stored float *pitch - pointer to the
variable where the pitch (Y) axis results should be stored
float *yaw - pointer to the variable where the yaw (Z) axis
results should be stored description:
gets the raw gyroscope values from the MPU6050 registers
nbsp
getGyro
args:
float *roll - pointer to the variable where the roll (X) axis
results should be stored float *pitch - pointer to the
variable where the pitch (Y) axis results should be stored
float *yaw - pointer to the variable where the yaw (Z) axis
results should be stored description:
gets the gyroscope values (in degrees/second)
getOffsets
args:
float *ax_off - pointer to the variable where the accelerometer x
axis offset will be stored &nsp float *ay_off - pointer to the
variable where the accelerometer y axis offset will be stored
float *az_off - pointer to the variable where the accelerometer z
axis offset will be stored float *gr_off - pointer to the
variable where the gyroscope roll axis offset will be stored
float *gp_off - pointer to the variable where the gyroscope pitch
axis offset will be stored float *gy_off - pointer to the
variable where the gyroscope yaw axis offset will be stored description:
finds the offsets needed
before running, place the module on a completely flat surface
(check with a sirit level if possible) and make sure that it stays still while running this function, the results will be
stored in the variables ax_off, ay_off, az_off, gr_off, gp_off, gy_off for accel x offset... and gyro roll offset..., take
these values and put them into the MPU6050.h file (A_OFF_X, A_OFF_Y, A_OFF_Z, G_OFF_X, G_OFF_Y and G_OFF_Z)
getAngle
args:
int axis - which axis to use (0 for roll (x), 1 for pitch (Y) and
2 for yaw (Z)) float *results - pointer to the variable where
the angle will be stored description:
gets the current combined (accelerometer and gyroscope) angle
NOTE: the yaw axis will return 0 unless 'calc_yaw' i set to true
- See Parameters
Parameters:
calc_yaw
type: bool description:
set this to true when you want to calculate the angle around the
yaw axis (remember to change this back to false after taking the yaw readings to prevent gyroscope drift)
this is used to prevent drift because only the gyroscope is used
to calculate the yaw rotation
Copyright (c) 2019, Alex Mous Licensed under the Creative Commons
Attribution-ShareAlike 4.0 International (CC-BY-4.0)
After running the example, the angles seem to be incrementally changing each read when the MPU6050 is stationary.
They seem to wrap around (e.g. here between -27 to -21).
I thought perhaps setting calc_yaw to false might help but same issue. I have also disconnected any other devices but still see the same issue.
I am using a raspberry pi 3b.
Below is the output from the example file:
Current angle around the roll axis: -25.4695
Current angle around the pitch axis: -25.9261
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.0986
Current angle around the pitch axis: -26.9491
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.727
Current angle around the pitch axis: -27.9685
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.3882
Current angle around the pitch axis: -22.6544
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.0298
Current angle around the pitch axis: -23.6461
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.6671
Current angle around the pitch axis: -24.6364
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.2543
Current angle around the pitch axis: -25.7628
Current angle around the yaw axis: 0
Current angle around the roll axis: -22.8914
Current angle around the pitch axis: -26.7387
Current angle around the yaw axis: 0
Current angle around the roll axis: -22.5377
Current angle around the pitch axis: -27.7226
Current angle around the yaw axis: 0
Current angle around the roll axis: -22.1756
Current angle around the pitch axis: -22.6529
Current angle around the yaw axis: 0
Current angle around the roll axis: -21.8173
Current angle around the pitch axis: -23.6487
Current angle around the yaw axis: 0
Current angle around the roll axis: -21.4295
Current angle around the pitch axis: -24.7079
Current angle around the yaw axis: 0
Current angle around the roll axis: -21.0691
Current angle around the pitch axis: -25.7156
Current angle around the yaw axis: 0
Current angle around the roll axis: -26.6888
Current angle around the pitch axis: -26.7705
Current angle around the yaw axis: 0
Current angle around the roll axis: -26.3078
Current angle around the pitch axis: -27.825
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.9559
Current angle around the pitch axis: -22.6705
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.5976
Current angle around the pitch axis: -23.6501
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.227
Current angle around the pitch axis: -24.669
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.8443
Current angle around the pitch axis: -25.7288
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.5081
Current angle around the pitch axis: -26.6652
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.1208
Current angle around the pitch axis: -27.7215
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.7356
Current angle around the pitch axis: -22.6929
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.3655
Current angle around the pitch axis: -23.7046
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.0154
Current angle around the pitch axis: -24.6873
Current angle around the yaw axis: 0
Current angle around the roll axis: -22.6472
Current angle around the pitch axis: -25.7079
Current angle around the yaw axis: 0
Current angle around the roll axis: -22.2735
Current angle around the pitch axis: -26.7372
Current angle around the yaw axis: 0
Current angle around the roll axis: -21.9365
Current angle around the pitch axis: -27.6659
Current angle around the yaw axis: 0
Current angle around the roll axis: -21.5539
Current angle around the pitch axis: -22.5427
Current angle around the yaw axis: 0
Current angle around the roll axis: -21.1856
Current angle around the pitch axis: -23.5513
Current angle around the yaw axis: 0
Current angle around the roll axis: -26.9229
Current angle around the pitch axis: -24.5942
Current angle around the yaw axis: 0
Current angle around the roll axis: -26.5378
Current angle around the pitch axis: -25.6452
Current angle around the yaw axis: 0
Current angle around the roll axis: -26.181
Current angle around the pitch axis: -26.6323
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.8149
Current angle around the pitch axis: -27.639
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.4375
Current angle around the pitch axis: -22.5403
Current angle around the yaw axis: 0
Current angle around the roll axis: -25.1064
Current angle around the pitch axis: -23.4519
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.7316
Current angle around the pitch axis: -24.4912
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.3729
Current angle around the pitch axis: -25.4876
Current angle around the yaw axis: 0
Current angle around the roll axis: -24.0183
Current angle around the pitch axis: -26.4669
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.6153
Current angle around the pitch axis: -27.593
Current angle around the yaw axis: 0
Current angle around the roll axis: -23.2599
Current angle around the pitch axis: -22.4995
Current angle around the yaw axis: 0
Accelerometer Readings: X: 0.326, Y: 0.41, Z: 0.804
Gyroscope Readings: X: 1.412, Y: -4.069, Z: 1.267
The header file should include the i2c library for pi with "c" external identifier like below: extern "c"{ #include <linux/i2c-dev> }
Or the compiler will be unable to find and include the c library in a cpp program.(errors below:
g++ -fPIC -c MPU6050.cpp -o MPU6050.o
MPU6050.cpp: In constructor ‘MPU6050::MPU6050(int8_t)’:
MPU6050.cpp:37:51: error: ‘i2c_smbus_write_byte_data’ was not declared in this scope
i2c_smbus_write_byte_data(f_dev, 0x6b, 0b00000000); //Take MPU6050 out of sleep mode - see Register Map
^
MPU6050.cpp: In member function ‘void MPU6050::getGyroRaw(float, float, float)’:
MPU6050.cpp:54:50: error: ‘i2c_smbus_read_byte_data’ was not declared in this scope
int16_t X = i2c_smbus_read_byte_data(f_dev, 0x43) << 8 | i2c_smbus_read_byte_data(f_dev, 0x44); //Read X registers
^
MPU6050.cpp: In member function ‘void MPU6050::getAccelRaw(float, float, float)’:
MPU6050.cpp:70:50: error: ‘i2c_smbus_read_byte_data’ was not declared in this scope
int16_t X = i2c_smbus_read_byte_data(f_dev, 0x3b) << 8 | i2c_smbus_read_byte_data(f_dev, 0x3c); //Read X registers
^
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1
I installed libi2c-dev, i2c-tools, and libi2c0.
when i try to compile it with:
g++ -o MPU6050 MPU6050.o -L. libMPU6050.so
it returns:
/usr/bin/ld: MPU6050.o: undefined reference to symbol 'i2c_smbus_write_byte_data'
/usr/bin/ld: //lib/arm-linux-gnueabihf/libi2c.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
i also ran the makefile.
Raspberry lite os.
I think i messed up somewhere but i cant tell where. I am sorry if this isnt a real issue.