Designing a PID (Making your robot get from point A to point B smoothly)
Monday, March 30, 2009 at 10:56AM A PID controller is a piece of code which is designed to help your robot move smoothly. It's roots come from a "critically damped" second order differential equation. After, stating that, I'm going to try my best to stay away from getting overly mathy in this description of a PID. To make it easier to explain, I'm going to use my specific application of reaching a certain depth as the system by which the PID was acting on.
Note however that PID's are applicable in almost any system of automation. They could be applied in thermostats, used for watering plants, brewing beer at a certain temp and almost any situation where we have a measurable value that needs to get from one place to another.
So here's the problem, we have Seawolf floating at 1 foot below the surface, we want it to head down to 5 feet. There's a few ways to go about this, we could simply design a basic controller that would turn the motors on and then kick them off when we got to 5 feet, but the Seawolf is naturally buoyant, meaning the motors would kick on, then off, then on then off, granted, we could figure out power is needed for the motors to hold steady, but every time we changed the load at all it would be totally thrown off. Also, we're engineers, we're smarter than a dumb system that has to keep oscillating, and besides, we're trying to be nice to the vision system here.
So lets look at what a second order differential equation actually does since I've already told you that's the basis of a PID. Lets think of 2nd order diff eq. simply as a line between two points to start with. We have the point at our initial time (t=0) and a point which we want to get to at what we can call t = infinity. So we take Seawolf, at our 1 foot at t=0. The slope of the line (if your visualizing this) is going to be our speed. So, we sendSeawolf off on the straight line, with a constant speed. it goes, and goes and goes and bam, we've hit 5 feet, motors turn off, and thanks to the laws of physics we just keep going (that whole screwy momentum thing...). So that's our straight line scenario. So now lets get a bit creative and use a curve lets say that coming out from t=0 we slowly begin to accelerate (so now our speeds not constant) and we do that until we're about 3 feet deep then begin to decelerate until we coast right into 5 feet. This is known as the critically damped point. There area few things I'm leaving out at the moment I'll touch on here momentarily but this is the basis for what we're trying to accomplish. A smooth path from A to B, no instant starts or stops. These two cases can be seen below.
A PID is the second one. It's made up of 3 parts, the Proportion, the Integral, and the Derivative. It's mostly based on the error, or the difference between where you are and where you want to be. For anyone reading this doing the, oh god not calculus routine, don't worry, discrete calculus is really gentle. We are lucky enough to not be working with equations but with data points, so the integral and the derivative are just sums and differences from one point to the next respectively.
The proportional part is an easy one. It's just simply where we are minus where we want to get to times some constant which I'll explain later. But the essential math of is just the set point minus the current point.
Proportional:
Just for a little reminder for those who haven't had calculus in a while, the integral is the area under the curve, so if we're working with data points, we can estimate the area with rectangles with a height of the data value (so our depth in feet) multiplied by the time elapsed since the last data point was captured. If the time between data captures was long this would be an awful estimation, but for the Seawolf our target is 30Hz (30 samples per second). At this rate, due to the length of time it takes us to get from A to B this sampling rate will have 100's if not thousands of those tiny little rectangles. If you were to log it and look at it, we'd be staring what was pretty much a perfect curve.
Integral:
Now for the derivative. The derivative is the instantaneous slope. It's the slope of the line at a single point. Again, if you were working from a set equation and not data points you could calculate another equation to give you an exact answer. However we've got data points, that makes life simple. The slope between two points on our curve is going to be the error of the last measurement minus the error of the current measurement of the error over the change in time.
Derivative:
Alright, now for the magic part... how to make the PID out of the P the I and the D. I'm going to use the standardnomenclaturefor this equation:
where:
Well most of that should look totally familiar, I'm assuming you have been reading...) but the three K's might be throwing you off a bit. These are those constants I mentioned earlier. They're what you use to actually tune the output of the equation to match your application. There are methods for tuning these equations mathematically but the Seawolf is a bit of a bear to run the calculations on to determine the dynamic effects of the props on it when its in the water. (Note, I am no physicist.) The K's are kinda arbitrary it's really difficult to even say X is a good number to start with, it's totallydependentupon what what output you need and what your inputs are. In our case, we're taking in feet in depth, and outputting a number between 0-64 that determines our thruster speed. I do have some advice though on how to go about tuning them. Tuning is really just throwing values into the K values to try to get the system to do what you want it to.
First we'll start with everything commented out except your proportional. The proportional you want to under damp. You won't be able to hit critically damped with just a proportional and theintegralwill essentially step up the equation. Under damped essentially indicates that we're too close to the straight line case, where we just blow right past our point and then keep bouncing back and forth around our set point. You want to get this really close tocriticallydamped though, like as close as you can.
Once you're happy with the proportional, tune the derivative. The derivative is designed to compensate for error in your proportional. At this point you will need to decide whether you want your system to be over damped or under damped. Using this method of tuning its near impossible to achieve exactly critical damping, but that doesn't mean we can't get really close. An over damped equation will level out at the set point, it will just take longer to get to that point than it would with a critically damped system. Essentially you're coming in just a bit too shallow to the set point and have to keep drifting closer and closer until you get there. The biggest reason to over damp is to ensure that you won't oscillate around a point. So where speed isn't a premium and oscillation would be bad, opt for over damped. Underdamped is pretty much exactly the opposite, you dove to sharply and overshot the point. This means you'll get there faster but it'll take just as long if not longer than an over damped system to reach a steady state where you aren't oscillating around the set point. Again as with the proportional there's an exact science to it, but for hobbyist purposes it's not worth it, just trial and error until you're happy. The larger the constant the more damping you'll add assuming you aren't feeding in negative sensor values.
Often times a PD is enough for someone to get by, you've used the proportional to establish a system, then adjusted it to fit your needs, however if there is some error in your system that's constant (like one motor that happens to have more thrust than others) than the integral will come into play.
The integral adjusts for your steady state error. It's not a factor that has to do with your oscillation but a running sum that works to offset and constant quirks. I highly recommend operating without one if you're tuning by hand as errors in your integral will either send your value blowing through the ceiling or will be hardly noticeable, and the line is a very fine one.
I suppose I'll take this opportunity to cover a bit of code. Right now this is the most basic PID possible, I'm not yet filtering any data which should be needed depending on how noisy your sensor is. This is especially true in the case of the integral part, spikes in your data can do really mean things to the integral part of your equation since it's a running sum. Filtering those spikes out is generally advised.
Any how, heres some example PID code:
Matthias |
5 Comments |
Reader Comments (5)
Christ , why do they let these people out of the homes?
Awesome!! Only the images of the formulas aren't very clear because they're transparant GIF's...
Use "View printer friendly version" just above the comments and it'll appear in black on white, complete with readable formulae.
I think, that you are not right. Write to me in PM.
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.