Skip to content
Programming, Controls & Sensors·Lesson 15 of 51

PID Feedback Control

Use PID controllers to drive a mechanism to a setpoint, and understand what each gain does.

Sign in to track progress, earn XP, and save lessons.

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:

  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.

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

  • 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.

Lesson quiz

Required

Answer all 3 questions correctly to complete this lesson.

1.In a PID controller, what does the proportional (P) term do?

2.Why is the integral (I) term used in a PID controller?

3.Fundamentally, what kind of control is PID?

Answer every question to submit.