PID Control

PID Control - Precise Position Control

PID (Proportional-Integral-Derivative) control replaces imprecise voltage commands with accurate, feedback-driven position control. Essential for mechanisms that need to hit specific targets.

🎯 Key Concept: PID uses sensor feedback to automatically adjust motor output to reach and maintain target positions.

Understanding PID Components

P - Proportional

Definition: "The amount of output to apply per unit of error in the system"

Error = Target - Current
P_Output = kP × Error

Behavior: 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 × dt
I_Output = kI × Accumulated_Error

Behavior: Eliminates steady-state error by accumulating past errors over time.

D - Derivative

Definition: "The amount of output to apply per change in error over time"

Error_Rate = (Error - Last_Error) / dt
D_Output = kD × Error_Rate

Behavior: 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

Constant output to overcome friction and get the mechanism moving.

kG - Gravity

Compensates for gravitational forces acting on the mechanism.

kV - Velocity

Output applied per target velocity to maintain smooth motion.

kA - Acceleration

Output applied per target acceleration for responsive movement.

📚 Complete PID Tuning Guide

For detailed PID tuning instructions, step-by-step processes, and mechanism-specific guidance:

📖 CTRE Manual PID Tuning Guide

PID Implementation in Code

🔧 PID Configuration Example
PID Setup in Subsystem ConstructorJAVA
1// In your subsystem constructor
2public ArmSubsystem() {
3    TalonFXConfiguration config = new TalonFXConfiguration();
4    
5    // PID Configuration for Slot 0
6    Slot0Configs slot0 = config.Slot0;
7    slot0.kP = 24.0;    // Proportional gain
8    slot0.kI = 0.0;     // Integral gain  
9    slot0.kD = 0.1;     // Derivative gain
10    
11    // Feedforward gains
12    slot0.kS = 0.25;    // Static friction compensation
13    slot0.kG = 0.12;    // Gravity compensation
14    slot0.kV = 0.12;    // Velocity feedforward
15    slot0.kA = 0.01;    // Acceleration feedforward
16    
17    motor.getConfigurator().apply(config);
18    
19    // Create PID control request
20    positionRequest = new PositionVoltage(0).withSlot(0);
21}
22
23// Method to set target position
24public void setTargetPosition(double positionRotations) {
25    motor.setControl(positionRequest.withPosition(positionRotations));
26}
27
28

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

💡 Next Step: PID gives us precise position control! In the next section, we'll upgrade to Motion Magic for smooth, profiled movements with controlled acceleration.