Class AutoChooser
- All Implemented Interfaces:
Sendable
SendableChooser
that allows for the selection of AutoRoutine
s
at runtime via a Dashboard.
This chooser takes a lazy loading
approach to AutoRoutine
s, only generating the AutoRoutine
when it is selected.
This approach has the benefit of not loading all autos on startup, but also not loading the auto
during auto start causing a delay.
Once the AutoChooser
is made you can add AutoRoutine
s to it using addRoutine(java.lang.String, java.util.function.Supplier<choreo.auto.AutoRoutine>)
or add Command
s to it using addCmd(java.lang.String, java.util.function.Supplier<edu.wpi.first.wpilibj2.command.Command>)
. Similar to
SendableChooser
this chooser can be added to the SmartDashboard
using
SmartDashboard.putData(Sendable)
.
You can set the Robot's autonomous command to the chooser's chosen auto routine via
RobotModeTriggers.autonomous.whileTrue(chooser.autoSchedulingCmd());
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a Command to the auto chooser.void
addRoutine
(String name, Supplier<AutoRoutine> generator) Add an AutoRoutine to the chooser.void
initSendable
(SendableBuilder builder) Select a new option in the chooser.Returns the currently selected command.Gets a Command that schedules the selected auto routine.
-
Constructor Details
-
AutoChooser
public AutoChooser()Constructs a newAutoChooser
.
-
-
Method Details
-
select
Select a new option in the chooser.This method is called automatically when published as a sendable.
- Parameters:
selectStr
- The name of the option to select.- Returns:
- The name of the selected option.
-
addRoutine
Add an AutoRoutine to the chooser.This is done to load AutoRoutines when and only when they are selected, in order to save memory and file loading time for unused AutoRoutines.
The generators are only run when the DriverStation is disabled and the alliance is known.
One way to keep this clean is to make an `Autos` class that all of your subsystems/resources are dependency injected into. Then create methods inside that class that take an
AutoFactory
and return anAutoRoutine
.Example:
AutoChooser chooser; Autos autos = new Autos(swerve, shooter, intake, feeder); public Robot() { chooser = new AutoChooser("/Choosers"); SmartDashboard.putData(chooser); // fourPieceRight is a method that accepts an AutoFactory and returns an AutoRoutine. chooser.addRoutine("4 Piece right", autos::fourPieceRight); chooser.addRoutine("4 Piece Left", autos::fourPieceLeft); chooser.addRoutine("3 Piece Close", autos::threePieceClose); }
- Parameters:
name
- The name of the auto routine.generator
- The function that generates the auto routine.
-
addCmd
Adds a Command to the auto chooser.This is done to load autonomous commands when and only when they are selected, in order to save memory and file loading time for unused autonomous commands.
The generators are only run when the DriverStation is disabled and the alliance is known.
Example:
AutoChooser chooser; Autos autos = new Autos(swerve, shooter, intake, feeder); public Robot() { chooser = new AutoChooser("/Choosers"); SmartDashboard.putData(chooser); // fourPieceLeft is a method that accepts an AutoFactory and returns a command. chooser.addCmd("4 Piece left", autos::fourPieceLeft); chooser.addCmd("Just Shoot", shooter::shoot); }
- Parameters:
name
- The name of the autonomous command.generator
- The function that generates an autonomous command.- See Also:
-
selectedCommandScheduler
Gets a Command that schedules the selected auto routine. This Command shares the lifetime of the scheduled Command. This Command can directly be bound to a trigger, like so:AutoChooser chooser = ...; public Robot() { RobotModeTriggers.autonomous().whileTrue(chooser.selectedCommandScheduler()); }
- Returns:
- A command that runs the selected
AutoRoutine
-
selectedCommand
Returns the currently selected command.If you plan on using this
Command
in aTrigger
it is recommended to useselectedCommandScheduler()
instead.- Returns:
- The currently selected command.
-
initSendable
- Specified by:
initSendable
in interfaceSendable
-