Documentation

Online PID Tuner

Automatically tune PID gains based on plant frequency responses estimated from open-loop simulation

  • Library:
  • Simulink Control Design

Description

The Online PID Tuner block lets you tune a PID controller in real time against a physical plant. The block can tune a PID controller to achieve a specified bandwidth and phase margin without a parametric plant model or an initial controller design. If you have a code-generation product such as Simulink® Coder™, you can generate code that implements the tuning algorithm on hardware, letting you tune in real time with or without Simulink in the loop.

If you have a plant model in Simulink, you can also use the block to obtain an initial PID design. Doing so lets you preview plant response and adjust the settings for PID autotuning before tuning the controller in real time.

To achieve model-free tuning, the Online PID Tuner block:

  1. Injects a test signal into the plant at the nominal operating point to collect plant input-output data and estimate frequency response in real time. The test signal is a combination of sine and step perturbation signals added on top of the nominal plant input measured when the experiment starts. If the plant is part of a feedback loop, the block opens the loop during the experiment.

  2. At the end of the experiment, tunes PID controller parameters based on estimated plant frequency responses near the open-loop bandwidth.

  3. Updates a PID Controller block or a custom PID controller with the tuned parameters, allowing you to validate closed-loop performance in real time.

Because the block performs an open-loop estimation experiment, do not use this block with an unstable plant or a plant with multiple integrators.

The block supports code generation with Simulink Coder, Embedded Coder®, and Simulink PLC Coder™. It does not support code generation with HDL Coder™.

For more information about using the Online PID Tuner block, see PID Autotuning in Real Time.

Ports

Input

expand all

Insert the Online PID Tuner block into your system such that this port accepts a control signal from a source. Typically, this port accepts the signal from the PID controller in your system.

Data Types: single | double

Connect this port to the plant output.

Data Types: single | double

To start and stop the online tuning process, provide a signal at the start/stop port. When the value of the signal changes from:

  • Negative or zero to positive, the experiment starts. When the experiment is running, the block opens the loop between u in and u out and injects test signals at u out.

  • Positive to negative or zero, the experiment stops. When the experiment is not running, the block passes signals unchanged from u in to u out. In this state, the block has no impact on plant or controller behavior.

Typically, you can control the experiment with a signal that changes from 0 to 1 to start the experiment, and from 1 to 0 to stop it. Some points to consider when configuring the start/stop signal include:

  • Start the experiment when the plant is at the desired equilibrium operating point. If necessary, you can use the source block connected to u in to drive the plant to the operating point.

  • Avoid any load disturbance to the plant during the experiment. Load disturbance can distort the plant output and reduce the accuracy of the frequency-response estimation.

  • Avoid stopping the experiment prematurely. You must let the experiment run long enough for the frequency-response estimation algorithm to collect sufficient data for a good estimate at all frequencies it probes. There are two ways to determine when stop the experiment:

    • Determine the experiment duration in advance. A conservative estimate for the experiment duration is 100/ωc, where ωc is the target bandwidth for tuning.

    • Observe the signal at the convergence output, and stop the experiment when the signal stabilizes near 100%.

  • When the experiment stops, the Online PID Tuner block computes tuned PID gains and updates the signal at the pid gains port.

Data Types: single | double

Supply a value for the Target bandwidth (rad/sec) parameter. See that parameter for details.

Dependencies

To enable this port, in the Tuning tab, next to Target bandwidth (rad/sec), select Use external source.

Data Types: single | double

Supply a value for the Target phase margin (degrees) parameter. See that parameter for details.

Dependencies

To enable this port, in the Tuning tab, next to Target phase margin (degrees), select Use external source.

Data Types: single | double

Supply a value for the Sine Amplitudes parameter. See that parameter for details.

Dependencies

To enable this port, in the Experiment tab, next to Sine Amplitudes, select Use external source.

Data Types: single | double

Supply a value for the Step Amplitude parameter. See that parameter for details.

Dependencies

To enable this port, in the Experiment tab, next to Step Amplitudes, select Use external source.

Data Types: single | double

Output

expand all

Insert the Online PID Tuner block into your system such that this port feeds the input signal to your plant.

  • When the experiment is running (start/stop positive), the block injects test signals into the plant at this port. If you have any saturation or rate limit protecting the plant, feed the signal from u out into it.

  • When the experiment is not running (start/stop zero or negative), the block passes signals unchanged from u in to u out.

Data Types: single | double

When the experiment is running (start/stop positive), the block injects test signals at u out and measures the plant response at y. It uses these signals to estimate the frequency response of the plant at several frequencies around the target bandwidth for tuning. % convergence indicates how close to completion the estimation of the plant frequency response is. Typically, this value quickly rises to about 90% after the experiment begins, and then gradually converges to a higher value. Stop the experiment when it levels off near 100%.

Data Types: single | double

This bus signal contains the tuned PID gains P, I, D, and the filter coefficient N. These values correspond to the P, I, D, and N parameters in the expressions given in the Form parameter. Initially, the values are 0, 0, 0, and 100, respectively. The block updates the values when the experiment ends.

If you have a PID controller associated with the Online PID Tuner block, you can update that controller with these values after the experiment ends. To do so, in the Block tab, click Update PID Block.

Data Types: single | double

This port outputs the estimated phase margin achieved by the tuned controller, in degrees. The block updates this value when the tuning experiment ends. The estimated phase margin is calculated from the angle of G(c)C(c), where G is the plant, C is the tuned controller, and ωc is the crossover frequency (bandwidth). The estimated phase margin might differ from the target phase margin specified by the Target phase margin (degrees) parameter. It is an indicator of the robustness and stability achieved by the tuned system.

  • Typically, the estimated phase margin is near the target phase margin. In general, the larger the value, the more robust is the tuned system, and the less overshoot there is.

  • A negative phase margin indicates that the closed-loop system might be unstable.

Dependencies

To enable this port, in the Tuning tab, select Output estimated phase margin achieved by tuned controller.

This port outputs the frequency-response data estimated by the experiment. Initially, the value at frd is [0,0,0,0]. During the experiment, the block injects signals at four frequencies, [1/3, 1, 3, 10]ωc, where ωc is the target bandwidth. When the experiment stops, the block updates frd with a vector containing the complex frequency response at each of these frequencies, respectively.

Dependencies

To enable this port, in the Experiment tab, select Plant frequency responses near bandwidth.

If you select Estimate DC gain with step signal in the Experiment tab, the block estimates the DC gain of the plant by injecting a step signal at u out. When the experiment stops, the block updates this port with the estimated DC gain value.

Dependencies

To enable this port, in the Experiment tab, select Plant DC Gain.

This port outputs a vector containing the plant input (u out) and plant output (y) when the experiment begins. These values are the plant input and output at the nominal operating point at which the block performs the experiment.

Dependencies

To enable this port, in the Experiment tab, select Plant nominal input and output.

Parameters

expand all

Tuning Tab

Specify the type of the PID controller in your system. The controller type indicates what actions are present in the controller. The following controller types are available for online PID tuning:

  • P — Proportional only

  • I — Integral only

  • PI — Proportional and integral

  • PD — Proportional and derivative

  • PDF — Proportional and derivative with derivative filter

  • PID — Proportional, integral, and derivative

  • PIDF — Proportional, integral, and derivative with derivative filter

When you update a PID Controller block or custom PID controller with tuned parameter values, make sure the controller type matches.

Tunable: Yes

Programmatic Use

Block Parameter: PIDType
Type: character vector
Values: 'P' | 'I' | 'PI' | 'PD' | 'PDF' | 'PID' | 'PIDF'
Default: 'PI'

Specify the controller form. The controller form determines the interpretation of the PID coefficients P, I, D, and N.

  • Parallel — In Parallel form, the transfer function of a discrete-time PIDF controller is:

    C=P+Fi(z)I+DN+Fd(z),

    where Fi(z) and Fd(z) are the integrator and filter formulas (see Integrator method and Filter method). The transfer function of a continuous-time parallel-form PIDF controller is:

    C=P+Is+DsNs+1.

    Other controller actions amount to setting P, I, or D to zero.

  • Ideal — In Ideal form, the transfer function of a discrete-time PIDF controller is:

    C=P(1+Fi(z)I+DD/N+Fd(z)).

    The transfer function of a continuous-time ideal-form PIDF controller is:

    C=P(1+1Is+DsDs/N+1).

    Other controller actions amount to setting D to zero or setting, I to Inf. (In ideal form, the controller must have proportional action.)

When you update a PID Controller block or custom PID controller with tuned parameter values, make sure the controller form matches.

Tunable: Yes

Programmatic Use

Block Parameter: PIDForm
Type: character vector
Values: 'Parallel' | 'Ideal'
Default: 'Parallel'

Specify whether your PID controller is a discrete-time or continuous-time controller.

  • For discrete time, you must specify the sample time of your PID controller using the Controller sample time (sec) parameter.

  • For continuous time, you must also specify a sample time for the PID autotuning experiment using the Experiment sample time (sec) parameter.

Tunable: No

Programmatic Use

Block Parameter: TimeDomain
Type: character vector
Values: 'discrete-time' | 'continuous-time'
Default: 'discrete-time'

Specify the sample time of your PID controller in seconds. This value also sets the sample time for the experiment performed by the block.

To perform PID tuning, the Online PID Tuner measures frequency-response information up to a frequency of 10 times the target bandwidth. To ensure that this frequency is less than the Nyquist frequency, the target bandwidth, ωc, must satisfy ωcTs ≤ 0.3, where Ts ωc is the controller sample time that you specify with the Controller sample time (sec) parameter.

When you update a PID Controller block or custom PID controller with tuned parameter values, make sure the controller sample time matches.

Tip

If you want to run the deployed block with different sample times in your application, set this parameter to –1 and put the block in a Triggered Subsystem. Then, trigger the subsystem at the desired sample time. If you do not plan to change the sample time after deployment, specify a fixed and finite sample time.

Tunable: No

Dependencies

This parameter is enabled when the Time Domain is discrete-time.

Programmatic Use

Block Parameter: DiscreteTs
Type: scalar
Value positive scalar | –1
Default: 0.1

Even when you tune a continuous-time controller, you must specify a sample time for the experiment performed by the block. In general, continuous-time controller tuning is not recommended for PID autotuning against a physical plant. If you want to tune in continuous time against a Simulink model of the plant, it is recommended that you use a fast experiment sample time, such as 0.02/ωc.

Tunable: No

Dependencies

This parameter is enabled when the Time Domain is continuous-time.

Programmatic Use

Block Parameter: ContinuousTs
Type: positive scalar
Default: 0.02

Specify the discrete integration formula for the integrator term in your controller. In discrete time, the PID controller transfer function assumed by the block is:

C=P+Fi(z)I+DN+Fd(z),

in parallel form, or

C=P(1+Fi(z)I+DD/N+Fd(z)).

in ideal form. For a controller sample time Ts, the Integrator method parameter determines the formula Fi as follows:

Integrator methodFi
Forward Euler

Tsz1

Backward Euler

Tszz1

Trapezoidal

Ts2z+1z1

For more information about the relative advantages of each method, see the PID Controller block reference page.

When you update a PID Controller block or custom PID controller with tuned parameter values, make sure the integrator method matches.

Tunable: Yes

Dependencies

This parameter is enabled when the Time Domain is discrete-time and the controller includes integral action.

Programmatic Use

Block Parameter: IntegratorFormula
Type: character vector
Values: 'Forward Euler' | 'Backward Euler' | 'Trapezoidal'
Default: 'Forward Euler'

Specify the discrete integration formula for the derivative filter term in your controller. In discrete time, the PID controller transfer function assumed by the block is:

C=P+Fi(z)I+DN+Fd(z),

in parallel form, or

C=P(1+Fi(z)I+DD/N+Fd(z)).

in ideal form. For a controller sample time Ts, the Filter method parameter determines the formula Fd as follows:

Filter methodFd
Forward Euler

Tsz1

Backward Euler

Tszz1

Trapezoidal

Ts2z+1z1

For more information about the relative advantages of each method, see the PID Controller block reference page.

When you update a PID Controller block or custom PID controller with tuned parameter values, make sure the filter method matches.

Tunable: Yes

Dependencies

This parameter is enabled when the Time Domain is discrete-time and the controller includes derivative action.

Programmatic Use

Block Parameter: FilterFormula
Type: character vector
Values: 'Forward Euler' | 'Backward Euler' | 'Trapezoidal'
Default: 'Forward Euler'

The target bandwidth 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 τ, a good guess for the target bandwidth is 2/τ.

To perform PID tuning, the Online PID Tuner block measures frequency-response information up to a frequency of 10 times the target bandwidth. To ensure that this frequency is less than the Nyquist frequency, the target bandwidth, ωc, must satisfy ωcTs ≤ 0.3, where Ts is the controller sample time that you specify with the Controller sample time (sec) parameter. Because of this condition, the fastest rise time you can enforce for tuning is about 1.67Ts. If this rise time does not meet your design goals, consider reducing Ts.

To provide the target bandwidth via an input port, select Use external source.

Tunable: Yes

Programmatic Use

Block Parameter: Bandwidth
Type: positive scalar
Default: 1

Specify a target minimum phase margin for the tuned open-loop response at the crossover frequency. The target phase margin reflects 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.

To provide the target phase margin via an input port, select Use external source.

Tunable: Yes

Programmatic Use

Block Parameter: TargetPM
Type: scalar
Values: 0–90
Default: 60

Experiment Tab

During the tuning experiment, the Online PID Tuner block injects a sinusoidal signal into the plant at four frequencies, [1/3,1,3,10]ωc , where ωc is the target bandwidth for tuning. Use Sine Amplitudes to specify the amplitude of each of these injected signals. Specify a:

  • Scalar value to inject the same amplitude at each frequency.

  • Vector of length 4 to specify a different amplitude at each of [1/3,1,3,10]ωc , respectively.

In a typical plant with typical target bandwidth, the magnitudes of the plant responses at the four frequencies do not vary widely. In such cases, you can use a scalar value to apply the same magnitude perturbation at all frequencies. However, if you know that there is a sharp decay in response over the range of frequencies, consider decreasing the amplitude of (1/3)ωc and increasing the amplitude of 10ωc. It is numerically better for the estimation experiment when all the plant responses have comparable magnitudes.

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.

In the experiment, the four sinusoidal signals are superimposed (with the step perturbation, if any). Thus, the perturbation can be at least as large as the sum of all amplitudes. Therefore, to obtain appropriate values for the amplitudes, consider:

  • Actuator limits. Make sure that the largest possible perturbation is within the range of your plant actuator. Saturating the actuator can introduce errors into the estimated frequency response.

  • How much the plant response changes in response to a given actuator input at the nominal operating point for tuning. For instance, suppose that you are tuning a PID controller used in engine-speed control. You have determined that a 1° change in throttle angle causes a change of about 200 rpm in the engine speed. Suppose further that to preserve linear performance the speed must not deviate by more than 100 rpm from the nominal operating point. In this case, choose amplitudes to ensure that the perturbation signal is no greater than 0.5 (assuming that value is within actuator limits).

To provide the sine amplitudes via an input port, select Use external source.

Tunable: Yes

Programmatic Use

Block Parameter: AmpStep
Type: scalar, vector of length 4
Default: 1

When this option is selected, the experiment includes an estimation of the plant DC gain. The block performs this estimation by injecting a step signal into the plant.

Caution

If your plant has a single integrator, clear this option. For plants with multiple integrators or unstable poles, do not use the Online PID Tuner block.

Tunable: Yes

Programmatic Use

Block Parameter: EstimateDCGain
Type: character vector
Values: 'off' | 'on'
Default: 'on'

If Estimate DC gain with step signal is selected, the block estimates the DC gain by injecting a step signal into the plant. Use this parameter to set the amplitude of the signal. The considerations for choosing a step amplitude are the same as the considerations for specifying Sine Amplitudes.

To provide the step amplitude via an input port, select Use external source.

Tunable: Yes

Dependencies

This parameter is enabled when Estimate DC gain with step signal is selected.

Programmatic Use

Block Parameter: AmpSine
Type: scalar
Default: 1

Block Tab

The Online PID Tuner contains two modules, one that performs the real-time frequency-response estimation, and one that uses the resulting estimated response to tune the PID gains. When you run a Simulink model containing the block in the external simulation mode, by default both modules are deployed. You can save memory on the target hardware by deploying the estimation module only (see PID Autotuning in External Mode). In this case, the tuning algorithm runs on the Simulink host computer instead of the target hardware. When this option is selected, the deployed algorithm uses about a third as much memory as when the option is cleared.

Additionally, the PID gain calculation demands more computational load than the frequency-response estimation. For fast controller sample times, some hardware might not finish the gain calculation within one execution cycle. Therefore, when using hardware with limited computing power, selecting this option lets you tune a PID controller with a fast sample time.

If you intend to deploy the block and use it in a standalone application without Simulink, do not select this option.

Tunable: No

Programmatic Use

Block Parameter: DeployTuningModule
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Select this parameter if you are using Simulink PLC Coder to generate code for the Online PID Tuner block. Clear the parameter for code generation with any other MathWorks® code-generation product.

Selecting this parameter affects internal block configuration only, for compatibility with Simulink PLC Coder. The parameter has no operative effect on generated code.

Specify the floating-point precision based on simulation environment or hardware requirements.

Tunable: No

Programmatic Use

Block Parameter: BlockDataType
Type: character vector
Values: 'double' | 'single'
Default: 'double'

Under some conditions, the Online PID Tuner block can write tuned gains to a standard or custom PID controller block. To indicate that the target PID controller is the block connected to the u in port of the Online PID Tuner block, select this option. To specify a block that is not connected to u in, clear this option.

To write tuned gains from the Online PID Tuner to a PID controller anywhere in the model, the target block must be either:

  • A PID Controller block.

  • A masked subsystem in which the PID coefficients are mask parameters named P, I, D, and N, or whatever subset of these parameters you need for the PID type you are using. For example, if you use a custom PI controller, then you only need mask parameters P and I.

Under some conditions, the Online PID Tuner block can write tuned gains to a standard or custom PID controller block. Use this parameter to specify the path of the target PID controller.

To write tuned gains from the Online PID Tuner to a PID controller anywhere in the model, the target block must be either:

  • A PID Controller block.

  • A masked subsystem in which the PID coefficients are mask parameters named P, I, D, and N, or whatever subset of these parameters you need for the PID type you are using.

Dependencies

This parameter is enabled when Clicking "Update PID Block" writes tuned gains to the PID block connected to "u in" port is selected.

The Online PID Tuner block does not automatically push the tuned gains to the target PID block. If your PID controller block meets the criteria described in the Specify PID block path parameter description, after tuning, click this button to transfer the tuned gains to the block.

You can update the PID block while the simulation is running, including when running in external mode. Doing so is useful for immediately validating tuned PID gains. At any time during simulation, you can change parameters, start the experiment again, and push the new tuned gains to the PID block. You can then continue to run the model and observe the behavior of your plant.

When you click this button, the Online PID Tuner block creates a structure in the MATLAB® workspace containing the experiment and tuning results. This structure, OnlinePIDTuningResult, contains the following fields:

  • P, I, D, N — Tuned PID gains. The structure contains whichever of these fields are necessary for the controller type you are tuning. For instance, if you are tuning a PI controller, the structure contains P and I, but not D and N.

  • TargetBandwidth — The value you specified in the Target bandwidth (rad/sec) parameter of the block.

  • TargetPhaseMargin — The value you specified in the Target phase margin (degrees) parameter of the block.

  • EstimatedPhaseMargin — Estimated phase margin achieved by the tuned system.

  • Controller — The tuned PID controller, returned as a pid (for parallel form) or pidstd (for ideal form) model object.

  • Plant — The estimated plant, returned as an frd model object. This frd contains the response data obtained at the four frequencies [1/3, 1, 3, 10]ωc.

  • PlantNominal — The plant input and output at the nominal operating point when the experiment begins, specified as a structure having fields u (input) and y (output).

  • PlantDCGain — The estimated DC gain of the system in absolute units, if Estimate DC gain with step signal is selected during tuning.

You can export to the MATLAB workspace while the simulation is running, including when running in external mode.

Introduced in R2017b

Was this topic helpful?