---
url: /dyad/stable/stdlib/DiscreteComponents/components/DiscreteBinaryController.md
description: >-
  Dyad DiscreteBinaryController docs.  Discrete-time binary (on-off) controller
  with hysteresis. The controller switches between two output levels based on
  comparing the measureme...
---

# `DiscreteBinaryController` {#DiscreteBinaryController}

Discrete-time binary (on-off) controller with hysteresis. The controller switches between two output levels based on comparing the measurement input `u_m` against the `u_s` signal, with a hysteresis bandwidth `b` to prevent chattering.

The controller 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 and stays OFF until the measurement drops below `u_s - b/2` again.

# Connectors: {#Connectors:}

* `u_m`: The measurement input

* `u_s`: The reference signal

* `y`: The control signal output

# Parameters: {#Parameters:}

* `b`: Bandwidth around reference signal within which the controller does not react

* `k`: Controller gain. The output of the controller is scaled by this gain, i.e., `k = 2, bool = false` will result in an output of -2 or 2.

* `bool`: (structural) If true (default), the controller switches between 0 and k. If false, the controller switches between -k and k.

## Usage {#Usage}

`DiscreteComponents.DiscreteBinaryController(b=0.1, k=1)`

## Parameters: {#Parameters:-2}

|   Name |                                                                                           Description | Units | Default value |
| ------:| -----------------------------------------------------------------------------------------------------:| -----:| -------------:|
| `bool` | If true, the controller switches between 0 and k. If false, the controller switches between -k and k. |     – |          true |
|    `b` |                          Bandwidth around reference signal within which the controller does not react |     – |           0.1 |
|    `k` |                                                                                       Controller gain |     – |             1 |

## Connectors {#Connectors}

* `u_m` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

* `u_s` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

* `y` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

## Variables {#Variables}

| Name |    Description | Units |
| ----:| --------------:| -----:|
|  `s` | Internal state |     – |

## Behavior {#Behavior}

$$ \begin{align}
s\left( t \right) &= ifelse\left( Shift(t, -1)\left( s\left( t \right) \right), \mathtt{u_m}\left( t \right) < \frac{b}{2} + \mathtt{u_s}\left( t \right), \mathtt{u_m}\left( t \right) <  - \frac{1}{2} ~ b + \mathtt{u_s}\left( t \right) \right) \\
y\left( t \right) &= ifelse\left( s\left( t \right), k, 0 \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Discrete-time binary (on-off) controller with hysteresis. The controller switches between two output levels based on comparing the measurement input `u_m` against the `u_s` signal, with a hysteresis bandwidth `b` to prevent chattering.

The controller 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 and stays OFF until the measurement drops below `u_s - b/2` again.

# Connectors:
- `u_m`: The measurement input
- `u_s`: The reference signal
- `y`: The control signal output

# Parameters:
- `b`: Bandwidth around reference signal within which the controller does not react
- `k`: Controller gain. The output of the controller is scaled by this gain, i.e., `k = 2, bool = false` will result in an output of -2 or 2.
- `bool`: (structural) If true (default), the controller switches between 0 and k. If false, the controller switches between -k and k.
"""
component DiscreteBinaryController@[input clk extends Discrete]
  "The measurement input"
  u_m = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 640, "x2": 0, "y2": 740, "rot": 0}
      },
      "tags": []
    }
  }
  "The reference signal"
  u_s = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 200, "x2": 0, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
  "The control signal output"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "If true, the controller switches between 0 and k. If false, the controller switches between -k and k."
  structural parameter bool::Boolean = true
  "Bandwidth around reference signal within which the controller does not react"
  parameter b::Real = 0.1
  "Controller gain"
  parameter k::Real = 1
  "Internal state"
  variable s::Boolean
relations
  initial s@(clk-1) = 0
  s@clk = ifelse(s@(clk-1), u_m < u_s + b / 2, u_m < u_s - b / 2)
  if bool
    y = ifelse(s, k, 0)
  else
    y = ifelse(s, k, -k)
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/BinaryController.svg"}}
}
end
```

```dyad
"""
Discrete-time binary (on-off) controller with hysteresis. The controller switches between two output levels based on comparing the measurement input `u_m` against the `u_s` signal, with a hysteresis bandwidth `b` to prevent chattering.

The controller 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 and stays OFF until the measurement drops below `u_s - b/2` again.

# Connectors:
- `u_m`: The measurement input
- `u_s`: The reference signal
- `y`: The control signal output

# Parameters:
- `b`: Bandwidth around reference signal within which the controller does not react
- `k`: Controller gain. The output of the controller is scaled by this gain, i.e., `k = 2, bool = false` will result in an output of -2 or 2.
- `bool`: (structural) If true (default), the controller switches between 0 and k. If false, the controller switches between -k and k.
"""
component DiscreteBinaryController
  "The measurement input"
  u_m = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 640, "x2": 0, "y2": 740, "rot": 0}
      },
      "tags": []
    }
  }
  "The reference signal"
  u_s = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 200, "x2": 0, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
  "The control signal output"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "If true, the controller switches between 0 and k. If false, the controller switches between -k and k."
  structural parameter bool::Boolean = true
  "Bandwidth around reference signal within which the controller does not react"
  parameter b::Real = 0.1
  "Controller gain"
  parameter k::Real = 1
  "Internal state"
  variable s::Boolean
relations
  initial s@(clk-1) = 0
  s@clk = ifelse(s@(clk-1), u_m < u_s + b / 2, u_m < u_s - b / 2)
  if bool
    y = ifelse(s, k, 0)
  else
    y = ifelse(s, k, -k)
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/BinaryController.svg"}}
}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`SimpleRelayControl`](DiscreteBinaryController.md)
