Shop OBEX P1 Docs P2 Docs Learn Events
Kalman filter for the prop? — Parallax Forums

Kalman filter for the prop?

jammajamma Posts: 33
edited 2007-06-28 03:33 in Propeller 1
Based on responses in the PEKbot thread, there are more than a few of us interested in creating balancing devices of one sort or another (typically robots, but·I'm looking to do a skateboard). The Sparkfun 5DOF IMU (http://www.sparkfun.com/commerce/product_info.php?products_id=741) is just begging to meet the Prop for these apps.

The fancy way to integrate the gyro and accelerometer data is with a Kalman filter. There is a generally well regarded implementation in c here: http://www.rotomotion.com/downloads/tilt.c·(see also http://www.rotomotion.com/downloads/tilt.h )

Sadly, the last C I wrote was 20 years ago (cripes!), and converting this to SPIN or prop asm is way beyond me. Anyone out there want to give it a shot? It'd make a heck of a nice object.

Comments

  • parskoparsko Posts: 501
    edited 2007-03-30 08:47
    www.scipy.org/Cookbook/KalmanFiltering
    www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf

    That (first link) is a simple Python algorithm. It suppose it can directly be converted to spin or assy.

    I'm curious about this too. This topic has popped up in my travels over the last year or so. It seems that it would be much simpler to implement than one would think. I'm new to the Kalman myself, but from what I have read, the parameters Q and R are the ones that set the conditions/state/constants for the system. Adjusting them affects how your system reacts.

    I assume one could apply this filter to a 12bit A/D signal to help stabilize that last few bits. I'm curious how folks would design a test setup using common parts a hobbiest would have. What I'm thinking is to filter some accelerometer values or simply the A/D output of a crappy variable resistor would be okay???

    Again, the algorithm seems relatively easy to code in Spin, but I'd be curious how the hardware would look to check it. I'm thinking the output would be two values on either the VGA or TV screen. One would be the unfiltered 12bit value, bounging around in the last few bits (+/- 0 to 3 in bit count), an a filtered value that seems to be very steady?

    Again, I'm pretty curious about this, but what I just typed could be completely wrong. Is my take on it correct?

    Any thoughts?

    -Parsko
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 09:19
    even if your C is up to date it really helps to understand the maths behind it otherwise you miss details. Finding a nice description of the algorithm is the key then you just code it, its obviously not too complex to code.

    Parkso, though its a filter I think it is used to combine data coming from different sources into one stream in particular gyros and accelerometers.

    Graham
  • simonlsimonl Posts: 866
    edited 2007-03-30 09:37
    Hi folks,

    DISCLAIMER: I'm very far from being a Kalman Filter expert (and that's a major understatement!)

    Graham's right; a Kalman filter can be used to combine several noisy inputs, providing a good estimate of state. In the case of a UAV the inputs could be gyro's and accelerometers (but can also include a GPS), and the output would be the estimate of the UAV's attitude and/or position.

    I've been trying to understand KFs enough to code one for a helicopter UAV, but the maths all looks greek to me :-( It also looks like we'll need to do some pretty hefty matrix maths too...

    Thanks for the 'scipy' link parsko; that code looks a little easier to follow than C! If anyone's going to convert that to Spin, I'd sure be interested.

    And jamma; thanks for bringing this up. Maybe we should start a campaign for a Kalman Filter tutorial? I've already asked Parallax, but they're not inclinde to do one at the moment (BTW: That's absolutely NOT a criticism!)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 09:49
    Believe it or not matrices are generally used to make things clearer, they just represent equations. The c code linked to above suggests there is nothing very hefty going on.

    I'll have a read of that paper, I don't think I need a Kalman filter but I'm a glutton for punishment [noparse]:)[/noparse] Besides it might be possible to turn my cordic object that is in the works into a signal processing tool kit, who knows.

    Graham
  • simonlsimonl Posts: 866
    edited 2007-03-30 10:03
    Hi Graham,

    I guess that gives you an idea as to my maths abilities (LOL).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 10:23
    Don't worry I don't always believe it [noparse]:)[/noparse]

    e.g a pair of simultaneous equations (solution x=1, y=2 btw)

    2x + 3y = 8
    x + 5y = 11

    becomes
    |2  3|.|x|=|8 |
    |1  5| |y| |11|
    
    



    which might be written as a.b=c

    And using the algebra of matrices you can solve them in efficient and automated ways.

    I hope that gives you an idea of what they hell they are for. And even more so I hope I got that right, I'm rusty as anything.

    Graham
  • simonlsimonl Posts: 866
    edited 2007-03-30 10:50
    No, no, really, I didn't have a clue! But now I do -- your example makes perfect sense to me -- thank you smile.gif

    I have to say, it's the last notation (a.b=c) that gets me. It's not always clear that they're meant to be matrices, and that's been making any attempt to convert C code hell for me!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 11:22
    you should be able to tell what is a matrix in the code by looking at when it is defined.

    In mathematics a matrix tends to be in bold face.

    note that the dot between a and b is more than a multiply it is a "dot product":

    
    |a b|.|e|= |a*e+b*f|
    |c d| |f|  |c*e+d*f|
    
    



    Which shows why it represents those equations.

    In the code when doing the various operations they realize that some parts will always be zero so they don't bother dealing with those to streamline the code.

    Graham

    Post Edited (Graham Stabler) : 3/30/2007 11:48:54 AM GMT
  • simonlsimonl Posts: 866
    edited 2007-03-30 11:43
    This is like a breath of fresh air to me Graham; the fog is finally starting to lift! Many thanks smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • parskoparsko Posts: 501
    edited 2007-03-30 11:43
    "After each time and measurement update pair, the process is repeated with the previous a posteriori
    estimates used to project or predict the new a priori estimates. This recursive nature is one of the
    very appealing features of the Kalman filter—it makes practical implementations much more
    feasible than (for example) an implementation of a Wiener filter [noparse][[/noparse]Brown92] which is designed to
    operate on all of the data directly for each estimate. The Kalman filter instead recursively
    conditions the current estimate on all of the past measurements."

    That is from the "kalman_intro" pdf, page 5. Graham, I disagree that it requires two inputs. I think it works with two inputs, but certainly can be applied to simply one, as a filter. I don't know that for fact though.

    My understanding of the Kalman is: it allows one to make a prediction on the current or future state based upon past measurements, normally those past measurements are noisy. So, it filters noise from an input, right?

    The test setup I suggested earlier is simply to test to see if this is true. If it is, then the Kalman can be used as a filter, basically. I think it would be a more accurate way of doing things besides, for instance, averaging the previous data.

    Why is it necessary in a chopper or autopilot is a good question. I would think it is so the plane/chopper does not make any drastic moves based upon the noisy inputs. So, if the gyro says that it is suddently 90 degrees from where it was in the past measurement, the Kalman will simply use that as a factor in predicting where the actual location is, subsequently only adjust a little based upon the huge amount of noise.

    Where this blows apart is when your initial numbers (the Q and R I mentioned earlier) are wrong, and the chopper/plane either overreacts (over predicts current actual location) or underreacts (does not compensate enough) that the prediction cause catastophic failure. I deduced from my readings that these numbers (Q & R) can be updated and calculated real time in certain cases...

    Gotta do some actual work now that lunch is over...

    Did I get something wrong???

    -Parsko
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 11:45
    Sorry Parkso, I was just trying to say that that is what most people want them for.
  • simonlsimonl Posts: 866
    edited 2007-03-30 11:50
    Hi parsko,

    Don't think you got anything wrong -- I've only been reading about KFs in the UAV context, so always shown as having multiple inputs. Don't see why it wouldn't work with single input though.

    I guess the KF is a bit like a PID on steroids? (I also guess I've WILDLY over-simplified it though; sorry Mr Kalman!)

    One other thing: I believe that a KF also allows us to take slower readings (e.g. GPS at 1Hz, but gyro/acclerometer at 10Hz) and use the slower one to correct any inherent errors.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • parskoparsko Posts: 501
    edited 2007-03-30 11:59
    Graham,

    No problem. I would also like to use it for multiple inputs myself, though not for choppers. I simply wanted to get everyone to agree on a simple example we could start to code, which would be a single input.

    Simon,

    Yes, I think you are right that it would work well with different sampling rate inputs.

    I can't decipher the algorithm now, but will look at in tonight after all my daily obligations have been me [noparse]:)[/noparse] [noparse]:)[/noparse] [noparse]:)[/noparse]

    Simon, I think the concept is quite abstract, but the reality is that the algorithm is quite simple. And Graham said it good earlier, linear algebra was practicallly invented by people who code. Matrices are quite useful. My math skills aren't awesome either, so I will need to relearn that as I go...

    -Luke
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 12:25
    Simon, I don't think it is anything like a PID but it might give you something to feed you PID.

    The basic idea very possibly repeating what Luke has said is to make an estimate of the current state based on the previous estimate and then use a measurement of the current state (along with its associated noise) to improve the estimate.

    It can definitely just filter a single signal but you can also make the state a function of multiple variables in order to combine them.

    I tried reading the pdf but I didn't like it that much so I had a look on wikipedia and I liked some of that until one point so I might go back. Its frustrating how hard what are probably quite simple things can be made by terminology that as an engineer rather than a mathematician I didn't learn.

    Luke, good idea. What about we take Chip's object that reads in from the microphone and try to filter that. You can then plot before and after.

    Graham
  • simonlsimonl Posts: 866
    edited 2007-03-30 12:29
    Hi Graham; Yup, that makes sense (feeding into PID).

    Glad I'm not the only one who is getting frustrated by mathematical explanations! I too get the impression that a KF isn't all that complex, but I'm yet to find the ellusive "Dummies guide" :-( Perhaps you guys will figure it out for me? (More than happy to see if I can follow your explanations wink.gif)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • parskoparsko Posts: 501
    edited 2007-03-30 12:36
    I'm thinking more about this and how to test.

    Accelerometer
    Servo

    Read accel. using 8 bits. Use chips Random number generator to give it some slightly different (but known) value from reality. Dump this value into the Kalman filter and use the output to adjust the servo. So, we'd be measuring the orientation w.r.t. gravity (not have the servo spinning level with earth)

    This should work. I'll think about it while waiting with super-preggo at the hospital...

    -Luke

    PS- Nonmeclature (spelling) is the reason why I never grasped many physical and mathematical concepts. That is why good teachers are in high demand! Also wish "they" would standardize ALL variables, so g is always gravity, and alpha is always angular acceleration, whatever...

    PPS - I'll try to pseudo-code the Python script in the earlier link later today...
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-30 12:39
    You would run out of greek letters if you did that.

    This is the first chapter from the book they guy mentions in the pdf, from the same site actually:

    http://www.cs.unc.edu/~welch/media/pdf/maybeck_ch1.pdf

    Graham
  • jammajamma Posts: 33
    edited 2007-03-30 16:04
    Other links I found useful in my research:

    http://tom.pycke.be/mav/71/kalman-filtering-of-imu-data

    http://academic.csuohio.edu/simond/courses/eec644/kalman.pdf

    Be warned: A too literal reproduction of the Kalman filter might not be the best in practice. The C code in the links I provided in the first post seems to be implementing an Extended Kalman Filter and computing a Jacobian matrix. This seems to work best in practice vs a strict Kalman filter.
  • simonlsimonl Posts: 866
    edited 2007-03-30 16:26
    Hi jamma,

    If I'm right, an Extended Kalman Filter (EKF) is required if your system cannot be described by a linear equation, and that's apparently the case in a UAV!

    p.s. Another site you may not have seen is www.dev6.com

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • ry.davidry.david Posts: 63
    edited 2007-06-28 03:33
    I hate to dig up an old topic, but any progress with this?· I have been reading up on this for the past week and understand the concept, but am totally lost in the actual math.· I am probably going to order the Sparkfun 5DOF breakout board next week and mess around with this.· Some of the results I have been reading about look promising.
Sign In or Register to comment.