Joystick

class wpilib.Joystick(port)[source]

Bases: wpilib.interfaces.GenericHID

Handle input from standard 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.

Construct an instance of a joystick.

The joystick index is the USB port on the Driver Station.

This constructor is intended for use by subclasses to configure the number of constants for axes and buttons.

Parameters:port (int) – The port on the Driver Station that the joystick is plugged into.
class Axis[source]

Bases: enum.IntEnum

Represents an analog axis on a joystick

kNumAxes = 5
kThrottle = 4
kTwist = 3
kX = 0
kY = 1
kZ = 2
class AxisType[source]

Bases: enum.IntEnum

Represents an analog axis on a joystick.

kThrottle = 4
kTwist = 3
kX = 0
kY = 1
kZ = 2
Button

alias of ButtonType

class ButtonType[source]

Bases: enum.IntEnum

Represents a digital button on the Joystick

kTop = 2
kTrigger = 1
getAxis(axis)[source]

For the current joystick, return the axis determined by the argument.

This is for cases where the joystick axis is returned programmatically, otherwise one of the previous functions would be preferable (for example getX()).

Parameters:axis (Joystick.AxisType) – The axis to read.
Return type:float
Returns:The value of the axis.
..deprecated: 2018.0.0
Use the more specific axis getter functions.
getAxisChannel(axis)[source]

Get the channel currently associated with the specified axis.

Parameters:axis (int) – The axis to look up the channel for.
Return type:int
Returns:The channel for the axis.
..deprecated:: 2018.0.0
Use the more specific axis channel getter functions
getButton(button)[source]

Get buttons based on an enumerated type.

The button type will be looked up in the list of buttons and then read.

Parameters:button (ButtonType) – The type of button to read.
Return type:bool
Returns:The state of the button.
..deprecated: 2018.0.0
Use Button enum values instead of ButtonType
getDirectionDegrees()[source]

Get the direction of the vector formed by the joystick and its origin in degrees.

Return type:float
Returns:The direction of the vector in degrees
getDirectionRadians()[source]

Get the direction of the vector formed by the joystick and its origin in radians.

Return type:float
Returns:The direction of the vector in radians
getMagnitude()[source]

Get the magnitude of the direction vector formed by the joystick’s current position relative to its origin.

Return type:float
Returns:The magnitude of the direction vector
getThrottle()[source]

Get the throttle value of the current joystick.

This depends on the mapping of the joystick connected to the current port.

Return type:float
Returns:The Throttle value of the joystick.
getThrottleChannel()[source]

Get the channel currently associated with the Throttle axis

Return type:int
Returns:The channel for the axis
getTop()[source]

Read the state of the top button on the joystick.

Look up which button has been assigned to the top and read its state.

Return type:bool
Returns:The state of the top button.
getTopPressed()[source]

Whether the trigger was pressed since the last check

Returns:Whether the button was pressed since the last check
getTopReleased()[source]

Whether the trigger was released since the last check.

Returns:Whether the button was released since the last check.
getTrigger()[source]

Read the state of the trigger on the joystick.

Look up which button has been assigned to the trigger and read its state.

Return type:bool
Returns:The state of the trigger.
getTriggerPressed()[source]

Whether the trigger was pressed since the last check

Returns:Whether the button was pressed since the last check
getTriggerReleased()[source]

Whether the trigger was released since the last check.

Returns:Whether the button was released since the last check.
getTwist()[source]

Get the twist value of the current joystick.

This depends on the mapping of the joystick connected to the current port.

Return type:float
Returns:The Twist value of the joystick.
getTwistChannel()[source]

Get the channel currently associated with the Twist axis

Return type:int
Returns:The channel for the axis
getX(hand=None)[source]

Get the X value of the joystick.

This depends on the mapping of the joystick connected to the current port.

Parameters:hand – Unused
Return type:float
Returns:The X value of the joystick.
getXChannel()[source]

Get the channel currently associated with the X axis

Return type:int
Returns:The channel for the axis
getY(hand=None)[source]

Get the Y value of the joystick.

This depends on the mapping of the joystick connected to the current port.

Parameters:hand – Unused
Return type:float
Returns:The Y value of the joystick.
getYChannel()[source]

Get the channel currently associated with the Y axis

Return type:int
Returns:The channel for the axis
getZ(hand=None)[source]

Get the Z position of the HID

Parameters:hand – Unused
Return type:float
Returns:the Z position
getZChannel()[source]

Get the channel currently associated with the Z axis

Return type:int
Returns:The channel for the axis
kDefaultThrottleAxis = 3
kDefaultTopButton = 2
kDefaultTriggerButton = 1
kDefaultTwistAxis = 2
kDefaultXAxis = 0
kDefaultYAxis = 1
kDefaultZAxis = 2
setAxisChannel(axis, channel)[source]

Set the channel associated with a specified axis.

Parameters:
  • axis (int) – The axis to set the channel for.
  • channel (int) – The channel to set the axis to.

Deprecated since version 2018.0.0: Use the more specific axis channel setter functions

setThrottleChannel(channel)[source]

Set the channel associated with the Throttle axis.

Parameters:channel (int) – The channel to set the axis to.
setTwistChannel(channel)[source]

Set the channel associated with the Twist axis.

Parameters:channel (int) – The channel to set the axis to.
Return type:int
setXChannel(channel)[source]

Set the channel associated with the X axis.

Parameters:channel (int) – The channel to set the axis to.
setYChannel(channel)[source]

Set the channel associated with the Y axis.

Parameters:channel (int) – The channel to set the axis to.
setZChannel(channel)[source]

Set the channel associated with the Z axis.

Parameters:channel (int) – The channel to set the axis to.