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
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
The PID transfer function, C(s), is discretized using the Bilinear (Tustin) transform [1]:
Equation 2: Bilinear (Tustin) transform
Substituting yields the z-domain transfer function, C(z):
Equation 3: PID z-domain transfer function
where
Cross multiplying, where and are the denominator and numerator of Equation 3, then solving for the highest-order coefficient operating on U(z) [2]:
Yields:
Equation 4: Controller output z-domain equation
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 and zero initial conditions
Yielding the difference equation:
Equation 6: PID controller difference equation
where
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:
| C coefficient | Value |
|---|---|
c[0], c[8] | |
c[1], c[9] | |
c[2] | |
c[3] | |
c[4] | |
c[5], c[7] | |
c[6] |
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
where
PD Controller Difference Equation
Equation 8: PD controller difference equation
where
Design Considerations
Derivative Filter Coefficient N
Implementing an ideal differentiator with a transfer function of 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, , produces a transfer function of for the derivative branch of the PID controller.
This allows for a realizable system, and the derivative branch behaves like an ideal differentiator if is sufficiently large:
Sampling Time T
The sampling time, , must be sufficiently small so that the sampling frequency, , 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 approaches .
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

The plant has a natural frequency of rad/s, a damping ratio of and a DC gain of :
The PID gains are , and . The derivative low-pass filter cutoff frequency is set to half the clock frequency, .
Effect of PID Clock Frequency
The robust_pid clock frequency, , determines how closely the digital loop tracks its S-domain counterpart. In the schematic, is set as a multiple of the plant bandwidth, , which is derived from the plant parameters:
With rad/s and , this gives Hz. The figures below compare the step response of both loops at two clock frequencies, kHz (Figure 3) and Hz (Figure 5).
Figure 3. Step Response with Clock at 100x Plant Bandwidth

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

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

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 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 to measure the gain crossover frequency, , and the phase margin of the open-loop transfer function.
The open-loop transfer function is measured as . The negative sign is due to the 180° phase inversion introduced by the negative feedback summing junction, . The phase margin can be calculated as follows:
Figure 6. Phase Margin Measurement with Clock at 100x Plant Bandwidth

The measurements give Hz and at crossover, which results in a phase margin of approximately .
Figure 7. Open-Loop Bode Plot with Clock at 100x Plant Bandwidth

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