Skip to content

ROS2 Gazebo harmonic integration

Variable Desc
GZ_SIM_RESOURCE_PATH Where gz sim looks for models, worlds, textures, media.
GZ_SIM_PLUGIN_PATH Where gz sim looks for shared libraries (.so) for system plugins, sensors, etc.

Tip

Check dsv hook to set the variable

gazebo project

Launch

YAML

launch:
  - arg:
      name: "bridge_config_file"
      default: "$(find-pkg-share bumperbot_bringup)/config/bridge.yaml"

    #gazebo simulation
  - executable:
      cmd: gz sim -v 4 -r world.sdf

    #ros-gz bridge
  - node:
      pkg: "ros_gz_bridge"
      exec: "parameter_bridge"
      output: screen
      args:
          "--ros-args -p config_file:=$(var bridge_config_file)"

Python

gazebo.launch.py
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription, TimerAction
from launch.launch_description_sources import PythonLaunchDescriptionSource

WORLD_NAME="world.sdf"
ROS_GZ_PKG = "ros_gz_sim"

def generate_launch_description():
    ld = LaunchDescription()


    gz_args = " ".join([
        "-r",
        "-v1",
        WORLD_NAME
    ])

    gazebo = IncludeLaunchDescription(
                PythonLaunchDescriptionSource([os.path.join(
                    get_package_share_directory(ROS_GZ_PKG), 'launch', 'gz_sim.launch.py')]),
                    launch_arguments={
                        'gz_args': gz_args,
                        "on_exit_shutdown": "true"}.items()
             )

    ld.add_action(gazebo)

    return ld

Bridge

Bridge clock from gazebo

/clock topic
ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock

Using launch file

launch/bridge.launch.py
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
from pathlib import Path

PKG_BRINGUP = 'robot_loc_bringup'
BRIDGE_CONFIG = "bridge.yaml"
CONFIG_FOLDER = "config"

def generate_launch_description():
    ld = LaunchDescription()



    bridge_file = Path(get_package_share_directory(PKG_BRINGUP)) \
        .joinpath(CONFIG_FOLDER) \
        .joinpath(BRIDGE_CONFIG) \
        .as_posix()

    ros_gz_bridge = Node(
        package="ros_gz_bridge",
        executable="parameter_bridge",
        arguments=[
            '--ros-args',
            '-p',
            f"config_file:={bridge_file}"
        ],
        parameters=[
            {'use_sim_time': True},
            {'qos_overrides./imu.publisher.reliability': 'best_effort'}
        ],
    )

    ld.add_action(ros_gz_bridge)


    return ld
config/bridge.yaml
1
2
3
4
5
- ros_topic_name: "/clock"
  gz_topic_name: "/clock"
  ros_type_name: "rosgraph_msgs/msg/Clock"
  gz_type_name: "gz.msgs.Clock"
  direction: GZ_TO_ROS