Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
slam_launch.py
1 # Copyright (c) 2020 Samsung Research Russia
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 from launch.conditions import IfCondition, UnlessCondition
21 from launch.launch_description_sources import PythonLaunchDescriptionSource
22 from launch.substitutions import LaunchConfiguration
23 from launch_ros.actions import Node, PushROSNamespace, SetParameter, SetRemap
24 from launch_ros.descriptions import ParameterFile
25 from nav2_common.launch import HasNodeParams, LaunchConfigAsBool, RewrittenYaml
26 
27 
28 def get_lifecycle_nodes(context):
29  return ('map_saver',)
30 
31 
32 def generate_launch_description() -> LaunchDescription:
33  # Input parameters declaration
34  namespace = LaunchConfiguration('namespace')
35  params_file = LaunchConfiguration('params_file')
36  use_sim_time = LaunchConfigAsBool('use_sim_time')
37  use_respawn = LaunchConfigAsBool('use_respawn')
38  log_level = LaunchConfiguration('log_level')
39 
40  # Variables
41  # Getting directories and launch-files
42  bringup_dir = get_package_share_directory('nav2_bringup')
43  slam_toolbox_dir = get_package_share_directory('slam_toolbox')
44  slam_launch_file = os.path.join(slam_toolbox_dir, 'launch', 'online_sync_launch.py')
45 
46  # Create our own temporary YAML files that include substitutions
47  configured_params = ParameterFile(
48  RewrittenYaml(
49  source_file=params_file,
50  root_key=namespace,
51  param_rewrites={},
52  convert_types=True,
53  ),
54  allow_substs=True,
55  )
56 
57  # Declare the launch arguments
58  declare_namespace_cmd = DeclareLaunchArgument(
59  'namespace', default_value='', description='Top-level namespace'
60  )
61 
62  declare_params_file_cmd = DeclareLaunchArgument(
63  'params_file',
64  default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
65  description='Full path to the ROS2 parameters file to use for all launched nodes',
66  )
67 
68  declare_use_sim_time_cmd = DeclareLaunchArgument(
69  'use_sim_time',
70  default_value='True',
71  description='Use simulation (Gazebo) clock if true',
72  )
73 
74  declare_use_respawn_cmd = DeclareLaunchArgument(
75  'use_respawn',
76  default_value='False',
77  description='Whether to respawn if a node crashes. Applied when composition is disabled.',
78  )
79 
80  declare_log_level_cmd = DeclareLaunchArgument(
81  'log_level', default_value='info', description='log level'
82  )
83 
84  # Nodes launching commands
85  start_map_server = GroupAction(
86  actions=[
87  PushROSNamespace(namespace),
88  SetParameter('use_sim_time', use_sim_time),
89  Node(
90  package='nav2_map_server',
91  executable='map_saver_server',
92  output='screen',
93  respawn=use_respawn,
94  respawn_delay=2.0,
95  arguments=['--ros-args', '--log-level', log_level],
96  parameters=[configured_params],
97  ),
98  ]
99  )
100 
101  # If the provided param file doesn't have slam_toolbox params, we must remove the 'params_file'
102  # LaunchConfiguration, or it will be passed automatically to slam_toolbox and will not load
103  # the default file
104  has_slam_toolbox_params = HasNodeParams(
105  source_file=params_file, node_name='slam_toolbox'
106  )
107 
108  start_slam_toolbox_cmd = GroupAction(
109 
110  actions=[
111  PushROSNamespace(namespace),
112 
113  # Remapping required to have a slam session subscribe & publish in optional namespaces
114  SetRemap(src='/scan', dst='scan'),
115  SetRemap(src='/tf', dst='tf'),
116  SetRemap(src='/tf_static', dst='tf_static'),
117  SetRemap(src='/map', dst='map'),
118 
119  IncludeLaunchDescription(
120  PythonLaunchDescriptionSource(slam_launch_file),
121  launch_arguments={'use_sim_time': use_sim_time}.items(),
122  condition=UnlessCondition(has_slam_toolbox_params),
123  ),
124 
125  IncludeLaunchDescription(
126  PythonLaunchDescriptionSource(slam_launch_file),
127  launch_arguments={'use_sim_time': use_sim_time,
128  'slam_params_file': params_file}.items(),
129  condition=IfCondition(has_slam_toolbox_params),
130  )
131  ]
132  )
133 
134  ld = LaunchDescription()
135 
136  # Declare the launch options
137  ld.add_action(declare_namespace_cmd)
138  ld.add_action(declare_params_file_cmd)
139  ld.add_action(declare_use_sim_time_cmd)
140  ld.add_action(declare_use_respawn_cmd)
141  ld.add_action(declare_log_level_cmd)
142 
143  # Running Map Saver Server
144  ld.add_action(start_map_server)
145 
146  # Running SLAM Toolbox (Only one of them will be run)
147  ld.add_action(start_slam_toolbox_cmd)
148 
149  return ld