2 tellopy sample using keyboard and video player 4 Requires mplayer to record/save video. 9 - WASD to move the drone 10 - space/shift to ascend/descent slowly 12 - arrow keys to ascend, descend, or yaw quickly 13 - backspace to land, or P to palm-land 14 - enter to take a picture 15 - R to start recording video, R again to stop recording 16 (video and photos will be saved to a timestamped file in ~/Pictures/) 17 - Z to toggle camera zoom state 18 (zoomed-in widescreen or high FOV 4:3) 31 from subprocess
import Popen, PIPE
36 prev_flight_data =
None 41 date_fmt =
'%Y-%m-%d_%H%M%S' 51 video_recorder.stdin.close()
52 status_print(
'Video saved to %s' % video_recorder.video_filename)
57 filename =
'%s/Pictures/tello-%s.mp4' % (os.getenv(
'HOME'),
58 datetime.datetime.now().strftime(date_fmt))
59 video_recorder = Popen([
60 'mencoder',
'-',
'-vc',
'x264',
'-fps',
'30',
'-ovc',
'copy',
61 '-of',
'lavf',
'-lavfopts',
'format=mp4',
66 video_recorder.video_filename = filename
89 drone.set_video_mode(
not drone.zoom)
90 pygame.display.get_surface().fill((0,0,0))
100 'right shift':
'down',
101 'q':
'counter_clockwise',
104 'left':
lambda drone, speed: drone.counter_clockwise(speed*2),
105 'right':
lambda drone, speed: drone.clockwise(speed*2),
106 'up':
lambda drone, speed: drone.up(speed*2),
107 'down':
lambda drone, speed: drone.down(speed*2),
108 'tab':
lambda drone, speed: drone.takeoff(),
109 'backspace':
lambda drone, speed: drone.land(),
111 'r': toggle_recording, 113 'enter': take_picture,
114 'return': take_picture,
124 def __init__(self, key, format, colour=(255,255,255), update=
None):
132 self.
_update =
lambda drone,data: getattr(data, self.
_key)
135 new_value = self.
_update(drone, data)
136 if self.
_value != new_value:
142 return (drone.zoom
and "VID" or "PIC")
145 return (video_recorder
and "REC 00:00" or "")
151 surface = element.update(drone, flight_data)
154 blits += [(surface, (0, h))]
156 h += surface.get_height()
158 overlay = pygame.Surface((w, h), pygame.SRCALPHA)
159 overlay.fill((0,0,0))
162 pygame.display.get_surface().blit(overlay, (0,0))
163 pygame.display.update(overlay.get_rect())
166 pygame.display.set_caption(text)
178 global prev_flight_data
180 if prev_flight_data != text:
182 prev_flight_data = text
186 global video_recorder
187 if video_player
is None:
188 cmd = [
'mplayer',
'-fps',
'35',
'-really-quiet' ]
190 cmd = cmd + [
'-wid', str(wid) ]
191 video_player = Popen(cmd + [
'-'], stdin=PIPE)
194 video_player.stdin.write(data)
195 except IOError
as err:
201 video_recorder.stdin.write(data)
202 except IOError
as err:
204 video_recorder =
None 209 path =
'%s/Pictures/tello-%s.jpeg' % (
211 datetime.datetime.now().strftime(
'%Y-%m-%d_%H%M%S'))
212 with open(path,
'wb')
as fd:
218 pygame.display.init()
219 pygame.display.set_mode((1280, 720))
223 font = pygame.font.SysFont(
"dejavusansmono", 32)
226 if 'window' in pygame.display.get_wm_info():
227 wid = pygame.display.get_wm_info()[
'window']
228 print(
"Tello video WID:", wid)
233 drone.subscribe(drone.EVENT_FLIGHT_DATA, flightDataHandler)
234 drone.subscribe(drone.EVENT_VIDEO_FRAME, videoFrameHandler)
235 drone.subscribe(drone.EVENT_FILE_RECEIVED, handleFileReceived)
241 for e
in pygame.event.get():
243 if e.type == pygame.locals.KEYDOWN:
244 print(
'+' + pygame.key.name(e.key))
245 keyname = pygame.key.name(e.key)
246 if keyname ==
'escape':
249 if keyname
in controls:
250 key_handler = controls[keyname]
251 if type(key_handler) == str:
252 getattr(drone, key_handler)(speed)
254 key_handler(drone, speed)
256 elif e.type == pygame.locals.KEYUP:
257 print(
'-' + pygame.key.name(e.key))
258 keyname = pygame.key.name(e.key)
259 if keyname
in controls:
260 key_handler = controls[keyname]
261 if type(key_handler) == str:
262 getattr(drone, key_handler)(0)
264 key_handler(drone, 0)
268 print(
'Shutting down connection to drone...')
274 if __name__ ==
'__main__':
def __init__(self, key, format, colour=(255, 255, 255), update=None)
def update(self, drone, data)
def toggle_recording(drone, speed)
def palm_land(drone, speed)
def flight_data_recording(args)
def handleFileReceived(event, sender, data)
def flightDataHandler(event, sender, data)
def flight_data_mode(drone, args)
def take_picture(drone, speed)
def update_hud(hud, drone, flight_data)
def toggle_zoom(drone, speed)
def videoFrameHandler(event, sender, data)