0% found this document useful (0 votes)
219 views8 pages

MPU6050 Light Library Documentation: Romain JL Fetick January 2021

The document describes an Arduino library for communicating with the MPU6050 device to retrieve acceleration, gyroscopic, and temperature measurements. It computes angles from the raw sensor data using two methods: integrating the gyroscope readings and estimating from acceleration. The library uses a complementary filter to fuse the angle estimates from both methods for a more accurate overall angle measurement. It provides functions for initializing and configuring the sensor, updating measurements, and accessing the sensor data and computed angles.

Uploaded by

Euler Torres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views8 pages

MPU6050 Light Library Documentation: Romain JL Fetick January 2021

The document describes an Arduino library for communicating with the MPU6050 device to retrieve acceleration, gyroscopic, and temperature measurements. It computes angles from the raw sensor data using two methods: integrating the gyroscope readings and estimating from acceleration. The library uses a complementary filter to fuse the angle estimates from both methods for a more accurate overall angle measurement. It provides functions for initializing and configuring the sensor, updating measurements, and accessing the sensor data and computed angles.

Uploaded by

Euler Torres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MPU6050 light

library documentation
Romain JL Fetick
January 2021

Abstract
The MPU6050 light library is made for your Arduino to communi-
cate with the MPU6050 device. It retrieves the MPU6050 acceleration,
gyroscopic and temperature measurements. The angles of the device are
computed from the raw measurements. The tilt angles are computed by
a complementary filter between acceleration and gyroscopic data, thus
providing quite a good accuracy of estimation.

Contents
1 Introduction 2

2 Mathematical expressions 2
2.1 Angle from gyroscopic measurement . . . . . . . . . . . . . . . . 2
2.2 Angle from accelerometric measurement . . . . . . . . . . . . . 3
2.3 Data fusion with complementary filter . . . . . . . . . . . . . . 4

3 Public attributes 4

4 Methods of the MPU6050 class 4


4.1 Constructor and initialisation . . . . . . . . . . . . . . . . . . . 4
4.2 Configuration setters . . . . . . . . . . . . . . . . . . . . . . . . 5
4.3 Configuration getter . . . . . . . . . . . . . . . . . . . . . . . . 5
4.4 Update measurement . . . . . . . . . . . . . . . . . . . . . . . . 5
4.5 Data getters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

5 Examples 7

6 Authors and license 8

1
1 Introduction
The MPU6050 delivers three angular speeds and three linear accelerations.
This raw data is directly accessible through the MPU6050 light library. More-
over the library performs basic angle computation from this data. Indeed an
integration of the angular speed gives the angles. However any small bias in
the angular speed measurement is integrated and leads to a severe drift of the
estimated angle. This must be corrected using another information, that is
provided by the accelerometer. The gravity is supposed to be the main com-
ponent of the acceleration, so a projection of the gravity on the MPU6050 axis
gives a rough (and noisy) estimate of the tilt angles of the device.

Merging the angles estimated from gyroscopic and accelerometric data pro-
vides a good estimation of the MPU6050 angles. A complementary filter per-
forms the merging of the two sources. For this library, the gyroscopic data is
weighted with a 0.98 factor, whereas the accelerometric data is weighted with
the complementary 0.02 factor. This factor can be fine tuned by the user. The
main hypothesises to ensure the consistency of the angle estimation are

• Small loop delay between to calls to the update( ) function, so the ap-
proximation made for the numerical integration of the angular speed
holds θg [i] ' θg [i − 1] + θ̇g [i] · dt

• Small linear accelerations (the gravity is the main one). This constraint
can be loosened since the complementary filter gives few confidence in
angles computed from acceleration (only 2% of confidence as default
value)

• Small tilt angles (due to approximations in the trigonometric equations)

2 Mathematical expressions
The MPU6050 provides acceleration and gyroscopic measurement on the three
axis X,Y and Z of the device. These axis have to be differentiated from the
X0 , Y0 and Z0 reference (North-West-Zenith) axis.

2.1 Angle from gyroscopic measurement


The angle around the X axis is found by integrating the gyroscopic data θ̇x
over a small amount of time δt such as

θx (t + δt) ' θx (t) + θ̇x · δt (1)

The same equations hold for the Y and Z axis.

2
Figure 1: Overview of the data accessible through the MPU6050 light library.
The raw data from the MPU6050 is the linear acceleration (green) and angular
speed (red) for the three axis. Computations allow to derive the angles of
the MPU6050. Angles computed from acceleration are noisy. The angles
computed from data fusion between accelerometric and gyroscopic data are
more accurate.

2.2 Angle from accelerometric measurement


2.2.1 Pitch and roll angles
The transformation matrix from Earth frame to MPU6050 frame is
 
cos α 0 sin α
R =  sin α sin β cos β − cos α sin β  (2)
− sin α cos β sin β cos α cos β

with a first rotation α (pitch) around the Earth Y0 axis, and then a rotation
β (roll) around the device X axis. Since the roll is around the MPU6050 axis,
we directly get the identity β = θx . It will be replaced in the following.

The gravity vector in the MPU6050 frame reads


     
ax 0 sin α
ay  = R ·  0  = −g − cos α sin θx  (3)
az −g cos α cos θx

One can express the angles as function of the accelerometric measurements


a sin α
p 2x 2 = (4)
ay + az | cos α|

and
ay
= − tan θx (5)
az

3
2.2.2 Tilt angles
The tilt angles are given such as
ay
tan φx = sgn(az ) p (6)
a2x + a2z

and
ax
tan φy = p (7)
a2y + a2z
We note that the tilt angle around Y0 corresponds to the pitch of the device,
we directly get the identity φx = α.

2.3 Data fusion with complementary filter


The angles estimated from gyroscopic and accelerometric data are merged
together using a complementary filter.

3 Public attributes
The upsideDownMounting is the only accessible (public) attribute of the
MPU6050 class. Setting it to true allows to make the library to work properly
when the device is mounted upside down as if it was upside up. Default value:
false.

4 Methods of the MPU6050 class


4.1 Constructor and initialisation
MPU6050( TwoWire &w):
Constructor. Needs to be called with Wire.

begin( int gyroConfig=1, int accConfig=0): void


Start communication with the MPU6050 device. User may choose a gyroscope
configuration (from 0 to 3) and accelerometer configuration (from 0 to 3) that
set the sensitivity and working range of the device. The configuration list can
be found on the Invensense website.
The function returns a byte to check the success of the connection (0=success).

calcOffsets( bool calcGyro=true, bool calcAcc=true): void


Compute gyroscope and accelerometer offsets to remove measurement bias.
MPU6050 device must be on a flat surface during the calibration.

4
4.2 Configuration setters
setGyroOffsets( float x, float y, float z): void
Manually set gyroscope offsets if you already know them. Otherwise use the
method calcOffsets( ).

setAccOffsets( float x, float y, float z): void


Manually set accelerometer offsets if you already know them. Otherwise use
the method calcOffsets( ).

setFilterGyroCoef( float fg): void


Set the gyroscope coefficient for the complementary filter. The coefficient must
be between 0 (accelero data only) and 1 (gyro data only). Recommended value
fg = 0.98. This function automatically sets the accelerometer coefficient to
fa = 1 − fg .

setFilterAccCoef( float fa): void


Set the accelerometer coefficient for the complementary filter. The coefficient
must be between 0 (gyro data only) and 1 (accelero data only). Recommended
value fa = 0.02. This function automatically sets the gyroscope coefficient to
fg = 1 − fa .

4.3 Configuration getter


getAccXoffset[,Y,Z]( ): float
Offset on the accelerometer data on the X axis.
Units: data is given as multiple of the gravity norm g = 9.81 m.s−2
Note: similar functions exist on Y and Z axis.

getGyroXoffset[,Y,Z]( ): float
Offset on the gyroscope around the X axis.
Units: data is given in degrees per second.
Note: similar functions exist on Y and Z axis.

4.4 Update measurement


update( ): void
Update data. This function must be called in the loop as often as possible
to get consistent angleX, angleY and angleZ. Indeed these angles rely on the
integration of the gyroscope data between two updates, a longer delay leads
to a less accurate integration.

5
4.5 Data getters
getTemp( ): float
Last measurement of the device temperature.
Units: data is given in Celsius degrees.

getAccX[,Y,Z]( ): float
Last measurement of the acceleration on the X axis.
Units: data is given as multiple of the gravity norm g = 9.81 m.s−2
Note: similar functions exist on Y and Z axis.

getGyroX[,Y,Z]( ): float
Last measurement of the angular speed around the X axis.
Units: data is given in degrees per second.
Note: similar functions exist on Y and Z axis.

getAccAngleX[,Y]( ): float
Estimated angle around the X axis, computed with the accelerometer data
(tilt of the gravity vector). This data is noisy, and is valid only for small linear
accelerations (the gravity is the major acceleration). It is better to use the
angle given by the getAngleX( ) function.
Units: data is given in degrees.
Note: a similar function exists on Y axis.

getAngleX[,Y,Z]( ): float
Estimated angle around the X axis. The angle is computed through a com-
plementary filter merging data from accelerometer and gyroscope integration.
This might be the best estimate of the angles provided by this library.
Units: data is given in degrees.
Note: similar functions exist on Y and Z axis.

6
5 Examples
The minimal code below shows how to initialise and retrieve some data from
the MPU6050 device.
#i n c l u d e ”Wire . h”
#i n c l u d e <MPU6050 light . h>
MPU6050 mpu( Wire ) ;

void setup ( ) {
Wire . b e g i n ( ) ;
mpu . b e g i n ( ) ;
mpu . c a l c O f f s e t s ( ) ;
}

void loop ( ) {
mpu . update ( ) ;
f l o a t a n g l e [ 3 ] = {mpu . getAngleX ( ) ,
mpu . getAngleY ( ) ,
mpu . getAngleZ ( ) } ;
f l o a t gyro [ 3 ] = {mpu . getGyroX ( ) ,
mpu . getGyroY ( ) ,
mpu . getGyroZ ( ) } ;
// p r o c e s s t h i s data f o r your needs . . .
// ( do something . . . )
}

The setup can be fine tuned with the following commands


void setup ( ) {
Wire . b e g i n ( ) ;
// d e f i n e gyro and a c c e l e r o c o n f i g u r a t i o n
// t u n i n g s e n s i t i v i t y vs range ( from 0 t o 3 )
byte s t a t u s = mpu . b e g i n ( 1 , 0 ) ;
// i f i n i t i a l i s a t i o n e r r o r −> s t o p
w h i l e ( s t a t u s !=0){ }
// d e f i n e which o f f s e t ( gyro , a c c e l e r o ) t o compute
mpu . c a l c O f f s e t s ( t r u e , t r u e ) ;
// d e f i n e custom complementary f i l t e r ( from 0 . 0 t o 1 . 0 )
mpu . s e t F i l t e r G y r o C o e f ( 0 . 9 8 ) ;
}

More examples can be found in the dedicated ”examples” folder of the


library. You may have a look at them in order to get started with the library.

7
6 Authors and license
• RFETICK (github.com/rfetick): modifications and documentation

• TOCKN (github.com/tockn): initial author (forked from v1.5.2)

The MPU6050 light library is provided under the MIT license. Please check
the LICENSE file included with the library for more information.

You might also like