Nav2 Navigation Stack - rolling  main
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 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 Command, 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  desc_dir = get_package_share_directory('nav2_minimal_tb4_description')
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', 'depot.yaml'), # Try warehouse.yaml!
60  description='Full path to map file to load',
61  )
62 
63  declare_graph_file_cmd = DeclareLaunchArgument(
64  'graph',
65  default_value=os.path.join(bringup_dir, 'graphs', 'depot_graph.geojson'),
66  )
67 
68  declare_params_file_cmd = DeclareLaunchArgument(
69  'params_file',
70  default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
71  description='Full path to the ROS2 parameters file to use for all launched nodes',
72  )
73 
74  declare_autostart_cmd = DeclareLaunchArgument(
75  'autostart',
76  default_value='true',
77  description='Automatically startup the nav2 stack',
78  )
79 
80  declare_use_composition_cmd = DeclareLaunchArgument(
81  'use_composition',
82  default_value='True',
83  description='Whether to use composed bringup',
84  )
85 
86  declare_use_intra_process_comms_cmd = DeclareLaunchArgument(
87  'use_intra_process_comms',
88  default_value='False',
89  description='Whether to use intra process communication',
90  )
91 
92  declare_use_respawn_cmd = DeclareLaunchArgument(
93  'use_respawn',
94  default_value='False',
95  description='Whether to respawn if a node crashes. Applied when composition is disabled.',
96  )
97 
98  declare_rviz_config_file_cmd = DeclareLaunchArgument(
99  'rviz_config_file',
100  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
101  description='Full path to the RVIZ config file to use',
102  )
103 
104  declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
105  'use_robot_state_pub',
106  default_value='True',
107  description='Whether to start the robot state publisher',
108  )
109 
110  declare_use_rviz_cmd = DeclareLaunchArgument(
111  'use_rviz', default_value='True', description='Whether to start RVIZ'
112  )
113 
114  sdf = os.path.join(desc_dir, 'urdf', 'standard', 'turtlebot4.urdf.xacro')
115  start_robot_state_publisher_cmd = Node(
116  condition=IfCondition(use_robot_state_pub),
117  package='robot_state_publisher',
118  executable='robot_state_publisher',
119  name='robot_state_publisher',
120  namespace=namespace,
121  output='screen',
122  parameters=[
123  {'use_sim_time': True, 'robot_description': Command(['xacro', ' ', sdf])}
124  ],
125  remappings=remappings,
126  )
127 
128  rviz_cmd = IncludeLaunchDescription(
129  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'rviz_launch.py')),
130  condition=IfCondition(use_rviz),
131  launch_arguments=[
132  ('namespace', namespace),
133  ('use_sim_time', 'True'),
134  ('rviz_config', rviz_config_file),
135  ],
136  )
137 
138  bringup_cmd = IncludeLaunchDescription(
139  PythonLaunchDescriptionSource(os.path.join(launch_dir, 'bringup_launch.py')),
140  launch_arguments={
141  'namespace': namespace,
142  'map': map_yaml_file,
143  'graph': graph_filepath,
144  'use_sim_time': 'True',
145  'params_file': params_file,
146  'autostart': autostart,
147  'use_composition': use_composition,
148  'use_intra_process_comms': use_intra_process_comms,
149  'use_respawn': use_respawn,
150  'use_keepout_zones': 'False', # Keepout zones not used in loopback simulation
151  'use_speed_zones': 'False', # Speed zones not used in loopback simulation
152  'use_localization': 'False',
153  'serve_static_map': 'True',
154  'container_name': 'nav2_container',
155  }.items(),
156  )
157 
158  loopback_sim_cmd = IncludeLaunchDescription(
159  PythonLaunchDescriptionSource(
160  os.path.join(loopback_sim_dir, 'loopback_simulation.launch.py')),
161  launch_arguments=[
162  ('namespace', namespace),
163  ('params_file', params_file),
164  ('scan_frame_id', 'rplidar_link'),
165  ],
166  )
167 
168  static_publisher_cmd = Node(
169  package='tf2_ros',
170  executable='static_transform_publisher',
171  arguments=[
172  '--x', '0.0', '--y', '0.0', '--z', '0.0',
173  '--roll', '0', '--pitch', '0', '--yaw', '0',
174  '--frame-id', 'base_footprint', '--child-frame-id', 'base_link']
175  )
176 
177  # Create the launch description and populate
178  ld = LaunchDescription()
179 
180  # Declare the launch options
181  ld.add_action(declare_namespace_cmd)
182  ld.add_action(declare_map_yaml_cmd)
183  ld.add_action(declare_graph_file_cmd)
184  ld.add_action(declare_params_file_cmd)
185  ld.add_action(declare_autostart_cmd)
186  ld.add_action(declare_use_composition_cmd)
187  ld.add_action(declare_use_intra_process_comms_cmd)
188 
189  ld.add_action(declare_rviz_config_file_cmd)
190  ld.add_action(declare_use_robot_state_pub_cmd)
191  ld.add_action(declare_use_rviz_cmd)
192  ld.add_action(declare_use_respawn_cmd)
193 
194  # Add the actions to launch all of the navigation nodes
195  ld.add_action(start_robot_state_publisher_cmd)
196  ld.add_action(static_publisher_cmd)
197  ld.add_action(loopback_sim_cmd)
198  ld.add_action(rviz_cmd)
199  ld.add_action(bringup_cmd)
200 
201  return ld