swarmsim.util.pid#

PID Controller class

Example

from pid import PID
controller = PID(p=0.07, i=0, imax=90)
target = 100
state = 0
while(True):
    error = target - state
    state = controller(error)
    # control value with output

See also

This class was adapted from the following micropython code: openmv/openmv

class swarmsim.util.pid.PID(p: float = 0, i: float = 0, d: float = 0, imax: int = 0, min: float | None = None, max: float | None = None)[source]#

PID Class

Parameters:
  • p (float, optional) – proportional gain, by default 0

  • i (float, optional) – integrator gain, by default 0

  • d (float, optional) – derivative gain, by default 0

  • imax (int, optional) – integrator max value, by default 0 The integrator will be clamped to within [-imax, imax]

Methods

__call__(error[, time, dt])

Call self as a function.

get_pid(error[, time, dt])

Calculate the PID output

reset_I

d_smoothing = 0.007957747154594767[source]#
get_pid(error, time=None, dt=None)[source]#

Calculate the PID output

Parameters:
  • error (float) – The error value

  • time (float, optional) – The current time. If you specify this, don’t specify dt.

  • dt (float, optional) – Elapsed time since the last call. If you specify this, don’t specify time.

Returns:

The PID output

Return type:

float

i_decay = 0.007957747154594767[source]#
reset_I()[source]#
reset_time = 100[source]#

Functions

clamp(x, xmin, xmax)

Classes

LivePID([p, i, d, imax, min, max])

PID([p, i, d, imax, min, max])

PID Class