Introduction

There are many explanations of how a PID works, many of them fantastic. The main issue comes down to how it is explained. I tried to pick up the idea of PID equations when I was much less knowledgeable on mathematics and struggled very hard to find a tutorial that didn’t explain it with notation I understood. Symbols are scary, okay?

I’m sure many of you out there are fine reading mathematical notation (and I am now too, to an limited extent) but as somebody not used to it, I found the symbols and notation to be very intimidating.

The way I want to explain PIDs here is with as little math as possible, favoring concepts over equations. They are something that are applicable to an extremely large amount of applications and I think very useful for anyone (even aside from programmers) to learn.

What is a PID Controller?

A PID Controller in essence is a method of stabilizing a system, whether that be a quadcopter, the fuel rods in a nuclear reactor, or the angle of an arm for a bio-mechanical dog harness.

A PID takes an amount of error (more on that later) and a method of reducing said error and calculates a rate to progressively solve it. For example, it is the method of taking angle data from a gyroscope and telling each rotor how fast to rotate.

The P, I, and D stand for Proportional, Derivative, and Integral gain. More on that later as well!

The Issue

You’re in a car that can drive forward or backward approaching a white line. Your only instruction is to write a computer program to stop directly on that white line while also reaching it as fast as possible. All your program can do is apply a negative or positive acceleration to the car.

The simplest idea that comes to mind is the idea of, well, if you’re behind the line, throttle on! If you’re in front of the line throttle back!

It’s pretty easy to see how that is a flawed solution. By the time you reach the line, you’re going to be going too fast and shoot right past it. So, maybe your next solution is to push the throttle proportionately to how far away you are from the line. The closer you are, the less you push the gas.

Now, the issue with this is it causes the same type of phenomenon as the previous method. You oscillate, going past the line, reversing and going past it the other way. This will always reach an even oscillation (similar to a sine wave).

While this isn’t our solution, this is a part of it. This is the proportional part of our PID.

The (Partial) Solution

What if you had a method of decelerating as you approached the line? A way to preemptively slow down even before you were at your goal.

This is where the D in PID comes in. Those of you who took Calculus in High School or College (don’t worry if you didn’t!) may know what a derivative is. Without going too deep into it, it is simply an equation that describes the rate of change of something. For example, you can see it as the car’s velocity. The velocity is the rate of change of the car’s position.

If we monitored how fast the car was moving, then this gives us our second tool in stopping on that line. As we approach the line, we want to be moving slower so think of the Derivative in a PID as a type of resistance. It’s a value that grows larger the faster we are moving. This more easily comes into effect as we get closer as the level of proportional gain lowers. The derivative gain lowers the overall function as your error approaches zero.

This may have been a bit confusing, so lets add some numbers!

Bring the math!

Lets say we start 50 meters away from the line we are attempting to reach. Since we are 50 meters away, this means our error is equal to 50.

I know. It’s hard to believe that I don’t do art for a living.

Each member of a PID Controller has what is called a gain. This is simply a number multiplied to it that changes how much that member affects the equation as a whole. For example, if our gains were all one, then each member (the P the I and the D) would affect the equation equally. Let us assume that currently our gains are all one for the sake of simplicity.

Let’s calculate our P and D (we’ll worry about the I later). Our proportional gain is 50 as it is based directly off of our error. Now, we aren’t moving yet so our Derivative gain is zero. It is based directly off of our change of error. Let’s write this out (the K’s stand for Gain don’t ask why):

Slam that accelerator!

Now we’re moving! Maybe a little too fast... Either way, we’re attempting to accelerate the car to 50m/s now. Maybe that’s a bit too harsh? This is where the gains come in. Balancing the gains of a PID is generally done manually as it only needs to be done once (unless changes to the system are made).

For those with a keen eye, you’ll see that the gain for the derivative is negative. This is because we want it to resist the proportional gain. The closer we get to the point the slower we want to be going. Thus, we want the speed to negatively drag the whole acceleration value down.

You can see that as we accelerate and reduce our error, the proportional gain will go lower and the derivative gain will go higher (since we’re speeding up). This even means that as we get closer to the line, if we’re going too fast (as in the derivative gain is too high) it will actually pull us into negative acceleration telling our system to slow down.

Lets say the next time we do a poll for data we’re now going 5m/s and our car is 40 meters away from our object. Lets see how hard we’re accelerating now:

You can see that our derivative gain is keeping us from going too fast the closer we get to our objective. It may even push our equation negative causing us to slow down before we even reach our destination. This is like pressing the break as you approach the line at a red light.

What about the I?

Now, I’ve saved the Integral element until last because it is generally the least important and most difficult to see it’s worth. A lot of systems don’t even include it!

The I stands for Integral gain. An Integral is known as a summation over a period of time. Essentially, if you have an equation that maps the velocity of an object the integral of that equation will map the position of the object. This is because it records the sum of the velocity at each moment resulting in the distance the object has moved.

How does this help the PID, you may ask. Well, now that we have Proportional and Derivative gain, you may ask what happens if we overshoot slightly? The issue with this is it is now very slow to correct that slight error (and there is likely to be some). As the proportional gain tries to accrue a slight velocity, the derivative is now resisting this.

If we sum up the total error, then this will drag us back into the Goldilocks zone of zero error resolving the issue of slow acceleration at points very close to zero error.

EDIT: As some great people on Reddit pointed out I left out the most important use of the integral term. The integral term is used to eliminate systematic error.

For example, let’s say there is some small amount of resistance on the turn (there always is in real world applications) or perhaps your quadcopter had some weight added to it.

The integral term due to its level of accumulation, is able to drag it back on track.

Calculating Gains

Without gains the system will not react properly. It may oscillate, or the derivative and proportional elements may cancel each other out. Balancing them is what brings the system to equilibrium.

Unfortunately there is no method of directly calculating what gains you want to use. There are ways to dynamically find good values based on how the system is reacting, but there are many papers on how to do that and I find that a very difficult concept to explain non-mathematically.

Luckily, there are good methods of doing it manually and unless any changes are made to the system you won’t have to do it more than once.

The method I have found most useful is to adjust the proportional gain until you have steady oscillation and have found a desirable speed in which you reach zero error.

From there you want to adjust the derivative gain until oscillations stop. If the system is now too slow, repeat the cycle.

Addendum

Ever since I learned PIDs I have always been fascinated by them. They’re very cool in the sense that they almost have an intelligence. As long as you can find a source of error and a way to reduce it, a PID can solve the issue.

Want to keep a ball from rolling off a platform by varying the tilt of the platform? Use a PID! Want to calculate how much to tilt a quadcopter to stop it from moving laterally? A PID is perfect for that.

It’s very interesting and a very widely applicable concept.

If you made it this far, thank you so much for reading. This is a concept I’ve always loved sharing with people purely due to the fact I find it easy to explain. If you found something confusing, please shoot me a message, comment, or tweet at me @bittneradave and I will try and answer your question/revise this post as well as I can!