Tune PID Controller in Real Time Using Closed-Loop PID Autotuner Block

This example shows how to use the Closed-Loop PID Autotuner block to tune a PID controller for a boost converter plant in both simulation and real time.

Introduction of Voltage-Mode Controlled Boost Converter

In this example, a voltage-mode boost converter is modeled in Simulink® using Simscape™ Electrical™ components. The parameters of these components are based on [1].

mdl = 'scdboostconverterPIDTuning';

A boost converter circuit converts a DC voltage to another, typically higher, DC voltage by controlled chopping or switching of the source voltage. In this model, a MOSFET driven by a pulse-width modulation (PWM) signal is used for switching. A digital PID controller adjusts the PWM duty cycle to maintain the load voltage Vout at its reference Vref.

At the nominal operating point, the load voltage is at 18 volts and the duty cycle is about 0.74. Duty cycle can vary from 0.1 to 0.85 during boost converter operation.

The existing PID controller has gains of P = 0.02, I = 160, D = 0.00005 and N = 20000. They are stored in a Data Store Memory block and provided externally to the PID block. Having external gain inports allows you to change them after new gains are computed by the Closed-Loop PID Autotuner block.

Introduction of Closed-Loop PID Autotuner Block

The Closed-Loop PID Autotuner block allows you to tune a single-loop PID controller in both simulation and real time. It injects sinusoidal perturbation signals at the plant input and measures the plant output during a closed-loop experiment. When the experiment stops, the block computes PID gains based on the plant frequency responses estimated near the desired bandwidth.

The Closed-Loop PID Autotuner block supports two typical PID tuning scenarios in real time applications:

(1) Deploy the block on hardware and use it in a stand-alone real time application, without the presence of Simulink.

(2) Deploy the block on hardware but monitor and manage the real time tuning process in Simulink, using the external simulation mode. External mode allows communication between the Simulink block diagram running on the host computer and the generated code running on the hardware.

This example focuses on the first scenario, deploying the block to perform the real-time tuning.

Simulink Control Design™ also provides an Open-Loop PID Autotuner block for real-time PID tuning. The main difference between the two autotuner blocks is that the Open-Loop PID Autotuner block carries out the experiment with the feedback loop open (i.e. the existing controller is not in action). To decide which autotuner block is best for your application, consider:

  • If you don't have an initial controller, use the Open-Loop PID Autotuner block to obtain one. You can continue using it to re-tune the controller or replace it with the Closed-Loop PID Autotuner.

  • If you have an initial controller, use the Closed-Loop PID Autotuner block for re-tuning. The major benefits are: (1) if there is an unexpected disturbance during the experiment, it will be rejected by the existing controller to ensure safe operation; (2) the existing controller will keep the plant running near its nominal operating point by suppressing the perturbation signals as well.

Connecting Autotuner Block with Plant and Controller

Insert the Closed-Loop PID Autotuner block between the PID block and the plant, as shown in the boost converter model. The start/stop signal starts and stops the closed-loop experiment. When no experiment is running, Closed-Loop PID Autotuner block behaves like a unity gain block, where the u signal directly passes to u+Δu.

There are a few things to be aware of when using the Closed-Loop PID Autotuner block in both simulation and real time application:

  • The plant must be either asymptotically stable (i.e. all the poles are strictly stable) or integrating. The autotuner block does not work with an unstable plant.

  • The feedback loop with the existing controller must be stable.

  • To estimate plant frequency responses more accurately in real time, minimize the occurrence of any load disturbance in the plant during the experiment. The autotuner block expects the plant output to be the response to the injected perturbation signals only, and load disturbance distorts this output.

  • Because the feedback loop is closed during the experiment, the existing controller suppresses the injected perturbation signals as well. The advantage of using closed-loop experiment is that the controller keeps the plant running near the nominal operating point and maintains safe operation. The disadvantage is that it reduces the accuracy of frequency response estimation if your target bandwidth is far away from the current bandwidth.

Configuring Autotuner Block

After properly connecting the Closed-Loop PID Autotuner block with the plant model and PID block, use the block parameters to specify tuning and experiment settings.

There are two main tuning settings in the Tuning tab:

  • Target bandwidth: Determines how fast you want the controller to respond. In this example, choose 10000 rad/sec, which is typical for a boost converter.

  • Target phase margin: Determines how robust you want the controller to be. In this example, choose the default value of 60 degrees.

There are three main experiment settings in the Experiment tab:

  • Plant Type: Specifies whether the plant is asymptotically stable or integrating. It this example, the boost converter plant is stable.

  • Plant Sign: Specifies whether the plant has a positive or negative sign. The plant sign is positive if a positive change in the plant input at the nominal operating point results in a positive change in the plant output when the plant reaches a new steady state. Otherwise, the plant sign is negative. If a plant is stable, plant sign is equivalent to the sign of its dc gain. If a plant is integrating, the plant sign is positive (or negative) if the plant output keeps increasing (or decreasing). It this example, the boost converter plant has a positive plant sign.

  • Sine Amplitudes: Specifies amplitudes of the injected sine waves. In this example, choose 0.03 for all the five frequencies of the perturbation signal to ensure the plant is properly excited within the saturation limit. If the excitation amplitude is too large, the boost converter will operate in discontinuous-current mode. If the input amplitude is too small, the sinusoidal signals will be indistinguishable from ripples in the power electronics circuits. Both situations produce inaccurate frequency response estimation results.

Simulating Autotuner Block in Normal Mode

If you have a plant model built in Simulink, it is recommended to simulate the Closed-Loop PID Autotuner block against the plant model in normal mode before deploying it for real-time tuning. Simulation will help you identify issues in signal connection and block settings so that you can adjust them before generating code.

Simulation of the boost converter plant usually takes a few minutes on your computer because of the very fast sample time of the PWM generator. Vout is the plant output and Duty Cycle is the plant input.


In this example, it takes the PID controller about 0.04 seconds to bring the boost converter to the nominal operating point. Strong oscillation is observed in the initial transient, which indicates that the existing controller needs to be re-tuned.

At 0.04 seconds, the autotuning process starts. The experiment lasts 0.02 seconds, because it typically takes about "200/bandwidth" seconds for the online frequency response estimation to converge.

When PID tuning stops at 0.06 seconds, the block calculates new gains, P = 0.04, I = 100, D = 0.00006 and N = 30000. The new gains are immediately written to the data store memory and sent to the external gain inports of the PID block, overwriting the original gains.

The model has a line disturbance (Vin from 5V to 10V) and a load current disturbance (Load from 6A to 3A). They occur at 0.07 second and 0.08 second respectively, and you can use them to examine controller performance. The new set of PID gains provides an improved closed-loop response with much less oscillation.

Using Autotuner Block in Stand-Alone Application

To tune a PID controller against a physical boost converter in a stand-alone real-time application, you need to generate C/C++ code from the Closed-Loop PID Autotuner block and deploy it on your hardware.

Here is a list of tunable parameters that you can change at run-time:

  • PID Type

  • PID Form

  • PID Integrator and Filter Methods (discrete-time only)

  • Target Bandwidth

  • Target Phase Margin

  • Plant Type

  • Plant Sign

  • Amplitudes of sine waves

Sample time of the Closed-Loop PID Autotuner block is not a tunable parameter. To use the autotuner block with different sample time without re-compilation, set the Controller Sample Time in the block dialog to -1 and put the tuner block inside a triggered subsystem. The tuner block now runs at the same sample time as the trigger signal demands.


[1] Lee, S. W. "Practical Feedback Loop Analysis for Voltage-Mode Boost Converter." Application Report No. SLVA057. Texas Instruments. January 2014. www.ti.com/lit/an/slva633/slva633.pdf


See Also

Related Topics