Video length is 6:00

How to Automatically Tune PID Controllers

Learn how to automatically tune PID controllers, whether you have an existing mathematical model of your dynamic system or you are tuning your PID parameters based on the response of an algorithm running on hardware. Automatic PID tuning can work for a variety of systems and will save development time by providing a good initial set of control parameters for your control system.

Published: 22 Sep 2020

In this video you will learn how to automatically tune PID controllers using two methods.  If you have a mathematical model for your system, we will show you how to use simulations and interactive PID tuning tools. On the other hand, if don’t have a model of your plant, we will show how to generate tests that run in the hardware used by your physical plant, in order to automatically calculate optimized PID gains.

To tune a PID controller using a simulation, start from a mathematical model representing the dynamics of the plant that you want to stabilize. A plant can be simulated using representations such as a transfer function or state space models, or the behavior can also be simulated using a physical model of the system based on the domain of the plant you are trying to control. Such as electrical or mechanical models developed with Simscape. In this case we are using the transfer function of a DC motor with input voltage and output shaft angle. Then set up the architecture for your controller using the PID Controller block in Simulink and select the type of controller you would like to implement using the options provided in the dialog window. Then click the “Tune” button in the automated tuning section of the block parameters. In this example our plant is linear, but if you your plant is non-linear, this process will linearize your plant with respect to its operating point and then calculate an initial set of control gains. Once it has finished, it will bring up the tuned response of your system in the PID tuner app. By default the tuning process calculates a set of gains for balanced performance, but you can further adjust your response by selecting options to prioritize between reference tracking or disturbance rejection, and you can fine tune your parameters by interacting with the “Response time” and “Transient Behavior” controls. Once you are satisfied with the response of your closed loop system, you can click the “Update block” button to send the calculated parameters back to your PID block. And finally, verify the system behavior using the simulation model, this verification will be more crucial for more complex non-linear systems. It is also worth noting that if you prefer a more programmatic approach you can setup your control architecture and the tuning process using MATLAB functions. For more information on that check out the links in the description.

On the other hand if you don’t have a model for your plant, or your system presents non-linear or complex behavior difficult to replicate in simulation, you can create an automated test to be uploaded to your hardware and as a result will provide you with a set of optimized PID  gains.

For the purpose of this example we are controlling the rotation of a wheel by setting the motor velocity and using the information from a rotational encoder attached to the motor shaft. In this case we will also start by setting up a stable control architecture in Simulink using the PID control block. However, we will now include the Closed-Loop PID Autotuner block in our model. This block should be placed in the signal connecting your PID controller to your plant and requires as an input the plant measurements. The algorithm will inject a test signal into the controller output in order to estimate the frequency response of your system. However, the starting gains set in the PID control block will be used throughout the experiment, so the system remains stable in the closed-loop configuration. Since the auto tuning algorithm is meant to execute a triggered test, you also get the option of controlling when the test starts and stops using the third input signal, and the third block output will contain the finals PID gains calculated by the algorithm. The minimum test setup includes making sure that the controller options matches those of your PID block, a set of tuning goals for the response of your controller, and a coefficient for the magnitude of the test signals injected into the system. In this case I would like my controller to respond within half a second or at 2 hertz bandwidth, and since the range of velocities of my motor is -100 to 100, I will set the disturbance magnitude to 10, which is a conservative fraction of that actuation range. To determine whether the test is successful or not, you can monitor the test convergence using the second output. Once we start the test, this value should converge quickly, once it gets close to 100% you can stop the test and record the PID gains calculated.

In this case I will  generate and deploy C code to my microcontroller and also interact directly with the hardware by using the hardware monitor and tune functionality included within available hardware support packages, but you can also generate standalone C code and integrate it with your preferred testing method. After the test has been enabled and the convergence is close to 100% you can stop the test and get the new PID gains. it is also possible to update the gains on your PID block directly from the block tab of the autotuner block parameters. In this case we improved our rise time by X seconds compared to the initial gains.

Thanks for watching, I hope you enjoyed this video. And if you are interested in learning more about PID controllers in general or tuning methods based on MATLAB and Simulink please check the links in the description below.