22 import matplotlib.pylab
as plt
25 from tabulate
import tabulate
28 def getPaths(results):
30 for i
in range(len(results)):
33 paths.append(results[i].path)
36 for result
in results[i]:
37 paths.append(result.path)
41 def getTimes(results):
43 for i
in range(len(results)):
47 results[i].planning_time.nanosec / 1e09 + results[i].planning_time.sec
51 for result
in results[i]:
53 result.smoothing_duration.nanosec / 1e09
54 + result.smoothing_duration.sec
59 def getMapCoordsFromPaths(paths, resolution):
64 for pose
in path.poses:
65 x.append(pose.pose.position.x / resolution)
66 y.append(pose.pose.position.y / resolution)
72 def getPathLength(path):
74 x_prev = path.poses[0].pose.position.x
75 y_prev = path.poses[0].pose.position.y
76 for i
in range(1, len(path.poses)):
77 x_curr = path.poses[i].pose.position.x
78 y_curr = path.poses[i].pose.position.y
79 path_length = path_length + math.sqrt(
80 (x_curr - x_prev) ** 2 + (y_curr - y_prev) ** 2
88 def getSmoothness(pt_prev, pt, pt_next):
92 return np.linalg.norm(delta)
95 def getPathSmoothnesses(paths):
97 pm0 = np.array([0.0, 0.0])
98 pm1 = np.array([0.0, 0.0])
99 pm2 = np.array([0.0, 0.0])
102 for i
in range(2, len(path.poses)):
103 pm0[0] = path.poses[i].pose.position.x
104 pm0[1] = path.poses[i].pose.position.y
105 pm1[0] = path.poses[i - 1].pose.position.x
106 pm1[1] = path.poses[i - 1].pose.position.y
107 pm2[0] = path.poses[i - 2].pose.position.x
108 pm2[1] = path.poses[i - 2].pose.position.y
109 smoothness += getSmoothness(pm2, pm1, pm0)
110 smoothnesses.append(smoothness)
115 def arcCenter(pt_prev, pt, pt_next):
121 d1_norm = d1 / np.linalg.norm(d1)
122 d2_norm = d2 / np.linalg.norm(d2)
123 cos_angle = np.dot(d1_norm, d2_norm)
125 if cos_angle < cusp_thresh:
130 det = d1[0] * d2[1] - d1[1] * d2[0]
132 return (float(
'inf'), float(
'inf'))
138 mid1 = (pt_prev + pt) / 2
139 mid2 = (pt + pt_next) / 2
142 det1 = (mid1[0] + n1[0]) * mid1[1] - (mid1[1] + n1[1]) * mid1[0]
143 det2 = (mid2[0] + n2[0]) * mid2[1] - (mid2[1] + n2[1]) * mid2[0]
145 [(det1 * n2[0] - det2 * n1[0]) / det, (det1 * n2[1] - det2 * n1[1]) / det]
150 def getPathCurvatures(paths):
152 pm0 = np.array([0.0, 0.0])
153 pm1 = np.array([0.0, 0.0])
154 pm2 = np.array([0.0, 0.0])
157 for i
in range(2, len(path.poses)):
158 pm0[0] = path.poses[i].pose.position.x
159 pm0[1] = path.poses[i].pose.position.y
160 pm1[0] = path.poses[i - 1].pose.position.x
161 pm1[1] = path.poses[i - 1].pose.position.y
162 pm2[0] = path.poses[i - 2].pose.position.x
163 pm2[1] = path.poses[i - 2].pose.position.y
164 center = arcCenter(pm2, pm1, pm0)
165 if center[0] != float(
'inf'):
166 turning_rad = np.linalg.norm(pm1 - center)
167 radiuses.append(turning_rad)
168 curvatures.append(np.average(radiuses))
172 def plotResults(costmap, paths):
173 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
174 data = np.asarray(costmap.data)
175 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
176 data = np.where(data <= 253, 0, data)
179 ax = sns.heatmap(data, cmap=
'Greys', cbar=
False)
180 for i
in range(0, len(coords), 2):
181 ax.plot(coords[i], coords[i + 1], linewidth=0.7)
183 ax.set_aspect(
'equal',
'box')
187 def averagePathCost(paths, costmap, num_of_planners):
188 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
189 data = np.asarray(costmap.data)
190 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
192 average_path_costs = []
193 for i
in range(num_of_planners):
194 average_path_costs.append([])
197 for i
in range(0, len(coords), 2):
199 for j
in range(len(coords[i])):
200 costs.append(data[math.floor(coords[i + 1][j])][math.floor(coords[i][j])])
201 average_path_costs[k % num_of_planners].append(sum(costs) / len(costs))
204 return average_path_costs
207 def maxPathCost(paths, costmap, num_of_planners):
208 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
209 data = np.asarray(costmap.data)
210 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
213 for i
in range(num_of_planners):
214 max_path_costs.append([])
217 for i
in range(0, len(coords), 2):
219 for j
in range(len(coords[i])):
220 cost = data[math.floor(coords[i + 1][j])][math.floor(coords[i][j])]
223 max_path_costs[k % num_of_planners].append(max_cost)
226 return max_path_costs
231 benchmark_dir = os.getcwd()
233 with open(os.path.join(benchmark_dir,
'results.pickle'),
'rb')
as f:
234 results = pickle.load(f)
236 with open(os.path.join(benchmark_dir,
'methods.pickle'),
'rb')
as f:
237 smoothers = pickle.load(f)
238 planner = smoothers[0]
240 methods_num = len(smoothers) + 1
242 with open(os.path.join(benchmark_dir,
'costmap.pickle'),
'rb')
as f:
243 costmap = pickle.load(f)
246 paths = getPaths(results)
250 path_lengths.append(getPathLength(path))
251 path_lengths = np.asarray(path_lengths)
252 total_paths = len(paths)
255 path_lengths.resize((int(total_paths / methods_num), methods_num))
257 path_lengths = path_lengths.transpose()
260 times = getTimes(results)
261 times = np.asarray(times)
262 times.resize((int(total_paths / methods_num), methods_num))
263 times = np.transpose(times)
266 average_path_costs = np.asarray(averagePathCost(paths, costmap, methods_num))
267 max_path_costs = np.asarray(maxPathCost(paths, costmap, methods_num))
270 smoothnesses = getPathSmoothnesses(paths)
271 smoothnesses = np.asarray(smoothnesses)
272 smoothnesses.resize((int(total_paths / methods_num), methods_num))
273 smoothnesses = np.transpose(smoothnesses)
276 curvatures = getPathCurvatures(paths)
277 curvatures = np.asarray(curvatures)
278 curvatures.resize((int(total_paths / methods_num), methods_num))
279 curvatures = np.transpose(curvatures)
289 'Path smoothness (x100)',
290 'Average turning rad (m)',
294 planner_table.append(
297 np.average(times[0]),
298 np.average(path_lengths[0]),
299 np.average(average_path_costs[0]),
300 np.average(max_path_costs[0]),
301 np.average(smoothnesses[0]) * 100,
302 np.average(curvatures[0]),
306 for i
in range(1, methods_num):
307 planner_table.append(
310 np.average(times[i]),
311 np.average(path_lengths[i]),
312 np.average(average_path_costs[i]),
313 np.average(max_path_costs[i]),
314 np.average(smoothnesses[i]) * 100,
315 np.average(curvatures[i]),
320 print(tabulate(planner_table))
321 plotResults(costmap, paths)
326 if __name__ ==
'__main__':