Skip to content

Commit 9840169

Browse files
committed
add in LogitechJoystick to show how it works as-is
1 parent 0d5c4a5 commit 9840169

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

donkeycar/parts/controller_events.py

+53-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def show_map(self) -> bool:
5050
def poll(self):
5151
'''
5252
Query the input controller for a button or axis state change event.
53-
This must be threadsafe.
53+
This must be threadsafe as it will generally be called on
54+
a separate thread.
5455
5556
returns: tuple of
5657
- button: string name of button if a button changed, otherwise None
@@ -81,8 +82,8 @@ class LinuxGameController(AbstractInputController):
8182
def __init__(self, button_names = {}, axis_names = {}, dev_fn='/dev/input/js0'):
8283
self.axis_states = {}
8384
self.button_states = {}
84-
self.axis_names = button_names
85-
self.button_names = axis_names
85+
self.axis_names = axis_names
86+
self.button_names = button_names
8687
self.axis_map = []
8788
self.button_map = []
8889
self.num_axes = 0
@@ -97,7 +98,7 @@ def init(self) -> bool:
9798
Attempt to initialize the controller.
9899
In Linux, query available buttons and axes using fnctl and
99100
a path in the linux device tree.
100-
If the button_names or axis_names mappings passed to the contruct
101+
If the button_names or axis_names mappings passed to the contructor
101102
maps to a discovered input control, then use that name when emitting
102103
events for that input control, otherwise emit using a default name.
103104
- on success returns self so it can be fluently chained
@@ -152,6 +153,7 @@ def init(self) -> bool:
152153
axis_name = self.axis_names.get(axis, 'axis(0x%02x)' % axis)
153154
self.axis_map.append(axis_name)
154155
self.axis_states[axis_name] = 0.0
156+
self.axis_names[axis] = axis_name # update the name map
155157

156158
# Get the button map.
157159
buf = array.array('H', [0] * 200)
@@ -161,7 +163,7 @@ def init(self) -> bool:
161163
btn_name = self.button_names.get(btn, 'button(0x%03x)' % btn)
162164
self.button_map.append(btn_name)
163165
self.button_states[btn_name] = 0
164-
#print('btn', '0x%03x' % btn, 'name', btn_name)
166+
self.button_names[btn] = btn_name # update the name map
165167

166168
self.initialized = True
167169
return True
@@ -235,6 +237,49 @@ def poll(self):
235237
return button, button_state, axis, axis_val
236238

237239

240+
class LogitechJoystick(LinuxGameController):
241+
'''
242+
An interface to a physical Logitech joystick available at /dev/input/js0
243+
Contains mapping that work for Raspian Stretch drivers
244+
Tested with Logitech Gamepad F710
245+
https://www.amazon.com/Logitech-940-000117-Gamepad-F710/dp/B0041RR0TW
246+
credit:
247+
https://github.com/kevkruemp/donkeypart_logitech_controller/blob/master/donkeypart_logitech_controller/part.py
248+
'''
249+
def __init__(self, *args, **kwargs):
250+
super(LogitechJoystick, self).__init__(*args, **kwargs)
251+
252+
self.axis_names = {
253+
0x00: 'left_stick_horz',
254+
0x01: 'left_stick_vert',
255+
0x03: 'right_stick_horz',
256+
0x04: 'right_stick_vert',
257+
258+
0x02: 'L2_pressure',
259+
0x05: 'R2_pressure',
260+
261+
0x10: 'dpad_leftright', # 1 is right, -1 is left
262+
0x11: 'dpad_up_down', # 1 is down, -1 is up
263+
}
264+
265+
self.button_names = {
266+
0x13a: 'back', # 8 314
267+
0x13b: 'start', # 9 315
268+
0x13c: 'Logitech', # a 316
269+
270+
0x130: 'A',
271+
0x131: 'B',
272+
0x133: 'X',
273+
0x134: 'Y',
274+
275+
0x136: 'L1',
276+
0x137: 'R1',
277+
278+
0x13d: 'left_stick_press',
279+
0x13e: 'right_stick_press',
280+
}
281+
282+
238283
class InputControllerEvents(object):
239284
'''
240285
Poll a AbstractGameController() and convert to button and axis events.
@@ -364,7 +409,9 @@ def shutdown(self):
364409
# step 1: collect button and axis names
365410
# - init must be called to initialize the
366411
#
367-
controller = LinuxGameController(button_names={}, axis_names={})
412+
# controller = LinuxGameController(button_names={}, axis_names={})
413+
controller = LogitechJoystick()
414+
368415

369416
#
370417
# step 2: start sending events

0 commit comments

Comments
 (0)