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
BlockDiscrete PIDKinddiscretePIDLibraryControl

Description

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 continuous PI and PID controllers and their sampled counterparts.

Set point (PI)PIPlant (PI)y (PI)Set point (PID)PIDPlant (PID)y (PID)Set point (PI (sampled))PI (sampled)Plant (PI (sampled))y (PI (sampled))Set point (PID (sampled))PID (sampled)Plant (PID (sampled))y (PID (sampled))
In Gradara, select a Discrete PID block and press F1, then choose Open example. It opens as a new model in My models, ready to run.

Also in this example:PI (discrete)StepGainLow-pass filterPI (continuous)PID (continuous)

Ports

Inputs 2

  • refreference

    Setpoint.

  • measmeasured

    Measured value, in the same units as the reference.

Outputs 1

  • y

    Controller output, held between samples and limited to ±limit.

Parameters

  • Proportional gainkp1

    Proportional gain.

  • Integral gainki0.5

    Integral gain, per second.

  • Derivative gainkd0.05

    Derivative gain, in seconds.

  • Derivative filterfilterTime0.01 s

    ≥ 0.0001

    Derivative filter time constant Tf, in seconds, at least 0.1 ms.

  • Output limitlimit10

    ≥ 0.001

    Output limit, at least 0.001. Clamps both the output and the integral to ±limit.

  • Sample periodsamplePeriod0.001 s

    ≥ 0.0001

    Sample period Ts, in seconds, at least 0.1 ms. The first sample is at t = 0.

Equations

Modelica
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

The block’s Modelica model
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.