20 from random
import randint, seed, uniform
23 from geometry_msgs.msg
import PoseStamped
27 from transforms3d.euler
import euler2quat
30 def getPlannerResults(navigator, initial_pose, goal_pose, planners):
32 for planner
in planners:
33 path = navigator._getPathImpl(initial_pose, goal_pose, planner, use_start=
True)
34 if path
is not None and path.error_code == 0:
37 print(planner,
'planner failed to produce the path')
42 def getRandomStart(costmap, max_cost, side_buffer, time_stamp, res):
44 start.header.frame_id =
'map'
45 start.header.stamp = time_stamp
47 row = randint(side_buffer, costmap.shape[0] - side_buffer)
48 col = randint(side_buffer, costmap.shape[1] - side_buffer)
50 if costmap[row, col] < max_cost:
51 start.pose.position.x = col * res
52 start.pose.position.y = row * res
54 yaw = uniform(0, 1) * 2 * math.pi
55 quad = euler2quat(0.0, 0.0, yaw)
56 start.pose.orientation.w = quad[0]
57 start.pose.orientation.x = quad[1]
58 start.pose.orientation.y = quad[2]
59 start.pose.orientation.z = quad[3]
64 def getRandomGoal(costmap, start, max_cost, side_buffer, time_stamp, res):
66 goal.header.frame_id =
'map'
67 goal.header.stamp = time_stamp
69 row = randint(side_buffer, costmap.shape[0] - side_buffer)
70 col = randint(side_buffer, costmap.shape[1] - side_buffer)
72 start_x = start.pose.position.x
73 start_y = start.pose.position.y
76 x_diff = goal_x - start_x
77 y_diff = goal_y - start_y
78 dist = math.sqrt(x_diff ** 2 + y_diff ** 2)
80 if costmap[row, col] < max_cost
and dist > 3.0:
81 goal.pose.position.x = goal_x
82 goal.pose.position.y = goal_y
84 yaw = uniform(0, 1) * 2 * math.pi
85 quad = euler2quat(0.0, 0.0, yaw)
86 goal.pose.orientation.w = quad[0]
87 goal.pose.orientation.x = quad[1]
88 goal.pose.orientation.y = quad[2]
89 goal.pose.orientation.z = quad[3]
97 navigator = BasicNavigator()
100 map_path = os.getcwd() +
'/' + glob.glob(
'**/100by100_20.yaml', recursive=
True)[0]
101 navigator.changeMap(map_path)
105 costmap_msg = navigator.getGlobalCostmap()
106 costmap = np.asarray(costmap_msg.data)
107 costmap.resize(costmap_msg.metadata.size_y, costmap_msg.metadata.size_x)
109 planners = [
'Navfn',
'ThetaStar',
'SmacHybrid',
'Smac2d',
'SmacLattice']
112 time_stamp = navigator.get_clock().now().to_msg()
117 res = costmap_msg.metadata.resolution
119 while len(results) != random_pairs:
120 print(
'Cycle: ', i,
'out of: ', random_pairs)
121 start = getRandomStart(costmap, max_cost, side_buffer, time_stamp, res)
122 goal = getRandomGoal(costmap, start, max_cost, side_buffer, time_stamp, res)
123 print(
'Start', start)
125 result = getPlannerResults(navigator, start, goal, planners)
126 if len(result) == len(planners):
127 results.append(result)
130 print(
'One of the planners was invalid')
132 print(
'Write Results...')
133 with open(os.getcwd() +
'/results.pickle',
'wb+')
as f:
134 pickle.dump(results, f, pickle.HIGHEST_PROTOCOL)
136 with open(os.getcwd() +
'/costmap.pickle',
'wb+')
as f:
137 pickle.dump(costmap_msg, f, pickle.HIGHEST_PROTOCOL)
139 with open(os.getcwd() +
'/planners.pickle',
'wb+')
as f:
140 pickle.dump(planners, f, pickle.HIGHEST_PROTOCOL)
141 print(
'Write Complete')
145 if __name__ ==
'__main__':