---
title: Digital control
heading: Sampling a plant with a zero-order hold, mapping poles with z = e^{sT}, choosing the sample rate, converting analog controllers with the Tustin transform and prewarping, writing a discrete PID, and the gain limits sampling introduces
eyebrow: S9 · Lesson 41 · Digital control
xp: 50
---

::: hook
Almost every controller built today is a few lines of code in a microcontroller's timer interrupt: read the ADC, compute, write the PWM. Between samples the controller is blind and its output is frozen, so the loop is part continuous and part discrete. Sampled fast enough, a digital loop behaves like the analog design it came from; sampled too slowly, it loses phase, gain margin and finally stability. This lesson gives the tools for both: exact discrete models of the plant, and conversions of continuous controllers into code.
:::

::: play
- A pair of continuous poles at $-\sigma \pm j\omega$ sampled every T: they map to $z = e^{(-\sigma \pm j\omega)T}$ in the z-plane, where stability means inside the unit circle.
- Fast sampling (small T) crowds the poles near z = 1, where all slow continuous dynamics end up. Slow sampling swings them around the circle.
- With ωT = π the poles reach the negative real axis: the sampled system can no longer tell this oscillation from one at a lower frequency (aliasing).
:::

```sim plot
view: zplane
range: 1.5
slider: s = 5..200 = 35
slider: w = 0..300 = 35.7
slider: T = 0.2..20 = 1 ms log
den: z^2 - 2*exp(-s*T/1000)*cos(w*T/1000)*z + exp(-2*s*T/1000)
readout: pole radius |z| = exp(-s*T/1000) | digits: 4
readout: pole angle, ° = w*T/1000*180/pi | digits: 3
```

::: predict
## The motor $20/(0.016s + 1)$ under proportional speed control is stable for every gain in continuous time. Sampled at 1 kHz with a hold, how large can $K_p$ be?

- [ ] Any value: sampling doesn't change stability
- [x] About 1.6
- [ ] About 16
- [ ] About 0.06

::: reveal
The sampled plant is $y_{k+1} = 0.939y_k + 1.212u_k$, and with $u_k = -K_py_k$ the closed-loop pole is $0.939 - 1.212K_p$. It leaves the unit circle at −1 when $K_p$ = 1.939/1.212 = 1.60. A first-order continuous loop can't go unstable, but the hold's half-sample delay plus a sample of computation make the sampled one able to.
:::
:::

::: nameit
## The sampled plant

A zero-order hold keeps $u$ constant over each sample period T. For $\dot{x} = Ax + Bu$ the exact discrete model is

$$x_{k+1} = \Phi x_k + \Gamma u_k, \qquad \Phi = e^{AT}, \qquad \Gamma = \int_0^Te^{A\tau}d\tau\,B$$

For $K_0/(\tau s + 1)$: $\Phi = e^{-T/\tau}$ and $\Gamma = K_0(1 - e^{-T/\tau})$.

## Poles and the sample rate

Each continuous pole maps to $z = e^{sT}$: radius $e^{-\sigma T}$, angle $\omega T$. Stable means |z| < 1. A common rule samples 10 to 30 times faster than the closed-loop bandwidth; the hold's delay (lesson 30) costs $\omega_cT/2$ radians of margin.

## From analog designs: the Tustin transform

Replace s by

$$s \leftarrow \frac{2}{T}\frac{z - 1}{z + 1}$$

which maps the left half-plane exactly onto the unit disk, so stable stays stable. It warps frequency: analog ω lands at digital $\frac{2}{T}\tan^{-1}\frac{\omega T}{2}$. To place a corner exactly at f, **prewarp**: design the analog prototype at $\tan(\pi fT)/(\pi T)$.

## Discrete PI and PID

Tustin applied to $K_p(1 + \frac{1}{T_is})$ gives

$$u_k = u_{k-1} + K_p\left(1 + \frac{T}{2T_i}\right)e_k - K_p\left(1 - \frac{T}{2T_i}\right)e_{k-1}$$

the **velocity form**, which updates the output by increments and makes anti-windup easy (clamp $u_k$). Add a filtered difference $(e_k - e_{k-1})/T$ for the derivative.

## Things only digital loops do

A discrete controller can place every closed-loop pole at z = 0: the **deadbeat** response settles exactly in n samples, at the cost of large actuator swings. And quantization of the ADC and of fixed-point coefficients (lesson 14 of C12) can cause small limit cycles near the setpoint.
:::

::: example
## Discretize the motor speed plant at 1 kHz and convert the PI controller of lesson 15 ($K_p$ = 0.4, $T_i$ = 16 ms).

::: solution
1. **Discretize the plant.**
   $$\begin{aligned} \Phi &= e^{-0.001/0.016} \\ &= 0.9394 \\ \Gamma &= 20(1 - 0.9394) \\ &= 1.212 \end{aligned}$$
   so $y_{k+1} = 0.9394y_k + 1.212u_k$.
2. **Tustin PI.**
   $$\begin{aligned} u_k &= u_{k-1} + 0.4125e_k \\ &\quad - 0.3875e_{k-1} \end{aligned}$$
   three multiplies and adds per interrupt.
3. **Check the sample rate.** The closed loop's 2 ms time constant means a bandwidth of 500 rad/s (80 Hz), and 1 kHz is only 12.6 times that, at the low end of the 10-to-30 rule.
4. **A faster rate.** Sampling at 2 kHz or more would track the analog design more closely.

**Answer:** the plant becomes $y_{k+1} = 0.9394y_k + 1.212u_k$ and the controller $u_k = u_{k-1} + 0.4125e_k - 0.3875e_{k-1}$.
:::
:::

::: example
## Where do the servo poles of lesson 34, $-35 \pm j35.7$, land when sampled at 1 kHz?

::: solution
1. **Radius.**
   $$\begin{aligned} r &= e^{-35 \times 0.001} \\ &= 0.9656 \end{aligned}$$
2. **Angle.**
   $$\begin{aligned} \theta &= 35.7 \times 0.001\ \text{rad} \\ &= 2.05° \end{aligned}$$
3. **Why so close to z = 1.** That's as it should be when sampling is about 125 times faster than the poles' natural frequency (50 rad/s, 8 Hz).
4. **At 100 Hz.** The radius would be 0.705 and the angle 20.5°.

**Answer:** at radius 0.9656 and angle ±2.05°, close to z = 1.
:::
:::

::: example
## A plant $2/(0.01s + 1)$ is sampled every 5 ms with a hold. What proportional gain puts the closed-loop pole at z = 0, and what does the output settle to?

::: solution
1. **Discretize.**
   $$\begin{aligned} \Phi &= e^{-0.5} \\ &= 0.607 \\ \Gamma &= 2(1 - \Phi) \\ &= 0.787 \end{aligned}$$
2. **Deadbeat gain.** With $u_k = K(r - y_k)$ the closed-loop pole is $\Phi - \Gamma K$, which is zero for
   $$\begin{aligned} K &= \Phi/\Gamma \\ &= 0.771 \end{aligned}$$
   deadbeat, settling in one sample.
3. **Where it settles.**
   $$\begin{aligned} y &= \Gamma Kr \\ &= \Phi r \\ &= 0.607r \end{aligned}$$
   a 39% error.
4. **What deadbeat needs.** Deadbeat speed needs integral action as well, and its large first move often saturates the actuator.

**Answer:** K = 0.771 puts the pole at z = 0, and the output settles to 0.607r, a 39% error.
:::
:::

::: check
given: s = 75 | 200; T = 2 | 5 | 10
answer: exp(-s*T/1000)
tolerance: 0.5%

## A continuous pole with real part −{s} s⁻¹ is sampled every {T} ms. What's the radius of the discrete pole?

::: right
Right. $|z| = e^{-\sigma T}$ = {answer}.
:::

::: misses
- [1 - s*T/1000] That's the first-order approximation; the exact map is the exponential.
- [any] Not yet. $z = e^{sT}$.
:::
:::

::: check
given: t = 2 | 5 | 20; T = 0.5 | 2; K = 2 | 20
answer: K*(1 - exp(-T/t))
tolerance: 0.5%

## A plant $\given{K}/(\tau s + 1)$ with τ = {t} ms is sampled every {T} ms with a hold. What's Γ, the input coefficient of the discrete model?

::: right
Right. $\Gamma = K_0(1 - e^{-T/\tau})$ = {answer}.
:::

::: misses
- [K*T/t] That's the small-T approximation.
- [any] Not yet. Integrate the step response over one sample.
:::
:::

::: check
given: t = 2 | 5 | 20; T = 0.5 | 2; K = 2 | 20
answer: (1 + exp(-T/t))/(K*(1 - exp(-T/t)))
tolerance: 0.5%

## The same sampled plant gets $u_k = -K_py_k$. What's the largest stable $K_p$?

::: right
Right. The pole $\Phi - \Gamma K_p$ reaches −1 at $K_p = (1 + \Phi)/\Gamma$ = {answer}.
:::

::: misses
- [(1 - exp(-T/t))/(K*(1 - exp(-T/t)))] Instability comes when the pole reaches −1, not +1.
- [any] Not yet. Require $|\Phi - \Gamma K_p| < 1$.
:::
:::

::: check
given: K = 0.2 | 1 | 5; i = 2 | 10; T = 0.5 | 2
answer: K*(1 + T/(2*i))
tolerance: 0.5%

## A PI controller ($K_p$ = {K}, $T_i$ = {i} ms) is converted by Tustin's method at T = {T} ms. What's the coefficient on $e_k$?

::: right
Right. $K_p(1 + T/2T_i)$ = {answer}.
:::

::: misses
- [K*(1 + T/i)] Tustin uses half the step: $T/2T_i$.
- [any] Not yet. Substitute $s = \frac{2}{T}\frac{z - 1}{z + 1}$ and collect terms.
:::
:::

::: check
given: f = 100 | 200 | 400; s = 1 | 2
answer: tan(pi*f/(s*1000))/(pi/(s*1000))
unit: Hz
tolerance: 0.5%

## A Tustin-converted filter sampled at {s} kHz needs its corner at exactly {f} Hz. What corner should the analog prototype have?

::: right
Right. Prewarp: $\tan(\pi fT)/(\pi T)$ = {answer} Hz.
:::

::: misses
- [f] Without prewarping the corner lands low, because Tustin compresses frequencies.
- [any] Not yet. Invert the warping $\omega_d = \frac{2}{T}\tan^{-1}\frac{\omega_aT}{2}$.
:::
:::

::: check
given: w = 20 | 100 | 500; T = 1 | 5
answer: w*T/1000*180/pi
unit: °
tolerance: 0.5%

## A continuous pole pair with damped frequency {w} rad/s is sampled every {T} ms. At what angle do the discrete poles sit?

::: right
Right. $\omega T$ = {answer}°.
:::

::: misses
- [w*T/1000] Convert radians to degrees.
- [any] Not yet. The angle of $e^{j\omega T}$.
:::
:::

::: check
kind: parts
given: t = 5 | 10 | 50; T = 1 | 5; K = 1 | 2 | 5

## A plant $\given{K}/(\tau s + 1)$ with τ = {t} ms is sampled every {T} ms with a hold.

::: part
answer: exp(-T/t)
tolerance: 0.5%

### What is Φ?

::: misses
- [any] $e^{-T/\tau}$.
:::
:::

::: part
answer: exp(-T/t)/(K*(1 - exp(-T/t)))
tolerance: 0.5%

### What proportional gain puts the closed-loop pole at z = 0?

::: misses
- [any] $\Phi - \Gamma K_p = 0$ with $\Gamma = K_0(1 - \Phi)$.
:::
:::

::: part
answer: exp(-T/t)
tolerance: 0.5%

### What fraction of a step reference does the output settle to?

::: misses
- [any] $\Gamma K_p/(1 - \Phi + \Gamma K_p)$, which here equals Φ.
:::
:::
:::

::: check
kind: derivation
vars: s = 0.1..100, w = 0.1..100, T = 0.0001..0.01

## A continuous pole at $-s \pm jw$ is sampled every T seconds.

::: step
answer: exp(-s*T)

### What is the discrete pole's radius?
:::

::: step
answer: w*T

### What is its angle, in radians?
:::

::: step
answer: exp(-s*T)*cos(w*T)

### What is its real part?
:::

::: right
Right. $z = e^{sT}$ maps decay to radius and frequency to angle.
:::
:::

::: explain
## Why can a sampled first-order loop go unstable when the continuous one can't?

::: model
In continuous time a first-order loop responds to the error instantly, so more gain only makes the correction faster. A sampled loop measures once, then holds its command for a whole period, and its correction is based on information that's one sample old. With high gain, a small error produces a command so large that by the next sample the output has overshot the setpoint by more than the original error, in the other direction. Each sample then flips and grows the error, which is the discrete pole crossing −1. The hold and the sampling act like a delay, and delay is what limits gain.
:::
:::

::: practice

::: check
given: s = 20 | 150; T = 1 | 4
answer: exp(-s*T/1000)
tolerance: 0.5%

## Discrete pole radius: real part −{s}, T = {T} ms?

::: misses
- [any] $e^{-\sigma T}$.
:::
:::

::: check
given: K = 0.5 | 3; i = 5 | 50; T = 0.2 | 2
answer: -K*(1 - T/(2*i))
tolerance: 0.5%

## Tustin PI coefficient on $e_{k-1}$: $K_p$ = {K}, $T_i$ = {i} ms, T = {T} ms?

::: misses
- [any] $-K_p(1 - T/2T_i)$.
:::
:::

::: check
given: f = 100 | 300; s = 2 | 5
answer: tan(pi*f/(s*1000))/(pi/(s*1000))
unit: Hz
tolerance: 0.5%

## Prewarped analog corner for {f} Hz at {s} kHz?

::: misses
- [any] $\tan(\pi fT)/(\pi T)$.
:::
:::
:::
