Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
bringup_launch.py
1 # Copyright (c) 2018 Intel Corporation
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 import os
16 
17 from ament_index_python.packages import get_package_share_directory
18 from launch import LaunchDescription
19 from launch.actions import (DeclareLaunchArgument, GroupAction, IncludeLaunchDescription,
20  SetEnvironmentVariable)
21 from launch.conditions import IfCondition
22 from launch.launch_description_sources import PythonLaunchDescriptionSource
23 from launch.substitutions import LaunchConfiguration, PythonExpression
24 from launch_ros.actions import Node
25 from launch_ros.descriptions import ParameterFile
26 from nav2_common.launch import LaunchConfigAsBool, RewrittenYaml
27 
28 
29 def generate_launch_description() -> LaunchDescription:
30  # Get the launch directory
31  bringup_dir = get_package_share_directory('nav2_bringup')
32  launch_dir = os.path.join(bringup_dir, 'launch')
33 
34  # Create the launch configuration variables
35  namespace = LaunchConfiguration('namespace')
36  slam = LaunchConfigAsBool('slam')
37  map_yaml_file = LaunchConfiguration('map')
38  keepout_mask_yaml_file = LaunchConfiguration('keepout_mask')
39  speed_mask_yaml_file = LaunchConfiguration('speed_mask')
40  graph_filepath = LaunchConfiguration('graph')
41  use_sim_time = LaunchConfigAsBool('use_sim_time')
42  params_file = LaunchConfiguration('params_file')
43  autostart = LaunchConfigAsBool('autostart')
44  use_composition = LaunchConfigAsBool('use_composition')
45  use_intra_process_comms = LaunchConfigAsBool('use_intra_process_comms')
46  container_name = LaunchConfiguration('container_name')
47  use_respawn = LaunchConfigAsBool('use_respawn')
48  log_level = LaunchConfiguration('log_level')
49  use_localization = LaunchConfigAsBool('use_localization')
50  use_keepout_zones = LaunchConfigAsBool('use_keepout_zones')
51  use_speed_zones = LaunchConfigAsBool('use_speed_zones')
52 
53  # Map fully qualified names to relative ones so the node's namespace can be prepended.
54  remappings = [('/tf', 'tf'), ('/tf_static', 'tf_static')]
55 
56  yaml_substitutions = {
57  'KEEPOUT_ZONE_ENABLED': use_keepout_zones,
58  'SPEED_ZONE_ENABLED': use_speed_zones,
59  }
60 
61  configured_params = ParameterFile(
62  RewrittenYaml(
63  source_file=params_file,
64  root_key=namespace,
65  param_rewrites={},
66  value_rewrites=yaml_substitutions,
67  convert_types=True,
68  ),
69  allow_substs=True,
70  )
71 
72  stdout_linebuf_envvar = SetEnvironmentVariable(
73  'RCUTILS_LOGGING_BUFFERED_STREAM', '1'
74  )
75 
76  declare_namespace_cmd = DeclareLaunchArgument(
77  'namespace', default_value='', description='Top-level namespace'
78  )
79 
80  declare_slam_cmd = DeclareLaunchArgument(
81  'slam', default_value='False', description='Whether run a SLAM'
82  )
83 
84  declare_map_yaml_cmd = DeclareLaunchArgument(
85  'map', default_value='', description='Full path to map yaml file to load'
86  )
87 
88  declare_keepout_mask_yaml_cmd = DeclareLaunchArgument(
89  'keepout_mask', default_value='',
90  description='Full path to keepout mask yaml file to load'
91  )
92 
93  declare_speed_mask_yaml_cmd = DeclareLaunchArgument(
94  'speed_mask', default_value='',
95  description='Full path to speed mask yaml file to load'
96  )
97 
98  declare_graph_file_cmd = DeclareLaunchArgument(
99  'graph',
100  default_value='', description='Path to the graph file to load'
101  )
102 
103  declare_use_localization_cmd = DeclareLaunchArgument(
104  'use_localization', default_value='True',
105  description='Whether to enable localization or not'
106  )
107 
108  declare_use_keepout_zones_cmd = DeclareLaunchArgument(
109  'use_keepout_zones', default_value='True',
110  description='Whether to enable keepout zones or not'
111  )
112 
113  declare_use_speed_zones_cmd = DeclareLaunchArgument(
114  'use_speed_zones', default_value='True',
115  description='Whether to enable speed zones or not'
116  )
117 
118  declare_use_sim_time_cmd = DeclareLaunchArgument(
119  'use_sim_time',
120  default_value='false',
121  description='Use simulation (Gazebo) clock if true',
122  )
123 
124  declare_params_file_cmd = DeclareLaunchArgument(
125  'params_file',
126  default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
127  description='Full path to the ROS2 parameters file to use for all launched nodes',
128  )
129 
130  declare_autostart_cmd = DeclareLaunchArgument(
131  'autostart',
132  default_value='true',
133  description='Automatically startup the nav2 stack',
134  )
135 
136  declare_use_composition_cmd = DeclareLaunchArgument(
137  'use_composition',
138  default_value='True',
139  description='Whether to use composed bringup',
140  )
141 
142  declare_use_intra_process_comms_cmd = DeclareLaunchArgument(
143  'use_intra_process_comms',
144  default_value='False',
145  description='Whether to use intra process communications',
146  )
147 
148  declare_container_name_cmd = DeclareLaunchArgument(
149  'container_name',
150  default_value='nav2_container',
151  description='the name of container that nodes will load in if use composition',
152  )
153 
154  declare_use_respawn_cmd = DeclareLaunchArgument(
155  'use_respawn',
156  default_value='False',
157  description='Whether to respawn if a node crashes. Applied when composition is disabled.',
158  )
159 
160  declare_log_level_cmd = DeclareLaunchArgument(
161  'log_level', default_value='info', description='log level'
162  )
163 
164  # Specify the actions
165  bringup_cmd_group = GroupAction(
166  [
167  Node(
168  condition=IfCondition(use_composition),
169  name=container_name,
170  namespace=namespace,
171  package='rclcpp_components',
172  executable='component_container',
173  parameters=[configured_params, {'autostart': autostart}],
174  arguments=['--isolated', '--executor-type', 'single-threaded',
175  '--ros-args', '--log-level', log_level],
176  remappings=remappings,
177  output='screen',
178  ),
179  IncludeLaunchDescription(
180  PythonLaunchDescriptionSource(
181  os.path.join(launch_dir, 'slam_launch.py')
182  ),
183  condition=IfCondition(PythonExpression([slam, ' and ', use_localization])),
184  launch_arguments={
185  'namespace': namespace,
186  'use_sim_time': use_sim_time,
187  'autostart': autostart,
188  'use_respawn': use_respawn,
189  'params_file': params_file,
190  }.items(),
191  ),
192  IncludeLaunchDescription(
193  PythonLaunchDescriptionSource(
194  os.path.join(launch_dir, 'localization_launch.py')
195  ),
196  condition=IfCondition(PythonExpression(['not ', slam, ' and ', use_localization])),
197  launch_arguments={
198  'namespace': namespace,
199  'map': map_yaml_file,
200  'use_sim_time': use_sim_time,
201  'autostart': autostart,
202  'params_file': params_file,
203  'use_composition': use_composition,
204  'use_intra_process_comms': use_intra_process_comms,
205  'use_respawn': use_respawn,
206  'container_name': container_name,
207  }.items(),
208  ),
209 
210  IncludeLaunchDescription(
211  PythonLaunchDescriptionSource(
212  os.path.join(launch_dir, 'keepout_zone_launch.py')
213  ),
214  condition=IfCondition(use_keepout_zones),
215  launch_arguments={
216  'namespace': namespace,
217  'keepout_mask': keepout_mask_yaml_file,
218  'use_sim_time': use_sim_time,
219  'params_file': params_file,
220  'use_composition': use_composition,
221  'use_intra_process_comms': use_intra_process_comms,
222  'use_respawn': use_respawn,
223  'container_name': container_name,
224  }.items(),
225  ),
226 
227  IncludeLaunchDescription(
228  PythonLaunchDescriptionSource(
229  os.path.join(launch_dir, 'speed_zone_launch.py')
230  ),
231  condition=IfCondition(use_speed_zones),
232  launch_arguments={
233  'namespace': namespace,
234  'speed_mask': speed_mask_yaml_file,
235  'use_sim_time': use_sim_time,
236  'params_file': params_file,
237  'use_composition': use_composition,
238  'use_intra_process_comms': use_intra_process_comms,
239  'use_respawn': use_respawn,
240  'container_name': container_name,
241  }.items(),
242  ),
243 
244  IncludeLaunchDescription(
245  PythonLaunchDescriptionSource(
246  os.path.join(launch_dir, 'navigation_launch.py')
247  ),
248  launch_arguments={
249  'namespace': namespace,
250  'use_sim_time': use_sim_time,
251  'autostart': autostart,
252  'graph': graph_filepath,
253  'params_file': params_file,
254  'use_composition': use_composition,
255  'use_intra_process_comms': use_intra_process_comms,
256  'use_respawn': use_respawn,
257  'use_keepout_zones': use_keepout_zones,
258  'use_speed_zones': use_speed_zones,
259  'container_name': container_name,
260  }.items(),
261  ),
262  ]
263  )
264 
265  # Create the launch description and populate
266  ld = LaunchDescription()
267 
268  # Set environment variables
269  ld.add_action(stdout_linebuf_envvar)
270 
271  # Declare the launch options
272  ld.add_action(declare_namespace_cmd)
273  ld.add_action(declare_slam_cmd)
274  ld.add_action(declare_map_yaml_cmd)
275  ld.add_action(declare_keepout_mask_yaml_cmd)
276  ld.add_action(declare_speed_mask_yaml_cmd)
277  ld.add_action(declare_graph_file_cmd)
278  ld.add_action(declare_use_sim_time_cmd)
279  ld.add_action(declare_params_file_cmd)
280  ld.add_action(declare_autostart_cmd)
281  ld.add_action(declare_use_composition_cmd)
282  ld.add_action(declare_use_intra_process_comms_cmd)
283  ld.add_action(declare_container_name_cmd)
284  ld.add_action(declare_use_respawn_cmd)
285  ld.add_action(declare_log_level_cmd)
286  ld.add_action(declare_use_localization_cmd)
287  ld.add_action(declare_use_keepout_zones_cmd)
288  ld.add_action(declare_use_speed_zones_cmd)
289 
290  # Add the actions to launch all of the navigation nodes
291  ld.add_action(bringup_cmd_group)
292 
293  return ld