Building a Custom Path Following Algorithm (FTC)

advanced30 min

Why Build Your Own Path Follower?

Libraries hide a lot of detail. A small custom follower teaches how pose error turns into motor power and makes it easier to debug when auto drifts off the path.

Core Concepts: Poses, Paths, and Controllers

Store waypoints as target poses. Each loop, compare the robot pose from odometry to the active waypoint. Use one PID (or PIDF) on distance to the target and one on heading error, then mix outputs into drive commands. Advance to the next waypoint when both errors fall below thresholds.

Basic Structure of a Custom Path Follower

Here's a simplified Java structure for a custom path follower in FTC:

// Assume you have a list of target poses (waypoints)
List<Pose2d> path = ...;
PIDFController translationalPID, headingPID;

while (opModeIsActive() && !path.isEmpty()) {
    Pose2d currentPose = getCurrentRobotPose();
    Pose2d targetPose = path.get(0);
    double distanceError = currentPose.distanceTo(targetPose);
    double headingError = angleWrap(targetPose.heading - currentPose.heading);
    double drivePower = translationalPID.calculate(distanceError);
    double turnPower = headingPID.calculate(headingError);
    setDrivePowers(drivePower, turnPower);
    if (distanceError < threshold && Math.abs(headingError) < headingThreshold) {
        path.remove(0); // Move to next waypoint
    }
}

// Helper: angleWrap wraps angle to [-pi, pi]
public double angleWrap(double angle) {
    while (angle > Math.PI) angle -= 2 * Math.PI;
    while (angle < -Math.PI) angle += 2 * Math.PI;
    return angle;
}

Tuning and Practical Tips

  • Tune translation and heading PID separately on a single waypoint before chaining many points.
  • Good pose estimates require calibrated odometry (or fusion); fix localization before chasing follower gains.
  • Start with a straight line and a 90° turn, then add curves.
  • At higher speeds, add velocity feedforward so PID does less work.
  • Plot error and pose on telemetry or FTC Dashboard while testing.