copapy.controller.pid#

copapy.controller.pid(setpoint, measurement, kp, ki=0.0, kd=0.0, *, tf=0.0, b=1.0, c=0.0, out_min=None, out_max=None, tt=None, feedforward=0.0, enable=True, default_out=0.0, dt=None)#

PID controller in parallel form with setpoint weighting, derivative filter, output limits with anti-windup and bumpless enabling:

u = sat(kp * (b * r - y) + I + D + feedforward) I = integral of ki * (r - y), starting at default_out D = kd * d/dt (c * r - y), low-pass filtered by tf

with r the setpoint and y the measurement. The integral is accumulated by the forward Euler method (I[k+1] = I[k] + dt * ki * e[k]), so the output depends on the current error by the proportional and derivative terms only. As ki is inside the integral, a change of ki does not change the output (bumpless tuning). The derivative is 0 at the first run of the task, there is no kick from the initial measurement.

With out_min or out_max the integral is protected from winding up by back-calculation: while the output is limited, the integral tracks the limited output with the time constant tt.

While enable is false, the output is default_out (limited) and the integral tracks it: after enabling the controller continues from default_out without a jump (for a controller with integral).

>>> u = cp.controller.pid(setpoint, position, kp=2.0, ki=5.0, kd=0.1,
...                       tf=0.01, out_min=-1.0, out_max=1.0)
Parameters:
  • setpoint (value[float] | float) – Reference value r

  • measurement (value[float] | float) – Measured value y of the controlled process

  • kp (value[float] | float) – Proportional gain

  • ki (value[float] | float) – Integral gain (kp / Ti), 0 for a controller without integral

  • kd (value[float] | float) – Derivative gain (kp * Td), 0 for a controller without derivative

  • tf (value[float] | float) – Time constant of the first order low-pass filter of the derivative in seconds, 0 for no filter. A typical value is Td / N with N between 5 and 20. The filter is stable for any time step.

  • b (value[float] | float) – Setpoint weight of the proportional term; values below 1 reduce the overshoot after setpoint steps

  • c (value[float] | float) – Setpoint weight of the derivative term; 0 (default) is the derivative of the measurement only (no kick by setpoint steps), 1 the derivative of the error

  • out_min (value[float] | float | None) – Lower limit of the output, None for no limit

  • out_max (value[float] | float | None) – Upper limit of the output, None for no limit

  • tt (value[float] | float | None) – Tracking time constant of the anti-windup in seconds, by default Ti = kp / ki. Smaller values reset the integral faster.

  • feedforward (value[float] | float) – Value added to the output before the limitation

  • enable (value[Any] | bool) – Controller active (value of type bool or number); while it is false the output is default_out

  • default_out (value[float] | float) – Output while the controller is disabled and initial value of the integral: at the first run the output for zero error. For a controller without integral (ki = 0) it is a constant offset of the output.

  • dt (value[float] | float | None) – Time step, by default the period of the task

Return type:

value[float]

Returns:

Control output u (limited to out_min and out_max)