Open Loop vs Closed Loop, and the PID Controller
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
Section titled “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 = kP error + kI(integral of error) + kD*(rate of change of error).
WPILib PIDController
Section titled “WPILib PIDController”PIDController pid = new PIDController(kP, kI, kD);pid.setTolerance(0.5); // 'close enough' banddouble 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
Section titled “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.
Key takeaways
Section titled “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.
This lesson was adapted from learnfrc.com.
