PID Control (Android Studio)

What is PID Control?

PID stands for Proportional-Integral-Derivative. It's a feedback control algorithm that helps your robot reach and maintain a target value (like position or speed) by adjusting motor power based on the error between the target and the actual value. PID is the most common control algorithm used in FTC for precise movement.

How PID Works

  • Proportional (P): Reacts to the current error. The bigger the error, the stronger the correction.
  • Integral (I): Reacts to the accumulation of past errors. Helps eliminate steady-state error.
  • Derivative (D): Reacts to the rate of change of the error. Helps dampen oscillations and overshoot.

The PID Formula

output = kP * error + kI * integral + kD * derivative

Where error = target - actual, integral is the sum of errors over time, and derivative is the rate of change of error.

Basic PID Loop in Java (Android Studio)

double kP = 0.01, kI = 0.0, kD = 0.0;
double integral = 0, lastError = 0;
long lastTime = System.currentTimeMillis();

while (opModeIsActive()) {
    double error = targetPosition - motor.getCurrentPosition();
    long now = System.currentTimeMillis();
    double dt = (now - lastTime) / 1000.0;
    integral += error * dt;
    double derivative = (error - lastError) / dt;
    double output = kP * error + kI * integral + kD * derivative;
    motor.setPower(Range.clip(output, -1, 1));
    lastError = error;
    lastTime = now;
    if (Math.abs(error) < 10) break; // Stop if close enough
}

Tuning PID Controllers

Tuning means finding the right values for kP, kI, and kD. Start with kP, then add kD to reduce overshoot, and finally kI if you have steady-state error. Adjust one at a time and observe the robot's response. Learn more about tuning: gm0 Control Loops

Sample Rate and Performance

The sample rate is how often your PID loop updates. Faster sample rates (more frequent updates) can improve stability and responsiveness, but may use more CPU. The FTC SDK's built-in PID runs at 20Hz, but you can run your own loop faster if needed. Read more about sample rate

Troubleshooting PID

  • Oscillation: Lower kP or increase kD.
  • Slow response: Increase kP.
  • Steady-state error: Increase kI.
  • Integral windup: Limit the integral term.

Practice Exercise: PID Tuning Simulator

Practice tuning PID controllers using an interactive simulator to understand how different parameters affect system behavior.

  • Open the PID Simulator at https://pknessness.github.io/pid_sim/pid.html
  • Start with default values and observe the system response
  • Practice tuning each parameter (kP, kI, kD) individually
  • Test different scenarios using the Test Sequence button
  • Record your best tuning values and explain why they work well

Caution: Using the Integral (kI) Term in Robotics

In FIRST robotics and many real-world applications, using the integral (kI) term in PID control is generally discouraged unless you have a strong understanding of how PID works. The kI term can sometimes introduce steady-state error or cause your robot to drift over time if not tuned carefully. This is because the integral term accumulates error over time, which can lead to integral windup and unpredictable behavior. For most FTC robots, you can achieve good control with just the proportional (kP) and derivative (kD) terms. Only add kI if you have a persistent steady-state error that cannot be fixed by adjusting kP and kD, and always test thoroughly to ensure stability.

Additional Resources

Open full interactive app