15 """This is all-in-one launch script intended for use by nav2 developers."""
20 from ament_index_python.packages
import get_package_share_directory
21 from launch
import LaunchDescription
22 from launch.actions
import (DeclareLaunchArgument, ExecuteProcess, IncludeLaunchDescription,
23 OpaqueFunction, RegisterEventHandler)
24 from launch.conditions
import IfCondition
25 from launch.event_handlers
import OnShutdown
26 from launch.launch_description_sources
import PythonLaunchDescriptionSource
27 from launch.substitutions
import LaunchConfiguration, PythonExpression
28 from launch_ros.actions
import Node
32 def generate_launch_description() -> LaunchDescription:
34 bringup_dir = get_package_share_directory(
'nav2_bringup')
35 launch_dir = os.path.join(bringup_dir,
'launch')
36 sim_dir = get_package_share_directory(
'nav2_minimal_tb3_sim')
39 slam = LaunchConfigAsBool(
'slam')
40 namespace = LaunchConfiguration(
'namespace')
41 map_yaml_file = LaunchConfiguration(
'map')
42 graph_filepath = LaunchConfiguration(
'graph')
43 use_sim_time = LaunchConfigAsBool(
'use_sim_time')
44 params_file = LaunchConfiguration(
'params_file')
45 autostart = LaunchConfiguration(
'autostart')
46 use_composition = LaunchConfigAsBool(
'use_composition')
47 use_intra_process_comms = LaunchConfigAsBool(
'use_intra_process_comms')
48 use_respawn = LaunchConfigAsBool(
'use_respawn')
51 rviz_config_file = LaunchConfiguration(
'rviz_config_file')
52 use_simulator = LaunchConfigAsBool(
'use_simulator')
53 use_robot_state_pub = LaunchConfigAsBool(
'use_robot_state_pub')
54 use_rviz = LaunchConfigAsBool(
'use_rviz')
55 headless = LaunchConfigAsBool(
'headless')
56 world = LaunchConfiguration(
'world')
58 'x': LaunchConfiguration(
'x_pose', default=
'-2.00'),
59 'y': LaunchConfiguration(
'y_pose', default=
'-0.50'),
60 'z': LaunchConfiguration(
'z_pose', default=
'0.01'),
61 'R': LaunchConfiguration(
'roll', default=
'0.00'),
62 'P': LaunchConfiguration(
'pitch', default=
'0.00'),
63 'Y': LaunchConfiguration(
'yaw', default=
'0.00'),
65 robot_name = LaunchConfiguration(
'robot_name')
66 robot_sdf = LaunchConfiguration(
'robot_sdf')
68 remappings = [(
'/tf',
'tf'), (
'/tf_static',
'tf_static')]
71 declare_namespace_cmd = DeclareLaunchArgument(
72 'namespace', default_value=
'', description=
'Top-level namespace'
75 declare_slam_cmd = DeclareLaunchArgument(
76 'slam', default_value=
'False', description=
'Whether run a SLAM'
79 declare_map_yaml_cmd = DeclareLaunchArgument(
81 default_value=os.path.join(bringup_dir,
'maps',
'tb3_sandbox.yaml'),
84 declare_graph_file_cmd = DeclareLaunchArgument(
86 default_value=os.path.join(bringup_dir,
'graphs',
'turtlebot3_graph.geojson'),
89 declare_use_sim_time_cmd = DeclareLaunchArgument(
92 description=
'Use simulation (Gazebo) clock if true',
95 declare_params_file_cmd = DeclareLaunchArgument(
97 default_value=os.path.join(bringup_dir,
'params',
'nav2_params.yaml'),
98 description=
'Full path to the ROS2 parameters file to use for all launched nodes',
101 declare_autostart_cmd = DeclareLaunchArgument(
103 default_value=
'true',
104 description=
'Automatically startup the nav2 stack',
107 declare_use_composition_cmd = DeclareLaunchArgument(
109 default_value=
'True',
110 description=
'Whether to use composed bringup',
113 declare_use_intra_process_comms_cmd = DeclareLaunchArgument(
114 'use_intra_process_comms',
115 default_value=
'False',
116 description=
'Whether to use intra process communication',
119 declare_use_respawn_cmd = DeclareLaunchArgument(
121 default_value=
'False',
122 description=
'Whether to respawn if a node crashes. Applied when composition is disabled.',
125 declare_rviz_config_file_cmd = DeclareLaunchArgument(
127 default_value=os.path.join(bringup_dir,
'rviz',
'nav2_default_view.rviz'),
128 description=
'Full path to the RVIZ config file to use',
131 declare_use_simulator_cmd = DeclareLaunchArgument(
133 default_value=
'True',
134 description=
'Whether to start the simulator',
137 declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
138 'use_robot_state_pub',
139 default_value=
'True',
140 description=
'Whether to start the robot state publisher',
143 declare_use_rviz_cmd = DeclareLaunchArgument(
144 'use_rviz', default_value=
'True', description=
'Whether to start RVIZ'
147 declare_simulator_cmd = DeclareLaunchArgument(
148 'headless', default_value=
'True', description=
'Whether to execute gzclient)'
151 declare_world_cmd = DeclareLaunchArgument(
153 default_value=os.path.join(sim_dir,
'worlds',
'tb3_sandbox.sdf.xacro'),
154 description=
'Full path to world model file to load',
157 declare_robot_name_cmd = DeclareLaunchArgument(
158 'robot_name', default_value=
'turtlebot3_waffle', description=
'name of the robot'
161 declare_robot_sdf_cmd = DeclareLaunchArgument(
163 default_value=os.path.join(sim_dir,
'urdf',
'gz_waffle.sdf.xacro'),
164 description=
'Full path to robot sdf file to spawn the robot in gazebo',
167 urdf = os.path.join(sim_dir,
'urdf',
'turtlebot3_waffle.urdf')
168 with open(urdf,
'r')
as infp:
169 robot_description = infp.read()
171 start_robot_state_publisher_cmd = Node(
172 condition=IfCondition(use_robot_state_pub),
173 package=
'robot_state_publisher',
174 executable=
'robot_state_publisher',
175 name=
'robot_state_publisher',
179 {
'use_sim_time': use_sim_time,
'robot_description': robot_description}
181 remappings=remappings,
184 rviz_cmd = IncludeLaunchDescription(
185 PythonLaunchDescriptionSource(os.path.join(launch_dir,
'rviz_launch.py')),
186 condition=IfCondition(use_rviz),
188 'namespace': namespace,
189 'use_sim_time': use_sim_time,
190 'rviz_config': rviz_config_file,
194 bringup_cmd = IncludeLaunchDescription(
195 PythonLaunchDescriptionSource(os.path.join(launch_dir,
'bringup_launch.py')),
197 'namespace': namespace,
199 'map': map_yaml_file,
200 'graph': graph_filepath,
201 'use_sim_time': use_sim_time,
202 'params_file': params_file,
203 'autostart': autostart,
204 'use_composition': use_composition,
205 'use_intra_process_comms': use_intra_process_comms,
206 'use_respawn': use_respawn,
207 'use_keepout_zones':
'False',
208 'use_speed_zones':
'False',
209 'container_name':
'nav2_container',
217 world_sdf = tempfile.mktemp(prefix=
'nav2_', suffix=
'.sdf')
218 world_sdf_xacro = ExecuteProcess(
219 cmd=[
'xacro',
'-o', world_sdf, [
'headless:=', headless], world])
220 gazebo_server = ExecuteProcess(
221 cmd=[
'gz',
'sim',
'-r',
'-s', world_sdf],
223 condition=IfCondition(use_simulator)
226 remove_temp_sdf_file = RegisterEventHandler(event_handler=OnShutdown(
228 OpaqueFunction(function=
lambda _: os.remove(world_sdf))
231 gazebo_client = IncludeLaunchDescription(
232 PythonLaunchDescriptionSource(
233 os.path.join(get_package_share_directory(
'ros_gz_sim'),
237 condition=IfCondition(PythonExpression(
238 [use_simulator,
' and not ', headless])),
239 launch_arguments={
'gz_args': [
'-v4 -g ']}.items(),
242 gz_robot = IncludeLaunchDescription(
243 PythonLaunchDescriptionSource(
244 os.path.join(sim_dir,
'launch',
'spawn_tb3.launch.py')),
245 launch_arguments={
'namespace': namespace,
246 'use_sim_time': use_sim_time,
247 'robot_name': robot_name,
248 'robot_sdf': robot_sdf,
254 'yaw': pose[
'Y']}.items())
257 ld = LaunchDescription()
260 ld.add_action(declare_namespace_cmd)
261 ld.add_action(declare_slam_cmd)
262 ld.add_action(declare_map_yaml_cmd)
263 ld.add_action(declare_graph_file_cmd)
264 ld.add_action(declare_use_sim_time_cmd)
265 ld.add_action(declare_params_file_cmd)
266 ld.add_action(declare_autostart_cmd)
267 ld.add_action(declare_use_composition_cmd)
268 ld.add_action(declare_use_intra_process_comms_cmd)
270 ld.add_action(declare_rviz_config_file_cmd)
271 ld.add_action(declare_use_simulator_cmd)
272 ld.add_action(declare_use_robot_state_pub_cmd)
273 ld.add_action(declare_use_rviz_cmd)
274 ld.add_action(declare_simulator_cmd)
275 ld.add_action(declare_world_cmd)
276 ld.add_action(declare_robot_name_cmd)
277 ld.add_action(declare_robot_sdf_cmd)
278 ld.add_action(declare_use_respawn_cmd)
280 ld.add_action(world_sdf_xacro)
281 ld.add_action(remove_temp_sdf_file)
282 ld.add_action(gz_robot)
283 ld.add_action(gazebo_server)
284 ld.add_action(gazebo_client)
287 ld.add_action(start_robot_state_publisher_cmd)
288 ld.add_action(rviz_cmd)
289 ld.add_action(bringup_cmd)