A lot of people have asked how I got the readings from the Gyro, Accelerometer, and Compass inertial measurement unit(IMU) which is used to keep PiBBOT upright.
UPDATED And updated guide has been published here, with cleaner and simpler code.
In this guide I will explain how to get readings from the IMU and convert these raw readings into usable angles. I will also show how to read some of the information in the datasheets for these devices. The theory and principals below can be applied to any digital IMU, just some minor modifications need to be made. Eg register values, sensitivity level…
The code can be found here;
A note about Gyros and Accelerometers
When using the IMU to calculate angles, readings from both the gyro and accelerometer are needed, which are then combined. This is because using either on their own will result in inaccurate readings.
Here is why;
Gyros – A gyro measures the rate of rotation, which has to be tracked over time to calculate the current angle. This tracking causes the gyro to drift. However, gyros are good at measuring quick sharp movements.
Accelerometers – Accelerometers are used to sense both static (e.g. gravity) and dynamic (e.g. sudden starts/stops) acceleration. They don’t need to be tracked like a gyro and can measure the current angle at any given time. Accelerometers however are very noisy and are only useful for tracking angles over a long period of time.
Here is an excellent tutorial about accelerometers and gyros;
Setting up the IMU and I2C
The IMU I use is a MinIMU-9 v2 Gyro, Accelerometer, and Compass which uses a L3GD20 3-axis gyroscope and a LSM303DLHC 3-axis accelerometer and 3-axis magnetometer.
The datasheets are needed if you want to use these devices;
This IMU communicates via the I2C interface.
From the images below, you can see how to connect it to the Raspberry Pi. You can also see the orientation of the X, Y and Z axis.