Package com.flarerobotics.lib.auto
Class AutonomousManager
java.lang.Object
com.flarerobotics.lib.auto.AutonomousManager
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!
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The primary pathfinding strategy for theAutonomousManager
.static enum
The primary source to get the autonomous command from for theAutonomousManager
.static class
Creates a trigger zone to execute a command when the robot is close enough to a pose by the given amount. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic boolean
Whether to throw an error or to warn via DS.static String
The autonomous key to use with NetworkTables. -
Constructor Summary
ConstructorsConstructorDescriptionAutonomousManager
(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.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.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. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Forcefully ends the autonomous routine.void
Forcefully schedules the autonomous routine.edu.wpi.first.wpilibj2.command.Command
generateAutoFromString
(String pathPattern) Generates an autonomous routine from the given string pattern.edu.wpi.first.wpilibj2.command.Command
Returns the autonomous command based on the primary source.edu.wpi.first.wpilibj.smartdashboard.SendableChooser<edu.wpi.first.wpilibj2.command.Command>
Returns the autonomous command chooser.Returns the key used for the dashboard pattern input.edu.wpi.first.wpilibj2.command.Command
Returns the current fallback command.static AutonomousManager
Returns the singletonAutonomousManager
instance.edu.wpi.first.wpilibj2.command.Command
getPathfindCommand
(String poseName) Pathfinds to the given pose according to the pathfind strategy.Returns the pathfinding strategy used for the autonomous manager.edu.wpi.first.wpilibj2.command.Command
Returns the retrieved auto command.edu.wpi.first.wpilibj2.command.button.Trigger
getZoneTrigger
(String poseName) Returns the zone trigger for the given pose name.Initializes the command autonomous sendable chooser.initCommandAutoChooser
(String defaultAuto) Initializes the command autonomous sendable chooser with the given default option.void
Puts the Flare auto builder in the default tab (kWidgetDefaultTab
) with the default titlekWidgetDefaultTitle
on ShuffleBoard.void
initFlareAutoBuilderWidget
(String tab, String title) Puts the Flare auto builder in the given tab on ShuffleBoard.Sets the pose triggers.setAutoSupplier
(Supplier<edu.wpi.first.wpilibj2.command.Command> supplier) Sets the autonomous command supplier.Sets the key to use for the dashboard input.setFallbackCommand
(edu.wpi.first.wpilibj2.command.Command fallback) Sets the fallback autonomous command for when the main autonomous command fails.Sets the pathfinding strategy to use for the autonomous manager.setPreAutoCommand
(edu.wpi.first.wpilibj2.command.Command preCommand, boolean isParallel) Sets the command to schedule right before starting the autonomous routine.Sets which source to get the autonomous command from.setRobotPoseSupplier
(Supplier<edu.wpi.first.math.geometry.Pose2d> poseSupplier) Sets the field-relative robot pose supplier.void
Updates the retrieved autonomous command.void
Updates the selected command preview on the dashboard.
-
Field Details
-
kErrorInsteadOfWarn
public static boolean kErrorInsteadOfWarnWhether to throw an error or to warn via DS. Generally should only be used when testing and not in competition. Defaults to false. -
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, andkErrorInsteadOfWarn
is set to true.
-
-
Method Details
-
generateAutoFromString
Generates an autonomous routine from the given string pattern. Uses the poses and commands provided in the constructor. Keep in mind that theAutoBuilder
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 andkErrorInsteadOfWarn
is true.
-
initCommandAutoChooser
Initializes the command autonomous sendable chooser.- Returns:
- The singleton instance for chaining.
-
initCommandAutoChooser
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
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 titlekWidgetDefaultTitle
on ShuffleBoard. The Flare widget needs to be installed. -
setAutoSupplier
Sets the autonomous command supplier. Only used when the primary source isAutonomousManager.PrimaryCommandSource.SUPPLIER
.- Parameters:
supplier
- The supplier.- Returns:
- The singleton instance for chaining.
-
setPrimaryCommandSource
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 ifkErrorInsteadOfWarn
is set to true.
-
getRetrievedCommand
public edu.wpi.first.wpilibj2.command.Command getRetrievedCommand()Returns the retrieved auto command. Defaults toCommands.none()
, and is only updated when the autonomous period begins, or whenupdateSelectedCommand()
is called.- Returns:
- The command.
-
setDashboardPatternInputKey
Sets the key to use for the dashboard input. This is only used when the primary input mode is set toAutonomousManager.PrimaryCommandSource.DASHBOARD_PATTERN_STRING_INPUT
.Defauts to "Autonomous Pattern".
- Parameters:
key
- The dashboard key.- Returns:
- The singleton instance for chaining.
-
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
Sets the fallback autonomous command for when the main autonomous command fails. This command will only be used if theautoScheduleCommand
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
Sets the pathfinding strategy to use for the autonomous manager. Defaults toAutonomousManager.PathfindStrategy.DEFAULT
.- Parameters:
strategy
- The new pathfinding strategy to use.- Returns:
- The singleton instance for chaining.
-
getPathfindStrategy
Returns the pathfinding strategy used for the autonomous manager.- Returns:
- The strategy.
-
getPathfindCommand
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 ifkErrorInsteadOfWarn
is set to true.
-
initializePoseTriggers
Sets the pose triggers. Requires the pose supplier to be set viasetRobotPoseSupplier(Supplier)
first.- Parameters:
triggers
- The list of zone triggers.- Returns:
- The singleton instance for chaining.
-
getZoneTrigger
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 viainitializePoseTriggers(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
Returns the singletonAutonomousManager
instance.- Returns:
- The instance.
-