1313# limitations under the License.
1414
1515import os
16+ import tempfile
1617
1718from ament_index_python .packages import get_package_share_directory
18-
1919from launch import LaunchDescription
20- from launch .actions import ExecuteProcess , IncludeLaunchDescription
20+ from launch .actions import ExecuteProcess , IncludeLaunchDescription , OpaqueFunction , RegisterEventHandler
21+ from launch .event_handlers import OnShutdown
2122from launch .launch_description_sources import PythonLaunchDescriptionSource
2223from launch_ros .actions import Node
2324
@@ -26,22 +27,21 @@ def generate_launch_description():
2627 nav2_bringup_dir = get_package_share_directory ('nav2_bringup' )
2728 coverage_demo_dir = get_package_share_directory ('opennav_coverage_demo' )
2829 rviz_config_file = os .path .join (coverage_demo_dir , 'rviz_config.rviz' )
30+ sim_dir = get_package_share_directory ("nav2_minimal_tb3_sim" )
2931
3032 world = os .path .join (coverage_demo_dir , 'blank.world' )
3133 param_file_path = os .path .join (coverage_demo_dir , 'demo_params.yaml' )
32- sdf = os .path .join (nav2_bringup_dir , 'worlds' , 'waffle.model' )
34+ robot_sdf = os .path .join (sim_dir , "urdf" , "gz_waffle.sdf.xacro" )
3335
3436 # start the simulation
35- start_gazebo_server_cmd = ExecuteProcess (
36- cmd = ['gzserver' , '-s' , 'libgazebo_ros_init.so' ,
37- '-s' , 'libgazebo_ros_factory.so' , world ],
38- cwd = [coverage_demo_dir ], output = 'screen' )
39-
40- # start_gazebo_client_cmd = ExecuteProcess(
41- # cmd=['gzclient'],
42- # cwd=[coverage_demo_dir], output='screen')
43-
44- urdf = os .path .join (nav2_bringup_dir , 'urdf' , 'turtlebot3_waffle.urdf' )
37+ world_sdf = tempfile .mktemp (prefix = "nav2_" , suffix = ".sdf" )
38+ world_sdf_xacro = ExecuteProcess (cmd = ["xacro" , "-o" , world_sdf , "headless:=false" , world ])
39+ gazebo_server = ExecuteProcess (
40+ cmd = ["gz" , "sim" , "-r" , "-s" , world_sdf ],
41+ output = "screen" ,
42+ )
43+
44+ urdf = os .path .join (sim_dir , 'urdf' , 'turtlebot3_waffle.urdf' )
4545 with open (urdf , 'r' ) as infp :
4646 robot_description = infp .read ()
4747
@@ -53,15 +53,29 @@ def generate_launch_description():
5353 parameters = [{'use_sim_time' : True ,
5454 'robot_description' : robot_description }])
5555
56- start_gazebo_spawner_cmd = Node (
57- package = 'gazebo_ros' ,
58- executable = 'spawn_entity.py' ,
59- output = 'screen' ,
60- arguments = [
61- '-entity' , 'tb3' ,
62- '-file' , sdf ,
63- '-x' , '6.23' , '-y' , '15.0' , '-z' , '0.10' ,
64- '-R' , '0.0' , '-P' , '0.0' , '-Y' , '-1.5708' ])
56+ remove_temp_sdf_file = RegisterEventHandler (
57+ event_handler = OnShutdown (on_shutdown = [OpaqueFunction (function = lambda _ : os .remove (world_sdf ))])
58+ )
59+
60+ gazebo_client = IncludeLaunchDescription (
61+ PythonLaunchDescriptionSource (
62+ os .path .join (get_package_share_directory ("ros_gz_sim" ), "launch" , "gz_sim.launch.py" )
63+ ),
64+ launch_arguments = {"gz_args" : ["-v4 -g " ]}.items (),
65+ )
66+
67+ start_gazebo_spawner_cmd = IncludeLaunchDescription (
68+ PythonLaunchDescriptionSource (
69+ os .path .join (sim_dir , 'launch' , 'spawn_tb3.launch.py' )),
70+ launch_arguments = {'namespace' : '' ,
71+ 'robot_name' : 'turtlebot3_waffle' ,
72+ 'robot_sdf' : robot_sdf ,
73+ 'x_pose' : str (6.23 ),
74+ 'y_pose' : str (15.0 ),
75+ 'z_pose' : str (0.1 ),
76+ 'roll' : str (0.0 ),
77+ 'pitch' : str (0.0 ),
78+ 'yaw' : str (- 1.5708 )}.items ())
6579
6680 # start the visualization
6781 rviz_cmd = IncludeLaunchDescription (
@@ -80,7 +94,7 @@ def generate_launch_description():
8094 package = 'tf2_ros' ,
8195 executable = 'static_transform_publisher' ,
8296 output = 'screen' ,
83- arguments = ['0 ' , '0 ' , '0' , '0' , '0' , '0' , 'map' , 'odom' ])
97+ arguments = ['6.23 ' , '15 ' , '0' , '0' , '0' , '0' , 'map' , 'odom' ])
8498 fake_gps_cmd = Node (
8599 package = 'tf2_ros' ,
86100 executable = 'static_transform_publisher' ,
@@ -95,10 +109,13 @@ def generate_launch_description():
95109 output = 'screen' )
96110
97111 ld = LaunchDescription ()
98- ld .add_action (start_gazebo_server_cmd )
99- # ld.add_action(start_gazebo_client_cmd)
100- ld .add_action (start_robot_state_publisher_cmd )
112+ ld .add_action (world_sdf_xacro )
113+ ld .add_action (remove_temp_sdf_file )
114+ ld .add_action (gazebo_server )
115+ ld .add_action (gazebo_client )
101116 ld .add_action (start_gazebo_spawner_cmd )
117+
118+ ld .add_action (start_robot_state_publisher_cmd )
102119 ld .add_action (rviz_cmd )
103120 ld .add_action (bringup_cmd )
104121 ld .add_action (fake_localization_cmd )
0 commit comments