MPU6050-C-CPP-Library-for-Raspberry-Pi icon indicating copy to clipboard operation
MPU6050-C-CPP-Library-for-Raspberry-Pi copied to clipboard

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

MPU6050 Accelerometer and Gyroscope C++ library


Description


&nbsp&nbsp&nbsp&nbspThis is a basic control library for using the MPU6050 accelerometer and gyroscope module with Raspberry Pi using i2c protocol
&nbsp&nbsp&nbsp&nbspIt 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

&nbsp&nbsp&nbsp&nbsp

__constructor__ (MPU6050)

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspint8_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")
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspsets up i2c device and starts loop to read the angle
&nbsp&nbsp&nbsp&nbsp

getAccelRaw

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *x - pointer to the variable where the X axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *y - pointer to the variable where the Y axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *z - pointer to the variable where the Z axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspgets the raw accelerometer values from the MPU6050 registers
&nbsp&nbsp&nbsp&nbsp

getAccel

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *x - pointer to the variable where the X axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *y - pointer to the variable where the Y axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbpfloat *z - pointer to the variable where the Z axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspgets the accelerometer values (in g), rounded to three decimal places
&nbsp&nbsp&nbsp&nbsp

getGyroRaw

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *roll - pointer to the variable where the roll (X) axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *pitch - pointer to the variable where the pitch (Y) axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *yaw - pointer to the variable where the yaw (Z) axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspgets the raw gyroscope values from the MPU6050 registers
&nbspnbsp&nbsp&nbsp

getGyro

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *roll - pointer to the variable where the roll (X) axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *pitch - pointer to the variable where the pitch (Y) axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *yaw - pointer to the variable where the yaw (Z) axis results should be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspgets the gyroscope values (in degrees/second)
&nbsp&nbsp&nbsp&nbsp

getOffsets

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *ax_off - pointer to the variable where the accelerometer x axis offset will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nsp&nbsp&nbspfloat *ay_off - pointer to the variable where the accelerometer y axis offset will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *az_off - pointer to the variable where the accelerometer z axis offset will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *gr_off - pointer to the variable where the gyroscope roll axis offset will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *gp_off - pointer to the variable where the gyroscope pitch axis offset will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *gy_off - pointer to the variable where the gyroscope yaw axis offset will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfinds the offsets needed
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspbefore 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)
&nbsp&nbsp&nbsp&nbsp

getAngle

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspargs:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspint axis - which axis to use (0 for roll (x), 1 for pitch (Y) and 2 for yaw (Z))
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspfloat *results - pointer to the variable where the angle will be stored
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspgets the current combined (accelerometer and gyroscope) angle
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspNOTE: the yaw axis will return 0 unless 'calc_yaw' i set to true - See Parameters

Parameters:

&nbsp&nbsp&nbsp&nbsp

calc_yaw

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsptype:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspbool
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdescription:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspset 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)
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspthis 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)