50(s+1)/(s+10)z = 1 − 10T(2−10T)/(2+10T)CPUT —1/(s(s+1))1e = r - read_adc()2u = A*u1 + B0*e + B1*e13write_dac(u) # hold T4u1, e1 = u, ey 0.00A—B0—B1—C(s)C(s)A B0 B1A B0 B1u
|z| = 1Euler unstable →00.10.20.3−2−101sample period T (s)pole zCONTROLLER POLE z vs Texact e^(−10T)EulerTustin012300.511.52time (s)position ySTEP RESPONSE, T = 0.02 scontinuousEulerTustin012300.511.52time (s)position ySTEP RESPONSE, T = 0.25 scontinuousEulerTustin
Lit wire with moving dashes: a design, numbers or a signal flowing along itBlocks: the design C(s), the two recipes, the computer with its three numbers and code, the motor and its positionPlots: controller pole against sample period, and step responses at T = 0.02 s and T = 0.25 s, computed from the modelPills carry C(s) into a recipe, the recipe's three numbers into memory, and the drive signal u to the motorA continuous lead controller is turned into code two ways: forward Euler replaces s by (z − 1)/T, Tustin by (2/T)(z − 1)/(z + 1). Each gives three numbers for the same four-line loop. Sampled fast (T = 0.02 s) both match the design. Sampled slowly (T = 0.25 s) Euler's controller pole lands at −1.50, outside the unit circle, and the loop blows up; Tustin's lands at −0.111 and the loop settles. Holding u constant for T acts roughly like a delay of T/2. At T = 0.02 s that delay is 0.01 s and barely shows; at T = 0.25 s it is 0.125 s, which is why even Tustin overshoots 53% there. A common practice is to sample many times faster than the closed-loop bandwidth.

From s to code

  1. A motor's shaft position is 1/(s(s + 1)). The lead controller C(s) = 50(s + 1)/(s + 10) makes the closed loop 50/(s² + 10s + 50): poles −5 ± 5j and 4.3% overshoot. It is a continuous-time design. A computer must now run it.

Two recipes

Fast and slow

The working

# step 1/11 · design
# about: Design C(s), Motor 1/(s(s+1)), Position y, continuous

plant       G(s) = 1 / (s(s + 1))      y'' + y' = u
controller  C(s) = 50(s + 1) / (s + 10)

C·G = 50 / (s(s + 10))   (the zero cancels the pole at -1)
closed loop  50 / (s² + 10s + 50)
poles        -5 ± 5j   ζ = 0.707   ωn = 7.07 rad/s
step         y = 1 - e^(-5t)(cos 5t + sin 5t)
peak         1.043 at t = 0.63 s (4.3 % overshoot)

The motor, the controller and both sample periods are round illustrative numbers. Every curve is computed from the sampled loop: the controller runs its difference equation every T, the DAC holds u, and the motor follows the exact solution of y'' + y' = u between samples. The Python on each step is plain Python 3 with the math module and prints the same numbers as the figure. python-control's sample_system and TransferFunction.sample accept method='euler' (forward difference) and method='tustin' or 'bilinear'.