0% found this document useful (0 votes)
222 views5 pages

Reading A IMU Without Kalman: The Complementary Filter

This document discusses using a complementary filter to process data from an inertial measurement unit (IMU) without using a more complex Kalman filter. It summarizes that gyroscope data is useful for short-term angular changes but prone to drift over time, while accelerometer data is useful for long-term angle measurements but noisy in the short-term. The complementary filter combines the two by giving more weight to the gyroscope for fast changes and the accelerometer for the overall mean value, keeping the measurement accurate in both the short and long-term without needing a complex Kalman filter implementation. An example implementation uses a Raspberry Pi to collect IMU data and applies the complementary filter to obtain pitch and roll angles.

Uploaded by

vromiko
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)
222 views5 pages

Reading A IMU Without Kalman: The Complementary Filter

This document discusses using a complementary filter to process data from an inertial measurement unit (IMU) without using a more complex Kalman filter. It summarizes that gyroscope data is useful for short-term angular changes but prone to drift over time, while accelerometer data is useful for long-term angle measurements but noisy in the short-term. The complementary filter combines the two by giving more weight to the gyroscope for fast changes and the accelerometer for the overall mean value, keeping the measurement accurate in both the short and long-term without needing a complex Kalman filter implementation. An example implementation uses a Raspberry Pi to collect IMU data and applies the complementary filter to obtain pitch and roll angles.

Uploaded by

vromiko
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/ 5

Reading a IMU Without Kalman: The Complementary

Submitted by Pieter-Jan on Fri, 26/04/2013 - 08:38

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

Very complex to understand.


Very hard, if not impossible, to implement on certain hardware (8-bit microcontroller etc.)

In this tutorial I will present a solution for both of these problems with another type of filter: the comp
even easier to implement.

Why do I need a filter?


Most IMU's have 6 DOF (Degrees Of Freedom). This means that there are 3 accelerometers, and 3 g
from a robotics class you might have taken, you might be fooled into thinking that the IMU will be abl
object it is attached to. This because they have told you that an object in free space has 6DOF. So if
Well ... not really. The sensor data is not good enough to be used in this way.

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 problem with accelerometers


As an accelerometer measures all forces that are working on the object, it will also see a lot more tha
the object will disturb our measurement completely. If we are working on an actuated system (like the q
be visible on the sensor as well. The accelerometer data is reliable only on the long term, so a "low pas

The problem with gyroscopes


In one of the previous articles I explained how to obtain the angular position by use of a gyroscop
measurement that was not susceptible to external forces. The less good news was that, because of the in
to drift, not returning to zero when the system went back to its original position. The gyroscope data
the long term.

The complementary filter


The complementary filter gives us a "best of both worlds" kind of deal. On the short term, we use the
not susceptible to external forces. On the long term, we use the data from the accelerometer, as it doe
follows:

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.

#define ACCELEROMETER_SENSITIVITY 8192.0


#define GYROSCOPE_SENSITIVITY 65.536

#define M_PI 3.14159265359

#define dt 0.01 // 10 ms sample rate!

void ComplementaryFilter(short accData[3], short gyrData[3], float *pitch, float *roll


{
float pitchAcc, rollAcc;

// Integrate the gyroscope data -> int(angularSpeed) = angle


*pitch += ((float)gyrData[0] / GYROSCOPE_SENSITIVITY) * dt; // Angle around the X-axis
*roll -= ((float)gyrData[1] / GYROSCOPE_SENSITIVITY) * dt; // Angle around the Y-axis

// Compensate for drift with accelerometer data if !bullshit


// Sensitivity = -2 to 2 G at 16Bit -> 2G = 32768 && 0.5G = 8192
int forceMagnitudeApprox = abs(accData[0]) + abs(accData[1]) + abs(accData[2]);
if (forceMagnitudeApprox > 8192 && forceMagnitudeApprox < 32768)
{
// Turning around the X axis results in a vector on the Y-axis
pitchAcc = atan2f((float)accData[1], (float)accData[2]) * 180 / M_PI;
*pitch = *pitch * 0.98 + pitchAcc * 0.02;

// Turning around the Y axis results in a vector on the X-axis


rollAcc = atan2f((float)accData[0], (float)accData[2]) * 180 / M_PI;
*roll = *roll * 0.98 + rollAcc * 0.02;
}
}

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.

Hope this article was of any help.

Tags:
Complementary Filter Gyroscope Accelerometer IMU Quadrocopter

You might also like