The parts
Our plant is a small DC motor spinning at 1000 rpm, and we want 1500 rpm. Its speed changes smoothly at every instant. A tachometer, a speed sensor, turns it into a voltage: 1 V per 1000 rpm. A computer cannot read that directly.
A clock ticks every T = 10 ms. T is the sample period; 100 ticks per second is the sample rate. Each tick starts one round of the loop. Between ticks the computer learns nothing new about the motor.
One tick
On a tick the ADC (analog-to-digital converter) takes a snapshot of the sensor voltage and rounds it down to one of 1024 levels (10 bits). One level is 2.93 rpm, so 1000 rpm becomes code 341.
The computer turns code 341 back into 999.0 rpm and finds the error, 501.0 rpm. Its rule: 15 V, enough to hold 1500 rpm, plus 0.04 V for every rpm of error. That asks for 35.04 V.
The computer writes the result to the DAC (digital-to-analog converter). A 12-bit DAC with its amplifier makes 0 to 48 V in 4096 steps, so it receives code 2990 and puts out 35.04 V to the motor.
The DAC keeps 35.04 V perfectly flat for the whole 10 ms. Holding each value for one period is a zero-order hold. The motor speeds up on its own curve, reaching 1122 rpm. The computer is blind until the next tick.
The loop runs
Sample, compute, hold, repeat. The steps of voltage shrink as the error shrinks. After 13 ticks (130 ms) the speed stays within 1% of 1500 rpm. The dots on the plot are the only speeds the computer ever saw.
The maths at the ticks
Because the voltage is flat between ticks, the motor can be solved exactly from tick to tick: speed[k+1] = a·speed[k] + (1 − a)·100·volts[k], with a = e^(−T/τ) = 0.951. τ, the motor's time constant, is 0.2 s. This is the ZOH equivalent.
Put the rule into that equation (ignore the rounding) and the error obeys error[k+1] = z·error[k], with z = a − 4(1 − a) = 0.756. Each tick keeps 75.6% of the error. |z| < 1 means the error dies away.
Sampling too slowly
Same rule, but the clock ticks every 80 ms. Now a = 0.670 and z = −0.648: the error flips sign every tick. The 35 V held for 80 ms throws the speed to 1825 rpm, then the voltage drops to 1.99 V. It rings.
z reaches −1 when T = τ·ln(5/3) = 102 ms; any slower and the loop is unstable. Run continuously, this rule could never ring. Sampling adds a delay, so tick several times faster than the loop responds: here 10 ms against 40 ms.
What is happening
# step 1/11 · plant
# about: Motor (plant), Tachometer (sensor)
plant DC motor, speed 1000 rpm now
goal 1500 rpm
sensor tachometer, 1 V per 1000 rpm
1000 rpm → 1.000 V
motor model 200 ms time constant,
100 rpm per volt when settled
The speed is an analog signal: it has a
value at every instant. A computer only
handles numbers, one at a time.
The motor is a first-order model with illustrative numbers: 100 rpm per volt when settled and a 200 ms time constant. Every curve and number in the figure is simulated tick by tick from that model, including the ADC's rounding down and the DAC's rounding. The Python on the steps is plain Python 3 with the math module. Between ticks it moves the motor with the exact solution of its equation, because the voltage is held constant, just as the figure does.