@@ -26,9 +26,10 @@ to learn about the different parameters that are accepted by each launch file.
26
26
27
27
## Using a custom launch file.
28
28
29
+ ### XML
29
30
It's also possible to start Gazebo from your custom launch file. For that
30
- purpose we have created the custom ` <gz_server/> ` tag that can be used from you
31
- XML or YAML launch file. In this case, the arguments are passed as attributes
31
+ purpose we have created the custom ` <gz_server/> ` tag that can be used from your
32
+ XML launch file. In this case, the arguments are passed as attributes
32
33
within this tag. Here's an example for launching Gazebo server:
33
34
34
35
``` xml
@@ -49,3 +50,53 @@ within this tag. Here's an example for launching Gazebo server:
49
50
In this case the ` <gz_server> ` parameters are read from the command line. That's
50
51
an option but not strictly necessary as you could decide to hardcode some of the
51
52
values.
53
+
54
+ ### Python
55
+ Python launch files provide more low-level customization and logic compared to XML launch files.
56
+ In the following example, the user can specify a world argument to launch an environment for
57
+ the Moon, Mars, or Enceladus. It additionally sets the resource path environment variable and
58
+ sets up empty arrays for topics to be bridged and remapped from Gazebo to ROS 2:
59
+ ``` python
60
+ from ament_index_python.packages import get_package_share_directory
61
+ from launch import LaunchDescription
62
+ from launch.actions import (DeclareLaunchArgument, SetEnvironmentVariable,
63
+ IncludeLaunchDescription, SetLaunchConfiguration)
64
+ from launch.substitutions import PathJoinSubstitution, LaunchConfiguration, TextSubstitution
65
+ from launch_ros.actions import Node
66
+ from launch.launch_description_sources import PythonLaunchDescriptionSource
67
+
68
+
69
+ def generate_launch_description ():
70
+ pkg_ros_gz_sim = get_package_share_directory(' ros_gz_sim' )
71
+ pkg_spaceros_gz_sim = get_package_share_directory(' spaceros_gz_sim' )
72
+ gz_launch_path = PathJoinSubstitution([pkg_ros_gz_sim, ' launch' , ' gz_sim.launch.py' ])
73
+ gz_model_path = PathJoinSubstitution([pkg_spaceros_gz_sim, ' models' ])
74
+
75
+ return LaunchDescription([
76
+ DeclareLaunchArgument(
77
+ ' world' ,
78
+ default_value = ' moon' ,
79
+ choices = [' moon' , ' mars' , ' enceladus' ],
80
+ description = ' World to load into Gazebo'
81
+ ),
82
+ SetLaunchConfiguration(name = ' world_file' ,
83
+ value = [LaunchConfiguration(' world' ),
84
+ TextSubstitution(text = ' .sdf' )]),
85
+ SetEnvironmentVariable(' GZ_SIM_RESOURCE_PATH' , gz_model_path),
86
+ IncludeLaunchDescription(
87
+ PythonLaunchDescriptionSource(gz_launch_path),
88
+ launch_arguments = {
89
+ ' gz_args' : [PathJoinSubstitution([pkg_spaceros_gz_sim, ' worlds' ,
90
+ LaunchConfiguration(' world_file' )])],
91
+ ' on_exit_shutdown' : ' True'
92
+ }.items(),
93
+ ),
94
+ Node(
95
+ package = ' ros_gz_bridge' ,
96
+ executable = ' parameter_bridge' ,
97
+ arguments = [],
98
+ remappings = [],
99
+ output = ' screen'
100
+ ),
101
+ ])
102
+ ```
0 commit comments