PID module

Houses PID control class used only in constant period loops.

class PID.PID(Kp=1.0, Ki=0.0, Kd=0.0, setpoint=0, sample_time=None, scale='s', output_limits=[None, None], auto_mode=True, proportional_on_measurement=False, error_map=None)

Bases: object

A simple PID controller.

Parameters:
  • Kp – The value for the proportional gain Kp

  • Ki – The value for the integral gain Ki

  • Kd – The value for the derivative gain Kd

  • setpoint – The initial setpoint that the PID will try to achieve

  • sample_time – The interval in the setted scale which the controller should wait before generating a new output value. The PID works best when it is constantly called (eg. during a loop), but with a sample time set so that the time difference between each update is (close to) constant. If set to None, the PID will compute a new output value every time it is called, keep None to use param dt for timestep.

  • scale – Set the scale of the controller, accepted values are ‘s’ for seconds, ‘ms’ for miliseconds, ‘us’ for microseconds, ‘ns’ for nanoscondas and ‘cpu’ for the highest precision at cpu clock. On default and on error set to seconds.

  • output_limits – The initial output limits to use, given as an iterable with 2 elements, for example: (lower, upper). The output will never go below the lower limit or above the upper limit. Either of the limits can also be set to None to have no limit in that direction. Setting output limits also avoids integral windup, since the integral term will never be allowed to grow outside of the limits.

  • auto_mode – Whether the controller should be enabled (auto mode) or not (manual mode)

  • proportional_on_measurement – Whether the proportional term should be calculated on the input directly rather than on the error (which is the traditional way). Using proportional-on-measurement avoids overshoot for some types of systems.

  • error_map – Function to transform the error value in another constrained value.

property auto_mode

Whether the controller is currently enabled (in auto mode) or not.

property components

The P-, I- and D-terms from the last computation as separate components as a tuple. Useful for visualizing what the controller is doing or when tuning hard-to-tune systems.

property output_limits

(lower, upper).

See also the output_limits parameter in PID.__init__().

Type:

The current output limits as a 2-tuple

reset()

Reset the PID controller internals.

This sets each term to 0 as well as clearing the integral, the last output and the last input (derivative calculation).

set_auto_mode(enabled, last_output=None)

Enable or disable the PID controller, optionally setting the last output value.

This is useful if some system has been manually controlled and if the PID should take over. In that case, disable the PID by setting auto mode to False and later when the PID should be turned back on, pass the last output variable (the control variable) and it will be set as the starting I-term when the PID is set to auto mode.

Parameters:
  • enabled – Whether auto mode should be enabled, True or False

  • last_output – The last output, or the control variable, that the PID should start from when going from manual mode to auto mode. Has no effect if the PID is already in auto mode.

property tunings

(Kp, Ki, Kd).

Type:

The tunings used by the controller as a tuple