Skip to content

Commit

Permalink
Update ros2_launch_gazebo.md (#559)
Browse files Browse the repository at this point in the history
Signed-off-by: David Dorf <[email protected]>
  • Loading branch information
david-dorf authored Jan 8, 2025
1 parent e9f3504 commit ccfda52
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions harmonic/ros2_launch_gazebo.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,48 @@ an option but not strictly necessary as you could decide to hardcode some of the
values or not even use all the parameters.

### Python
Python launch files provide more low-level customization and logic compared to XML launch files.
In the following example, the user can specify a world argument to launch an environment for
the Moon, Mars, or Enceladus. It additionally sets the resource path environment variable and
sets up empty arrays for topics to be bridged and remapped from Gazebo to ROS 2:
Python launch files provide more low-level customization and logic compared to XML launch files. For example, you can set environment variables and include Python specific functions and logic.
In the following example, the user can replace the example package, world, and bridged topic with their own. This is intended as a scaffolding more than something that can be run on its own.

```python
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import (DeclareLaunchArgument, SetEnvironmentVariable,
IncludeLaunchDescription, SetLaunchConfiguration)
from launch.substitutions import PathJoinSubstitution, LaunchConfiguration, TextSubstitution
from launch_ros.actions import Node
from launch.actions import SetEnvironmentVariable, IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare


def generate_launch_description():
pkg_ros_gz_sim = get_package_share_directory('ros_gz_sim')
pkg_spaceros_gz_sim = get_package_share_directory('spaceros_gz_sim')
ros_gz_sim_pkg_path = get_package_share_directory('ros_gz_sim')
example_pkg_path = FindPackageShare('example_package') # Replace with your own package name
gz_launch_path = PathJoinSubstitution([pkg_ros_gz_sim, 'launch', 'gz_sim.launch.py'])
gz_model_path = PathJoinSubstitution([pkg_spaceros_gz_sim, 'models'])

return LaunchDescription([
DeclareLaunchArgument(
'world',
default_value='moon',
choices=['moon', 'mars', 'enceladus'],
description='World to load into Gazebo'
SetEnvironmentVariable(
'GZ_SIM_RESOURCE_PATH',
PathJoinSubstitution([example_pkg_path, 'models'])
),
SetEnvironmentVariable(
'GZ_SIM_PLUGIN_PATH',
PathJoinSubstitution([example_pkg_path, 'plugins'])
),
SetLaunchConfiguration(name='world_file',
value=[LaunchConfiguration('world'),
TextSubstitution(text='.sdf')]),
SetEnvironmentVariable('GZ_SIM_RESOURCE_PATH', gz_model_path),
IncludeLaunchDescription(
PythonLaunchDescriptionSource(gz_launch_path),
launch_arguments={
'gz_args': [PathJoinSubstitution([pkg_spaceros_gz_sim, 'worlds',
LaunchConfiguration('world_file')])],
'gz_args': [PathJoinSubstitution([example_pkg_path, 'worlds/example_world.sdf'])], # Replace with your own world file
'on_exit_shutdown': 'True'
}.items(),
),

# Bridging and remapping Gazebo topics to ROS 2 (replace with your own topics)
Node(
package='ros_gz_bridge',
executable='parameter_bridge',
arguments=['/example_imu_topic@sensor_msgs/msg/[email protected]',],
remappings=[('/remapped_imu_topic'),],
output='screen'
),
])
```

Expand Down

0 comments on commit ccfda52

Please sign in to comment.