Skip to main content

PID

Generic PID class

Constructors

PID();
PID(float kp, float ki, float kd);
PID(float kp, float ki, float kd, float starti);
PID(float kp, float ki, float kd, float starti,
float settle_error, float settle_time, float exit_error,
float timeout, float stall_timeout = 0);

starti keeps the integral term at 0 until error drops below it, which prevents integral windup on large errors.

The settling constructor adds exit conditions. The controller is settled when error stays below settle_error for settle_time, or once the motion has run longer than timeout.

ParameterDescription
kpProportional constant.
kiIntegral constant.
kdDerivative constant.
startiMaximum error at which the integral term starts accumulating.
settle_errorMaximum error to be considered settled.
settle_timeMinimum time (ms) under settle_error to be considered settled.
exit_errorError at which the motion is considered finished.
timeoutTime (ms) after which to give up and move on. 0 disables it.
stall_timeoutTime (ms) to give up when the robot is near 0 velocity. 0 disables it.

Methods

compute

float compute(float error);

Computes the output power from the current error. Includes two optimizations: the integral term resets to 0 whenever the robot crosses error == 0, and integral only accumulates while error is below starti.

  • Returns — the output power.

is_settled

bool is_settled();

Returns whether the movement has settled, per the settling rules described above.

  • Returnstrue if the movement is settled.

Public members

These are readable at any time (e.g. for graphing or debugging):

MemberDescription
errorCurrent error.
kp, ki, kdActive PID gains.
startiIntegral gating threshold.
settle_error, settle_timeActive settle conditions.
exit_errorActive exit error.
timeout, stall_timeoutActive timeouts.
accumulated_errorRunning integral sum.
previous_errorError from the previous compute() call.
outputLast computed output.
time_spent_settledTime (ms) the controller has been within settle_error.
time_spent_runningTotal time (ms) the controller has been running.
time_spent_stalledTime (ms) the controller has been near 0 velocity.
exitingtrue once an exit condition has been met.