PID

PID Controller

Continuous
Run the live example Free · no sign-up · runs in your browser

The PIDController block implements an industrial-grade continuous-time Proportional-Integral-Derivative feedback controller. It combines immediate error correction (proportional action), steady-state offset elimination (integral action), and anticipatory damping of error transients (derivative action with first-order high-frequency filtering). The block includes integrated anti-windup clamping, configurable parallel or ideal series topologies, optional individual component output ports, and an external reset port for bumpless initialization.

Video tutorial

Mathematical Model

In the standard parallel formulation, the continuous Laplace domain transfer function relating error E(s)E(s) to control command U(s)U(s) is:

U(s)=(Kp+Kis+KdNss+N)E(s)U(s) = \left( K_p + \frac{K_i}{s} + K_d \frac{N s}{s + N} \right) E(s)

where KpK_p is kp, KiK_i is ki, KdK_d is kd, and NN is the derivative filter coefficient (n).

In the time domain, the controller is realized using two continuous ODE states: an integrator state xI(t)x_I(t) and a filtered derivative state xD(t)x_D(t):

x˙I(t)=Kie(t)x˙D(t)=N(KdNe(t)−xD(t))\begin{aligned} \dot{x}_I(t) &= K_i e(t) \\ \dot{x}_D(t) &= N \left( K_d N e(t) - x_D(t) \right) \end{aligned}

The raw unsaturating control signal is:

uraw(t)=Kpe(t)+xI(t)+(KdNe(t)−xD(t))u_{\text{raw}}(t) = K_p e(t) + x_I(t) + \left( K_d N e(t) - x_D(t) \right)

The final output is clamped between saturation bounds:

u(t)=clamp⁡(uraw(t),umin⁡,umax⁡)=min⁡(max⁡(uraw(t),umin⁡),umax⁡)u(t) = \operatorname{clamp}(u_{\text{raw}}(t), u_{\min}, u_{\max}) = \min(\max(u_{\text{raw}}(t), u_{\min}), u_{\max})

When the output saturates (uraw≠uu_{\text{raw}} \ne u), the integrator state is clamped (x˙I=0\dot{x}_I = 0 if (uraw−u)e>0(u_{\text{raw}} - u) e > 0), preventing integrator windup and long recovery delays.

In series (ideal) form (form = "series"), the controller parameters relate via interaction terms: u(t)=Kp(e(t)+1Ti∫e dτ+Tddedt)u(t) = K_p \left( e(t) + \frac{1}{T_i} \int e\, d\tau + T_d \frac{de}{dt} \right).

Inputs & Outputs

Direction ID Label Type Status
→ In error Error number Required
→ In reset Reset number Optional
← Out out Output number Output
← Out p P number Output
← Out i I number Output
← Out d D number Output

Parameters

Parameters Label Type Default Description
kp Proportional Gain (Kp) number 1 Controls immediate response to error
ki Integral Gain (Ki) number 0.1 Eliminates steady-state error over time
kd Derivative Gain (Kd) number 0.1 Dampens oscillations by responding to error rate of change
n Filter Coefficient (N) number 100 Derivative filter cutoff frequency. Higher = less filtering
form PID Form select parallel Parallel: u = Kp·e + Ki·∫e + Kd·de/dt. Series: u = Kp·(1 + 1/(Ti·s) + Td·s)·e
min Saturation Min number -1000000000000000 Lower output limit for anti-windup protection
max Saturation Max number 1000000000000000 Upper output limit for anti-windup protection
showComponents Show P/I/D Outputs boolean false Expose individual Proportional, Integral, and Derivative term outputs

Usage Examples

Example 1: Closed-Loop DC Motor Speed Control with Anti-Windup

Regulating motor shaft angular velocity under fluctuating mechanical loads. Configuring kp = 2.5, ki = 8.0, kd = 0.05, and n = 150 with output limits min = -24.0 V and max = 24.0 V. In response to a 1000 RPM step command, the controller delivers maximum 24 V24\text{ V} initial voltage to accelerate the armature without integrator over-accumulation, settling cleanly within 80 ms80\text{ ms} with zero steady-state error.

Example 2: Thermal Heat Exchanger Temperature Regulation

Controlling temperature in a fluid heat exchanger. Because thermal systems exhibit large time constants and sensor noise, configuring kp = 1.2, ki = 0.02, kd = 10.0, and n = 10 with limits min = 0.0% and max = 100.0% (valve position). The low filter coefficient N=10N = 10 filters out high-frequency thermocouple noise while derivative lead action anticipates slow thermal drift.

Remarks & Best Practices

  • Anti-Windup Protection: Clamping the output limits automatically stops integrator accumulation whenever an actuator saturates, preventing huge overshoot transients when returning to the linear operating region.
  • Derivative Noise Filtering: Pure mathematical differentiation (ss) amplifies high-frequency measurement noise infinitely. The parameter NN caps derivative gain to Kd⋅NK_d \cdot N at high frequencies, ensuring physical stability.
  • Continuous State Integration: Formulated with continuous ODE states in Rust/WASM, enabling compatibility with both explicit (RK4, RK45) and stiff implicit (TR-BDF2) numerical simulation solvers.
  • External Reset and Mode Switching: While the optional reset input is high (ureset>0u_{\text{reset}} > 0) the integrator is held at zero, so the I term reads zero and the block acts as a PD controller. Releasing the reset restarts integration from zero. The derivative filter keeps tracking the error throughout, so neither edge of the reset produces a derivative kick.

Frequently Asked Questions (FAQ)

Q: How does PID anti-windup clamping work and when should I use back-calculation?

Clamping (conditional integration) halts integration whenever the actuator output saturates AND the control error has the same sign as the saturated output. This prevents the integrator state from accumulating error that cannot be physically actuated. Back-calculation continuously discharges the integrator state via a feedback gain KtK_t proportional to the difference between demanded and saturated control output (u−usatu - u_{sat}). Clamping is ideal for hard physical limits (valves, PWM duty cycle) to eliminate severe overshoot upon setpoint recovery.

Q: What is the derivative filter coefficient N in the PID block?

An ideal derivative term D(s)=Kd⋅sD(s) = K_d \cdot s has infinite high-frequency gain, amplifying sensor noise and causing actuator chatter. BlockWerk implements a real, band-limited derivative filter: D(s)=Kd⋅s1+sKdND(s) = \frac{K_d \cdot s}{1 + s \frac{K_d}{N}}. The parameter NN (typically chosen between 8 and 20) places a low-pass roll-off pole at frequency ωp=N/(Kd/Kp)\omega_p = N / (K_d / K_p) rad/s, providing derivative phase lead at control frequencies while attenuating high-frequency noise.

Related Components