forked from SparkDevF19/ai-uav-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrone_goal_search2.py
199 lines (145 loc) · 4.79 KB
/
drone_goal_search2.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
"""
AUTHORS:
Ernest J. Quant
Ivan A. Reyes
Orson Meyreles
John Quitto-Graham
Carlos Valdes
Maria Celeste Carbonell
"""
from numpy import loadtxt
import tensorflow as tf
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from mpl_toolkits.mplot3d import Axes3D
from queue import PriorityQueue
import setup_path
import airsim
from airsim import *
import matplotlib.pyplot as plt
import math
import time
import os
#Will be used to return the successful path
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(xCords, yCords, zCords, c='r', marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
print(path)
#Checks current path in the X,Y coordinate direction for possible collision
def checkSafe(drone,yawVal,droneNetwork):
imagequeue = []
tmp_dir = r"C:\Users\Ernest\Pictures\DronePic"
drone.moveByAngleZAsync(0,0,-1,yaw=yawVal,duration=1)
time.sleep(4)
responses = drone.simGetImages([airsim.ImageRequest(0,airsim.ImageType.Scene)])
imagequeue.append(responses[0].image_data_uint8)
filename = os.path.join(tmp_dir, "current")
airsim.write_file(os.path.normpath(filename + '.png'),imagequeue[0])
img_addr = r"C:\Users\Ernest\Pictures\DronePic\current.png"
img = image.load_img(img_addr, target_size = (80,80))
img_test = image.img_to_array(img)
img_test = np.expand_dims(img_test, axis=0)
img_test = preprocess_input(img_test)
result = droneNetwork.predict(img_test)
if result == [[1.]]:
return True
else:
return False
def calcHeuristic(startX,startY,goalX,goalY):
heuristic = math.sqrt(((goalX-startX)**2+(goalY-startY)**2))
return heuristic
def yahSearch(goal,droneNetwork):
moveMag = 10
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)))
hpriq = PriorityQueue()
inInitial = True
while not isGoal:
time.sleep(3)
z = pq.get()
h = z[0]
x,y = z[1].getCargo()
curX, curY = (x,y)
currentNode = z[1]
time.sleep(3)
visited.append((x,y))
# GOAL TEST
if (x,y) == (goalX,goalY):
print("GOAL REACHED!")
break
if (x+moveMag,y) not in visited:
upH = (calcHeuristic(x+moveMag,y,goalX,goalY),(x+moveMag,y),0)
hpriq.put(upH)
if (x,y-moveMag) not in visited:
leftH = (calcHeuristic(x,y-moveMag,goalX,goalY),(x,y-moveMag),-1.5)
hpriq.put(leftH)
if inInitial:
downH = (calcHeuristic(x-moveMag,y,goalX,goalY),(x-moveMag,y),3)
hpriq.put(downH)
inInitial = False
if (x,y+moveMag) not in visited:
rightH = (calcHeuristic(x,y+moveMag,goalX,goalY),(x,y+moveMag),1.5)
hpriq.put(rightH)
safeMove = False
while not hpriq.empty():
movement = hpriq.get()
x, y = movement[1]
h = movement[0]
if checkSafe(client,movement[2],droneNetwork):
time.sleep(5)
safeMove = True
newNode = Node((x,y),currentNode)
pq.put((h,(newNode)))
while not hpriq.empty():
hpriq.get()
client.moveToPositionAsync(x,y,-1,3,drivetrain=DrivetrainType.ForwardOnly,yaw_mode=YawMode(False,0))
time.sleep(7)
client.hoverAsync()
break
if safeMove == False:
currentNode = currentNode.parent
x,y = currentNode.getCargo()
h = calcHeuristic(x,y,goalX,goalY)
pq.put((h,(currentNode)))
printPathAndGraph(currentNode)
def main():
network = tf.keras.models.load_model(r'C:\Users\Ernest\AirSim\PythonClient\multirotor\CNN_tester.model')
yahSearch((100,20),network)
if __name__ == "__main__":
main()