Hands-on with RPi and MPU9250 Part 3

Niranjan Reddy
5 min readSep 19, 2020

--

Obtaining orientation from MPU9250

MPU9250 sensor with mems accelerometer, gyroscope and magnetometer. It also has an inbuilt temperature sensor

In this article we will interface MPU9250 with Raspeberry pi and see some of the theory that we have discussed in the previous two articles in action. If you haven’t read the previous articles, here is the link to them Part-1 and Part-2

We will start by figuring out the communication between MPU9250 and rpi. The MPU9250 sensor supports two communications namely I2C and SPI. Both of these use clock for high data rates. We can not fully say one communication is better than the other, its just that they are used in different situations. More on that in this link.

We will be using I2C communication protocol and we will be using smbus library in python for handling the communication. Apart from that lets talk about the connections that we will have to go through between MPU9250 and RPi.

Connections from rpi to MPU9250
Raspberry pi 3 pin out.

Note: Once you have made the connections, just make sure that your sensor is not close to any battery source or any high current carrying wires. These wires could distort the magnetic sensor and give strange yaw values.

Basic Usage

Before you directly run the below code, you will have to enable I2C communications on your rpi and install the smbus library. Use this link and link(or run this command “pip install smbus2”).

After this install the imusensor package, which has all the functionality related to calibration and various filters(also it is coded by me so if you have any issues, I can fix them). The lib uses python 3.

pip install imusensor
if the above command doesn't work try
pip3 install imusensor

The log of these values would be noisy and inaccurate. For them to make sense you will have to follow the next topic of calibration.

Calibrating your sensor

Though most sensors are calibrated in the factory itself, it is still important to calibrate sensors again according to your new environment. It is best to calibrate magnetometer last after accelerometer and gyroscope as these depend in the calibration of magnetometer.

Steps for gyroscope

Just place the sensor in stand still position till it takes values and averages the bias error.

Steps for Accelerometer

For this you will have to place the accelerometer in 6 different positions and for more information, have a look at article 1.

Steps for Magnetometer

When this is happening, it is important to move the sensor in an eight shape and make sure every roll and pitch are covered in this motion. For more information on theory see article 1 and to know if you have magnetometer is calibrated or not check out this code.

The below code automates the complete process, however you will still have to move the sensor accordingly —

You will have to do this tiring process only once and can save the calibration values in a file. Later, you can just load the values like the below code.

If you have done the calibration properly, you will see decent values like the acceleration will be 10 only in one direction. Now that we have proper values, lets see how to feed them into the filters.

Applying Filters

Before we directly feed the values to kalman or madgwick filter, we need to make sure that all our three sensors accelerometer, gyroscope and magnetometer are aligned properly. Seeing the figure of their placement from MPU9250 datasheet, they don’t seem to be aligned.

There is no alignment across the sensors. credits: link

We will have to transform the sensor values to one orientation, so that there is uniformity. We normally have two conventions → NED(North-X-axis, East-Y-axis and Down-Zaxis) and ENU(East-X-axis, North-Y-axis and Up-Z-axis).

I have used NED convention and hence accordingly transformed the accelerometer and gyroscope axes according to magnetometer axes.

Acc X Y Z tranformed to Y X -Z
Gyro X Y Z tranformed to Y X -Z

If you want to follow a different convention, you can simply apply this on the following variables

imu.AccelVals or imu.GyroVals or imu.MagVals

Use the below code to run the kalman example

For Madgwick, there is one important thing to consider. As madgwick operates by gradient descent, it is crucial for the filter to run at least 4–5 times faster than the sampling frequency of sensor values. As madgwick updates more often, some of approximations would make sense and the orientation obtained is smooth. In the below code, I run madgwick 10 times faster than the sensor sampling frequency.

Visualization

The last part that is the visualization of the imu orientation on your computer. The way this is done is by sending the imu values in real time to your computer over wifi and then displaying the orientation in openGL. We will be using a package zmq that works on publisher and scriber principle

pip install zmq # needs to be done on both computer and rpi
pip install pygame # for the viz, install only on computer
pip install PyOpenGL # for the viz, install only on computer

Thanks to sentdex for some amazing videos on openGL, the viz was inspired from his tutorials. Here’s a link to his videos.

Just make sure that both your computer and rpi are on the same network. Also place the IP address of your RPi in the code above.

The below code shows the computer side of the code which is basically the subscriber of all the messages of RPi.

Conclusion

As we have covered from theory to practicals, there is still a lot we can explore with the applications of it. Currently, I am working on the robotic application of it and will come up with one more blog on that. I would like to know if you have any cool ideas about its applications in DIY projects or work or something different.

Till then Sayonara

--

--

Niranjan Reddy

Robotics, Statistics, ML and CV deployment on hardware.