How PID WorksTo describe how a PID algorithm works, I’ll use the simple example of a temperature controller. For this example, we have a system that includes an electric burner, a pot of water, a temperature sensor, and a controller. The controller can read the temperature of the water using the temperature sensor, and it can adjust the power level of the burner from 0 to 100 percent. The software in the controller is responsible for adjusting the power level of the burner, such that the temperature of the water in the pot reaches a desired value (the set point) as quickly as possible and then maintains that temperature indefinitely. A PID algorithm could be used in the controller to solve this problem.
ProportionalThe first component of the PID algorithm is the simplest to understand and the most crucial to the performance of the controller. The P stands for proportional. It means that the control variable should be adjusted proportionally to the amount of error in the system. A PID algorithm that only utilized a P component could be expressed as:
I just used a couple of terms that might sound unfamiliar, so let me explain. Control Variable: The control variable is the output of the controller that we get to adjust. In our example, the control variable is the power level of the burner. Process Variable: The process variable is the measured value in the system that you are attempting to control. The process variable is used as feedback to the controller so that it can decide how to adjust the control variable. In this case, it’s the process variable is the temperature of the water. Error: The error is the difference between the process variable and the set point. In our example, the set point would be some temperature that we are trying to get the water to. The error at any point in time would be the difference between the current temperature and the target temperature. The P component of the algorithm works by adjusting the output proportionally to the error. Let’s say the set point for the temperature controller is 100 degrees. If the measured temperature is currently 60 degrees, then the error is 40. The output of the controller would be set to 40 * Kp, where Kp is the proportional coefficient. Kp is a constant, non-negative value that is determined during the tuning process of the controller (we’ll talk more about tuning later). Now let’s say that the value of Kp is 10 (pulled out of thin air). Then the output of system would be (100 – 60) * 10 = 400. You might be thinking, “400?! That’s way more than the 0 to 100% range of the burner. How can we set it to 400?” And you’re exactly right. This is called saturation, and it happens frequently in PID controllers. When saturation occurs, the control variable just needs to set the output to its maximum value, 100%. As the measured temperature approaches the target value, the error gets smaller and eventually the system becomes unsaturated. In our example, the system will become unsaturated at 90 degrees. At 95 degrees, the error would be 5 and the the output of the system would be 5 * 10 = 50%. The problem with using a proportional-only controller is that it usually results in a steady-state error. As the error gets smaller and smaller, the result of the error multiplied by the proportional coefficient eventually becomes too small to have any affect on the process variable. This is because real-world systems are never 100% efficient. And that’s where the I component of the algorithm comes into play.
output = error * Kp
IntegralThe I stands for integral, which is a mathematical term that means to accumulate something over time*. In the case of PIDs, the I component accumulates the error that occurs over time. That accumulated error is then multiplied by Ki, the integral coefficient, and added to the output. A controller algorithm that only utilized the P and I components could be expressed with the following pseudo-code:
The integral component of the control algorithm can remove any steady-state error in the system because it accumulates that error over time and compensates for it, rather than just looking at an instantaneous snapshot of the error at one moment in time.
accumulation_of_error += error * delta_time output = (error * Kp) + (accumulation_of_error * Ki)
DerivativeThe D stands for derivative and is probably the most complicated. The derivative component is used less frequently in controllers but is still important in some applications. For the case of a pot of water sitting on a burner undisturbed, the D component is not necessary at all. However, if we complicate things by occasionally dumping handfulls of ice into the pot, then it could be helpful. The derivative component is responsible for compensating for sudden changes in the error. If we dumped ice into our pot of water, the temperature would suddenly drop, the error would suddenly increase, and the derivative component of the algorithm would kick in and increase the output of the burner. A derivative is a mathematical term that means “the slope of a curve.” In this case, the curve is the plot of the error over time. If the error is steady-state, then the result of the D component is zero. A full PID algorithm could be expressed by the following pseudo-code:
accumulation_of_error += error * delta_time derivative_of_error = (error - last_error) / delta_time last_error = error output = (error * Kp) + (accumulation_of_error * Ki) + (derivative_of_error * Kd)
The derivative component is mostly useful when the system is already at or near steady state. At steady state, the P and I components are both very small because the error is very small. If the error suddenly increases it takes a while before the P and I components start kicking in again. However, the D component respond to the sudden change in error and begin compensating immediately. For this reason, it is often said that the D component is responsible for compensating for future error; it sees the error changing and tries to prevent it from changing more.
Because the derivative component responds to changes in the error, it can be problematic in situations where the measured process variable is noisy. This noise can cause the output to become unstable when the D component is utilized in the control algorithm.