Reading A IMU Without Kalman: The Complementary Filter
Reading A IMU Without Kalman: The Complementary Filter
These days, IMU's (Intertial Measurement Units) are used everywhere. They are e.g. the sensors that
your mobile phone. This can be very useful for automatic screen tilting etc. The reason I am interested
quadrocopter. If you are here for another reason, this is not a problem as this tutorial will apply for ever
When looking for the best way to make use of a IMU-sensor, thus combine the accelerometer and gy
very powerful but complex Kalman filter. However the Kalman filter is great, there are 2 big problems
In this tutorial I will present a solution for both of these problems with another type of filter: the comp
even easier to implement.
We will use both the accelerometer and gyroscope data for the same purpose: obtaining the angular
integrating the angular velocity over time, as was explained in a previous article. To obtain the ang
determine the position of the gravity vector (g-force) which is always visible on the accelerometer. Thi
these cases, there is a big problem, which makes the data very hard to use without filter.
The gyroscope data is integrated every timestep with the current angle value. After this it is combined
processed with atan2). The constants (0.98 and 0.02) have to add up to 1 but can of course be changed
I implemented this filter on a Raspberry Pi using a MPU6050 IMU. I will not discuss how to read da
want the source code). The implementation of the filter is shown in the code snippet below. As you can
The function "ComplementaryFilter" has to be used in a infinite loop. Every iteration the pitch and
values by means of integration over time. The filter then checks if the magnitude of the force seen by
the real g-force vector. If the value is too small or too big, we know for sure that it is a disturbance
update the pitch and roll angles with the accelerometer data by taking 98% of the current value, and a
This will ensure that the measurement won't drift, but that it will be very accurate on the short term.
It should be noted that this code snippet is only an example, and should not be copy pasted as it will pr
settings as me.
If we use the sweetness of having a real computer (Raspberry Pi) collecting our data, we can easily
filter (red) follows the gyroscope (blue) for fast changes, but keeps following the mean value of the acc
the noisy accelerometer data and not drifting away eiter.
The filter is very easy and light to implement making it perfect for embedded systems. It should be note
the angle will never be very big. The atan2 function can then be approximated by using a small angle a
an 8 bit system.
Tags:
Complementary Filter Gyroscope Accelerometer IMU Quadrocopter