Tutorials
Zero to Hero
Episode 6: PID Control

🎯 What is PID?

PID is a feedback loop. It looks at Error (Where I want to be - Where I am) and calculates motor power.


πŸ§ͺ Chapter 1: The Tuning Process

How do you find the magic numbers?

  1. Set kF (Feedforward) first: If it's a flywheel, find the voltage needed to hold the speed. If it's an arm, calculate holding voltage against gravity.
  2. Increase kP: Until it oscillates (wiggles back and forth).
  3. Add kD: To stop the wiggle (dampen it).
  4. Add kI (Rarely): Only if you are consistently off by a tiny bit. Warning: kI causes instability easily.

πŸ’» Chapter 2: Implementation (WPILib vs Vendor)

Option A: WPILib PIDController (Runs on RIO)

Good for learning, but runs in the main loop (20ms latency).

PIDController m_pid = new PIDController(0.1, 0, 0); // kP, kI, kD
 
public void periodic() {
    double output = m_pid.calculate(
        m_encoder.getPosition(), // Measurement
        m_targetPosition         // Setpoint
    );
    m_motor.set(output);
}

Option B: Vendor On-Board PID (TalonFX/SparkMax)

Preferred Method. Runs on the motor controller (1ms latency = 20x faster!).

// Setup (In Constructor)
TalonFXConfiguration config = new TalonFXConfiguration();
config.Slot0.kP = 0.1;
config.Slot0.kI = 0.0;
config.Slot0.kD = 0.01;
m_motor.getConfigurator().apply(config);
 
// Usage
// "Go to 50 rotations using Slot 0"
m_motor.setControl(new PositionVoltage(50).withSlot(0));

πŸš€ Chapter 3: Feedforward (The Secret Sauce)

PID reacts to error. Feedforward predicts it. If you know your arm weighs 10lbs, you know you always need 2V to hold it up. Why wait for PID to figure that out?

ArmFeedforward m_feedforward = new ArmFeedforward(
    0,    // kS (Static friction)
    1.5,  // kG (Gravity voltage)
    2.1   // kV (Velocity)
);
 
// Calculate output
double ff = m_feedforward.calculate(targetAngleRadians, targetVelocity);
double pid = m_pid.calculate(currentAngle, targetAngle);
 
m_motor.setVoltage(ff + pid);
βœ…

Rule of Thumb: Feedforward gets you 90% of the way there. PID cleans up the last 10% error.


🏁 Summary

You have mastered the Math of Motion!

  • P pushes you there.
  • D slows you down.
  • Feedforward fights gravity/friction.

In Episode 7, we will combine all of this to make the robot drive itself in Autonomous mode!


πŸ“ž Need Help? We've Got Your Back!

Keep coding, keep learning, and remember: every expert was once a beginner who refused to give up! πŸš€

P.S. - If your robot starts talking back, that's either very advanced AI or you need more sleep. Probably more sleep. πŸ€–πŸ’€


This documentation is part of the Zero to Hero programming series. For the complete learning experience, watch the accompanying video and practice with real robot code. Remember: the only way to get good at programming is to write lots of bad code first! πŸ˜„

πŸ’

Special Thanks: To all the students who asked "Why doesn't this work?" and inspired us to create better documentation. Your questions make us better teachers, and your curiosity drives innovation. You're the real MVPs! πŸ†

Remember: Every expert was once a beginner who refused to give up. Keep coding, keep learning, and most importantly - keep having fun! πŸš€