I was recently trying to explain PID controllers to someone and realized that I didn’t have a very good intuitive understanding of what they’re useful for and how they work. When looking around the web, I had trouble finding a straightforward explainer. So in this post, I’ll give (hopefully) simple answers to some basic questions that I had about PID controllers. Since this post got a bit long, here’s a table of contents.

A PID controller is a way to solve problems with the following formulation:

- You can change some input to the system, called the process variable
- You have a sensor which monitors something about the system
- You want the sensor measurement to be close to some target value, called the set point

The PID controller is a good way to decide what the input to the system should be without knowing anything about the internal workings of the system, except that the change in output is roughly proportional to the input.

When running a 3D printer, you want the nozzel end to be a specific temperature. You control the temperature by regulating the voltage through a hot end - higher voltage makes the temperature go up, whereas if you turn it off entirely the temperature will go down (sometimes helped by a fan next to the extruder). You likely want to change the temperature during the course of printing, and you want it to reach the target temperature as quickly as possible. Changing the voltage affects the *rate of change in temperature* rather than the temperature itself.

When driving a car, you regulate the speed by controlling how much to open the throttle. Opening the throttle will cause the car to accelerate, while closing it will cause the car to decelerate. You want the car to reach some set speed as quickly as possible. Changing the throttle toggles the *acceleration* and not the *velocity*, but the variable you care about controlling is the *velocity*.

When giving vasopressors to a patient in a hospital, you want them to reach some target blood pressure. After injecting a particular amount, the patient’s blood pressure will go up or down. Changing that amount will change the *rate of change* of their blood pressure. You want to reach the target blood pressure as quickly as possible without overshooting.

To answer this question, I think the best way is to start off with an easy-to-understand controller, then add on top of it until we get to the final PID formulation.

You could write a control rule like this:

- If the sensor measurement is too low, set the system input to “positive” (try to make the sensor measurement higher)
- If the sensor measurement is too high, set the system input to “negative” (try to make the sensor measurement lower)

However, if the system has *inertia* (in other words, a delay between the change in input and the change in output), then this control algorithm will start oscillating as you repeatedly undershoot and overshoot. Inertia can happen in lots of different ways and is common in most systems that you would actually want to control. An improvement to this could be to scale the input relative to the *error*, so that as your error gets smaller, you decrease your input.

In this case, we introduce an additional scaling constant $K_p$, which relates the size of the error to the size of the input. For example, in the case of our 3D printer, our error is the difference between the target temperature and the observed temperature, while our input is the voltage, so we need to convert from degrees celcius to volts somehow.

We can try these out with some sample numbers to demonstrate the idea. Suppose our target temperature is $200 \deg$ and our current temperature is $175 \deg$. We can use some scaling constant $K_p = 0.1 V / \deg$.

\[\begin{aligned} \text{error} & = 200 \deg - 175 \deg = 25 \deg \\ \text{input} &= (0.1 V / \deg) * 25 \deg = 2.5 V \end{aligned}\]After some time, the temperature increases to $190 \deg$.

\[\begin{aligned} \text{error} & = 200 \deg - 190 \deg = 10 \deg \\ \text{input} &= (0.1 V / \deg) * 10 \deg = 1 V \end{aligned}\]As expected, our input is smaller than when the error was larger, since we want to make the temperature delta smaller as we get closer to our target temperature.

Let’s say sometime later the system has gotten hotter, and now our temperature reading is $240 \deg$.

\[\begin{aligned} \text{error} & = 200 \deg - 240 \deg = -40 \deg \\ \text{input} &= (0.1 V / \deg) * -40 \deg = -4 V \end{aligned}\]As expected, now that we’ve overshot our target temperature, we need to supply an input in the opposite direction. Note that the input voltage is relative to some zero point, since we can’t actually have a negative input voltage.

Here’s a simple program to simulate a 3D printer nozzel. Note that the heater simulator is only a very loose approximation to the behavior of the actual heater, and can stand in for any black box system that you might want to use a PID controller for.

We can run this script using:

You can try running this script yourself to see how playing with different parts of the system affect the temperature curves. In particular, `--v-offset`

and `--inertia`

are interesting parameters to play with.

The resulting temperature curves for the built-in configuration are as follows:

PID stands for “proportional, integral, derivative” and is a way to address some issues with the above model. Namely, there are two issues that we want to address:

- Undershoot: Our input is too weak, and the output isn’t changing quickly enough in response to a change in input
- Overshoot: Our input is too strong, and the output is changing too quickly

Among the temperature curves above, the $K_p = 0.8$ overshoots the most, while the $K_p = 0.05$ undershoots the most.

Consider the case in which we are **undershooting** our target value. We can detect that we’re undershooting if the error is accumulating too fast - in other words, our error isn’t going down fast enough. We can add another term to the controller which takes this into account, using the integral of the error.

We can approximate this by keeping track of our running error:

\[\text{input} \approx K_i \sum_{t = 0}^{T} \text{error}_t \, d \text{ time}\]This controller can work on it’s own, and will correct for undershooting. However, by itself it will naturally oscillate, because it has to accumulate error on the opposite side of the target value in order to start heading in the opposite direction.

Here’s a few temperature curves for an undershooting proportional controller, with different integral controller coefficients.

Consider the case in which we are **overshooting** our target value. We can detect that we’re about to overshoot if the error is getting smaller too fast. We can add another term to the controller which takes this into account, using the derivative of the error. The desired behavior is to decrease the input if the error is getting smaller too quickly, and increase the input if the error is getting smaller too slowly. This can be expressed as a function of the derivative of the error:

We can approximate this by keeping track of our past error:

\[\text{input} \approx K_d \frac{\text{error} - \text{prev error}}{d \, \text{time}}\]This controller won’t work on it’s own, because the error shouldn’t change without changing the input. The power of this controller is to help correct for the overshooting behavior of our original controller.

Here’s a few temperature curves for an overshooting proportional controller, with different derivative controller coefficients.

We can put together each of our controllers into the final PID controller formulation shown below:

\[\begin{aligned} \text{input} \, = \, K_p & * \text{error} \\ + K_i & * \int \text{error}_t \, d \text{ time} \\ + K_d & * \frac{d \, \text{error}}{d \, \text{time}} \end{aligned}\]A Python implementation for this controller can be found below.

Now that we’ve figured out the basic formulation for PID controllers, how can we figure out the values for $K_p$, $K_I$ and $K_d$ which give us the best behavior?

There are a few methods for doing this, and it depends a lot on the particular scenario. Some relevant questions:

- Can you run the control algorithm many times, or is it important to run it only a few times?
- Is there a lot of noise in the system, or if you run the same control algorithm with the same parameters will the result be relatively similar each time?

There are many packages and techniques which will figure out these parameters for you, but if you’re in a pinch you can follow the approach below.

- Choose an objective to minimize
- Do a grid search over $K_p$, $K_i$ and $K_d$
- Choose the values of $K_p$, $K_i$ and $K_d$ which minimize that objective

For most PID controllers, you care about making the output reach the set point as quickly as possible, without overshoot. Different applications can tolerate different amount of overshoot and undershoot, so the metric might vary. However, in this application I’m going to choose a simple error function which optimizes for both quickly reaching the target and not overshooting, by taking the absolute error when the output is less than the set point and the squared error when the output is greater than the set point.

\[\begin{aligned} \text{error} & = \text{set point} - \text{output} \\ L & = \int_0^T \begin{cases} \text{error}_t^2 & \text{if } \text{error}_t < 0 \\ \text{error}_t & \text{if } \text{otherwise} \end{cases} \end{aligned} dt\]I’ve included a script which can be used for sweeping different PID configurations for our original simulation.

The script can be run, for example, using the command below:

Running this command gives the following plot of the error curves generated when varying $K_p$ for different values of $K_d$:

As this graph shows, the ideal value of $K_p$ (the lowest point in the error curve) increases as we increase our value for $K_d$. This makes sense intuitively; by increasing the derivative term, the temperature will not shoot up as quickly for the same value of $K_p$, but we can safely use a higher $K_p$ without worrying about overshooting.

Now that we’ve looked at a few different configurations, we can just choose the configuration which minimizes our objective.

We can plot the associated temperature curve using the command below:

It looks reasonable, and definitely better than our original curve.

We can do a much more careful job and get a better curve, but this is pretty reasonable for our toy problem. In fact, for the default parameters, we can just make $K_p$ and $K_d$ really large and get very close to an ideal curve. It’s kind of fun to play around with different values for `--heat-coeff`

, `--voltage-coeff`

and `--inertia`

to see how that changes the ideal PID parameters.