Skip to content
TUTORIAL

On-Off Controller ​

A common form of controller is one which can output two distinct values only, such as on/off, or 1/-1. Below, we demonstrate the usage of such a controller, the DiscreteBinaryController, and let it control an unstable second-order system. The controller runs with an update frequency of 5 Hz.

The DiscreteBinaryController turns ON when the measurement drops below u_s - b/2 and stays ON until the measurement exceeds u_s + b/2, at which point it turns OFF. The parameter b controls the width of the hysteresis band, and the structural parameter bool = false indicates that this is a 1/-1 controller and not a 0/1 controller. The controller additionally takes a parameter k which is the gain of the controller. Download as a Dyad projectONOFF.zipOpen in Dyad Studio

dyad
test component OnOffControlDemo
  discretebinarycontroller = DiscreteComponents.DiscreteBinaryController(k = 2, bool = false, b = 0.05) {^discretebinarycontroller}
  secondorder = BlockComponents.Continuous.SecondOrder() {^secondorder}
  reference = BlockComponents.Sources.Constant(k = 1) {^reference}
  zeroorderhold = DiscreteComponents.ZeroOrderHold() {^zeroorderhold}
  sampler = DiscreteComponents.Sampler() {^sampler}
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.2) {^periodicclock}
relations
  initial secondorder.x = 0.0
  initial secondorder.xd = 0.0
  connect(zeroorderhold.y, secondorder.u) {^id7}
  connect(secondorder.y, sampler.u) {^id8}
  connect(discretebinarycontroller.y, zeroorderhold.u) {^id9}
  connect(sampler.y, discretebinarycontroller.u_m, periodicclock.y) {^id10}
  connect(reference.y, discretebinarycontroller.u_s) {^id11}
metadata {
  "_links": {
    "discretebinarycontroller": {
      "Dyad": {
        "placement": {
          "diagram": {"iconName": "default", "x1": 360, "y1": 340, "x2": 460, "y2": 440, "rot": 0}
        },
        "tags": []
      }
    },
    "secondorder": {
      "Dyad": {
        "placement": {
          "diagram": {"iconName": "default", "x1": 720, "y1": 340, "x2": 820, "y2": 440, "rot": 0}
        },
        "tags": []
      }
    },
    "reference": {
      "Dyad": {
        "placement": {
          "diagram": {"iconName": "default", "x1": 140, "y1": 320, "x2": 240, "y2": 420, "rot": 0}
        },
        "tags": []
      }
    },
    "zeroorderhold": {
      "Dyad": {
        "placement": {
          "diagram": {"iconName": "default", "x1": 550, "y1": 340, "x2": 650, "y2": 440, "rot": 0}
        },
        "tags": []
      }
    },
    "sampler": {
      "Dyad": {
        "placement": {
          "diagram": {"iconName": "default", "x1": 650, "y1": 520, "x2": 550, "y2": 620, "rot": 0}
        },
        "tags": []
      }
    },
    "periodicclock": {
      "Dyad": {
        "placement": {
          "diagram": {"iconName": "default", "x1": 550, "y1": 670, "x2": 650, "y2": 770, "rot": 0}
        },
        "tags": []
      }
    },
    "id7": {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}},
    "id8": {
      "Dyad": {
        "edges": [{"S": 1, "M": [{"x": 860, "y": 390}, {"x": 860, "y": 570}], "E": 2}],
        "renderStyle": "standard"
      }
    },
    "id9": {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}},
    "id10": {
      "Dyad": {
        "edges": [
          {"S": 1, "M": [], "E": -1},
          {"S": -1, "M": [{"x": 290, "y": 570}, {"x": 290, "y": 409}], "E": 2},
          {"S": 3, "M": [{"x": 470, "y": 720}], "E": -1}
        ],
        "junctions": [{"x": 470, "y": 570}],
        "renderStyle": "standard"
      }
    },
    "id11": {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  }
}
end


analysis OnOffControlDemoAnalysis
  extends TransientAnalysis(stop = 40.0)
  model = OnOffControlDemo()
end
julia
using Plots
result = OnOffControlDemoAnalysis()
using DyadInterface
model = artifacts(result, :SimplifiedSystem)
plot(result; idxs = [model.secondorder.y, model.discretebinarycontroller.y],
    labels = ["Plant output" "Control signal"],
    title = "On-off control with hysteresis")

The plot shows the typical behavior of an on-off controller: the output oscillates around the reference value within the hysteresis band. The wider the hysteresis band b, the larger the oscillations but the lower the switching frequency.