-
Notifications
You must be signed in to change notification settings - Fork 7
/
drone_goal_search.py
225 lines (169 loc) · 5.62 KB
/
drone_goal_search.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
AUTHORS:
Ernest J. Quant
Ivan A. Reyes
Orson Meyreles
John Quitto-Graham
Carlos Valdes
Maria Celeste Carbonell
"""
from mpl_toolkits.mplot3d import Axes3D
from queue import PriorityQueue
from airsim import *
import matplotlib.pyplot as plt
import setup_path
import airsim
import math
import time
import os
class Node:
def __init__ (self,cargo=None,parent=None):
self.cargo = cargo
self.parent = parent
def __str__(self):
return str(self.cargo)
def getCargo(self):
return self.cargo
def getParent(self):
return self.parent
def printPathAndGraph(lastNode):
path = []
xCords = []
yCords = []
zCords = []
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
while lastNode.parent != None:
x,y = lastNode.getCargo()
xCords.insert(0,x)
yCords.insert(0,y)
zCords.insert(0,-1)
path.insert(0,(x,y))
lastNode = lastNode.parent
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
print(path)
def graphPath():
return 0
def calcHeuristic(startX,startY,goalX,goalY):
heuristic = math.sqrt(((goalX-startX)**2+(goalY-startY)**2))
return heuristic
"""
Checks to see if the drone can move front, left, right or backwards from it's current position.
"""
def checkAdj(xCord,yCord,visitedList,drone):
north = False
east = False
south = False
west = False
noSafe = False
tmp_dir = r"C:\Users\Ernest\Pictures\DronePic"
imagequeue = []
direct = [north,east,south,west]
# FRONT PICTURE
drone.moveToPositionAsync(3,0,-1,0.00001,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
time.sleep(5)
responses = drone.simGetImages([airsim.ImageRequest(0,airsim.ImageType.Scene)])
imagequeue.append(responses[0].image_data_uint8)
# RIGHT PICTURE
drone.moveToPositionAsync(0,3,-1,0.00001,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
time.sleep(5)
responses = drone.simGetImages([airsim.ImageRequest(0,airsim.ImageType.Scene)])
imagequeue.append(responses[0].image_data_uint8)
# BACK PICTURE
drone.moveToPositionAsync(-3,0,-1,0.00001,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
time.sleep(5)
responses = drone.simGetImages([airsim.ImageRequest(0,airsim.ImageType.Scene)])
imagequeue.append(responses[0].image_data_uint8)
# LEFT PICTURE
drone.moveToPositionAsync(0,-3,-1,0.00001,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
time.sleep(5)
responses = drone.simGetImages([airsim.ImageRequest(0,airsim.ImageType.Scene)])
imagequeue.append(responses[0].image_data_uint8)
# FACE FRONT AGAIN
drone.moveToPositionAsync(3,0,-1,0.00001,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
for i in range(len(imagequeue)):
filename = os.path.join(tmp_dir, str(i))
airsim.write_file(os.path.normpath(filename + '.png'),imagequeue[i])
""" USED FOR TESTING, DELETE WHEN FINISHED """
yah = input("n true: ").lower()
if yah == "t":
north = True
yah = input("e true: ").lower()
if yah == "t":
east = True
yah = input("s true: ").lower()
if yah == "t":
south = True
yah = input("w true: ").lower()
if yah == "t":
west = True
""" WILL BE ACTUAL IMPLEMENTATION WHEN CNN IS FINISHED, DELETE ABOVE WHEN UNCOMMENTING THIS"""
"""if safeCheck() and not in visited:
north = True
if safeCheck() and not in visited:
east = True
if safeCheck() and not in visited:
south = True
if safeCheck() and not in visited:
west = True"""
if not any(direct):
noSafe = True
return north, east, south, west, noSafe
def yahSearch(goal):
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)
client.takeoffAsync().join()
goalX,goalY = goal
isGoal = False
pq = PriorityQueue()
visited = []
currentNode = Node((0,0))
isGoal = False
pq.put((1,(currentNode)))
while not isGoal:
z = pq.get()
while not pq.empty():
pq.get()
h = z[0]
x,y = z[1].getCargo()
print(x,y)
currentNode = z[1]
print(currentNode)
client.moveToPositionAsync(x,y,-1,5,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
visited.append((x,y))
# GOAL TEST
if (x,y) == (goalX,goalY):
break
northSafe, eastSafe, southSafe, westSafe,noneSafe = checkAdj(x,y,visited,client)
if northSafe:
newNode1 = Node((x+10,y),currentNode)
dist = calcHeuristic(x+10,y,goalX,goalY)
pq.put((dist,(newNode1)))
if eastSafe:
newNode2 = Node((x,y+10),currentNode)
dist = calcHeuristic(x,y+10,goalX,goalY)
pq.put((dist,(newNode2)))
if southSafe:
newNode3 = Node((x-10,y),currentNode)
dist = calcHeuristic(x-10,y,goalX,goalY)
pq.put((dist,(newNode3)))
if westSafe:
newNode4 = Node((x,y-10),currentNode)
dist = calcHeuristic(x,y-10,goalX,goalY)
pq.put((dist,(newNode4)))
if noneSafe:
currentNode = currentNode.parent
x,y = currentNode.getCargo()
h = calcHeuristic(x,y,goalX,goalY)
pq.put((h,(currentNode)))
returnPath(currentNode)
def main():
yahSearch((60,100))
if __name__ == "__main__":
main()