PID Control
PID (Proportional-Integral-Derivative) control replaces imprecise voltage commands with accurate, feedback-driven position control. Toggle between an arm (position) and a flywheel (velocity) below — the gains in each map directly to a TalonFX Slot0Configs.
Key concept: PID uses sensor feedback to automatically adjust motor output; feedforward predicts the voltage needed before any error has accumulated.
Each loop the arm starts at 0° (horizontal). For the first second the setpoint stays at 0° — use that window to tune kGuntil the arm holds. For a Kraken X60 + 25:1 driving a 2 kg · 0.4 m arm, the spec-sheet math gives kG = mgL / (Kₜ·R) ≈ 0.53 V. Add kS to overcome residual static friction. At t = 1 s the setpoint steps to your slider target — kP and kD chase the arm there and damp the overshoot. Order: kG → kS → kP → kD.
Feedback · PID
Feedforward
2 kg · 0.4 m arm on a Kraken X60 + 25:1 reduction (7.09 N·m / 6000 RPM motor, ≈ 177 N·m / 240 RPM at the arm; back-EMF modelled, ±12 V saturation). Gains use Phoenix 6 / WPILib mechanism-side units — drop these values straight into a Slot0Configs with SensorToMechanismRatio = 25.
Understanding PID Components
P - Proportional
Definition:"The amount of output to apply per unit of error in the system"
Error = Target - CurrentP_Output = kP × ErrorBehavior: Larger error = stronger correction. Provides immediate response but may cause oscillation.
I - Integral
Definition:"The amount of output to apply per unit of error for every second of that error"
Accumulated_Error += Error × dtI_Output = kI × Accumulated_ErrorBehavior: Eliminates steady-state error by accumulating past errors over time.
Note:The integral term can lead to "windup," which may make your mechanism unstable. In most FRC applications, you can leave the integral term at zero.
D - Derivative
Definition:"The amount of output to apply per change in error over time"
Error_Rate = (Error - Last_Error) / dtD_Output = kD × Error_RateBehavior: Reduces overshoot by predicting future error trends and dampening response.
⚡ Feedforward Gains
Feedforward gains help the system by predicting the required output based on the target, rather than reacting to error.
kS - Static
Always
kG - Gravity
Arms/Elevators
kV - Velocity
Flywheels/Intakes
kA - Acceleration
High Inertia Mechanisms
📚 Complete PID Tuning Guide
For detailed PID tuning instructions, step-by-step processes, and mechanism-specific guidance:
CTRE Manual PID Tuning Guide📹 PID and Feedforward Tuning Tutorial
Watch this comprehensive tutorial on PID and feedforward tuning techniques, practical tuning steps, and optimization strategies:
PID Implementation in Code
🔧 PID Configuration Example
Workshop Implementation: PID Control
Before & After: Implementation
Before
- • Commands control Arm with voltage
- • No position feedback control
- • Imprecise, inconsistent movement
- • No automatic target reaching
- • Manual voltage adjustment needed
After
- • PID position control with PositionVoltage
- • Automatic target position reaching
- • Precise, repeatable movements
- • Feedforward compensation for gravity
- • Tolerance checking for "at target"
Loading file...
Code Walkthrough
PID Implementation:
- • PositionVoltage: Replaces VoltageOut for closed-loop control
- • Slot0 Config: PID and feedforward gains configuration
- • Target Setting: setTargetPosition() method for precise control
Gain Values Used:
- • kP = 24.0: Strong proportional response
- • kD = 0.1: Small derivative for damping
- • kS = 0.25: Static friction compensation
- • kG = 0.12: Gravity feedforward for Arm
PID gives us precise position control! In the next section, we'll upgrade to Motion Magic for smooth, profiled movements with controlled acceleration.