# Choosing an Autonomous Program

Often teams have more than one autonomous program, either for competitive reasons or for testing new software. Programs often vary by adding things like time delays, different strategies, etc. The methods to choose the strategy to run usually involves switches, joystick buttons, knobs or other hardware based inputs.

With the SmartDashboard you can simply display a widget on the screen to choose the autonomous program that you would like to run. And with command based programs, that program is encapsulated in one of several commands. This article shows how to select an autonomous program with only a few lines of code and a nice looking user interface, with examples for both TimedRobot and Command-Based Robots.

## TimedRobot

Note

The code snippets shown below are part of the TimedRobot template (Java, C++):

### Creating SendableChooser Object

In Robot.java / Robot.h, create a variable to hold a reference to a SendableChooser object. Two or more auto modes can be added by creating strings to send to the chooser. Using the SendableChooser, one can choose between them. In this example, Default and My Auto are shown as options. You will also need a variable to store which auto has been chosen, m_autoSelected.

  private static final String kDefaultAuto = "Default";
private static final String kCustomAuto = "My Auto";
private String m_autoSelected;
private final SendableChooser<String> m_chooser = new SendableChooser<>();


### Setting Up Options

The chooser allows you to pick from a list of defined elements, in this case the strings we defined above. In robotInit, add your options created as strings above using setDefaultOption or addOption. setDefaultOption will be the one selected by default when the dashboard starts. The putData function will push it to the dashboard on your driver station computer.

  public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
SmartDashboard.putData("Auto choices", m_chooser);
}


### Running Autonomous Code

Now, in autonomousInit and autonomousPeriodic, you can use the m_autoSelected variable to read which option was chosen, and change what happens during the autonomous period.

  @Override
public void autonomousInit() {
m_autoSelected = m_chooser.getSelected();
// m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
System.out.println("Auto selected: " + m_autoSelected);
}

/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {
switch (m_autoSelected) {
case kCustomAuto:
// Put custom auto code here
break;
case kDefaultAuto:
default:
// Put default auto code here
break;
}
}


## Command-Based

Note

The code snippets shown below are part of the HatchbotTraditional example project (Java, C++):

### Creating the SendableChooser Object

In RobotContainer, create a variable to hold a reference to a SendableChooser object. Two or more commands can be created and stored in new variables. Using the SendableChooser, one can choose between them. In this example, SimpleAuto and ComplexAuto are shown as options.

  // A simple auto routine that drives forward a specified distance, and then stops.
private final Command m_simpleAuto =
new DriveDistance(
AutoConstants.kAutoDriveDistanceInches, AutoConstants.kAutoDriveSpeed, m_robotDrive);

// A complex auto routine that drives forward, drops a hatch, and then drives backward.
private final Command m_complexAuto = new ComplexAuto(m_robotDrive, m_hatchSubsystem);

// A chooser for autonomous commands
SendableChooser<Command> m_chooser = new SendableChooser<>();


### Setting up SendableChooser

Imagine that you have two autonomous programs to choose between and they are encapsulated in commands SimpleAuto and ComplexAuto. To choose between them:

In RobotContainer, create a SendableChooser object and add instances of the two commands to it. There can be any number of commands, and the one added as a default (setDefaultOption), becomes the one that is initially selected. Notice that each command is included in an setDefaultOption() or addOption() method call on the SendableChooser instance.

// Add commands to the autonomous command chooser
m_chooser.setDefaultOption("Simple Auto", m_simpleAuto);

// Put the chooser on the dashboard
SmartDashboard.putData(m_chooser);


### Starting an Autonomous Command

In Robot.java, when the autonomous period starts, the SendableChooser object is polled to get the selected command and that command must be scheduled.

  public Command getAutonomousCommand() {
return m_chooser.getSelected();
}

  public void autonomousInit() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();

// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
}
}


### Running the Scheduler during Autonomous

In Robot.java, this will run the scheduler every driver station update period (about every 20ms) and cause the selected autonomous command to run.

Note

Running the scheduler can occur in the autonomousPeriodic() function or robotPeriodic(), both will function similarly in autonomous mode.

40  @Override
41  public void robotPeriodic() {
42    CommandScheduler.getInstance().run();
43  }


### Canceling the Autonomous Command

In Robot.java, when the teleop period begins, the autonomous command will be canceled.

78  @Override
79  public void teleopInit() {
80    // This makes sure that the autonomous stops running when
81    // teleop starts running. If you want the autonomous to
82    // continue until interrupted by another command, remove
83    // this line or comment it out.
84    if (m_autonomousCommand != null) {
85      m_autonomousCommand.cancel();
86    }
87  }


### SmartDashboard Display

When the SmartDashboard is run, the choices from the SendableChooser are automatically displayed. You can simply pick an option before the autonomous period begins and the corresponding command will run.