Skip to content

Commit

Permalink
Python example to launch gzserver (#509)
Browse files Browse the repository at this point in the history
* Python example to launch gzserver.

Signed-off-by: Carlos Agüero <[email protected]>
  • Loading branch information
caguero committed Sep 13, 2024
1 parent b8c8b50 commit 81fdd19
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
48 changes: 47 additions & 1 deletion harmonic/ros2_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ The package `ros_gz_bridge` contains a launch file named
`ros_gz_bridge.launch.py`. You can use it to start a ROS 2 and Gazebo bridge.
Here's an example:

Note: If you run the bridge as a standalone node with composition enabled (default configuration),
you'll need to create a container first.
```bash
ros2 run rclcpp_components component_container --ros-args -r __node:=ros_gz_container
```

And now, the container will load your bridge with:
```bash
ros2 launch ros_gz_bridge ros_gz_bridge.launch.py name:=ros_gz_bridge config_file:=<path_to_your_YAML_file>
```

## Launching the bridge from a custom launch file.
## Launching the bridge from a custom launch file in XML.

It's also possible to trigger the bridge from your custom launch file. For that
purpose we have created the `<ros_gz_bridge/>` tag that can be used from you
Expand Down Expand Up @@ -104,6 +111,45 @@ In this case the `<ros_gz_bridge>` parameters are read from the command line.
That's an option but not strictly necessary as you could decide to hardcode some
of the values or not even use all the parameters.

## Launching the bridge from a custom launch file in Python.

Here's a simplified example of a Python launch file used to load a bridge from
Python:
```python
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration, TextSubstitution
from ros_gz_bridge.actions import RosGzBridge


def generate_launch_description():

bridge_name = LaunchConfiguration('bridge_name')
config_file = LaunchConfiguration('config_file')

declare_bridge_name_cmd = DeclareLaunchArgument(
'bridge_name', description='Name of ros_gz_bridge node'
)

declare_config_file_cmd = DeclareLaunchArgument(
'config_file', description='YAML config file'
)

# Create the launch description and populate
ld = LaunchDescription([
RosGzBridge(
bridge_name=LaunchConfiguration('bridge_name'),
config_file=LaunchConfiguration('config_file'),
),
])

# Declare the launch options
ld.add_action(declare_bridge_name_cmd)
ld.add_action(declare_config_file_cmd)

return ld
```

## Publish key strokes to ROS

Let's send messages to ROS using the `Key Publisher` an Gazebo plugin.
Expand Down
27 changes: 27 additions & 0 deletions harmonic/ros2_launch_gazebo.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,30 @@ def generate_launch_description():
),
])
```

Here's another example using a higher level action from Python to launch `gzserver`:
```python
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from ros_gz_sim.actions import GzServer


def generate_launch_description():

declare_world_sdf_file_cmd = DeclareLaunchArgument(
'world_sdf_file', default_value='',
description='Path to the SDF world file')

# Create the launch description and populate
ld = LaunchDescription([
GzServer(
world_sdf_file=LaunchConfiguration('world_sdf_file')
),
])

# Declare the launch options
ld.add_action(declare_world_sdf_file_cmd)

return ld
```

0 comments on commit 81fdd19

Please sign in to comment.