From f3d9d2e697e001418abb91284f8aade589813355 Mon Sep 17 00:00:00 2001 From: Harry <96154793+WasteOfTheOcean@users.noreply.github.com> Date: Sat, 8 Feb 2025 14:20:31 -0600 Subject: [PATCH 1/2] Implemented Reef Scoring and Dealgaefy Set Desired Level Commands --- src/main/java/frc/robot/RobotContainer.java | 15 +++ .../frc/robot/util/ReefPositionsUtil.java | 110 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/main/java/frc/robot/util/ReefPositionsUtil.java diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 15a3aea..0ea1251 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -75,6 +75,9 @@ import frc.robot.util.CanDef; import frc.robot.util.CanDef.CanBus; import frc.robot.util.LoggedTunableNumber; +import frc.robot.util.ReefPositionsUtil; +import frc.robot.util.ReefPositionsUtil.DeAlgaeLevel; +import frc.robot.util.ReefPositionsUtil.ScoreLevel; /** * This class is where the bulk of the robot should be declared. Since Command-based is a @@ -278,6 +281,18 @@ private void configureButtonBindings() { drive) .ignoringDisable(true)); + // Reef scoring position sets + co_controller.y().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L4)); + co_controller.x().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L3)); + co_controller.b().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L2)); + co_controller.a().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L1)); // Trough + + // Reef DeAlgaefy scoring position sets + co_controller.rightBumper().onTrue(ReefPositionsUtil.getNewSetDeAlgaeLevel(DeAlgaeLevel.Top)); // L3/4 + co_controller.rightTrigger().onTrue(ReefPositionsUtil.getNewSetDeAlgaeLevel(DeAlgaeLevel.Low)); // L2/3 + + // TODO: Implement climbing controls (L Bumper climb and (maybe) L Trigger unclimb) + characterizeController .back() .and(characterizeController.y()) diff --git a/src/main/java/frc/robot/util/ReefPositionsUtil.java b/src/main/java/frc/robot/util/ReefPositionsUtil.java new file mode 100644 index 0000000..1cd87ba --- /dev/null +++ b/src/main/java/frc/robot/util/ReefPositionsUtil.java @@ -0,0 +1,110 @@ +package frc.robot.util; + +import edu.wpi.first.wpilibj2.command.InstantCommand; + +public class ReefPositionsUtil { + + public static enum ScoreLevel { + L1, + L2, + L3, + L4 + } + + public static enum DeAlgaeLevel { + Top, + Low + } + + // Defaults + private static ScoreLevel selectedScoreLevel = ScoreLevel.L1; + private static DeAlgaeLevel selectedDeAlgaeLevel = DeAlgaeLevel.Low; + + /** + * Sets selected level variable to given ScoreLevel value. + * For a command that runs this method use getNewSetScoreLevelCommand(ScoreLevel) + * + * @param level the desired level to select (L1 is Trough) + */ + public static void setScoreLevel(ScoreLevel level) { + selectedScoreLevel = level; + } + + /** + * Creates a new command that sets the selected level variable. + * For the runnable itself use setScoreLevel(ScoreLevel) + * + * @param level the desired level to select (L1 is Trough) + * @return an instant command that runs the set method + */ + public static InstantCommand getNewSetScoreLevelCommand(ScoreLevel level) { + return new InstantCommand(() -> setScoreLevel(level)); + } + + /** + * Use to determine which reef score level is currently selected. Useful for logging. + * For a boolean output, use isSelected(ScoreLevel level) + * + * @return the currently selected scoring level + */ + public static ScoreLevel getScoreLevel() { + return selectedScoreLevel; + } + + /** + * Use to determine whether selected position is the given level. Useful for conditional commands. + * For simply determining which is selected, use getScoreLevel() + * + * Input a ScoreLevel to check the scoring level, and DeAlgaeLevel to check the dealgaefy level + * + * @param level the score level to check + * @return whether the selected scoring level is the same as the level parameter + */ + public static boolean isSelected(ScoreLevel level) { + return (level.equals(selectedScoreLevel)); + } + + /** + * Sets selected level variable to given DeAlgae value. + * For a command that runs this method use getNewSetDeAlgaeLevelCommand(ScoreLevel) + * + * @param level the desired level to select (Top is between L3 and L4; Bottom is between L2 and L3) + */ + public static void setDeAlgaeLevel(DeAlgaeLevel level) { + selectedDeAlgaeLevel = level; + } + + /** + * Sets selected level variable to given DeAlgae value. + * For a command that runs this method use getNewSetDeAlgaeLevelCommand(ScoreLevel) + * + * @param level the desired level to select (Top is between L3 and L4; Bottom is between L2 and L3) + * @return an instant command that runs the set method + */ + public static InstantCommand getNewSetDeAlgaeLevel(DeAlgaeLevel level) { + return new InstantCommand(() -> setDeAlgaeLevel(level)); + } + + /** + * Use to determine which dealgae level is currently selected. Useful for logging. + * For a boolean output, use isSelected(DeAlgaeLevel level) + * + * @return the currently selected dealgaefy level + */ + public static DeAlgaeLevel getDeAlgaeLevel() { + return selectedDeAlgaeLevel; + } + + /** + * Use to determine whether selected position is the given level. Useful for conditional commands. + * For simply determining which is selected, use getDeAlgaeLevel() + * + * Input a ScoreLevel to check the scoring level, and DeAlgaeLevel to check the dealgaefy level + * + * @param level the dealgae level to check + * @return whether the selected dealgaefy level is the same as the level parameter + */ + public static boolean isSelected(DeAlgaeLevel level) { + return (level.equals(selectedDeAlgaeLevel)); + } +} From 6809c8aef33d7020c28f2057055544a9fe9d0e84 Mon Sep 17 00:00:00 2001 From: Harry <96154793+WasteOfTheOcean@users.noreply.github.com> Date: Sat, 8 Feb 2025 14:48:29 -0600 Subject: [PATCH 2/2] Changed ReefPositionsUtil to Singleton pattern from static structure --- src/main/java/frc/robot/RobotContainer.java | 14 ++++---- .../frc/robot/util/ReefPositionsUtil.java | 34 +++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 0ea1251..03d7913 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -116,6 +116,7 @@ public class RobotContainer { private AutoCommandManager autoCommandManager; private RobotState robotState; + private ReefPositionsUtil reefPositions; private boolean m_TeleopInitialized = false; @@ -235,6 +236,7 @@ public RobotContainer(){ } autoCommandManager = new AutoCommandManager(drive); + reefPositions = ReefPositionsUtil.getInstance(); // Configure the button bindings configureButtonBindings(); @@ -282,14 +284,14 @@ private void configureButtonBindings() { .ignoringDisable(true)); // Reef scoring position sets - co_controller.y().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L4)); - co_controller.x().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L3)); - co_controller.b().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L2)); - co_controller.a().onTrue(ReefPositionsUtil.getNewSetScoreLevelCommand(ScoreLevel.L1)); // Trough + co_controller.y().onTrue(reefPositions.getNewSetScoreLevelCommand(ScoreLevel.L4)); + co_controller.x().onTrue(reefPositions.getNewSetScoreLevelCommand(ScoreLevel.L3)); + co_controller.b().onTrue(reefPositions.getNewSetScoreLevelCommand(ScoreLevel.L2)); + co_controller.a().onTrue(reefPositions.getNewSetScoreLevelCommand(ScoreLevel.L1)); // Trough // Reef DeAlgaefy scoring position sets - co_controller.rightBumper().onTrue(ReefPositionsUtil.getNewSetDeAlgaeLevel(DeAlgaeLevel.Top)); // L3/4 - co_controller.rightTrigger().onTrue(ReefPositionsUtil.getNewSetDeAlgaeLevel(DeAlgaeLevel.Low)); // L2/3 + co_controller.rightBumper().onTrue(reefPositions.getNewSetDeAlgaeLevel(DeAlgaeLevel.Top)); // L3/4 + co_controller.rightTrigger().onTrue(reefPositions.getNewSetDeAlgaeLevel(DeAlgaeLevel.Low)); // L2/3 // TODO: Implement climbing controls (L Bumper climb and (maybe) L Trigger unclimb) diff --git a/src/main/java/frc/robot/util/ReefPositionsUtil.java b/src/main/java/frc/robot/util/ReefPositionsUtil.java index 1cd87ba..287a303 100644 --- a/src/main/java/frc/robot/util/ReefPositionsUtil.java +++ b/src/main/java/frc/robot/util/ReefPositionsUtil.java @@ -17,8 +17,22 @@ public static enum DeAlgaeLevel { } // Defaults - private static ScoreLevel selectedScoreLevel = ScoreLevel.L1; - private static DeAlgaeLevel selectedDeAlgaeLevel = DeAlgaeLevel.Low; + private ScoreLevel selectedScoreLevel = ScoreLevel.L1; + private DeAlgaeLevel selectedDeAlgaeLevel = DeAlgaeLevel.Low; + + private static ReefPositionsUtil instance; + + public static ReefPositionsUtil getInstance() { + if (instance == null) { + instance = new ReefPositionsUtil(); + } + return instance; + } + + private ReefPositionsUtil() { + selectedScoreLevel = ScoreLevel.L1; + selectedDeAlgaeLevel = DeAlgaeLevel.Low; + } /** * Sets selected level variable to given ScoreLevel value. @@ -26,7 +40,7 @@ public static enum DeAlgaeLevel { * * @param level the desired level to select (L1 is Trough) */ - public static void setScoreLevel(ScoreLevel level) { + public void setScoreLevel(ScoreLevel level) { selectedScoreLevel = level; } @@ -37,7 +51,7 @@ public static void setScoreLevel(ScoreLevel level) { * @param level the desired level to select (L1 is Trough) * @return an instant command that runs the set method */ - public static InstantCommand getNewSetScoreLevelCommand(ScoreLevel level) { + public InstantCommand getNewSetScoreLevelCommand(ScoreLevel level) { return new InstantCommand(() -> setScoreLevel(level)); } @@ -47,7 +61,7 @@ public static InstantCommand getNewSetScoreLevelCommand(ScoreLevel level) { * * @return the currently selected scoring level */ - public static ScoreLevel getScoreLevel() { + public ScoreLevel getScoreLevel() { return selectedScoreLevel; } @@ -60,7 +74,7 @@ public static ScoreLevel getScoreLevel() { * @param level the score level to check * @return whether the selected scoring level is the same as the level parameter */ - public static boolean isSelected(ScoreLevel level) { + public boolean isSelected(ScoreLevel level) { return (level.equals(selectedScoreLevel)); } @@ -70,7 +84,7 @@ public static boolean isSelected(ScoreLevel level) { * * @param level the desired level to select (Top is between L3 and L4; Bottom is between L2 and L3) */ - public static void setDeAlgaeLevel(DeAlgaeLevel level) { + public void setDeAlgaeLevel(DeAlgaeLevel level) { selectedDeAlgaeLevel = level; } @@ -81,7 +95,7 @@ public static void setDeAlgaeLevel(DeAlgaeLevel level) { * @param level the desired level to select (Top is between L3 and L4; Bottom is between L2 and L3) * @return an instant command that runs the set method */ - public static InstantCommand getNewSetDeAlgaeLevel(DeAlgaeLevel level) { + public InstantCommand getNewSetDeAlgaeLevel(DeAlgaeLevel level) { return new InstantCommand(() -> setDeAlgaeLevel(level)); } @@ -91,7 +105,7 @@ public static InstantCommand getNewSetDeAlgaeLevel(DeAlgaeLevel level) { * * @return the currently selected dealgaefy level */ - public static DeAlgaeLevel getDeAlgaeLevel() { + public DeAlgaeLevel getDeAlgaeLevel() { return selectedDeAlgaeLevel; } @@ -104,7 +118,7 @@ public static DeAlgaeLevel getDeAlgaeLevel() { * @param level the dealgae level to check * @return whether the selected dealgaefy level is the same as the level parameter */ - public static boolean isSelected(DeAlgaeLevel level) { + public boolean isSelected(DeAlgaeLevel level) { return (level.equals(selectedDeAlgaeLevel)); } }