Skip to content
LIBRARY
Examples.ACADOSMPCPreviewExample.md

Examples.ACADOSMPCPreviewExample ​

Reference preview with ACADOSMPC over a horizon of Np steps on the pendulum. The angle reference is known Np + 1 samples ahead of real time and runs through a DelayLine of Np + 1 taps; tap i of the line is the reference delayed Np + 1 - i samples and feeds r[i], the reference for the predicted state i steps ahead, so r[1] is one sample ahead of the current tick and r[Np] carries the value furthest ahead (the undelayed last tap is not used). The controller therefore starts moving before the step reaches the plant. Verified in test/test_acados_mpc.jl.

Usage ​

MPCComponents.Examples.ACADOSMPCPreviewExample()

Parameters: ​

NameDescriptionUnitsDefault value
NpPrediction horizon–10
output_trajectoriesRecord the predicted trajectories and solver residuals of the controller (for mpc_gui)–false

Variables ​

NameDescriptionUnits
thetaAngle–
omegaAngular velocity–
u1Torque–
flagacados status–

Behavior ​

Behavior of this component cannot be rendered because it includes path variables.

Source ​

dyad
"""
Reference preview with `ACADOSMPC` over a horizon of `Np` steps on the pendulum. The angle reference is
known `Np + 1` samples ahead of real time and runs through a `DelayLine` of `Np + 1` taps; tap `i` of the
line is the reference delayed `Np + 1 - i` samples and feeds `r[i]`, the reference for the predicted
state `i` steps ahead, so `r[1]` is one sample ahead of the current tick and `r[Np]` carries the value
furthest ahead (the undelayed last tap is not used). The controller therefore starts moving before the
step reaches the plant. Verified in `test/test_acados_mpc.jl`.
"""
example component ACADOSMPCPreviewExample
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.05) {
    "Dyad": {"placement": {"diagram": {"x1": 300, "y1": 100, "x2": 400, "y2": 200}}}
  }
  plant = MPCComponents.Examples.ACADOSPendulumPlant() {
    "Dyad": {"placement": {"diagram": {"x1": 700, "y1": 300, "x2": 800, "y2": 400}}}
  }
  s1 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 500, "x2": 600, "y2": 600, "rot": 180}}
    }
  }
  s2 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 650, "x2": 600, "y2": 750, "rot": 180}}
    }
  }
  sr = DiscreteComponents.Sampler() {
    "Dyad": {"placement": {"diagram": {"x1": -50, "y1": 250, "x2": 50, "y2": 350}}}
  }
  zoh = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {"placement": {"diagram": {"x1": 500, "y1": 300, "x2": 600, "y2": 400}}}
  }
  "Angle reference, known Np + 1 samples ahead of real time"
  ref = BlockComponents.Sources.Step(height = 0.5, final start_time = 2.0 - 0.05 * (Np + 1)) {
    "Dyad": {"placement": {"diagram": {"x1": -250, "y1": 250, "x2": -150, "y2": 350}}}
  }
  "Delay line producing the reference preview taps"
  dline = MPCComponents.DelayLine(final N = Np + 1) {
    "Dyad": {"placement": {"diagram": {"x1": 100, "y1": 250, "x2": 200, "y2": 350}}}
  }
  mpc = MPCComponents.ACADOSMPC(dynamics = plant, inputs = ["tau"], state_variables = ["th", "om"], outputs = ["theta"], Ts = 0.05, final Np = Np, Q1 = diagonal([10.0]), Q2 = diagonal([0.01]), umin = [-8.0], umax = [8.0], reference_preview = true, final output_trajectories = output_trajectories) {
    "Dyad": {"placement": {"diagram": {"x1": 300, "y1": 300, "x2": 400, "y2": 400}}}
  }
  "Prediction horizon"
  structural parameter Np::Integer = 10
  "Record the predicted trajectories and solver residuals of the controller (for mpc_gui)"
  structural parameter output_trajectories::Boolean = false
  "Angle"
  variable theta::Real
  "Angular velocity"
  variable omega::Real
  "Torque"
  variable u1::Real
  "acados status"
  variable flag::Real
relations
  theta = plant.theta
  omega = plant.omega
  u1 = mpc.u[1]
  flag = mpc.exitflag
  connect(zoh.y, plant.tau)
  connect(plant.theta, s1.u)
  connect(plant.omega, s2.u)
  connect(ref.y, sr.u)
  connect(sr.y, dline.u)
  connect(s1.y, mpc.x[1], periodicclock.y)
  connect(s2.y, mpc.x[2])
  for i in 1:Np
    connect(dline.y[i], mpc.r[i])
  end
  connect(mpc.u[1], zoh.u)
metadata {"Dyad": {"doc": {"behavior": false}}}
end
Flattened Source
dyad
"""
Reference preview with `ACADOSMPC` over a horizon of `Np` steps on the pendulum. The angle reference is
known `Np + 1` samples ahead of real time and runs through a `DelayLine` of `Np + 1` taps; tap `i` of the
line is the reference delayed `Np + 1 - i` samples and feeds `r[i]`, the reference for the predicted
state `i` steps ahead, so `r[1]` is one sample ahead of the current tick and `r[Np]` carries the value
furthest ahead (the undelayed last tap is not used). The controller therefore starts moving before the
step reaches the plant. Verified in `test/test_acados_mpc.jl`.
"""
example component ACADOSMPCPreviewExample
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.05) {
    "Dyad": {"placement": {"diagram": {"x1": 300, "y1": 100, "x2": 400, "y2": 200}}}
  }
  plant = MPCComponents.Examples.ACADOSPendulumPlant() {
    "Dyad": {"placement": {"diagram": {"x1": 700, "y1": 300, "x2": 800, "y2": 400}}}
  }
  s1 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 500, "x2": 600, "y2": 600, "rot": 180}}
    }
  }
  s2 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 650, "x2": 600, "y2": 750, "rot": 180}}
    }
  }
  sr = DiscreteComponents.Sampler() {
    "Dyad": {"placement": {"diagram": {"x1": -50, "y1": 250, "x2": 50, "y2": 350}}}
  }
  zoh = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {"placement": {"diagram": {"x1": 500, "y1": 300, "x2": 600, "y2": 400}}}
  }
  "Angle reference, known Np + 1 samples ahead of real time"
  ref = BlockComponents.Sources.Step(height = 0.5, final start_time = 2.0 - 0.05 * (Np + 1)) {
    "Dyad": {"placement": {"diagram": {"x1": -250, "y1": 250, "x2": -150, "y2": 350}}}
  }
  "Delay line producing the reference preview taps"
  dline = MPCComponents.DelayLine(final N = Np + 1) {
    "Dyad": {"placement": {"diagram": {"x1": 100, "y1": 250, "x2": 200, "y2": 350}}}
  }
  mpc = MPCComponents.ACADOSMPC(dynamics = plant, inputs = ["tau"], state_variables = ["th", "om"], outputs = ["theta"], Ts = 0.05, final Np = Np, Q1 = diagonal([10.0]), Q2 = diagonal([0.01]), umin = [-8.0], umax = [8.0], reference_preview = true, final output_trajectories = output_trajectories) {
    "Dyad": {"placement": {"diagram": {"x1": 300, "y1": 300, "x2": 400, "y2": 400}}}
  }
  "Prediction horizon"
  structural parameter Np::Integer = 10
  "Record the predicted trajectories and solver residuals of the controller (for mpc_gui)"
  structural parameter output_trajectories::Boolean = false
  "Angle"
  variable theta::Real
  "Angular velocity"
  variable omega::Real
  "Torque"
  variable u1::Real
  "acados status"
  variable flag::Real
relations
  theta = plant.theta
  omega = plant.omega
  u1 = mpc.u[1]
  flag = mpc.exitflag
  connect(zoh.y, plant.tau)
  connect(plant.theta, s1.u)
  connect(plant.omega, s2.u)
  connect(ref.y, sr.u)
  connect(sr.y, dline.u)
  connect(s1.y, mpc.x[1], periodicclock.y)
  connect(s2.y, mpc.x[2])
  for i in 1:Np
    connect(dline.y[i], mpc.r[i])
  end
  connect(mpc.u[1], zoh.u)
metadata {"Dyad": {"doc": {"behavior": false}}}
end


Test Cases ​

No test cases defined.

  • Examples

  • Experiments

  • Analyses