-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtello.py
222 lines (129 loc) · 5.19 KB
/
tello.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
import cv2
from turtle import Turtle
from djitellopy import Tello
from time import sleep
from threading import Thread
from ultralytics import YOLO
"""
********************************** INFORMATION ************************************************************************
According to the drone's docs, the speed is 15 cm/s.
The distance is in pixels, so we need to create a conversion rate. You can create yours as your convenience
Each iteration will have a time.sleep(seg) to wait the instruction be done.
*********************************************************************************************************************
"""
model = YOLO('yolov8_house_items.pt')
class Fly(Turtle):
def __init__(self, exported_path):
super().__init__()
self.hideturtle()
self.start = False
self.keep_recording = True
self.exported_path = exported_path
self.tello = Tello()
def start_flying(self, start, path):
if start:
self.tello.connect()
self.tello.get_battery()
self.showturtle()
self.shapesize(2)
self.left(90)
recorder = Thread(target=self.video_recorder, daemon=True)
recorder.start()
self.path_to_commands(path)
def draw_turtle(self, path_list):
for l in path_list:
x = l[0]
y = l[1]
path_list.pop(0)
return x, y
def to_travel(self, dist_px):
"""
speed = 15cm/s
1px -> 1 cm
return the seconds that will be used to know the time interval between commands.
Feel free to use as you want
"""
if dist_px < 0:
if dist_px < -100: # I put this condition min value because testing at home. Outside max value is -500
cm = 50
else:
cm = abs(dist_px)
elif dist_px > 100: # I put this max value because testing at home. Outside max value is 500
cm = 50
else:
cm = dist_px
return round((cm / 15), 2)
def path_to_commands(self,full_path):
print("******** START THE PATH ********")
sleep(2) # wait to initialize the video_recorder
self.tello.takeoff()
speed = 20
path_list = full_path[-1]
path = full_path[:-1]
for item in path:
if item["motion"] == "forward":
seconds = self.to_travel(item["distance"])
self.tello.send_rc_control(0, speed, 0, 0)
print("SENCONDS: ############", seconds)
sleep(seconds)
elif item["motion"] == "backward":
seconds = self.to_travel(item["distance"])
self.tello.send_rc_control(0, -speed, 0, 0)
print("SENCONDS: ############", seconds)
sleep(seconds)
elif item["motion"] == "left":
seconds = self.to_travel(item["distance"])
self.tello.send_rc_control(speed, 0, 0, 0)
print("SENCONDS: ############", seconds)
sleep(seconds)
elif item["motion"] == "right":
seconds = self.to_travel(item["distance"])
self.tello.send_rc_control(-speed, 0, 0, 0)
print("SENCONDS: ############", seconds)
sleep(seconds)
elif item["motion"] == "rotate_right":
yw = 90
seconds = 1
self.tello.send_rc_control(0, 0, 0, yw)
print("SENCONDS: ############", seconds)
self.right(90)
sleep(seconds)
elif item["motion"] == "rotate_left":
yw = 90
seconds = 1
self.tello.send_rc_control(0, 0, 0, yw)
print("SENCONDS: ############", seconds)
self.left(90)
sleep(seconds) # As it is in other thread it keeps flying
elif item["motion"] == "up":
self.tello.send_rc_control(0, 0, speed, 0)
seconds = 1
sleep(seconds)
elif item["down"] == "down":
self.tello.send_rc_control(0, 0, -speed, 0)
seconds = 1
sleep(seconds)
self.tello.send_rc_control(0, 0, 0, 0)
try:
x, y = self.draw_turtle(path_list)
self.goto(x, y)
except TypeError:
pass
print("*************** LAND ********************")
self.keep_recording = False
self.tello.land()
def abort_mission(self):
if self.tello: # Check if tello is initialized
return self.tello.emergency()
def video_recorder(self):
self.tello.streamon()
while self.keep_recording:
frame_read = self.tello.get_frame_read().frame
frame_read = cv2.resize(frame_read, (640, 640))
results = model(frame_read)
annotated_frame = results[0].plot()
cv2.imshow("DRONE DETECTIONS", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
self.tello.land()
self.tello.streamoff()