Package choreo.auto

Class AutoChooser

java.lang.Object
choreo.auto.AutoChooser
All Implemented Interfaces:
Sendable

public class AutoChooser extends Object implements Sendable
An Choreo specific SendableChooser that allows for the selection of AutoRoutines at runtime via a Dashboard.

This chooser takes a lazy loading approach to AutoRoutines, 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 AutoRoutines to it using addRoutine(java.lang.String, java.util.function.Supplier<choreo.auto.AutoRoutine>) or add Commands 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 Details

  • Method Details

    • select

      public String select(String selectStr)
      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

      public void addRoutine(String name, Supplier<AutoRoutine> generator)
      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 an AutoRoutine.

      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

      public void addCmd(String name, Supplier<Command> generator)
      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 a Trigger it is recommended to use selectedCommandScheduler() instead.

      Returns:
      The currently selected command.
    • initSendable

      public void initSendable(SendableBuilder builder)
      Specified by:
      initSendable in interface Sendable