Unit 7: Advanced FRC Coding
A competitive robot needs multiple autonomous routines. The `SendableChooser` is a powerful WPILib utility that creates an interactive drop-down menu on the SmartDashboard, allowing the drive team to select the right auto for any match.
Implementing a `SendableChooser` is a straightforward process that takes place almost entirely inside `RobotContainer.java`. It provides a simple, reliable way to manage multiple autonomous strategies without redeploying code.
First, create an instance of `SendableChooser` as a private field in `RobotContainer`. Its generic type will be `Command`, since we'll be choosing between different autonomous command groups.
public class RobotContainer {
// ... instances of subsystems ...
// Declare the SendableChooser to hold Command objects
private final SendableChooser<Command> m_chooser = new SendableChooser<>();
// ... rest of the class ...
}
In the `RobotContainer` constructor, populate the chooser with your autonomous routines. Use `.addOption()` for regular choices and `.setDefaultOption()` for the crucial fallback routine.
public RobotContainer() {
// ... other setup ...
// Create instances of your different autonomous commands
Command twoPieceAuto = new TwoPieceAuto(m_drivetrain, m_intake);
Command defenseAuto = new DefenseAuto(m_drivetrain);
Command simpleAuto = new DriveForwardAuto(m_drivetrain);
// Add options to the chooser
m_chooser.addOption("Two Piece Auto", twoPieceAuto);
m_chooser.addOption("Defense Auto", defenseAuto);
// Set a default option as a safety measure
m_chooser.setDefaultOption("Simple Drive Forward", simpleAuto);
// ... configureButtonBindings() ...
}
Finally, publish the chooser to the SmartDashboard and create the `getAutonomousCommand()` method that `Robot.java` will call to retrieve the drive team's selection.
public RobotContainer() {
// ... add options to chooser ...
// Put the chooser on the SmartDashboard
SmartDashboard.putData("Autonomous Mode", m_chooser);
configureButtonBindings();
}
/**
* Use this to pass the autonomous command to the main {@link Robot} class.
* @return the command to run in autonomous
*/
public Command getAutonomousCommand() {
// Returns the command selected by the user on the SmartDashboard
return m_chooser.getSelected();
}
Question: What is the purpose of using `.setDefaultOption()` when setting up a `SendableChooser`?