-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
tester.py
executable file
·278 lines (226 loc) · 9.14 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#! /usr/bin/env python3
# Copyright 2019 Samsung Research America
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import time
from action_msgs.msg import GoalStatus
from geometry_msgs.msg import PoseStamped, PoseWithCovarianceStamped
from nav2_msgs.action import ComputePathToPose, FollowWaypoints
from nav2_msgs.srv import ManageLifecycleNodes
from rcl_interfaces.srv import SetParameters
import rclpy
from rclpy.action import ActionClient
from rclpy.node import Node
from rclpy.parameter import Parameter
from rclpy.qos import QoSDurabilityPolicy, QoSHistoryPolicy, QoSReliabilityPolicy
from rclpy.qos import QoSProfile
class WaypointFollowerTest(Node):
def __init__(self):
super().__init__(node_name='nav2_waypoint_tester', namespace='')
self.waypoints = None
self.action_client = ActionClient(self, FollowWaypoints, 'follow_waypoints')
self.initial_pose_pub = self.create_publisher(
PoseWithCovarianceStamped, 'initialpose', 10
)
self.initial_pose_received = False
self.goal_handle = None
self.action_result = None
pose_qos = QoSProfile(
durability=QoSDurabilityPolicy.TRANSIENT_LOCAL,
reliability=QoSReliabilityPolicy.RELIABLE,
history=QoSHistoryPolicy.KEEP_LAST,
depth=1,
)
self.model_pose_sub = self.create_subscription(
PoseWithCovarianceStamped, 'amcl_pose', self.poseCallback, pose_qos
)
self.param_cli = self.create_client(
SetParameters, '/waypoint_follower/set_parameters'
)
def setInitialPose(self, pose):
self.init_pose = PoseWithCovarianceStamped()
self.init_pose.pose.pose.position.x = pose[0]
self.init_pose.pose.pose.position.y = pose[1]
self.init_pose.header.frame_id = 'map'
self.publishInitialPose()
time.sleep(5)
def poseCallback(self, msg):
self.info_msg('Received amcl_pose')
self.initial_pose_received = True
def setWaypoints(self, waypoints):
self.waypoints = []
for wp in waypoints:
msg = PoseStamped()
msg.header.frame_id = 'map'
msg.pose.position.x = wp[0]
msg.pose.position.y = wp[1]
msg.pose.orientation.w = 1.0
self.waypoints.append(msg)
def run(self, block, cancel):
# if not self.waypoints:
# rclpy.error_msg('Did not set valid waypoints before running test!')
# return False
while not self.action_client.wait_for_server(timeout_sec=1.0):
self.info_msg("'follow_waypoints' action server not available, waiting...")
action_request = FollowWaypoints.Goal()
action_request.poses = self.waypoints
self.info_msg('Sending goal request...')
send_goal_future = self.action_client.send_goal_async(action_request)
try:
rclpy.spin_until_future_complete(self, send_goal_future)
self.goal_handle = send_goal_future.result()
except Exception as e: # noqa: B902
self.error_msg(f'Service call failed {e!r}')
if not self.goal_handle.accepted:
self.error_msg('Goal rejected')
return False
self.info_msg('Goal accepted')
if not block:
return True
get_result_future = self.goal_handle.get_result_async()
if cancel:
time.sleep(2)
self.cancel_goal()
self.info_msg("Waiting for 'follow_waypoints' action to complete")
try:
rclpy.spin_until_future_complete(self, get_result_future)
status = get_result_future.result().status
result = get_result_future.result().result
self.action_result = result
except Exception as e: # noqa: B902
self.error_msg(f'Service call failed {e!r}')
if status != GoalStatus.STATUS_SUCCEEDED:
self.info_msg(f'Goal failed with status code: {status}')
return False
if len(self.action_result.missed_waypoints) > 0:
self.info_msg(
'Goal failed to process all waypoints,'
' missed {0} wps.'.format(len(self.action_result.missed_waypoints))
)
return False
self.info_msg('Goal succeeded!')
return True
def publishInitialPose(self):
self.initial_pose_pub.publish(self.init_pose)
def setStopFailureParam(self, value):
req = SetParameters.Request()
req.parameters = [
Parameter('stop_on_failure', Parameter.Type.BOOL, value).to_parameter_msg()
]
future = self.param_cli.call_async(req)
rclpy.spin_until_future_complete(self, future)
def shutdown(self):
self.info_msg('Shutting down')
self.action_client.destroy()
self.info_msg('Destroyed follow_waypoints action client')
transition_service = 'lifecycle_manager_navigation/manage_nodes'
mgr_client = self.create_client(ManageLifecycleNodes, transition_service)
while not mgr_client.wait_for_service(timeout_sec=1.0):
self.info_msg(f'{transition_service} service not available, waiting...')
req = ManageLifecycleNodes.Request()
req.command = ManageLifecycleNodes.Request().SHUTDOWN
future = mgr_client.call_async(req)
try:
rclpy.spin_until_future_complete(self, future)
future.result()
except Exception as e: # noqa: B902
self.error_msg(f'{transition_service} service call failed {e!r}')
self.info_msg(f'{transition_service} finished')
transition_service = 'lifecycle_manager_localization/manage_nodes'
mgr_client = self.create_client(ManageLifecycleNodes, transition_service)
while not mgr_client.wait_for_service(timeout_sec=1.0):
self.info_msg(f'{transition_service} service not available, waiting...')
req = ManageLifecycleNodes.Request()
req.command = ManageLifecycleNodes.Request().SHUTDOWN
future = mgr_client.call_async(req)
try:
rclpy.spin_until_future_complete(self, future)
future.result()
except Exception as e: # noqa: B902
self.error_msg(f'{transition_service} service call failed {e!r}')
self.info_msg(f'{transition_service} finished')
def cancel_goal(self):
cancel_future = self.goal_handle.cancel_goal_async()
rclpy.spin_until_future_complete(self, cancel_future)
def info_msg(self, msg: str):
self.get_logger().info(msg)
def warn_msg(self, msg: str):
self.get_logger().warn(msg)
def error_msg(self, msg: str):
self.get_logger().error(msg)
def main(argv=sys.argv[1:]):
rclpy.init()
# wait a few seconds to make sure entire stacks are up
time.sleep(10)
wps = [[-0.52, -0.54], [0.58, -0.55], [1.78, -0.57]]
starting_pose = [-2.0, -0.5]
test = WaypointFollowerTest()
test.setWaypoints(wps)
retry_count = 0
retries = 2
while not test.initial_pose_received and retry_count <= retries:
retry_count += 1
test.info_msg('Setting initial pose')
test.setInitialPose(starting_pose)
test.info_msg('Waiting for amcl_pose to be received')
rclpy.spin_once(test, timeout_sec=1.0) # wait for poseCallback
result = test.run(True, False)
assert result
# preempt with new point
test.setWaypoints([starting_pose])
result = test.run(False, False)
time.sleep(2)
test.setWaypoints([wps[1]])
result = test.run(False, False)
# cancel
time.sleep(2)
test.cancel_goal()
# set waypoint outside of map
time.sleep(2)
test.setWaypoints([[100.0, 100.0]])
result = test.run(True, False)
assert not result
result = not result
assert (
test.action_result.missed_waypoints[0].error_code
== ComputePathToPose.Result().GOAL_OUTSIDE_MAP
)
# stop on failure test with bogus waypoint
test.setStopFailureParam(True)
bwps = [[-0.52, -0.54], [100.0, 100.0], [0.58, 0.52]]
test.setWaypoints(bwps)
result = test.run(True, False)
assert not result
result = not result
mwps = test.action_result.missed_waypoints
result = (len(mwps) == 1) & (mwps[0] == 1)
test.setStopFailureParam(False)
# Zero goal test
test.setWaypoints([])
result = test.run(True, False)
# Cancel test
test.setWaypoints(wps)
result = test.run(True, True)
assert not result
result = not result
test.shutdown()
test.info_msg('Done Shutting Down.')
if not result:
test.info_msg('Exiting failed')
exit(1)
else:
test.info_msg('Exiting passed')
exit(0)
if __name__ == '__main__':
main()