Pathfinding
A drawn path starts where you drew it. Pathfinding starts wherever the robot is and searches a grid of the field for a way around the obstacles, then drives the route it found.
AutoBuilder.configureand the Auto OpMode from PathPlanner, working in simulation.- A pose you trust, from odometry or from vision.
Find a route and drive it
The search, AD*, runs on its own thread. Start it in the DriveMechanism constructor, before AutoBuilder.configure, so the first button press does not pay to load the grid.
// Load deploy/pathplanner/navgrid.json and start the route search on its own thread, now,// so the first request does not pay for it.Pathfinding.ensureInitialized();Then one method asks AutoBuilder for a pathfinding command. It uses the controller, config and alliance flip that configure already gave it.
public Command pathfindTo(Pose2d goal) { // Speed limits for routes the pathfinder makes up: 2 m/s, 2 m/s², and a turn rate of 3/4 of a // turn per second. A drawn path carries its own limits; a found one gets these. return AutoBuilder.pathfindToPose( goal, new PathConstraints(2.0, 2.0, Math.toRadians(270), Math.toRadians(360), 12.0));}The command plans from the robot's pose when it starts. AD* keeps refining that route while the robot drives. The command takes each better one until the robot is within 2 m of the goal. It finishes at the goal with zero speed. Started within 0.5 m of the goal, it sends zero and finishes without moving. That is one more reason the last stretch belongs to Drive to Point.
Bind it to a button
Hold A in teleop and the robot drives itself to the middle of the neutral zone, around the hub.
// Hold A: find a way around the hub to the middle of the neutral zone, and drive it. Let go// and the sticks take over again. The goal is blue-origin, like every pose on this robot.driver.a().whileTrue(drivetrain.pathfindTo(new Pose2d(7.5, 4.0, Rotation2d.ZERO)));Let go and the command is canceled. A canceled pathfind sends no zero. What stops the robot is the joystick default command, which takes the drivetrain back the same loop and sends whatever the sticks say. Bind this anywhere without a drive default and the robot keeps its last request.
Pathfinding is good at crossing the field and poor at the last few centimeters. Send it to a pose near the target, then finish with Drive to Point.
Three failure shapes
- No route
- A goal on a blocked square is moved to the nearest open one, so that is not it. The open area around the goal is walled off from the robot. The command sends nothing and holds the drivetrain, so the last request stays on. Let go of A.
- Grid too thin
- The center stayed on open squares and the bumper still hit. The robot is wider than the grid's margin. Paint more squares blocked around that obstacle.
- A bad pose
- The route begins somewhere the robot is not. The search trusts
getPose(), so fix odometry or vision before the grid.
Check your work
- In simulation, run the
Leave Startauto so the robot ends on the near side of the hub. - Switch to Teleop, enable, and hold A. Watch
Drivetrain/Poseon the 2D field in AdvantageScope. - Drive back with the sticks, hold A again, and let go halfway.
You should see
- A route that passes beside the hub, never through it, and ends within a few centimeters of (7.5, 4.0), facing down the field.
- A stop at the goal that stays stopped while A is held.
- Letting go halfway hands the robot straight back to the sticks.
Check yourself
What does an open square on the navigation grid mean?
You let go of A halfway. What stops the robot from rolling on?
Someone bumps the robot halfway along a found route. What happens?
You need the robot square to a scoring target within 2 cm. What is the better plan?