16 from pathlib
import Path
19 from ament_index_python.packages
import get_package_share_directory
20 from launch
import LaunchDescription
21 from launch.actions
import (AppendEnvironmentVariable, DeclareLaunchArgument, ExecuteProcess,
22 IncludeLaunchDescription, OpaqueFunction, RegisterEventHandler)
23 from launch.conditions
import IfCondition
24 from launch.event_handlers
import OnShutdown
25 from launch.launch_description_sources
import PythonLaunchDescriptionSource
26 from launch.substitutions
import Command, LaunchConfiguration, PythonExpression
27 from launch_ros.actions
import Node
30 def generate_launch_description() -> LaunchDescription:
31 nav2_bringup_dir = get_package_share_directory(
'nav2_bringup')
32 sim_dir = get_package_share_directory(
'nav2_minimal_tb4_sim')
33 desc_dir = get_package_share_directory(
'nav2_minimal_tb4_description')
35 robot_sdf = os.path.join(desc_dir,
'urdf',
'standard',
'turtlebot4.urdf.xacro')
36 world = os.path.join(sim_dir,
'worlds',
'depot.sdf')
37 map_yaml_file = os.path.join(nav2_bringup_dir,
'maps',
'depot.yaml')
40 use_rviz = LaunchConfiguration(
'use_rviz')
41 headless = LaunchConfiguration(
'headless')
44 declare_use_rviz_cmd = DeclareLaunchArgument(
45 'use_rviz', default_value=
'True', description=
'Whether to start RVIZ'
48 declare_simulator_cmd = DeclareLaunchArgument(
49 'headless', default_value=
'False', description=
'Whether to execute gzclient)'
53 world_sdf = tempfile.mktemp(prefix=
'nav2_', suffix=
'.sdf')
54 world_sdf_xacro = ExecuteProcess(
55 cmd=[
'xacro',
'-o', world_sdf, [
'headless:=', headless], world])
56 start_gazebo_server_cmd = IncludeLaunchDescription(
57 PythonLaunchDescriptionSource(
58 os.path.join(get_package_share_directory(
'ros_gz_sim'),
'launch',
60 launch_arguments={
'gz_args': [
'-r -s ', world_sdf]}.items())
62 remove_temp_sdf_file = RegisterEventHandler(event_handler=OnShutdown(
64 OpaqueFunction(function=
lambda _: os.remove(world_sdf))
67 set_env_vars_resources = AppendEnvironmentVariable(
68 'GZ_SIM_RESOURCE_PATH',
69 os.path.join(sim_dir,
'worlds'))
70 start_gazebo_client_cmd = IncludeLaunchDescription(
71 PythonLaunchDescriptionSource(
72 os.path.join(get_package_share_directory(
'ros_gz_sim'),
76 condition=IfCondition(PythonExpression(
78 launch_arguments={
'gz_args': [
'-v4 -g ']}.items(),
81 spawn_robot_cmd = IncludeLaunchDescription(
82 PythonLaunchDescriptionSource(
83 os.path.join(sim_dir,
'launch',
'spawn_tb4.launch.py')),
84 launch_arguments={
'use_sim_time':
'True',
85 'robot_sdf': robot_sdf,
91 'yaw':
'0.0'}.items())
93 start_robot_state_publisher_cmd = Node(
94 package=
'robot_state_publisher',
95 executable=
'robot_state_publisher',
96 name=
'robot_state_publisher',
99 {
'use_sim_time':
True,
'robot_description': Command([
'xacro',
' ', robot_sdf])}
104 rviz_cmd = IncludeLaunchDescription(
105 PythonLaunchDescriptionSource(
106 os.path.join(nav2_bringup_dir,
'launch',
'rviz_launch.py')
108 condition=IfCondition(use_rviz),
109 launch_arguments={
'namespace':
''}.items(),
113 bringup_cmd = IncludeLaunchDescription(
114 PythonLaunchDescriptionSource(
115 os.path.join(nav2_bringup_dir,
'launch',
'bringup_launch.py')
117 launch_arguments={
'map': map_yaml_file}.items(),
122 package=
'nav2_simple_commander',
123 executable=
'example_follow_path',
128 set_env_vars_resources2 = AppendEnvironmentVariable(
129 'GZ_SIM_RESOURCE_PATH',
130 str(Path(os.path.join(desc_dir)).parent.resolve()))
132 ld = LaunchDescription()
133 ld.add_action(declare_use_rviz_cmd)
134 ld.add_action(declare_simulator_cmd)
135 ld.add_action(world_sdf_xacro)
136 ld.add_action(remove_temp_sdf_file)
137 ld.add_action(set_env_vars_resources)
138 ld.add_action(set_env_vars_resources2)
139 ld.add_action(start_gazebo_server_cmd)
140 ld.add_action(start_gazebo_client_cmd)
141 ld.add_action(spawn_robot_cmd)
142 ld.add_action(start_robot_state_publisher_cmd)
143 ld.add_action(rviz_cmd)
144 ld.add_action(bringup_cmd)
145 ld.add_action(demo_cmd)