This repository was archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontrollers.py
461 lines (321 loc) · 10.9 KB
/
controllers.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""
Implementation of controllers for simulated drone (AirSim) and real drone (DJI
Tello EDU).
The simulated drone is implemented twice: a normal and a noisy version.
Controllers have been implemented to work with the exact same interface in
order to be interchangeable in their use.
"""
###########
# Imports #
###########
import airsim
import cv2
import keyboard
import numpy as np
import socket
import threading
import time
from abc import ABC, abstractmethod
from colored import fg, attr
from PIL import Image
###########
# Classes #
###########
class Controller(ABC):
"""
Abstract class used to define a controller.
Each controller must inherit this class.
"""
def __init__(self):
super().__init__()
# Initialization
@abstractmethod
def arm(self):
pass
@abstractmethod
def disarm(self):
pass
# Navigation
@abstractmethod
def takeoff(self):
pass
@abstractmethod
def land(self):
pass
@abstractmethod
def move(self, direction: str, distance: float, speed: int = 50):
pass
@abstractmethod
def rotate(self, direction: str, angle: float):
pass
@abstractmethod
def hover(self):
pass
# Camera
@abstractmethod
def picture(self) -> np.array:
pass
# Common
def manual(self):
img_idx = 0
while True:
if keyboard.is_pressed('up'):
self.move('up', 20)
elif keyboard.is_pressed('down'):
self.move('down', 20)
elif keyboard.is_pressed('left'):
self.move('left', 20)
elif keyboard.is_pressed('right'):
self.move('right', 20)
elif keyboard.is_pressed('z'):
self.move('forward', 20)
elif keyboard.is_pressed('s'):
self.move('back', 20)
elif keyboard.is_pressed('q'):
self.rotate('ccw', 5)
elif keyboard.is_pressed('d'):
self.rotate('cw', 5)
elif keyboard.is_pressed('p'):
img = self.picture()
img = Image.fromarray(img)
img.save(f'img_{img_idx}.png')
img_idx += 1
elif keyboard.is_pressed('c'):
break
class AirSimDrone(Controller):
"""
Implementation of the controller used for the simulated drone in AirSim.
This version uses the functions of AirSim API without any noise.
"""
def __init__(self):
super().__init__()
self.client = airsim.MultirotorClient()
self.client.confirmConnection()
# Abstract interface
def arm(self):
self.client.enableApiControl(True)
self.client.armDisarm(True)
def disarm(self):
self.client.armDisarm(False)
self.client.reset()
self.client.enableApiControl(False)
def takeoff(self):
landed = self.state().landed_state
if landed == airsim.LandedState.Landed:
self.client.takeoffAsync().join()
self.move('down', 50, 50)
def land(self):
landed = self.state().landed_state
if landed != airsim.LandedState.Landed:
self.client.landAsync().join()
def move(self, direction, distance, speed=50):
error = False
if speed < 10 or speed > 100:
print('Speed must be between 10 and 100 [cm/s]')
error = True
if distance < 20 or distance > 500:
print('Distance must be between 20 and 500 [cm]')
error = True
speed /= 100
duration = (distance / 100) / speed
z = self.state().kinematics_estimated.position.z_val
directions = {
'up': [0, 0, -speed, duration],
'down': [0, 0, speed, duration],
'left': [0, -speed, z, duration],
'right': [0, speed, z, duration],
'forward': [speed, 0, z, duration],
'back': [-speed, 0, z, duration]
}
factors = directions.get(direction)
if factors is None:
print('Unknown direction')
error = True
if not error:
if direction in ['up', 'down']:
self.client.moveByVelocityBodyFrameAsync(*factors).join()
else:
self.client.moveByVelocityZBodyFrameAsync(*factors).join()
def rotate(self, direction, angle):
error = False
factors = {
'cw': 1,
'ccw': -1
}
factor = factors.get(direction)
if factor is None:
print('Unknown direction')
error = True
if angle < 1 or angle > 360:
print('Angle must be between 1 and 360 [degree]')
error = True
rate = factor * 45
duration = angle / 45
if not error:
self.client.rotateByYawRateAsync(rate, duration).join()
def hover(self):
self.client.hoverAsync().join()
def picture(self):
# Take picture
pictures = self.client.simGetImages([
airsim.ImageRequest(
camera_name='front_center',
image_type=0,
pixels_as_float=False,
compress=False
)
])
picture = pictures[0]
# Process data
image = airsim.string_to_uint8_array(picture.image_data_uint8)
image = image.reshape(picture.height, picture.width, 3)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
# Specific
def log(self, message: str):
self.client.simPrintLogMessage(message)
def teleport(self, position: list):
pose = self.client.simGetVehiclePose()
pose.position = airsim.Vector3r(*position)
self.client.simSetVehiclePose(pose, True)
def state(self):
return self.client.getMultirotorState()
def imu(self):
return self.client.getImuData()
def gps(self):
return self.client.getGpsData()
class AirSimDroneNoisy(AirSimDrone):
"""
Implementation of the controller used for the simulated drone in AirSim.
This version uses the functions of AirSim API with additional noise on
movement to simulate the real drone behaviour.
"""
def __init__(self):
super().__init__()
def _noise(self, value: float) -> float:
mean = value / 15
std = value / 50
noise = np.random.normal(mean, std)
return value + noise
def move(self, direction, distance, speed=50.):
distance = self._noise(distance)
distance = min(max(20, distance), 500)
super().move(direction, distance, speed)
def rotate(self, direction, angle):
angle = self._noise(angle)
angle = min(max(1, angle), 360)
super().rotate(direction, angle)
class TelloEDU(Controller):
"""
Implementation of the controller used for the DJI Tello EDU.
"""
def __init__(self):
super().__init__()
# Socket for sending command
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind(('', 8889))
# DJI Tello EDU information
self.tello_address = ('192.168.10.1', 8889)
# Response of the drone
self.response = None
# Time out
self.max_timeout = 15.0
# State of the drone
self.disarmed = True
# Last frame captured by the camera
self.frame = None
# Thread for receiving command response
self.response_thread = threading.Thread(target=self._response_thread)
self.response_thread.daemon = True
# Thread for receiving video stream
self.stream_thread = threading.Thread(target=self._stream_thread)
self.stream_thread.daemon = True
# Speed of the drone, in [cm/s]
self.speed = 50
# Abstract interface
def arm(self):
self.disarmed = False
self.response_thread.start()
self._send_command('command')
self._send_command('streamon')
self.stream_thread.start()
def disarm(self):
self._send_command('streamoff')
self.disarmed = True
self.stream_thread.join()
self.response_thread.join()
self.socket.close()
def takeoff(self):
self._send_command('takeoff')
def land(self):
self._send_command('land')
def move(self, direction, distance, speed=50):
error = False
if speed < 10 or speed > 100:
print('Speed must be between 10 and 100 [cm/s]')
error = True
if direction not in ['up', 'down', 'left', 'right', 'forward', 'back']:
print('Unknown direction')
error = True
if distance < 20 or distance > 500:
print('Distance must be between 20 and 500 [cm]')
error = True
if not error:
if speed != self.speed:
self._send_command(f'speed {speed}')
self.speed = speed
self._send_command(f'{direction} {distance}')
def rotate(self, direction, angle):
error = False
if direction not in ['cw', 'ccw']:
print('Unknown direction')
error = True
if angle < 1 or angle > 360:
print('Angle must be between 1 and 360 [degree]')
error = True
if not error:
self._send_command(f'{direction} {angle}')
def hover(self):
self._send_command('stop')
def picture(self):
return self.frame
# Specific
def emergency(self):
self._send_command('emergency')
def battery(self):
self._send_command('battery?')
def _send_command(self, command: str):
print(f'Command: {fg(6)}{command}{attr(0)}')
self.socket.sendto(
command.encode('utf-8'),
self.tello_address
)
start = time.time()
while self.response is None:
now = time.time()
if now - start > self.max_timeout:
print('Time out')
break
self.response = None
def _response_thread(self):
while True:
if self.disarmed:
break
try:
self.response, ip = self.socket.recvfrom(1024)
if self.response is not None:
self.response = self.response.decode('utf-8').strip()
color = fg(2) if self.response == 'ok' else fg(1)
print(f'Response: {color}{self.response}{attr(0)}')
except Exception as e:
print(f'Exception: {e}')
def _stream_thread(self):
stream = cv2.VideoCapture('udp://@0.0.0.0:11111')
stream.set(cv2.CAP_PROP_BUFFERSIZE, 1)
while True:
if self.disarmed:
break
ack, frame = stream.read()
if ack:
self.frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
stream.release()