2
2
3
3
Guides/Launch-file-different-formats
4
4
5
- Using Python, XML , and YAML for ROS 2 Launch Files
5
+ Using XML, YAML , and Python for ROS 2 Launch Files
6
6
==================================================
7
7
8
8
.. contents :: Table of Contents
@@ -29,290 +29,19 @@ Each launch file performs the following actions:
29
29
30
30
.. group-tab :: XML
31
31
32
- .. code-block :: xml
33
-
34
- <!-- example_launch.xml -->
35
-
36
- <launch >
37
-
38
- <!-- args that can be set from the command line or a default will be used -->
39
- <arg name =" background_r" default =" 0" />
40
- <arg name =" background_g" default =" 255" />
41
- <arg name =" background_b" default =" 0" />
42
- <arg name =" chatter_py_ns" default =" chatter/py/ns" />
43
- <arg name =" chatter_xml_ns" default =" chatter/xml/ns" />
44
- <arg name =" chatter_yaml_ns" default =" chatter/yaml/ns" />
45
-
46
- <!-- include another launch file -->
47
- <include file =" $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py" />
48
- <!-- include a Python launch file in the chatter_py_ns namespace-->
49
- <group >
50
- <!-- push_ros_namespace to set namespace of included nodes -->
51
- <push_ros_namespace namespace =" $(var chatter_py_ns)" />
52
- <include file =" $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py" />
53
- </group >
54
- <!-- include a xml launch file in the chatter_xml_ns namespace-->
55
- <group >
56
- <!-- push_ros_namespace to set namespace of included nodes -->
57
- <push_ros_namespace namespace =" $(var chatter_xml_ns)" />
58
- <include file =" $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.xml" />
59
- </group >
60
- <!-- include a yaml launch file in the chatter_yaml_ns namespace-->
61
- <group >
62
- <!-- push_ros_namespace to set namespace of included nodes -->
63
- <push_ros_namespace namespace =" $(var chatter_yaml_ns)" />
64
- <include file =" $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.yaml" />
65
- </group >
66
-
67
- <!-- start a turtlesim_node in the turtlesim1 namespace -->
68
- <node pkg =" turtlesim" exec =" turtlesim_node" name =" sim" namespace =" turtlesim1" />
69
- <!-- start another turtlesim_node in the turtlesim2 namespace
70
- and use args to set parameters -->
71
- <node pkg =" turtlesim" exec =" turtlesim_node" name =" sim" namespace =" turtlesim2" >
72
- <param name =" background_r" value =" $(var background_r)" />
73
- <param name =" background_g" value =" $(var background_g)" />
74
- <param name =" background_b" value =" $(var background_b)" />
75
- </node >
76
- <!-- perform remap so both turtles listen to the same command topic -->
77
- <node pkg =" turtlesim" exec =" mimic" name =" mimic" >
78
- <remap from =" /input/pose" to =" /turtlesim1/turtle1/pose" />
79
- <remap from =" /output/cmd_vel" to =" /turtlesim2/turtle1/cmd_vel" />
80
- </node >
81
- </launch >
32
+ .. literalinclude :: launch/different_formats_launch.xml
33
+ :language: xml
82
34
83
35
.. group-tab :: YAML
84
36
85
- .. code-block :: yaml
86
-
87
- # example_launch.yaml
88
-
89
- launch :
90
-
91
- # args that can be set from the command line or a default will be used
92
- - arg :
93
- name : " background_r"
94
- default : " 0"
95
- - arg :
96
- name : " background_g"
97
- default : " 255"
98
- - arg :
99
- name : " background_b"
100
- default : " 0"
101
- - arg :
102
- name : " chatter_py_ns"
103
- default : " chatter/py/ns"
104
- - arg :
105
- name : " chatter_xml_ns"
106
- default : " chatter/xml/ns"
107
- - arg :
108
- name : " chatter_yaml_ns"
109
- default : " chatter/yaml/ns"
110
-
111
-
112
- # include another launch file
113
- - include :
114
- file : " $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py"
115
-
116
- # include a Python launch file in the chatter_py_ns namespace
117
- - group :
118
- - push_ros_namespace :
119
- namespace : " $(var chatter_py_ns)"
120
- - include :
121
- file : " $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py"
122
-
123
- # include a xml launch file in the chatter_xml_ns namespace
124
- - group :
125
- - push_ros_namespace :
126
- namespace : " $(var chatter_xml_ns)"
127
- - include :
128
- file : " $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.xml"
129
-
130
- # include a yaml launch file in the chatter_yaml_ns namespace
131
- - group :
132
- - push_ros_namespace :
133
- namespace : " $(var chatter_yaml_ns)"
134
- - include :
135
- file : " $(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.yaml"
136
-
137
- # start a turtlesim_node in the turtlesim1 namespace
138
- - node :
139
- pkg : " turtlesim"
140
- exec : " turtlesim_node"
141
- name : " sim"
142
- namespace : " turtlesim1"
143
-
144
- # start another turtlesim_node in the turtlesim2 namespace and use args to set parameters
145
- - node :
146
- pkg : " turtlesim"
147
- exec : " turtlesim_node"
148
- name : " sim"
149
- namespace : " turtlesim2"
150
- param :
151
- -
152
- name : " background_r"
153
- value : " $(var background_r)"
154
- -
155
- name : " background_g"
156
- value : " $(var background_g)"
157
- -
158
- name : " background_b"
159
- value : " $(var background_b)"
160
-
161
- # perform remap so both turtles listen to the same command topic
162
- - node :
163
- pkg : " turtlesim"
164
- exec : " mimic"
165
- name : " mimic"
166
- remap :
167
- -
168
- from : " /input/pose"
169
- to : " /turtlesim1/turtle1/pose"
170
- -
171
- from : " /output/cmd_vel"
172
- to : " /turtlesim2/turtle1/cmd_vel"
37
+ .. literalinclude :: launch/different_formats_launch.yaml
38
+ :language: yaml
173
39
174
40
.. group-tab :: Python
175
41
176
- .. code-block :: python
177
-
178
- # example_launch.py
179
-
180
- import os
181
-
182
- from ament_index_python import get_package_share_directory
183
-
184
- from launch import LaunchDescription
185
- from launch.actions import DeclareLaunchArgument
186
- from launch.actions import GroupAction
187
- from launch.actions import IncludeLaunchDescription
188
- from launch.launch_description_sources import PythonLaunchDescriptionSource
189
- from launch.substitutions import LaunchConfiguration
190
- from launch.substitutions import TextSubstitution
191
- from launch_ros.actions import Node
192
- from launch_ros.actions import PushROSNamespace
193
- from launch_xml.launch_description_sources import XMLLaunchDescriptionSource
194
- from launch_yaml.launch_description_sources import YAMLLaunchDescriptionSource
195
-
196
-
197
- def generate_launch_description ():
198
-
199
- # args that can be set from the command line or a default will be used
200
- background_r_launch_arg = DeclareLaunchArgument(
201
- " background_r" , default_value = TextSubstitution(text = " 0" )
202
- )
203
- background_g_launch_arg = DeclareLaunchArgument(
204
- " background_g" , default_value = TextSubstitution(text = " 255" )
205
- )
206
- background_b_launch_arg = DeclareLaunchArgument(
207
- " background_b" , default_value = TextSubstitution(text = " 0" )
208
- )
209
- chatter_py_ns_launch_arg = DeclareLaunchArgument(
210
- " chatter_py_ns" , default_value = TextSubstitution(text = " chatter/py/ns" )
211
- )
212
- chatter_xml_ns_launch_arg = DeclareLaunchArgument(
213
- " chatter_xml_ns" , default_value = TextSubstitution(text = " chatter/xml/ns" )
214
- )
215
- chatter_yaml_ns_launch_arg = DeclareLaunchArgument(
216
- " chatter_yaml_ns" , default_value = TextSubstitution(text = " chatter/yaml/ns" )
217
- )
218
-
219
- # include another launch file
220
- launch_include = IncludeLaunchDescription(
221
- PythonLaunchDescriptionSource(
222
- os.path.join(
223
- get_package_share_directory(' demo_nodes_cpp' ),
224
- ' launch/topics/talker_listener_launch.py' ))
225
- )
226
- # include a Python launch file in the chatter_py_ns namespace
227
- launch_py_include_with_namespace = GroupAction(
228
- actions = [
229
- # push_ros_namespace first to set namespace of included nodes for following actions
230
- PushROSNamespace(' chatter_py_ns' ),
231
- IncludeLaunchDescription(
232
- PythonLaunchDescriptionSource(
233
- os.path.join(
234
- get_package_share_directory(' demo_nodes_cpp' ),
235
- ' launch/topics/talker_listener_launch.py' ))
236
- ),
237
- ]
238
- )
239
-
240
- # include a xml launch file in the chatter_xml_ns namespace
241
- launch_xml_include_with_namespace = GroupAction(
242
- actions = [
243
- # push_ros_namespace first to set namespace of included nodes for following actions
244
- PushROSNamespace(' chatter_xml_ns' ),
245
- IncludeLaunchDescription(
246
- XMLLaunchDescriptionSource(
247
- os.path.join(
248
- get_package_share_directory(' demo_nodes_cpp' ),
249
- ' launch/topics/talker_listener_launch.xml' ))
250
- ),
251
- ]
252
- )
253
-
254
- # include a yaml launch file in the chatter_yaml_ns namespace
255
- launch_yaml_include_with_namespace = GroupAction(
256
- actions = [
257
- # push_ros_namespace first to set namespace of included nodes for following actions
258
- PushROSNamespace(' chatter_yaml_ns' ),
259
- IncludeLaunchDescription(
260
- YAMLLaunchDescriptionSource(
261
- os.path.join(
262
- get_package_share_directory(' demo_nodes_cpp' ),
263
- ' launch/topics/talker_listener_launch.yaml' ))
264
- ),
265
- ]
266
- )
267
-
268
- # start a turtlesim_node in the turtlesim1 namespace
269
- turtlesim_node = Node(
270
- package = ' turtlesim' ,
271
- namespace = ' turtlesim1' ,
272
- executable = ' turtlesim_node' ,
273
- name = ' sim'
274
- )
275
-
276
- # start another turtlesim_node in the turtlesim2 namespace
277
- # and use args to set parameters
278
- turtlesim_node_with_parameters = Node(
279
- package = ' turtlesim' ,
280
- namespace = ' turtlesim2' ,
281
- executable = ' turtlesim_node' ,
282
- name = ' sim' ,
283
- parameters = [{
284
- " background_r" : LaunchConfiguration(' background_r' ),
285
- " background_g" : LaunchConfiguration(' background_g' ),
286
- " background_b" : LaunchConfiguration(' background_b' ),
287
- }]
288
- )
289
-
290
- # perform remap so both turtles listen to the same command topic
291
- forward_turtlesim_commands_to_second_turtlesim_node = Node(
292
- package = ' turtlesim' ,
293
- executable = ' mimic' ,
294
- name = ' mimic' ,
295
- remappings = [
296
- (' /input/pose' , ' /turtlesim1/turtle1/pose' ),
297
- (' /output/cmd_vel' , ' /turtlesim2/turtle1/cmd_vel' ),
298
- ]
299
- )
300
-
301
- return LaunchDescription([
302
- background_r_launch_arg,
303
- background_g_launch_arg,
304
- background_b_launch_arg,
305
- chatter_py_ns_launch_arg,
306
- chatter_xml_ns_launch_arg,
307
- chatter_yaml_ns_launch_arg,
308
- launch_include,
309
- launch_py_include_with_namespace,
310
- launch_xml_include_with_namespace,
311
- launch_yaml_include_with_namespace,
312
- turtlesim_node,
313
- turtlesim_node_with_parameters,
314
- forward_turtlesim_commands_to_second_turtlesim_node,
315
- ])
42
+ .. literalinclude :: launch/different_formats_launch.py
43
+ :language: python
44
+
316
45
317
46
Using the Launch files from the command line
318
47
--------------------------------------------
@@ -361,7 +90,7 @@ To test that the remapping is working, you can control the turtles by running th
361
90
362
91
.. _launch-file-different-formats-which :
363
92
364
- Python, XML , or YAML : Which should I use?
93
+ XML, YAML , or Python : Which should I use?
365
94
-----------------------------------------
366
95
367
96
.. note ::
0 commit comments