Lessons and the learning game

Sensor fusion and the Kalman filter

Learn the math behind fusing sensors, from variance to a two-state Kalman filter, and build an attitude display that learns its own gyro biases.

What the course teaches

Sensor fusion and the Kalman filter is seven lessons on the Uno, in four chapters. It follows Accelerometers, gyros and IMUs and builds on its complementary filter rather than repeating it.

  • Noise, measured: mean, standard deviation and variance, the 68-95 rule, the square-root law of averaging, and the noise and lag of a moving average and an EMA, measured with a step test.
  • From fusion to Kalman: the complementary filter derived (alpha from tau, the crossover frequency, the steady error bias x tau, the noise it passes), then the 1D Kalman filter worked step by step to its steady state, with an innovation test and a wrong R.
  • Kalman for real: the gallery's Kalman filter tilt angle (angle and gyro bias in one filter), and a GPS fused with a compass, including what a compass 10 degrees off does to it.
  • Boss build: an artificial horizon on an SSD1306 OLED, driven by two two-state Kalman filters written out in the sketch, with adaptive measurement noise.

Each chapter ends in a short quiz. Open it from /learn.

The circuits

One lesson opens the gallery's Kalman filter tilt angle with an MPU6050. The rest run sketches written for the course:

sketch parts what it does
kalnoise MPU6050 windows of 200 readings: mean, sd, variance, 68-95, and avg N
kalema MPU6050 a moving average and an EMA side by side, step, alpha and spike
kalcomp MPU6050 a complementary filter scored against the formulas, tau and bias
kal1d MPU6050 a scalar Kalman filter step by step, steady state, q
kalgps NEO-6M, MPU-9250 dead reckoning plus GPS fixes, scored against the truth, and ins
kalboss MPU6050, SSD1306 two-state filters that learn the gyro biases, and shake

Why the sketches add noise

The simulated sensors are perfect. Every sketch in this course adds seeded Gaussian noise and known biases on top of the clean reading, with the numbers as constants at the top, and keeps the clean reading as the truth to score against. That is what lets each lesson predict a number from a formula and then check it. A seeded generator means a run prints the same numbers every time.

Honest limits

The filters here are linear and assume Gaussian noise. 3D attitude is not linear, which is why drones run extended Kalman filters or Madgwick and Mahony filters on quaternions; a full inertial navigation system also integrates the accelerometers twice and needs GPS to stop the drift. A Kalman filter is only as good as the Q and R it is given.

Troubleshooting

  • A number is a little off the lesson's. A statistic from a few hundred noisy samples wanders by several percent; the lesson says how much.
  • The OLED updates slowly. One 1 KB frame over the Uno's I2C takes about a third of a second, so the sketch redraws once a second.
  • No GPS fixes. Check the module's TX goes to pin 4, and that its fix is on (click the module to toggle it).