Data logging and Kalman Filtering

Matt again! Our copter project is alive again and making excellent progress. I’ve finished my last year of marching band, something I already miss dearly, but one upside is that I’ve got enough time to get back to quadcopter coding.

Last Sunday, we made the decision to abandon our homemade serial shifter and continue to use Sparkfun’s. I won’t get my one touch programming, but thats obviously not terribly necessary to the actual quad. We put serial headers back on the board, and now we’re back in business for programming.

Over the past few days I’ve improved the visualization capabilities of “QuadComm,” something I don’t think I’ve shown yet on the blog. It can now display real time graphs of numbers received over the serial connection, which is pretty fun to play with. Here’s a pic of it in action:

Grapher

QuadComm ploting accelerometer angle and gyro integration

This is a 3 second interval me rocking the board back and forth on its roll axis. The jagged upper line is the angle computed from the accelerometer, using arctan. As you can see, its pretty jagged, particularly on the right side of the image, where I began to shake the board a little. The smooth line below it is the integral of the gyro signal. Though much cleaner then the accelerometer signal, because the integration process, it slowly accumulates error and drifts away from the actual angle. In this picture, the gyro output is below the actual angle of the board by nearly 10 degrees!

Anyone whose ever made a quadcopter, balancing robot, or otherwise used gyros and accelerometers knows that the way to fix the problem of gyro drift is by using a Kalman filter. The Kalman filter is able to track the drift of the gyro using the accelerometer as an absolute reference. By taking the best of both sensors, the filter gives a signal that doesn’t drift like an accelerometer, but isn’t prone to spikes or bumps like a gyro.

Unfortunately, Kalman filters are rather complex. To make matters worse, I’m fairly certain that numerous simplified explanations and example code on the internet are incorrect. They may “make sense” and produce functioning results, but don’t actually implement a correct Kalman filter. Here’s a few of the sources I used to piece together my understanding:

Lots of reading, re-reading, note taking, and experimenting eventually yielded a GNU Octave script that produced this chart:

Kalman Filter

Output of my GNU Octave Kalman filter script

On the chart, red is the accelerometer, green is a naive integration of the gyro signal, and blue is the output of the filter. As you can see, the Kalman filter is basically outputting the gyro signal, only offset to be near what the accelerometer is currently reading. The first third of the chart is me slowly turning the board left and right. Because I just booted up the system, the gyro hasn’t drifted much yet. Then I kept tilting the board but began shaking it along its axis, producing the massive accelerometer jitter that the filter manages to plot right through. Finally, I hold the board still. By now the naive integration gyro has drifted downward significantly, yet even with me introducing a massive amount of accelerometer noise it was able to track the drift quite well.

To wrap up this post, I’m going to include a few notes on the implementation of the above filter that took me a while to figure out.

  • States are completely independent of sensors. The states of the system are angle, angular velocity, and gyro velocity drift. The measurement is accelerator angle and gyro angular rate.
  • There is no control vector ‘u’, or at least one isn’t needed. A few places incorrectly claim that the gyro is a control vector. If you wanted to add a control vector, it would be the output of the motors, along with a B matrix to convert motor outputs to a change in state. Nobody seems to have needed this for a workable filter, but I may try and add it later to see what happens.
  • The filter itself performs the integration of the gyro. A source I found somewhere gave the impression that you needed to integrate the gyro and pass the angle as an input to the filter. While this may work, I think a measurement vector consisting of an accelerometer angle and the gyro angular rate will enable better tracking of angular velocity, which aids in the prediction of the angle.
  • Gyro’s output will change slightly due to temperature and other factors even when completely stationary. Its not simply noise, the signal actually holds quite stable but circles around a bit over time, or has a variable bias. Thus, your kalman filter should really track this velocity bias, and not the angular drift/error that is accumulated by integrating it.
  • The Q matrix should not have zeros. I don’t understand how best to find covariance, but I found somewhere that you can get a passable approximation by multiplying a vector of the variances of the sensors by the vector’s transpose, as I did in the example script below:

I also took the time to clean up, organize, and comment my Kalman filter script. I feel its considerably more simple, concise and understandable than any other script I stumbled upon. I’d definitely be interested to see if anyone else finds it helpful in learning the Kalman filter! Here’s a link to it on the google code SVN: http://code.google.com/p/mjwquadcopter/source/browse/trunk/Octave/kalman.m.

With a basic understanding of the filter in hand, I’ll begin work on writing it again in C. Then its optimization time!

Update:

I’ve finished rewriting the Kalman filter in C. With virtually no optimizations using explicit floating point matrix operations, it can already compute roll and pitch in just under a millisecond. With yaw thrown in it the update interrupt shouldn’t exceed 1.5 ms, and since the Kalman filter represents the bulk of the computations being done, I may be able to set the sampling rate to 2ms, or 500hz! I’m also looking at performing oversampling using DMA and the ADC’s continuous sample mode to get some additional accuracy on the analog readings. Very glad we went with the stm32 over an AVR or Propeller!

I’ve also tuned the Kalman filter some more. I improved the “recovery time” when errors are introduced tricking the gyro using various means, at the expense of increasing the jitter due to accelerometer noise. I expect, however, that the motors will produce noise of a much higher frequency than my shaking the board, which I think will mean that in practice the output of the filter will still be pretty smooth. This also caused the Kalman filter to settle much closer to the actual angle during periods of relative calm, as opposed to before when even nearly stationary, the filter would never get within 2 degrees of the actual angle, something that I think would make stationary hovering very difficult.

16 Responses to “Data logging and Kalman Filtering”


  1. 1 jose August 3, 2010 at 5:03 am

    Hi,

    The kalman.m code google page does not open ! It says forbidden. Can you please, check it ?

    Thnaks !!

  2. 2 Martin! September 21, 2010 at 3:10 am

    Thanks for your explanations!!!!

    I`m traying to understand kalman filters and make my own quadcopter, so it`s really nice to see that other people working in a project like this.

    Congratulations for your work!

  3. 3 Peter Mortensen October 8, 2010 at 9:24 am

    Can you make http://code.google.com/p/mjwquadcopter/source/browse/trunk/Octave/kalman.m. accessible for us?

  4. 4 Sampo October 23, 2010 at 9:13 am

    Hi,

    Could you make kalman.m accessible somewhere? I’d be very interested in it for some rocketry-related projects.

  5. 5 Avraham Tsalik October 26, 2010 at 11:53 am

    I am very much interested in the filter but can’t get it through http://code.google.com/p/mjwquadcopter/source/browse/trunk/Octave/kalman.m

  6. 6 Roy March 17, 2011 at 7:25 pm

    the kalman.m is not accessible.

  7. 7 Vishnu January 27, 2012 at 9:59 am

    I am unable to access the code could pls repost it ….

  8. 8 Dijkstra May 23, 2012 at 9:05 pm

    SVN Link not accessible

  9. 9 Richard September 3, 2012 at 12:05 pm

    Same problem here, can not access code. Looks like they are no longer active since 2009 :P

  10. 10 quit smoking February 9, 2013 at 12:39 am

    Good day! I could have sworn I’ve been to this website before but after reading through some of the post I realized it’s new to me.
    Anyhow, I’m definitely delighted I found it and I’ll be book-marking and checking back often!

  11. 11 quit smoking February 23, 2013 at 3:03 am

    whoah this weblog is wonderful i love reading your articles.
    Keep up the good work! You realize, lots of persons are looking around
    for this information, you can help them greatly.

  12. 12 11:39 am March 18, 2013 at 8:10 am

    Hello there! This post could not be written any better!
    Reading this post reminds me of my good old room mate! He always kept
    talking about this. I will forward this write-up to him.
    Pretty sure he will have a good read. Thanks for sharing!

  13. 13 watermelon salsa April 17, 2013 at 6:05 am

    I do believe all the ideas you’ve introduced to your post. They are very convincing and will certainly work. Nonetheless, the posts are too quick for novices. Could you please extend them a little from subsequent time? Thanks for the post.

  14. 14 http://www.zhengdatansu.com/Shownews.asp?id=9447 April 28, 2013 at 12:32 am

    Article writing is also a excitement, if you be acquainted with then you can write
    or else it is complex to write.

  15. 15 Phen375 Reality April 30, 2013 at 6:42 am

    I simply could not depart your site prior to suggesting that I
    really enjoyed the usual info a person supply to your guests?
    Is going to be back frequently to check out new posts

  16. 16 www.lamannschaft.org May 5, 2013 at 12:38 am

    Thank you, I have just been looking for information approximately this subject for a long time and yours is the greatest I’ve discovered till now. But, what in regards to the conclusion? Are you positive about the source?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Project Quadcopter

Welcome to our quadcopter blog! We're a bunch of high school seniors from Florida attempting to create an awesome flying robot before we all have to go our separate ways for college. To learn more, see the about pages!

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: