Introduction
PID control is the gold standard for maintaining a steady state in dynamic systems (like keeping a drone level or a robot following a line smoothly).
The Components
- Proportional (P): Corrects based on current error.
- Integral (I): Corrects based on past errors (accumulated).
- Derivative (D): Predicts future error based on current rate of change.
Implementation Snippet
float Kp = 1.0, Ki = 0.1, Kd = 0.01;
float error, lastError, integral;
float calculatePID(float setpoint, float current) {
error = setpoint - current;
integral += error;
float derivative = error - lastError;
lastError = error;
return (Kp * error) + (Ki * integral) + (Kd * derivative);
}