Skip to content

Launch Gazebo and spawn robot

ROS / ros eco / launch / python

Launch gazebo and spawn robot from urdf/xacro file or sdf


Harmonic

launch gazebo with custom world gazebo search for worlds using GZ_SIM_RESOURCE_PATH environment

Launch gazebo world

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

WORLD_NAME="my_world.sdf"
ROS_GZ_PKG = "ros_gz_sim"

def generate_launch_description():
    ld = LaunchDescription()


    gz_args = " ".join([
        "-r",
        "-v4",
        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

Launch gazebo and spawn robot

sim.launch.py
import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution, Command
from launch_ros.actions import Node

WORLD = "my_world.sdf"

PKG_DESCRIPTION = "m2wr_description"
PKG_BRINGUP = "robot_loc_bringup"
URDF_XACRO_FILE = "m2wr.xacro"
PKG_GAZEBO = "robot_loc_gazebo"

def generate_launch_description():
    ld = LaunchDescription()


    xacro_file = PathJoinSubstitution([
        get_package_share_directory(PKG_DESCRIPTION),
        'urdf',
        URDF_XACRO_FILE
    ])



    robot_description_config = Command(['xacro ', xacro_file, " prefix:='ns/'"])
    params = {'robot_description': robot_description_config, 'use_sim_time': True}

    gz_args = " ".join([
        "-r",
        "-v4",
        WORLD
    ])

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

    node_robot_state_publisher = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        output='screen',
        parameters=[params]
    )

    spawn_entity = Node(package='ros_gz_sim', executable='create',
                        arguments=['-topic', 'robot_description',
                                   '-name', 'my_bot',
                                   '-z', '0.2'],
                        output='screen')

    ld.add_action(node_robot_state_publisher)
    ld.add_action(gazebo)
    ld.add_action(spawn_entity)
    return ld

Declare GZ_SIM_RESOURCE_PATH

Use dsv to set environment variable

ament dsv

1
2
3
4
5
.
├── CMakeLists.txt
├── hooks
   ├── robot_loc_gazebo.dsv.in
   └── robot_loc_gazebo.sh.in
robot_loc_gazebo.dsv.in
prepend-non-duplicate;GZ_SIM_RESOURCE_PATH;share;@CMAKE_INSTALL_PREFIX@/share/robot_loc_gazebo/worlds
obot_loc_gazebo.sh.in
ament_prepend_unique_value GZ_SIM_RESOURCE_PATH "$AMENT_CURRENT_PREFIX/share/@PROJECT_NAME@/worlds"
CMakeLists.txt
1
2
3
4
ament_environment_hooks("${CMAKE_CURRENT_SOURCE_DIR}/hooks/${PROJECT_NAME}.dsv.in")
ament_environment_hooks("${CMAKE_CURRENT_SOURCE_DIR}/hooks/${PROJECT_NAME}.sh.in")

ament_package()

Gazebo classic

Using ExecuteProcess with gazebo environment variables - Spawn robot (using sdf model)

import os
from launch_ros.actions import Node
from launch import LaunchDescription
from launch.actions import (
    ExecuteProcess,
)


PKG = "cable_sim"
WORLD_NAME = "empty.world"


def generate_launch_description():
    model_env = os.environ.get("GAZEBO_MODEL_PATH", "")
    model_path = "/workspace/models"
    model_path = model_path if model_env == "" else model_path + ":" + model_env

    plugin_env = os.environ.get("GAZEBO_RESOURCE_PATH", "")
    resource_path = "/workspace/worlds"
    resource_path = (
        resource_path if plugin_env == "" else resource_path + ":" + plugin_env
    )

    plugin_env = os.environ.get("GAZEBO_PLUGIN_PATH", "")
    plugin_path = "/workspace/plugins"
    plugin_path = plugin_path if plugin_env == "" else plugin_path + ":" + plugin_env

    gazebo = ExecuteProcess(
        cmd=[
            "gazebo",
            "--verbose",
            "-s",
            "libgazebo_ros_init.so",
            "-s",
            "libgazebo_ros_factory.so",
            "empty.world",
        ],
        output="screen",
        additional_env={
            "GAZEBO_MODEL_PATH": model_path,
            "GAZEBO_RESOURCE_PATH": resource_path,
            "GAZEBO_PLUGIN_PATH": plugin_path,
        },
    )

    spawn_robot = Node(
        package="gazebo_ros",
        executable="spawn_entity.py",
        output="screen",
        arguments=[
            "-file",
            "/workspace/models/simple_box/model.sdf",
            "-entity",
            "robot",
            # optional pose:
            "-x",
            "0.0",
            "-y",
            "0.0",
            "-z",
            "0.2",
            "-R",
            "0.0",
            "-P",
            "0.0",
            "-Y",
            "0.0",
        ],
    )

    return LaunchDescription([gazebo, spawn_robot])