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 of an engine speed control system in both simulation and real time.
Introduction of Open-Loop PID Autotuner Block
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 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 Open-Loop PID Autotuner 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 Open-Loop PID Autotuner block and an engine model.
mdl = 'scdspeedctrlOnlinePIDTuning'; open_system(mdl);
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 later.
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, Open-Loop PID Autotuner 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 Open-Loop PID Autotuner 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 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.
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 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 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 Open-Loop PID Autotuner Block in External Mode
To tune PI controller against a physical engine in the external mode, you need to 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".
Here is an example Simulink diagram configured to tune in the external mode, assuming your PI controller is running on an Arduino DUE board and communicating with your physical engine via serial ports.
Here is a summary of the changes (in that order) made to the original Simulink model in order to make it work in the external mode:
- Have a host computer that runs Simulink and communicates with an Arduino DUE board via a USB connection.
- Install "Simulink Support Package for Arduino Hardware". You need to install a different hardware support package if your hardware is different.
- In the "Configuration Parameters" dialog, choose "Fixed-Step" solver type in the "Solver" pane and the "Arduino DUE" hardware board in the "Hardware Implementation" pane.
- 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 via the "Serial Transmit" block (to actuator).
- 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. Similarily, update the PI gains by flipping a "Gain Switch" and change the reference signal by flipping a "Ref Switch".
- Choose "External Mode" in the Simulink model and set simulation time to "infinite".
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 can help deploy the generated code on hardware with limited memory resources.
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 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.
Note that due to the limited communication bandwidth between the hardware and Simulink in external mode, the controller needs to run at a relatively slow speed (typically less than 1K Hz) for proper monitoring.