Class Gamepad

java.lang.Object
frc.spectrumLib.gamepads.Gamepad
All Implemented Interfaces:
Subsystem
Direct Known Subclasses:
Operator, Pilot

public abstract class Gamepad extends Object implements Subsystem
Abstract base class for robot gamepad (Xbox-compatible) controllers.

Wraps a WPILib CommandXboxController and exposes:

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.

  • Field Details

    • kFalse

      public static final Trigger kFalse
      A trigger that is always false; used as a safe default before hardware is ready.
    • A

      protected Trigger A
      Trigger for the A (cross) face button.
    • B

      protected Trigger B
      Trigger for the B (circle) face button.
    • X

      protected Trigger X
      Trigger for the X (square) face button.
    • Y

      protected Trigger Y
      Trigger for the Y (triangle) face button.
    • leftBumper

      protected Trigger leftBumper
      Trigger for the left bumper (LB).
    • rightBumper

      protected Trigger rightBumper
      Trigger for the right bumper (RB).
    • leftTrigger

      protected Trigger leftTrigger
      Trigger active when the left analog trigger exceeds the configured deadzone threshold.
    • rightTrigger

      protected Trigger rightTrigger
      Trigger active when the right analog trigger exceeds the configured deadzone threshold.
    • leftStickClick

      protected Trigger leftStickClick
      Trigger for pressing the left analog stick (L3).
    • rightStickClick

      protected Trigger rightStickClick
      Trigger for pressing the right analog stick (R3).
    • start

      protected Trigger start
      Trigger for the Start / Menu button.
    • select

      protected Trigger select
      Trigger for the Select / Back / View button.
    • upDpad

      protected Trigger upDpad
      Trigger for D-pad up.
    • downDpad

      protected Trigger downDpad
      Trigger for D-pad down.
    • leftDpad

      protected Trigger leftDpad
      Trigger for D-pad left (including up-left and down-left diagonals).
    • rightDpad

      protected Trigger rightDpad
      Trigger for D-pad right (including up-right and down-right diagonals).
    • leftStickY

      protected Trigger leftStickY
      Trigger active when the left stick Y-axis exceeds the configured deadzone.
    • leftStickX

      protected Trigger leftStickX
      Trigger active when the left stick X-axis exceeds the configured deadzone.
    • rightStickY

      protected Trigger rightStickY
      Trigger active when the right stick Y-axis exceeds the configured deadzone.
    • rightStickX

      protected Trigger rightStickX
      Trigger active when the right stick X-axis exceeds the configured deadzone.
    • noBumpers

      public Trigger noBumpers
      Active when neither bumper is pressed.
    • leftBumperOnly

      public Trigger leftBumperOnly
      Active when only the left bumper is pressed.
    • rightBumperOnly

      public Trigger rightBumperOnly
      Active when only the right bumper is pressed.
    • bothBumpers

      public Trigger bothBumpers
      Active when both bumpers are pressed simultaneously.
    • noTriggers

      public Trigger noTriggers
      Active when neither analog trigger is pressed.
    • leftTriggerOnly

      public Trigger leftTriggerOnly
      Active when only the left trigger is pressed.
    • rightTriggerOnly

      public Trigger rightTriggerOnly
      Active when only the right trigger is pressed.
    • bothTriggers

      public Trigger bothTriggers
      Active when both analog triggers are pressed simultaneously.
    • noModifiers

      public Trigger noModifiers
      Active when no bumpers and no triggers are pressed (no modifier held).
    • leftStickCurve

      protected final ExpCurve leftStickCurve
      Exponential response curve applied to both left-stick axes.
    • rightStickCurve

      protected final ExpCurve rightStickCurve
      Exponential response curve applied to both right-stick axes.
    • triggersCurve

      protected final ExpCurve triggersCurve
      Exponential response curve applied to both analog trigger axes.
    • teleop

      protected Trigger teleop
      Trigger active during the teleoperated period.
    • autoMode

      protected Trigger autoMode
      Trigger active during the autonomous period.
    • testMode

      protected Trigger testMode
      Trigger active during the test mode period.
    • disabled

      protected Trigger disabled
      Trigger active while the robot is disabled.
  • Constructor Details

    • Gamepad

      protected Gamepad(Gamepad.Config config)
      Constructs a Gamepad object with the specified configuration.
      Parameters:
      config - the configuration object containing settings for the gamepad

      The 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()
      Specified by:
      periodic in interface Subsystem
    • configure

      public void configure()
      Detects whether the gamepad has been connected since power-on and prints a one-time confirmation message. Also raises a Alert whenever the controller is disconnected. Called automatically by periodic().
    • resetConfig

      public void resetConfig()
      Resets the controller configuration state so that the next configure() call will re-detect connection and re-apply button bindings. Should be paired with CommandScheduler.getInstance().clearButtons().
    • getLeftStickDirection

      public Rotation2d getLeftStickDirection()
      Returns the current direction of the left stick as a Rotation2d. 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

      public Rotation2d getRightStickDirection()
      Returns the current direction of the right stick as a Rotation2d. 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

      public Trigger leftYTrigger(Gamepad.Threshold t, double threshold)
      Returns a Trigger that fires based on the left-stick Y axis and the given threshold comparison.
      Parameters:
      t - the Gamepad.Threshold comparison type
      threshold - the value to compare against
      Returns:
      trigger based on the left Y axis
    • leftXTrigger

      public Trigger leftXTrigger(Gamepad.Threshold t, double threshold)
      Returns a Trigger that fires based on the left-stick X axis and the given threshold comparison.
      Parameters:
      t - the Gamepad.Threshold comparison type
      threshold - the value to compare against
      Returns:
      trigger based on the left X axis
    • rightYTrigger

      public Trigger rightYTrigger(Gamepad.Threshold t, double threshold)
      Returns a Trigger that fires based on the right-stick Y axis and the given threshold comparison.
      Parameters:
      t - the Gamepad.Threshold comparison type
      threshold - the value to compare against
      Returns:
      trigger based on the right Y axis
    • rightXTrigger

      public Trigger rightXTrigger(Gamepad.Threshold t, double threshold)
      Returns a Trigger that fires based on the right-stick X axis and the given threshold comparison.
      Parameters:
      t - the Gamepad.Threshold comparison type
      threshold - the value to compare against
      Returns:
      trigger based on the right X axis
    • rightStick

      public Trigger rightStick(double threshold)
      Returns a Trigger that 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

      public Trigger leftStick(double threshold)
      Returns a Trigger that 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

      public Command rumbleCommand(double leftIntensity, double rightIntensity, double durationSeconds)
      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

      public Command rumbleCommand(double intensity, double durationSeconds)
      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

      public Command rumbleCommand(Command command)
      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:
      true if the controller is attached and reports as connected
    • getRightTriggerAxis

      protected double getRightTriggerAxis()
      Returns the raw right-trigger axis value (0–1), or 0.0 if not connected.
      Returns:
      right-trigger axis value
    • getLeftTriggerAxis

      protected double getLeftTriggerAxis()
      Returns the raw left-trigger axis value (0–1), or 0.0 if 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, or 0.0 if not connected.
      Returns:
      left X axis value in the range [-1, 1]
    • getLeftY

      protected double getLeftY()
      Returns the left-stick Y axis value, or 0.0 if 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, or 0.0 if not connected.
      Returns:
      right X axis value in the range [-1, 1]
    • getRightY

      protected double getRightY()
      Returns the right-stick Y axis value, or 0.0 if not connected.
      Returns:
      right Y axis value in the range [-1, 1] (negative = up on most gamepads)
    • getHID

      protected GenericHID getHID()
      Returns the underlying GenericHID for low-level access, or null if not attached.
      Returns:
      the raw HID device, or null
    • getRumbleHID

      protected GenericHID getRumbleHID()
      Returns the underlying GenericHID for rumble output, or null if 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. Use rumbleCommand(double, double, double) for timed rumble sequences.
      Parameters:
      leftIntensity - left rumble motor intensity (0–1)
      rightIntensity - right rumble motor intensity (0–1)
    • getLeftStickCurve

      public ExpCurve getLeftStickCurve()
      Exponential response curve applied to both left-stick axes.
    • getRightStickCurve

      public ExpCurve getRightStickCurve()
      Exponential response curve applied to both right-stick axes.
    • getTriggersCurve

      public ExpCurve getTriggersCurve()
      Exponential response curve applied to both analog trigger axes.