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

Open Loop vs Closed Loop, and the PID Controller

What feedback control is and how the three PID terms (kP, kI, kD) shape a mechanism's response.

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

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.

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.

Lesson quiz

Required

Answer all 3 questions correctly to complete this lesson.

1.What is the fundamental difference between open-loop and closed-loop control?

2.In a PID controller, how is the error e(t) defined?

3.Which statement correctly describes what each PID term responds to?

Answer every question to submit.