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#
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#
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.
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#
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#
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.
the part worth keeping
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.
Programming, Controls & SensorsMotors, Sensors, and Closed-Loop Controllesson 3 of 4
Keep going
Take the quiz+10 XP with an accountMore in Motors, Sensors, and Closed-Loop Control
where this came from
Sources and corrections
This lesson is AI-assisted: drafted from primary sources, then reviewed and edited by hand. Errors still get through. When one is reported we fix it and write down what changed, in public, in the corrections log.
sources and further reading
- docs.wpilib.orgPID Control in WPILib
- docs.wpilib.orgPID Control in Command-Based
clipped to this lesson
Articles that go further on this
The lesson gets you through the topic. These go wider on it, and they read in one sitting.
- 4 min readPID Control in FRC, Explained SimplyA beginner-friendly guide to PID control in FRC: what kP, kI, and kD actually do, how to tune them, and why most teams skip the I term./blogread it
- 8 min readHow to Tune PID on an FRC Robot: A Practical GuideA hands-on guide to tuning PID and feedforward on FRC mechanisms: a safe tuning order, fixing oscillation and steady-state error, and using WPILib SysId./blogread it
- 7 min readFRC "Loop Time of 0.02s Overrun" & Watchdog Not Fed: Causes and FixesWhat "Loop time of 0.02s overrun" and watchdog-not-fed warnings actually mean in FRC, how to read WPILib's epoch dump, and the usual causes./blogread it
answer sheet
Lesson quiz
All 3 right completes the lesson. Miss one and only that question comes back, anything you already answered correctly stays banked.
0 of 3 answered
01In a PID controller, what does the proportional (P) term do?
02Why is the integral (I) term used in a PID controller?
03Fundamentally, what kind of control is PID?
Answer every question to submit.
All 51 lessons in Programming, Controls & Sensorsopenclose
01 / prerequisites
02 / foundations-tools-and-first-program
03 / robot-program-and-command-based
04 / motors-and-control
05 / autonomous-trajectories-simulation
06 / sensing-fundamentals
07 / encoders
08 / gyros-imus-orientation
09 / closed-loop-control
10 / vision-pose-estimation
11 / worked-examples-mini-projects
- Not read yet:Mini-Project: A Closed-Loop Elevator with Motion Magic
- Not read yet:Mini-Project: A Velocity-Controlled Shooter on REVLib
- Not read yet:Mini-Project: A Teleop Swerve Drive Subsystem
- Not read yet:Mini-Project: An Autonomous Routine with PathPlanner
- Not read yet:Mini-Project: Vision-Aligned Scoring with Limelight
12 / common-mistakes-troubleshooting
13 / advanced-techniques-case-studies
- Not read yet:State-Space Control and Kalman Filtering
- Not read yet:Log Replay Architecture with AdvantageKit
- Not read yet:Advanced Pose Estimation: Multi-Tag Fusion and Standard Deviations
- Not read yet:Robot Coordination, Alerts, and Operator Feedback
- Not read yet:Case Study: Hardening Software Before an Event