Source code for seamonsters.joystick
import wpilib
__author__ = "seamonsters"
class TFlightHotasX:
"""
Constants for the Thrustmaster T-Flight Hotas X joystick.
"""
AXIS_X = 0
AXIS_Y = 1
AXIS_THROTTLE = 2
AXIS_TWIST = 3
AXIS_LEVER = 4
def deadZone(value, deadZone=0.1, maxValue=1.0):
"""
Add a dead-zone to the number. Any input from ``deadZone`` to ``maxValue``
is mapped to the range 0 to ``maxValue``. Negative numbers are also mapped.
Any input between ``deadZone`` and ``-deadZone`` is mapped to 0.
:param value: input number
:param deadZone: the minimum "dead zone" value
:param maxValue: the maximum value
:return: the number, constrained to the minimum/maximum values
"""
if abs(value) > deadZone:
newValue = (abs(value) - deadZone) / (maxValue - deadZone)
if newValue > maxValue:
newValue = maxValue
if value < 0:
return -newValue
else:
return newValue
else:
return 0.0
def whileButtonPressed(joystick, button):
"""
A generator which runs while the joystick button is pressed, then exits.
:param joystick: a ``wpilib.Joystick``
:param button: a button number
"""
yield
while joystick.getRawButton(button):
yield
def untilButtonPressed(joystick, button):
"""
A generator which runs until the joystick button is pressed, then exits.
:param joystick: a ``wpilib.Joystick``
:param button: a button number
"""
yield
while not joystick.getRawButton(button):
yield
class DynamicAxis:
"""
Makes values from a joystick axis go higher if you push them faster.
"""
# try 2.0, 4.0, 0.5?
[docs] def __init__(self, exponent=1.0, speedScaleFactor=0.0,
speedScaleExponent=0.0, deadZone=None):
"""
``exponent`` should be >= 1. ``speedScaleFactor`` should be >= 1.
``speedScaleExponent`` should be <= 1 (use 0 to ignore speed).
``deadZone`` should be < 1 and close to 0.
"""
self.exponent = exponent
self.speedScaleFactor = speedScaleFactor
self.speedScaleExponent = speedScaleExponent
self.deadZone = deadZone
self.values = [0.0, 0.0, 0.0]
self.scale = 1.0
[docs] def update(self, value):
"""
Update the value of the axis
:param value: the raw axis value from the joystick
:return: the adjusted value
"""
if self.deadZone is not None:
if abs(value) < self.deadZone:
value = 0.0
self.values.append(value)
self.values.pop(0)
reversedValues = list(self.values)
reversedValues.reverse()
if 0.0 in reversedValues:
if reversedValues.index(0) == len(reversedValues) - 1:
self.scale = (abs(self.values[-1]) * self.speedScaleFactor) \
** self.speedScaleExponent
adjustedValue = (value * self.scale) ** self.exponent
if value >= 0:
adjustedValue = abs(adjustedValue)
else:
adjustedValue = -abs(adjustedValue)
return adjustedValue
if __name__ == "__main__":
axis = DynamicAxis()
print(axis.update(0.0))
print(axis.update(1.0))
print(axis.update(1.0))
print(axis.update(0.5))