Skip to content

Commit 79016ed

Browse files
david-dorfazeey
andauthored
Update ros2_launch_gazebo.md with a Python launch example and typo fixes/formatting for the XML launch file example. (#482)
Signed-off-by: David Dorf <113081373+david-dorf@users.noreply.github.com> Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent ac943c4 commit 79016ed

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

harmonic/ros2_launch_gazebo.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ to learn about the different parameters that are accepted by each launch file.
2626

2727
## Using a custom launch file.
2828

29+
### XML
2930
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
3233
within this tag. Here's an example for launching Gazebo server:
3334

3435
```xml
@@ -49,3 +50,53 @@ within this tag. Here's an example for launching Gazebo server:
4950
In this case the `<gz_server>` parameters are read from the command line. That's
5051
an option but not strictly necessary as you could decide to hardcode some of the
5152
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

Comments
 (0)