Class AutonomousManager

java.lang.Object
com.flarerobotics.lib.auto.AutonomousManager

public class AutonomousManager extends Object
A utility class used to generate, manage and select autonomous routines on the fly.

Call updateSelectedCommand() to update the retrieved command manually, as it's only updated automatically when the autonomous period begins.

Note: Only 1 instance of the autonomous manager is allowed!

  • Field Details

    • kErrorInsteadOfWarn

      public static boolean kErrorInsteadOfWarn
      Whether to throw an error or to warn via DS. Generally should only be used when testing and not in competition. Defaults to false.
    • kNTAutonomousKey

      public static String kNTAutonomousKey
      The autonomous key to use with NetworkTables. Defaults to "Auto".
  • Constructor Details

    • AutonomousManager

      public AutonomousManager(Map<String,edu.wpi.first.math.geometry.Pose2d> poses, Map<String,edu.wpi.first.wpilibj2.command.Command> commands, com.pathplanner.lib.path.PathConstraints constraints)
      Constructs a new AutonomousManager.
      Parameters:
      poses - The map of pose names to poses.
      commands - The map of command names to commands.
      constraints - The path constraints to use for pathfinding.
    • AutonomousManager

      public AutonomousManager(Map<String,edu.wpi.first.math.geometry.Pose2d> poses, Map<String,edu.wpi.first.wpilibj2.command.Command> commands, com.pathplanner.lib.path.PathConstraints constraints, boolean autoScheduleCommand)
      Constructs a new AutonomousManager.
      Parameters:
      poses - The map of pose names to poses.
      commands - The map of command names to commands.
      constraints - The path constraints to use for pathfinding.
      autoScheduleCommand - Whether to automatically schedule the command when the autonomous period begins.
    • AutonomousManager

      public AutonomousManager(Map<String,edu.wpi.first.math.geometry.Pose2d> poses, Map<String,edu.wpi.first.wpilibj2.command.Command> commands, com.pathplanner.lib.path.PathConstraints constraints, boolean autoScheduleCommand, boolean warmupPathfind)
      Constructs a new AutonomousManager.

      Note that AutoBuilder needs to be configured if warmupPathfind is set to true.

      Parameters:
      poses - The map of pose names to poses.
      commands - The map of command names to commands.
      constraints - The path constraints to use for pathfinding.
      autoScheduleCommand - Whether to automatically schedule the command when the autonomous period begins.
      warmupPathfind - Whether to call the PathPlanner pathfind warmup command.
      Throws:
      RuntimeException - If warmupPathfind is true while AutoBuilder is not configured, and kErrorInsteadOfWarn is set to true.
  • Method Details

    • generateAutoFromString

      public edu.wpi.first.wpilibj2.command.Command generateAutoFromString(String pathPattern)
      Generates an autonomous routine from the given string pattern. Uses the poses and commands provided in the constructor. Keep in mind that the AutoBuilder needs to be configured if any poses are used. Command names are case-sensitive. If duplicate command-pose names are used, the command will be prioritized.

      Note: The command names used in the string can't contain "/", ":" or spaces.

      Example usage:

       
       String str = "path1/path2:command1:command2/path3:command3";
       m_generator.generateAutoFromString(str); // Would generate: PATH1 -> (PATH2 COMMAND1 COMMAND2 parallel) -> (PATH3
                                                // COMMAND3 parallel)
        
      Parameters:
      pathPattern - The path pattern. The commands and path names separated by a '/'. To make 2 or more parts parallel, use a ':' inbetween each.
      Returns:
      The generated autonomous command, or an empty command if the pattern is invalid or empty.
      Throws:
      RuntimeException - If the path pattern is malformed and kErrorInsteadOfWarn is true.
    • initCommandAutoChooser

      public AutonomousManager initCommandAutoChooser()
      Initializes the command autonomous sendable chooser.
      Returns:
      The singleton instance for chaining.
    • initCommandAutoChooser

      public AutonomousManager initCommandAutoChooser(String defaultAuto)
      Initializes the command autonomous sendable chooser with the given default option.
      Parameters:
      defaultAuto - The default routine to select.
      Returns:
      The singleton instance for chaining.
    • getCommandAutoChooser

      public edu.wpi.first.wpilibj.smartdashboard.SendableChooser<edu.wpi.first.wpilibj2.command.Command> getCommandAutoChooser()
      Returns the autonomous command chooser.
      Returns:
      The chooser.
    • initFlareAutoBuilderWidget

      public void initFlareAutoBuilderWidget(String tab, String title)
      Puts the Flare auto builder in the given tab on ShuffleBoard. The Flare widget needs to be installed.
      Parameters:
      tab - The tab to put the widget in.
      title - The title of the widget.
    • initFlareAutoBuilderWidget

      public void initFlareAutoBuilderWidget()
      Puts the Flare auto builder in the default tab (kWidgetDefaultTab) with the default title kWidgetDefaultTitle on ShuffleBoard. The Flare widget needs to be installed.
    • setAutoSupplier

      public AutonomousManager setAutoSupplier(Supplier<edu.wpi.first.wpilibj2.command.Command> supplier)
      Sets the autonomous command supplier. Only used when the primary source is AutonomousManager.PrimaryCommandSource.SUPPLIER.
      Parameters:
      supplier - The supplier.
      Returns:
      The singleton instance for chaining.
    • setPrimaryCommandSource

      public AutonomousManager setPrimaryCommandSource(AutonomousManager.PrimaryCommandSource option)
      Sets which source to get the autonomous command from.
      Parameters:
      option - The option to set.
      Returns:
      The singleton instance for chaining.
    • getAutonomousCommand

      public edu.wpi.first.wpilibj2.command.Command getAutonomousCommand()
      Returns the autonomous command based on the primary source. May return null if a faulty supplier is passed or an error is thrown. Does not account for the fallback command.
      Returns:
      The autonomous command.
      Throws:
      RuntimeException - If an error occurs while getting the command and if kErrorInsteadOfWarn is set to true.
    • getRetrievedCommand

      public edu.wpi.first.wpilibj2.command.Command getRetrievedCommand()
      Returns the retrieved auto command. Defaults to Commands.none(), and is only updated when the autonomous period begins, or when updateSelectedCommand() is called.
      Returns:
      The command.
    • setDashboardPatternInputKey

      public AutonomousManager setDashboardPatternInputKey(String key)
      Sets the key to use for the dashboard input. This is only used when the primary input mode is set to AutonomousManager.PrimaryCommandSource.DASHBOARD_PATTERN_STRING_INPUT.

      Defauts to "Autonomous Pattern".

      Parameters:
      key - The dashboard key.
      Returns:
      The singleton instance for chaining.
    • getDashboardPatternInputKey

      public String getDashboardPatternInputKey()
      Returns the key used for the dashboard pattern input.
      Returns:
      The dashboard key.
    • forceStartAutonomousCommand

      public void forceStartAutonomousCommand()
      Forcefully schedules the autonomous routine.
    • forceEndAutonomousCommand

      public void forceEndAutonomousCommand()
      Forcefully ends the autonomous routine.
    • updateSelectedCommandPreview

      public void updateSelectedCommandPreview()
      Updates the selected command preview on the dashboard.
    • updateSelectedCommand

      public void updateSelectedCommand()
      Updates the retrieved autonomous command.
    • setFallbackCommand

      public AutonomousManager setFallbackCommand(edu.wpi.first.wpilibj2.command.Command fallback)
      Sets the fallback autonomous command for when the main autonomous command fails. This command will only be used if the autoScheduleCommand parameter is set to true, unless called manually.
      Parameters:
      fallback - The fallback command.
      Returns:
      The singleton instance for chaining.
    • getFallbackCommand

      public edu.wpi.first.wpilibj2.command.Command getFallbackCommand()
      Returns the current fallback command.
      Returns:
      The fallback command.
    • setPreAutoCommand

      public AutonomousManager setPreAutoCommand(edu.wpi.first.wpilibj2.command.Command preCommand, boolean isParallel)
      Sets the command to schedule right before starting the autonomous routine.
      Parameters:
      preCommand - The commannd to attach.
      isParallel - Whether to run the command in parallel with the autonomous routine.
      Returns:
      The singleton instance for chaining.
    • setPathfindStrategy

      public AutonomousManager setPathfindStrategy(AutonomousManager.PathfindStrategy strategy)
      Sets the pathfinding strategy to use for the autonomous manager. Defaults to AutonomousManager.PathfindStrategy.DEFAULT.
      Parameters:
      strategy - The new pathfinding strategy to use.
      Returns:
      The singleton instance for chaining.
    • getPathfindStrategy

      public AutonomousManager.PathfindStrategy getPathfindStrategy()
      Returns the pathfinding strategy used for the autonomous manager.
      Returns:
      The strategy.
    • getPathfindCommand

      public edu.wpi.first.wpilibj2.command.Command getPathfindCommand(String poseName)
      Pathfinds to the given pose according to the pathfind strategy.
      Parameters:
      poseName - The name of the pose to pathfind to, must be in the pose map.
      Returns:
      The command, empty if an invalid pose is provided.
      Throws:
      RuntimeException - If an error occurs while retrieving the command and if kErrorInsteadOfWarn is set to true.
    • initializePoseTriggers

      public AutonomousManager initializePoseTriggers(List<AutonomousManager.ZoneTrigger> triggers)
      Sets the pose triggers. Requires the pose supplier to be set via setRobotPoseSupplier(Supplier) first.
      Parameters:
      triggers - The list of zone triggers.
      Returns:
      The singleton instance for chaining.
    • getZoneTrigger

      public edu.wpi.first.wpilibj2.command.button.Trigger getZoneTrigger(String poseName)
      Returns the zone trigger for the given pose name. The pose name must be registered in the pose map, and the triggers must be initialized via initializePoseTriggers(List).
      Parameters:
      poseName - The name of the pose to get the trigger for.
      Returns:
      The trigger, or null if not found.
    • setRobotPoseSupplier

      public AutonomousManager setRobotPoseSupplier(Supplier<edu.wpi.first.math.geometry.Pose2d> poseSupplier)
      Sets the field-relative robot pose supplier.
      Parameters:
      poseSupplier - The pose supplier.
      Returns:
      The singleton instance for chaining.
    • getInstance

      public static AutonomousManager getInstance()
      Returns the singleton AutonomousManager instance.
      Returns:
      The instance.