-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoy.py
252 lines (211 loc) · 7.43 KB
/
joy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
Copyright 2020 Aircomm
SPDX-License-Identifier: MIT
Author: Giovanni Grieco <[email protected]>
"""
import collections
import time
import os
from threading import Thread
from scapy import sendrecv
from scapy.layers.inet import IP, UDP
from scapy.packet import Raw
"""
Importing SDL2 in Windows could lead to an ImportError if DLL is not found.
Let's force it to search in the current directory.
"""
if os.name == 'nt':
os.environ['PYSDL2_DLL_PATH'] = os.curdir
import sdl2
import sdl2.ext
class JoystickController:
"""
Control your DJI Tello drone using your Joystick, directly from your PC.
Be sure that networking is already setup and the drone is reachable.
"""
def __init__(self):
"""
Initialize useful constants and routing mapping to setup controller actions.
"""
self._running = True
self._command_queue = collections.deque()
###
# You may want to customize the constants and button mapping below
# to suit your specific needs and Joystick characteristics.
###
self._joystick = self._init_joystick()
self._AXIS_DEAD = 2500
self._AXIS_MAX_VAL = 32767
self._axis_state = {
'roll': 0,
'quota': 0,
'yaw': 0,
'pitch': 0
}
self._event_map = {
'SELECT': self._land,
'START': self._takeoff,
'A': self._emergency_land,
'Y': self._command,
'LEFT_X': self._set_roll,
'LEFT_Y': self._set_pitch,
'RIGHT_X': self._set_yaw,
'RIGHT_Y': self._set_quota
}
self._button_map = ('A', 'B', 'X', 'Y', 'LB', 'RB', 'SELECT', 'START', 'JL', 'JR')
self._axis_map = ('LEFT_X', 'LEFT_Y', 'LT', 'RIGHT_X', 'RIGHT_Y', 'RT')
print(f'Connected to {sdl2.SDL_JoystickName(self._joystick).decode()}')
def run(self):
"""
Main runtime procedure.
Manage threads, an empty loop and shutdown procedure in case of program termination.
"""
threads = (
Thread(target=self._receive_command_loop, daemon=True),
Thread(target=self._send_command_loop, daemon=False),
)
for t in threads:
t.start()
self._run_loop()
# if we exit from the run loop for some reason, shutdown
self._command_queue.clear()
self._land()
for t in threads:
t.join()
@staticmethod
def _init_joystick():
"""
Initialize joystick using SDL library. Note that it is automatically
chosen the first enumerated joystick.
Returns
-------
The SDL Joystick object.
"""
sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
njoysticks = sdl2.SDL_NumJoysticks()
if njoysticks < 1:
raise RuntimeError(f'No joysticks connected!')
print('Joysticks available:')
for i in range(njoysticks):
joy = sdl2.SDL_JoystickOpen(i)
print(f' - {sdl2.SDL_JoystickName(joy).decode()}')
return sdl2.SDL_JoystickOpen(0)
def _run_loop(self):
"""
Main running loop, just to check and handle interrupt signal.
"""
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
self._running = False
def _receive_command_loop(self):
"""
Manage Joystick events and call their mapped function.
"""
while self._running:
for event in sdl2.ext.get_events():
try:
if event.type == sdl2.SDL_JOYBUTTONDOWN:
self._event_map[self._button_map[event.jbutton.button]]()
elif event.type == sdl2.SDL_JOYAXISMOTION:
if abs(event.jaxis.value) > self._AXIS_DEAD:
self._event_map[self._axis_map[event.jaxis.axis]](event.jaxis.value)
except KeyError:
pass
def _send_command_loop(self):
"""
Handle command execution using hard real-time, FCFS-based scheduling policy.
"""
while self._running:
try:
cmd = self._command_queue.pop()
answer = sendrecv.sr1(IP(dst='192.168.10.1') / UDP(dport=8889) / cmd,
verbose=1,
timeout=0.2)
try:
response = answer[Raw].load.decode()
print(f'EXE {cmd}: {response}')
except TypeError:
print(f'EXE {cmd}: unknown')
continue
except IndexError: # nothing to schedule, retry another time
time.sleep(0.5)
continue
def _command(self):
"""
Take control of the DJI Tello.
"""
print('Pressed Command button')
self._command_queue.append('command')
def _land(self, force=False):
"""
Schedule drone landing.
Parameters
----------
force: clear out FCFS queue, in case of absolute necessity.
"""
print('Pressed Land button')
if force:
self._command_queue.clear()
self._command_queue.append('land')
def _emergency_land(self):
"""
Schedule drone emergency landing.
Caution: don't harm the drone!
"""
self._command_queue.clear()
self._command_queue.append('emergency')
def _takeoff(self):
"""
Schedule drone takeoff.
Note: if drone is not taking off, check your battery charge level!
"""
print('Pressed Takeoff button')
self._command_queue.append('takeoff')
def _set_roll(self, raw_val):
"""
Set roll axis value.
"""
val = int(raw_val * 100 / self._AXIS_MAX_VAL)
if self._axis_state['roll'] != val:
self._axis_state['roll'] = val
self._dispatch_axis_update()
def _set_quota(self, raw_val):
"""
Set quota axis value.
"""
val = -int(raw_val * 100 / self._AXIS_MAX_VAL)
if self._axis_state['quota'] != val:
self._axis_state['quota'] = val
self._dispatch_axis_update()
def _set_yaw(self, raw_val):
"""
Set yaw axis value.
"""
val = int(raw_val * 100 / self._AXIS_MAX_VAL)
if self._axis_state['yaw'] != val:
self._axis_state['yaw'] = val
self._dispatch_axis_update()
def _set_pitch(self, raw_val):
"""
Set pitch axis value.
"""
val = -int(raw_val * 100 / self._AXIS_MAX_VAL)
if self._axis_state['pitch'] != val:
self._axis_state['pitch'] = val
self._dispatch_axis_update()
def _dispatch_axis_update(self):
"""
Schedule an update of the pitch-roll-quota-yaw of the drone, tipically
managed using Joystick analog sticks.
"""
# print(f'RC: {self._axis_state}') # Caution: this message is highly frequent
self._command_queue.append(f'rc {self._axis_state["roll"]} '
f'{self._axis_state["pitch"]} '
f'{self._axis_state["quota"]} '
f'{self._axis_state["yaw"]}')
if __name__ == '__main__':
print(f'This is Joy - Copyright 2020 Aircomm')
ctrl = JoystickController()
ctrl.run()