Package frc.spectrumLib.gamepads
Class Gamepad
java.lang.Object
frc.spectrumLib.gamepads.Gamepad
- All Implemented Interfaces:
Subsystem
Abstract base class for robot gamepad (Xbox-compatible) controllers.
Wraps a WPILib CommandXboxController and exposes:
- Pre-built
Triggerfields for every button, bumper, trigger, stick-click, and D-pad direction. - Composite modifier triggers (
noBumpers,bothTriggers, etc.) for chord-based bindings. - Exponential-curve axis helpers (
leftStickCurve, etc.) for driver-tuned response. - Stick-direction utilities (
getLeftStickDirection(),chooseCardinalDirections()) for field-relative driving. - Rumble commands (
rumbleCommand(double, double, double)) for haptic feedback.
Subclass this once per operator role (pilot, copilot) and override #setupStates() and
#setupDefaultCommand() to bind subsystem commands to triggers.
When Gamepad.Config.isAttached() returns false, all triggers remain permanently
false and axis reads return 0.0.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classConfiguration for aGamepadinstance, defining the DriverStation USB port, axis curve parameters, and whether the controller should be used on this robot.static enumComparison type used by axis-basedTriggerfactories such asleftYTrigger(Threshold, double). -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected TriggerTrigger for the A (cross) face button.protected TriggerTrigger active during the autonomous period.protected TriggerTrigger for the B (circle) face button.Active when both bumpers are pressed simultaneously.Active when both analog triggers are pressed simultaneously.protected TriggerTrigger active while the robot is disabled.protected TriggerTrigger for D-pad down.static final TriggerA trigger that is alwaysfalse; used as a safe default before hardware is ready.protected TriggerTrigger for the left bumper (LB).Active when only the left bumper is pressed.protected TriggerTrigger for D-pad left (including up-left and down-left diagonals).protected TriggerTrigger for pressing the left analog stick (L3).protected final ExpCurveExponential response curve applied to both left-stick axes.protected TriggerTrigger active when the left stick X-axis exceeds the configured deadzone.protected TriggerTrigger active when the left stick Y-axis exceeds the configured deadzone.protected TriggerTrigger active when the left analog trigger exceeds the configured deadzone threshold.Active when only the left trigger is pressed.Active when neither bumper is pressed.Active when no bumpers and no triggers are pressed (no modifier held).Active when neither analog trigger is pressed.protected TriggerTrigger for the right bumper (RB).Active when only the right bumper is pressed.protected TriggerTrigger for D-pad right (including up-right and down-right diagonals).protected TriggerTrigger for pressing the right analog stick (R3).protected final ExpCurveExponential response curve applied to both right-stick axes.protected TriggerTrigger active when the right stick X-axis exceeds the configured deadzone.protected TriggerTrigger active when the right stick Y-axis exceeds the configured deadzone.protected TriggerTrigger active when the right analog trigger exceeds the configured deadzone threshold.Active when only the right trigger is pressed.protected TriggerTrigger for the Select / Back / View button.protected TriggerTrigger for the Start / Menu button.protected TriggerTrigger active during the teleoperated period.protected TriggerTrigger active during the test mode period.protected final ExpCurveExponential response curve applied to both analog trigger axes.protected TriggerTrigger for D-pad up.protected TriggerTrigger for the X (square) face button.protected TriggerTrigger for the Y (triangle) face button. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedGamepad(Gamepad.Config config) Constructs a Gamepad object with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptiondoubleGet proper stick angles for each alliancevoidDetects whether the gamepad has been connected since power-on and prints a one-time confirmation message.doubleSnaps the right stick to the nearest 45° increment using the Blue-alliance field orientation (forward = 0 rad).protected GenericHIDgetHID()Returns the underlyingGenericHIDfor low-level access, ornullif not attached.doubleSnaps the left-stick direction to the nearest cardinal angle (0, ±π/2, π radians).Exponential response curve applied to both left-stick axes.Returns the current direction of the left stick as aRotation2d.doubleReturns the Euclidean magnitude of the left stick deflection (0–√2 before curve, 0–1 after typical scalar).protected doubleReturns the raw left-trigger axis value (0–1), or0.0if not connected.protected doublegetLeftX()Returns the left-stick X axis value, or0.0if not connected.protected doublegetLeftY()Returns the left-stick Y axis value, or0.0if not connected.doubleFlips the stick direction for the red alliance.doubleSnaps the right-stick direction to the nearest cardinal angle (0, ±π/2, π radians).Exponential response curve applied to both right-stick axes.Returns the current direction of the right stick as aRotation2d.doubleReturns the Euclidean magnitude of the right stick deflection.protected doubleReturns the raw right-trigger axis value (0–1), or0.0if not connected.protected doubleReturns the right-stick X axis value, or0.0if not connected.protected doubleReturns the right-stick Y axis value, or0.0if not connected.protected GenericHIDReturns the underlyingGenericHIDfor rumble output, ornullif not connected.Exponential response curve applied to both analog trigger axes.protected doublegetTwist()Returns the differential trigger value (rightTrigger - leftTrigger), useful as a single "twist" axis for field-relative rotation commands.booleanReturns whether the physical gamepad is currently connected to the DriverStation.leftStick(double threshold) Returns aTriggerthat fires when either left-stick axis exceeds the given absolute threshold.leftXTrigger(Gamepad.Threshold t, double threshold) Returns aTriggerthat fires based on the left-stick X axis and the given threshold comparison.leftYTrigger(Gamepad.Threshold t, double threshold) Returns aTriggerthat fires based on the left-stick Y axis and the given threshold comparison.voidperiodic()voidResets the controller configuration state so that the nextconfigure()call will re-detect connection and re-apply button bindings.rightStick(double threshold) Returns aTriggerthat fires when either right-stick axis exceeds the given absolute threshold.rightXTrigger(Gamepad.Threshold t, double threshold) Returns aTriggerthat fires based on the right-stick X axis and the given threshold comparison.rightYTrigger(Gamepad.Threshold t, double threshold) Returns aTriggerthat fires based on the right-stick Y axis and the given threshold comparison.rumbleCommand(double intensity, double durationSeconds) Overloaded method for rumbleCommand that allows for the same intensity on both rumble motors.rumbleCommand(double leftIntensity, double rightIntensity, double durationSeconds) Command that can be used to rumble the pilot controller.rumbleCommand(Command command) Returns a new Command object that combines the given command with a rumble command.voidrumbleController(double leftIntensity, double rightIntensity) Immediately sets the left and right rumble motor intensities.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface edu.wpi.first.wpilibj2.command.Subsystem
defer, getCurrentCommand, getDefaultCommand, getName, idle, register, removeDefaultCommand, run, runEnd, runOnce, setDefaultCommand, simulationPeriodic, startEnd, startRun
-
Field Details
-
kFalse
A trigger that is alwaysfalse; used as a safe default before hardware is ready. -
A
Trigger for the A (cross) face button. -
B
Trigger for the B (circle) face button. -
X
Trigger for the X (square) face button. -
Y
Trigger for the Y (triangle) face button. -
leftBumper
Trigger for the left bumper (LB). -
rightBumper
Trigger for the right bumper (RB). -
leftTrigger
Trigger active when the left analog trigger exceeds the configured deadzone threshold. -
rightTrigger
Trigger active when the right analog trigger exceeds the configured deadzone threshold. -
leftStickClick
Trigger for pressing the left analog stick (L3). -
rightStickClick
Trigger for pressing the right analog stick (R3). -
start
Trigger for the Start / Menu button. -
select
Trigger for the Select / Back / View button. -
upDpad
Trigger for D-pad up. -
downDpad
Trigger for D-pad down. -
leftDpad
Trigger for D-pad left (including up-left and down-left diagonals). -
rightDpad
Trigger for D-pad right (including up-right and down-right diagonals). -
leftStickY
Trigger active when the left stick Y-axis exceeds the configured deadzone. -
leftStickX
Trigger active when the left stick X-axis exceeds the configured deadzone. -
rightStickY
Trigger active when the right stick Y-axis exceeds the configured deadzone. -
rightStickX
Trigger active when the right stick X-axis exceeds the configured deadzone. -
noBumpers
Active when neither bumper is pressed. -
leftBumperOnly
Active when only the left bumper is pressed. -
rightBumperOnly
Active when only the right bumper is pressed. -
bothBumpers
Active when both bumpers are pressed simultaneously. -
noTriggers
Active when neither analog trigger is pressed. -
leftTriggerOnly
Active when only the left trigger is pressed. -
rightTriggerOnly
Active when only the right trigger is pressed. -
bothTriggers
Active when both analog triggers are pressed simultaneously. -
noModifiers
Active when no bumpers and no triggers are pressed (no modifier held). -
leftStickCurve
Exponential response curve applied to both left-stick axes. -
rightStickCurve
Exponential response curve applied to both right-stick axes. -
triggersCurve
Exponential response curve applied to both analog trigger axes. -
teleop
Trigger active during the teleoperated period. -
autoMode
Trigger active during the autonomous period. -
testMode
Trigger active during the test mode period. -
disabled
Trigger active while the robot is disabled.
-
-
Constructor Details
-
Gamepad
Constructs a Gamepad object with the specified configuration.- Parameters:
config- the configuration object containing settings for the gamepadThe constructor initializes the following: - Superclass with port and attachment status from the configuration. - Curve objects for left stick, right stick, and triggers using exponential curves. - If the gamepad is attached, initializes the Xbox controller and its buttons, triggers, sticks, and D-pad.
-
-
Method Details
-
periodic
public void periodic() -
configure
public void configure()Detects whether the gamepad has been connected since power-on and prints a one-time confirmation message. Also raises aAlertwhenever the controller is disconnected. Called automatically byperiodic(). -
resetConfig
public void resetConfig()Resets the controller configuration state so that the nextconfigure()call will re-detect connection and re-apply button bindings. Should be paired withCommandScheduler.getInstance().clearButtons(). -
getLeftStickDirection
Returns the current direction of the left stick as aRotation2d. Zero points up (toward positive Y), and 90° points to the left (toward negative X). The last non-zero direction is retained when the stick is released.- Returns:
- left-stick direction; zero-up / 90-left convention
-
getRightStickDirection
Returns the current direction of the right stick as aRotation2d. The last non-zero direction is retained when the stick is released.- Returns:
- right-stick direction
-
getLeftStickCardinals
public double getLeftStickCardinals()Snaps the left-stick direction to the nearest cardinal angle (0, ±π/2, π radians).- Returns:
- the snapped angle in radians
-
getRightStickCardinals
public double getRightStickCardinals()Snaps the right-stick direction to the nearest cardinal angle (0, ±π/2, π radians).- Returns:
- the snapped angle in radians
-
getLeftStickMagnitude
public double getLeftStickMagnitude()Returns the Euclidean magnitude of the left stick deflection (0–√2 before curve, 0–1 after typical scalar).- Returns:
- left-stick vector magnitude
-
getRightStickMagnitude
public double getRightStickMagnitude()Returns the Euclidean magnitude of the right stick deflection.- Returns:
- right-stick vector magnitude
-
chooseCardinalDirections
public double chooseCardinalDirections()Get proper stick angles for each alliance- Returns:
-
getBlueAllianceStickCardinals
public double getBlueAllianceStickCardinals()Snaps the right stick to the nearest 45° increment using the Blue-alliance field orientation (forward = 0 rad).- Returns:
- the snapped heading in radians for the Blue alliance perspective
-
getRedAllianceStickCardinals
public double getRedAllianceStickCardinals()Flips the stick direction for the red alliance.- Returns:
-
leftYTrigger
Returns aTriggerthat fires based on the left-stick Y axis and the given threshold comparison.- Parameters:
t- theGamepad.Thresholdcomparison typethreshold- the value to compare against- Returns:
- trigger based on the left Y axis
-
leftXTrigger
Returns aTriggerthat fires based on the left-stick X axis and the given threshold comparison.- Parameters:
t- theGamepad.Thresholdcomparison typethreshold- the value to compare against- Returns:
- trigger based on the left X axis
-
rightYTrigger
Returns aTriggerthat fires based on the right-stick Y axis and the given threshold comparison.- Parameters:
t- theGamepad.Thresholdcomparison typethreshold- the value to compare against- Returns:
- trigger based on the right Y axis
-
rightXTrigger
Returns aTriggerthat fires based on the right-stick X axis and the given threshold comparison.- Parameters:
t- theGamepad.Thresholdcomparison typethreshold- the value to compare against- Returns:
- trigger based on the right X axis
-
rightStick
Returns aTriggerthat fires when either right-stick axis exceeds the given absolute threshold.- Parameters:
threshold- minimum absolute axis value to activate the trigger- Returns:
- trigger active when the right stick is deflected beyond the threshold
-
leftStick
Returns aTriggerthat fires when either left-stick axis exceeds the given absolute threshold.- Parameters:
threshold- minimum absolute axis value to activate the trigger- Returns:
- trigger active when the left stick is deflected beyond the threshold
-
rumbleCommand
Command that can be used to rumble the pilot controller. The intensity should be a value between 0 and 1, where 0 is no rumble and 1 is full rumble. The duration of the rumble is specified in seconds.- Parameters:
leftIntensity- the intensity of the left rumble motor (0 to 1)rightIntensity- the intensity of the right rumble motor (0 to 1)durationSeconds- the duration of the rumble in seconds- Returns:
- a Command object that can be used to rumble the controller with the specified intensities and duration
-
rumbleCommand
Overloaded method for rumbleCommand that allows for the same intensity on both rumble motors. The duration of the rumble is specified in seconds. The intensity should be a value between 0 and 1, where 0 is no rumble and 1 is full rumble.- Parameters:
intensity- the intensity of the rumble (0 to 1)durationSeconds- the duration of the rumble in seconds- Returns:
- a Command object that can be used to rumble the controller with the specified intensity and duration
-
rumbleCommand
Returns a new Command object that combines the given command with a rumble command. The rumble command has a rumble strength of 1 and a duration of 0.5 seconds. The name of the returned command is set to the name of the given command.- Parameters:
command- the command to be combined with the rumble command- Returns:
- a new Command object with rumble command
-
isConnected
public boolean isConnected()Returns whether the physical gamepad is currently connected to the DriverStation.- Returns:
trueif the controller is attached and reports as connected
-
getRightTriggerAxis
protected double getRightTriggerAxis()Returns the raw right-trigger axis value (0–1), or0.0if not connected.- Returns:
- right-trigger axis value
-
getLeftTriggerAxis
protected double getLeftTriggerAxis()Returns the raw left-trigger axis value (0–1), or0.0if not connected.- Returns:
- left-trigger axis value
-
getTwist
protected double getTwist()Returns the differential trigger value (rightTrigger - leftTrigger), useful as a single "twist" axis for field-relative rotation commands.- Returns:
- twist value in the range [-1, 1]
-
getLeftX
protected double getLeftX()Returns the left-stick X axis value, or0.0if not connected.- Returns:
- left X axis value in the range [-1, 1]
-
getLeftY
protected double getLeftY()Returns the left-stick Y axis value, or0.0if not connected.- Returns:
- left Y axis value in the range [-1, 1] (negative = up on most gamepads)
-
getRightX
protected double getRightX()Returns the right-stick X axis value, or0.0if not connected.- Returns:
- right X axis value in the range [-1, 1]
-
getRightY
protected double getRightY()Returns the right-stick Y axis value, or0.0if not connected.- Returns:
- right Y axis value in the range [-1, 1] (negative = up on most gamepads)
-
getHID
Returns the underlyingGenericHIDfor low-level access, ornullif not attached.- Returns:
- the raw HID device, or
null
-
getRumbleHID
Returns the underlyingGenericHIDfor rumble output, ornullif not connected.- Returns:
- the raw HID device (only when connected), or
null
-
rumbleController
public void rumbleController(double leftIntensity, double rightIntensity) Immediately sets the left and right rumble motor intensities. UserumbleCommand(double, double, double)for timed rumble sequences.- Parameters:
leftIntensity- left rumble motor intensity (0–1)rightIntensity- right rumble motor intensity (0–1)
-
getLeftStickCurve
Exponential response curve applied to both left-stick axes. -
getRightStickCurve
Exponential response curve applied to both right-stick axes. -
getTriggersCurve
Exponential response curve applied to both analog trigger axes.
-