Nav2 Navigation Stack - rolling  main
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, IncludeLaunchDescription
22 from launch.conditions import IfCondition
23 from launch.launch_description_sources import PythonLaunchDescriptionSource
24 from launch.substitutions import LaunchConfiguration
25 from launch_ros.actions import Node
26 from nav2_common.launch import LaunchConfigAsBool
27 
28 
29 def generate_launch_description() -> LaunchDescription:
30  # Get the launch directory
31  bringup_dir = get_package_share_directory('nav2_bringup')
32  loopback_sim_dir = get_package_share_directory('nav2_loopback_sim')
33  launch_dir = os.path.join(bringup_dir, 'launch')
34  sim_dir = get_package_share_directory('nav2_minimal_tb3_sim')
35 
36  # Create the launch configuration variables
37  namespace = LaunchConfiguration('namespace')
38  map_yaml_file = LaunchConfiguration('map')
39  graph_filepath = LaunchConfiguration('graph')
40  params_file = LaunchConfiguration('params_file')
41  autostart = LaunchConfigAsBool('autostart')
42  use_composition = LaunchConfigAsBool('use_composition')
43  use_intra_process_comms = LaunchConfigAsBool('use_intra_process_comms')
44  use_respawn = LaunchConfigAsBool('use_respawn')
45 
46  # Launch configuration variables specific to simulation
47  rviz_config_file = LaunchConfiguration('rviz_config_file')
48  use_robot_state_pub = LaunchConfigAsBool('use_robot_state_pub')
49  use_rviz = LaunchConfigAsBool('use_rviz')
50  remappings = [('/tf', 'tf'), ('/tf_static', 'tf_static')]
51 
52  # Declare the launch arguments
53  declare_namespace_cmd = DeclareLaunchArgument(
54  'namespace', default_value='', description='Top-level namespace'
55  )
56 
57  declare_map_yaml_cmd = DeclareLaunchArgument(
58  'map',
59  default_value=os.path.join(bringup_dir, 'maps', 'tb3_sandbox.yaml'),
60  )
61 
62  declare_graph_file_cmd = DeclareLaunchArgument(
63  'graph',
64  default_value=os.path.join(bringup_dir, 'graphs', 'turtlebot3_graph.geojson'),
65  )
66 
67  declare_params_file_cmd = DeclareLaunchArgument(
68  'params_file',
69  default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
70  description='Full path to the ROS2 parameters file to use for all launched nodes',
71  )
72 
73  declare_autostart_cmd = DeclareLaunchArgument(
74  'autostart',
75  default_value='true',
76  description='Automatically startup the nav2 stack',
77  )
78 
79  declare_use_composition_cmd = DeclareLaunchArgument(
80  'use_composition',
81  default_value='True',
82  description='Whether to use composed bringup',
83  )
84 
85  declare_use_intra_process_comms_cmd = DeclareLaunchArgument(
86  'use_intra_process_comms',
87  default_value='False',
88  description='Whether to use intra process communication',
89  )
90 
91  declare_use_respawn_cmd = DeclareLaunchArgument(
92  'use_respawn',
93  default_value='False',
94  description='Whether to respawn if a node crashes. Applied when composition is disabled.',
95  )
96 
97  declare_rviz_config_file_cmd = DeclareLaunchArgument(
98  'rviz_config_file',
99  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
100  description='Full path to the RVIZ config file to use',
101  )
102 
103  declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
104  'use_robot_state_pub',
105  default_value='True',
106  description='Whether to start the robot state publisher',
107  )
108 
109  declare_use_rviz_cmd = DeclareLaunchArgument(
110  'use_rviz', default_value='True', description='Whether to start RVIZ'
111  )
112 
113  urdf = os.path.join(sim_dir, 'urdf', 'turtlebot3_waffle.urdf')
114  with open(urdf, 'r') as infp:
115  robot_description = infp.read()
116 
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': robot_description}
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_sim_time', 'True'),
136  ('rviz_config', rviz_config_file),
137  ],
138  )
139 
140  bringup_cmd = IncludeLaunchDescription(
141  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'bringup_launch.py')),
142  launch_arguments={
143  'namespace': namespace,
144  'map': map_yaml_file,
145  'graph': graph_filepath,
146  'use_sim_time': 'True',
147  'params_file': params_file,
148  'autostart': autostart,
149  'use_composition': use_composition,
150  'use_intra_process_comms': use_intra_process_comms,
151  'use_respawn': use_respawn,
152  'use_localization': 'False',
153  'serve_static_map': 'True',
154  'use_keepout_zones': 'False',
155  'use_speed_zones': 'False',
156  'container_name': 'nav2_container',
157  }.items(),
158  )
159 
160  loopback_sim_cmd = IncludeLaunchDescription(
161  PythonLaunchDescriptionSource(
162  os.path.join(loopback_sim_dir, 'loopback_simulation.launch.py')),
163  launch_arguments={
164  'namespace': namespace,
165  'params_file': params_file,
166  }.items(),
167  )
168 
169  # Create the launch description and populate
170  ld = LaunchDescription()
171 
172  # Declare the launch options
173  ld.add_action(declare_namespace_cmd)
174  ld.add_action(declare_map_yaml_cmd)
175  ld.add_action(declare_graph_file_cmd)
176  ld.add_action(declare_params_file_cmd)
177  ld.add_action(declare_autostart_cmd)
178  ld.add_action(declare_use_composition_cmd)
179  ld.add_action(declare_use_intra_process_comms_cmd)
180 
181  ld.add_action(declare_rviz_config_file_cmd)
182  ld.add_action(declare_use_robot_state_pub_cmd)
183  ld.add_action(declare_use_rviz_cmd)
184  ld.add_action(declare_use_respawn_cmd)
185 
186  # Add the actions to launch all of the navigation nodes
187  ld.add_action(start_robot_state_publisher_cmd)
188  ld.add_action(loopback_sim_cmd)
189  ld.add_action(rviz_cmd)
190  ld.add_action(bringup_cmd)
191 
192  return ld