Main Content

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

This example shows how to use Open-Loop PID Autotuner block to tune a PI controller for an engine speed control system in both simulation and real time.

Open-Loop PID Autotuner Block

The Open-Loop PID Autotuner block allows you to tune a single-loop PID controller in real time. It carries out an open-loop experiment that injects perturbation signals to the plant and computes PID gains based on the plant frequency responses estimated near the desired bandwidth.

The Open-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 standalone 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 external 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 second scenario, where the Open-Loop PID Autotuner block is used to tune an engine speed control system in real time using external mode.

Engine Speed Model

The Simulink model contains a PID block, an Open-Loop PID Autotuner block, and an engine model.

mdl = 'scdspeedctrlOnlinePIDTuning';

The PI controller has initial gains of P = 0.01 and I = 0.01, provided externally to the PID block via "P" and "I" inports. Having external P and I gains allows you to change them after new gains are computed by the Open-Loop PID Autotuner block.

The Open-Loop PID Autotuner block is inserted between the PID block and the engine model. The start/stop signal is used to start and stop an open-loop experiment. When no experiment is running, the Open-Loop PID Autotuner block behaves like a unity gain block, where the "u" signal directly passes to "u+Δu". When the experiment ends, the block tunes PID gains and outputs them at the "pid gains" port.

There are a few important considerations when using the Open-Loop PID Autotuner block with a physical plant in real time.

  • The plant must be asymptotically stable because an open-loop experiment is conducted during the tuning process. If your plant has a single integrator, you can still use the block by choosing not to estimate the plant DC gain. However, in both cases, you must closely monitor the plant behavior during the tuning process and intervene promptly if the plant gets too close to an undesirable operating condition.

  • To help estimate plant frequency responses more accurately in real time, there should be minimum load disturbance occurring during the tuning process. The block expects the plant output to be the response to the injected perturbation signals only, and load disturbance distorts this output.

  • The "tracking mode" (the TR inport) in the PID block is turned on, which enables the PID block to track the real plant input "u+Δu" during the tuning process. This feature should always be used to provide a bumpless transfer when the loop is closed and PID block resumes control after the tuning process is completed.

Configure Open-Loop PID Autotuner Block

After properly connecting the Open-Loop PID Autotuner block with the plant model and PID block, open the block dialog and specify tuning and experiment settings.

On the Tuning tab, there are two main tuning settings.

  • Target bandwidth: Determines how fast you want the controller to respond. In this example, choose 2 rad/sec because the desired rise time is 1 sec.

  • Target phase margin: Determines how robust you want the controller to be. In this example, choose the default value of 60 degrees, which leads to about 5% overshoot in general.

On the Experiment tab, there are two main experiment settings.

  • Sine Amplitudes: Specifies amplitudes of the injected sine waves. In this example, choose 0.1 for all four sine waves, a fraction of the nominal plant input of 9. During the tuning process, the plant output varies between 1900 and 2100 rpm, which is about +/- 5% of the nominal plant output of 2000. The goal is to keep the plant operating near the nominal operating point to avoid exciting nonlinear plant behavior.

  • Step Amplitude: Specifies the amplitude of the injected step signal. In this example, choose 0.1 as well. If the plant has a single integrator, you must not estimate DC gain. In this case, clear the Estimate DC gain with step signal parameter. As a result, no step signal is injected to the plant.

Simulate Open-Loop PID Autotuner Block in Normal Mode

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


In this example, the engine speed reference signal goes from 2000 to 3000 rpm and then back to 2000 rpm in the first 20 seconds. The original gains of P = 0.01 and I = 0.01 cause strong oscillation in the transient and must be retuned.

At 20 seconds, the plant is running at the nominal operating point of 2000 rpm and online PID tuning starts. The experiment duration is 50 seconds, because a conservative guideline suggests that it takes about 100/bandwidth seconds for online frequency response estimation to converge.

When PID tuning stops at 70 seconds, new gains P = 0.0026 and I = 0.0065 are immediately available at the "pid gains" outport and sent to the external P and I port of PID block, overwriting the original gains. There is almost no bump in transient when the loop is closed and the PID block resumes control.

The engine speed reference signal goes from 2000 to 3000 rpm and then back to 2000 rpm again between 80 and 100 seconds. The new PI gains provide a much better closed-loop response.

Using Open-Loop PID Autotuner Block in External Mode

To tune the PI controller against a physical engine in the external mode, you replace the Engine Model section in the Simulink model with hardware interface blocks that provide the rpm measurement as "y" and send throttle angle to the actuator as "u".

As an example, the following Simulink diagram is configured to tune in external mode, assuming your PI controller is running on an Arduino® DUE board and communicating with your physical engine via serial ports.

To make the original model work in external mode, the following changes were made (in order) to the original Simulink model.

  1. Have a host computer that runs Simulink and communicates with an Arduino DUE board via a USB connection.

  2. Install Simulink Support Package for Arduino Hardware software. You must install a different hardware support package if your hardware is different.

  3. In the Configuration Parameters dialog, in the Solver pane, select the "Fixed-Step" solver type. In the Hardware Implementation pane, select the "Arduino DUE" hardware board.

  4. Replace the engine model section in the original model with two serial interface blocks. In real time, the Open-Loop PID Autotuner block running on the Arduino board collects plant output from the Serial Receive block (from sensor) and sends the experiment signals to the engine using the Serial Transmit block (to actuator).

  5. For more flexibility in real-time operation, start and stop the tuning process by flipping a manual "Tuning Switch" instead of based on the simulation clock. Similarly, update the PI gains by flipping a "Gain Switch" and change the reference signal by flipping a "Ref Switch".

  6. Choose "External Mode" in the Simulink model and the set simulation time to "infinite".

Run the simulation. First, Simulink generates code for the whole model and downloads it to the Arduino DUE board. After the program starts running on the board, you can monitor the plant input and output from the scope in real time. When the plant reaches the nominal operating point of 2000 rpm, use the three manual switches to tune, update, and validate the controller.

Reduce Memory and Avoid Task Overrun in External Mode

On the Block tab, the Reduce memory and avoid task overrun (external mode only) option can help deploy the generated code on hardware with limited memory resources or very fast sample time.

If your hardware has low memory on board, use this option when tuning in external mode. With this option, Simulink only generates code for the online frequency response estimation functionality. Because no code is deployed for the PID design functionality, the result is reduced memory usage on the hardware. In this case, after the estimation is done, the PID gains are computed in Simulink on the host computer and then sent back to the autotuner block.

The PID gain calculation at the end of the tuning process demands much more computation load than the online frequency response estimation. If the controller sample time is very fast, some hardware might not be able to finish the calculation within an execution cycle. Therefore, having the host computer to perform the PID gain calculation also enables you to tune a PID controller with fast sample time on hardware with limited computing power.


See Also

Related Topics