Autonomous
An autonomous routine is one class holding one command. The one you build here drives off the starting line and stops.
- A swerve robot you can drive, with a pose you trust, from Swerve Drive Tuning.
run(coroutine -> ...)andcoroutine.wait(...), from Coroutines.- Three meters of clear floor and one person on the disable switch.
You wrote an @Autonomous class on Coroutines, for the arm and flywheel. This one has the same shape and drives the whole robot, on a timer.
Build the routine
Create LeaveStartAuto.java in the opmodes folder, beside TeleopOpMode.java. The constructor builds the routine when somebody picks the mode. start() schedules it on enable. end() cancels it when the mode stops for any reason, a disable included.
package frc.robot.opmodes; import static org.wpilib.units.Units.Seconds; import com.ctre.phoenix6.swerve.SwerveRequest;import frc.robot.Robot;import org.wpilib.command3.Command;import org.wpilib.command3.Scheduler;import org.wpilib.opmode.Autonomous;import org.wpilib.opmode.PeriodicOpMode; @Autonomous(name = "Leave Start")public class LeaveStartAuto extends PeriodicOpMode { private final Command routine; public LeaveStartAuto(Robot robot) { // Robot-centric: X is the robot's own forward, so the starting heading sets the direction. final var forward = new SwerveRequest.RobotCentric().withVelocityX(1.0); // meters per second final var stopped = new SwerveRequest.RobotCentric(); // every speed is zero routine = robot .drivetrain .run( coroutine -> { robot.drivetrain.setControl(forward); coroutine.wait(Seconds.of(1.5)); robot.drivetrain.setControl(stopped); }) .whenCanceled(() -> robot.drivetrain.setControl(stopped)) .named("Leave Start"); } @Override public void start() { Scheduler.getDefault().schedule(routine); } @Override public void end() { Scheduler.getDefault().cancel(routine); }}The name in the annotation is what the driver station lists. The name in .named(...) is what the command is called on the dashboard.
When the routine ends, the joystick default from Robot.java takes the drivetrain back, and centered sticks ask for zero. The routine still sends stopped itself, so it does not rely on the sticks. A cancel skips the last line of the coroutine, so whenCanceled sends the same zero.
The routine never sets the robot's field position. Restart the robot code before a measured run and Drivetrain/Pose starts near zero.
Four test passes
Run them in order.
Nobody in front of the robot
An autonomous routine drives with nobody holding a stick. Give one person the robot to watch and one person the driver station, with a thumb near disable. Keep the first three meters clear of anything you care about. Enable last.
- On blocks. Deploy, pick Leave Start off the mode list, and enable. All four modules drive forward together for about a second and a half. Then they stop, and they stay stopped while the mode runs.
- On the floor, once. Put the robot on its tape mark with clear floor ahead of it and run the same routine. It leaves in the direction its front bumper points, because
RobotCentricX is the robot's forward and not the field's. - Measured, three times. Tape the floor at the front edge before and after each run, starting from the same mark every time. The taped distance and the last
Drivetrain/Posein AdvantageScope should agree, and the three runs should land close together. - Disabled partway. Hit disable about a second into the drive. The wheels stop. Enable again and the routine starts over from the first line.
Check your work
You are done when three runs from the same tape mark land on top of each other.
You should see
The robot leaving in the direction its front bumper points, stopping and staying stopped, and three end poses in Drivetrain/Pose close to each other.
Write down the taped distance, the end pose and the wait. BLine Paths replaces the wait with a path drawn from the same tape mark.
A second routine is a second file. Copy this one, change the annotation name and the numbers, and it shows up on the list beside the first. Nothing registers it.
Check yourself
You delete the setControl(stopped) line after the wait and run Leave Start on blocks, hands off the controller. What do the wheels do after 1.5 seconds?
The robot starts the run facing the side wall instead of down the field. Which way does it drive?
You want a second routine that drives 2 meters. What do you do?
You hit disable one second into the drive. Which method stops the routine, and what do you do before the next run?