Class Joystick

java.lang.Object
org.wpilib.driverstation.Joystick
All Implemented Interfaces:
HIDDevice

public class Joystick extends Object implements HIDDevice
Handle input from Flight Joysticks connected to the Driver Station.

This class handles standard input that comes from the Driver Station. Each time a value is requested the most recent value is returned. There is a single class instance for each joystick and the mapping of ports to hardware buttons depends on the code in the Driver Station.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Represents an analog axis on a joystick.
    static enum 
    Represents a digital button on a joystick.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte
    Default throttle axis channel.
    static final byte
    Default twist axis channel.
    static final byte
    Default X axis channel.
    static final byte
    Default Y axis channel.
    static final byte
    Default Z axis channel.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Joystick(int port)
    Construct an instance of a joystick.
    Construct an instance of a joystick with a GenericHID object.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Get the direction of the vector formed by the joystick and its origin in degrees.
    double
    Get the direction of the vector formed by the joystick and its origin in radians.
    Get the underlying GenericHID object.
    double
    Get the magnitude of the vector formed by the joystick's current position relative to its origin.
    Get the angle in degrees of the default POV on the HID.
    getPOV(int pov)
    Get the angle in degrees of a POV on the HID.
    double
    getRawAxis(int axis)
    Get the value of the axis.
    boolean
    getRawButton(int button)
    Get the button value (starting at button 1).
    boolean
    getRawButtonPressed(int button)
    Whether the button was pressed since the last check.
    boolean
    Whether the button was released since the last check.
    final double
    Get the throttle value of the current joystick.
    int
    Get the channel currently associated with the throttle axis.
    boolean
    Read the state of the top button on the joystick.
    boolean
    Whether the top button was pressed since the last check.
    boolean
    Whether the top button was released since the last check.
    boolean
    Read the state of the trigger on the joystick.
    boolean
    Whether the trigger was pressed since the last check.
    boolean
    Whether the trigger was released since the last check.
    final double
    Get the twist value of the current joystick.
    int
    Get the channel currently associated with the twist axis.
    final double
    Get the X value of the joystick.
    int
    Get the channel currently associated with the X axis.
    final double
    Get the Y value of the joystick.
    int
    Get the channel currently associated with the Y axis.
    final double
    Get the z position of the HID.
    int
    Get the channel currently associated with the Z axis.
    void
    setThrottleChannel(int channel)
    Set the channel associated with the throttle axis.
    void
    setTwistChannel(int channel)
    Set the channel associated with the twist axis.
    void
    setXChannel(int channel)
    Set the channel associated with the X axis.
    void
    setYChannel(int channel)
    Set the channel associated with the Y axis.
    void
    setZChannel(int channel)
    Set the channel associated with the Z axis.
    top(EventLoop loop)
    Constructs an event instance around the top button's digital signal.
    Constructs an event instance around the trigger button's digital signal.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_X_CHANNEL

      public static final byte DEFAULT_X_CHANNEL
      Default X axis channel.
      See Also:
    • DEFAULT_Y_CHANNEL

      public static final byte DEFAULT_Y_CHANNEL
      Default Y axis channel.
      See Also:
    • DEFAULT_Z_CHANNEL

      public static final byte DEFAULT_Z_CHANNEL
      Default Z axis channel.
      See Also:
    • DEFAULT_TWIST_CHANNEL

      public static final byte DEFAULT_TWIST_CHANNEL
      Default twist axis channel.
      See Also:
    • DEFAULT_THROTTLE_CHANNEL

      public static final byte DEFAULT_THROTTLE_CHANNEL
      Default throttle axis channel.
      See Also:
  • Constructor Details

    • Joystick

      public Joystick(int port)
      Construct an instance of a joystick.
      Parameters:
      port - The port index on the Driver Station that the joystick is plugged into.
    • Joystick

      public Joystick(GenericHID hid)
      Construct an instance of a joystick with a GenericHID object.
      Parameters:
      hid - The GenericHID object to use for this joystick.
  • Method Details

    • getHID

      public GenericHID getHID()
      Get the underlying GenericHID object.
      Specified by:
      getHID in interface HIDDevice
      Returns:
      the wrapped GenericHID object
    • setXChannel

      public void setXChannel(int channel)
      Set the channel associated with the X axis.
      Parameters:
      channel - The channel to set the axis to.
    • setYChannel

      public void setYChannel(int channel)
      Set the channel associated with the Y axis.
      Parameters:
      channel - The channel to set the axis to.
    • setZChannel

      public void setZChannel(int channel)
      Set the channel associated with the Z axis.
      Parameters:
      channel - The channel to set the axis to.
    • setThrottleChannel

      public void setThrottleChannel(int channel)
      Set the channel associated with the throttle axis.
      Parameters:
      channel - The channel to set the axis to.
    • setTwistChannel

      public void setTwistChannel(int channel)
      Set the channel associated with the twist axis.
      Parameters:
      channel - The channel to set the axis to.
    • getXChannel

      public int getXChannel()
      Get the channel currently associated with the X axis.
      Returns:
      The channel for the axis.
    • getYChannel

      public int getYChannel()
      Get the channel currently associated with the Y axis.
      Returns:
      The channel for the axis.
    • getZChannel

      public int getZChannel()
      Get the channel currently associated with the Z axis.
      Returns:
      The channel for the axis.
    • getTwistChannel

      public int getTwistChannel()
      Get the channel currently associated with the twist axis.
      Returns:
      The channel for the axis.
    • getThrottleChannel

      public int getThrottleChannel()
      Get the channel currently associated with the throttle axis.
      Returns:
      The channel for the axis.
    • getRawButton

      public boolean getRawButton(int button)
      Get the button value (starting at button 1).

      The buttons are returned in a single 16 bit value with one bit representing the state of each button. The appropriate button is returned as a boolean value.

      Parameters:
      button - The button number to be read (starting at 1)
      Returns:
      The state of the button
    • getRawButtonPressed

      public boolean getRawButtonPressed(int button)
      Whether the button was pressed since the last check. Button indexes begin at 1.
      Parameters:
      button - The button index, beginning at 1.
      Returns:
      Whether the button was pressed since the last check.
    • getRawButtonReleased

      public boolean getRawButtonReleased(int button)
      Whether the button was released since the last check. Button indexes begin at 1.
      Parameters:
      button - The button index, beginning at 1.
      Returns:
      Whether the button was released since the last check.
    • getRawAxis

      public double getRawAxis(int axis)
      Get the value of the axis.
      Parameters:
      axis - The axis to read, starting at 0.
      Returns:
      The value of the axis.
    • getPOV

      public POVDirection getPOV(int pov)
      Get the angle in degrees of a POV on the HID.

      The POV angles start at 0 in the up direction, and increase clockwise (e.g. right is 90, upper-left is 315).

      Parameters:
      pov - The index of the POV to read, starting at 0.
      Returns:
      the angle of the POV
    • getPOV

      public POVDirection getPOV()
      Get the angle in degrees of the default POV on the HID.

      The POV angles start at 0 in the up direction, and increase clockwise (e.g. right is 90, upper-left is 315).

      Returns:
      the angle of the POV
    • getX

      public final double getX()
      Get the X value of the joystick. This depends on the mapping of the joystick connected to the current port. On most joysticks, positive is to the right.
      Returns:
      The X value of the joystick.
    • getY

      public final double getY()
      Get the Y value of the joystick. This depends on the mapping of the joystick connected to the current port. On most joysticks, positive is to the back.
      Returns:
      The Y value of the joystick.
    • getZ

      public final double getZ()
      Get the z position of the HID.
      Returns:
      the z position
    • getTwist

      public final double getTwist()
      Get the twist value of the current joystick. This depends on the mapping of the joystick connected to the current port.
      Returns:
      The Twist value of the joystick.
    • getThrottle

      public final double getThrottle()
      Get the throttle value of the current joystick. This depends on the mapping of the joystick connected to the current port.
      Returns:
      The Throttle value of the joystick.
    • getTrigger

      public boolean getTrigger()
      Read the state of the trigger on the joystick.
      Returns:
      The state of the trigger.
    • getTriggerPressed

      public boolean getTriggerPressed()
      Whether the trigger was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getTriggerReleased

      public boolean getTriggerReleased()
      Whether the trigger was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • trigger

      public BooleanEvent trigger(EventLoop loop)
      Constructs an event instance around the trigger button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the trigger button's digital signal attached to the given loop.
    • getTop

      public boolean getTop()
      Read the state of the top button on the joystick.
      Returns:
      The state of the top button.
    • getTopPressed

      public boolean getTopPressed()
      Whether the top button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getTopReleased

      public boolean getTopReleased()
      Whether the top button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • top

      public BooleanEvent top(EventLoop loop)
      Constructs an event instance around the top button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the top button's digital signal attached to the given loop.
    • getMagnitude

      public double getMagnitude()
      Get the magnitude of the vector formed by the joystick's current position relative to its origin.
      Returns:
      The magnitude of the direction vector
    • getDirectionRadians

      public double getDirectionRadians()
      Get the direction of the vector formed by the joystick and its origin in radians. 0 is forward and clockwise is positive. (Straight right is π/2.)
      Returns:
      The direction of the vector in radians
    • getDirectionDegrees

      public double getDirectionDegrees()
      Get the direction of the vector formed by the joystick and its origin in degrees. 0 is forward and clockwise is positive. (Straight right is 90.)
      Returns:
      The direction of the vector in degrees