|
18 | 18 |
|
19 | 19 | from ament_index_python.packages import get_package_share_directory |
20 | 20 | from launch import LaunchDescription |
21 | | -from launch.actions import (AppendEnvironmentVariable, DeclareLaunchArgument, ExecuteProcess, |
22 | | - IncludeLaunchDescription, OpaqueFunction, RegisterEventHandler, EmitEvent) |
| 21 | +from launch.actions import ( |
| 22 | + AppendEnvironmentVariable, |
| 23 | + DeclareLaunchArgument, |
| 24 | + ExecuteProcess, |
| 25 | + IncludeLaunchDescription, |
| 26 | + OpaqueFunction, |
| 27 | + RegisterEventHandler, |
| 28 | + EmitEvent, |
| 29 | +) |
23 | 30 | from launch.conditions import IfCondition |
24 | 31 | from launch.event_handlers import OnProcessExit, OnShutdown |
25 | 32 | from launch.events import Shutdown |
26 | 33 | from launch.launch_description_sources import PythonLaunchDescriptionSource |
27 | 34 | from launch.substitutions import Command, LaunchConfiguration, PythonExpression |
28 | 35 | from launch_ros.actions import Node |
| 36 | +from launch.substitutions import PathJoinSubstitution |
29 | 37 |
|
30 | 38 |
|
31 | 39 | def generate_launch_description(): |
32 | | - nav2_bringup_dir = get_package_share_directory('nav2_bringup') |
| 40 | + # Launch configuration variables |
| 41 | + world_file_name = LaunchConfiguration("world_file") |
| 42 | + use_rviz = LaunchConfiguration("use_rviz") |
| 43 | + headless = LaunchConfiguration("headless") |
| 44 | + |
| 45 | + nav2_bringup_dir = get_package_share_directory("nav2_bringup") |
33 | 46 | sim_dir = get_package_share_directory('nav2_minimal_tb4_sim') |
34 | | - desc_dir = get_package_share_directory('nav2_minimal_tb4_description') |
| 47 | + desc_dir = get_package_share_directory("nav2_minimal_tb4_description") |
| 48 | + test_pkg_dir = get_package_share_directory('sam_bot_nav2_gz') |
35 | 49 |
|
36 | | - robot_sdf = os.path.join(desc_dir, 'urdf', 'standard', 'turtlebot4.urdf.xacro') |
37 | | - world = os.path.join(sim_dir, 'worlds', 'depot.sdf') |
38 | | - map_yaml_file = os.path.join(nav2_bringup_dir, 'maps', 'depot.yaml') |
| 50 | + robot_sdf = os.path.join(desc_dir, "urdf", "standard", "turtlebot4.urdf.xacro") |
| 51 | + world = PathJoinSubstitution([test_pkg_dir, "worlds", world_file_name]) |
| 52 | + map_yaml_file = os.path.join(nav2_bringup_dir, "maps", "depot.yaml") |
39 | 53 |
|
40 | | - # Launch configuration variables |
41 | | - use_rviz = LaunchConfiguration('use_rviz') |
42 | | - headless = LaunchConfiguration('headless') |
43 | 54 |
|
44 | 55 | # Declare the launch arguments |
45 | 56 | declare_use_rviz_cmd = DeclareLaunchArgument( |
46 | | - 'use_rviz', default_value='True', description='Whether to start RVIZ' |
| 57 | + "use_rviz", default_value="True", description="Whether to start RVIZ" |
47 | 58 | ) |
48 | 59 |
|
49 | 60 | declare_simulator_cmd = DeclareLaunchArgument( |
50 | | - 'headless', default_value='False', description='Whether to execute gzclient)' |
| 61 | + "headless", default_value="False", description="Whether to execute gzclient)" |
| 62 | + ) |
| 63 | + |
| 64 | + declare_world_file_cmd = DeclareLaunchArgument( |
| 65 | + name="world_file", |
| 66 | + default_value="depot.sdf", |
| 67 | + description="Name of the world file to load", |
51 | 68 | ) |
52 | 69 |
|
53 | 70 | # start the simulation |
54 | | - world_sdf = tempfile.mktemp(prefix='nav2_', suffix='.sdf') |
| 71 | + world_sdf = tempfile.mktemp(prefix="nav2_", suffix=".sdf") |
| 72 | + print( |
| 73 | + f"Temporary world file created at {world_sdf}. This will be removed on shutdown." |
| 74 | + ) |
55 | 75 | world_sdf_xacro = ExecuteProcess( |
56 | | - cmd=['xacro', '-o', world_sdf, ['headless:=', headless], world]) |
| 76 | + cmd=["xacro", "-o", world_sdf, ["headless:=", headless], world] |
| 77 | + ) |
57 | 78 | start_gazebo_server_cmd = IncludeLaunchDescription( |
58 | 79 | PythonLaunchDescriptionSource( |
59 | | - os.path.join(get_package_share_directory('ros_gz_sim'), 'launch', |
60 | | - 'gz_sim.launch.py')), |
61 | | - launch_arguments={'gz_args': ['-r -s ', world_sdf]}.items()) |
| 80 | + os.path.join( |
| 81 | + get_package_share_directory("ros_gz_sim"), "launch", "gz_sim.launch.py" |
| 82 | + ) |
| 83 | + ), |
| 84 | + launch_arguments={"gz_args": ["-r -s ", world_sdf]}.items(), |
| 85 | + ) |
62 | 86 |
|
63 | | - remove_temp_sdf_file = RegisterEventHandler(event_handler=OnShutdown( |
64 | | - on_shutdown=[ |
65 | | - OpaqueFunction(function=lambda _: os.remove(world_sdf)) |
66 | | - ])) |
| 87 | + remove_temp_sdf_file = RegisterEventHandler( |
| 88 | + event_handler=OnShutdown( |
| 89 | + on_shutdown=[OpaqueFunction(function=lambda _: os.remove(world_sdf))] |
| 90 | + ) |
| 91 | + ) |
67 | 92 |
|
68 | 93 | set_env_vars_resources = AppendEnvironmentVariable( |
69 | | - 'GZ_SIM_RESOURCE_PATH', |
70 | | - os.path.join(sim_dir, 'worlds')) |
| 94 | + "GZ_SIM_RESOURCE_PATH", os.path.join(sim_dir, "worlds") |
| 95 | + ) |
71 | 96 | start_gazebo_client_cmd = IncludeLaunchDescription( |
72 | 97 | PythonLaunchDescriptionSource( |
73 | | - os.path.join(get_package_share_directory('ros_gz_sim'), |
74 | | - 'launch', |
75 | | - 'gz_sim.launch.py') |
| 98 | + os.path.join( |
| 99 | + get_package_share_directory("ros_gz_sim"), "launch", "gz_sim.launch.py" |
| 100 | + ) |
76 | 101 | ), |
77 | | - condition=IfCondition(PythonExpression( |
78 | | - ['not ', headless])), |
79 | | - launch_arguments={'gz_args': ['-v4 -g ']}.items(), |
| 102 | + condition=IfCondition(PythonExpression(["not ", headless])), |
| 103 | + launch_arguments={"gz_args": ["-v4 -g "]}.items(), |
80 | 104 | ) |
81 | 105 |
|
82 | 106 | spawn_robot_cmd = IncludeLaunchDescription( |
83 | 107 | PythonLaunchDescriptionSource( |
84 | | - os.path.join(sim_dir, 'launch', 'spawn_tb4.launch.py')), |
85 | | - launch_arguments={'use_sim_time': 'True', |
86 | | - 'robot_sdf': robot_sdf, |
87 | | - 'x_pose': '-8.0', |
88 | | - 'y_pose': '0.0', |
89 | | - 'z_pose': '0.0', |
90 | | - 'roll': '0.0', |
91 | | - 'pitch': '0.0', |
92 | | - 'yaw': '0.0'}.items()) |
| 108 | + os.path.join(sim_dir, "launch", "spawn_tb4.launch.py") |
| 109 | + ), |
| 110 | + launch_arguments={ |
| 111 | + "use_sim_time": "True", |
| 112 | + "robot_sdf": robot_sdf, |
| 113 | + "x_pose": "-8.0", |
| 114 | + "y_pose": "0.0", |
| 115 | + "z_pose": "0.0", |
| 116 | + "roll": "0.0", |
| 117 | + "pitch": "0.0", |
| 118 | + "yaw": "0.0", |
| 119 | + }.items(), |
| 120 | + ) |
93 | 121 |
|
94 | 122 | start_robot_state_publisher_cmd = Node( |
95 | | - package='robot_state_publisher', |
96 | | - executable='robot_state_publisher', |
97 | | - name='robot_state_publisher', |
98 | | - output='screen', |
| 123 | + package="robot_state_publisher", |
| 124 | + executable="robot_state_publisher", |
| 125 | + name="robot_state_publisher", |
| 126 | + output="screen", |
99 | 127 | parameters=[ |
100 | | - {'use_sim_time': True, 'robot_description': Command(['xacro', ' ', robot_sdf])} |
101 | | - ] |
| 128 | + { |
| 129 | + "use_sim_time": True, |
| 130 | + "robot_description": Command(["xacro", " ", robot_sdf]), |
| 131 | + } |
| 132 | + ], |
102 | 133 | ) |
103 | 134 |
|
104 | 135 | # start the visualization |
105 | 136 | rviz_cmd = IncludeLaunchDescription( |
106 | 137 | PythonLaunchDescriptionSource( |
107 | | - os.path.join(nav2_bringup_dir, 'launch', 'rviz_launch.py') |
| 138 | + os.path.join(nav2_bringup_dir, "launch", "rviz_launch.py") |
108 | 139 | ), |
109 | 140 | condition=IfCondition(use_rviz), |
110 | | - launch_arguments={'namespace': ''}.items(), |
| 141 | + launch_arguments={"namespace": ""}.items(), |
111 | 142 | ) |
112 | 143 |
|
113 | 144 | # start navigation |
114 | 145 | bringup_cmd = IncludeLaunchDescription( |
115 | 146 | PythonLaunchDescriptionSource( |
116 | | - os.path.join(nav2_bringup_dir, 'launch', 'bringup_launch.py') |
| 147 | + os.path.join(nav2_bringup_dir, "launch", "bringup_launch.py") |
117 | 148 | ), |
118 | | - launch_arguments={'map': map_yaml_file}.items(), |
| 149 | + launch_arguments={"map": map_yaml_file}.items(), |
119 | 150 | ) |
120 | 151 |
|
121 | 152 | # start the demo autonomy task |
122 | 153 | demo_cmd = Node( |
123 | | - package='sam_bot_nav2_gz', |
124 | | - executable='example_waypoint_follower.py', |
| 154 | + package="sam_bot_nav2_gz", |
| 155 | + executable="example_waypoint_follower.py", |
125 | 156 | emulate_tty=True, |
126 | | - output='screen', |
| 157 | + output="screen", |
127 | 158 | ) |
128 | | - |
| 159 | + |
129 | 160 | # Register an event handler to shutdown everything when the demo node exits |
130 | 161 | shutdown_on_demo_exit = RegisterEventHandler( |
131 | 162 | OnProcessExit( |
132 | 163 | target_action=demo_cmd, |
133 | | - on_exit=[EmitEvent(event=Shutdown(reason='Waypoint follower demo completed'))] |
| 164 | + on_exit=[ |
| 165 | + EmitEvent(event=Shutdown(reason="Waypoint follower demo completed")) |
| 166 | + ], |
134 | 167 | ) |
135 | 168 | ) |
136 | 169 |
|
137 | 170 | set_env_vars_resources2 = AppendEnvironmentVariable( |
138 | | - 'GZ_SIM_RESOURCE_PATH', |
139 | | - str(Path(os.path.join(desc_dir)).parent.resolve())) |
| 171 | + "GZ_SIM_RESOURCE_PATH", str(Path(os.path.join(desc_dir)).parent.resolve()) |
| 172 | + ) |
140 | 173 |
|
141 | 174 | ld = LaunchDescription() |
142 | 175 | ld.add_action(declare_use_rviz_cmd) |
143 | 176 | ld.add_action(declare_simulator_cmd) |
| 177 | + ld.add_action(declare_world_file_cmd) |
144 | 178 | ld.add_action(world_sdf_xacro) |
145 | 179 | ld.add_action(remove_temp_sdf_file) |
146 | 180 | ld.add_action(set_env_vars_resources) |
|
0 commit comments