UP | HOME

Xacro, URDF, SDF, Gazebo

Overview

  • This document describes how to build Gazebo worlds and models.
    • A world is the overall simulation scene containing models
    • A model is something that is simulated in the world
  • Gazebo uses the sdf format to describe worlds and models
  • SDF files allow you to describe a robot's links, joints, actuators and sensors
    • Sensors and actuators are implemented in gazebo using plugins
    • Plugins designed to work with ROS are here
    • Plugins are implemented in C++
  • Both SDF and urdf files use XML, so they can interoperate with each other (and with xacro)
  • The <gazebo> tag allows you to associate snippets of SDF with the links and joints in your model
  • When gazebo starts, it loads a world file. The world file can contain models and you can also spawn additional models at runtime

From Xacro to Gazebo

There are three main steps when going from a xacro file to a gazebo file, convert to URDF, convert to SDF, and spawn in Gazebo.

Gazebo-compatible URDF

  1. The main tutorial for understanding how to make a URDF file ready for gazebo is here
  2. A gazebo compatible URDF is a URDF file that can be successfully converted to an SDF file and loaded in Gazebo
  3. Every link should have visual, inertial, and collision components
  4. Every link should have an associated gazebo material
    • This tag is different from the URDF material tag
  5. Typically, the gazebo parts are placed in a separate file and included by the main urdf xacro file
    • You can do this because each <gazebo> tag can be associated with a <link>, <joint> or the overall robot
  6. You can directly spawn urdf files in gazebo. They automatically are converted to sdf files.
    • Start with a xacro file. Convert to a urdf with xacro and then gazebo converts it to an =sdf and loads the sdf
    • When done in a launchfile, you never see the intervening urdf or sdf files. These files are automatically generated

The sections below go through how you can see the individual conversion steps, which is useful when debugging these files.

Xacro to URDF

  1. Use the xacro command and redirect to a urdf file:
    • xacro myrobot.urdf.xacro > myrobot.urdf
    • You can then inspect the URDF to make sure it makes sense
    • Syntax and other errors from the xacro conversion will be easier to track down this way.
    • Does not have the overhead of launching rviz to check
    • xacro error messages can be quite cryptic, especially if there is a non-xml syntax error (such as missing a closing } from ${}. The only remedy I have found is reading carefully and using the text editor to make sure that all braces are matched.
  2. Write a launchfile to run rviz, and call xacro and view your robot

URDF to SDF

  1. Use the gz command to convert URDF files to sdf files for gazebo
    • gz sdf -p myrobot.urdf > myrobot.sdf
  2. Inspect the resulting sdf file. There may be some surprises:
    • By default, all links that are connected by fixed joints are merged into a single link in the sdf.
      • You can apply the <preserveFixedJoint>true</preserveFixedJoint> sdf property to a joint to prevent this merging from happening
    • The <material> tag in a urdf is ignored by gazebo
      • You should apply a gazebo material tag to have a color or texture appear on a link for your URDF.
  3. Think of gazebo tags as "decorating" parts of your urdf
    • With <gazebo reference = "name"> you associate the sdf-specific tags with joints, links, or globally in the urdf file.

Spawning an SDF

  1. It is convenient to run gazebo (either from a launchfile or rosrun gazebo_ros gazebo) and keep it running.
  2. After converting to sdf you can spawn directly in gazebo using rosrun gazebo_ros spawn_model -sdf -file myrobot.sdf (directly from an sdf), or rosrun gazebo_ros spawn_model -urdf -file myrobot.urdf (from a urdf which is converted to sdf first).
    • The -b option causes the spawn_model node to run until it is killed. When it is killed, the model will be deleted from gazebo.
    • Use --help to see all the options for spawn_model
  3. Use the view menu in gazebo to see the moments of inertias and centers of mass
    • Since by default gazebo merges fixed links into a single rigid body, you may see fewer bodies than specified in your urdf.
    • You may need to turn transparency on in the view menu to actually see the inertias (which appear as purple boxes)

Gazebo From a Launch File

  1. To start gazebo from a launchfile, it typically makes sense to use the basic empty_world.launch template from the gazebo_ros package and then spawn a model:

    <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="$(find mypackage)/worlds/myworld.world" />
    <arg name="paused" value="true" />
    </include>
    
    <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model"
         args="-urdf -param robot_description -model my_robot_name -z 0.25" />
    
  2. The URDF file for your robot, in this example, has already been loaded into the robot_description parameter and will then be automatically converted to an sdf by the spawn_urdf/spawn_model node.
  3. Your package.xml should contain an <exec_depend> on gazebo_ros
  4. If you want users of your package to be able to use your gazebo resources, package.xml should

    <gazebo_ros gazebo_model_path="${prefix}/models"
                gazebo_plugin_path="${prefix}/../../lib" 
                gazebo_media_path="${prefix}/models"/>
    
    • This code lets external packages know where to find gazebo resources.
  5. For details see Using roslaunch

Author: Matthew Elwin