Project 5 — A One-Button Autonomous Routine
Compose drive and launch commands into a timed autonomous sequence that scores in REBUILT's 20-second auto period.
Sign in to track progress, earn XP, and save lessons.
REBUILT gives robots a 20-second autonomous period at the start of each match. A reliable auto that scores even one preloaded Fuel and then leaves its starting zone is worth more than a fancy one that fails. We will compose the commands you already wrote into a sequence.
Command-based shines here because you build complex routines by composing simple commands. The key compositions are andThen (sequential), withTimeout (stop a command after N seconds), and Commands.sequence(...).
Add a factory to DriveSubsystem:
public Command driveForTime(double fwd, double seconds) {
return run(() -> arcade(fwd, 0)).withTimeout(seconds)
.andThen(runOnce(() -> arcade(0, 0)));
}
Now assemble the routine in RobotContainer:
import edu.wpi.first.wpilibj2.command.Commands;
public Command getAutonomousCommand() {
return Commands.sequence(
m_launcher.shoot().withTimeout(2.5), // spin up & fire preload
m_drive.driveForTime(0.4, 1.5) // leave the starting zone
);
}
And schedule it in Robot.java:
@Override public void autonomousInit() {
Command auto = m_robotContainer.getAutonomousCommand();
if (auto != null) auto.schedule();
}
@Override public void teleopInit() {
// cancel any leftover auto command when teleop starts
CommandScheduler.getInstance().cancelAll();
}
Why this scores points: firing the preload aims at the Hub for Fuel points (each Fuel in the active Hub is worth 1 point in auto), and driveForTime moves the robot off its starting line. Driving forward at 40% for 1.5s is gentle enough not to overshoot. The withTimeout on the shoot command is essential — without an end condition, an auto command can run forever and block the sequence.
Make it selectable: real teams put a SendableChooser<Command> on the dashboard so drivers pick 'Score + Leave', 'Just Leave', or 'Do Nothing' before the match. Add it later; the composition pattern above is the engine.
Test methodically: run auto on blocks first, confirm the launcher fires and the wheels turn the right direction, then floor-test in an open space. A simple, repeatable 20-second auto that works every match is a genuine competitive advantage — many rookie alliances are decided by who scored in auto and who sat still.
Key takeaways
- Compose autos from simple commands using Commands.sequence, andThen, and withTimeout
- Every timed auto command needs an end condition (withTimeout) or it blocks the rest of the sequence
- A boring, reliable 'score preload + leave the zone' auto beats a flashy one that fails — auto often decides rookie matches
Go deeper
Lesson quiz
RequiredAnswer all 3 questions correctly to complete this lesson.
1.In a command-based project, which RobotContainer method returns the command to run during autonomous?
2.Where does the Robot class actually start (schedule) the autonomous command?
3.For the scheduled autonomous command to actually run each loop, what must be called periodically?
Answer every question to submit.