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.
  • numAxisTypes (int) – The number of axis types.
  • numButtonTypes (int) – The number of button types.
class Axis[source]

Bases: object

Represents an analog axis on a joystick

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

Bases: object

Represents an analog axis on a joystick.

kThrottle = 4
kTwist = 3
kX = 0
kY = 1
kZ = 2
class Button[source]

Bases: object

Represents a digital button on the Joystick

kTop = 2
kTrigger = 1
class ButtonType[source]

Bases: object

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.
Returns:The value of the axis.
Return type:float
..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.
Returns:The channel for the axis.
Return type:int
..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 (Joystick.ButtonType) – The type of button to read.
Returns:The state of the button.
Return type:bool
..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.

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

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

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

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

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

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.
Return type:float
getThrottleChannel()[source]

Get the channel currently associated with the Throttle axis

Returns:The channel for the axis
Return type:int
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.

Returns:The state of the top button.
Return type:bool
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.

Returns:The state of the trigger.
Return type:bool
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.

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

Get the channel currently associated with the Twist axis

Returns:The channel for the axis
Return type:int
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
Returns:The X value of the joystick.
Return type:float
getXChannel()[source]

Get the channel currently associated with the X axis

Returns:The channel for the axis
Return type:int
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
Returns:The Y value of the joystick.
Return type:float
getYChannel()[source]

Get the channel currently associated with the Y axis

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

Get the Z position of the HID

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

Get the channel currently associated with the Z axis

Returns:The channel for the axis
Return type:int
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.