How it works
A PI controller tuned in the s-domain has to become a difference equation before it runs on an MCU or DSP. Every common method gives the same shape, u[k] = u[k-1] + b0·e[k] + b1·e[k-1]; only b0 and b1 change. The calculator computes them for Tustin (bilinear), Tustin with frequency prewarping, backward Euler, and forward Euler, which for a PI is identical to the zero-order-hold equivalent.
It then evaluates each discrete controller on the unit circle, z = e^(jωT), and compares it with Kp + Ki/(jω) at the frequency you enter, usually the loop crossover. The Bode plot runs up to the Nyquist frequency fs/2, where the discrete versions bend away from the continuous one.
The last result is the one that usually matters more: the phase lost to the sample-and-hold (ωT/2) and to a one-sample computation delay (another ωT). These belong to the loop, not to the controller, so no choice of discretization method removes them.
Design procedure
- 1Tune Kp and Ki in continuous time with the plant model, and note the crossover frequency.
- 2Enter the sample rate the control loop really runs at. With interrupts that skip cycles, this is lower than the PWM frequency.
- 3Pick Tustin unless you have a reason not to, and copy b0 and b1. Keep the prewarp frequency at the PI zero, or set it to the crossover if that is where you want an exact match.
- 4Subtract the delay phase shown here from the phase margin you designed for. If the result is too small, lower the crossover or reduce the delay; changing the method won't recover it.
Formulas
- Difference equation
- u[k] = u[k−1] + b0·e[k] + b1·e[k−1]
- Tustin
- b0 = Kp + Ki·T/2, b1 = −Kp + Ki·T/2
- Tustin, prewarped at ωp
- K = ωp / tan(ωp·T/2); b0 = Kp + Ki/K, b1 = −Kp + Ki/K
- Backward Euler
- b0 = Kp + Ki·T, b1 = −Kp
- Forward Euler = ZOH
- b0 = Kp, b1 = −Kp + Ki·T
- Delay phase
- φ_ZOH = ω·T/2, φ_ZOH+1 sample = 1.5·ω·T (degrees: ×180/π)
Worked example
Kp = 0.5, Ki = 2000 s⁻¹ (PI zero at 637 Hz), fs = 20 kHz, crossover 1 kHz. Tustin gives b0 = 0.55, b1 = −0.45. Backward Euler gives 0.60 / −0.50, and forward Euler (ZOH) gives 0.50 / −0.40. At 1 kHz the continuous PI has a phase of −32.48°. Tustin lands within 0.21° and 0.02 dB of it, while the two Euler versions are about 2.6° and 0.6 dB off in opposite directions. Compare that with the delay: the hold alone costs 9.0° at 1 kHz, and hold plus one sample of computation costs 27.0°. Halving fs to 10 kHz doubles that to 54°. If phase margin is short, look at the sampling and update timing before worrying about Tustin versus Euler.
Common mistakes
- Mixing units: Ki here is in 1/s (the integral gain multiplying ∫e dt). If your tuning uses Ti, Ki = Kp/Ti.
- Using the PWM frequency as fs when the controller only runs every second or fourth PWM period. T is the controller's own update period.
- Forgetting anti-windup. The difference equation above has no output limit; when u saturates, clamp it and stop integrating, or the loop overshoots badly after a large step.
Frequently asked questions
Which method should I use?
For a PI at a crossover well below fs/10, Tustin. It keeps gain and phase closest to the continuous design, and it maps every stable continuous pole inside the unit circle. The Euler forms are fine when the crossover is far below fs, and they are simpler to explain in code reviews.
What does prewarping change for a PI?
Very little. Prewarping makes the discrete response match exactly at one frequency. For a PI at fs/20 the coefficients move in the fourth decimal place. It matters more for notch filters and resonant controllers, where the frequency is the whole point.
Why is forward Euler the same as ZOH here?
The ZOH equivalent of an integrator Ki/s is Ki·T/(z−1), which is exactly the forward-Euler integrator. The proportional term passes through unchanged, so for a PI the two methods give the same b0 and b1. For other transfer functions they differ.
Which method does this site's PID code generator use?
Its generated code adds e[k]·dt to the integral before using it, which is backward Euler (b0 = Kp + Ki·T, b1 = −Kp). At the example above that is about 2.6° of phase lead compared with the continuous PI, small next to the delay but worth knowing.
References
- G. F. Franklin, J. D. Powell, M. L. Workman, "Digital Control of Dynamic Systems," 3rd ed., Addison-Wesley, 1998.
Results are engineering estimates. Verify your design with datasheets, simulation and measurement.