Skip to content
LIBRARY
Examples.LinearMPCTrackingExample.md

Examples.LinearMPCTrackingExample ​

Reference tracking of the position of a double integrator with LinearMPCController: only the position is measured and a Kalman filter estimates the velocity. Exercises a step reference, soft output bounds, a hard general constraint on the velocity, move blocking, a prestabilizing feedback, a terminal weight and the observer covariances.

Usage ​

MPCComponents.Examples.LinearMPCTrackingExample()

Parameters: ​

NameDescriptionUnitsDefault value
output_trajectoriesRecord the predicted trajectories and the QP statistics of the controller, for mpc_gui–false

Variables ​

NameDescriptionUnits
y1Position–
u1Control signal–
v1Estimated velocity–

Behavior ​

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

Source ​

dyad
"""
Reference tracking of the position of a double integrator with `LinearMPCController`: only the
position is measured and a Kalman filter estimates the velocity. Exercises a step reference,
soft output bounds, a hard general constraint on the velocity, move blocking, a prestabilizing
feedback, a terminal weight and the observer covariances.
"""
example component LinearMPCTrackingExample
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.1) {
    "Dyad": {"placement": {"diagram": {"x1": 425, "x2": 525, "y1": 191, "y2": 291}}}
  }
  vel = BlockComponents.Continuous.Integrator(x0 = 0.0) {
    "Dyad": {"placement": {"diagram": {"x1": 560, "x2": 660, "y1": 71, "y2": 171}}}
  }
  pos = BlockComponents.Continuous.Integrator(x0 = 0.0) {
    "Dyad": {"placement": {"diagram": {"x1": 700, "x2": 800, "y1": 71, "y2": 171}}}
  }
  s1 = DiscreteComponents.Sampler() {
    "Dyad": {"placement": {"diagram": {"x1": 834, "x2": 934, "y1": 71, "y2": 171}}}
  }
  sr = DiscreteComponents.Sampler() {
    "Dyad": {"placement": {"diagram": {"x1": 154, "x2": 254, "y1": 56, "y2": 156}}}
  }
  zoh = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {"placement": {"diagram": {"x1": 424, "x2": 524, "y1": 71, "y2": 171}}}
  }
  ref = BlockComponents.Sources.Step(start_time = 1.0, height = 1.0) {"Dyad": {"placement": {"diagram": {"x1": 20, "x2": 120, "y1": 56, "y2": 156}}}}
  mpc = MPCComponents.LinearMPCController(A = [[1.0, 0.1], [0.0, 1.0]], B = [[0.005], [0.1]], C = [[1.0, 0.0]], Q1 = [1.0], Q2 = [0.1], Qf = [2.0], umin = [-1.0], umax = [1.0], ymax = [1.05], Ax = [[0.0, 1.0]], lb = [-0.4], ub = [0.4], move_block = [1, 1, 2, 3, 3], prestabilizing_feedback = true, Np = 10, R1 = [0.01, 0.01], R2 = [0.01], output_trajectories = output_trajectories) {
    "Dyad": {"placement": {"diagram": {"x1": 290, "x2": 390, "y1": 91, "y2": 191}}}
  }
  "Record the predicted trajectories and the QP statistics of the controller, for mpc_gui"
  structural parameter output_trajectories::Boolean = false
  "Position"
  variable y1::Real
  "Control signal"
  variable u1::Real
  "Estimated velocity"
  variable v1::Real
relations
  y1 = pos.y
  u1 = mpc.u[1]
  v1 = mpc.xhat[2]
  connect(zoh.y, vel.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(vel.y, pos.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(pos.y, s1.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(s1.y, mpc.y[1], periodicclock.y) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 950, "y": 121}, {"x": 950, "y": 400}, {"x": 270, "y": 400}],
          "E": -1
        },
        {"S": -1, "M": [{"x": 270, "y": 141}], "E": 2},
        {"S": -1, "M": [{"x": 270, "y": 241}], "E": 3}
      ],
      "junctions": [{"x": 270, "y": 240}],
      "path": {}
    }
  }
  connect(ref.y, sr.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(sr.y, mpc.r[1]) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(mpc.u[1], zoh.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
metadata {"Dyad": {"tests": {"case1": {"stop": 8}}, "doc": {"behavior": false}}}
end
Flattened Source
dyad
"""
Reference tracking of the position of a double integrator with `LinearMPCController`: only the
position is measured and a Kalman filter estimates the velocity. Exercises a step reference,
soft output bounds, a hard general constraint on the velocity, move blocking, a prestabilizing
feedback, a terminal weight and the observer covariances.
"""
example component LinearMPCTrackingExample
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.1) {
    "Dyad": {"placement": {"diagram": {"x1": 425, "x2": 525, "y1": 191, "y2": 291}}}
  }
  vel = BlockComponents.Continuous.Integrator(x0 = 0.0) {
    "Dyad": {"placement": {"diagram": {"x1": 560, "x2": 660, "y1": 71, "y2": 171}}}
  }
  pos = BlockComponents.Continuous.Integrator(x0 = 0.0) {
    "Dyad": {"placement": {"diagram": {"x1": 700, "x2": 800, "y1": 71, "y2": 171}}}
  }
  s1 = DiscreteComponents.Sampler() {
    "Dyad": {"placement": {"diagram": {"x1": 834, "x2": 934, "y1": 71, "y2": 171}}}
  }
  sr = DiscreteComponents.Sampler() {
    "Dyad": {"placement": {"diagram": {"x1": 154, "x2": 254, "y1": 56, "y2": 156}}}
  }
  zoh = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {"placement": {"diagram": {"x1": 424, "x2": 524, "y1": 71, "y2": 171}}}
  }
  ref = BlockComponents.Sources.Step(start_time = 1.0, height = 1.0) {"Dyad": {"placement": {"diagram": {"x1": 20, "x2": 120, "y1": 56, "y2": 156}}}}
  mpc = MPCComponents.LinearMPCController(A = [[1.0, 0.1], [0.0, 1.0]], B = [[0.005], [0.1]], C = [[1.0, 0.0]], Q1 = [1.0], Q2 = [0.1], Qf = [2.0], umin = [-1.0], umax = [1.0], ymax = [1.05], Ax = [[0.0, 1.0]], lb = [-0.4], ub = [0.4], move_block = [1, 1, 2, 3, 3], prestabilizing_feedback = true, Np = 10, R1 = [0.01, 0.01], R2 = [0.01], output_trajectories = output_trajectories) {
    "Dyad": {"placement": {"diagram": {"x1": 290, "x2": 390, "y1": 91, "y2": 191}}}
  }
  "Record the predicted trajectories and the QP statistics of the controller, for mpc_gui"
  structural parameter output_trajectories::Boolean = false
  "Position"
  variable y1::Real
  "Control signal"
  variable u1::Real
  "Estimated velocity"
  variable v1::Real
relations
  y1 = pos.y
  u1 = mpc.u[1]
  v1 = mpc.xhat[2]
  connect(zoh.y, vel.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(vel.y, pos.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(pos.y, s1.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(s1.y, mpc.y[1], periodicclock.y) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 950, "y": 121}, {"x": 950, "y": 400}, {"x": 270, "y": 400}],
          "E": -1
        },
        {"S": -1, "M": [{"x": 270, "y": 141}], "E": 2},
        {"S": -1, "M": [{"x": 270, "y": 241}], "E": 3}
      ],
      "junctions": [{"x": 270, "y": 240}],
      "path": {}
    }
  }
  connect(ref.y, sr.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(sr.y, mpc.r[1]) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
  connect(mpc.u[1], zoh.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {}}}
metadata {"Dyad": {"tests": {"case1": {"stop": 8}}, "doc": {"behavior": false}}}
end


Test Cases ​

Test Case case1 ​

  • Examples

  • Experiments

  • Analyses