Skip to main content

PID Class

General-use PID class for drivetrains. It includes both control calculation and settling calculation. The default update period is 10ms or 100Hz.

Constructors​


PID constructor with P, I, D, and starti. Starti keeps the I term at 0 until error is less than starti.

Parameters
errorDifference in desired and current position.
kpProportional constant.
kiIntegral constant.
kdDerivative constant.
startiMaximum error to start integrating.
PID::PID(float error, float kp, float ki, float kd, float starti);

PID constructor with settling inputs. The settling system works like this: The robot is settled when error is less than settle_error for a duration of settle_time, or if the function has gone on for longer than timeout. Otherwise it is not settled. Starti keeps the I term at 0 until error is less than starti.

Parameters
errorDifference in desired and current position.
kpProportional constant.
kiIntegral constant.
kdDerivative constant.
startiMaximum error to start integrating.
settle_errorMaximum error to be considered settled.
settle_timeMinimum time to be considered settled.
timeoutTime after which to give up and move on.
PID::PID(float error, float kp, float ki, float kd, float starti, float settle_error, float settle_time, float timeout);

PID constructor with custom update period. The default update period is 10ms, but if you want to run a faster or slower loop, you need to let the settler know.

Parameters
errorDifference in desired and current position.
kpProportional constant.
kiIntegral constant.
kdDerivative constant.
startiMaximum error to start integrating.
settle_errorMaximum error to be considered settled.
settle_timeMinimum time to be considered settled.
timeoutTime after which to give up and move on.
update_periodLoop delay time in ms.
PID::PID(float error, float kp, float ki, float kd, float starti, float settle_error, float settle_time, float timeout, float update_period);

compute​

Computes the output power based on the error. Typical PID calculation with some optimizations: When the robot crosses error=0, the i-term gets reset to 0. And, of course, the robot only accumulates i-term when error is less than starti. Read about these at https://georgegillard.com/resources/documents.

Parameters
errorDifference in desired and current position.

Returns the controller's output power.

float PID::compute(float error);

is_settled​

Computes whether or not the movement has settled. The robot is considered settled when error is less than settle_error for a duration of settle_time, or if the function has gone on for longer than timeout. Otherwise it is not settled.

Returns whether the movement is settled.

bool PID::is_settled();