Having received many positive emails about my Extended Kalman Filter Tutorial, I wanted to see whether I could write my own general-purpose EKF from scratch, suitable for running on a microcontroller like Arduino, Teensy, and the STM32 platform used on today's popular flight controllers (Pixhawk, Naze, CC3D).  I wanted something that could be easily modified as new sensors were added, but that didn't use dynamic memory allocation or other techniques that are impractical in an embedded environment.

The result is TinyEKF, a C/C++ EKF implementation that takes care of most of the EKF algorithm for you.  There is a C++ version for Arduino/Teensy, and a pure C version for STM32.  For both versions, you use #define to specify the number of state values N and the number of sensor measurements M.  You also provide a method to compute the output of the state-transition function f(x), the observation function h(x), and their respective Jacobian matrices F(x) and H(x).  As you'll see from the examples, this involves perhaps ten lines of code for each implementation.  TinyEKF takes care of the rest (prediction, update).  This implementation is naturally not as efficient as the hard-coded EKFs you see in actual flight controllers, but I am hoping that with today's increasingly fast ARM processors, and perhaps some behind-the-scenes optimizations, TinyEKF will become a useful, scaleable way of implementing your own EKF.

I've provided an Arduino example using sensor fusion of a BMP180 barometer / temperature sensor with an LM35 temperature sensor. For those who don't have an Arduino but want to try it out anyway, there's also a GPS example in C, using realistic satellite data stored in a .CSV spreadsheet.  Once I get my STM32 Nucleo board, I plan to  port the Arduino sensor fusion example to that platform as well.

E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones


  • Great Extended Kalman Filter Tutorial, I learnt a lot.

  • Thanks, Oleksandr!  The reason I did not use bold letters for vectors is that I could not figure out how to do this in MathJax, the system I used for typesetting the math notation in the tutorial.  I tried \matbf{x} and \bold{x}, but it looked just like the non-bold version. So I gave up trying to do it. If you know how I can do it the right way, please let me know.

  • It's the best Kalman Filter tutorial I ever seen (have I missed something?), thanks Simon! I only wonder is there any purpose not to use bold letters for vectors notation?
  • Great Kalman tutorial Simon, thanks a lot for doing it! Keep on the good work! 

  • I believe that Nucleo will take any Arduino standard (R3) shield.

  • Ok, that seems about right on the speed front.  So if a person wanted to overclock to 96Mhz, there is another 30%+ more performance to be had.  

    Just looked up the Nucleo boards.  That is an amazing amount of power for $10.  Physically larger than I would like for my flying devices, but for the price, I can be forgiving.  I am tempted to buy one and make a sensor+I/O shield for it.

  • Cool -- I'd definitely go with vim!

  • in the office i use both environments.

    the mbed offline programming variant is nice as you can use the editor you like.

  • STM32F401.  Still not unboxed.  

    I was planning to go with makefiles from the beginning, but I may play a bit with MBED since it looks so cool.

  • simon,

    which nucleo do you have and what environment are you going to use?

    i have quite a few nucleo's - even the 180mhz m4 variant.

This reply was deleted.