The components of a PID control system are P (proportional), I (integral) and D (differential), and it is not always the case that all of the components are present in a given control system.
To understand PID control systems, and how to implement them, it is perhaps useful to consider the physical system upon which the PID control system is based. Suppose we have an object of mass M that we want to control the position of. Let the position of the object be X, and the desired position of the object be C. The position of the object, X, and the desired position of the object, C, might be indicated by the voltage reading on potentiometers attached to the object, and a control lever.
The P in PID is for a control force that is proportional to the difference between the desired position of the object, C, and the position of the object, X. If C is greater than X, the force is set up to increase X. In our robot one way to apply the control force is to set the pulse width (% on time) of a motor that is applying a force to the object. This produces, approximately, an average force that is proportional to the pulse width.
What do we have at this point? We have an object of mass M, with a force being applied to it that is proportional to C - X. Those who have taken basic physics will recognize this as the force that a spring produces, giving us the motion of a mass on a spring. If the force is perfect, and there is no damping, the mass will oscillate about the desired position, C, for quite a long time…
This brings us to the D (differential) in PID, which can also be usefully referred to as “damping.” Referring back to our mass on a spring that is oscillating forever because of the perfection of our control system, if we suspend the mass in molasses we find that the oscillation rapidly decays and, neglecting gravity, that it nicely settles into position C.
So, we have to figure out what is going on with the molasses… The mass moving in the molasses produces a frictional force that opposes its motion, and to first order produces a force that is proportional to the velocity of the mass. The effect of this dissapative force is to slow the mass down, so that it eventually comes to rest.
To implement this force we need the velocity of the mass. Lacking a direct sensor for this we can simply remember the position of the mass on the prior trip through the loop in robot control program and subtract that from the current value. This gives us a measure of the velocity and we can then suitably adjust the force being applied to the mass. If we do it perfectly, and the world is not perfect, we will get damped motion. I like to call it electric molasses. It is only the velocity of the mass, itself, that matters. If you remember (C - X) from the past loop and then subtract it from the new (C - X), C - C gets you zero so you can do it either way you would like.
Finally, we have the I (integral) in PID. Suppose our mass and spring are suspended vertically, so that the force of gravity is pulling on the mass a bit. With gravity pulling on the mass, the spring is stretched a bit to counteract the force of gravity, and as a result the mass is not pulled into the correct position by the spring. Because of the force of gravity there is a persistent error in the positon of the mass.
Now, in every control system that Team 1280 has built for a FIRST robot, such an error has always been made up by the human operator who just moves the control stick just a little more; but this is where the I in PID comes in. If there is a small error, and we integrate the error in time, the integral eventually becomes big. If we apply a force to the mass that opposes the inegrated error, the mass will eventually go to the desired control point, regardless of which way the influence that is causing the error is coming from. It this case you are integrating the error, C - X.
The world is not perfect. Timing errors in the application of the forces involved in a PID system can lead to an unstable control system where the oscillations grow instead of damping. I’ll refrain from discussing these, but will refer you to how a child pumps up the motion of a swing. It is all in the timing… Oscillation problems usually arise when the “gain” is too high for the timing errors that exist in your control system. Additionally, the “dead zone” of your victor controller (the range of values for which no motor output occurs) must be dealt with if you want fine control out of a PID control system on a robot.
Now, your question had to do with controlling the speed of your robot, instead of the position of perhaps an articulating arm. This is not something that we have done, in that students using joysticks to control motor power has been sufficient for us (with some massaging with regard to the dead zone in the victor), but the principle is the same as for position.
You need a velocity sensor, and if you do not have one that directly supplies a velocity reading to the robot you could use one that reads position and subtract a past value in order to get the velocity. A force that that is proportional to the error vis-a-vis the desired speed will cause the robot to accelerate. In this case you don’t want your “damping” force to be proportional to velocity, you want it proportional to the measured acceleration, if you use a D feedback signal. There are really huge frictional losses that require substantial motor power in order to make up for. This is the moral equivalent of the influence of gravity in the discussion above and is likely severe enough that you might want to be using the integral, I, control term.
Additionally, you can get quite an abrupt stop if you run the motors in the reverse direction of the current motor travel. It may be the case that you want to make sure that your control system is coded to avoid this type of violent behavior. In fact, if you desire to set the speed of the robot with a joystick you could be better off constructing a simple static map of motor power as a function of joystick position that tends to produce the desired speed result, and then use integral feed back to produce a correction term in response to any error in the speed, but limit the correction term in a way that avoids any power reversals in the motor vis-a-vis the direction of travel.
When debugging a PID control system, the most frequent error is a sign error in force being applied to the thing being moved. If the sign is wrong the control system goes completly out of control, so to speak. For things like an arm, it can lead to damaged equipment. For the motors driving a robot it can lead to a 130 pound robot racing across the floor completely out of control. It pays to put your robot up on blocks, or disconnect the drive to an arm, checking that the feedback is working correctly before you risk the equipment, or arm and legs.
If you control the appendages on your robot with PID systems, you can then use a state machine to have them do interesting things during the autonomous period. That is where the robot programmers really get to show off!
Have fun,
Eugene
A nice tutorial for PID control can be found at:
http://www.engin.umich.edu/group/ctm/PID/PID.html#ol
In particular, the “cruise control” or “motor speed control” examples located at the bottom of the web page may be of interest.