Configure ros_gz_bridge to connect Gazebo topics with ROS 2 for closed-loop control
Think like a robotics systems integrator who connects simulation to control systems. You understand message type mappings, topic naming conventions, and launch file orchestration. You create reliable bridges that enable seamless ROS 2 control of simulated robots.
Before configuring any bridge, ask yourself:
Command-line format:
/TOPIC@ROS_MSG@GZ_MSG
Direction modifiers:
/TOPIC@ROS_MSG@GZ_MSG # Bidirectional (default)
/TOPIC@ROS_MSG[gz.msgs.X # GZ to ROS only
/TOPIC@ROS_MSG]gz.msgs.X # ROS to GZ only
| ROS 2 Type | Gazebo Type | Use Case |
|---|---|---|
geometry_msgs/msg/Twist |
gz.msgs.Twist |
Velocity commands |
sensor_msgs/msg/Image |
gz.msgs.Image |
Camera images |
sensor_msgs/msg/LaserScan |
gz.msgs.LaserScan |
2D LIDAR |
sensor_msgs/msg/PointCloud2 |
gz.msgs.PointCloudPacked |
3D LIDAR |
sensor_msgs/msg/Imu |
gz.msgs.IMU |
IMU data |
std_msgs/msg/Float64 |
gz.msgs.Double |
Scalar values |
geometry_msgs/msg/Pose |
gz.msgs.Pose |
Position/orientation |
# Single topic bridge
ros2 run ros_gz_bridge parameter_bridge \
/cmd_vel@geometry_msgs/msg/Twist]gz.msgs.Twist
# Multiple topics
ros2 run ros_gz_bridge parameter_bridge \
/cmd_vel@geometry_msgs/msg/Twist]gz.msgs.Twist \
/camera/image@sensor_msgs/msg/Image[gz.msgs.Image \
/lidar/scan@sensor_msgs/msg/LaserScan[gz.msgs.LaserScan
For complex setups, use YAML:
# bridge_config.yaml
- ros_topic_name: "/cmd_vel"
gz_topic_name: "/cmd_vel"
ros_type_name: "geometry_msgs/msg/Twist"
gz_type_name: "gz.msgs.Twist"
direction: ROS_TO_GZ
- ros_topic_name: "/camera/image"
gz_topic_name: "/camera/image"
ros_type_name: "sensor_msgs/msg/Image"
gz_type_name: "gz.msgs.Image"
direction: GZ_TO_ROS
- ros_topic_name: "/lidar/scan"
gz_topic_name: "/lidar/scan"
ros_type_name: "sensor_msgs/msg/LaserScan"
gz_type_name: "gz.msgs.LaserScan"
direction: GZ_TO_ROS
Launch with:
ros2 run ros_gz_bridge parameter_bridge --ros-args -p config_file:=bridge_config.yaml
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch_ros.actions import Node
from launch.launch_description_sources import PythonLaunchDescriptionSource
def generate_launch_description():
return LaunchDescription([
# Start Gazebo
IncludeLaunchDescription(
PythonLaunchDescriptionSource([
'gz_sim.launch.py'
]),
launch_arguments={'world': 'my_world.sdf'}.items(),
),
# Spawn robot
Node(
package='ros_gz_sim',
executable='create',
arguments=[
'-topic', '/robot_description',
'-name', 'my_robot',
'-x', '0', '-y', '0', '-z', '0.1'
],
),
# Bridge topics
Node(
package='ros_gz_bridge',
executable='parameter_bridge',
arguments=[
'/cmd_vel@geometry_msgs/msg/Twist]gz.msgs.Twist',
'/camera/image@sensor_msgs/msg/Image[gz.msgs.Image',
],
),
# Robot state publisher
Node(
package='robot_state_publisher',
executable='robot_state_publisher',
parameters=[{'robot_description': robot_description}],
),
])
gz topic -e -t /topicros2 topic list# Check ROS 2 message type
ros2 interface show sensor_msgs/msg/Image
# Check Gazebo message type
gz msg --info gz.msgs.Image
# Velocity command (ROS → Gazebo)
- ros_topic_name: "/cmd_vel"
gz_topic_name: "/model/my_robot/cmd_vel"
ros_type_name: "geometry_msgs/msg/Twist"
gz_type_name: "gz.msgs.Twist"
direction: ROS_TO_GZ
# Odometry (Gazebo → ROS)
- ros_topic_name: "/odom"
gz_topic_name: "/model/my_robot/odometry"
ros_type_name: "nav_msgs/msg/Odometry"
gz_type_name: "gz.msgs.Odometry"
direction: GZ_TO_ROS
# Camera
- ros_topic_name: "/camera/image_raw"
gz_topic_name: "/camera/image"
ros_type_name: "sensor_msgs/msg/Image"
gz_type_name: "gz.msgs.Image"
direction: GZ_TO_ROS
# LIDAR
- ros_topic_name: "/scan"
gz_topic_name: "/lidar/scan"
ros_type_name: "sensor_msgs/msg/LaserScan"
gz_type_name: "gz.msgs.LaserScan"
direction: GZ_TO_ROS
# IMU
- ros_topic_name: "/imu/data"
gz_topic_name: "/imu"
ros_type_name: "sensor_msgs/msg/Imu"
gz_type_name: "gz.msgs.IMU"
direction: GZ_TO_ROS
Before finalizing any bridge configuration:
gz topic -l)ros2 topic list)ros2 topic echo)This skill is used by:
content-implementer agent when generating Module 2 lessonsDependencies:
urdf-robot-model - robots to spawn and controlgazebo-world-builder - worlds to simulate insensor-simulation - sensor data to bridge