Hello Lauszus and others!

I too am making a balancing bot with PID controlling (I will get a Bachelors degree for it!) and good PID loop relies on good input data so I tried out your sample sketch for ADXL/ITG combo (got mine of dealextreme for pennies). For some reason, the Kalman filter seemed to be too reliant on accelerometer so the output data was rather noisy for me and jumped all over the place during rotation.

First column is Gyro, second is Accelerometer, third is Kalman and fourth is Complementary. As you can see, Kalman pretty much follows the accelerometer. Since I could not figure this out, I gave up on Kalman and looked into ways to improve complementary filter and I did succeed in this!

First I changed tuning to 99.7 and 0.03, which improved the stability a bit. Second, I implemented two instances of running median filters to smooth out spikes in raw data - I had problems with large spikes coming in from both gyro and accelerometer, gyro ones making the result drift more than 10 degrees ever so often and afterwards rolling back to right results over the course of several seconds - unacceptable. Sudden odd spikes from the accelerometer on the other hand were compromising the static stability, with fast jumps of +/- 1.5 degrees.

Now, the simple median filter works by looking at three readings and organizing them from smallest to biggest value, returning the middle one; so if we get three readings i.e. 190; 25; 192 it would be simple to see which of them is the bogus one and by rearranging them to 25; 190; 192 the 190 would be pretty much on spot. Unless we get more than one spike in a row, simple median filter will do the trick just fine. And since it is rolling, the correct value of 192 will also pass - it wont be omitted permanently, just used in the next calculation! If it is right, it will pass. But you can use more elaborate median filters with i.e 13 data points also, there is a lot written about implementing it on the web.

This is more precise than rolling average filter, which would've given us the result of 135.66. I also tried that and I also tried running complementary filter's result through Kalman, the second integer being gyro rate.

I will stick by the improved complementary from now on, saves up a few process cycles for me!

The microcontroller I am running my sketch on for now is a humble, honest Arduino Micro. And the data looks damn sexy on that Processing graph

PS: code is attached to this post! I left Kalman in for reference. If it works on your processor just as well as it should and is up par with complementary or even better then good, it is something I did wrong then