Language: EN

teoria-de-control-en-arduino-el-controlador-pid

Arduino Control Theory - The PID Controller

We continue with the post dedicated to giving some basic knowledge of automatic control systems on a processor like Arduino. In this post we will look at the PID controller, one of the most widely used controllers due to its simplicity and its ability to achieve good performance in a wide variety of situations.

In the first post, we saw some generalities and definitions about system theory. In the second one, we saw one of the simplest controls, the all or nothing control with hysteresis, as well as its limitations.

Of course, we insist that we are not going to go into the details of the PID, since it is a very extensive (and interesting) topic. If you are interested, you can consult the abundant documentation available.

Here we will try to give an intuitive vision of the controller and the motivation that explains its behavior. Without using equations or mathematics. In fact, one of the great things about the PID is that you do not need to know in detail how it works to make it work.

What is the PID controller?

The PID controller is one of the most widely used in the industry for controlling feedback systems. Some of its strengths are its simplicity and its ability to achieve a good performance in a wide variety of situations without the need to know in detail the plant to be controlled.

The PID controller has been known for a long time. Its first uses date back to 1911, and its first theoretical analysis in 1922 by Nicolas Minorsky. In those times, the PID control was exclusively analog. However, it is easy to implement a digital PID in programming and the calculations it requires are simple and efficient. Also, it is “relatively simple” to extrapolate the theory of analog PIDs to its digital equivalent.

Despite its popularity, we must say that currently the PID is not the best controller available. But in most cases, it is more than enough. On the other hand, many of the “most modern” controllers are nothing more than improved versions of a PID, such as the different families based on PID with adaptive parameters.

How does the PID work?

The PID algorithm (proportional, integral, derivative) is formed by the sum of three components, Proportional, Integral, and Derivative. Mathematically, a PID controller has the following formulation.

One moment, one moment. We said no equations! Well yes, but we can’t do a post about PID without seeing at least its formulation. I promise there won’t be more, okay?

Each component of the PID is “independent” of the others, in the sense that each one calculates an output of what “for it” you should do to obtain the appropriate response.

The three components are added together to give the controller’s output. Each one fulfills a certain function and improves a certain part of the response. And when the three components work together, in the right proportion, they achieve great performance.

arduino-pid-esquema

Each component has a parameter Kp, Ki, and Kd, respectively. These parameters indicate the weighting (or “strength”) in the final result. For the PID response to be good, broken things, death, destruction, and dismissal, reside in the correct adjustment of these three parameters.

And here comes the “funny” part of the PID. In the three parameters, if you put a very low value, the effect of the component on the output will not be noticed. And if you put it too high… well, that’s it, death, destruction, broken things, etc.

Furthermore, in the overall response of the controller, the three components work together and influence each other, so it is not enough to adjust each of the parameters independently. There is a certain “zone” within the three parameters, where the behavior is more or less good.

Logically, it is clear that the difficulty (which is not that much) of a PID is to adjust the parameters K, Ki, and Kd, so that the behavior is good. And that’s what we’ll dedicate the next post to.

Explaining the PID

To explain the PID controller, we will continue with our example. Remember that in this case you are the controller and you are sitting in a room. On the wall you have two displays, one blue and one red. You also have a lever.

teoria-control-arduino-que-es-un-controlador

All you know is that the lever controls the power of the building’s air conditioning system. The red number is the actual temperature of the building, and the blue one is the one we want it to be. Your mission, logically, is for the actual temperature of the building to be equal to the setpoint.

Beyond that, you don’t know anything. You don’t know what the building is like, what your air conditioning system is like, if all the windows are open, if 100 people are entering the building, if it’s dawning, if it’s winter. You don’t even know if moving the lever causes a small effect, or if you’re going to incinerate someone.

As a positive point, we have a continuous (or semi-continuous) input and output, which is a requirement for a PID controller. As we saw in the previous post, if the sensor or actuator were on/off type, we would have to go to an all-or-nothing controller or a controller with hysteresis.

As we mentioned, the PID controller is based on three components, PID. Its “strength” lies in the role that each of these components plays in the response. To have it in mind, here is a summary.

  • The proportional component reacts to the present
  • The integral component reacts to the past, and provides “memory” to the controller.
  • The derivative component reacts to the future, and provides “prediction” to the controller

Next, we will go in depth into each of the components, seeing their motivation, operation, and behavior.

PID Simulation

To check the behavior of the PID components, we have this simulation where you can adjust the values of Kp, Ki, and Kd to visualize the influence of each parameter on the output and the action requested by the controller.

Move the Kp, Ki, and Kd sliders to see the changes in the controller’s response.

K

Ki

Kd

PID Components

For this, we will use our simple example of waking up in the middle of a control room, without knowing anything else. Unless stated otherwise, the setpoint (blue display) is always at 24ºC.

Component P - Proportional

Motivation

The motivation of the proportional component is probably the most intuitive to explain. If you enter the room and see 12ºC, it seems logical that you have to give more action to the lever than if it is 23ºC. Right?

Well, that is the motivation of the proportional component, which makes sense that you apply more action the further you are from the desired value, and vice versa.

Formulation

The proportional component is simply calculated as a factor K multiplied by the error (difference between setpoint and actual value).

Behavior

The proportional factor has a great influence on the speed of response of the system. If we have a small K, the system will take a long time to reach the setpoint, because we are giving “little power” to the actuator. If we increase it, we manage to decrease the response time.

But if we increase K too much, we may exceed the setpoint, oscillate, or even oscillate and mess everything up. Let’s see how that happens.

If you have a K that is too large, you will give too much to the lever. So, when you see 18ºC, you move the lever and the next value is 25. You’ve gone too far. Lower the lever, 21, you’ve now gone down. Raise the lever, 23. Okay, you’ve got it.

But if you have an even larger K, it could happen that when you see 18º, you give A LOT to the lever, and the next thing you see could be 32ºC. Oops, I’ve gone too far, LOWER THE LEVER! 14º RAISE RAISE! 45º… 8º… 56º… You have just made your system oscillate and be unstable (death, destruction…).

Another characteristic is that, in general, the proportional component does not completely eliminate long-term error. In our example, suppose that at 22ºC and a certain position of the lever, the energy we provide to the building is exactly what the building needs to maintain the temperature. We would have achieved a stable temperature, but since the position of the lever is given by the error (which has stabilized), we will never be able to raise the remaining 2ºC.

Component I - Integral

Motivation

The function of the integral component may be the most complicated to explain within the PID. Imagine that you are in the room, and we have the same 22ºC as before. The proportional tells you to place the display in a position and you do it. A minute passes, another, another… and it’s still at 22ºC, 2ºC below what we want.

The integral component is the one that says… hey guys, maybe we need to raise the lever a little, right? Because we’ve been at 22ºC for a while without moving the lever, and it doesn’t look like it’s going to change. Well, that voice with so much sense, which reacts to the memory of past errors, is the integral component.

Formulation

The integral component applies an action that is proportional to the integral of the error over time. Equivalently, it responds proportionally to the sum of all previous errors.

Graphically, it corresponds to the area enclosed under the error curve, which is also the same as the area between the output and the setpoint. In the discrete domain, the integral is replaced by a discrete method for its calculation, such as calculation using rectangles or trapezoids.

Behavior

The integral component allows the controller to completely eliminate long-term error. However, if Ki is very small, the system will take a long time to eliminate the error.

On the other hand, keep in mind that the integral term only responds to the area between the output and the setpoint. A consequence of this is that if we have accumulated (for example) an error for being below the setpoint, the only way the integral term has to compensate for it is to be above for a while.

And indeed, the integral component tends to exceed and oscillate, even more than the proportional term.

Component D - Derivative

Motivation

The derivative component is also simple to explain. Let’s say you are comfortably at the setpoint temperature of 24ºC. Everything is perfect! The temperature drops to 23ºC, then to 22ºC, and you raise the lever a little. All good, very calm.

Now imagine that, when you were at 24ºC, the temperature suddenly drops to 18ºC. Oops, that’s a good drop. You raise the lever. It shows 12ºC… I don’t know if they opened a window or what’s going on, but this is dropping very quickly! Next value 2ºC GIVE GIVE GIVE, it’s going out of control in seconds!

That is the function of the derivative component, reacting to variations in the error (due to a change in setpoint or the variable). Because it is not the same to be at 12ºC and rising slowly, as if the temperature is plummeting at full speed. In other words, the derivative term responds to the rate of change of the error.

Formulation

The derivative component is calculated proportionally to the derivative of the error with respect to time at the present moment. Equivalently, it responds proportionally to the difference between the current error and the error at the previous instant.

In the discrete domain, the derivative is replaced by the difference between the current error and the previous error, divided by the sampling time (or this division is completely omitted and included in the Kd coefficient, if the sampling time is constant).

Behavior

The derivative component improves the overall response of many systems for moderate values of Kd. However, if we go overboard with Kd, we will see a lack of “smoothness” in the response, and other “weird” behaviors.

In addition, the derivative component responds very poorly to measurement noise. Noise, a high-frequency variation, means very rapid variations, even if they are of small amplitude. These variations are amplified by the derivative component.

And, finally, the derivative component is sometimes a little “rough” because it demands very large actions. Imagine, for example, an instantaneous change in the setpoint. The derivative component would demand an infinite action that the actuator cannot satisfy. Since the actuator cannot provide the action requested by the controller, we would have deviations from what was calculated, or we could even damage the actuator.

Conclusion

We have seen the PID control, one of the most widely used and versatile PID controllers that can control a wide variety of systems with good results, without the need to characterize the plant.

The behavior of the PID is based on the sum of its three components, which are controlled through the constants Kp, Ki, and Kd. The three constants have to be calibrated for the plant to be controlled, and the response of the system depends on this adjustment being good, bad, or very bad.

The process of adjusting the constants is called “tuning” a PID controller, and in the next post in the series, we will see different strategies for adjusting the PID. See you soon!