-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
99 lines (76 loc) · 2.43 KB
/
app.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
import os
from flask import Flask, Response, render_template, request, jsonify, send_from_directory
from lib.camera import Camera
from lib.udp import UDP
from lib.telemetry import Telemetry
from lib.drone import Drone
from lib.mission import Mission
app = Flask(__name__)
@app.route("/")
def main():
return render_template('index.html')
@app.route('/video_stream')
def video_feed():
camera.get_video()
return Response(get_frame(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
def get_frame(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route("/connect")
def connect():
udp.send_command("command")
udp.get_response()
if status == "ok":
drone.is_connected = True
else:
drone.is_connected = False
return drone.toJSON()
@app.route("/status")
def status():
return drone.toJSON()
@app.route('/send_command', methods=['POST'])
def send_command():
command = request.json['command']
udp.send_command(command)
response = udp.get_response()
return response
@app.route('/take_photo')
def take_photo():
camera.take_photo()
return drone.toJSON()
@app.route('/start_recording')
def start_recording():
camera.start_recording()
return drone.toJSON()
@app.route('/stop_recording')
def stop_recording():
camera.stop_recording()
return drone.toJSON()
@app.route('/launch_mission', methods=['POST'])
def launch_mission():
mission_code = request.json['mission_code']
mission.parse_mission(mission_code)
return ""
# So that we can load DroneBlocks in an iframe
static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'droneblocks/dist')
@app.route('/droneblocks/<path:path>', methods=['GET'])
def droneblocks(path):
if not os.path.isfile(os.path.join(static_file_dir, path)):
path = os.path.join(path, 'tello.html')
return send_from_directory(static_file_dir, path)
if __name__ == "__main__":
# Initialize the drone class
drone = Drone(is_aruco_tracking_enabled=True)
# Camera for stream, photo, video
camera = Camera(drone)
# Udp for sending commands
udp = UDP()
udp.start_listening()
# Create the mission handler
mission = Mission(udp, camera)
# Handle Tello's state information
telemetry = Telemetry(drone)
telemetry.receive_telemetry()
# Fire up the app
app.run()