PI (discrete)
Sampled PI with output saturation and a bounded integral state. Every state updates at the sample instant, so exported C reproduces it exactly.
- signal
- Gradara equations
- 3 ports
- 4 parameters
piLibraryControlDescription
A sampled PI controller acting on the error between a reference and a measurement. At each sample it updates a clamped integral and computes a saturated output, which then holds until the next sample.
Use it for loops such as motor speed or output-voltage regulation where a fixed controller rate matters.
Example
PI and PID loops The same first-order plant under continuous PI and PID controllers and their sampled counterparts.
Also in this example:Discrete PIDStepGainLow-pass filterPI (continuous)PID (continuous)
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
ki2Integral gain, per second.
-
Output limit
limit10≥ 0.001
Output limit, at least 0.001, in the unit of the output. 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
error = reference − measured At t = k · samplePeriod: integral = max(−limit, min(limit, integral⁻ + samplePeriod · ki · error)) y = max(−limit, min(limit, kp · error + integral)) integral⁻ is the value from the previous sample; integral starts at 0.
Implementation
discrete Real integral(start=0, fixed=true); Real error; error = reference - measured; when sample(0, samplePeriod) then integral = max(-limit, min(limit, pre(integral) + samplePeriod*ki*error)); y = max(-limit, min(limit, kp*error + integral)); end when;
Assumptions and limitations
- The integral is updated with the current error (backward Euler), so a step in error affects y at the same sample, with no computation delay.
- Anti-windup is a clamp on the integral at ±limit, not back-calculation. The integral can still sit at the limit while the proportional term alone saturates the output.
- No derivative term. For one, use Discrete PID; for a continuous controller with back-calculation anti-windup, use PI (continuous).
- The error is read only at sample instants; nothing between samples is seen.
Tips
- Choose samplePeriod well below the loop’s time constants; each sample is a simulation event.
Used in
These larger examples use it too. Open them from Examples in the app.
- Data center cooling control
- Motor speed control
- 480 VAC → 24 VDC flyback
See also
Select a block in Gradara and press F1 to open its page offline.