-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrone.py
More file actions
231 lines (193 loc) · 6.99 KB
/
Copy pathdrone.py
File metadata and controls
231 lines (193 loc) · 6.99 KB
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
import cv2
import numpy as np
from e_drone.drone import *
from e_drone.protocol import *
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
def initialize():
drone = Drone()
drone.open()
return drone
def capture_img():
camera = PiCamera()
img = 'img.jpg'
camera.resolution = (240, 240)
camera.framerate = 32
camera.rotation = 180
camera.capture(img)
camera.close()
return img
def move_to_center(drone):
h = -1
lower_blue = np.array([100, 30, 30])
upper_blue = np.array([110, 255, 255])
while h < 2:
try:
img = cv2.imread(capture_img())
img = cv2.GaussianBlur(img, (9, 9), 3)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_blue, upper_blue)
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
M = cv2.moments(cnt)
cx = int(M['m10'] / (M['m00'] + 0.000000000000001))
cy = int(M['m01'] / (M['m00'] + 0.000000000000001))
h = len(hierarchy[0])
if h >= 2:
break
while not check_y(drone):
if cy < 140:
drone.sendControlPosition(0, 0, 0.1, 0.5, 0, 0)
elif cy > 160:
drone.sendControlPosition(0, 0, -0.1, 0.5, 0, 0)
else:
print('y ok y : ', cy)
time.sleep(1)
drone.sendControlWhile(0, 0, 0, 0, 1000)
while not check_x(drone):
if cx < 110:
drone.sendControlPosition(0, 0.1, 0, 0.5, 0, 0)
elif cx > 130:
drone.sendControlPosition(0, -0.1, 0, 0.5, 0, 0)
else:
print('x ok x : ', cx)
time.sleep(1)
drone.sendControlPosition(-0.3, 0, 0, 0.5, 0, 0)
time.sleep(1)
except Exception as e:
drone.sendControlPosition(-0.3, 0, 0, 0.5, 0, 0)
time.sleep(1)
def find_centroid(drone, flag):
lower_blue = np.array([100, 30, 30])
upper_blue = np.array([110, 255, 255])
while True:
try:
img = cv2.imread(capture_img())
img = cv2.GaussianBlur(img, (9, 9), 3)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_blue, upper_blue)
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
if len(hierarchy[0]) <= 1 and flag == 1:
print("go back")
drone.sendControlPosition(-0.3, 0, 0, 0.5, 0, 0)
time.sleep(1)
elif len(hierarchy[0]) <= 1 and flag != 1:
drone.sendControlPosition(-0.3, 0, 0, 0.5, 0, 0)
move_to_center(drone)
else:
cnt = contours[0]
M = cv2.moments(cnt)
cx = int(M['m10'] / (M['m00'] + 0.000000000000001))
cy = int(M['m01'] / (M['m00'] + 0.000000000000001))
print(cx, cy)
return cx, cy
except Exception as e :
drone.sendControlPosition(-0.3, 0, 0, 0.5, 0, 0)
time.sleep(2)
def match_center(drone, flag):
while not check_y(drone):
cy = find_centroid(drone, flag)[1]
if cy < 140:
drone.sendControlPosition(0, 0, 0.1, 0.5, 0, 0)
elif cy > 160:
drone.sendControlPosition(0, 0, -0.1, 0.5, 0, 0)
else:
print('y ok y : ', cy)
time.sleep(1)
time.sleep(1)
while not check_x(drone):
cx = find_centroid(drone, flag)[0]
if cx < 110:
drone.sendControlPosition(0, 0.1, 0, 0.5, 0, 0)
elif cx > 130:
drone.sendControlPosition(0, -0.1, 0, 0.5, 0, 0)
else:
print('x ok x : ',cx)
time.sleep(1)
time.sleep(1)
pass_obstacle(drone)
def check_x(drone):
try:
lower_blue = np.array([100, 30, 30])
upper_blue = np.array([110, 255, 255])
img = cv2.imread(capture_img())
img = cv2.GaussianBlur(img, (9, 9), 3)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_blue, upper_blue)
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
M = cv2.moments(cnt)
cx = int(M['m10'] / (M['m00'] + 0.000000000000001))
cy = int(M['m01'] / (M['m00'] + 0.000000000000001))
print('check_x : ', cx)
if abs(cx - 120) <= 10:
print('x true')
return True
else:
return False
except Exception as e:
return False
def check_y(drone):
try:
lower_blue = np.array([100, 30, 30])
upper_blue = np.array([110, 255, 255])
img = cv2.imread(capture_img())
img = cv2.GaussianBlur(img, (9, 9), 3)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_blue, upper_blue)
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
M = cv2.moments(cnt)
cx = int(M['m10'] / (M['m00'] + 0.000000000000001))
cy = int(M['m01'] / (M['m00'] + 0.000000000000001))
print('check_y : ', cy)
if abs(cy - 150) <= 10:
print('y true')
return True
else:
return False
except Exception as e:
return False
def find_redpoint():
img = cv2.imread('img.jpg')
upper_red = np.array([20, 255, 255])
lower_red = np.array([0, 30, 0])
img = cv2.GaussianBlur(img, (9, 9), 2.5)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_red, upper_red)
point_red = np.nonzero(mask)
num_point_red = np.size(point_red)
print('red : ', num_point_red)
return num_point_red
def find_purplepoint():
img = cv2.imread(capture_img())
upper_purple = np.array([125, 255, 255])
lower_purple = np.array([110, 0, 0])
img = cv2.GaussianBlur(img, (9, 9), 2.5)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_purple, upper_purple)
point_purple = np.nonzero(mask)
num_point_purple = np.size(point_purple)
print('purple : ', num_point_purple)
return num_point_purple
def pass_obstacle(drone):
while True:
if find_purplepoint() > 1000:
print('detect purple point')
drone.sendLanding()
drone.close()
return 0
if find_redpoint() < 1000:
drone.sendControlPosition(0.4, 0, 0, 0.5, 0, 0)
time.sleep(1)
print('not find red(purple) point')
else:
drone.sendControlPosition(0, 0, 0, 0, 90, 45)
print('find red point')
time.sleep(3)
drone.sendControlWhile(0, 0, 0, 0, 1000)
time.sleep(1)
drone.sendControlPosition16(10, 0, 0, 5, 0, 0)
time.sleep(2)
return 0