-
Notifications
You must be signed in to change notification settings - Fork 1
Safe Mode Utilities #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f4f7a7
safe mode and unit tests
CoolSpy3 8326693
add newlines
CoolSpy3 88a7b49
add safe joystick configuration functions which accept a GenericHID
CoolSpy3 426e82c
bugfixes and better tests
CoolSpy3 513384a
documentation
CoolSpy3 489cb9e
fix typos
CoolSpy3 73cb8ef
document LoggingCommand
CoolSpy3 c43a679
Merge branch 'master' into safe-mode
CoolSpy3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/org/carlmontrobotics/lib199/safeMode/SafeCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.carlmontrobotics.lib199.safeMode; | ||
|
|
||
| import edu.wpi.first.wpilibj2.command.Command; | ||
| import edu.wpi.first.wpilibj2.command.CommandScheduler; | ||
| import edu.wpi.first.wpilibj2.command.FunctionalCommand; | ||
| import edu.wpi.first.wpilibj2.command.Subsystem; | ||
|
|
||
| /** | ||
| * A command that only runs when safe-mode is enabled and returns {@code isFinished() = true} otherwise. | ||
| * | ||
| * Note that this does not block calls to {@link #end(boolean)} (If the command is scheduled when safe-mode is disabled, {@link #end(boolean)} will be called immediately}) | ||
| */ | ||
| public class SafeCommand extends FunctionalCommand { | ||
|
|
||
| private final Command command; | ||
|
|
||
| /** | ||
| * Creates a new SafeCommand | ||
| * @param command The command to run | ||
| */ | ||
| public SafeCommand(Command command) { | ||
| super( | ||
| () -> { if(SafeMode.isEnabled()) command.initialize(); }, | ||
| () -> { if(SafeMode.isEnabled()) command.execute(); }, | ||
| command::end, | ||
| () -> command.isFinished() || !SafeMode.isEnabled(), | ||
| command.getRequirements().toArray(Subsystem[]::new) | ||
| ); | ||
| CommandScheduler.getInstance().registerComposedCommands(this.command = command); | ||
|
ProfessorAtomicManiac marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| public boolean runsWhenDisabled() { | ||
| return command.runsWhenDisabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public InterruptionBehavior getInterruptionBehavior() { | ||
| return command.getInterruptionBehavior(); | ||
| } | ||
|
|
||
| } | ||
39 changes: 39 additions & 0 deletions
39
src/main/java/org/carlmontrobotics/lib199/safeMode/SafeExecuteBlockingCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.carlmontrobotics.lib199.safeMode; | ||
|
|
||
| import edu.wpi.first.wpilibj2.command.Command; | ||
| import edu.wpi.first.wpilibj2.command.CommandScheduler; | ||
| import edu.wpi.first.wpilibj2.command.FunctionalCommand; | ||
| import edu.wpi.first.wpilibj2.command.Subsystem; | ||
|
|
||
| /** | ||
| * A command that only runs its {@link #execute()} method when safe-mode is enabled, and continues running as long as the underlying command is not finished. | ||
| * | ||
| * Keep in mind that this does not block calls to {@link #initialize()} or {@link #end(boolean)}, so it is not appropriate for wrapping commands such as {@link edu.wpi.first.wpilibj2.command.InstantCommand}. | ||
| * It is intended for cases where the command should keep running such as a default command where {@link #isFinished()} must always return {@code false} | ||
| */ | ||
| public class SafeExecuteBlockingCommand extends FunctionalCommand { | ||
|
|
||
| private final Command command; | ||
|
|
||
| public SafeExecuteBlockingCommand(Command command) { | ||
| super( | ||
| () -> command.initialize(), | ||
| () -> { if(SafeMode.isEnabled()) command.execute(); }, | ||
| command::end, | ||
| command::isFinished, | ||
| command.getRequirements().toArray(Subsystem[]::new) | ||
| ); | ||
| CommandScheduler.getInstance().registerComposedCommands(this.command = command); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean runsWhenDisabled() { | ||
| return command.runsWhenDisabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public InterruptionBehavior getInterruptionBehavior() { | ||
| return command.getInterruptionBehavior(); | ||
| } | ||
|
|
||
| } |
119 changes: 119 additions & 0 deletions
119
src/main/java/org/carlmontrobotics/lib199/safeMode/SafeJoystick.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package org.carlmontrobotics.lib199.safeMode; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import edu.wpi.first.wpilibj.GenericHID; | ||
|
|
||
| /** | ||
| * A wrapper for a {@link GenericHID} that implements safe-mode features. | ||
| * | ||
| * This class is designed for internal use via {@link org.carlmontrobotics.lib199.Mocks}. | ||
| * As such it provides re-implementations for {@link GenericHID} methods without extending {@link GenericHID} | ||
| * to allow for the extension of subclasses such as {@link edu.wpi.first.wpilibj.Joystick}, {@link edu.wpi.first.wpilibj.PS4Controller}, | ||
| * {@link edu.wpi.first.wpilibj.XboxController}, etc. To wrap a joystick, call {@link SafeMode#makeSafe(GenericHID)}. | ||
| */ | ||
| public class SafeJoystick { | ||
|
|
||
| /** | ||
| * The unsafe joystick that this class wraps. | ||
| */ | ||
| public final GenericHID unsafeJoystick; | ||
|
|
||
| private final Set<Integer> safeDisabledButtons; | ||
| private final Set<Integer> safeDisabledAxes; | ||
| private final Map<Integer, Double> safeScaledAxes; | ||
| private final Map<Integer, Set<Integer>> safeDisabledPOV; | ||
|
|
||
| /** | ||
| * Creates a new SafeJoystick | ||
| * | ||
| * @param unsafeJoystick The unsafe joystick to wrap | ||
| * @param safeDisabledButtons The buttons that should be disabled in safe-mode | ||
| * @param safeDisabledAxes The axes that should be disabled in safe-mode | ||
| * @param safeScaledAxes The axes that should be scaled in safe-mode (key: axis, value: scale factor) | ||
| * @param safeDisabledPOV The POVs that should be disabled in safe-mode (key: POV, value: set of disabled values) | ||
| */ | ||
| public SafeJoystick(GenericHID unsafeJoystick, Set<Integer> safeDisabledButtons, Set<Integer> safeDisabledAxes, Map<Integer, Double> safeScaledAxes, Map<Integer, Set<Integer>> safeDisabledPOV) { | ||
| this.unsafeJoystick = unsafeJoystick; | ||
| this.safeDisabledButtons = safeDisabledButtons; | ||
| this.safeDisabledAxes = safeDisabledAxes; | ||
| this.safeScaledAxes = safeScaledAxes; | ||
| this.safeDisabledPOV = safeDisabledPOV; | ||
| } | ||
|
|
||
| // All other methods always fall through to these five | ||
|
|
||
| /** | ||
| * Safe version of {@link GenericHID#getRawButton(int)}. | ||
| * | ||
| * @param button The button to read | ||
| * @return The state of the button | ||
| */ | ||
| public boolean getRawButton(int button) { | ||
| if (SafeMode.isEnabled() && safeDisabledButtons.contains(button)) { | ||
| return false; | ||
| } else { | ||
| return unsafeJoystick.getRawButton(button); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Safe version of {@link GenericHID#getRawButtonPressed(int)}. | ||
| * | ||
| * @param button The button to read | ||
| * @return Whether the button was pressed since the last check | ||
| */ | ||
| public boolean getRawButtonPressed(int button) { | ||
| if (SafeMode.isEnabled() && safeDisabledButtons.contains(button)) { | ||
| return false; | ||
| } else { | ||
| return unsafeJoystick.getRawButtonPressed(button); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Safe version of {@link GenericHID#getRawButtonReleased(int)}. | ||
| * | ||
| * @param button The button to read | ||
| * @return Whether the button was released since the last check | ||
| */ | ||
| public boolean getRawButtonReleased(int button) { | ||
| if (SafeMode.isEnabled() && safeDisabledButtons.contains(button)) { | ||
| return false; | ||
| } else { | ||
| return unsafeJoystick.getRawButtonReleased(button); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Safe version of {@link GenericHID#getRawAxis(int)}. | ||
| * | ||
| * @param axis The axis to read | ||
| * @return The value of the axis | ||
| */ | ||
| public double getRawAxis(int axis) { | ||
| if (SafeMode.isEnabled() && safeDisabledAxes.contains(axis)) { | ||
| return 0; | ||
| } else if (SafeMode.isEnabled() && safeScaledAxes.containsKey(axis)) { | ||
| return unsafeJoystick.getRawAxis(axis) * safeScaledAxes.get(axis); | ||
| } else { | ||
| return unsafeJoystick.getRawAxis(axis); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Safe version of {@link GenericHID#getPOV(int)}. | ||
| * | ||
| * @param pov The POV to read | ||
| * @return The value of the POV | ||
| */ | ||
| public double getPOV(int pov) { | ||
| if (SafeMode.isEnabled() && safeDisabledPOV.containsKey(pov) && safeDisabledPOV.get(pov).contains(unsafeJoystick.getPOV(pov))) { | ||
| return -1; | ||
| } else { | ||
| return unsafeJoystick.getPOV(pov); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.