Skip to content

PID Autotuning Analysis

The PID autotuning analysis is used to automatically tune the parameters of a PID controller for a given model. The analysis computes optimal PID gains that satisfy frequency-domain robustness constraints, such as maximum sensitivity (Ms), complementary sensitivity (Mt), and noise sensitivity (Mks).

Method Overview

PID autotuning in DyadControlSystems is based on step-response optimization, subject to robustness constraints on closed-loop sensitivity functions. The method performs joint optimization of a PID controller and a measurement filter of the form

K(s)=C(s)F(s)=(kp+ki/s+kds)1(sTf)2/(4ζ2)+Tfs+1,ζ=1

by solving

minimizeC,Fm(e(t))dt

subject to

||S(s)||MS||T(s)||MT||KS(s)||MKS

where e(t)=r(t)y(t) is the control error and m is a user-defined metric (defaults to abs2). The user can select which signals to optimize, the reference value, and the filter order.

The transfer functions in the frequency-domain constraints are defined as follows:

  • Sensitivity: S=1/(1+PK)

  • Complementary sensitivity: T=PK/(1+PK)

  • Noise sensitivity: K/(1+PK)

See control-system sensitivity and DyadControlSystems autotuning documentation for more information.

Example Definition

The following example will demonstrate autotuning of a PID controller for an active suspension system. The model of the system is available in DyadExampleComponents.

dyad
using DyadExampleComponents: ActiveSuspension

analysis ActiveSuspensionPIDAutotuningAnalysis
  extends DyadControlSystems.PIDAutotuningAnalysis(
    measurement   = "y",
    control_input = "u",
    step_input    = "u",
    step_output   = "y",
    duration = 10.0,
    Ts       = 0.01,
    Ms       = 1.2,
    Mt       = 1.2,
    Mks      = 3e4,
    ki_lb    = 2000,
    wl       = 1e-2,
    wu       = 1e3
  )

  model = DyadExampleComponents.ActiveSuspension()

end
julia
using DyadExampleComponents, DyadInterface
ActiveSuspensionPIDAutotuningAnalysis() # hide
solution = ActiveSuspensionPIDAutotuningAnalysis()
┌ Warning: Initialization system is overdetermined. 1 equations for 0 unknowns. Initialization will default to using least squares. `SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Z9mEq/src/systems/diffeqs/abstractodesystem.jl:1522
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.8.0.

Number of nonzeros in equality constraint Jacobian...:        0
Number of nonzeros in inequality constraint Jacobian.:     2404
Number of nonzeros in Lagrangian Hessian.............:        0

Total number of variables............................:        4
                     variables with only lower bounds:        4
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        0
Total number of inequality constraints...............:      601
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:      601

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.5712512e-02 2.75e+01 6.29e-01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   5  1.4948499e-02 2.59e+01 1.40e+03  -1.1 1.13e+05    -  1.66e-03 2.14e-05h  1
  10  8.0867914e-03 7.67e+00 6.70e+04   0.7 1.11e+04    -  6.06e-02 1.60e-04h  1
  15  3.1943462e-03 6.16e-01 1.16e+06   1.6 7.44e+03    -  4.40e-02 3.95e-01f  1
  20  2.8874972e-03 3.61e-01 5.82e+05  -0.1 2.31e+03    -  6.28e-02 1.51e-01f  1
  25  2.6192246e-03 2.43e-01 3.09e+05  -0.7 2.20e+03    -  1.54e-01 2.46e-01f  1
  30  1.0175217e-02 0.00e+00 3.66e+06   2.5 1.04e+04    -  1.68e-03 2.95e-01f  1
  35  8.1953888e-03 0.00e+00 4.11e-01  -2.6 5.88e+00    -  9.88e-01 1.00e+00h  1
  40  3.0075684e-03 0.00e+00 4.48e-02  -4.3 2.01e+03    -  1.00e+00 1.00e+00h  1
  45  1.1903039e-03 0.00e+00 3.08e-03  -5.4 6.53e+02    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  50  9.7690415e-04 0.00e+00 1.63e-05  -8.3 9.02e+00    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 51

                                   (scaled)                 (unscaled)
Objective...............:   9.7688560787864459e-04    9.7688560787864459e-04
Dual infeasibility......:   3.2453130769138745e-07    3.2453130769138745e-07
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.0961374754484757e-10    1.0961374754484757e-10
Overall NLP error.......:   3.2453130769138745e-07    3.2453130769138745e-07


Number of objective function evaluations             = 52
Number of objective gradient evaluations             = 52
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 53
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 52
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 27.021

EXIT: Optimal Solution Found.
┌ Warning: Initialization system is overdetermined. 1 equations for 0 unknowns. Initialization will default to using least squares. `SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Z9mEq/src/systems/diffeqs/abstractodesystem.jl:1522
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.8.0.

Number of nonzeros in equality constraint Jacobian...:        0
Number of nonzeros in inequality constraint Jacobian.:     2404
Number of nonzeros in Lagrangian Hessian.............:        0

Total number of variables............................:        4
                     variables with only lower bounds:        4
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        0
Total number of inequality constraints...............:      601
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:      601

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.5712512e-02 2.75e+01 6.29e-01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   5  1.4948499e-02 2.59e+01 1.40e+03  -1.1 1.13e+05    -  1.66e-03 2.14e-05h  1
  10  8.0867914e-03 7.67e+00 6.70e+04   0.7 1.11e+04    -  6.06e-02 1.60e-04h  1
  15  3.1943462e-03 6.16e-01 1.16e+06   1.6 7.44e+03    -  4.40e-02 3.95e-01f  1
  20  2.8874972e-03 3.61e-01 5.82e+05  -0.1 2.31e+03    -  6.28e-02 1.51e-01f  1
  25  2.6192246e-03 2.43e-01 3.09e+05  -0.7 2.20e+03    -  1.54e-01 2.46e-01f  1
  30  1.0175217e-02 0.00e+00 3.66e+06   2.5 1.04e+04    -  1.68e-03 2.95e-01f  1
  35  8.1953888e-03 0.00e+00 4.11e-01  -2.6 5.88e+00    -  9.88e-01 1.00e+00h  1
  40  3.0075684e-03 0.00e+00 4.48e-02  -4.3 2.01e+03    -  1.00e+00 1.00e+00h  1
  45  1.1903039e-03 0.00e+00 3.08e-03  -5.4 6.53e+02    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  50  9.7690415e-04 0.00e+00 1.63e-05  -8.3 9.02e+00    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 51

                                   (scaled)                 (unscaled)
Objective...............:   9.7688560787864459e-04    9.7688560787864459e-04
Dual infeasibility......:   3.2453130769138745e-07    3.2453130769138745e-07
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.0961374754484757e-10    1.0961374754484757e-10
Overall NLP error.......:   3.2453130769138745e-07    3.2453130769138745e-07


Number of objective function evaluations             = 52
Number of objective gradient evaluations             = 52
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 53
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 52
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 0.487

EXIT: Optimal Solution Found.
julia
using Plots
figs  = artifacts(solution, :SensitivityFunctions)
fign  = artifacts(solution, :NoiseSensitivityAndController)
figr  = artifacts(solution, :OptimizedResponse)
figny = artifacts(solution, :NyquistPlot)

plot!(figs, ylims=(1e-2, Inf))
plot!(figny, legend=:bottomleft)

plot(figs, fign, figr, figny,
    link = :none,
    size = (600, 600),
)

We may also ask for the optimized controller parameters:

julia
artifacts(solution, :OptimizedParameters)
1×7 DataFrame
Rowkp_parallelki_parallelkd_parallelKp_standardTi_standardTd_standardTf
Float64Float64Float64Float64Float64Float64Float64
11239.15461.29702.6051239.10.2268880.5670290.0259857

Analysis Arguments

The following arguments define a PID autotuning analysis:

Required Arguments

  • model: The system model to be controlled.

  • measurement::String: Name of the measured output signal.

  • control_input::String: Name of the control input signal.

  • step_input::String: Name of the step input signal.

  • step_output::String: Name of the step output signal.

  • Ts::Real: Sampling time to use in the discretization.

  • duration::Real: Simulation duration.

  • Ms::Real: Maximum sensitivity constraint. Values between 1 and 2 are typical. A lower value increases robustness but may reduce performance.

  • Mt::Real: Maximum complementary sensitivity constraint. Values between 1 and 2 are typical. A lower value increases robustness but may reduce performance.

  • Mks::Real: Maximum noise sensitivity constraint. This controls the maximum amplification of measurement noise in the control signal.

Optional Arguments

  • ref::Real = 0.0: Reference value for the step input.

  • disc::String = "tustin": Discretization method.

  • kp_lb, ki_lb, kd_lb, Tf_lb: Lower bounds for PID parameters.

  • kp_ub, ki_ub, kd_ub, Tf_ub: Upper bounds for PID parameters.

  • kp_guess, ki_guess, kd_guess, Tf_guess: Initial guesses for PID parameters.

  • timeweight::Bool = false: Whether or not to use time-weighted optimization.

  • filter_order::Int = 2: Order of the derivative filter (1 or 2).

  • optimize_d::Bool = false: Whether or not to optimize the damping parameter of the second-order filter.

  • wl::Real: Lower frequency bound for design.

  • wu::Real: Upper frequency bound for design.

  • num_frequencies::Int: Number of frequency points.

Controller Structure

The optimized controller has the form:

K(s)=C(s)F(s)=(kp+ki/s+kds)F(s)

where F(s) is a low-pass filter, either first- or second-order depending on filter_order.

Constraints

  • Sensitivity (Ms): Limits the peak of the sensitivity function S(s). Typical values are between 1 and 2.

  • Complementary Sensitivity (Mt): Limits the peak of T(s). Typical values are between 1 and 2.

  • Noise Sensitivity (Mks): Limits the peak of K(s)S(s), which determines the amplification of measurement noise in the control signal.

Lower values increase robustness but may reduce performance.

The peak MS in the sensitivity function S(s) is associated with lower bounds on the classical gain and phase margins, gm,ϕm:

gmMSMS1ϕm2sin1(12MS)

For example, a maximum sensitivity of MS=1.3 corresponds to a gain margin of at least 4.3 and a phase margin of at least 45 degrees. The peak constraints MS and MT corresponds to circles which the Nyquist curve must fall outside, these circles are drawn with dashed lines in the Nyquist plot artifact.

Advanced Options

  • Reference vs. Disturbance Step: By default, the disturbance step response is optimized. To optimize reference tracking, set step_input = :reference_input.

  • Controller Type: By setting parameter bounds, you can restrict the controller to P, PI, PD, or PID.

  • Filter Damping: With filter_order = 2 and optimize_d = true, the damping ratio of the filter is also optimized.

  • Penalizing Control Action: You can augment the model to penalize control effort or noise amplification.

Artifacts

A PIDAutotuningAnalysis returns the following artifacts:

Standard Plots

  • :SensitivityFunctions: Bode plot of sensitivity (S) and complementary sensitivity (T) functions.

  • :NoiseSensitivityAndController: Bode plot of noise sensitivity (KS) and controller (K).

  • :OptimizedResponse: Step response of the optimized closed-loop system.

  • :NyquistPlot: Nyquist plot of the loop transfer function.

Tables

  • OptimizedParameters: DataFrame of the optimized PID parameters. The parameters are available for the parallel controller structure as well as converted to the standard form Kp(1+1/(Tss)+Tds)

Further Reading