We have updated to the python code in our git repo.

It now includes;

The elusive Kalman filter.

Math needed when the IMU is upside down

Automatically calculate loop period.

A lot more comments.

What is a Kalman filter? In a nutshell; A Kalman filter is, it is an algorithm which uses a series of measurements observed over time, in this context an accelerometer and a gyroscope. These measurements will contain noise that will contribute to the error of the measurement. The Kalman filter will then try to estimate the state of the system, based on the current and previous states, that tend to be more precise that than the measurements alone.

A Kalman filter is more precise than a Complementary filter. This can be seen in the image below, which is the output of a complementary filter (CFangleX) and a Kalman filter (kalmanX) from the X axis plotted in a graph.

The red line (KalmanX) is better at filtering out noisep;

The code can be found here in our Git repository here
And can be pulled down to your Raspberry Pi with;