16 from geometry_msgs.msg
import PoseStamped
19 from ament_index_python.packages
import get_package_share_directory
28 from random
import seed
29 from random
import randint
30 from random
import uniform
32 from transforms3d.euler
import euler2quat
35 def getPlannerResults(navigator, initial_pose, goal_pose, planners):
37 for planner
in planners:
38 path = navigator._getPathImpl(initial_pose, goal_pose, planner, use_start=
True)
46 def getRandomStart(costmap, max_cost, side_buffer, time_stamp, res):
48 start.header.frame_id =
'map'
49 start.header.stamp = time_stamp
51 row = randint(side_buffer, costmap.shape[0]-side_buffer)
52 col = randint(side_buffer, costmap.shape[1]-side_buffer)
54 if costmap[row, col] < max_cost:
55 start.pose.position.x = col*res
56 start.pose.position.y = row*res
58 yaw = uniform(0, 1) * 2*math.pi
59 quad = euler2quat(0.0, 0.0, yaw)
60 start.pose.orientation.w = quad[0]
61 start.pose.orientation.x = quad[1]
62 start.pose.orientation.y = quad[2]
63 start.pose.orientation.z = quad[3]
68 def getRandomGoal(costmap, start, max_cost, side_buffer, time_stamp, res):
70 goal.header.frame_id =
'map'
71 goal.header.stamp = time_stamp
73 row = randint(side_buffer, costmap.shape[0]-side_buffer)
74 col = randint(side_buffer, costmap.shape[1]-side_buffer)
76 start_x = start.pose.position.x
77 start_y = start.pose.position.y
80 x_diff = goal_x - start_x
81 y_diff = goal_y - start_y
82 dist = math.sqrt(x_diff ** 2 + y_diff ** 2)
84 if costmap[row, col] < max_cost
and dist > 3.0:
85 goal.pose.position.x = goal_x
86 goal.pose.position.y = goal_y
88 yaw = uniform(0, 1) * 2*math.pi
89 quad = euler2quat(0.0, 0.0, yaw)
90 goal.pose.orientation.w = quad[0]
91 goal.pose.orientation.x = quad[1]
92 goal.pose.orientation.y = quad[2]
93 goal.pose.orientation.z = quad[3]
101 navigator = BasicNavigator()
104 map_path = os.getcwd() +
'/' + glob.glob(
'**/100by100_20.yaml', recursive=
True)[0]
105 navigator.changeMap(map_path)
109 costmap_msg = navigator.getGlobalCostmap()
110 costmap = np.asarray(costmap_msg.data)
111 costmap.resize(costmap_msg.metadata.size_y, costmap_msg.metadata.size_x)
113 planners = [
'Navfn',
'ThetaStar',
'SmacHybrid',
'Smac2d',
'SmacLattice']
116 time_stamp = navigator.get_clock().now().to_msg()
121 res = costmap_msg.metadata.resolution
123 while len(results) != random_pairs:
124 print(
"Cycle: ", i,
"out of: ", random_pairs)
125 start = getRandomStart(costmap, max_cost, side_buffer, time_stamp, res)
126 goal = getRandomGoal(costmap, start, max_cost, side_buffer, time_stamp, res)
127 print(
"Start", start)
129 result = getPlannerResults(navigator, start, goal, planners)
130 if len(result) == len(planners):
131 results.append(result)
134 print(
"One of the planners was invalid")
136 print(
"Write Results...")
137 with open(os.getcwd() +
'/results.pickle',
'wb+')
as f:
138 pickle.dump(results, f, pickle.HIGHEST_PROTOCOL)
140 with open(os.getcwd() +
'/costmap.pickle',
'wb+')
as f:
141 pickle.dump(costmap_msg, f, pickle.HIGHEST_PROTOCOL)
143 with open(os.getcwd() +
'/planners.pickle',
'wb+')
as f:
144 pickle.dump(planners, f, pickle.HIGHEST_PROTOCOL)
145 print(
"Write Complete")
149 if __name__ ==
'__main__':