Gray Matter
WorkshopDrive to Point
Rough draft: nobody has reviewed this lesson yet, and it may not be how things are done this season.
LESSON 23

Drive to Point

Hold a button and the robot drives itself to one spot on the field. It is a BLine path with one waypoint, built in code instead of drawn in the editor.

Branchswerve-drive-to-point8 minutes
You’ll need
  • BLine Paths, for the builder in DriveMechanism.
  • whileTrue from Writing Commands.

The target pose

The target is a Pose2d, a field coordinate measured from the blue alliance corner, as in Swerve Basics. The robot only arrives there if getPose() agrees with the field, and odometry alone drifts. Expect the robot to stop a little off the spot. The Vision workshop corrects that drift with a camera.

One waypoint, built in code

Add driveToPoint to DriveMechanism, under followPath. One Path.Waypoint is the whole path: BLine starts from wherever the robot is when the command starts, and drives to the waypoint.

src/main/java/frc/robot/subsystems/DriveMechanism.java
public class DriveMechanism implements Mechanism {
// ... everything up to followPath, unchanged
 
/**
* Returns a command that drives straight to a spot on the field, from wherever the robot is when
* it starts. It is a BLine path with one waypoint, built in code instead of drawn in the editor,
* so it gets the same controllers, speed limits and end tolerances as every drawn path.
*
* <p>The command finishes when the robot is within the end tolerances in {@code config.json}, and
* sends a zero when it finishes or is canceled.
*
* @param goal where to drive, in field coordinates from the blue alliance corner
*/
public FollowPath driveToPoint(Pose2d goal) {
return pathBuilder
.build(new Path(new Path.Waypoint(goal)))
// A field coordinate is the same spot on either alliance, so never turn it around.
.withShouldFlip(() -> false);
}
 
// ... the rest, unchanged
}

Everything the pathBuilder was given in BLine Paths comes along: the three controllers, the speed limits and the end tolerances. The command finishes once the robot is within 3 cm and 2° of the goal. It sends a zero velocity when it finishes or is canceled.

withShouldFlip(() -> false) overrides the builder's default. On red, BLine turns a drawn path around the field center. A field coordinate is a fixed spot, so flipping (3, 2) would send the robot to the far end of the field.

Bind it

Hold A to drive to the field origin, and B to drive to (3 m, 2 m) facing 180°. Add the two imports and the two bindings to TeleopOpMode, under the left-bumper binding.

src/main/java/frc/robot/opmodes/TeleopOpMode.java
import org.wpilib.math.geometry.Pose2d;
import org.wpilib.math.geometry.Rotation2d;
// ... the other imports, unchanged
 
public class TeleopOpMode extends PeriodicOpMode {
public TeleopOpMode(Robot robot) {
// Left bumper: make the robot's current facing the new "forward".
robot.driver.leftBumper().onTrue(robot.drivetrain.seedFieldCentric());
 
// Hold A or B to drive straight to a fixed spot on the field. Let go to stop.
robot.driver.a().whileTrue(robot.drivetrain.driveToPoint(Pose2d.ZERO));
robot.driver.b().whileTrue(robot.drivetrain.driveToPoint(new Pose2d(3, 2, Rotation2d.PI)));
}
}

whileTrue cancels the command when the button is released. While it runs, the path command owns the drivetrain, so the joystick default waits. Once the robot arrives the command ends, and the sticks have the drivetrain back even if the button is still down.

Check your work

  1. Enable Teleop in full simulation, drive a few meters from where the robot started, and turn it. Hold A.
  2. Release A halfway through the trip with the sticks centered.
  3. Hold B until the robot stops, then switch to red and hold B again.
Check

You should see

  • On A, the robot drive back and turn to 0° at the same time, then stop on its own.
  • On release, the robot stop where it is.
  • On B, Drivetrain/Pose settle within 3 cm of (3, 2) and 2° of 180°, on both alliances.

Check yourself

How many waypoints does the path inside driveToPoint have, and where does the robot start from?

Why does driveToPoint call withShouldFlip(() -> false) when followPath does not?

The driver holds B. The robot arrives and the button is still down. Who has the drivetrain?

Pick an answer for each.