Skip to content
LIBRARY
Examples.ACADOSMPCPendulumExample.md

Examples.ACADOSMPCPendulumExample ​

Regulation of the pendulum to its hanging position with ACADOSMPC from the initial angle π/4 with the torque bounded to ±5. Gravity accelerates the pendulum toward the hanging position, and the controller applies a braking torque that peaks at about 4.4, just below the bound. Both states are sampled and fed to x, the control is held. Verified in test/test_acados_mpc.jl.

Usage ​

MPCComponents.Examples.ACADOSMPCPendulumExample()

Parameters: ​

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

Variables ​

NameDescriptionUnits
thetaAngle–
u1Torque–
flagacados status–

Behavior ​

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

Source ​

dyad
"""
Regulation of the pendulum to its hanging position with `ACADOSMPC` from the initial angle π/4 with
the torque bounded to ±5. Gravity accelerates the pendulum toward the hanging position, and the
controller applies a braking torque that peaks at about 4.4, just below the bound. Both states are
sampled and fed to `x`, the control is held. Verified in `test/test_acados_mpc.jl`.
"""
example component ACADOSMPCPendulumExample
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.05) {
    "Dyad": {"placement": {"diagram": {"x1": 150, "y1": 200, "x2": 50, "y2": 300}}}
  }
  plant = MPCComponents.Examples.ACADOSPendulumPlant(theta0 = 0.7853981633974483) {
    "Dyad": {"placement": {"diagram": {"x1": 700, "y1": 300, "x2": 800, "y2": 400}}}
  }
  s1 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 570, "x2": 600, "y2": 670, "rot": 180}}
    }
  }
  s2 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 450, "x2": 600, "y2": 550, "rot": 180}}
    }
  }
  zoh = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {"placement": {"diagram": {"x1": 500, "y1": 300, "x2": 600, "y2": 400}}}
  }
  mpc = MPCComponents.ACADOSMPC(dynamics = plant, inputs = ["tau"], state_variables = ["th", "om"], outputs = ["theta", "omega"], Ts = 0.05, Np = 20, Q1 = diagonal([10.0, 1.0]), Q2 = diagonal([0.1]), umin = [-5.0], umax = [5.0], reference_tracking = false, final output_trajectories = output_trajectories) {
    "Dyad": {"placement": {"diagram": {"x1": 300, "y1": 300, "x2": 400, "y2": 400}}}
  }
  "Record the predicted trajectories and solver residuals of the controller (for mpc_gui)"
  structural parameter output_trajectories::Boolean = false
  "Angle"
  variable theta::Real
  "Torque"
  variable u1::Real
  "acados status"
  variable flag::Real
relations
  theta = plant.theta
  u1 = mpc.u[1]
  flag = mpc.exitflag
  connect(zoh.y, plant.tau)
  connect(plant.theta, s1.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 880, "y": 335}, {"x": 880, "y": 620}], "E": 2}],
      "path": {}
    }
  }
  connect(plant.omega, s2.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 830, "y": 365}, {"x": 830, "y": 500}], "E": 2}],
      "path": {}
    }
  }
  connect(s1.y, mpc.x[1], periodicclock.y) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 260, "y": 620}, {"x": 260, "y": 350}], "E": 2},
        {"S": 2, "M": [{"x": 260, "y": 350}, {"x": 260, "y": 250}], "E": 3}
      ],
      "path": {}
    }
  }
  connect(s2.y, mpc.x[2]) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 280, "y": 500}, {"x": 280, "y": 350}], "E": 2}],
      "path": {}
    }
  }
  connect(mpc.u[1], zoh.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 450, "y": 373}, {"x": 450, "y": 350}], "E": 2}],
      "path": {}
    }
  }
metadata {"Dyad": {"doc": {"behavior": false}}}
end
Flattened Source
dyad
"""
Regulation of the pendulum to its hanging position with `ACADOSMPC` from the initial angle π/4 with
the torque bounded to ±5. Gravity accelerates the pendulum toward the hanging position, and the
controller applies a braking torque that peaks at about 4.4, just below the bound. Both states are
sampled and fed to `x`, the control is held. Verified in `test/test_acados_mpc.jl`.
"""
example component ACADOSMPCPendulumExample
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.05) {
    "Dyad": {"placement": {"diagram": {"x1": 150, "y1": 200, "x2": 50, "y2": 300}}}
  }
  plant = MPCComponents.Examples.ACADOSPendulumPlant(theta0 = 0.7853981633974483) {
    "Dyad": {"placement": {"diagram": {"x1": 700, "y1": 300, "x2": 800, "y2": 400}}}
  }
  s1 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 570, "x2": 600, "y2": 670, "rot": 180}}
    }
  }
  s2 = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {"diagram": {"x1": 500, "y1": 450, "x2": 600, "y2": 550, "rot": 180}}
    }
  }
  zoh = DiscreteComponents.ZeroOrderHold() {
    "Dyad": {"placement": {"diagram": {"x1": 500, "y1": 300, "x2": 600, "y2": 400}}}
  }
  mpc = MPCComponents.ACADOSMPC(dynamics = plant, inputs = ["tau"], state_variables = ["th", "om"], outputs = ["theta", "omega"], Ts = 0.05, Np = 20, Q1 = diagonal([10.0, 1.0]), Q2 = diagonal([0.1]), umin = [-5.0], umax = [5.0], reference_tracking = false, final output_trajectories = output_trajectories) {
    "Dyad": {"placement": {"diagram": {"x1": 300, "y1": 300, "x2": 400, "y2": 400}}}
  }
  "Record the predicted trajectories and solver residuals of the controller (for mpc_gui)"
  structural parameter output_trajectories::Boolean = false
  "Angle"
  variable theta::Real
  "Torque"
  variable u1::Real
  "acados status"
  variable flag::Real
relations
  theta = plant.theta
  u1 = mpc.u[1]
  flag = mpc.exitflag
  connect(zoh.y, plant.tau)
  connect(plant.theta, s1.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 880, "y": 335}, {"x": 880, "y": 620}], "E": 2}],
      "path": {}
    }
  }
  connect(plant.omega, s2.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 830, "y": 365}, {"x": 830, "y": 500}], "E": 2}],
      "path": {}
    }
  }
  connect(s1.y, mpc.x[1], periodicclock.y) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 260, "y": 620}, {"x": 260, "y": 350}], "E": 2},
        {"S": 2, "M": [{"x": 260, "y": 350}, {"x": 260, "y": 250}], "E": 3}
      ],
      "path": {}
    }
  }
  connect(s2.y, mpc.x[2]) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 280, "y": 500}, {"x": 280, "y": 350}], "E": 2}],
      "path": {}
    }
  }
  connect(mpc.u[1], zoh.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 450, "y": 373}, {"x": 450, "y": 350}], "E": 2}],
      "path": {}
    }
  }
metadata {"Dyad": {"doc": {"behavior": false}}}
end


Test Cases ​

No test cases defined.

  • Examples

  • Experiments

  • Analyses