como-ajustar-un-controlador-pid-en-arduino

How to adjust a PID controller

  • 5 min

We continue with this mini-series of posts dedicated to control systems in Arduino. The moment feared by many has arrived: doing the PID adjustment or tuning.

In previous posts, we have seen what a controller is, control with hysteresis, and we have introduced the powerful PID controller. We highlighted that, without being the perfect controller, the PID is popular for being relatively simple and achieving good behavior in a large number of plants, without knowing their nature.

We also stated that the response of a PID depends on the parameters K, Ki, and Kd of the controller. And that these depend on the plant we are controlling; that is, there are no “magic values” that make it work well every time.

Depending on the value we assign to the parameters, we will obtain a good response, or a slow response, or an oscillating one, or even… remember: death, destruction, getting fired.

To make it more complicated, the effect of the factors on the response is not independent. Their contribution mixes and intermingles. Therefore, we cannot adjust the parameters “one by one”.

The goodness and success of our controller depend entirely on the tuning. For this, it is important that we know the characteristics and effects of each factor on the system’s response.

Summary of PID effects

Summarizing what we saw in the previous post, as a cheat sheet:

Proportional Component

  • Low K: slow response
  • High K: overshoot, oscillation, and even instability
  • Fails to eliminate steady-state error

Integral

  • Eliminates steady-state error
  • Too much Ki: oscillation and instability

Derivative

  • Improves general behavior
  • Too much Kd: “weird” behavior in the output
  • Very sensitive to noise
  • Very sensitive to sudden changes in error (disturbances or setpoint changes)

On the other hand, facing an oscillating input, we will get an oscillating response. It is important to know:

  • The effect caused by K has the same frequency.
  • The effect caused by Ki has a different frequency.

Tuning the PID

There are many ways to tune a PID, some more theoretical than others. Let’s do a quick review.

This consists of determining the transfer function of the plant. Common practices include applying a step or frequency input and analyzing the output characteristics.

Then we can operate mathematically to perform an exact calculation of the controller for the required response.

Similarly, this consists of applying a specific input to the system and measuring the output. Then, one of the many tuning rules (famous example: Ziegler-Nichols rules) is applied to obtain the PID parameters.

Similar to the previous one, but performed automatically by the controller. PID regulators have advanced a lot and, nowadays, many have great auto-tuning algorithms.

Precisely, this auto-tuning capability is one of the reasons for the commercial success of PID controllers.

Finally, we have “manual” tuning. Which, although it sounds terrible, is a very valid and very popular option. On many occasions, a human operator can achieve a tuning as good as (or even superior to) a generic Auto tuning.

And this is the one we are going to focus on in this post, because it is much less theoretical and more appropriate for #maker projects (robots, light control, etc.) which are frequently done with Arduino.

Manual PID Tuning

To tune the PID manually, we must be able to act on the system input, on the PID parameters, and visualize the response. And here, “visualize” does not necessarily mean using an oscilloscope. In the case of a motor, for example, we can “see” the system response simply by watching how it turns (or how it sounds).

If you search for tutorials online, you will mainly see two “schools of thought”.

Proportional/Integral/Derivative Tuning

  • Adjust K until the system overshoots or starts to oscillate.
  • Adjust Ki until steady-state error is eliminated.
  • Increase Kd until weird things start happening.

Proportional/Derivative/Integral Tuning

  • Adjust K until the system overshoots or starts to oscillate.
  • Increase Kd until weird things start happening.
  • Adjust Ki until steady-state error is eliminated.

With both methods, you will achieve similar results ultimately. Personally, I advise the first one, and I don’t understand why they insist on explaining the second one.

The reason is that the derivative term is very sensitive to measurement noise and, therefore, is difficult to adjust in a real case. But, you can try for yourselves.

And so you can practice manual parameter tuning, here is a PID simulator, with a generic plant that has a bit of everything (it’s a second-order system with a tiny bit of pure delay).

Move the Sliders for Kp, Ki, and Kd to see the changes in the controller response.

You can play with the different methods to tune the PID until you achieve an adequate response. (Hint: around K = 2.5, Ki = 0.02, Kd = 12 looks good).

Of course, the parameters depend on the response you want. Do we accept overshooting the setpoint a little in exchange for more speed? Do we prefer it slower, but with less effort on the motor? It depends on your design criteria.

And we have finished the topic of PID tuning. In the next post, we will see how to implement a PID control in Arduino.