2 tellopy sample using joystick and video palyer 4 - you can use PS3/PS4/XONE joystick to controll DJI Tello with tellopy module 5 - you must install mplayer to replay the video 6 - Xbox One Controllers were only tested on Mac OS with the 360Controller Driver. 7 get it here -> https://github.com/360Controller/360Controller''' 15 from subprocess
import Popen, PIPE
51 RIGHT_Y_REVERSE = -1.0
82 RIGHT_Y_REVERSE = -1.0
111 LEFT_Y_REVERSE = -1.0
112 RIGHT_X_REVERSE = 1.0
113 RIGHT_Y_REVERSE = -1.0
141 LEFT_Y_REVERSE = -1.0
142 RIGHT_X_REVERSE = 1.0
143 RIGHT_Y_REVERSE = -1.0
171 LEFT_Y_REVERSE = -1.0
172 RIGHT_X_REVERSE = 1.0
173 RIGHT_Y_REVERSE = -1.0
203 RIGHT_X_REVERSE = 1.0
204 RIGHT_Y_REVERSE = 1.0
208 prev_flight_data =
None 209 run_recv_thread =
True 221 global prev_flight_data
225 if event
is drone.EVENT_FLIGHT_DATA:
226 if prev_flight_data != str(data):
228 prev_flight_data = str(data)
230 elif event
is drone.EVENT_LOG_DATA:
233 print(
'event="%s" data=%s' % (event.getname(), str(data)))
237 if abs(old - new) <= max_delta:
250 if e.type == pygame.locals.JOYAXISMOTION:
252 if -buttons.DEADZONE <= e.value
and e.value <= buttons.DEADZONE:
254 if e.axis == buttons.LEFT_Y:
255 throttle =
update(throttle, e.value * buttons.LEFT_Y_REVERSE)
256 drone.set_throttle(throttle)
257 if e.axis == buttons.LEFT_X:
258 yaw =
update(yaw, e.value * buttons.LEFT_X_REVERSE)
260 if e.axis == buttons.RIGHT_Y:
261 pitch =
update(pitch, e.value *
262 buttons.RIGHT_Y_REVERSE)
263 drone.set_pitch(pitch)
264 if e.axis == buttons.RIGHT_X:
265 roll =
update(roll, e.value * buttons.RIGHT_X_REVERSE)
267 elif e.type == pygame.locals.JOYHATMOTION:
269 drone.counter_clockwise(speed)
273 drone.clockwise(speed)
280 elif e.type == pygame.locals.JOYBUTTONDOWN:
281 if e.button == buttons.LAND:
283 elif e.button == buttons.UP:
285 elif e.button == buttons.DOWN:
287 elif e.button == buttons.ROTATE_RIGHT:
288 drone.clockwise(speed)
289 elif e.button == buttons.ROTATE_LEFT:
290 drone.counter_clockwise(speed)
291 elif e.button == buttons.FORWARD:
293 elif e.button == buttons.BACKWARD:
294 drone.backward(speed)
295 elif e.button == buttons.RIGHT:
297 elif e.button == buttons.LEFT:
299 elif e.type == pygame.locals.JOYBUTTONUP:
300 if e.button == buttons.TAKEOFF:
303 print(
'### throttle != 0.0 (This may hinder the drone from taking off)')
306 elif e.button == buttons.UP:
308 elif e.button == buttons.DOWN:
310 elif e.button == buttons.ROTATE_RIGHT:
312 elif e.button == buttons.ROTATE_LEFT:
313 drone.counter_clockwise(0)
314 elif e.button == buttons.FORWARD:
316 elif e.button == buttons.BACKWARD:
318 elif e.button == buttons.RIGHT:
320 elif e.button == buttons.LEFT:
324 font = cv2.FONT_HERSHEY_SIMPLEX
327 font_color = (255,255,255)
330 height, width = image.shape[:2]
333 pos = (left_mergin, height + font_size * row + 1)
335 pos = (left_mergin, font_size * (row + 1))
336 cv2.putText(image, text, pos, font, font_scale, bg_color, 6)
337 cv2.putText(image, text, pos, font, font_scale, font_color, 1)
340 global run_recv_thread
345 print(
'start recv_thread()')
347 container = av.open(drone.get_video_stream())
351 for frame
in container.decode(video=0):
353 frame_skip = frame_skip - 1
355 start_time = time.time()
356 image = cv2.cvtColor(numpy.array(frame.to_image()), cv2.COLOR_RGB2BGR)
359 draw_text(image,
'TelloPy: joystick_and_video ' + str(flight_data), 0)
361 draw_text(image,
'MVO: ' + str(log_data.mvo), -3)
362 draw_text(image, (
'IMU: ' + str(log_data.imu))[0:52], -2)
363 draw_text(image,
' ' + (
'IMU: ' + str(log_data.imu))[52:], -1)
365 if frame.time_base < 1.0/60:
368 time_base = frame.time_base
369 frame_skip = int((time.time() - start_time)/time_base)
370 except Exception
as ex:
371 exc_type, exc_value, exc_traceback = sys.exc_info()
372 traceback.print_exception(exc_type, exc_value, exc_traceback)
377 global run_recv_thread
380 pygame.joystick.init()
384 js = pygame.joystick.Joystick(0)
386 js_name = js.get_name()
387 print(
'Joystick name: ' + js_name)
388 if js_name
in (
'Wireless Controller',
'Sony Computer Entertainment Wireless Controller'):
389 buttons = JoystickPS4
390 elif js_name ==
'Sony Interactive Entertainment Wireless Controller':
391 buttons = JoystickPS4ALT
392 elif js_name
in (
'PLAYSTATION(R)3 Controller',
'Sony PLAYSTATION(R)3 Controller'):
393 buttons = JoystickPS3
394 elif js_name
in (
'Logitech Gamepad F310'):
395 buttons = JoystickF310
396 elif js_name ==
'Xbox One Wired Controller':
397 buttons = JoystickXONE
398 elif js_name ==
'FrSky Taranis Joystick':
399 buttons = JoystickTARANIS
404 print(
'no supported joystick found')
409 drone.subscribe(drone.EVENT_FLIGHT_DATA, handler)
410 drone.subscribe(drone.EVENT_LOG_DATA, handler)
411 threading.Thread(target=recv_thread, args=[drone]).start()
417 for e
in pygame.event.get():
419 if current_image
is not new_image:
420 cv2.imshow(
'Tello', new_image)
421 current_image = new_image
423 except KeyboardInterrupt
as e:
425 except Exception
as e:
426 exc_type, exc_value, exc_traceback = sys.exc_info()
427 traceback.print_exception(exc_type, exc_value, exc_traceback)
430 run_recv_thread =
False 431 cv2.destroyAllWindows()
436 if __name__ ==
'__main__':
def handle_input_event(drone, e)
def update(old, new, max_delta=0.3)
def handler(event, sender, data, args)
def draw_text(image, text, row)