How it works
A PID controller computes its output from the error between the reference and the measured value: a proportional term reacts to the present error, an integral term removes steady-state error, and a derivative term reacts to how fast the error changes. In power electronics, PI controllers are the workhorse of current and voltage loops.
This generator produces a discrete-time implementation for a fixed sampling period dt, ready to drop into a control interrupt. You can choose C (including a direct-access style for ISRs and the TI C2000 CLA), C++, Python or Arduino, and enable anti-windup, feedforward, and input and output limits.
Anti-windup here clamps the integral accumulator to ±windup_limit, so it cannot keep growing while the output is saturated; this avoids large overshoot when the system comes out of saturation. Choose the limit so that Ki × limit roughly matches the output range. Output limits clamp the command, for example to the PWM duty range.
Design procedure
- 1Fix the sampling period dt to the real interrupt period and keep it constant.
- 2Start with a PI controller: tune Kp for the desired crossover frequency, then add Ki so that the integral zero sits below crossover.
- 3Set the output limits to the physical range of the actuator, for example 0 to 1 for a PWM duty cycle.
- 4Enable anti-windup and set the integral limit so that Ki times the limit roughly matches the output range.
- 5Add the derivative term only if the plant needs extra phase lead, and filter the measured value first, because the derivative amplifies noise.
Formulas
- Continuous PID
- u(t) = Kp·e(t) + Ki·∫e dt + Kd·de/dt
- Discrete integral (rectangular)
- I[k] = I[k−1] + e[k] · dt
- Discrete derivative
- D[k] = (e[k] − e[k−1]) / dt
- Output
- u[k] = Kp·e[k] + Ki·I[k] + Kd·D[k]
Worked example
For a current loop sampled at 20 kHz, set dt = 50 µs, choose PI, enable anti-windup and limit the output to the duty-cycle range 0–1. The generated function can be called once per PWM interrupt with the current reference and the measured current.
Common mistakes
- Using a dt that does not match the actual interrupt rate, which scales Ki and Kd by the wrong amount.
- Changing gains while the controller runs without handling the integral state, which causes a bump in the output.
- Applying the derivative to the error instead of the measurement, which produces a spike every time the reference changes.
- Leaving out output limits and anti-windup, which leads to large overshoot after saturation.
Frequently asked questions
Should I use PI or PID?
Most power converter current and voltage loops use PI. The derivative term amplifies measurement noise and switching ripple, so it is used only when the plant needs extra phase lead.
How do I choose Kp and Ki?
Start from the plant model and the desired crossover frequency, typically one tenth of the switching frequency or less for a current loop, then check the phase margin on a Bode plot.
Why does sampling time matter?
The discrete integral and derivative are scaled by dt. Using a different dt than the real interrupt period changes the effective gains.
Results are engineering estimates. Verify your design with datasheets, simulation and measurement.