Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
tb4_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 
21 from launch import LaunchDescription
22 from launch.actions import (
23  DeclareLaunchArgument,
24  GroupAction,
25  IncludeLaunchDescription,
26 )
27 from launch.conditions import IfCondition
28 from launch.launch_description_sources import PythonLaunchDescriptionSource
29 from launch.substitutions import Command, LaunchConfiguration
30 from launch_ros.actions import Node, SetParameter
31 from launch_ros.descriptions import ParameterFile
32 
33 from nav2_common.launch import RewrittenYaml
34 
35 
36 def generate_launch_description():
37  # Get the launch directory
38  bringup_dir = get_package_share_directory('nav2_bringup')
39  loopback_sim_dir = get_package_share_directory('nav2_loopback_sim')
40  launch_dir = os.path.join(bringup_dir, 'launch')
41  desc_dir = get_package_share_directory('nav2_minimal_tb4_description')
42 
43  # Create the launch configuration variables
44  namespace = LaunchConfiguration('namespace')
45  use_namespace = LaunchConfiguration('use_namespace')
46  map_yaml_file = LaunchConfiguration('map')
47  params_file = LaunchConfiguration('params_file')
48  autostart = LaunchConfiguration('autostart')
49  use_composition = LaunchConfiguration('use_composition')
50  use_respawn = LaunchConfiguration('use_respawn')
51 
52  # Launch configuration variables specific to simulation
53  rviz_config_file = LaunchConfiguration('rviz_config_file')
54  use_robot_state_pub = LaunchConfiguration('use_robot_state_pub')
55  use_rviz = LaunchConfiguration('use_rviz')
56 
57  remappings = [('/tf', 'tf'), ('/tf_static', 'tf_static')]
58 
59  # Declare the launch arguments
60  declare_namespace_cmd = DeclareLaunchArgument(
61  'namespace', default_value='', description='Top-level namespace'
62  )
63 
64  declare_use_namespace_cmd = DeclareLaunchArgument(
65  'use_namespace',
66  default_value='false',
67  description='Whether to apply a namespace to the navigation stack',
68  )
69 
70  declare_map_yaml_cmd = DeclareLaunchArgument(
71  'map',
72  default_value=os.path.join(bringup_dir, 'maps', 'depot.yaml'), # Try warehouse.yaml!
73  description='Full path to map file to load',
74  )
75 
76  declare_params_file_cmd = DeclareLaunchArgument(
77  'params_file',
78  default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
79  description='Full path to the ROS2 parameters file to use for all launched nodes',
80  )
81 
82  declare_autostart_cmd = DeclareLaunchArgument(
83  'autostart',
84  default_value='true',
85  description='Automatically startup the nav2 stack',
86  )
87 
88  declare_use_composition_cmd = DeclareLaunchArgument(
89  'use_composition',
90  default_value='True',
91  description='Whether to use composed bringup',
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  sdf = os.path.join(desc_dir, 'urdf', 'standard', 'turtlebot4.urdf.xacro')
117  start_robot_state_publisher_cmd = Node(
118  condition=IfCondition(use_robot_state_pub),
119  package='robot_state_publisher',
120  executable='robot_state_publisher',
121  name='robot_state_publisher',
122  namespace=namespace,
123  output='screen',
124  parameters=[
125  {'use_sim_time': True, 'robot_description': Command(['xacro', ' ', sdf])}
126  ],
127  remappings=remappings,
128  )
129 
130  rviz_cmd = IncludeLaunchDescription(
131  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'rviz_launch.py')),
132  condition=IfCondition(use_rviz),
133  launch_arguments={
134  'namespace': namespace,
135  'use_namespace': use_namespace,
136  'use_sim_time': 'True',
137  'rviz_config': rviz_config_file,
138  }.items(),
139  )
140 
141  bringup_cmd = IncludeLaunchDescription(
142  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'bringup_launch.py')),
143  launch_arguments={
144  'namespace': namespace,
145  'use_namespace': use_namespace,
146  'map': map_yaml_file,
147  'use_sim_time': 'True',
148  'params_file': params_file,
149  'autostart': autostart,
150  'use_composition': use_composition,
151  'use_respawn': use_respawn,
152  'use_localization': 'False', # Don't use SLAM, AMCL
153  }.items(),
154  )
155 
156  loopback_sim_cmd = IncludeLaunchDescription(
157  PythonLaunchDescriptionSource(
158  os.path.join(loopback_sim_dir, 'loopback_simulation.launch.py')),
159  launch_arguments={
160  'params_file': params_file,
161  'scan_frame_id': 'rplidar_link',
162  }.items(),
163  )
164 
165  static_publisher_cmd = Node(
166  package='tf2_ros',
167  executable='static_transform_publisher',
168  arguments=[
169  '0.0', '0.0', '0.0', '0', '0', '0',
170  'base_footprint', 'base_link']
171  )
172 
173  configured_params = ParameterFile(
174  RewrittenYaml(
175  source_file=params_file,
176  root_key=namespace,
177  param_rewrites={},
178  convert_types=True,
179  ),
180  allow_substs=True,
181  )
182 
183  start_map_server = GroupAction(
184  actions=[
185  SetParameter('use_sim_time', True),
186  Node(
187  package='nav2_map_server',
188  executable='map_server',
189  name='map_server',
190  output='screen',
191  respawn=use_respawn,
192  respawn_delay=2.0,
193  parameters=[configured_params, {'yaml_filename': map_yaml_file}],
194  remappings=remappings,
195  ),
196  Node(
197  package='nav2_lifecycle_manager',
198  executable='lifecycle_manager',
199  name='lifecycle_manager_map_server',
200  output='screen',
201  parameters=[
202  configured_params,
203  {'autostart': autostart}, {'node_names': ['map_server']}],
204  ),
205  ]
206  )
207 
208  # Create the launch description and populate
209  ld = LaunchDescription()
210 
211  # Declare the launch options
212  ld.add_action(declare_namespace_cmd)
213  ld.add_action(declare_use_namespace_cmd)
214  ld.add_action(declare_map_yaml_cmd)
215  ld.add_action(declare_params_file_cmd)
216  ld.add_action(declare_autostart_cmd)
217  ld.add_action(declare_use_composition_cmd)
218 
219  ld.add_action(declare_rviz_config_file_cmd)
220  ld.add_action(declare_use_robot_state_pub_cmd)
221  ld.add_action(declare_use_rviz_cmd)
222  ld.add_action(declare_use_respawn_cmd)
223 
224  # Add the actions to launch all of the navigation nodes
225  ld.add_action(start_robot_state_publisher_cmd)
226  ld.add_action(static_publisher_cmd)
227  ld.add_action(start_map_server)
228  ld.add_action(loopback_sim_cmd)
229  ld.add_action(rviz_cmd)
230  ld.add_action(bringup_cmd)
231 
232  return ld