image thumbnail

Online Tuning using the Closed-Loop PID Autotuner Block

version 1.0.14 (37.8 KB) by Quanser
Perform an online tuning of a proportional-integral (PI) speed controller for a servo DC motor using the Closed-Loop PID Autotuner block.


Updated Wed, 17 Nov 2021 17:48:33 +0000

View License

In this post, we demonstrate how to use the Closed-Loop PID Autotuner block, part of the Simulink Control Design™ toolbox, to perform an online tuning of a proportional-integral (PI) speed controller for a servo DC motor. The controller is validated using the Quanser QLabs Virtual QUBE-Servo 2, which is the digital twin of a physical Quanser QUBE-Servo 2. This submission originally apeared as a blog on the Quanser website.
What is Simulink’s Closed-Loop PID Autotuner Block?
The Closed-Loop PID Autotuner block is part of the Simulink Control Design™ toolbox and it lets you tune a PID controller in real-time starting with a set of initial controller gains that results in a stable loop. The advantages of autotuning methods such as this – and why it’s used in industry so much – is the system remains under closed-loop control with the initial PID gains and the gains are tuned without needing a model of the plant. It is a very practical technique. The block lets you tune your controller to achieve a specified bandwidth and phase margin without a parametric plant model.
Schematic diagram of how to use the PID Autotuner block
How Does it Work?
The diagram below shows how the PID Autotuner block could be integrated into a PID controller. The block works by performing a frequency-response estimation experiment by injecting test signals into your plant. The test signals are sinusoids at the frequencies [1/10, 1/3, 1, 3, 10]ωc, where ωc is the specified target bandwidth for tuning. You can start the autotuning process by applying a non-negative value to the start/stop port, or applying a zero or negative value to stop the tuning process. The autotuner finds PID gains based on the estimated frequency response and the desired bandwidth and phase margin. Once the autotuning process has stopped, the tuned PID gains can be applied to the PID controller block to validate the closed-loop performance in real-time. Keep in mind that until the autotuning process begins, the block relays the PID control signal, u, directly to the plant input, u+Δu.
Before You Run this Example
In additon to the required MathWorks products, this example requires the Quanser QLabs Virtual QUBE-Servo 2 software, which is the digital twin on the physical Quanser QUBE-Servo 2 unit. If you don’t have QLabs, sign up for a free trial.
How to Run the Example
  • Open QLabs. Click on QUBE 2 – DC Motor, then click on the Servo Workspace.
QLabs Virtual QUBE-Servo 2
  • Download the supplied Simulink model, self_tuning_PI_controller.slx
  • The Simulink model implements a PI speed controller. A square wave with a frequency of 0.4 Hz is commanded to the digital twin to alternate the speed of the load shaft between 10 rad/s and 50 rad/s. The model starts off by using kp and ki values of 0.2 and 0.1, respectively. These initial gains were selected such that they would not result in an unstable system.
  • The Autotuner block is configured with a desired bandwidth and phase margin values of 75 rad/s and 60 deg respectively. With the Start Tuning manual switch set to 0 (effectively bypassing the Autotuner block), run the model. Below is the untuned response that you should observe.
Untuned system response
  • Toggle the Start Tuning manual switch block. The Autotuner block will started to interject test signals int the digital twin plant. The sine amplitude of the test signals is set to 0.1. You can change that value to see how it affects the results. Below is the response of the digital twin while being subjected to the test signals.
System response while being subjected to test signals
  • The One Shot block is used to produce a pulse signal that would run the Autotuner block for 4 seconds. During this time, the model monitored the %conv output of the Autotuner block. Once the value converged to 98%, tuning is considered complete, and the Simulink diagram displayed the tuned gains as well as the estimated phase margin. Additional logic is implemented to transfer the tuned gains generated by the autotuning block to the PI controller. This means that you do not have to stop my model to apply the tuned gains to the plant.
  • Below is the system response upon completion of the autotuning process and applying the new controller gains. You can see a far better response compared to the untuned controller we started with. The resulting estimated phase margin was 58 deg (very close to the desired 60 deg) and the calculated tuned gains were kp = 0.38 and ki = 2.68.
Tuned response of the controller

Cite As

Quanser (2022). Online Tuning using the Closed-Loop PID Autotuner Block (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020a
Compatible with R2020a to R2021b
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!