PIDController

class PIDController(Kp: float | int, Ki: float | int, Kd: float | int, clamping: bool = False, reference_min: float | int | None = None, reference_max: float | int | None = None)

Bases: object

Added in version 1.3.0.

PIDController object.

Proportional-integral-derivative controller which computes the reference value, used to keep a variable under control with respect to a set value, based on current value and control parameters.

Methods

compute()

It computes the reference value based on current error.

reset()

It resets the stored cumulative error integral, used for integrative part, and the previous error, used for derivative part.

Raises

TypeError
ValueError

If reference_min is lower than reference_max.

compute(error: float | int, time_step: TimeInterval) float | int

It computes the reference value based on current error.

Parameters

errorfloat or int

Current error.

time_stepTime

Time interval to be used for integrative and derivative parts.

Returns

float or int

Reference value to be used for control.

Raises

TypeError
  • If error is not a float or an int,

  • if time_step is not an instance of Time.

Notes

The reference value is computed as:

\[u(t) = K_P e(t) + K_I \int_0^t e(\tau) d \tau + K_D \frac{d e(t)}{d t}\]

where:

  • \(u(t)\) is the reference value,

  • \(e(t)\) is the current error,

  • \(K_P\) is the proportional constant Kp,

  • \(K_I\) is the integral constant Ki,

  • \(K_D\) is the derivative constant Kd.

The integral part is approximated with:

\[\int_0^t e(\tau) d \tau \approx \frac{1}{2}(e_i + e_{i - 1}) dt\]

and the derivative part is approximated with:

\[\frac{d e(t)}{d t} \approx \frac{e_i - e_{i - 1}}{dt}\]

where:

  • \(e_i\) is the current error,

  • \(e_{i - 1}\) is the error at the previous time step,

  • \(dt\) is the time_step.

If the reference value \(u(t)\) exceeds the limits reference_min or reference_max, then it is saturated to these values. In this case, if clamping is True, then the cumulative error is not updated (anti-windup).

reset() None

It resets the stored cumulative error integral, used for integrative part, and the previous error, used for derivative part.