PID Feedback Control
The problem PID solves
Section titled “The problem PID solves”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.
The three terms
Section titled “The three terms”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’s PIDController
Section titled “WPILib’s PIDController”WPILib provides a PIDController class you can run on the roboRIO:
PIDController pid = new PIDController(0.1, 0.0, 0.0); // kP, kI, kDpid.setTolerance(2.0); // "close enough" window
@Overridepublic 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.
Where to run the loop
Section titled “Where to run the loop”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.
Tuning, practically
Section titled “Tuning, practically”A reliable manual procedure:
- Set kI and kD to 0.
- Raise kP until the mechanism reaches the target quickly but oscillates a little.
- Add kD to damp the oscillation.
- 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.
A key limitation
Section titled “A key limitation”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.
Key takeaways
Section titled “Key takeaways”- 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.
