PROGRAMMING – PID CONTROL
A proportional integral derivative controller (PID controller) is a common method of controlling robots. PID theory will help you design a better control equation for your robot.
Shown here is the basic closed-loop (a complete cycle) control diagram:
The point of a control system is to get your robot actuators (or anything really) to do what you want without . . . ummmm . . . going out of control. The sensor (usually an encoder on the actuator) will determine what is changing, the program you write defines what the final result should be, and the actuator actually makes the change. Another sensor could sense the environment, giving the robot a higher-level sense of where to go.
To get you started, here are a few terms you will need to know:
error – The error is the amount at which your device isnt doing something right. For example, if your robot is going 3mph but you want it to go 2mph, the error is 3mph-2mph = 1mph. Or suppose your robot is located at x=5 but you want it at x=7, then the error is 2. A control system cannot do anything if there is no error – think about it, if your robot is doing what you want, it wouldnt need control!
proportional (P) – The proportional term is typically the error. This is usually the distance you want the robot to travel, or perhaps a temperature you want something to be at.
derivative (D) – The derivative term is the change in error made over a set time period (t). This is usually the velocity of your robot. So if your robot was at x=5 about one t ago, and is at x=7 now, then the derivative term is 7 – 5 = 2/t. If you are using a microcontroller, you can calculate the time with this timer tutorial.
integral (I) – The integral term is the rate of change in the error made over a set period of time (t). This is usually the acceleration of your robot. If your derivative term was 2/t a second ago, and it is 2/t now, your integral term is 2 – 2 = 0/t^2. Thats an acceleration error of zero . . .
note: there is an error in the above equation but haven’t had time to fix and re-explain it -admin, January 10th 2008 . . . i need a few days
tweak constant (gain) – Each term (P, I, D) will need to be tweaked in your code. There are many things about a robot that is very difficult to model mathematically (ground friction, motor inductance, center of mass, ducktape holding your robot together, etc.). So often times it is better to just build the robot, implement a control equation, then tweak the equation until it works properly. A tweak constant is just a guessed number that you multiple each term with. For example, Kd is the derivative constant. Idealy you want the tweak constant high enough that your settling time is minimal but low enough so that there is no overshoot.
What you see in this image is typically what will happen with your PID robot. It will start with some error and the actuator output will change until the error goes away (near the final value). The time it takes for this to happen is called the settling time. Shorter settling times are almost always better. Often times you might not design the system properly and the system will change so fast that it overshoots (bad!), causing some oscillation until the system settles. And there will usually be some error band. The error band is dependent on how fine a control your design is capable of – you will have to program your robot to ignore error within the error band or it will probably oscillate. There will always be an error band, no matter how advanced the system.
ignoring acceptable error band example:
The Complete PID Equation
Combining everything from above, here is the complete PID equation:
Actuator_Output = Kp*P + Ki*I + Kd*D
or in easy to understand terms:
tweakA * (distance from goal)
+ tweakB * (velocity error)
+ tweakC * (acceleration error)
The nice thing about tuning a PID controller is that you don’t need to have a good understanding of formal control theory to do a fairly good job of it. Most control situations will work with just an hour or so max of tuning.
Better yet, rarely will you need the integral term. Thats right, just delete and ignore it! The only time you will need this term is when acceleration plays a big factor with your robot. If your robot is really heavy, or gravity is not on it’s side (such as steep hills), then you will need the integral term. But out of all the robots I have ever programmed, only two needed an integral term – and both robots were over 30 lbs with a requirement for extremely high precision (millimeter or less error band). Control without the integral term is commonly referred to as simply PD control.
There are also times when you do not require a derivative term, but usually only when the device mechanical stabalizes itself, works at very low speeds so that overshoot just doesnt happen, or you simply dont require good precision.
Sampling Rate Issues
The sampling rate is the speed at which your control algorithm can update itself. The faster the sampling rate, the higher precision control your robot will have. Slower sampling rates will result in higher settling times and an increased chance of overshoot (bad). To increase sampling rate, you want an even faster update of sensor readings, and minimal delay in your program loop. Its good to have the robot react to a changing environment before it drives off the table, anyway. Humans suffer from the sampling rate issue too (apparently drinking reduces the sampling rate, who would have guessed?).
The rule of thumb is that the sample time should be between 1/10th and 1/100th of the desired system settling time. For a typical homemade robot you want a sampling rate of about 20+/second (very reasonable with today’s microcontrollers).