## PID Autotuning for a Plant Modeled in Simulink

To use PID autotuning for a plant modeled in Simulink®, you incorporate a PID autotuner block into the model. You can control the autotuning process while the model is running. When tuning is complete you can validate tuned controller parameters against the simulated plant. Using PID autotuning this way can be useful for generating an initial PID design that you later refine with real-time autotuning.

### Workflow for Autotuning in Simulink

The following steps provide a general overview of the workflow for PID autotuning in Simulink using the Closed-Loop PID Autotuner or Open-Loop PID Autotuner blocks.

1. Incorporate a PID autotuner block into your model between the PID controller and the plant.

2. Configure the start/stop signal that controls when the tuning experiment begins and ends.

3. Specify controller parameters such as controller type and the target bandwidth for tuning.

4. Configure experiment parameters such as the amplitudes of the perturbations injected during the frequency-response experiment.

5. Run the model and initiate tuning. Use the start/stop signal to initiate the PID autotuning process. When you start the process, the autotuner block injects test signals and measures the response of the plant.

6. Stop the experiment with the start/stop signal. When the experiment stops, the autotuner block computes and returns tuned PID gains. You can examine the tuned gains for reasonableness.

7. Transfer the tuned gains from the autotuner block to your PID controller. You can then validate the performance of the tuned controller in Simulink.

### Step 1. Incorporate Autotuner into Model

The following illustration shows one way to incorporate a Closed-Loop PID Autotuner block in between your PID controller and your plant.

The control signal u from the PID controller feeds into the u port of the autotuner block. The u+Δu port feeds into the plant input. Before you begin the autotuning process, the autotuner block feeds the PID control signal directly from u to u+Δu and the plant input. In that state, the autotuner block has no effect on plant or controller behavior. During the autotuning process, the block injects test signals at the plant input and measures the response at y.

The start/stop signal controls when the autotuning process begins and ends (see Step 2. Configure Start/Stop Signal). When the experiment ends, the block calculates PID gains and returns them at the pid gains port.

For a more detailed example of a Simulink model configured for closed-loop PID autotuning, see Tune PID Controller in Real Time Using Closed-Loop PID Autotuner Block.

#### Bumpless Transfer for Open-Loop Tuning

The Open-Loop PID Autotuner block opens the loop between u and u+Δu during the estimation experiment. If your controller includes integral action, you can use signal tracking to avoid integrator windup while the loop is open. Signal tracking enables the PID controller to continue to track the real plant input while it is out of the loop. Without it, your system can experience a bump when the control loop is closed at the end of the tuning process. In system of the following illustration, the PID controller is a Simulink PID Controller block with the Enable tracking mode parameter on. The plant input feeds into the tracking input of the controller block.

For a more detailed example of a Simulink model configured for open-loop PID autotuning, see Tune PID Controller in Real Time Using Open-Loop PID Autotuner Block.

### Step 2. Configure Start/Stop Signal

To start and stop the autotuning process, use a signal at the start/stop port. When the experiment is not running, the block passes signals unchanged from u to u+Δu. In this state, the block has no impact on plant or controller behavior.

The frequency-response estimation experiment begins and ends when the block receives a rising or falling signal at the start/stop port, respectively. In the systems illustrated in Step 1. Incorporate Autotuner into Model, the start/stop signal is a simple switch. While the model is running, you can use the switch to begin and end the experiment. When you end the experiment, the algorithm generates the tuned PID gains and the block returns them at the pid gains port.

As an alternative to a manual switch, you can configure the start/stop signal to begin and end the experiment automatically at particular simulation times. For example, you can use the sum of two Step blocks: Configure one Step block to step from 0 to 1 at the experiment start time, and a second Step block to step from 1 to 0 at the end time. Feed the sum of the two signals into the start/stop port of the PID autotuner block.

You can configure any other logic appropriate for your application to control the start and stop times of the experiment. For more information about when to start and stop the experiment, see Step 5. Run Model and Initiate Tuning Experiment.

### Step 3. Specify Controller Parameters and Tuning Goals

In the PID autotuner block, specify the configuration of the PID controller you are tuning, using the following block parameters:

• Type

• Form

• Time Domain

• Controller sample time (sec)

• Integrator method

• Filter method

Then, specify the target bandwidth and phase margin for tuning with the Target bandwidth (rad/sec) and Target phase margin (degrees) parameters, respectively.

The target bandwidth, specified in rad/sec, is the target value for the 0-dB gain crossover frequency of the tuned open-loop response CP, where P is the plant response, and C is the controller response. This crossover frequency roughly sets the control bandwidth. For a desired rise-time τ seconds, a good guess for the target bandwidth is 2/τ rad/sec.

The target phase margin reflects your desired robustness of the tuned system. Typically, choose a value in the range of about 45°– 60°. In general, higher phase margin improves overshoot, but can limit response speed. The default value, 60°, tends to balance performance and robustness, yielding about 5-10% overshoot, depending on the characteristics of your plant.

For more information about setting these parameters, see the Closed-Loop PID Autotuner or Open-Loop PID Autotuner block reference pages.

### Step 4. Set Experiment Parameters

The frequency-response estimation experiment injects sinusoidal signals at frequencies around the target bandwidth ωc:

• [1/3, 1, 3, 10]ωc for the Open-Loop PID Autotuner block

• [1/10,1/3, 1, 3, 10]ωc for the Closed-Loop PID Autotuner block

Use the Sine Amplitudes parameter of the blocks to specify the amplitudes of these signals.

If your plant is asymptotically stable, the Open-Loop PID Autotuner block can estimate the plant DC gain with a step perturbation. Specify the amplitude of this perturbation with the Step Amplitude parameter. If your plant has a single integrator, clear the Estimate DC gain with step signal parameter.

Caution

• Do not use either closed-loop or open-loop PID autotuning with an unstable plant.

• Do not use open-loop PID autotuning with a plant that has more than one integrator. You can use closed-loop PID autotuning with a multiple-integrator plant.

All the perturbation amplitudes must be:

• Large enough that the perturbation overcomes any deadband in the plant actuator and generates a response above the noise level.

• Small enough to keep the plant running within the approximately linear region near the nominal operating point, and to avoid saturating the plant input or output.

For more information about setting the experiment parameters, see the Closed-Loop PID Autotuner and Open-Loop PID Autotuner block reference pages.

### Step 5. Run Model and Initiate Tuning Experiment

After you have configured all the parameters for tuning, run the model.

• If you have configured a manual start/stop signal, begin the experiment when your plant has reached steady-state.

• If you have configured the start/stop signal to begin and end the tuning process at specific times, allow the simulation to run long enough to begin the experiment.

### Step 6. Stop Experiment and Examine Tuned Gains

The frequency-response estimation experiment ends when the start/stop signal falls.

• If you have configured a manual start/stop signal, end the experiment when the signal at the % conv output stabilizes near 100%.

• If you have configured the start/stop signal to begin and end the tuning process at specific times, allow the simulation to run through the end of the experiment.

In either case, a conservative estimate for the experiment time is 200/ωc for closed-loop tuning or 100/ωc for open-loop tuning, where ωc is your target bandwidth.

When you stop experiment, the block computes new PID gains based on the estimated frequency response of the system and your specified tuning goals. Examine them for reasonableness. For instance, if you have an initial PID controller, you might expect the tuned gains to be roughly the same magnitude as the gains of the initial design. There are several ways to see the tuned gains:

• View the output of the pid gains port of the autotuner block. One way to view this output is to connect the output to a Simulink Display block.

• In the block, in the Block tab, click Export to MATLAB. The block creates a structure in the MATLAB® workspace, OnlinePIDTuningResult. For more information about the contents of this structure, see the Closed-Loop PID Autotuner or Open-Loop PID Autotuner block reference pages.

### Step 7. Update PID Controller with Tuned Gains

The autotuner block can write tuned controller parameters directly to the PID controller block, if your PID controller is either:

• A Simulink PID Controller block.

• A custom PID controller for which the following conditions are both true:

• The custom controller is a masked subsystem.

• The PID gains are mask parameters named P, I, D, and N. (You do not need to use all four parameters. For example, if you use a custom PI controller, then you only need mask parameters P and I.)

To configure the autotuner block to write tuned gains to your controller, designate the controller as the associated PID block in the PID autotuner block parameters. (For more information, see the see the Closed-Loop PID Autotuner or Open-Loop PID Autotuner block reference pages.) Then, update your controller by clicking Update PID Block. You can update the PID gains while the simulation is running. Doing so is useful for immediately validating tuned PID gains.

Note

At any time during simulation, you can change tuning or experiment parameters, start the experiment again, and push the new tuned gains to the PID block. You can then observe the behavior of your plant as simulation continues with the new gains.

#### Manual Update of PID Gains

If your custom PID controller does not satisfy the conditions for direct update, you must transfer the tuned gains to your controller some other way, such as manually or with your own logic.

When you examine these gains and transfer them to your own controller, be aware of the meaning of these gains in the PID autotuner blocks. In discrete time, the blocks assume the following PID controller transfer function:

$C=P+I{F}_{i}\left(z\right)+D\left[\frac{N}{1+N{F}_{d}\left(z\right)}\right],$

in parallel form, or in ideal form,

$C=P\left[1+I{F}_{i}\left(z\right)+D\left(\frac{N}{1+N{F}_{d}\left(z\right)}\right)\right].$

Fi(z) and Fd(z) depend on the values you specify for the Integrator method and Filter method formulas, respectively. For more details, see the Closed-Loop PID Autotuner or Open-Loop PID Autotuner block reference pages.