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 | |
---|---|
error | Difference in desired and current position. |
kp | Proportional constant. |
ki | Integral constant. |
kd | Derivative constant. |
starti | Maximum error to start integrating. |
- Prototype
- Example
PID drivePID(16.67, 3, .01, 5, 15);
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 | |
---|---|
error | Difference in desired and current position. |
kp | Proportional constant. |
ki | Integral constant. |
kd | Derivative constant. |
starti | Maximum error to start integrating. |
settle_error | Maximum error to be considered settled. |
settle_time | Minimum time to be considered settled. |
timeout | Time after which to give up and move on. |
- Prototype
- Example
PID drivePID(16.67, 3, .01, 5, 15, 3, 200, 4000);
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 | |
---|---|
error | Difference in desired and current position. |
kp | Proportional constant. |
ki | Integral constant. |
kd | Derivative constant. |
starti | Maximum error to start integrating. |
settle_error | Maximum error to be considered settled. |
settle_time | Minimum time to be considered settled. |
timeout | Time after which to give up and move on. |
update_period | Loop delay time in ms. |
- Prototype
- Example
PID drivePID(16.67, 3, .01, 5, 15, 3, 200, 4000, 5);
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 | |
---|---|
error | Difference in desired and current position. |
Returns the controller's output power.
- Prototype
- Example
drivePID.compute(3.555);
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.
- Prototype
- Example
if( drivePID.is_settled() ){
break;
}
bool PID::is_settled();