Pre-planned autonomous paths work when the field is completely static. During teleoperated match play or chaotic multi-robot autonomous routines, robots encounter disabled alliance partners, active defense, and unexpected game piece congestion.
On-the-fly pathfinding enables the robot to calculate a smooth, collision-free trajectory from its current live pose to any target position in real time on the roboRIO.
1. What to Learn First
Section titled “1. What to Learn First”Before deploying on-the-fly pathfinding:
- Ensure your robot pose estimator fuses wheel odometry and AprilTag vision measurements with bounded drift as taught in Vision Localization.
- Calibrate robot bounding box dimensions including bumpers. Pathfinding algorithms rely on bumper boundaries to ensure safe clearance around field structures.
2. How Real-Time Pathfinding Works
Section titled “2. How Real-Time Pathfinding Works”PathPlannerLib implements a hybrid navigation algorithm composed of two stages:
- *Global Grid Search (A)**:
- The field is mapped into a discrete grid of traversable nodes.
- Field obstacles (trusses, stage legs, reefs, speaker structures) are defined as non-traversable exclusion polygons buffered by the robot’s radius plus safety margins.
- An A* graph search finds the shortest path of clear nodes from the current pose to the destination.
- Local Spline Smoothing:
- The node sequence is converted into a smooth cubic Bézier curve.
- Velocity profiles are generated using the robot’s kinematic constraints, ensuring smooth transitions without sudden lateral acceleration steps.
- Execution Pipeline:
- The pathfinder runs on a separate thread on the roboRIO to prevent dropping 20ms periodic robot cycles.
- As the robot drives, the path is continually updated if new target poses or updated dynamic obstacles are commanded.
3. Configuring Pathfinding Constraints
Section titled “3. Configuring Pathfinding Constraints”In your SwerveSubsystem initialization or RobotContainer, configure the PathfindingConstraints:
package frc.robot.subsystems;
import com.pathplanner.lib.pathfinding.Pathfinding;import com.pathplanner.lib.path.PathConstraints;import static edu.wpi.first.units.Units.*;
public class NavigationConfig { // Kinematic limits for real-time pathfinding (typed WPILib Units in PathPlannerLib 2025+) public static final PathConstraints PATHFINDING_CONSTRAINTS = new PathConstraints( MetersPerSecond.of(3.5), MetersPerSecondPerSecond.of(2.5), DegreesPerSecond.of(360.0), DegreesPerSecondPerSecond.of(540.0) );
// Warm up the pathfinding thread during robot initialization public static void initializePathfinding() { // Ensures the navigation graph is loaded into memory before the match begins Pathfinding.ensureInitialized(); }}The initial construction of the navigation graph can take 100 to 200 milliseconds of CPU time. Always call Pathfinding.ensureInitialized() in robotInit() so this processing occurs during the pit connection phase rather than when the driver presses a button during a match.
4. Building On-the-Fly Navigation Commands
Section titled “4. Building On-the-Fly Navigation Commands”PathPlannerLib provides AutoBuilder.pathfindToPose() to construct self-contained commands that drive from the robot’s live pose to a desired target pose:
package frc.robot.commands;
import com.pathplanner.lib.auto.AutoBuilder;import edu.wpi.first.math.geometry.Pose2d;import edu.wpi.first.math.geometry.Rotation2d;import edu.wpi.first.wpilibj2.command.Command;import frc.robot.subsystems.NavigationConfig;
public class DriveToTargetCommands { /** * Commands the swerve drivetrain to pathfind autonomously to the amp scoring station. */ public static Command pathfindToAmpCommand() { Pose2d ampTargetPose = new Pose2d( 1.84, // Field X in meters 7.70, // Field Y in meters Rotation2d.fromDegrees(-90.0) // Desired terminal robot heading );
return AutoBuilder.pathfindToPose( ampTargetPose, NavigationConfig.PATHFINDING_CONSTRAINTS, 0.0 // Goal end velocity (0.0 m/s for a full stop at the station) ); }}Driver Assist Binding
Section titled “Driver Assist Binding”In RobotContainer, bind this command to an operator trigger. While holding the button, the robot plans and follows the path; releasing the button immediately interrupts the command and returns manual control to the driver:
// Driver controller button X triggers automated pathfinding to the scoring station:m_driver.x().whileTrue(DriveToTargetCommands.pathfindToAmpCommand());5. Adding Dynamic Obstacles
Section titled “5. Adding Dynamic Obstacles”If an alliance partner dies on the field, your autonomous or teleop assist routines can add dynamic bounding boxes to route around the stationary robot:
import com.pathplanner.lib.pathfinding.LocalADStar;import edu.wpi.first.math.geometry.Translation2d;import java.util.List;
public void registerDisabledRobotObstacle(Translation2d centerPosition) { double halfWidth = 0.50; // 0.50m half-width for standard robot with bumpers
List<Translation2d> obstacleCorners = List.of( new Translation2d(centerPosition.getX() - halfWidth, centerPosition.getY() - halfWidth), new Translation2d(centerPosition.getX() + halfWidth, centerPosition.getY() - halfWidth), new Translation2d(centerPosition.getX() + halfWidth, centerPosition.getY() + halfWidth), new Translation2d(centerPosition.getX() - halfWidth, centerPosition.getY() + halfWidth) );
// Injects dynamic obstacle polygon into the navigation graph Pathfinding.setDynamicObstacles(List.of(obstacleCorners), m_poseEstimator.getCurrentPose().getTranslation());}How Success is Checked
Section titled “How Success is Checked”- Calculation Latency:
- Verify in your AdvantageScope log that path calculation latency is under 40 ms from trigger assertion to command execution.
- Obstacle Clearance:
- Drive the robot toward a field structure with automated pathfinding enabled.
- Confirm in simulation and on the physical field that the path leaves at least 0.10 meters of buffer clearance between the bumpers and the obstacle perimeter.
- Smooth Seamless Hand-Off:
- When the driver releases the button mid-path, the drivetrain must immediately switch to teleop default driving without hesitation or lurching.
Where to Go Next
Section titled “Where to Go Next”With trajectory optimization and dynamic pathfinding established, advance to Module 2: Choreography & Sensor-Fused Realignment to synchronize high-speed mechanism actions with robot motion.
Verified Sources & Standards
- PathPlanner Pathfinding Architecturepathplanner.devCommunity Reference
- WPILib Geometry & Coordinate Systemsdocs.wpilib.orgWPILib Standard