Discrete PID
Sampled PID with a filtered derivative, a clamped integral, and output saturation. Every state updates at the sample instant, so exported C reproduces it exactly.
- signal
- Gradara equations
- 3 ports
- 6 parameters
discretePIDLibraryControlDescription
A sampled PID controller acting on the error between a reference and a measurement. At each sample it updates a clamped integral and a filtered derivative, then computes a saturated output that holds until the next sample.
All states change only at sample instants, so the block behaves like a fixed-rate control routine.
Example
PI and PID loops The same first-order plant under a sampled PI, a continuous PID, and a sampled PID controller.
Also in this example:SubtractStepPI controllerGainLow-pass filterPID
Ports
Inputs 2
-
ref
referenceSetpoint.
-
meas
measuredMeasured value, in the same units as the reference.
Outputs 1
-
y
Controller output, held between samples and limited to ±limit.
Parameters
-
Proportional gain
kp1Proportional gain.
-
Integral gain
ki0.5Integral gain, per second.
-
Derivative gain
kd0.05Derivative gain, in seconds.
-
Derivative filter
filterTime0.01 s≥ 0.0001
Derivative filter time constant Tf, in seconds, at least 0.1 ms.
-
Output limit
limit10≥ 0.001
Output limit, at least 0.001. Clamps both the output and the integral to ±limit.
-
Sample period
samplePeriod0.001 s≥ 0.0001
Sample period Ts, in seconds, at least 0.1 ms. The first sample is at t = 0.
Equations
At t = k · samplePeriod, with ⁻ marking the previous sample’s value: e = reference − measured integral = max(−limit, min(limit, integral⁻ + samplePeriod · ki · e)) derivative = (filterTime · derivative⁻ + kd · (e − e⁻))/(filterTime + samplePeriod) y = max(−limit, min(limit, kp · e + integral + derivative)) integral, derivative, and e⁻ start at 0.
Implementation
discrete Real e(start=0, fixed=true); discrete Real integral(start=0, fixed=true); discrete Real derivative(start=0, fixed=true); discrete Real errorPrev(start=0, fixed=true); when sample(0, samplePeriod) then e = reference - measured; integral = max(-limit, min(limit, pre(integral) + samplePeriod*ki*e)); derivative = (filterTime*pre(derivative) + kd*(e - pre(errorPrev)))/(filterTime + samplePeriod); errorPrev = e; y = max(-limit, min(limit, kp*e + integral + derivative)); end when;
Assumptions and limitations
- The derivative is a backward-Euler discretization of kd · s/(Tf · s + 1), acting on the error, so a reference step causes a derivative kick.
- e⁻ starts at 0, so a nonzero error at the first sample gives a derivative kick of kd · e/(filterTime + samplePeriod).
- Anti-windup is a clamp on the integral at ±limit, not back-calculation.
- The integral uses the current error, so there is no one-sample computation delay.
Tips
- The same limit bounds the integral and the output; they cannot be set separately.
Used in
These larger examples use it too. Open them from Examples in the app.
- Servo position control
See also
Select a block in Gradara and press F1 to open its page offline.