SMACC2
sm_test_moveit_ur5e_ignition_sim.launch.py
Go to the documentation of this file.
1# Copyright (c) 2021 RobosoftAI Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15#
16# Author: Denis Štogl
17#
18
19from launch import LaunchDescription
20from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription, OpaqueFunction
21from launch.launch_description_sources import PythonLaunchDescriptionSource
22from launch.substitutions import Command, FindExecutable, LaunchConfiguration, PathJoinSubstitution
23from launch_ros.actions import Node
24from launch_ros.substitutions import FindPackageShare
25
26# TODO(destogl): BEGIN this should be removed when MoveIt can handle parameters properly
27import os
28from ament_index_python.packages import get_package_share_directory
29import math
30import yaml
31
32
33def construct_angle_radians(loader, node):
34 """utility function to construct radian values from yaml."""
35 value = loader.construct_scalar(node)
36 try:
37 return float(value)
38 except SyntaxError:
39 raise Exception("invalid expression: %s" % value)
40
41
42def construct_angle_degrees(loader, node):
43 """utility function for converting degrees into radians from yaml."""
44 return math.radians(construct_angle_radians(loader, node))
45
46
47def load_yaml(package_name, file_path):
48 package_path = get_package_share_directory(package_name)
49 absolute_file_path = os.path.join(package_path, file_path)
50
51 try:
52 yaml.SafeLoader.add_constructor("!radians", construct_angle_radians)
53 yaml.SafeLoader.add_constructor("!degrees", construct_angle_degrees)
54 except Exception:
55 raise Exception("yaml support not available; install python-yaml")
56
57 try:
58 with open(absolute_file_path) as file:
59 return yaml.safe_load(file)
60 except OSError: # parent of IOError, OSError *and* WindowsError where available
61 return None
62
63
64# TODO(destogl): END this should be removed when MoveIt can handle parameters properly
65
66
67def launch_setup(context, *args, **kwargs):
68
69 ur_type = "ur5e"
70
71 # General arguments
72 runtime_config_package = LaunchConfiguration("runtime_config_package")
73 controllers_file = LaunchConfiguration("controllers_file")
74 description_package = LaunchConfiguration("description_package")
75 description_file = LaunchConfiguration("description_file")
76 moveit_config_package = LaunchConfiguration("moveit_config_package")
77 moveit_config_file = LaunchConfiguration("moveit_config_file")
78 prefix = LaunchConfiguration("prefix")
79
80 robot_description_content = Command(
81 [
82 PathJoinSubstitution([FindExecutable(name="xacro")]),
83 " ",
84 PathJoinSubstitution(
85 [FindPackageShare(description_package), "urdf", description_file]
86 ),
87 " ",
88 "name:=",
89 "ur",
90 " ",
91 "ur_type:=",
92 ur_type,
93 " ",
94 "prefix:=",
95 prefix,
96 " ",
97 ]
98 )
99 robot_description = {"robot_description": robot_description_content}
100
101 # MoveIt Configuration
102 robot_description_semantic_content = Command(
103 [
104 PathJoinSubstitution([FindExecutable(name="xacro")]),
105 " ",
106 PathJoinSubstitution(
107 [FindPackageShare(moveit_config_package), "srdf", moveit_config_file]
108 ),
109 " ",
110 "name:=",
111 "ur",
112 " ",
113 "ur_type:=",
114 ur_type,
115 " ",
116 "prefix:=",
117 prefix,
118 " ",
119 ]
120 )
121 robot_description_semantic = {"robot_description_semantic": robot_description_semantic_content}
122
123 kinematics_yaml = load_yaml("ur_moveit_config", "config/kinematics.yaml")
124 robot_description_kinematics = {"robot_description_kinematics": kinematics_yaml}
125
126 smacc2_sm_config = PathJoinSubstitution(
127 [
128 FindPackageShare("sm_test_moveit_ur5_sim"),
129 "config",
130 "sm_test_moveit_ur5_sim_config.yaml",
131 ]
132 )
133
134 sm_test_moveit_ur5_sim_node = Node(
135 package="sm_test_moveit_ur5_sim",
136 executable="sm_test_moveit_ur5_sim_node",
137 prefix="xterm -xrm 'XTerm*scrollBar: true' -xrm 'xterm*rightScrollBar: true' -hold -sl 10000 -geometry 1000x600 -e",
138 parameters=[
139 robot_description,
140 robot_description_semantic,
141 robot_description_kinematics,
142 smacc2_sm_config,
143 ],
144 arguments=["--log-level", "smacc2:=DEBUG"],
145 )
146
147 ur_control_launch = IncludeLaunchDescription(
148 PythonLaunchDescriptionSource(
149 [FindPackageShare("ur_simulation_ignition"), "/launch", "/ur_sim_control.launch.py"]
150 ),
151 launch_arguments={
152 "ur_type": ur_type,
153 "runtime_config_package": runtime_config_package,
154 "controllers_file": controllers_file,
155 "description_package": description_package,
156 "description_file": description_file,
157 "prefix": prefix,
158 "launch_rviz": "false",
159 }.items(),
160 )
161
162 ur_moveit_launch = IncludeLaunchDescription(
163 PythonLaunchDescriptionSource(
164 [FindPackageShare("ur_moveit_config"), "/launch", "/ur_moveit.launch.py"]
165 ),
166 launch_arguments={
167 "ur_type": ur_type,
168 "description_package": description_package,
169 "description_file": description_file,
170 "moveit_config_package": moveit_config_package,
171 "moveit_config_file": moveit_config_file,
172 "use_sim_time": "false",
173 "prefix": prefix,
174 "launch_rviz": "true",
175 }.items(),
176 )
177
178 nodes_to_launch = [
179 ur_control_launch,
180 ur_moveit_launch,
181 sm_test_moveit_ur5_sim_node,
182 ]
183
184 return nodes_to_launch
185
186
188 declared_arguments = []
189 # General arguments
190 declared_arguments.append(
191 DeclareLaunchArgument(
192 "runtime_config_package",
193 default_value="ur_bringup",
194 description='Package with the controller\'s configuration in "config" folder. \
195 Usually the argument is not set, it enables use of a custom setup.',
196 )
197 )
198 declared_arguments.append(
199 DeclareLaunchArgument(
200 "controllers_file",
201 default_value="ur_controllers.yaml",
202 description="YAML file with the controllers configuration.",
203 )
204 )
205 declared_arguments.append(
206 DeclareLaunchArgument(
207 "description_package",
208 default_value="ur_description",
209 description="Description package with robot URDF/XACRO files. Usually the argument \
210 is not set, it enables use of a custom description.",
211 )
212 )
213 declared_arguments.append(
214 DeclareLaunchArgument(
215 "description_file",
216 default_value="ur.urdf.xacro",
217 description="URDF/XACRO description file with the robot.",
218 )
219 )
220 declared_arguments.append(
221 DeclareLaunchArgument(
222 "moveit_config_package",
223 default_value="ur_moveit_config",
224 description="MoveIt config package with robot SRDF/XACRO files. Usually the argument \
225 is not set, it enables use of a custom moveit config.",
226 )
227 )
228 declared_arguments.append(
229 DeclareLaunchArgument(
230 "moveit_config_file",
231 default_value="ur.srdf.xacro",
232 description="MoveIt SRDF/XACRO description file with the robot.",
233 )
234 )
235 declared_arguments.append(
236 DeclareLaunchArgument(
237 "prefix",
238 default_value='""',
239 description="Prefix of the joint names, useful for \
240 multi-robot setup. If changed than also joint names in the controllers' configuration \
241 have to be updated.",
242 )
243 )
244 declared_arguments.append(
245 DeclareLaunchArgument("launch_rviz", default_value="true", description="Launch RViz?")
246 )
247
248 return LaunchDescription(declared_arguments + [OpaqueFunction(function=launch_setup)])