From f9e8fb31459ac998d68fe86ed6a1cdbb278c02d5 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Tue, 4 Jan 2022 13:17:30 +0000 Subject: [PATCH] Joystick: learn axis min/max (#23377) * Joystick: learn axis min/max * clean up updating max/min Co-authored-by: Willem Melching old-commit-hash: 5a77157ea435260b3e7ecacb83f13d3753ede859 --- tools/joystick/joystickd.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/joystick/joystickd.py b/tools/joystick/joystickd.py index ba7b29df54..0e3269556a 100755 --- a/tools/joystick/joystickd.py +++ b/tools/joystick/joystickd.py @@ -34,7 +34,8 @@ class Keyboard: class Joystick: def __init__(self): - self.max_axis_value = 255 # tune based on your joystick, 0 to this + self.min_axis_value = 0 + self.max_axis_value = 255 self.cancel_button = 'BTN_TRIGGER' self.axes_values = {'ABS_Y': 0., 'ABS_RZ': 0.} # gb, steer @@ -45,7 +46,10 @@ class Joystick: if event[0] == self.cancel_button and event[1] == 0: # state 0 is falling edge self.cancel = True elif event[0] in self.axes_values: - norm = -interp(event[1], [0, self.max_axis_value], [-1., 1.]) + self.max_axis_value = max(event[1], self.max_axis_value) + self.min_axis_value = min(event[1], self.min_axis_value) + + norm = -interp(event[1], [self.min_axis_value, self.max_axis_value], [-1., 1.]) self.axes_values[event[0]] = norm if abs(norm) > 0.05 else 0. # center can be noisy, deadzone of 5% else: return False