Lesson 22: PID Controllers

PID control is a way to make the robot move toward a target using sensor feedback. Instead of guessing a motor speed and hoping the mechanism lands in the right place, PID compares the current value to the goal and adjusts the output.

The Error

The most important number in any controller is the error.

error = target - current

If an elevator target is 50 inches and the encoder says it is at 40 inches, the error is 10 inches. If it is at 52 inches, the error is -2 inches.

P, I, and D

Many FRC mechanisms can start with only P. Add I and D only when you understand what problem you are trying to solve.

double target = 90.0;
double current = armPot.get();
double error = target - current;
double output = error * 0.03;

armMotor.set(output);

Clamp the Output

A controller can ask for too much power. Limit the output so the mechanism stays safe.

if (output > 0.5) {
    output = 0.5;
} else if (output < -0.5) {
    output = -0.5;
}

Tuning

Start with all constants at zero. Increase P slowly until the mechanism moves toward the target. If it oscillates, P is probably too high. Add small changes and test carefully.

Practice

Pick a sensor value and a target. Calculate the error by hand for three different current readings. Then decide which direction the motor should move for each error.