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

Autonomous

Build an autonomous routine that drives off the starting line and stops.

Branchswerve-autonomous8 minutes
You’ll need
  • A swerve robot you can drive, from Swerve Drive Tuning.
  • run(coroutine -> ...) and coroutine.wait(...), from Coroutines.
  • Three meters of clear floor and one person on the disable switch.

An autonomous routine is one class holding one command.

Build the routine

Create LeaveStartAuto.java in the opmode folder, beside TeleopOpMode.java. We will be creating a coroutine to drive the robot forward on a timer. Start with the empty class.

src/main/java/first/robot/opmode/LeaveStartAuto.java
package first.robot.opmode;
 
import first.robot.Robot;
import org.wpilib.opmode.Autonomous;
import org.wpilib.opmode.PeriodicOpMode;
 
@Autonomous(name = "Leave Start")
public class LeaveStartAuto extends PeriodicOpMode {
public LeaveStartAuto(Robot robot) {}
}

The name in the annotation is what the driver station lists.

Next, add a routine field and build it in the constructor. The routine needs two requests: one that drives forward and one that asks for zero.

src/main/java/first/robot/opmode/LeaveStartAuto.java
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");
}
}

The coroutine sends forward, waits a second and a half, then sends stopped. A cancel skips the last line of the coroutine, so whenCanceled sends the same zero. You will also see our coroutine has .named(...), which we will be able to see running on the dashboard.

Last, tell the OpMode when to run the routine. start() schedules it on enable. There is no end() to write. The framework cancels a command an OpMode scheduled when that OpMode exits, a disable included.

src/main/java/first/robot/opmode/LeaveStartAuto.java
// ... the constructor, unchanged
 
@Override
public void start() {
Scheduler.getDefault().schedule(routine);
}
}

The finished file, with its imports:

LeaveStartAuto.java38 lines
package first.robot.opmode;
 
import static org.wpilib.units.Units.Seconds;
 
import com.ctre.phoenix6.swerve.SwerveRequest;
import first.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);
}
}

Simulation first

Before you run any autonomous on a real robot, run it in simulation.

  1. Run WPILib: Simulate Robot Code and pick Leave Start from the autonomous list.
  2. Enable, and watch Drivetrain/Pose on the 2D field in AdvantageScope.
Check

You should see

The robot drive forward about a meter and a half, stop, and stay stopped.

Check your work

Don't

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. Enable last.

Put the robot on the floor with clear floor ahead of it, pick Leave Start, and enable.

Check

You should see

The robot leaving in the direction its front bumper points, then stopping and staying stopped.

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. What cancels the routine, and what happens on the next enable?

Pick an answer for each.