Skip to content

PID Feedback Control

learnfrc.com
learnfrc.comAuthor
Veer Bajaj
Veer BajajMaintainer

Open-loop control (“set the motor to 50%”) can’t hit a precise target — battery voltage, friction, and load all vary. Closed-loop control measures the result with a sensor and corrects continuously. The workhorse is the PID controller.

PID computes a motor output from the error (setpoint minus measurement):

  • P (proportional) — output proportional to current error. Bigger error → bigger correction. Too much P causes oscillation.
  • I (integral) — accumulates error over time to eliminate small steady-state offsets. Often left at 0; too much causes overshoot/instability.
  • D (derivative) — responds to how fast the error is changing, damping oscillation and overshoot.

Output = kP*error + kI*∫error + kD*(d error/dt).

WPILib provides a PIDController class you can run on the roboRIO:

PIDController pid = new PIDController(0.1, 0.0, 0.0); // kP, kI, kD
pid.setTolerance(2.0); // "close enough" window
@Override
public void execute() {
double output = pid.calculate(encoder.getDistance(), setpoint);
motor.set(output);
}
public boolean isFinished() { return pid.atSetpoint(); }

For rotational mechanisms that wrap (like a turret or swerve azimuth), call enableContinuousInput(-Math.PI, Math.PI) (or -180, 180 if you work in degrees) so the controller takes the short way around.

You can run PID on the roboRIO (the WPILib PIDController, simple and flexible) or on the motor controller (Phoenix 6 Slot0Configs / REVLib closedLoop, which run faster and offload the roboRIO). Beginners often start on the roboRIO; advanced teams push tight loops onto the controller.

A reliable manual procedure:

  1. Set kI and kD to 0.
  2. Raise kP until the mechanism reaches the target quickly but oscillates a little.
  3. Add kD to damp the oscillation.
  4. Add a small kI only if a stubborn steady-state error remains.

Keep changes small and test after each one. WPILib’s SysId tool can characterize a mechanism and suggest gains, which beats pure guesswork.

Pure PID is reactive — it only acts once there’s error, so it can lag and struggle with gravity or momentum. For mechanisms like arms and flywheels, PID alone is rarely enough. That’s why the next lesson adds feedforward , which predicts the output a mechanism needs before error appears.

  • PID closes the loop: it corrects motor output based on measured error.
  • P reacts to error size, I removes steady-state offset, D damps oscillation.
  • Use WPILib’s PIDController on the roboRIO, or on-controller PID for speed.
  • Enable continuous input for wrapping mechanisms like turrets/swerve azimuth.
  • Tune kP first, then kD, then a little kI; SysId can characterize gains for you.

This lesson was adapted from learnfrc.com.