Choosing an autonomous program from SmartDashboard
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.
Creating the SendableChooser object in Robot.java
Set up the SendableChooser in the robotInit() method
Imagine that you have two autonomous programs to choose between and they are encapsulated in commands Pickup and ElevatorPickup. To choose between them:
- 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 (addDefault), becomes the one that is initially selected. Notice that each command is included in an addDefault() or addObject() method call on the SendableChooser instance.
- When the autonomous period starts the SendableChooser object is polled to get the selected command and that command is scheduled.
Run the scheduler during the autonomous period
Creating a SendableChooser in C++
This is an example of creating a SendableChooser object and using it to select between a Defensive and Offensive autonomous command to run when the autonomous period of the match starts. Just as in the Java example:
- Create variables to hold the autonomousCommand pointer and the SendableChooser pointer.
- the SendableChooser is created and initialized in the RobotInit() method.
- In the AutonomousInit() method just before the Autonomous code starts running, the choosen command is retrieved from the SmartDashboard and scheduled.
- In the AutonomousPeriodic() method, the scheduler is repeatedly run.