Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
tb3_loopback_simulation_launch.py
1 # Copyright (C) 2024 Open Navigation LLC
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 """This is all-in-one launch script intended for use by nav2 developers."""
16 
17 import os
18 
19 from ament_index_python.packages import get_package_share_directory
20 from launch import LaunchDescription
21 from launch.actions import DeclareLaunchArgument, GroupAction, IncludeLaunchDescription
22 from launch.conditions import IfCondition
23 from launch.launch_description_sources import PythonLaunchDescriptionSource
24 from launch.substitutions import LaunchConfiguration, PythonExpression
25 from launch_ros.actions import LoadComposableNodes, Node, SetParameter
26 from launch_ros.descriptions import ComposableNode, ParameterFile
27 from nav2_common.launch import LaunchConfigAsBool, RewrittenYaml
28 
29 
30 def generate_launch_description() -> LaunchDescription:
31  # Get the launch directory
32  bringup_dir = get_package_share_directory('nav2_bringup')
33  loopback_sim_dir = get_package_share_directory('nav2_loopback_sim')
34  launch_dir = os.path.join(bringup_dir, 'launch')
35  sim_dir = get_package_share_directory('nav2_minimal_tb3_sim')
36 
37  # Create the launch configuration variables
38  namespace = LaunchConfiguration('namespace')
39  map_yaml_file = LaunchConfiguration('map')
40  graph_filepath = LaunchConfiguration('graph')
41  params_file = LaunchConfiguration('params_file')
42  autostart = LaunchConfigAsBool('autostart')
43  use_composition = LaunchConfigAsBool('use_composition')
44  use_intra_process_comms = LaunchConfigAsBool('use_intra_process_comms')
45  use_respawn = LaunchConfigAsBool('use_respawn')
46 
47  # Launch configuration variables specific to simulation
48  rviz_config_file = LaunchConfiguration('rviz_config_file')
49  use_robot_state_pub = LaunchConfigAsBool('use_robot_state_pub')
50  use_rviz = LaunchConfigAsBool('use_rviz')
51  container_name_full = (namespace, '/', 'nav2_container')
52 
53  remappings = [('/tf', 'tf'), ('/tf_static', 'tf_static')]
54 
55  # Declare the launch arguments
56  declare_namespace_cmd = DeclareLaunchArgument(
57  'namespace', default_value='', description='Top-level namespace'
58  )
59 
60  declare_map_yaml_cmd = DeclareLaunchArgument(
61  'map',
62  default_value=os.path.join(bringup_dir, 'maps', 'tb3_sandbox.yaml'),
63  )
64 
65  declare_graph_file_cmd = DeclareLaunchArgument(
66  'graph',
67  default_value=os.path.join(bringup_dir, 'graphs', 'turtlebot3_graph.geojson'),
68  )
69 
70  declare_params_file_cmd = DeclareLaunchArgument(
71  'params_file',
72  default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
73  description='Full path to the ROS2 parameters file to use for all launched nodes',
74  )
75 
76  declare_autostart_cmd = DeclareLaunchArgument(
77  'autostart',
78  default_value='true',
79  description='Automatically startup the nav2 stack',
80  )
81 
82  declare_use_composition_cmd = DeclareLaunchArgument(
83  'use_composition',
84  default_value='True',
85  description='Whether to use composed bringup',
86  )
87 
88  declare_use_intra_process_comms_cmd = DeclareLaunchArgument(
89  'use_intra_process_comms',
90  default_value='False',
91  description='Whether to use intra process communication',
92  )
93 
94  declare_use_respawn_cmd = DeclareLaunchArgument(
95  'use_respawn',
96  default_value='False',
97  description='Whether to respawn if a node crashes. Applied when composition is disabled.',
98  )
99 
100  declare_rviz_config_file_cmd = DeclareLaunchArgument(
101  'rviz_config_file',
102  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
103  description='Full path to the RVIZ config file to use',
104  )
105 
106  declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
107  'use_robot_state_pub',
108  default_value='True',
109  description='Whether to start the robot state publisher',
110  )
111 
112  declare_use_rviz_cmd = DeclareLaunchArgument(
113  'use_rviz', default_value='True', description='Whether to start RVIZ'
114  )
115 
116  urdf = os.path.join(sim_dir, 'urdf', 'turtlebot3_waffle.urdf')
117  with open(urdf, 'r') as infp:
118  robot_description = infp.read()
119 
120  start_robot_state_publisher_cmd = Node(
121  condition=IfCondition(use_robot_state_pub),
122  package='robot_state_publisher',
123  executable='robot_state_publisher',
124  name='robot_state_publisher',
125  namespace=namespace,
126  output='screen',
127  parameters=[
128  {'use_sim_time': True, 'robot_description': robot_description}
129  ],
130  remappings=remappings,
131  )
132 
133  rviz_cmd = IncludeLaunchDescription(
134  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'rviz_launch.py')),
135  condition=IfCondition(use_rviz),
136  launch_arguments=[
137  ('namespace', namespace),
138  ('use_sim_time', 'True'),
139  ('rviz_config', rviz_config_file),
140  ],
141  )
142 
143  bringup_cmd = IncludeLaunchDescription(
144  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'bringup_launch.py')),
145  launch_arguments={
146  'namespace': namespace,
147  'map': map_yaml_file,
148  'graph': graph_filepath,
149  'use_sim_time': 'True',
150  'params_file': params_file,
151  'autostart': autostart,
152  'use_composition': use_composition,
153  'use_intra_process_comms': use_intra_process_comms,
154  'use_respawn': use_respawn,
155  'use_localization': 'False', # Don't use SLAM, AMCL
156  'use_keepout_zones': 'False',
157  'use_speed_zones': 'False',
158  'container_name': 'nav2_container',
159  }.items(),
160  )
161 
162  loopback_sim_cmd = IncludeLaunchDescription(
163  PythonLaunchDescriptionSource(
164  os.path.join(loopback_sim_dir, 'loopback_simulation.launch.py')),
165  launch_arguments={
166  'params_file': params_file,
167  }.items(),
168  )
169 
170  configured_params = ParameterFile(
171  RewrittenYaml(
172  source_file=params_file,
173  root_key=namespace,
174  param_rewrites={},
175  convert_types=True,
176  ),
177  allow_substs=True,
178  )
179 
180  start_map_server = GroupAction(
181  condition=IfCondition(PythonExpression(['not ', use_composition])),
182  actions=[
183  SetParameter('use_sim_time', True),
184  Node(
185  package='nav2_map_server',
186  executable='map_server',
187  name='map_server',
188  output='screen',
189  respawn=use_respawn,
190  respawn_delay=2.0,
191  parameters=[configured_params, {'yaml_filename': map_yaml_file}],
192  remappings=remappings,
193  ),
194  Node(
195  package='nav2_lifecycle_manager',
196  executable='lifecycle_manager',
197  name='lifecycle_manager_map_server',
198  output='screen',
199  parameters=[
200  configured_params,
201  {'autostart': autostart}, {'node_names': ['map_server']}],
202  ),
203  ]
204  )
205 
206  start_composable_map_server = GroupAction(
207  condition=IfCondition(use_composition),
208  actions=[
209  SetParameter('use_sim_time', True),
210  LoadComposableNodes(
211  target_container=container_name_full,
212  composable_node_descriptions=[
213  ComposableNode(
214  package='nav2_map_server',
215  plugin='nav2_map_server::MapServer',
216  name='map_server',
217  parameters=[configured_params, {'yaml_filename': map_yaml_file}],
218  remappings=remappings,
219  extra_arguments=[{'use_intra_process_comms': use_intra_process_comms}],
220  ),
221  ComposableNode(
222  package='nav2_lifecycle_manager',
223  plugin='nav2_lifecycle_manager::LifecycleManager',
224  name='lifecycle_manager_map_server',
225  parameters=[
226  configured_params,
227  {'autostart': autostart}, {'node_names': ['map_server']}
228  ],
229  extra_arguments=[{'use_intra_process_comms': use_intra_process_comms}],
230  ),
231  ],
232  ),
233  ],
234  )
235 
236  # Create the launch description and populate
237  ld = LaunchDescription()
238 
239  # Declare the launch options
240  ld.add_action(declare_namespace_cmd)
241  ld.add_action(declare_map_yaml_cmd)
242  ld.add_action(declare_graph_file_cmd)
243  ld.add_action(declare_params_file_cmd)
244  ld.add_action(declare_autostart_cmd)
245  ld.add_action(declare_use_composition_cmd)
246  ld.add_action(declare_use_intra_process_comms_cmd)
247 
248  ld.add_action(declare_rviz_config_file_cmd)
249  ld.add_action(declare_use_robot_state_pub_cmd)
250  ld.add_action(declare_use_rviz_cmd)
251  ld.add_action(declare_use_respawn_cmd)
252 
253  # Add the actions to launch all of the navigation nodes
254  ld.add_action(start_robot_state_publisher_cmd)
255  ld.add_action(start_map_server)
256  ld.add_action(start_composable_map_server)
257  ld.add_action(loopback_sim_cmd)
258  ld.add_action(rviz_cmd)
259  ld.add_action(bringup_cmd)
260 
261  return ld