|
| 1 | +# Bang-Bang / Hysteresis (Relay) Controller |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +The bang-bang controller is the simplest closed-loop regulator: its output switches between two discrete levels depending on whether the controlled variable is above or below a threshold. Without hysteresis, a plain threshold comparator chatters — switching at high frequency whenever noise nudges the signal across the boundary. |
| 6 | + |
| 7 | +Adding a **dead-band (Schmitt trigger)** eliminates chatter by giving the relay memory: once the output goes High it stays High until the measurement falls all the way to a *lower* threshold, and vice versa. This makes the relay a practical actuator-safe control primitive for thermostats, fridge compressors, tank level switches, and power-stage on/off regulation. |
| 8 | + |
| 9 | +## Mathematical Theory |
| 10 | + |
| 11 | +### State Transition |
| 12 | + |
| 13 | +The relay holds a binary state $s \in \{\text{Low}, \text{High}\}$ with the following transition rules: |
| 14 | + |
| 15 | +$$ |
| 16 | +s[k] = \begin{cases} |
| 17 | +\text{High} & \text{if } s[k-1] = \text{Low} \text{ and } x[k] \geq \theta_H \\ |
| 18 | +\text{Low} & \text{if } s[k-1] = \text{High} \text{ and } x[k] \leq \theta_L \\ |
| 19 | +s[k-1] & \text{otherwise} |
| 20 | +\end{cases} |
| 21 | +$$ |
| 22 | + |
| 23 | +where $\theta_L < \theta_H$ are the lower and upper switching thresholds (the hysteresis band). |
| 24 | + |
| 25 | +### Output Map |
| 26 | + |
| 27 | +$$ |
| 28 | +u[k] = \begin{cases} |
| 29 | +u_H & \text{if } s[k] = \text{High} \\ |
| 30 | +u_L & \text{if } s[k] = \text{Low} |
| 31 | +\end{cases} |
| 32 | +$$ |
| 33 | + |
| 34 | +The output levels $u_L$ and $u_H$ are arbitrary; common choices are $\{0, 1\}$ or $\{-1, +1\}$. |
| 35 | + |
| 36 | +### Hysteresis Band Width |
| 37 | + |
| 38 | +The band width $\Delta = \theta_H - \theta_L$ is the key design parameter. It bounds the switching frequency $f_s$ given a signal slope $\dot{x}$: |
| 39 | + |
| 40 | +$$ |
| 41 | +f_s \leq \frac{|\dot{x}|}{2\Delta} |
| 42 | +$$ |
| 43 | + |
| 44 | +A wider band reduces $f_s$ (protecting relays and power stages) at the cost of a larger steady-state limit cycle amplitude. |
| 45 | + |
| 46 | +## Complexity Analysis |
| 47 | + |
| 48 | +| Case | Time | Space | Notes | |
| 49 | +|------|--------|--------|--------------------------------------------| |
| 50 | +| All | $O(1)$ | $O(1)$ | Two comparisons, one state bit, one select | |
| 51 | + |
| 52 | +No arithmetic on the signal path — only comparisons — so the relay introduces no numerical error and is exactly representable in any floating-point format. |
| 53 | + |
| 54 | +## Step-by-Step Walkthrough |
| 55 | + |
| 56 | +**Setup:** band $[\theta_L, \theta_H] = [-0.2, 0.2]$, outputs $u_L = 0$, $u_H = 1$, initial state Low. |
| 57 | + |
| 58 | +| Step | $x[k]$ | Condition | $s[k]$ | $u[k]$ | |
| 59 | +|------|--------|------------------------------|--------|--------| |
| 60 | +| 1 | 0.0 | Low, $x < 0.2$ | Low | 0 | |
| 61 | +| 2 | 0.3 | Low, $x \geq 0.2$ → switch | High | 1 | |
| 62 | +| 3 | 0.1 | High, $x > -0.2$ → hold | High | 1 | |
| 63 | +| 4 | −0.3 | High, $x \leq -0.2$ → switch | Low | 0 | |
| 64 | +| 5 | 0.0 | Low, $x < 0.2$ → hold | Low | 0 | |
| 65 | + |
| 66 | +## Pitfalls & Edge Cases |
| 67 | + |
| 68 | +- **Inverted band.** $\theta_H \leq \theta_L$ latches the output in an undefined state; reject this at construction time via a precondition assertion. |
| 69 | +- **Exactly on threshold.** Transitions are inclusive: $x = \theta_H$ triggers Low→High and $x = \theta_L$ triggers High→Low. This avoids a dead-zone at the switching points. |
| 70 | +- **Zero-width band.** $\theta_H = \theta_L$ collapses the relay to a pure comparator (no hysteresis). The logic is still correct but offers no chatter suppression. |
| 71 | +- **Noise sizing.** The band width must exceed the peak-to-peak noise amplitude; otherwise noise alone drives state transitions at the sampling rate. |
| 72 | +- **Actuator minimum on-time.** Size $\Delta$ so that the minimum on-time (derived from $\Delta / |\dot{x}|_\text{max}$) is above the actuator's rated minimum switching period. |
| 73 | + |
| 74 | +## Variants & Generalizations |
| 75 | + |
| 76 | +| Variant | Key Difference | |
| 77 | +|-------------------------------------------|-----------------------------------------------------------------------------------------------------------| |
| 78 | +| **Plain comparator** | $\Delta = 0$; no memory, chatters on noise | |
| 79 | +| **Asymmetric band** | $\theta_H$ and $\theta_L$ not symmetric around the set-point; biases the duty cycle | |
| 80 | +| **Three-state relay** | Adds a dead-band output level $u_0$; used in motor direction control | |
| 81 | +| **Adaptive hysteresis** | Band width tracks signal variance online to maintain a target switching rate | |
| 82 | +| **Relay feedback test (Åström–Hägglund)** | Deliberate oscillation under relay feedback to identify the ultimate gain/period for automatic PID tuning | |
| 83 | + |
| 84 | +## Applications |
| 85 | + |
| 86 | +- **Thermostats and HVAC** — heating/cooling switched on/off around a temperature set-point. |
| 87 | +- **Tank and vessel level control** — pump on/off between high- and low-level floats. |
| 88 | +- **Power-stage converters** — hysteretic current-mode control in DC-DC converters and class-D amplifiers. |
| 89 | +- **Motor drive enable/disable** — protecting power stages with a current-band relay. |
| 90 | +- **Åström–Hägglund auto-tuning** — the relay feedback experiment that drives limit-cycle oscillation for PID parameter identification. |
| 91 | + |
| 92 | +## Connections to Other Algorithms |
| 93 | + |
| 94 | +| Algorithm | Relationship | |
| 95 | +|-------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------| |
| 96 | +| [PID Controller](Pid.md) | The relay's limit cycle can be used to identify PID tuning parameters via the Åström–Hägglund relay-feedback test | |
| 97 | +| [Saturation / Rate Limiter](SaturationRateLimiter.md) | Continuous-output counterpart for actuator constraint; often combined with a relay in cascaded loops | |
| 98 | + |
| 99 | +## References & Further Reading |
| 100 | + |
| 101 | +- K. J. Åström, R. M. Murray, *Feedback Systems: An Introduction for Scientists and Engineers*, Princeton University Press, 2008 — relay feedback, Chapter 10. |
| 102 | +- Ya. Z. Tsypkin, *Relay Control Systems*, Cambridge University Press, 1984. |
| 103 | +- K. J. Åström, T. Hägglund, "Automatic Tuning of Simple Regulators with Specifications on Phase and Amplitude Margins," *Automatica*, 20(5), 1984. |
0 commit comments