Joystick

class wpilib.Joystick(port, numAxisTypes=None, numButtonTypes=None)[source]

Bases: builtins.object

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 drivers 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 AxisType[source]

Bases: builtins.object

Represents an analog axis on a joystick.

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

Bases: builtins.object

Represents a digital button on the Joystick

kNumButton = 2
kTop = 1
kTrigger = 0
class Joystick.RumbleType[source]

Bases: builtins.object

Represents a rumble output on the Joystick

kLeftRumble_val = 0
kRightRumble_val = 1
Joystick.flush_outputs()[source]

Flush all joystick HID & rumble output values to the HAL

Joystick.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 – The axis to read.
Returns:The value of the axis.
Return type:float
Joystick.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
Joystick.getAxisCount()[source]

For the current joystick, return the number of axis

Joystick.getBumper(hand=None)[source]

This is not supported for the Joystick.

This method is only here to complete the GenericHID interface.

Parameters:hand – This parameter is ignored for the Joystick class and is only here to complete the GenericHID interface.
Returns:The state of the bumper (always False)
Return type:bool
Joystick.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
Joystick.getButtonCount()[source]

For the current joystick, return the number of buttons

:rtype int

Joystick.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
Joystick.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
Joystick.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
Joystick.getPOV(pov=0)[source]

Get the state of a POV on the joystick.

Parameters:pov (int) – which POV (default is 0)
Returns:The angle of the POV in degrees, or -1 if the POV is not pressed.
Return type:float
Joystick.getPOVCount()[source]

For the current joystick, return the number of POVs

Return type:int
Joystick.getRawAxis(axis)[source]

Get the value of the axis.

Parameters:axis (int) – The axis to read, starting at 0.
Returns:The value of the axis.
Return type:float
Joystick.getRawButton(button)[source]

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 (int) – The button number to be read (starting at 1).
Returns:The state of the button.
Return type:bool
Joystick.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
Joystick.getTop(hand=None)[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.

Parameters:hand – This parameter is ignored for the Joystick class and is only here to complete the GenericHID interface.
Returns:The state of the top button.
Return type:bool
Joystick.getTrigger(hand=None)[source]

Read the state of the trigger on the joystick.

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

Parameters:hand – This parameter is ignored for the Joystick class and is only here to complete the GenericHID interface.
Returns:The state of the trigger.
Return type:bool
Joystick.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
Joystick.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
Joystick.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
Joystick.getZ(hand=None)[source]

Get the Z value of the joystick.

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

Parameters:hand – Unused
Returns:The Z value of the joystick.
Return type:float
Joystick.kDefaultThrottleAxis = 3
Joystick.kDefaultTopButton = 2
Joystick.kDefaultTriggerButton = 1
Joystick.kDefaultTwistAxis = 2
Joystick.kDefaultXAxis = 0
Joystick.kDefaultYAxis = 1
Joystick.kDefaultZAxis = 2
Joystick.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.
Joystick.setOutput(outputNumber, value)[source]

Set a single HID output value for the joystick.

Parameters:
  • outputNumber – The index of the output to set (1-32)
  • value – The value to set the output to.
Joystick.setOutputs(value)[source]

Set all HID output values for the joystick.

Parameters:value (int) – The 32 bit output value (1 bit for each output)
Joystick.setRumble(type, value)[source]

Set the rumble output for the joystick. The DS currently supports 2 rumble values, left rumble and right rumble

Parameters:
  • type (Joystick.RumbleType) – Which rumble value to set
  • value (float) – The normalized value (0 to 1) to set the rumble to