|
| 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 | + |
| 19 | +from launch import LaunchDescription |
| 20 | +from launch.actions import DeclareLaunchArgument, GroupAction, SetEnvironmentVariable |
| 21 | +from launch.conditions import IfCondition |
| 22 | +from launch.substitutions import LaunchConfiguration, PythonExpression |
| 23 | +from launch_ros.actions import LoadComposableNodes |
| 24 | +from launch_ros.actions import Node |
| 25 | +from launch_ros.descriptions import ComposableNode |
| 26 | +from nav2_common.launch import RewrittenYaml |
| 27 | + |
| 28 | + |
| 29 | +def generate_launch_description(): |
| 30 | + # Get the launch directory |
| 31 | + bringup_dir = get_package_share_directory('nav2_bringup') |
| 32 | + |
| 33 | + namespace = LaunchConfiguration('namespace') |
| 34 | + map_yaml_file = LaunchConfiguration('map') |
| 35 | + use_sim_time = LaunchConfiguration('use_sim_time') |
| 36 | + autostart = LaunchConfiguration('autostart') |
| 37 | + params_file = LaunchConfiguration('params_file') |
| 38 | + use_composition = LaunchConfiguration('use_composition') |
| 39 | + container_name = LaunchConfiguration('container_name') |
| 40 | + use_respawn = LaunchConfiguration('use_respawn') |
| 41 | + log_level = LaunchConfiguration('log_level') |
| 42 | + |
| 43 | + lifecycle_nodes = ['map_server', 'amcl'] |
| 44 | + |
| 45 | + # Map fully qualified names to relative ones so the node's namespace can be prepended. |
| 46 | + # In case of the transforms (tf), currently, there doesn't seem to be a better alternative |
| 47 | + # https://github.com/ros/geometry2/issues/32 |
| 48 | + # https://github.com/ros/robot_state_publisher/pull/30 |
| 49 | + # TODO(orduno) Substitute with `PushNodeRemapping` |
| 50 | + # https://github.com/ros2/launch_ros/issues/56 |
| 51 | + remappings = [('/tf', 'tf'), |
| 52 | + ('/tf_static', 'tf_static')] |
| 53 | + |
| 54 | + # Create our own temporary YAML files that include substitutions |
| 55 | + param_substitutions = { |
| 56 | + 'use_sim_time': use_sim_time, |
| 57 | + 'yaml_filename': map_yaml_file} |
| 58 | + |
| 59 | + configured_params = RewrittenYaml( |
| 60 | + source_file=params_file, |
| 61 | + root_key=namespace, |
| 62 | + param_rewrites=param_substitutions, |
| 63 | + convert_types=True) |
| 64 | + |
| 65 | + stdout_linebuf_envvar = SetEnvironmentVariable( |
| 66 | + 'RCUTILS_LOGGING_BUFFERED_STREAM', '1') |
| 67 | + |
| 68 | + declare_namespace_cmd = DeclareLaunchArgument( |
| 69 | + 'namespace', |
| 70 | + default_value='', |
| 71 | + description='Top-level namespace') |
| 72 | + |
| 73 | + declare_map_yaml_cmd = DeclareLaunchArgument( |
| 74 | + 'map', |
| 75 | + description='Full path to map yaml file to load') |
| 76 | + |
| 77 | + declare_use_sim_time_cmd = DeclareLaunchArgument( |
| 78 | + 'use_sim_time', |
| 79 | + default_value='false', |
| 80 | + description='Use simulation (Gazebo) clock if true') |
| 81 | + |
| 82 | + declare_params_file_cmd = DeclareLaunchArgument( |
| 83 | + 'params_file', |
| 84 | + default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'), |
| 85 | + description='Full path to the ROS2 parameters file to use for all launched nodes') |
| 86 | + |
| 87 | + declare_autostart_cmd = DeclareLaunchArgument( |
| 88 | + 'autostart', default_value='true', |
| 89 | + description='Automatically startup the nav2 stack') |
| 90 | + |
| 91 | + declare_use_composition_cmd = DeclareLaunchArgument( |
| 92 | + 'use_composition', default_value='False', |
| 93 | + description='Use composed bringup if True') |
| 94 | + |
| 95 | + declare_container_name_cmd = DeclareLaunchArgument( |
| 96 | + 'container_name', default_value='nav2_container', |
| 97 | + description='the name of conatiner that nodes will load in if use composition') |
| 98 | + |
| 99 | + declare_use_respawn_cmd = DeclareLaunchArgument( |
| 100 | + 'use_respawn', default_value='False', |
| 101 | + description='Whether to respawn if a node crashes. Applied when composition is disabled.') |
| 102 | + |
| 103 | + declare_log_level_cmd = DeclareLaunchArgument( |
| 104 | + 'log_level', default_value='info', |
| 105 | + description='log level') |
| 106 | + |
| 107 | + load_nodes = GroupAction( |
| 108 | + condition=IfCondition(PythonExpression(['not ', use_composition])), |
| 109 | + actions=[ |
| 110 | + Node( |
| 111 | + package='nav2_map_server', |
| 112 | + executable='map_server', |
| 113 | + name='map_server', |
| 114 | + output='screen', |
| 115 | + respawn=use_respawn, |
| 116 | + respawn_delay=2.0, |
| 117 | + parameters=[configured_params], |
| 118 | + arguments=['--ros-args', '--log-level', log_level], |
| 119 | + remappings=remappings), |
| 120 | + Node( |
| 121 | + package='nav2_amcl', |
| 122 | + executable='amcl', |
| 123 | + name='amcl', |
| 124 | + output='screen', |
| 125 | + respawn=use_respawn, |
| 126 | + respawn_delay=2.0, |
| 127 | + parameters=[configured_params], |
| 128 | + arguments=['--ros-args', '--log-level', log_level], |
| 129 | + remappings=remappings), |
| 130 | + Node( |
| 131 | + package='nav2_lifecycle_manager', |
| 132 | + executable='lifecycle_manager', |
| 133 | + name='lifecycle_manager_localization', |
| 134 | + output='screen', |
| 135 | + arguments=['--ros-args', '--log-level', log_level], |
| 136 | + parameters=[{'use_sim_time': use_sim_time}, |
| 137 | + {'autostart': autostart}, |
| 138 | + {'node_names': lifecycle_nodes}]) |
| 139 | + ] |
| 140 | + ) |
| 141 | + |
| 142 | + load_composable_nodes = LoadComposableNodes( |
| 143 | + condition=IfCondition(use_composition), |
| 144 | + target_container=container_name, |
| 145 | + composable_node_descriptions=[ |
| 146 | + ComposableNode( |
| 147 | + package='nav2_map_server', |
| 148 | + plugin='nav2_map_server::MapServer', |
| 149 | + name='map_server', |
| 150 | + parameters=[configured_params], |
| 151 | + remappings=remappings), |
| 152 | + ComposableNode( |
| 153 | + package='nav2_amcl', |
| 154 | + plugin='nav2_amcl::AmclNode', |
| 155 | + name='amcl', |
| 156 | + parameters=[configured_params], |
| 157 | + remappings=remappings), |
| 158 | + ComposableNode( |
| 159 | + package='nav2_lifecycle_manager', |
| 160 | + plugin='nav2_lifecycle_manager::LifecycleManager', |
| 161 | + name='lifecycle_manager_localization', |
| 162 | + parameters=[{'use_sim_time': use_sim_time, |
| 163 | + 'autostart': autostart, |
| 164 | + 'node_names': lifecycle_nodes}]), |
| 165 | + ], |
| 166 | + ) |
| 167 | + |
| 168 | + # Create the launch description and populate |
| 169 | + ld = LaunchDescription() |
| 170 | + |
| 171 | + # Set environment variables |
| 172 | + ld.add_action(stdout_linebuf_envvar) |
| 173 | + |
| 174 | + # Declare the launch options |
| 175 | + ld.add_action(declare_namespace_cmd) |
| 176 | + ld.add_action(declare_map_yaml_cmd) |
| 177 | + ld.add_action(declare_use_sim_time_cmd) |
| 178 | + ld.add_action(declare_params_file_cmd) |
| 179 | + ld.add_action(declare_autostart_cmd) |
| 180 | + ld.add_action(declare_use_composition_cmd) |
| 181 | + ld.add_action(declare_container_name_cmd) |
| 182 | + ld.add_action(declare_use_respawn_cmd) |
| 183 | + ld.add_action(declare_log_level_cmd) |
| 184 | + |
| 185 | + # Add the actions to launch all of the localiztion nodes |
| 186 | + ld.add_action(load_nodes) |
| 187 | + ld.add_action(load_composable_nodes) |
| 188 | + |
| 189 | + return ld |
0 commit comments