MATLAB Examples

Tune PID Controller in Real Time Using Online PID Tuner Block

This example shows how to use Online PID Tuner block to tune a PI controller of an engine speed control system in both simulation and real time.


Introduction of Online PID Tuner Block

Online PID Tuner 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 Online PID Tuner 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 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 Online PID Tuner block is used to tune an engine speed control system in real time using the external mode.

Tuning Engine Speed Controller

The Simulink model contains a PID block, an Online PID Tuner 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 Online PID Tuner block later.

The Online PID Tuner 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, Online PID Tuner block behaves like a unity gain block, where the "u in" signal directly passes to "u out". When the experiment ends, the block tunes PID gains and outputs them at the "pid gains" port.

There are a few things you need to be aware of when using the Online PID Tuner block against 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 plant dc gain. In both cases, however, 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 out" during the tuning process. This feature should be used all the time to provide a bumpless transfer when the loop is closed and PID block resumes control after the tuning process is completed.

Configuring Online PID Tuner Block

After properly connecting the Online PID Tuner block with the plant model and PID block, open the block dialog and 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 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 that leads to about 5% overshoot in general.

There are two main experiment settings in the "Experiment" tab:

  • 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. Note that, if the plant has a single integrator, you need to choose not to estimate dc gain, and as a result, no step signal is injected to the plant.

Simulating Online PID Tuner Block in Normal Mode

If you have a plant model built in Simulink, it is recommended to simulate the Online PID Tuner block against the plant model in the normal mode before using the block in the external mode 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.


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 causes strong oscillation in the transient and need to be re-tuned.

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. Note that there is almost no bump in transient when the loop is closed and PID block resumes control at that moment.

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 provides a much better closed-loop response.

Using Online PID Tuner Block in External Mode

To tune the PI controller against a physical engine in the external mode, 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". For this example, suppose that

  • Your PI controller is running on an Arduino DUE board and communicating with your physical engine via serial ports.
  • You have a host computer that runs Simulink and communicates with an Arduino DUE board via a USB connection.
  • You have "Simulink Support Package for Arduino Hardware" installed. (If you are using different hardware, install the appropriate support package for that hardware.)

To prepare for external-mode tuning, make the following changes to the model:

  • In the "Configuration Parameters" dialog, in the "Solver" pane , choose "Fixed-Step" solver type. In the "Hardware Implementation" pane, choose the "Arduino DUE" hardware board.
  • Replace the engine model section in the original model with two serial interface blocks. In real time, the Online PID Tuner block running on the Arduino board collects plant output from the "Serial Receive" block (from sensor) and sends the experiment signals to the engine via the "Serial Transmit" block (to actuator).
  • Instead of using the simulation clock to start and stop the experiment, create a manual start/stop switch. Doing so gives you more flexibility in controlling the experiment during real-time operation. Similarly, create switches for manually updating the PI gains and changing the reference signal.
  • In the Simulink model, select "External Mode" and set the simulation time to Inf.

The following illustration shows what the model looks like after you implement these changes.

Run simulation. First, Simulink generates code for the whole model and download 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 plant reaches the nominal operating point of 2000 rpm, use the three manual switches to tune, update and validate the controller.

Decreasing Memory Footprint in External Mode

The "Decrease memory footprint (external mode only)" option in the "Block" tab is selected in this example.

If your hardware has low memory on board, use this option when tuning in the external mode. Simulink will only generate code for the online frequency response estimation functionality and deploy it on hardware. Since no code is generated for the PID design functionality, PID gains are computed in Simulink on your host computer instead of on the hardware, which help reduce memory footprint on the hardware.

PID gain calculation at the end of the tuning process demands much more computation load than 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 PID controller with fast sample time on hardware with limited computing power.