In open-loop control you command a motor a fixed output and hope it does the right thing. In closed-loop (feedback) control you measure the result with a sensor and continuously correct. PID is the most common feedback algorithm in FRC.
The PID idea#
A PID controller computes an error = setpoint - measurement, then outputs a correction built from three terms:
- Proportional (kP): output proportional to current error. Bigger error, harder push. Too little kP is sluggish; too much causes overshoot and oscillation.
- Integral (kI): accumulates error over time to eliminate small, persistent steady-state error. Powerful but dangerous — it can 'wind up' and cause big overshoot. Many FRC mechanisms use little or no kI, preferring feedforward instead.
- Derivative (kD): responds to how fast the error is changing, damping oscillation and overshoot like a shock absorber.
Output = kPerror + kI(integral of error) + kD*(rate of change of error).
WPILib PIDController#
PIDController pid = new PIDController(kP, kI, kD);
pid.setTolerance(0.5); // 'close enough' band
double out = pid.calculate(encoder.getDistance(), setpoint);
motor.setVoltage(out);
if (pid.atSetpoint()) { /* done */ }
Call calculate() every loop (the default TimedRobot loop is 20 ms, i.e. 50 Hz). Key features:
enableContinuousInput(-180, 180)for angular mechanisms (turrets, swerve steering) so the controller takes the shortest path around the circle instead of unwinding the long way.setTolerance()+atSetpoint()to know when you have arrived.setIZone()/setIntegratorRange()to tame integral windup.
A note on where PID runs#
You can run PID in robot code (PIDController) or on the motor controller (Talon FX and Spark MAX both have onboard closed-loop control). On-controller PID runs at a much higher rate than your 50 Hz robot loop (on the order of 1 kHz), which is excellent for velocity and position control. The tuning concepts are the same either way.
the part worth keeping
Key takeaways
- Closed-loop control measures the result and corrects continuously; PID is the standard algorithm.
- kP drives toward the setpoint, kD damps oscillation, and kI removes steady-state error but can wind up.
- Use enableContinuousInput for angles, atSetpoint to detect arrival, and consider on-controller PID for high-rate loops.
Programming, Controls & SensorsClosed-Loop Control: PID, Feedforward, and SysIdlesson 1 of 4
Keep going
Take the quiz+10 XP with an accountMore in Closed-Loop Control: PID, Feedforward, and SysId
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.orgWPILib: PID Control in WPILib
- docs.wpilib.orgWPILib: Introduction to PID
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
01What is the fundamental difference between open-loop and closed-loop control?
02In a PID controller, how is the error defined?
03Which statement correctly describes what each PID term responds to?
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