13
13
# limitations under the License.
14
14
15
15
import os
16
+ import tempfile
16
17
17
18
from ament_index_python .packages import get_package_share_directory
18
-
19
19
from 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
21
22
from launch .launch_description_sources import PythonLaunchDescriptionSource
22
23
from launch_ros .actions import Node
23
24
@@ -26,22 +27,21 @@ def generate_launch_description():
26
27
nav2_bringup_dir = get_package_share_directory ('nav2_bringup' )
27
28
coverage_demo_dir = get_package_share_directory ('opennav_coverage_demo' )
28
29
rviz_config_file = os .path .join (coverage_demo_dir , 'rviz_config.rviz' )
30
+ sim_dir = get_package_share_directory ("nav2_minimal_tb3_sim" )
29
31
30
32
world = os .path .join (coverage_demo_dir , 'blank.world' )
31
33
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" )
33
35
34
36
# 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' )
45
45
with open (urdf , 'r' ) as infp :
46
46
robot_description = infp .read ()
47
47
@@ -53,15 +53,29 @@ def generate_launch_description():
53
53
parameters = [{'use_sim_time' : True ,
54
54
'robot_description' : robot_description }])
55
55
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' , '5.0' , '-y' , '5.0' , '-z' , '0.10' ,
64
- '-R' , '0.0' , '-P' , '0.0' , '-Y' , '0.0' ])
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 (5.0 ),
74
+ 'y_pose' : str (5.0 ),
75
+ 'z_pose' : str (0.1 ),
76
+ 'roll' : str (0.0 ),
77
+ 'pitch' : str (0.0 ),
78
+ 'yaw' : str (0.0 )}.items ())
65
79
66
80
# start the visualization
67
81
rviz_cmd = IncludeLaunchDescription (
@@ -90,10 +104,13 @@ def generate_launch_description():
90
104
output = 'screen' )
91
105
92
106
ld = LaunchDescription ()
93
- ld .add_action (start_gazebo_server_cmd )
94
- # ld.add_action(start_gazebo_client_cmd)
95
- ld .add_action (start_robot_state_publisher_cmd )
107
+ ld .add_action (world_sdf_xacro )
108
+ ld .add_action (remove_temp_sdf_file )
109
+ ld .add_action (gazebo_server )
110
+ ld .add_action (gazebo_client )
96
111
ld .add_action (start_gazebo_spawner_cmd )
112
+
113
+ ld .add_action (start_robot_state_publisher_cmd )
97
114
ld .add_action (rviz_cmd )
98
115
ld .add_action (bringup_cmd )
99
116
ld .add_action (fake_localization_cmd )
0 commit comments