Robot PID Control Applications (Android Studio)
Multi-Axis PID Control
Drivetrains and arms often need more than one controlled quantity at once (for example X and Y, or multiple joints). Use separate PID instances per axis and watch how corrections on one axis affect another.
Example: Dual-Axis PID for Drivetrain
Field-centric or odometry-based drive to a point (conceptual):
double xError = targetX - getCurrentX();
double yError = targetY - getCurrentY();
double xOutput = xPID.calculate(xError);
double yOutput = yPID.calculate(yError);
setDrivePower(xOutput, yOutput);
Drivetrain PID Control
Encoders support distance straight; an IMU supports heading. Together they back closed-loop drive and turn routines.
Example: PID Turn to Angle (IMU)
Turn to a target heading (simplified differential drive):
double error = targetAngle - imu.getHeading();
double output = turnPID.calculate(error);
leftMotor.setPower(output);
rightMotor.setPower(-output);
Arm and Mechanism PID Control
Arms and lifts use the same idea: encoder position as feedback, PID output as motor power.
Example: Arm PID Control
Hold an arm at a tick target:
double error = targetPosition - armMotor.getCurrentPosition();
double output = armPID.calculate(error);
armMotor.setPower(Range.clip(output, -1, 1));
Tuning Strategies for Complex Systems
- Tune one axis or mechanism at a time.
- Start with kP, then add kD and kI as needed.
- Watch for interactions between axes (e.g., X and Y).
- Use telemetry to monitor errors and outputs.
Integrating PID with the FTC SDK
PIDFCoefficients on DcMotorEx sets on-controller velocity PIDF for built-in modes. See PIDFCoefficients Javadoc. Custom OpMode loops still use your own gains when you command power directly.
Safety and Best Practices
- Always set limits on motor power and positions to prevent hardware damage.
- Use failsafes and watchdog timers to stop the robot if something goes wrong.
- Test new PID code at low power and with the robot off the ground.