# PID explained

The topic of PID often comes up when I talk with FIRST teams. This post is how I explain it to students. I was recently explaining it in a post on Chief Delphi, so I decided to turn it into a blog post while I was at it.

The basic idea for PID is that it corrects for errors. if you make a PID loop based on position, it will move your motors such that the position is what you want. you can replace the word position with the words velocity, angle, and even acceleration or motor current. you can also loop multiple parts of your motion. (i.e. Loop position and velocity), but for FIRST, you usually only need to put a loop around one thing.

Let’s use position for discussion, which is easiest to visualize. First, you need a way to measure your position (an encoder can be used for position or velocity, the gyro or an accelerometer can measure angular data.). The difference between the position you are in, and the position you want to be in, is your error. PID acts on this error to try and make it 0.

In a typical machine tool (like a CNC machine (Hey look, FIRST applies to the real world )), you want to position an axis. Say the axis is currently at zero, and you want to move it a meter. Your commanded position is 1m, and your actual is 0m, making your error +1m. (The sign is important!). Proportional Gain is a number we multiply by the error to get a motor command. Sometimes this is a speed (engineering unis such as m/s), or in our case, it is a motor command in the range of -1 to +1. Let’s say our proportional gain is 0.5. 0.5(gain)*1m(error) = 0.5(command), which moves our motor towards where it is supposed to go.

For the sake of discussion, let’s say our motor is geared such that a command of 1 means the mechanism in question is moving at 1m/s. That is also the top speed of the motion, since the command cannot be greater than 1.

PID loops execute repeatedly in your Periodic tasks, so they occur with a fixed time base. To demonstrate, I’ve put this together as an excel worksheet, and posted it to here -> PID worksheet. The sheet lets you play with some different PID settings, and lets you look at how the mechanism will move based on the commanded velocity. It’s not taking friction or even acceleration into account, but it gives the basic idea of how PID works.

The D (derivative) gain is applied to the difference between the error last cycle, and this cycle. It can be used to slow down the movement if it is approaching the target to quickly.

Integral Gain (I) is applied to the total sum of all errors. It is common to not accumulate errors if the error is too large, or to limit the accumulation to a certain value, or to clear out the accumulator when you are in position. The job of I is to take care of small position errors that occur when the P gain alone is not enough to overcome friction or some other limiting mechanical feature.

The sum of the P I and D values are added together to create the motor command. The gains have to be tuned to get the response you want. If P I or D are too large, the system will oscillate. If the signs are wrong, the system will run away. If I is not controlled a bit, it can overshoot significantly. Most FIRST mechanisms work just fine using P, PI, or PD modes. P mode means that I and D gains are 0. PD means I is 0, and PI means that D is 0.

PID does not always need to be reactive. In general terms, there is a further thing you can add called FeedForward, which you can apply to either velocity or acceleration, as the case may be. The basic idea with Feedforward is that you know you’ll be moving in a certain direction anyway, so give a little boost to your velocity or acceleration, then let the rest of PID react once you get going. You can see this on the second sheet of the workbook.