PID Control

intermediate55 min

Overview

PID (proportional–integral–derivative) is a feedback controller. It compares where the mechanism is to where you want it (the setpoint) and adjusts motor output to close the gap. Elevators use position setpoints; shooters often use velocity setpoints.

The Terms

P (proportional): Output scales with current error. Large error → stronger correction. P does most of the work during a move.

I (integral): Output scales with error summed over time. Use I when the mechanism stops short of the setpoint and stays there (steady-state error). Gravity on an elevator is often handled with feedforward (kG) instead of I.

D (derivative): Output scales with how fast error is changing. D opposes rapid corrections and can reduce overshoot and oscillation.

Implementation Methods

Where to run PID:

  • On-RIO (WPILib PIDController): Runs in your robot code (typically a 20 ms loop). Easy to log and change in software.
  • On-controller (SparkMax / Talon FX): Runs on the motor controller (faster loop). Less RIO CPU use; tune with Phoenix Tuner or REV Hardware Client.

WPILib PIDController

Use this when you want PID in Java (custom logic, multiple sensors, or learning how the math works).

RIO PID Example

package frc.robot.subsystems;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.math.controller.PIDController;

public class Elevator extends SubsystemBase {
    // Create PID controller: kP, kI, kD
    private PIDController pid = new PIDController(0.5, 0.0, 0.0);
    
    // Example: encoder and motor would be defined elsewhere
    // private Encoder encoder;
    // private Motor motor;
    private double targetHeight = 0.0;
    
    // ... (other methods and fields)
    
    @Override
    public void periodic() {
        // Calculate PID output: current position vs target
        double currentPosition = encoder.getPosition();
        double output = pid.calculate(currentPosition, targetHeight);
        
        // Apply output to motor
        motor.set(output);
    }
    
    // ... (rest of class)

On-Controller PID

Use on-controller PID when the mechanism needs a fast loop and your gains are stable.

Hardware PID

package frc.robot.subsystems;

import com.ctre.phoenix6.configs.Slot0Configs;
import com.ctre.phoenix6.controls.PositionVoltage;
import com.ctre.phoenix6.hardware.TalonFX;

public class Example {
    private TalonFX motor = new TalonFX(1);
    
    public void configurePID() {
        // Configure PID gains for Slot 0
        Slot0Configs config = new Slot0Configs();
        config.kP = 0.1;  // Proportional gain
        config.kI = 0.0;  // Integral gain
        config.kD = 0.01; // Derivative gain
        motor.getConfigurator().apply(config);
    }
    
    public void setPosition(double targetRotations) {
        // Use position control with configured PID
        motor.setControl(new PositionVoltage(targetRotations));
    }
}
package frc.robot.subsystems;

import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.revrobotics.spark.config.SparkMaxConfig;
import com.revrobotics.spark.SparkBase.ResetMode;
import com.revrobotics.spark.SparkBase.PersistMode;
import com.revrobotics.spark.ClosedLoopController.ControlType;

public class Example {
    private SparkMax motor = new SparkMax(1, MotorType.kBrushless);
    private SparkClosedLoopController controller = motor.getClosedLoopController();
    
    public void configurePID() {
        // Configure PID gains
        SparkMaxConfig config = new SparkMaxConfig();
        config.closedLoop.p(0.1).i(0.0).d(0.01);
        motor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
    }
    
    public void setPosition(double target) {
        // Use position control with configured PID
        controller.setReference(target, ControlType.kPosition);
    }
}

Tuning Process

Tune feedforward first, then PID. Change gains from Phoenix Tuner, REV Hardware Client, Elastic, or AdvantageScope while the mechanism runs. Graph commanded vs measured position or velocity; do not tune by eye alone. The kG-then-kP workflow below matches SuperNURDS FRC 3255 and the PID Tuning Practice lessons.

Feedforward Tuning

Steps:

  • Set all PID constants to 0.
  • Command the mechanism to a non-zero position.
  • Manually raise to that position and release.
  • Observe if it holds, raises, or falls.
  • Adjust kG (gravity feedforward) and repeat until it holds position.

PID Tuning

Steps:

  • Log desired position and actual position.
  • Slowly increase kP until you see overshoot, then set kP slightly below that value.
  • (For a riskier strategy, double kP until you see overshoot, then set kP to the midpoint between the old and current kP values until oscillation mostly disappears)
  • If steady-state error appears (doesn’t reach target), increase kS (static feedforward) slightly.
  • This makes kP temporarily too large (overshoot), but your final kP will be close.
  • Repeat adjusting kS and kP until properly tuned.

Resources