main content

automatically tune pid gains based on plant frequency responses estimated from closed-凯发k8网页登录

automatically tune pid gains based on plant frequency responses estimated from closed-loop experiment in real time

  • closed-loop pid autotuner block

libraries:
simulink control design

description

the closed-loop pid autotuner block lets you tune a pid controller in real time against a physical plant for which you have an initial pid controller that yields a stable loop. the plant remains under closed-loop control of the initial pid controller during the entire autotuning process. the block can tune the pid controller to achieve a specified bandwidth and phase margin without a parametric plant model. 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 using simulink to manage the autotuning process.

if you have a plant modeled in simulink and an initial pid controller, you can perform closed-loop pid autotuning against the modeled plant. 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 closed-loop pid autotuner block:

  1. injects a test signal into the plant to collect plant input-output data and estimate frequency response in real time. the test signal is combination of sinusoidal perturbation signals added on top of the plant input.

  2. at the end of the experiment, tunes pid controller parameters based on estimated plant frequency responses near the target 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.

unlike with the block, the loop remains closed throughout the experiment. keeping the loop closed helps to maintain safe operation of the plant during the estimation experiment.

you can use the closed-loop pid autotuner block to tune pid controllers for:

  • any stable plant

  • any continuous-time plant with one or more integrators (poles at s = 0) or one or more pairs of complex poles on the imaginary axis

  • any discrete-time plant with one or more integrators (poles at z = –1) or pairs of complex poles on the unit circle |z| = 1

if you do not have an initial pid controller, you can use the block to obtain one. you can then switch to closed-loop pid autotuning for refinement or retuning.

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 closed-loop pid autotuner block, see:

for more general information about pid autotuning and a comparison of the closed-loop and open-loop approaches, see .

examples

ports

input

insert the 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 autotuning 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

  • positive to negative or zero, the experiment stops

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.

typically, you can use 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. use the initial controller to drive the plant to the operating point. if you have no initial controller (open-loop tuning only) you can use a source block connected to u 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.

  • let the experiment run long enough for the algorithm to collect sufficient data for a good estimate at all frequencies it probes. there are two ways to determine when to stop the experiment:

    • determine the experiment duration in advance. a conservative estimate for the experiment duration is 200/ωc in superposition experiment mode or 550/ωc in sinestream experiment mode, where ωc is your target bandwidth.

    • observe the signal at the % conv output, and stop the experiment when the signal stabilizes near 100%.

  • when you stop the experiment, the block computes tuned pid gains and updates the signal at the pid gains port.

you can configure any logic appropriate for your application to control the start and stop times of the experiment.

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

output

insert the 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 δu into it.

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

dependencies

to enable this port, in output signal configuration, select control perturbation.

data types: single | double

the block generates a perturbation signal at this port. typically, you inject the perturbation from this port via a sum block, as shown in the following diagram.

  • when the experiment is running (start/stop positive), the block generates perturbation signals at this port.

  • when the experiment is not running (start/stop zero or negative), the signal at this port is zero. in this state, the block has no effect on the plant.

dependencies

to enable this port, in output signal configuration, select perturbation only.

data types: single | double

when the experiment is running (start/stop positive), the block injects test signals into the plant 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. % conv 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 4-element 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. this bus signal always has four elements, even if you are not tuning a pidf controller.

if you have a pid controller associated with the 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 estimated 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, 0]. during the experiment, the block injects signals at frequencies [1/10, 1/3, 1, 3, 10]ωc, where ωc is the target bandwidth. at each sample time during the experiment, the block updates frd with a vector containing the complex frequency response at each of these frequencies, respectively. you can use the progress of the response as an alternative to % conv to examine the convergence of the estimation. when the experiment stops, the block updates frd with the final estimated frequency response used for computing the pid gains.

dependencies

to enable this port, in the experiment tab, select plant frequency responses near bandwidth.

this port outputs a vector containing the plant input (u δu) 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

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 pid autotuning:

  • 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 ifi(z) d[n1 nfd(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 i(1s) d(nss n).

    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 ifi(z) d(n1 nfd(z))].

    the transfer function of a continuous-time ideal-form pidf controller is:

    c=p[1 i(1s) d(nss n)].

    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.

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 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 ω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.

tips

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 . 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.

dependencies

to enable this parameter, set time domain to discrete-time.

programmatic use

block parameter: discretets
type: scalar
value positive scalar | –1
default: 0.1

enable this parameter to run tuning at a sample rate that is different from the sample rate of the pid controller you are tuning and the frequency response estimation experiment performed by the block. the pid gain tuning algorithm is computationally intensive, and when you want to deploy the block to hardware and tune a controller with a fast sample time, some hardware might not complete the pid gain calculation in a single time step. to reduce the hardware throughput requirements, specify a tuning sample time slower than the controller sample time using the tuning sample time (sec) parameter.

dependencies

to enable this parameter, set time domain to discrete-time.

programmatic use

block parameter: usetuningts
type: character vector
value 'off' | 'on'
default: 'off'

specify the sample time of the tuning algorithm in seconds.

if you intend to deploy the block on hardware with limited processing power and want to tune a controller with a fast sample time, specify a sample time such that the tuning algorithm runs at a slower rate than the pid controller you are tuning.

dependencies

to enable this parameter, set time domain to discrete-time and select tune at different sample time.

programmatic use

block parameter: tuningts
type: scalar
value positive scalar
default: 0.2

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, use a fast experiment sample time, such as 0.02/ωc.

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 ifi(z) d[n1 nfd(z)],

in parallel form, or in ideal form,

c=p[1 ifi(z) d(n1 nfd(z))].

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 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 ifi(z) d[n1 nfd(z)],

in parallel form, or in ideal form,

c=p[1 ifi(z) d(n1 nfd(z))].

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 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 a derivative filter term.

programmatic use

block parameter: filterformula
type: character vector
values: 'forward euler' | 'backward euler' | 'trapezoidal'
default: 'forward euler'

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 rise-time τ seconds, a good guess for the target bandwidth is 2/τ rad/sec.

to perform pid tuning, the autotuner 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 6.67ts. if this rise time does not meet your design goals, consider reducing ts.

for best results with closed-loop tuning, use a target bandwidth that is within about a factor of 10 of the bandwidth with the initial pid controller. to tune a controller for a larger change in bandwidth, tune incrementally using smaller changes.

to provide the target bandwidth via an input port, select use external source.

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

specify whether the perturbation at each frequency is applied sequentially (sinestream) or simultaneously (superposition).

  • sinestream — in this mode, the block applies perturbation at each frequency separately. for more information about sinestream signals for estimation, see .

  • superposition — in this mode, the perturbation signal includes all specified frequencies at once. for frequency response estimation at a vector of frequencies ω = [ω1, … , ωn] at amplitudes a = [a1, … , an], the perturbation signal is:

    δu=iaisin(ωit).

sinestream mode can be more accurate and can also be less intrusive, because the total size of the perturbation is never bigger than the values specified by the sine amplitudes parameter. however, due to the sequential nature of the sinestream perturbation, each frequency point you add increases the recommended experiment time (see the start/stop input port for details). thus, the estimation experiment is typically much faster in superposition mode with satisfactory results.

sinestream signals reduce the execution time compared to superposition input signals, but also take longer to estimate the frequency response. frequency response estimation using sinestream signals is useful when you have limited processing power and you want to reduce the execution time.

programmatic use

block parameter: experimentmode
type: character vector
values: 'superposition' | 'sinestream'
default: 'superposition'

specify whether the plant is stable or integrating. if the plant has one or more integrators, select integrating.

programmatic use

block parameter: planttype
type: character vector
values: 'stable' | 'integrating'
default: 'stable'

specify whether the plant is positive or negative. if a positive change in the plant input at the nominal operating point results in a positive change in the plant output, specify positive. otherwise, specify negative. for stable plants, the sign of the plant is the sign of the plant dc gain.

programmatic use

block parameter: plantsign
type: character vector
values: 'positive' | 'negative'
default: 'positive'

during the experiment, the block injects a sinusoidal signal into the plant at the frequencies [1/10, 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 5 to specify a different amplitude at each of [1/10, 1/3, 1, 3, 10]ωc

in a typical plant with typical target bandwidth, the magnitudes of the plant responses at the experiment 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 the response decays sharply over the frequency range, consider decreasing the amplitude of the lower-frequency inputs and increasing the amplitude of the higher-frequency inputs. 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

when experiment mode is superposition, the sinusoidal signals are superimposed. thus, the perturbation can be at least as large as the sum of all amplitudes. 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.

to provide the sine amplitudes via an input port, select use external source.

tunable: yes

programmatic use

block parameter: ampsine
type: scalar, vector of length 5
default: 1
block tab

the block 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 ). 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.

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.

additionally, when you enable this option, there can be a delay of several sampling periods between when the tuning experiment ends and when the new pid gains arrive at the pid gains output port. before pushing gains to the controller, first confirm the change at the pid gains output port instead of using start/stop signal as the trigger for the update.

if you intend to deploy the block and perform pid tuning without using external simulation mode, do not select this option.

caution

when you use this option, the model must be configured such that numeric block parameters are tunable in generated code, not inlined. to specify tunable parameters:

  • in the model editor: in configuration parameters, in code generation > optimization, set default parameter behavior to tunable.

  • at the command line: use set_param(mdl,'defaultparameterbehavior','tunable').

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 autotuner 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.

by default, the block takes a control signal as input and provides the control signal plus the experiment perturbation at the port u δu. you then feed this signal into the plant input, as shown in the following diagram.

this default configuration requires inserting the block between the controller and the plant. if you want to add the perturbation signal to the control signal yourself, select perturbation only. in this configuration, the block output contains the perturbation signal only, at the port δu. you inject this perturbation signal into the plant using, for example, a sum block, as in the following diagram.

in this configuration, you can optionally comment out the closed-loop pid autotuner block without disrupting the model.

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

programmatic use

block parameter: blockdatatype
type: character vector
values: 'double' | 'single'
default: 'double'

under some conditions, the autotuner 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 port of the autotuner block, select this option. to specify a pid controller that is not connected to u, clear this option.

to write tuned gains from the autotuner block to a pid controller anywhere in the model, the target block must be either:

  • a or block.

  • a masked subsystem in which the pid coefficients are mask parameters named p, i, d, and n, or whatever subset of these parameters exist in the your controller. for example, if you use a custom pi controller, then you only need mask parameters p and i.

under some conditions, the autotuner 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 autotuner block to a pid controller anywhere in the model, the target block must be either:

  • a or block.

  • a masked subsystem in which the pid coefficients are mask parameters named p, i, d, and n, or whatever subset of these parameters exist in your controller

dependencies

this parameter is enabled when clicking "update pid block" writes tuned gains to the pid block connected to "u" port is selected.

the 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 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 (for parallel form) or (for ideal form) model object.

  • plant — the estimated plant, returned as an model object. this frd contains the response data obtained at the experiment frequencies [1/10, 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).

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

extended capabilities

c/c code generation
generate c and c code using simulink® coder™.

plc code generation
generate structured text code using simulink® plc coder™.

version history

introduced in r2018a

see also

topics

    网站地图