Index
COMPLETE2026-11-15

Robust PID Controller

A floating-point C implementation of a parallel PID controller discretized with the bilinear (Tustin) transform, supporting PID, PI, and PD topologies.

Robust PID Controller hardware

Brief

A floating-point C implementation of a parallel PID controller discretized using the bilinear (Tustin) transform.

Repository can be found here.

Features/Notes

  • PID, PI, and PD topologies supported.
  • Configurable sample period and derivative low-pass filter cutoff frequency.
  • Precomputed IIR coefficients.

Figure 1. Equivalent Parallel PID Controller

Rendering diagram…

Derivation of PID Controller Difference Equation

The s-domain transfer function of the PID controller seen in Figure 1 is given by:

Equation 1: PID s-domain transfer function

C(s)=U(s)E(s)=Kp+Kis+KdNss+NC(s)=\frac{U(s)}{E(s)}=K_p+\frac{K_i}{s}+K_d\frac{Ns}{s+N}

The PID transfer function, C(s), is discretized using the Bilinear (Tustin) transform [1]:

Equation 2: Bilinear (Tustin) transform

s=2Tz−1z+1s=\frac{2}{T}\frac{z-1}{z+1}

Substituting yields the z-domain transfer function, C(z):

Equation 3: PID z-domain transfer function

C(z)=U(z)E(z)=Kpα1+Kiα2+Kdα3(2NT+4)z2−8z+(4−2NT)C(z)=\frac{U(z)}{E(z)}=\frac{K_p\alpha_1+K_i\alpha_2+K_d\alpha_3}{(2NT+4)z^2-8z+(4-2NT)}

where

α1=(4+2NT)z2−8z+(4−2NT),α2=(NT2+2T)z2+2NT2z+(NT2−2T),α3=4Nz2−8Nz+4N,β1=2NT+4.\begin{aligned} \alpha_1&=(4+2NT)z^2-8z+(4-2NT),\\ \alpha_2&=(NT^2+2T)z^2+2NT^2z+(NT^2-2T),\\ \alpha_3&=4Nz^2-8Nz+4N,\\ \beta_1&=2NT+4. \end{aligned}

Cross multiplying, where A(z)A(z) and B(z)B(z) are the denominator and numerator of Equation 3, then solving for the highest-order coefficient operating on U(z) [2]:

U(z)A(z)=E(z)B(z)U(z)A(z)=E(z)B(z)

Yields:

Equation 4: Controller output z-domain equation

U(z)=Kp[E(z)−8β1z−1E(z)+4−2NTβ1z−2E(z)]+Ki[T2E(z)+2NT2β1z−1E(z)+NT2−2Tβ1z−2E(z)]+Kd4Nβ1[E(z)−2z−1E(z)+z−2E(z)]+8β1z−1U(z)+2NT−4β1z−2U(z).\begin{aligned} U(z)={}&K_p\left[E(z)-\frac{8}{\beta_1}z^{-1}E(z)+\frac{4-2NT}{\beta_1}z^{-2}E(z)\right]\\ &+K_i\left[\frac{T}{2}E(z)+\frac{2NT^2}{\beta_1}z^{-1}E(z)+\frac{NT^2-2T}{\beta_1}z^{-2}E(z)\right]\\ &+K_d\frac{4N}{\beta_1}\left[E(z)-2z^{-1}E(z)+z^{-2}E(z)\right]\\ &+\frac{8}{\beta_1}z^{-1}U(z)+\frac{2NT-4}{\beta_1}z^{-2}U(z). \end{aligned}

The time-shifting property is used to convert the z-domain equation, U(z), to the sampled-time equation U_n:

Equation 5: Time-shifting property, for integer sample delay kk and zero initial conditions

z−kX(z)⟷x[n−k]z^{-k}X(z)\longleftrightarrow x[n-k]

Yielding the difference equation:

Equation 6: PID controller difference equation

un=Kp[en−8β1en−1+4−2NTβ1en−2]+Ki[T2en+2NT2β1en−1+NT2−2Tβ1en−2]+Kd4Nβ1[en−2en−1+en−2]+8β1un−1+2NT−4β1un−2.\begin{aligned} u_n={}&K_p\left[e_n-\frac{8}{\beta_1}e_{n-1}+\frac{4-2NT}{\beta_1}e_{n-2}\right]\\ &+K_i\left[\frac{T}{2}e_n+\frac{2NT^2}{\beta_1}e_{n-1}+\frac{NT^2-2T}{\beta_1}e_{n-2}\right]\\ &+K_d\frac{4N}{\beta_1}\left[e_n-2e_{n-1}+e_{n-2}\right]\\ &+\frac{8}{\beta_1}u_{n-1}+\frac{2NT-4}{\beta_1}u_{n-2}. \end{aligned}

where

un=Current controller outputun−1=Output T second(s) agoun−2=Output 2T second(s) agoen=Current erroren−1=Error T second(s) agoen−2=Error 2T second(s) agoKp=Proportional gainKi=Integral gainKd=Derivative gainN=2πfc=Derivative low-pass filter pole location (rad/s)T=Sample period (s)β1=2NT+4\begin{aligned} u_n= \text{Current controller output}\newline u_{n-1}= \text{Output T second(s) ago}\newline u_{n-2}= \text{Output 2T second(s) ago}\newline e_n= \text{Current error}\newline e_{n-1}= \text{Error T second(s) ago}\newline e_{n-2}= \text{Error 2T second(s) ago}\newline K_p= \text{Proportional gain}\newline K_i= \text{Integral gain}\newline K_d= \text{Derivative gain}\newline N= 2\pi f_{c}= \text{Derivative low-pass filter pole location (rad/s)}\newline T= \text{Sample period (s)}\newline \beta_1= 2NT+4\newline \end{aligned}

Equation 6 is the infinite impulse response (IIR) difference equation of the PID controller in Figure 1. The PID exhibits an IIR (as opposed to a finite impulse response) due to its dependence on past outputs, introduced by the integrator and the filtered feedback path implemented in the derivative branch of the controller. This difference equation is implemented in robust_pid_run().

Mapping Equation 6 to Coefficient Array

The coefficients of the difference equation are stored in the c array of the robust_pid context. The coefficients are computed using the following equations:

τ=1N,σ1=2T−4τ,σ2=2T+4τ.\tau=\frac{1}{N},\qquad \sigma_1=2T-4\tau,\qquad \sigma_2=2T+4\tau.
C coefficientValue
c[0], c[8]8τ/σ28\tau/\sigma_2
c[1], c[9]σ1/σ2\sigma_1/\sigma_2
c[2](T2+2τT)/σ2=T/2(T^2+2\tau T)/\sigma_2=T/2
c[3]2T2/σ22T^2/\sigma_2
c[4](T2−2τT)/σ2(T^2-2\tau T)/\sigma_2
c[5], c[7]4/σ24/\sigma_2
c[6]8/σ28/\sigma_2

Derivation of Alternate Controller Topologies

The difference equations for the remaining topologies are derived in a similar manner.

PI Controller Difference Equation

Equation 7: PI controller difference equation

un=un−1+Kp(en−en−1)+KiT2(en+en−1).u_n=u_{n-1}+K_p(e_n-e_{n-1})+\frac{K_iT}{2}(e_n+e_{n-1}).

where

un=Current controller outputun−1=Output T second(s) agoen=Current erroren−1=Error T second(s) agoKp=Proportional gainKi=Integral gainT=Sample period (s)\begin{aligned} u_n= \text{Current controller output}\newline u_{n-1}= \text{Output T second(s) ago}\newline e_n= \text{Current error}\newline e_{n-1}= \text{Error T second(s) ago}\newline K_p= \text{Proportional gain}\newline K_i= \text{Integral gain}\newline T= \text{Sample period (s)}\newline \end{aligned}

PD Controller Difference Equation

Equation 8: PD controller difference equation

un=Kp(en+α1en−1)+Kdβ1(en−en−1)−α1un−1,u_n=K_p(e_n+\alpha_{1}e_{n-1})+K_d\beta_{1}(e_n-e_{n-1})-\alpha_{1}u_{n-1},

where

α1=T−2/NT+2/N,β1=2T+2/N.\alpha_{1}=\frac{T-2/N}{T+2/N},\qquad \beta_{1}=\frac{2}{T+2/N}.

Design Considerations

Derivative Filter Coefficient N

Implementing an ideal differentiator with a transfer function of KdsK_d s is not possible since the resulting PID s-domain transfer function would be an improper fraction, resulting in a non-causal (and therefore un-implementable) difference equation. The inclusion of the filter coefficient, NN, produces a transfer function of KdsN/(s+N)K_d s N / (s + N) for the derivative branch of the PID controller.

This allows for a realizable system, and the derivative branch behaves like an ideal differentiator if NN is sufficiently large:

lim⁡N→∞KdsNs+N=KdssN+1  ⟹  Kds0+1=Kds\lim_{N \to \infty} K_d \frac{s N}{s + N} = K_d \frac{s}{ \frac{s}{N} + 1} \implies K_d \frac{s}{0 + 1}= K_d s

Sampling Time T

The sampling time, TT, must be sufficiently small so that the sampling frequency, fs=1/Tf_s=1/T, is significantly higher than the system's Nyquist rate (twice the system bandwidth). If the sampling frequency is not sufficiently high relative to the system bandwidth, the frequency response of the discretized transfer function (Equation 6) diverges from the continuous transfer function (Equation 1) as the input frequency ff approaches fs/2f_s/2.

Code Usage

Source can be found here.

Populate a robust_pid_cfg_t and pass it to robust_pid_initCtx().

#include "robust_pid.h"

// PID controller context
static robust_pid_t pid;

robust_pid_cfg_t cfg = 
{
    .T          = 0.001f,   // PID loop invocation interval, sec 
    .LPF_3dB_Hz = 50.0f,    // Derivative branch LPF cutoff, Hz
    .Kp         = 1.2f,     // Proportional gain
    .Ki         = 0.4f,     // Integral gain
    .Kd         = 0.02f,    // Derivative gain
};


if (!robust_pid_initCtx(&pid, &cfg))
{
    // handle invalid configuration
}

Call robust_pid_run() once per sample period robust_pid_cfg_t.T.

// Run PID controller
float u = robust_pid_run(&pid, setpoint, measurement);

Change gains with robust_pid_updateGains() and the LPF cutoff with robust_pid_updateLPFCutoffFrequency().

robust_pid_updateGains(&pid, 1.5f, 0.4f, 0.02f);
robust_pid_updateLPFCutoffFrequency(&pid, 80.0f); 

Validation

sim/robust_pid_second_order_plant.qsch is a QSPICE schematic that implements the robust_pid C source code via a C++ block. The schematic follows the same format seen here.

The schematic implements two separate control loops, both driving identical second-order plants:

  • digital loop: controller is the discretized PID implemented via the robust_pid C driver.
  • analog loop: controller is the S-domain equivalent.

Figure 2. QSPICE Schematic of Digital and Analog PID Loops

robust_pid_second_order_plant.qsch QSPICE schematic
robust_pid_second_order_plant.qsch QSPICE schematic

The plant has a natural frequency of ωn=2π⋅80\omega_n = 2\pi \cdot 80 rad/s, a damping ratio of ζ=0.6\zeta = 0.6 and a DC gain of Av=1A_v = 1:

G(s)=Avωn2s2+2ζωns+ωn2G(s) = \frac{A_v \omega_n^2}{s^2 + 2\zeta\omega_n s + \omega_n^2}

The PID gains are Kp=2.0K_p = 2.0, Ki=200K_i = 200 and Kd=0.0001K_d = 0.0001. The derivative low-pass filter cutoff frequency is set to half the clock frequency, f3dB=fclk/2f_{3dB} = f_{clk}/2.

Effect of PID Clock Frequency

The robust_pid clock frequency, fclk=1/Tf_{clk} = 1/T, determines how closely the digital loop tracks its S-domain counterpart. In the schematic, fclkf_{clk} is set as a multiple of the plant bandwidth, fbwf_{bw}, which is derived from the plant parameters:

ωb=ωn1−2ζ2+(2ζ2−1)2+1,fbw=ωb2π\omega_b = \omega_n \sqrt{1 - 2\zeta^2 + \sqrt{(2\zeta^2 - 1)^2 + 1}}, \qquad f_{bw} = \frac{\omega_b}{2\pi}

With ωn=2π⋅80\omega_n = 2\pi \cdot 80 rad/s and ζ=0.6\zeta = 0.6, this gives fbw≈91.9f_{bw} \approx 91.9 Hz. The figures below compare the step response of both loops at two clock frequencies, fclk=100fbw≈9.19f_{clk} = 100 f_{bw} \approx 9.19 kHz (Figure 3) and fclk=4fbw≈367f_{clk} = 4 f_{bw} \approx 367 Hz (Figure 5).

Figure 3. Step Response with Clock at 100x Plant Bandwidth

QSPICE transient analysis: control signals for both loops (top), analog loop output tracking the set point (middle), and digital robust_pid loop output tracking the set point (bottom).
QSPICE transient analysis: control signals for both loops (top), analog loop output tracking the set point (middle), and digital robust_pid loop output tracking the set point (bottom).

With the clock running at 100 times the plant bandwidth, the digital PID closely follows its S-domain counterpart, as seen in Figure 4.

Figure 4. Analog v Digital PID Step Response with Clock at 100x Plant Bandwidth

Zoomed overlay of the digital loop plant output, analog loop plant output, and set point step
Zoomed overlay of the digital loop plant output, analog loop plant output, and set point step

Figure 5. Step Response with Clock at 4x Plant Bandwidth

QSPICE transient results over 300 ms with the clock at 4 times the plant bandwidth: control signals for both loops (top), analog loop output (middle), and digital robust_pid loop output (bottom).
QSPICE transient results over 300 ms with the clock at 4 times the plant bandwidth: control signals for both loops (top), analog loop output (middle), and digital robust_pid loop output (bottom).

Lowering the PID controller clock to 4 times the plant bandwidth noticeably degrades the digital loop. Its output overshoots to about 1.45 V and rings for roughly 150 ms before settling. As fclkf_{clk} approaches the plant bandwidth, the sample-and-hold delay and the frequency warping of the bilinear transform add phase lag, eroding the phase margin of the digital loop (see Sampling Time T).

Phase Margin

sim/robust_pid_sec_order_plant_pm.qsch runs a .bode analysis on the digital loop with fclk=100fbwf_{clk} = 100 f_{bw} to measure the gain crossover frequency, fgcf_{gc}, and the phase margin of the open-loop transfer function.

The open-loop transfer function is measured as G(s)H(s)=−fb/out_digitalG(s)H(s) = -\text{fb}/\text{out\_digital}. The negative sign is due to the 180° phase inversion introduced by the negative feedback summing junction, E(s)=R(s)−Y(s)E(s) = R(s) - Y(s). The phase margin can be calculated as follows:

PM=180∘+∠GH(j2πfgc)PM = 180^\circ + \angle GH(j 2 \pi f_{gc})

Figure 6. Phase Margin Measurement with Clock at 100x Plant Bandwidth

QSPICE .meas directives and output window: gain crossover frequency of 113.791 Hz, open-loop phase of -126.004 degrees at the crossover, and a resulting phase margin of 53.9956 degrees.
QSPICE .meas directives and output window: gain crossover frequency of 113.791 Hz, open-loop phase of -126.004 degrees at the crossover, and a resulting phase margin of 53.9956 degrees.

The measurements give fgc≈114f_{gc} \approx 114 Hz and ∠GH≈−126∘\angle G H \approx -126^\circ at crossover, which results in a phase margin of approximately 54∘54^\circ.

Figure 7. Open-Loop Bode Plot with Clock at 100x Plant Bandwidth

Bode plot of the open-loop transfer function. The cursor marks the 0 dB gain crossover at ~114 Hz, where the phase is approx. -126 degrees. Note the usage of '-TransferFunction' as outlined in the schematic comment (red text) in Figure 6.
Bode plot of the open-loop transfer function. The cursor marks the 0 dB gain crossover at ~114 Hz, where the phase is approx. -126 degrees. Note the usage of '-TransferFunction' as outlined in the schematic comment (red text) in Figure 6.

References

  1. Discrete control #5: The bilinear transform — Brian Douglas
  2. N. S. Nise, Control Systems Engineering, 8th ed., Wiley, 2019, Sec. 13.11 ("Implementing the Digital Compensator"), pp. 616–619.