-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
241 lines (193 loc) · 7.28 KB
/
main.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
226
227
228
229
230
231
232
import numpy as np
import time
import matplotlib.pyplot as plt; plt.ion()
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import Planner
import CollisionDetection as CD
import WeightedAStarPlanner as Astar
import rrtSPlanner as rrts
def tic():
return time.time()
def toc(tstart, nm=""):
print('%s took: %s sec.\n' % (nm,(time.time() - tstart)))
def load_map(fname):
'''
Loads the bounady and blocks from map file fname.
boundary = [['xmin', 'ymin', 'zmin', 'xmax', 'ymax', 'zmax','r','g','b']]
blocks = [['xmin', 'ymin', 'zmin', 'xmax', 'ymax', 'zmax','r','g','b'],
...,
['xmin', 'ymin', 'zmin', 'xmax', 'ymax', 'zmax','r','g','b']]
'''
mapdata = np.loadtxt(fname,dtype={'names': ('type', 'xmin', 'ymin', 'zmin', 'xmax', 'ymax', 'zmax','r','g','b'),\
'formats': ('S8','f', 'f', 'f', 'f', 'f', 'f', 'f','f','f')})
blockIdx = mapdata['type'] == b'block'
boundary = mapdata[~blockIdx][['xmin', 'ymin', 'zmin', 'xmax', 'ymax', 'zmax','r','g','b']].view('<f4').reshape(-1,11)[:,2:]
blocks = mapdata[blockIdx][['xmin', 'ymin', 'zmin', 'xmax', 'ymax', 'zmax','r','g','b']].view('<f4').reshape(-1,11)[:,2:]
return boundary, blocks
def draw_map(boundary, blocks, start, goal):
'''
Visualization of a planning problem with environment boundary, obstacle blocks, and start and goal points
'''
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
hb = draw_block_list(ax,blocks)
hs = ax.plot(start[0:1],start[1:2],start[2:],'ro',markersize=7,markeredgecolor='k')
hg = ax.plot(goal[0:1],goal[1:2],goal[2:],'go',markersize=7,markeredgecolor='k')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(boundary[0,0],boundary[0,3])
ax.set_ylim(boundary[0,1],boundary[0,4])
ax.set_zlim(boundary[0,2],boundary[0,5])
return fig, ax, hb, hs, hg
def draw_block_list(ax,blocks):
'''
Subroutine used by draw_map() to display the environment blocks
'''
v = np.array([[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]],dtype='float')
f = np.array([[0,1,5,4],[1,2,6,5],[2,3,7,6],[3,0,4,7],[0,1,2,3],[4,5,6,7]])
clr = blocks[:,6:]/255
n = blocks.shape[0]
d = blocks[:,3:6] - blocks[:,:3]
vl = np.zeros((8*n,3))
fl = np.zeros((6*n,4),dtype='int64')
fcl = np.zeros((6*n,3))
for k in range(n):
vl[k*8:(k+1)*8,:] = v * d[k] + blocks[k,:3]
fl[k*6:(k+1)*6,:] = f + k*8
fcl[k*6:(k+1)*6,:] = clr[k,:]
if type(ax) is Poly3DCollection:
ax.set_verts(vl[fl])
else:
pc = Poly3DCollection(vl[fl], alpha=0.25, linewidths=1, edgecolors='k')
pc.set_facecolor(fcl)
h = ax.add_collection3d(pc)
return h
def runtest(mapfile, start, goal, verbose = True, search = True):
'''
This function:
* load the provided mapfile
* creates a motion planner
* plans a path from start to goal
* checks whether the path is collision free and reaches the goal
* computes the path length as a sum of the Euclidean norm of the path segments
'''
# Load a map and instantiate a motion planner
boundary, blocks = load_map(mapfile)
# Display the environment
if verbose:
fig, ax, hb, hs, hg = draw_map(boundary, blocks, start, goal)
# search_based_planning
if search:
MP = Astar.Planner(boundary, blocks, resolution=0.2) # TODO: replace this with your own planner implementation
# Call the motion planner
t0 = tic()
path = MP.plan(start, goal, weight=1.5)
toc(t0, "Planning")
# sample_based_planning
else:
MP = rrts.Planner(boundary, blocks) # TODO: replace this with your own planner implementation
# Call the motion planner
t0 = tic()
path = MP.plan(start, goal)
toc(t0, "Planning")
# Plot the path
if verbose:
ax.plot(path[:,0],path[:,1],path[:,2],'r-')
# TODO: You should verify whether the path actually intersects any of the obstacles in continuous space
# TODO: You can implement your own algorithm or use an existing library for segment and
# axis-aligned bounding box (AABB) intersection
collision = CD.collision_detector(path, blocks, edge=False)
goal_reached = sum((path[-1]-goal)**2) <= 0.1
success = (not collision) and goal_reached
pathlength = np.sum(np.sqrt(np.sum(np.diff(path,axis=0)**2,axis=1)))
return success, pathlength
def test_single_cube(verbose = False,search = True):
print('Running single cube test...\n')
start = np.array([2.3, 2.3, 1.3])
goal = np.array([7.0, 7.0, 5.5])
success, pathlength = runtest('./maps/single_cube.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
def test_maze(verbose = False,search = True):
print('Running maze test...\n')
start = np.array([0.0, 0.0, 1.0])
goal = np.array([12.0, 12.0, 5.0])
success, pathlength = runtest('./maps/maze.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
def test_window(verbose = False, search = True):
print('Running window test...\n')
start = np.array([0.2, -4.9, 0.2])
goal = np.array([6.0, 18.0, 3.0])
success, pathlength = runtest('./maps/window.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
def test_tower(verbose = False, search = True):
print('Running tower test...\n')
start = np.array([2.5, 4.0, 0.5])
goal = np.array([4.0, 2.5, 19.5])
success, pathlength = runtest('./maps/tower.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
def test_flappy_bird(verbose = False, search = True):
print('Running flappy bird test...\n')
start = np.array([0.5, 2.5, 5.5])
goal = np.array([19.0, 2.5, 5.5])
success, pathlength = runtest('./maps/flappy_bird.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
def test_room(verbose = False, search = True):
print('Running room test...\n')
start = np.array([1.0, 5.0, 1.5])
goal = np.array([9.0, 7.0, 1.5])
success, pathlength = runtest('./maps/room.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
def test_monza(verbose = False, search = True):
print('Running monza test...\n')
start = np.array([0.5, 1.0, 4.9])
goal = np.array([3.8, 1.0, 0.1])
success, pathlength = runtest('./maps/monza.txt', start, goal, verbose, search)
print('Success: %r'%success)
print('Path length: %d'%pathlength)
print('\n')
if __name__=="__main__":
# sample_based_planning
search = False
test_single_cube(True, search)
plt.show(block=True)
test_flappy_bird(True, search)
plt.show(block=True)
test_window(True, search)
plt.show(block=True)
test_room(True, search)
plt.show(block=True)
test_tower(True, search)
plt.show(block=True)
test_monza(True, search)
plt.show(block=True)
test_maze(True, search)
plt.show(block=True)
# search_based_planning
test_single_cube(True)
plt.show(block=True)
test_flappy_bird(True)
plt.show(block=True)
test_window(True)
plt.show(block=True)
test_room(True)
plt.show(block=True)
test_tower(True)
plt.show(block=True)
test_monza(True)
plt.show(block=True)
test_maze(True)
plt.show(block=True)