PID stands for Proportional Integral Derivative control. As the name suggests, this is a modular type of control that consists of three components:
-
Proportional component "P" - Also called "Error." This is the amount the robot has deviated from the line. We calculate it by measuring how much the sensor’s reflected light differs from the "Target Value" the robot is trying to follow.
P = Error
Error = Target Reflected Light Value - Measured Reflected Light Value
-
Integral component "I" - This is the sum of all Error values. Since Error can be positive or negative, this sum naturally trends toward zero. It adjusts the sharpness of the robot's turns based on how far it deviates from zero. Usually, this component is negligible for smaller robots, so many choose to ignore it entirely.
I = I + Error
-
Derivative component "D" - Also called "Differential." This value helps prevent overcorrection. It’s calculated by measuring the difference in Error (the Proportional component) between the current and previous measurements.
D = Error - Previous Error
These three components are added together to produce our PID value. In formula form, it looks like this:
PIDvalue = P + I + D
However, the raw values are rarely ideal for every robot, field, sensor, and wheel size. Each component needs to be proportionally adjusted to suit the specific robot, achieved by simple multiplication:
PIDvalue = P*Kp + I*Ki + D*Kd
Kp, Ki, and Kd are values chosen to optimize performance. Several methods exist to determine these values; here, we’ll use the simplest, a five-step testing method:
- Set Kp, Kd, and Ki to zero.
- Experiment with different Kp values until you find the best result for your robot.
- Keep Kp at the optimal value from step 2, and test to find the best Kd value.
- Keep Kd at the optimal value from step 3, and test to find the best Ki value.
- With the best Ki value from step 4, repeat steps 2–4 until you’re satisfied with the robot’s line-following performance.
Ultimately, we obtain a PID value that we can use to adjust the motors’ speeds from a preset base speed.