# Kalman filter vs Complementary filter

A comparison between Kalman and Complementary filter

The use of accelerometer and gyroscope to build little robots, such as the self-balancing, requires a math filter in order to merge the signals returned by the sensors.

The gyroscope has a drift and in a few time the values returned are completely wrong. The accelerometer, from the other side, while returns a true value when the acceleration is progressive, it suffers much the vibrations, returning values ​​of the angle wrong.

To filter the signals it is used a filter, usually th Kalman filter (here), very complex from math point of view. There is also a simple filter, called Complementary filter (here) that could be used.

But could the results of kalman and complementary filter really compared? This is a picture with the kalman filter (green), first order complemetary filter  (black) and second order complementary filter (yellow)

In my opinion the complementary filter can substitue the Kalaman filter. It is more easy, more fast. The Kalman filter is the best filter, also from the theorical point of view, but the its complexity is too much for a hobbist robot builder.

The code, other pics, details here:

http://robottini.altervista.org/kalman-filter-vs-complementary-filter

## Comment viewing options

"In my opinion the complementary filter can substitue the Kalaman filter."

Do you believe this to be true for all applications?  I think systems demanding higher precision than others such as quadrocopters could possibly suffer from using a complementary filter for inclination measurements causing system instability.

It sounds like a complementary filter does have a great tradeoff for processing speed, but as seen in your plots of a second order complementary filter it exhibits more noise.  Thanks for sharing!

^^ I don't think computational complexity is the problem. Kalman filters are almost always ran in real time.

right, processing speed -> ease of implementation?

Hi,

I tried to post this comment on your blog, but it always gives me following error: "Error 1: Click back and type in the password". So I will just post my comment here:

Nice job!

I have used the same kalman algorithm for my balancing robot: http://blog.tkjelectronics.dk/2012/03/the-balancing-robot/, but I have actual never tried to tune the noise covariance constants. I can determine by the picture, that we have the same IMU. I would really like to know if you make any further investigations and find the "perfect" constants both for the kalman- and the complimentary filter, so I don't have to do the same :)

You can contact me by email at: lauszus at gmail dot com.

Best Regards

Lauszus

Hello Lauszus, your balancing bot is awesome!!! For me it is very interesting the encoders usage. I have to study your code. I bought two encoders, but never used.

I tried to find the 'perfect' constants but I sow it is very difficult to achieve!  So the answer is negative. In next months I would to build a new balancing bot, so I can try to find the best constants.

Thank you very much - I'm also very pleased with it.

I have actually ported the code to Arduino, if you interrested: https://github.com/TKJElectronics/BalancingRobotArduino.

It's actually very easy to use encoders, take a look at the following line if you are using Arduino: https://github.com/TKJElectronics/BalancingRobotArduino/blob/master/BalancingRobotArduino.ino#L407 and the following class if you are using the mbed board: https://github.com/TKJElectronics/BalancingRobot/blob/master/Encoder.h

Okay, let me how your balancing robot works out :)