20 from typing
import Optional
22 from action_msgs.msg
import GoalStatus
24 from lifecycle_msgs.srv
import GetState
25 from nav2_msgs.action
import NavigateThroughPoses
26 from nav2_msgs.srv
import ManageLifecycleNodes
28 from rclpy.action
import ActionClient
29 from rclpy.node
import Node
30 from rclpy.qos
import QoSDurabilityPolicy, QoSHistoryPolicy, QoSProfile, QoSReliabilityPolicy
35 def __init__(self, initial_pose: Pose, goal_pose: Pose, namespace: str =
''):
36 super().__init__(node_name=
'nav2_tester', namespace=namespace)
38 PoseWithCovarianceStamped,
'initialpose', 10
41 pose_qos = QoSProfile(
42 durability=QoSDurabilityPolicy.TRANSIENT_LOCAL,
43 reliability=QoSReliabilityPolicy.RELIABLE,
44 history=QoSHistoryPolicy.KEEP_LAST,
49 PoseWithCovarianceStamped,
'amcl_pose', self.
poseCallbackposeCallback, pose_qos
55 self, NavigateThroughPoses,
'navigate_through_poses'
58 def info_msg(self, msg: str) ->
None:
59 self.get_logger().info(
'\033[1;37;44m' + msg +
'\033[0m')
61 def warn_msg(self, msg: str) ->
None:
62 self.get_logger().warn(
'\033[1;37;43m' + msg +
'\033[0m')
64 def error_msg(self, msg: str) ->
None:
65 self.get_logger().error(
'\033[1;37;41m' + msg +
'\033[0m')
67 def setInitialPose(self) -> None:
68 msg = PoseWithCovarianceStamped()
70 msg.header.frame_id =
'map'
71 self.
info_msginfo_msg(
'Publishing Initial Pose')
75 def getStampedPoseMsg(self, pose: Pose) -> PoseStamped:
77 msg.header.frame_id =
'map'
81 def runNavigateAction(self, goal_pose: Optional[Pose] =
None) -> bool:
83 self.
info_msginfo_msg(
"Waiting for 'NavigateThroughPoses' action server")
84 while not self.
action_clientaction_client.wait_for_server(timeout_sec=1.0):
86 "'NavigateThroughPoses' action server not available, waiting..."
89 self.
goal_posegoal_pose = goal_pose
if goal_pose
is not None else self.
goal_posegoal_pose
90 goal_msg = NavigateThroughPoses.Goal()
91 goal_msg.poses.header.frame_id =
'map'
92 goal_msg.poses.header.stamp = self.get_clock().now().to_msg()
93 goal_msg.poses.goals = [
98 self.
info_msginfo_msg(
'Sending goal request...')
99 send_goal_future = self.
action_clientaction_client.send_goal_async(goal_msg)
101 rclpy.spin_until_future_complete(self, send_goal_future)
102 goal_handle = send_goal_future.result()
104 if not goal_handle
or not goal_handle.accepted:
108 self.
info_msginfo_msg(
'Goal accepted')
109 get_result_future = goal_handle.get_result_async()
111 self.
info_msginfo_msg(
"Waiting for 'NavigateToPose' action to complete")
112 rclpy.spin_until_future_complete(self, get_result_future)
113 status = get_result_future.result().status
114 if status != GoalStatus.STATUS_SUCCEEDED:
115 result = get_result_future.result().result
116 self.
info_msginfo_msg(f
'Goal failed with status code: {status}'
117 f
' error code:{result.error_code}'
118 f
' error msg:{result.error_msg}')
121 self.
info_msginfo_msg(
'Goal succeeded!')
124 def runFakeNavigateAction(self) -> bool:
126 self.
info_msginfo_msg(
"Waiting for 'NavigateThroughPoses' action server")
127 while not self.
action_clientaction_client.wait_for_server(timeout_sec=1.0):
129 "'NavigateThroughPoses' action server not available, waiting..."
132 goal_msg = NavigateThroughPoses.Goal()
134 self.
info_msginfo_msg(
'Sending goal request...')
135 send_goal_future = self.
action_clientaction_client.send_goal_async(goal_msg)
137 rclpy.spin_until_future_complete(self, send_goal_future)
138 goal_handle = send_goal_future.result()
140 if not goal_handle
or not goal_handle.accepted:
144 self.
info_msginfo_msg(
'Goal accepted')
145 get_result_future = goal_handle.get_result_async()
147 self.
info_msginfo_msg(
"Waiting for 'NavigateToPose' action to complete")
148 rclpy.spin_until_future_complete(self, get_result_future)
149 status = get_result_future.result().status
150 if status != GoalStatus.STATUS_SUCCEEDED:
151 result = get_result_future.result().result
152 self.
info_msginfo_msg(f
'Goal failed with status code: {status}'
153 f
' error code:{result.error_code}'
154 f
' error msg:{result.error_msg}')
157 self.
info_msginfo_msg(
'Goal succeeded!')
160 def runNavigatePreemptionAction(self, block: bool) -> bool:
162 self.
info_msginfo_msg(
"Waiting for 'NavigateThroughPoses' action server")
163 while not self.
action_clientaction_client.wait_for_server(timeout_sec=1.0):
165 "'NavigateThroughPoses' action server not available, waiting..."
168 goal_msg = NavigateThroughPoses.Goal()
171 self.
info_msginfo_msg(
'Sending goal request...')
172 send_goal_future = self.
action_clientaction_client.send_goal_async(goal_msg)
174 rclpy.spin_until_future_complete(self, send_goal_future)
175 goal_handle = send_goal_future.result()
177 if not goal_handle
or not goal_handle.accepted:
184 self.
info_msginfo_msg(
'Goal accepted')
185 get_result_future = goal_handle.get_result_async()
187 self.
info_msginfo_msg(
"Waiting for 'NavigateToPose' action to complete")
188 rclpy.spin_until_future_complete(self, get_result_future)
189 status = get_result_future.result().status
190 if status != GoalStatus.STATUS_SUCCEEDED:
191 result = get_result_future.result().result
192 self.
info_msginfo_msg(f
'Goal failed with status code: {status}'
193 f
' error code:{result.error_code}'
194 f
' error msg:{result.error_msg}')
197 self.
info_msginfo_msg(
'Goal succeeded!')
200 def poseCallback(self, msg: PoseWithCovarianceStamped) ->
None:
201 self.
info_msginfo_msg(
'Received amcl_pose')
205 def wait_for_node_active(self, node_name: str) ->
None:
207 self.
info_msginfo_msg(f
'Waiting for {node_name} to become active')
208 node_service = f
'{node_name}/get_state'
209 state_client = self.create_client(GetState, node_service)
210 while not state_client.wait_for_service(timeout_sec=1.0):
211 self.
info_msginfo_msg(f
'{node_service} service not available, waiting...')
212 req = GetState.Request()
214 while state !=
'active':
215 self.
info_msginfo_msg(f
'Getting {node_name} state...')
216 future = state_client.call_async(req)
217 rclpy.spin_until_future_complete(self, future)
218 if future.result()
is not None:
219 state = future.result().current_state.label
220 self.
info_msginfo_msg(f
'Result of get_state: {state}')
223 f
'Exception while calling service: {future.exception()!r}'
227 def shutdown(self) -> None:
228 self.
info_msginfo_msg(
'Shutting down')
231 transition_service =
'lifecycle_manager_navigation/manage_nodes'
232 mgr_client = self.create_client(ManageLifecycleNodes, transition_service)
233 while not mgr_client.wait_for_service(timeout_sec=1.0):
234 self.
info_msginfo_msg(f
'{transition_service} service not available, waiting...')
236 req = ManageLifecycleNodes.Request()
237 req.command = ManageLifecycleNodes.Request().SHUTDOWN
238 future = mgr_client.call_async(req)
240 self.
info_msginfo_msg(
'Shutting down navigation lifecycle manager...')
241 rclpy.spin_until_future_complete(self, future)
243 self.
info_msginfo_msg(
'Shutting down navigation lifecycle manager complete.')
244 except Exception
as e:
245 self.
error_msgerror_msg(f
'Service call failed {e!r}')
246 transition_service =
'lifecycle_manager_localization/manage_nodes'
247 mgr_client = self.create_client(ManageLifecycleNodes, transition_service)
248 while not mgr_client.wait_for_service(timeout_sec=1.0):
249 self.
info_msginfo_msg(f
'{transition_service} service not available, waiting...')
251 req = ManageLifecycleNodes.Request()
252 req.command = ManageLifecycleNodes.Request().SHUTDOWN
253 future = mgr_client.call_async(req)
255 self.
info_msginfo_msg(
'Shutting down localization lifecycle manager...')
256 rclpy.spin_until_future_complete(self, future)
258 self.
info_msginfo_msg(
'Shutting down localization lifecycle manager complete')
259 except Exception
as e:
260 self.
error_msgerror_msg(f
'Service call failed {e!r}')
262 def wait_for_initial_pose(self) -> None:
265 self.
info_msginfo_msg(
'Setting initial pose')
267 self.
info_msginfo_msg(
'Waiting for amcl_pose to be received')
268 rclpy.spin_once(self, timeout_sec=1)
271 def run_all_tests(robot_tester: NavTester) -> bool:
275 robot_tester.wait_for_node_active(
'amcl')
276 robot_tester.wait_for_initial_pose()
277 robot_tester.wait_for_node_active(
'bt_navigator')
278 result = robot_tester.runNavigateAction()
280 result = result
and not robot_tester.runFakeNavigateAction()
282 result = result
and robot_tester.runNavigatePreemptionAction(
False)
283 result = result
and robot_tester.runNavigatePreemptionAction(
True)
288 robot_tester.info_msg(
'Test PASSED')
290 robot_tester.error_msg(
'Test FAILED')
295 def fwd_pose(x: float = 0.0, y: float = 0.0, z: float = 0.01) -> Pose:
296 initial_pose = Pose()
297 initial_pose.position.x = x
298 initial_pose.position.y = y
299 initial_pose.position.z = z
300 initial_pose.orientation.x = 0.0
301 initial_pose.orientation.y = 0.0
302 initial_pose.orientation.z = 0.0
303 initial_pose.orientation.w = 1.0
307 def get_testers(args: argparse.Namespace) -> list[NavTester]:
310 init_x, init_y, final_x, final_y = args.robot[0]
312 initial_pose=fwd_pose(float(init_x), float(init_y)),
313 goal_pose=fwd_pose(float(final_x), float(final_y)),
316 'Starting tester, robot going from '
326 testers.append(tester)
330 def main(argv: list[str] = sys.argv[1:]):
332 parser = argparse.ArgumentParser(description=
'System-level navigation tester node')
333 group = parser.add_mutually_exclusive_group(required=
True)
339 metavar=(
'init_x',
'init_y',
'final_x',
'final_y'),
340 help=
'The robot starting and final positions.',
343 args, unknown = parser.parse_known_args()
348 testers = get_testers(args)
353 for tester
in testers:
354 passed = run_all_tests(tester)
358 for tester
in testers:
362 testers[0].info_msg(
'Done Shutting Down.')
365 testers[0].info_msg(
'Exiting failed')
368 testers[0].info_msg(
'Exiting passed')
372 if __name__ ==
'__main__':
None error_msg(self, str msg)
None poseCallback(self, PoseWithCovarianceStamped msg)
None setInitialPose(self)
PoseStamped getStampedPoseMsg(self, Pose pose)
None info_msg(self, str msg)