-
Notifications
You must be signed in to change notification settings - Fork 406
/
test_move
executable file
·307 lines (267 loc) · 12.6 KB
/
test_move
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# -- BEGIN LICENSE BLOCK ----------------------------------------------
# Copyright 2021 FZI Forschungszentrum Informatik
# Created on behalf of Universal Robots A/S
#
# 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.
# -- END LICENSE BLOCK ------------------------------------------------
#
# ---------------------------------------------------------------------
# !\file
#
# \author Felix Exner mauch@fzi.de
# \date 2021-08-05
#
#
# ---------------------------------------------------------------------
import sys
import rospy
import actionlib
from control_msgs.msg import FollowJointTrajectoryAction, FollowJointTrajectoryGoal
from trajectory_msgs.msg import JointTrajectoryPoint
from controller_manager_msgs.srv import SwitchControllerRequest, SwitchController
from controller_manager_msgs.srv import LoadControllerRequest, LoadController
from controller_manager_msgs.srv import ListControllers, ListControllersRequest
import geometry_msgs.msg as geometry_msgs
from cartesian_control_msgs.msg import (
FollowCartesianTrajectoryAction,
FollowCartesianTrajectoryGoal,
CartesianTrajectoryPoint,
)
# Compatibility for python2 and python3
if sys.version_info[0] < 3:
input = raw_input
# If your robot description is created with a tf_prefix, those would have to be adapted
JOINT_NAMES = [
"shoulder_pan_joint",
"shoulder_lift_joint",
"elbow_joint",
"wrist_1_joint",
"wrist_2_joint",
"wrist_3_joint",
]
# All of those controllers can be used to execute joint-based trajectories.
# The scaled versions should be preferred over the non-scaled versions.
JOINT_TRAJECTORY_CONTROLLERS = [
"scaled_pos_joint_traj_controller",
"scaled_vel_joint_traj_controller",
"pos_joint_traj_controller",
"vel_joint_traj_controller",
"forward_joint_traj_controller",
]
# All of those controllers can be used to execute Cartesian trajectories.
# The scaled versions should be preferred over the non-scaled versions.
CARTESIAN_TRAJECTORY_CONTROLLERS = [
"pose_based_cartesian_traj_controller",
"joint_based_cartesian_traj_controller",
"forward_cartesian_traj_controller",
]
# We'll have to make sure that none of these controllers are running, as they will
# be conflicting with the joint trajectory controllers
CONFLICTING_CONTROLLERS = ["joint_group_vel_controller", "twist_controller"]
class TrajectoryClient:
"""Small trajectory client to test a joint trajectory"""
def __init__(self):
rospy.init_node("test_move")
timeout = rospy.Duration(5)
self.switch_srv = rospy.ServiceProxy(
"controller_manager/switch_controller", SwitchController
)
self.load_srv = rospy.ServiceProxy("controller_manager/load_controller", LoadController)
self.list_srv = rospy.ServiceProxy("controller_manager/list_controllers", ListControllers)
try:
self.switch_srv.wait_for_service(timeout.to_sec())
except rospy.exceptions.ROSException as err:
rospy.logerr("Could not reach controller switch service. Msg: {}".format(err))
sys.exit(-1)
self.joint_trajectory_controller = JOINT_TRAJECTORY_CONTROLLERS[0]
self.cartesian_trajectory_controller = CARTESIAN_TRAJECTORY_CONTROLLERS[0]
def send_joint_trajectory(self):
"""Creates a trajectory and sends it using the selected action server"""
# make sure the correct controller is loaded and activated
self.switch_controller(self.joint_trajectory_controller)
trajectory_client = actionlib.SimpleActionClient(
"{}/follow_joint_trajectory".format(self.joint_trajectory_controller),
FollowJointTrajectoryAction,
)
# Wait for action server to be ready
timeout = rospy.Duration(5)
if not trajectory_client.wait_for_server(timeout):
rospy.logerr("Could not reach controller action server.")
sys.exit(-1)
# Create and fill trajectory goal
goal = FollowJointTrajectoryGoal()
goal.trajectory.joint_names = JOINT_NAMES
# The following list are arbitrary positions
# Change to your own needs if desired
position_list = [[0, -1.57, -1.57, 0, 0, 0]]
position_list.append([0.2, -1.57, -1.57, 0, 0, 0])
position_list.append([-0.5, -1.57, -1.2, 0, 0, 0])
velocity_list = [[0.2, 0, 0, 0, 0, 0]]
velocity_list.append([-0.2, 0, 0, 0, 0, 0])
velocity_list.append([0, 0, 0, 0, 0, 0])
duration_list = [3.0, 7.0, 10.0]
for i, position in enumerate(position_list):
point = JointTrajectoryPoint()
point.positions = position
point.velocities = velocity_list[i]
point.time_from_start = rospy.Duration(duration_list[i])
goal.trajectory.points.append(point)
self.ask_confirmation(position_list)
rospy.loginfo("Executing trajectory using the {}".format(self.joint_trajectory_controller))
trajectory_client.send_goal(goal)
trajectory_client.wait_for_result()
result = trajectory_client.get_result()
rospy.loginfo("Trajectory execution finished in state {}".format(result.error_code))
def send_cartesian_trajectory(self):
"""Creates a Cartesian trajectory and sends it using the selected action server"""
self.switch_controller(self.cartesian_trajectory_controller)
# make sure the correct controller is loaded and activated
goal = FollowCartesianTrajectoryGoal()
trajectory_client = actionlib.SimpleActionClient(
"{}/follow_cartesian_trajectory".format(self.cartesian_trajectory_controller),
FollowCartesianTrajectoryAction,
)
# Wait for action server to be ready
timeout = rospy.Duration(5)
if not trajectory_client.wait_for_server(timeout):
rospy.logerr("Could not reach controller action server.")
sys.exit(-1)
# The following list are arbitrary positions
# Change to your own needs if desired
pose_list = [
geometry_msgs.Pose(
geometry_msgs.Vector3(0.4, -0.1, 0.4), geometry_msgs.Quaternion(0, 0, 0, 1)
),
geometry_msgs.Pose(
geometry_msgs.Vector3(0.4, -0.1, 0.6), geometry_msgs.Quaternion(0, 0, 0, 1)
),
geometry_msgs.Pose(
geometry_msgs.Vector3(0.4, 0.3, 0.6), geometry_msgs.Quaternion(0, 0, 0, 1)
),
geometry_msgs.Pose(
geometry_msgs.Vector3(0.4, 0.3, 0.4), geometry_msgs.Quaternion(0, 0, 0, 1)
),
geometry_msgs.Pose(
geometry_msgs.Vector3(0.4, -0.1, 0.4), geometry_msgs.Quaternion(0, 0, 0, 1)
),
]
duration_list = [3.0, 4.0, 5.0, 6.0, 7.0]
for i, pose in enumerate(pose_list):
point = CartesianTrajectoryPoint()
point.pose = pose
point.time_from_start = rospy.Duration(duration_list[i])
goal.trajectory.points.append(point)
self.ask_confirmation(pose_list)
rospy.loginfo(
"Executing trajectory using the {}".format(self.cartesian_trajectory_controller)
)
trajectory_client.send_goal(goal)
trajectory_client.wait_for_result()
result = trajectory_client.get_result()
rospy.loginfo("Trajectory execution finished in state {}".format(result.error_code))
###############################################################################################
# #
# Methods defined below are for the sake of safety / flexibility of this demo script only. #
# If you just want to copy the relevant parts to make your own motion script you don't have #
# to use / copy all the functions below. #
# #
###############################################################################################
def ask_confirmation(self, waypoint_list):
"""Ask the user for confirmation. This function is obviously not necessary, but makes sense
in a testing script when you know nothing about the user's setup."""
rospy.logwarn("The robot will move to the following waypoints: \n{}".format(waypoint_list))
confirmed = False
valid = False
while not valid:
input_str = input(
"Please confirm that the robot path is clear of obstacles.\n"
"Keep the EM-Stop available at all times. You are executing\n"
"the motion at your own risk. Please type 'y' to proceed or 'n' to abort: "
)
valid = input_str in ["y", "n"]
if not valid:
rospy.loginfo("Please confirm by entering 'y' or abort by entering 'n'")
else:
confirmed = input_str == "y"
if not confirmed:
rospy.loginfo("Exiting as requested by user.")
sys.exit(0)
def choose_controller(self):
"""Ask the user to select the desired controller from the available list."""
rospy.loginfo("Available trajectory controllers:")
for (index, name) in enumerate(JOINT_TRAJECTORY_CONTROLLERS):
rospy.loginfo("{} (joint-based): {}".format(index, name))
for (index, name) in enumerate(CARTESIAN_TRAJECTORY_CONTROLLERS):
rospy.loginfo("{} (Cartesian): {}".format(index + len(JOINT_TRAJECTORY_CONTROLLERS), name))
choice = -1
while choice < 0:
input_str = input(
"Please choose a controller by entering its number (Enter '0' if "
"you are unsure / don't care): "
)
try:
choice = int(input_str)
if choice < 0 or choice >= len(JOINT_TRAJECTORY_CONTROLLERS) + len(
CARTESIAN_TRAJECTORY_CONTROLLERS
):
rospy.loginfo(
"{} not inside the list of options. "
"Please enter a valid index from the list above.".format(choice)
)
choice = -1
except ValueError:
rospy.loginfo("Input is not a valid number. Please try again.")
if choice < len(JOINT_TRAJECTORY_CONTROLLERS):
self.joint_trajectory_controller = JOINT_TRAJECTORY_CONTROLLERS[choice]
return "joint_based"
self.cartesian_trajectory_controller = CARTESIAN_TRAJECTORY_CONTROLLERS[
choice - len(JOINT_TRAJECTORY_CONTROLLERS)
]
return "cartesian"
def switch_controller(self, target_controller):
"""Activates the desired controller and stops all others from the predefined list above"""
other_controllers = (
JOINT_TRAJECTORY_CONTROLLERS
+ CARTESIAN_TRAJECTORY_CONTROLLERS
+ CONFLICTING_CONTROLLERS
)
other_controllers.remove(target_controller)
srv = ListControllersRequest()
response = self.list_srv(srv)
for controller in response.controller:
if controller.name == target_controller and controller.state == "running":
return
srv = LoadControllerRequest()
srv.name = target_controller
self.load_srv(srv)
srv = SwitchControllerRequest()
srv.stop_controllers = other_controllers
srv.start_controllers = [target_controller]
srv.strictness = SwitchControllerRequest.BEST_EFFORT
self.switch_srv(srv)
if __name__ == "__main__":
client = TrajectoryClient()
# The controller choice is obviously not required to move the robot. It is a part of this demo
# script in order to show all available trajectory controllers.
trajectory_type = client.choose_controller()
if trajectory_type == "joint_based":
client.send_joint_trajectory()
elif trajectory_type == "cartesian":
client.send_cartesian_trajectory()
else:
raise ValueError(
"I only understand types 'joint_based' and 'cartesian', but got '{}'".format(
trajectory_type
)
)