Language: EN

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

How to adjust a PID controller in Arduino

We continue with this mini-series of posts dedicated to control systems in Arduino. The most feared moment for many arrives, making the adjustment or tuning of the PID.

In previous posts, we have seen what a controller is, hysteresis control, and we have introduced the powerful PID controller. We emphasize 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 said 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 always make it work well.

Depending on the value we put on the parameters, we will obtain a good response, or a slow response, or oscillating, or even … remember, death, destruction, dismissal.

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

The goodness and success of our controller depends entirely on the adjustment. Therefore, it is important that we know the characteristics and effects of each factor on the system’s response.

Summary of the effects of the PID

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
  • It does not eliminate the steady-state error

Integral

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

Derivative

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

On the other hand, with 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

Adjusting the PID

There are many ways to adjust a PID, some more or less theoretical. Let’s take a brief look.

Plant characterization

It consists of determining the transfer function of the plant. Common practices are to apply a step or frequency input, and analyze the characteristics of the output.

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

Application of tuning rules

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

Auto tuning

Similar to the previous one, but carried out automatically by the controller. PID controllers have advanced a lot and, currently, many have great auto-tuning algorithms.

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

Manual adjustment

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

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

Manual PID adjustment

To adjust 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 looking at how it turns (or sounds).

If you look for tutorials on the internet, you will mainly see two “schools”.

Proportional/integral/derivative adjustment

  • Adjust K until the system overshoots or starts to oscillate
  • Adjust Ki to eliminate steady-state error
  • Increase Kd until weird things start to happen

Proportional/derivative/integral adjustment

  • Adjust K until the system overshoots or starts to oscillate
  • Increase Kd until weird things start to happen
  • Adjust Ki to eliminate steady-state error

With both, you will achieve similar results, ultimately. Personally, I recommend the first one and I do not understand why they insist on explaining the second one.

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

And to practice manual parameter adjustment, here is a PID simulator, with a generic plant that has a little bit of everything (it is a second-order with a bit of pure delay).

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

K

Ki

Kd

You can play with the different methods to adjust the PID until you get 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 exceeding the setpoint a bit in exchange for more speed? Do we prefer it to be slower, but with less effort in the motor? It depends on your design criteria.

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