21 from typing
import Optional
23 from action_msgs.msg
import GoalStatus
24 from geometry_msgs.msg
import Pose
25 from geometry_msgs.msg
import PoseStamped
26 from geometry_msgs.msg
import PoseWithCovarianceStamped
27 from lifecycle_msgs.srv
import GetState
28 from nav2_msgs.action
import NavigateThroughPoses
29 from nav2_msgs.srv
import ManageLifecycleNodes
33 from rclpy.action
import ActionClient
34 from rclpy.node
import Node
35 from rclpy.qos
import QoSDurabilityPolicy, QoSHistoryPolicy, QoSReliabilityPolicy
36 from rclpy.qos
import QoSProfile
41 def __init__(self, initial_pose: Pose, goal_pose: Pose, namespace: str =
''):
42 super().__init__(node_name=
'nav2_tester', namespace=namespace)
44 PoseWithCovarianceStamped,
'initialpose', 10
47 pose_qos = QoSProfile(
48 durability=QoSDurabilityPolicy.TRANSIENT_LOCAL,
49 reliability=QoSReliabilityPolicy.RELIABLE,
50 history=QoSHistoryPolicy.KEEP_LAST,
55 PoseWithCovarianceStamped,
'amcl_pose', self.
poseCallbackposeCallback, pose_qos
61 self, NavigateThroughPoses,
'navigate_through_poses'
64 def info_msg(self, msg: str):
65 self.get_logger().info(
'\033[1;37;44m' + msg +
'\033[0m')
67 def warn_msg(self, msg: str):
68 self.get_logger().warn(
'\033[1;37;43m' + msg +
'\033[0m')
70 def error_msg(self, msg: str):
71 self.get_logger().error(
'\033[1;37;41m' + msg +
'\033[0m')
73 def setInitialPose(self):
74 msg = PoseWithCovarianceStamped()
76 msg.header.frame_id =
'map'
77 self.
info_msginfo_msg(
'Publishing Initial Pose')
81 def getStampedPoseMsg(self, pose: Pose):
83 msg.header.frame_id =
'map'
87 def runNavigateAction(self, goal_pose: Optional[Pose] =
None):
89 self.
info_msginfo_msg(
"Waiting for 'NavigateThroughPoses' action server")
90 while not self.
action_clientaction_client.wait_for_server(timeout_sec=1.0):
92 "'NavigateThroughPoses' action server not available, waiting..."
95 self.
goal_posegoal_pose = goal_pose
if goal_pose
is not None else self.
goal_posegoal_pose
96 goal_msg = NavigateThroughPoses.Goal()
102 self.
info_msginfo_msg(
'Sending goal request...')
103 send_goal_future = self.
action_clientaction_client.send_goal_async(goal_msg)
105 rclpy.spin_until_future_complete(self, send_goal_future)
106 goal_handle = send_goal_future.result()
108 if not goal_handle.accepted:
112 self.
info_msginfo_msg(
'Goal accepted')
113 get_result_future = goal_handle.get_result_async()
115 self.
info_msginfo_msg(
"Waiting for 'NavigateToPose' action to complete")
116 rclpy.spin_until_future_complete(self, get_result_future)
117 status = get_result_future.result().status
118 if status != GoalStatus.STATUS_SUCCEEDED:
119 self.
info_msginfo_msg(f
'Goal failed with status code: {status}')
122 self.
info_msginfo_msg(
'Goal succeeded!')
125 def runFakeNavigateAction(self):
127 self.
info_msginfo_msg(
"Waiting for 'NavigateThroughPoses' action server")
128 while not self.
action_clientaction_client.wait_for_server(timeout_sec=1.0):
130 "'NavigateThroughPoses' action server not available, waiting..."
133 goal_msg = NavigateThroughPoses.Goal()
135 self.
info_msginfo_msg(
'Sending goal request...')
136 send_goal_future = self.
action_clientaction_client.send_goal_async(goal_msg)
138 rclpy.spin_until_future_complete(self, send_goal_future)
139 goal_handle = send_goal_future.result()
141 if not goal_handle.accepted:
145 self.
info_msginfo_msg(
'Goal accepted')
146 get_result_future = goal_handle.get_result_async()
148 self.
info_msginfo_msg(
"Waiting for 'NavigateToPose' action to complete")
149 rclpy.spin_until_future_complete(self, get_result_future)
150 status = get_result_future.result().status
151 if status != GoalStatus.STATUS_SUCCEEDED:
152 self.
info_msginfo_msg(f
'Goal failed with status code: {status}')
155 self.
info_msginfo_msg(
'Goal succeeded!')
158 def runNavigatePreemptionAction(self, block):
160 self.
info_msginfo_msg(
"Waiting for 'NavigateThroughPoses' action server")
161 while not self.
action_clientaction_client.wait_for_server(timeout_sec=1.0):
163 "'NavigateThroughPoses' action server not available, waiting..."
166 goal_msg = NavigateThroughPoses.Goal()
169 self.
info_msginfo_msg(
'Sending goal request...')
170 send_goal_future = self.
action_clientaction_client.send_goal_async(goal_msg)
172 rclpy.spin_until_future_complete(self, send_goal_future)
173 goal_handle = send_goal_future.result()
175 if not goal_handle.accepted:
182 self.
info_msginfo_msg(
'Goal accepted')
183 get_result_future = goal_handle.get_result_async()
185 self.
info_msginfo_msg(
"Waiting for 'NavigateToPose' action to complete")
186 rclpy.spin_until_future_complete(self, get_result_future)
187 status = get_result_future.result().status
188 if status != GoalStatus.STATUS_SUCCEEDED:
189 self.
info_msginfo_msg(f
'Goal failed with status code: {status}')
192 self.
info_msginfo_msg(
'Goal succeeded!')
195 def poseCallback(self, msg):
196 self.
info_msginfo_msg(
'Received amcl_pose')
200 def wait_for_node_active(self, node_name: str):
202 self.
info_msginfo_msg(f
'Waiting for {node_name} to become active')
203 node_service = f
'{node_name}/get_state'
204 state_client = self.create_client(GetState, node_service)
205 while not state_client.wait_for_service(timeout_sec=1.0):
206 self.
info_msginfo_msg(f
'{node_service} service not available, waiting...')
207 req = GetState.Request()
209 while state !=
'active':
210 self.
info_msginfo_msg(f
'Getting {node_name} state...')
211 future = state_client.call_async(req)
212 rclpy.spin_until_future_complete(self, future)
213 if future.result()
is not None:
214 state = future.result().current_state.label
215 self.
info_msginfo_msg(f
'Result of get_state: {state}')
218 f
'Exception while calling service: {future.exception()!r}'
223 self.
info_msginfo_msg(
'Shutting down')
226 transition_service =
'lifecycle_manager_navigation/manage_nodes'
227 mgr_client = self.create_client(ManageLifecycleNodes, transition_service)
228 while not mgr_client.wait_for_service(timeout_sec=1.0):
229 self.
info_msginfo_msg(f
'{transition_service} service not available, waiting...')
231 req = ManageLifecycleNodes.Request()
232 req.command = ManageLifecycleNodes.Request().SHUTDOWN
233 future = mgr_client.call_async(req)
235 self.
info_msginfo_msg(
'Shutting down navigation lifecycle manager...')
236 rclpy.spin_until_future_complete(self, future)
238 self.
info_msginfo_msg(
'Shutting down navigation lifecycle manager complete.')
239 except Exception
as e:
240 self.
error_msgerror_msg(f
'Service call failed {e!r}')
241 transition_service =
'lifecycle_manager_localization/manage_nodes'
242 mgr_client = self.create_client(ManageLifecycleNodes, transition_service)
243 while not mgr_client.wait_for_service(timeout_sec=1.0):
244 self.
info_msginfo_msg(f
'{transition_service} service not available, waiting...')
246 req = ManageLifecycleNodes.Request()
247 req.command = ManageLifecycleNodes.Request().SHUTDOWN
248 future = mgr_client.call_async(req)
250 self.
info_msginfo_msg(
'Shutting down localization lifecycle manager...')
251 rclpy.spin_until_future_complete(self, future)
253 self.
info_msginfo_msg(
'Shutting down localization lifecycle manager complete')
254 except Exception
as e:
255 self.
error_msgerror_msg(f
'Service call failed {e!r}')
257 def wait_for_initial_pose(self):
260 self.
info_msginfo_msg(
'Setting initial pose')
262 self.
info_msginfo_msg(
'Waiting for amcl_pose to be received')
263 rclpy.spin_once(self, timeout_sec=1)
266 def run_all_tests(robot_tester):
270 robot_tester.wait_for_node_active(
'amcl')
271 robot_tester.wait_for_initial_pose()
272 robot_tester.wait_for_node_active(
'bt_navigator')
273 result = robot_tester.runNavigateAction()
275 result = result
and not robot_tester.runFakeNavigateAction()
277 result = result
and robot_tester.runNavigatePreemptionAction(
False)
278 result = result
and robot_tester.runNavigatePreemptionAction(
True)
283 robot_tester.info_msg(
'Test PASSED')
285 robot_tester.error_msg(
'Test FAILED')
290 def fwd_pose(x=0.0, y=0.0, z=0.01):
291 initial_pose = Pose()
292 initial_pose.position.x = x
293 initial_pose.position.y = y
294 initial_pose.position.z = z
295 initial_pose.orientation.x = 0.0
296 initial_pose.orientation.y = 0.0
297 initial_pose.orientation.z = 0.0
298 initial_pose.orientation.w = 1.0
302 def get_testers(args):
305 init_x, init_y, final_x, final_y = args.robot[0]
307 initial_pose=fwd_pose(float(init_x), float(init_y)),
308 goal_pose=fwd_pose(float(final_x), float(final_y)),
311 'Starting tester, robot going from '
321 testers.append(tester)
325 def main(argv=sys.argv[1:]):
327 parser = argparse.ArgumentParser(description=
'System-level navigation tester node')
328 group = parser.add_mutually_exclusive_group(required=
True)
334 metavar=(
'init_x',
'init_y',
'final_x',
'final_y'),
335 help=
'The robot starting and final positions.',
338 args, unknown = parser.parse_known_args()
343 testers = get_testers(args)
348 for tester
in testers:
349 passed = run_all_tests(tester)
353 for tester
in testers:
357 testers[0].info_msg(
'Done Shutting Down.')
360 testers[0].info_msg(
'Exiting failed')
363 testers[0].info_msg(
'Exiting passed')
367 if __name__ ==
'__main__':
def poseCallback(self, msg)
def info_msg(self, str msg)
def getStampedPoseMsg(self, Pose pose)
def error_msg(self, str msg)