This repository was archived by the owner on Nov 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: dynamic controller switching #88
Draft
tk2217
wants to merge
21
commits into
main
Choose a base branch
from
test-mode-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
38a7052
dynamic controller switching
tk2217 1c600d4
Merge remote-tracking branch 'origin/main' into test-mode-fix
tk2217 aebfe1e
debug
tk2217 1b44d1a
lint
samfreund 6ae1ce6
,
tk2217 144d1cb
istg pls run linting
samfreund c7754b3
more
tk2217 ea3b988
Merge remote-tracking branch 'refs/remotes/origin/test-mode-fix' into…
tk2217 8ff6077
more stuff
tk2217 7ad9e11
logic fix
tk2217 b608c37
spotless
tk2217 52fe790
Merge remote-tracking branch 'origin/main' into test-mode-fix
tk2217 50c9436
istg owen run the linting
samfreund 4521584
warning method
tk2217 1f24cab
Merge remote-tracking branch 'refs/remotes/origin/test-mode-fix' into…
tk2217 63faa92
spotless
tk2217 b19fff1
Oh boy! An update!
MKECyberCheese 3174439
Oh boy! An update!
MKECyberCheese aaa0a13
Oh boy! An update!
MKECyberCheese b86ce4f
Merge remote-tracking branch 'origin/main' into test-mode-fix
tk2217 374e007
Oh boy! An update!
MKECyberCheese 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
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
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
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,154 @@ | ||
| package frc.robot; | ||
|
|
||
| import edu.wpi.first.wpilibj.Alert; | ||
| import edu.wpi.first.wpilibj.DriverStation; | ||
| import edu.wpi.first.wpilibj.Timer; | ||
| import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
| import edu.wpi.first.wpilibj2.command.Commands; | ||
| import edu.wpi.first.wpilibj2.command.button.CommandXboxController; | ||
| import frc.robot.utils.FilteredJoystick; | ||
| import java.util.List; | ||
|
|
||
| public class Controllers { | ||
|
|
||
| private static final ControllerState DEFAULT_CONTROLLER_STATE = | ||
| Robot.isSimulation() ? ControllerState.XBOX : ControllerState.JOYSTICKS; | ||
|
|
||
| private final Alert simulationJoysticksAlert = | ||
| new Alert("Joysticks are connected in simulation", Alert.AlertType.kWarning); | ||
| private final Alert realXboxAlert = | ||
| new Alert("Xbox controller is connected to physical robot", Alert.AlertType.kWarning); | ||
|
|
||
| // Driver joysticks | ||
| public final FilteredJoystick leftJoystick = | ||
| new FilteredJoystick(Constants.IOConstants.kLeftJoystickPort); | ||
| public final FilteredJoystick rightJoystick = | ||
| new FilteredJoystick(Constants.IOConstants.kRightJoystickPort); | ||
|
|
||
| // Operator controller | ||
| public final CommandXboxController controller = | ||
| new CommandXboxController(Constants.IOConstants.kControllerPort); | ||
|
|
||
| private ControllerState controllerState = DEFAULT_CONTROLLER_STATE; | ||
|
|
||
| private double lastControllerConsoleWarning = 0.; | ||
|
|
||
| public Controllers() { | ||
| SmartDashboard.putData("Controllers/Xbox", this.controller.getHID()); | ||
|
|
||
| this.switchController(this.controllerState); | ||
|
|
||
| this.controller | ||
| .start() | ||
| .onTrue(Commands.runOnce(() -> this.switchController(ControllerState.XBOX))); | ||
| this.leftJoystick | ||
| .getButtonNine() | ||
| .onTrue(Commands.runOnce(() -> this.switchController(ControllerState.JOYSTICKS))); | ||
| this.rightJoystick | ||
| .getButtonNine() | ||
| .onTrue(Commands.runOnce(() -> this.switchController(ControllerState.JOYSTICKS))); | ||
| } | ||
|
|
||
| private boolean isConnected(ControllerState state) { | ||
| return switch (state) { | ||
| case XBOX -> this.isXboxConnected(); | ||
| case JOYSTICKS -> this.isAnyJoystickConnected(); | ||
| }; | ||
| } | ||
|
|
||
| private boolean isXboxConnected() { | ||
| return this.controller.isConnected(); | ||
| } | ||
|
|
||
| private boolean isAnyJoystickConnected() { | ||
| return this.leftJoystick.getJoystick().isConnected() | ||
| || this.rightJoystick.getJoystick().isConnected(); | ||
| } | ||
|
|
||
| public boolean useXboxController() { | ||
| return this.controllerState != ControllerState.JOYSTICKS; | ||
| } | ||
|
|
||
| public double getControllerX() { | ||
| if (this.useXboxController()) { | ||
| return controller.getLeftX(); | ||
| } else { | ||
| return leftJoystick.getX(); | ||
| } | ||
| } | ||
|
|
||
| public double getControllerY() { | ||
| if (this.useXboxController()) { | ||
| return controller.getLeftY(); | ||
| } else { | ||
| return leftJoystick.getY(); | ||
| } | ||
| } | ||
|
|
||
| public double getControllerRotation() { | ||
| if (this.useXboxController()) { | ||
| return -controller.getRightX(); | ||
| } else { | ||
| return rightJoystick.getX(); | ||
| } | ||
| } | ||
|
|
||
| public boolean getControllerSlow() { | ||
| if (this.useXboxController()) { | ||
| return controller.rightBumper().getAsBoolean(); | ||
| } else { | ||
| return rightJoystick.getButtonTwo().getAsBoolean(); | ||
| } | ||
| } | ||
|
|
||
| public double getControllerThrottle() { | ||
| if (this.useXboxController()) { | ||
| return 1.0; | ||
| } else { | ||
| return leftJoystick.getThrottle(); | ||
| } | ||
| } | ||
|
|
||
| public void switchController(ControllerState newState) { | ||
| this.controllerState = newState; | ||
| System.out.println("Controller: " + this.controllerState); | ||
| SmartDashboard.putString("Controller", this.controllerState.toString()); | ||
|
|
||
| this.simulationJoysticksAlert.set( | ||
| Robot.isSimulation() && this.controllerState == ControllerState.JOYSTICKS); | ||
| // TODO: Maybe only with fms? | ||
| this.realXboxAlert.set(Robot.isReal() && this.controllerState == ControllerState.XBOX); | ||
| } | ||
|
|
||
| public void updateControllerConnections() { | ||
| final var time = Timer.getTimestamp(); | ||
| if (this.realXboxAlert.get() && this.lastControllerConsoleWarning < time - 15.) { | ||
| this.lastControllerConsoleWarning = time; | ||
| DriverStation.reportWarning( | ||
| "Oops! Looks like you have the Xbox controller selected for drive on a physical robot.", | ||
| false); | ||
| } | ||
|
|
||
| if (!this.isConnected(this.controllerState)) { | ||
| // Try the default first, then try everything else. | ||
| if (this.isConnected(DEFAULT_CONTROLLER_STATE)) { | ||
| this.switchController(DEFAULT_CONTROLLER_STATE); | ||
| } else { | ||
| for (final var controller : ControllerState.VALUES) { | ||
| if (this.isConnected(controller)) { | ||
| this.switchController(controller); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public enum ControllerState { | ||
| XBOX, | ||
| JOYSTICKS, | ||
| ; | ||
|
|
||
| public static final List<ControllerState> VALUES = List.of(values()); | ||
| } | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk how I feel about this calling every single control loop. That feels maybe excessive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can demonstrate it doesn't make an impact tho I'm fine with it.