ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I'd like to update this answer for some people, as ROS2 functionality has improved but the documentation has not yet caught up. (To be clear it is March 2021 and I am running ROS 2 Foxy)

If you have a URDF file (note that the syntax for URDF in ROS 2 is slightly different than ROS 1) in your package that you want to spawn into Gazebo, you have to do the following:

1) Launch gazebo (not the standard way, I will elaborate below)
2) Launch the robot state publisher with your URDF file
3) Run thespawn_entity node to spawn your robot into gazebo

Here is how you go about doing it (as individual steps)

1) Create a Launch File for for your robot state publisher, here is an example:

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

def generate_launch_description():

  use_sim_time = LaunchConfiguration('use_sim_time', default='false')
  urdf_file_name = 'urdf/camera_bot.xacro'

  print("urdf_file_name : {}".format(urdf_file_name))

  urdf = os.path.join(
      get_package_share_directory('ros2_sim_pkg'),
      urdf_file_name)

  return LaunchDescription([

        DeclareLaunchArgument(
            'use_sim_time',
            default_value='false',
            description='Use simulation (Gazebo) clock if true'),
        Node(
            package='robot_state_publisher',
            executable='robot_state_publisher',
            name='robot_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time}],
            arguments=[urdf])
  ])

In this example I am launching the robot state publisher with a URDF file called camera_bot.xacro (I am using .xacro because I want to reference other XML files, but a standard .urdf or .xml will work all the same) from a package called ros2_sim_pkg. Change the URDF and package names to fit your project.

2) Build your workspace (colcon build)

3) Source your workspace in the terminals you open (source install/setup.bash)

4) Launch gazebo with ros2 launch gazebo_ros gazebo.launch.py

5) Launch the launch file you just created to configure your robot_state_publisher: ros2 launch ros2_sim_pkg cam_bot_world.launch. Make sure to change the package name and launch file name to match yours.

6) Run the URDF spawner node: ros2 run gazebo_ros spawn_entity.py -topic /robot_description -entity my_cam_bot`

The entity name will be the name of your model within gazebo, so feel free to change "my_cam_bot" to whatever you want. It will automatically spawn at the origin, so if you do not want this, you can check out the other parameters here: https://github.com/ros-simulation/gazebo_ros_pkgs/blob/foxy/gazebo_ros/scripts/spawn_entity.py#L51

And with that you should have gazebo running with your URDF spawned in at the origin. I do not have Karma points so I can not show you a picture of it working unfortunately.

You can feel free to combine all these steps into one big launch file for convenience, but I just wanted to illustrate the process order so it was more understandable.

Hope this helps.