Kalman filter for the prop?
jamma
Posts: 33
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.
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
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
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
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
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 ;-)
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
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
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 ;-)
e.g a pair of simultaneous equations (solution x=1, y=2 btw)
2x + 3y = 8
x + 5y = 11
becomes
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
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
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 ;-)
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":
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
BTW: I type as I'm thinking, so please don't take any offense at my writing style
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 ;-)
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
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
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 ;-)
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
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
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 )
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
BTW: I type as I'm thinking, so please don't take any offense at my writing style
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 ;-)
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...
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
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.
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
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 ;-)