25 import matplotlib.pylab
as plt
26 from tabulate
import tabulate
29 def getPaths(results):
31 for i
in range(len(results)):
34 paths.append(results[i].path)
37 for result
in results[i]:
38 paths.append(result.path)
42 def getTimes(results):
44 for i
in range(len(results)):
47 times.append(results[i].planning_time.nanosec/1e09 + results[i].planning_time.sec)
50 for result
in results[i]:
51 times.append(result.smoothing_duration.nanosec/1e09 + result.smoothing_duration.sec)
55 def getMapCoordsFromPaths(paths, resolution):
60 for pose
in path.poses:
61 x.append(pose.pose.position.x/resolution)
62 y.append(pose.pose.position.y/resolution)
68 def getPathLength(path):
70 x_prev = path.poses[0].pose.position.x
71 y_prev = path.poses[0].pose.position.y
72 for i
in range(1, len(path.poses)):
73 x_curr = path.poses[i].pose.position.x
74 y_curr = path.poses[i].pose.position.y
75 path_length = path_length + math.sqrt((x_curr-x_prev)**2 + (y_curr-y_prev)**2)
81 def getSmoothness(pt_prev, pt, pt_next):
85 return np.dot(delta, delta)
87 def getPathSmoothnesses(paths):
89 pm0 = np.array([0.0, 0.0])
90 pm1 = np.array([0.0, 0.0])
91 pm2 = np.array([0.0, 0.0])
94 for i
in range(2, len(path.poses)):
95 pm0[0] = path.poses[i].pose.position.x
96 pm0[1] = path.poses[i].pose.position.y
97 pm1[0] = path.poses[i-1].pose.position.x
98 pm1[1] = path.poses[i-1].pose.position.y
99 pm2[0] = path.poses[i-2].pose.position.x
100 pm2[1] = path.poses[i-2].pose.position.y
101 smoothness += getSmoothness(pm2, pm1, pm0)
102 smoothnesses.append(smoothness)
106 def arcCenter(pt_prev, pt, pt_next):
112 d1_norm = d1 / np.linalg.norm(d1)
113 d2_norm = d2 / np.linalg.norm(d2)
114 cos_angle = np.dot(d1_norm, d2_norm)
116 if cos_angle < cusp_thresh:
121 det = d1[0] * d2[1] - d1[1] * d2[0]
123 return (float(
'inf'), float(
'inf'))
129 mid1 = (pt_prev + pt) / 2
130 mid2 = (pt + pt_next) / 2
133 det1 = (mid1[0] + n1[0]) * mid1[1] - (mid1[1] + n1[1]) * mid1[0]
134 det2 = (mid2[0] + n2[0]) * mid2[1] - (mid2[1] + n2[1]) * mid2[0]
135 center = np.array([(det1 * n2[0] - det2 * n1[0]) / det, (det1 * n2[1] - det2 * n1[1]) / det])
138 def getPathCurvatures(paths):
140 pm0 = np.array([0.0, 0.0])
141 pm1 = np.array([0.0, 0.0])
142 pm2 = np.array([0.0, 0.0])
145 for i
in range(2, len(path.poses)):
146 pm0[0] = path.poses[i].pose.position.x
147 pm0[1] = path.poses[i].pose.position.y
148 pm1[0] = path.poses[i-1].pose.position.x
149 pm1[1] = path.poses[i-1].pose.position.y
150 pm2[0] = path.poses[i-2].pose.position.x
151 pm2[1] = path.poses[i-2].pose.position.y
152 center = arcCenter(pm2, pm1, pm0)
153 if center[0] != float(
'inf'):
154 turning_rad = np.linalg.norm(pm1 - center);
155 radiuses.append(turning_rad)
156 curvatures.append(np.average(radiuses))
159 def plotResults(costmap, paths):
160 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
161 data = np.asarray(costmap.data)
162 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
163 data = np.where(data <= 253, 0, data)
166 ax = sns.heatmap(data, cmap=
'Greys', cbar=
False)
167 for i
in range(0, len(coords), 2):
168 ax.plot(coords[i], coords[i+1], linewidth=0.7)
170 ax.set_aspect(
'equal',
'box')
174 def averagePathCost(paths, costmap, num_of_planners):
175 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
176 data = np.asarray(costmap.data)
177 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
179 average_path_costs = []
180 for i
in range(num_of_planners):
181 average_path_costs.append([])
184 for i
in range(0, len(coords), 2):
186 for j
in range(len(coords[i])):
187 costs.append(data[math.floor(coords[i+1][j])][math.floor(coords[i][j])])
188 average_path_costs[k % num_of_planners].append(sum(costs)/len(costs))
191 return average_path_costs
194 def maxPathCost(paths, costmap, num_of_planners):
195 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
196 data = np.asarray(costmap.data)
197 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
200 for i
in range(num_of_planners):
201 max_path_costs.append([])
204 for i
in range(0, len(coords), 2):
206 for j
in range(len(coords[i])):
207 cost = data[math.floor(coords[i+1][j])][math.floor(coords[i][j])]
210 max_path_costs[k % num_of_planners].append(max_cost)
213 return max_path_costs
218 benchmark_dir = os.getcwd()
220 with open(os.path.join(benchmark_dir,
'results.pickle'),
'rb')
as f:
221 results = pickle.load(f)
223 with open(os.path.join(benchmark_dir,
'methods.pickle'),
'rb')
as f:
224 smoothers = pickle.load(f)
225 planner = smoothers[0]
227 methods_num = len(smoothers) + 1
229 with open(os.path.join(benchmark_dir,
'costmap.pickle'),
'rb')
as f:
230 costmap = pickle.load(f)
233 paths = getPaths(results)
237 path_lengths.append(getPathLength(path))
238 path_lengths = np.asarray(path_lengths)
239 total_paths = len(paths)
242 path_lengths.resize((int(total_paths/methods_num), methods_num))
244 path_lengths = path_lengths.transpose()
247 times = getTimes(results)
248 times = np.asarray(times)
249 times.resize((int(total_paths/methods_num), methods_num))
250 times = np.transpose(times)
253 average_path_costs = np.asarray(averagePathCost(paths, costmap, methods_num))
254 max_path_costs = np.asarray(maxPathCost(paths, costmap, methods_num))
257 smoothnesses = getPathSmoothnesses(paths)
258 smoothnesses = np.asarray(smoothnesses)
259 smoothnesses.resize((int(total_paths/methods_num), methods_num))
260 smoothnesses = np.transpose(smoothnesses)
263 curvatures = getPathCurvatures(paths)
264 curvatures = np.asarray(curvatures)
265 curvatures.resize((int(total_paths/methods_num), methods_num))
266 curvatures = np.transpose(curvatures)
269 planner_table = [[
'Planner',
274 'Path smoothness (x100)',
275 'Average turning rad (m)']]
277 planner_table.append([planner,
278 np.average(times[0]),
279 np.average(path_lengths[0]),
280 np.average(average_path_costs[0]),
281 np.average(max_path_costs[0]),
282 np.average(smoothnesses[0]) * 100,
283 np.average(curvatures[0])])
285 for i
in range(1, methods_num):
286 planner_table.append([smoothers[i-1],
287 np.average(times[i]),
288 np.average(path_lengths[i]),
289 np.average(average_path_costs[i]),
290 np.average(max_path_costs[i]),
291 np.average(smoothnesses[i]) * 100,
292 np.average(curvatures[i])])
295 print(tabulate(planner_table))
296 plotResults(costmap, paths)
301 if __name__ ==
'__main__':