Git Product home page Git Product logo

alex-mous / mpu6050-c-cpp-library-for-raspberry-pi Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 17.0 28 KB

A library for using the MPU6050 accelerometer and gyroscope module with Raspberry Pi to get both raw values and filtered angles on all axes

License: Creative Commons Attribution Share Alike 4.0 International

C++ 96.21% Makefile 3.79%
mpu6050 c cpp raspberry-pi accelerometer-and-gyroscope complementary-filter imu raspberry-pi-library

mpu6050-c-cpp-library-for-raspberry-pi's Introduction

MPU6050 Accelerometer and Gyroscope C++ library


Description


    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)



mpu6050-c-cpp-library-for-raspberry-pi's People

Contributors

alex-mous avatar jmrhvl avatar scottpedia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mpu6050-c-cpp-library-for-raspberry-pi's Issues

Angles are drifting

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

thanks in advance

Modify the header file to fix the error during compilation

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

Accelerometer Readings: X are constant & don't change

All values from accelerometer y,z & gyro x,y,z change as expected when the MPU6050 is moved, except the accelerometer X values.

This is reproducible in the original "Example.cpp" that is provided by the repo:
image

I was also able to replicate this odd behavior via my own code:

image

Im running the code on Raspberri Pi Zero W, with OS:

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"

Please look into this bug.

Referenced a macro name is different

Hi,

I think it should refer to ACCEL_RANGE instead of GYRO_RANGE.

line 72-85 in MPU6050.h

#if GYRO_RANGE == 1
	#define ACCEL_SENS 8192.0
	#define ACCEL_CONFIG 0b00001000
#elif GYRO_RANGE == 2
	#define ACCEL_SENS 4096.0
	#define ACCEL_CONFIG 0b00010000
#elif GYRO_RANGE == 3
	#define ACCEL_SENS 2048.0
	#define ACCEL_CONFIG 0b00011000
#else //Otherwise, default to 0
	#define ACCEL_SENS 16384.0
	#define ACCEL_CONFIG 0b00000000
#endif
#undef GYRO_RANGE

Should be fixed as follows

#if ACCEL_RANGE == 1
	#define ACCEL_SENS 8192.0
	#define ACCEL_CONFIG 0b00001000
#elif ACCEL_RANGE == 2
	#define ACCEL_SENS 4096.0
	#define ACCEL_CONFIG 0b00010000
#elif ACCEL_RANGE == 3
	#define ACCEL_SENS 2048.0
	#define ACCEL_CONFIG 0b00011000
#else //Otherwise, default to 0
	#define ACCEL_SENS 16384.0
	#define ACCEL_CONFIG 0b00000000
#endif
#undef ACCEL_RANGE

MPU6050.o: undefined reference to symbol 'i2c_smbus_write_byte_data'

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.

undefined reference to `i2c_smbus_write_byte_data

Hi !
I'm trying to compile the example in your library, but I get the following error:

make example g++ Example.cpp -o Example -lMPU6050 -pthread /usr/bin/ld: /usr/lib/gcc/arm-linux-gnueabihf/8/../../../libMPU6050.so: undefined reference toi2c_smbus_write_byte_data(int, unsigned char, unsigned char)'
/usr/bin/ld: /usr/lib/gcc/arm-linux-gnueabihf/8/../../../libMPU6050.so: undefined reference to i2c_smbus_read_byte_data(int, unsigned char)' collect2: error: ld returned 1 exit status make: *** [Makefile:15: example] Error 1

Compiling and installing the library went well, without any issues...

I've installed libi2c-dev

How can I solve this ?

Thank you

Can't run the Example.cpp file

I tried to run Example.cpp file with the command:
sudo ./Example.cpp
and the error message was:
sudo: ./Example.cpp: command not found

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.