Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ros2_launch_gazebo.md with generic scaffolding example and eliminate space ros inclusion #559

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/Imu@gz.msgs.IMU',],
remappings=[('/remapped_imu_topic'),],
output='screen'
),
])
```

Expand Down
Loading