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
- 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. Theworld
file can contain models and you can alsospawn
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
- The main tutorial for understanding how to make a URDF file ready for gazebo is here
- A gazebo compatible URDF is a URDF file that can be successfully converted to an SDF file and loaded in Gazebo
- Every link should have visual, inertial, and collision components
- Every link should have an associated
gazebo
material- This tag is different from the
URDF
material tag
- This tag is different from the
- Typically, the
gazebo
parts are placed in a separate file and included by the main urdfxacro
file- You can do this because each
<gazebo>
tag can be associated with a<link>
,<joint>
or the overall robot
- You can do this because each
- You can directly spawn
urdf
files ingazebo
. They automatically are converted tosdf
files.- Start with a
xacro
file. Convert to a urdf withxacro
and thengazebo converts it to an =sdf
and loads the sdf - When done in a launchfile, you never see the intervening
urdf
orsdf
files. These files are automatically generated
- Start with a
The sections below go through how you can see the individual conversion steps, which is useful when debugging these files.
Xacro to URDF
- 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.
- Write a launchfile to run
rviz
, and callxacro
and view your robot
URDF to SDF
- Use the
gz
command to convertURDF
files tosdf
files for gazebogz sdf -p myrobot.urdf > myrobot.sdf
- 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
- You can apply the
- 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.
- You should apply a
- By default, all links that are connected by fixed joints are
merged into a single link in the sdf.
- Think of
gazebo
tags as "decorating" parts of yoururdf
- With
<gazebo reference = "name">
you associate the sdf-specific tags with joints, links, or globally in the urdf file.
- With
Spawning an SDF
- It is convenient to run gazebo (either from a launchfile or
rosrun gazebo_ros gazebo
) and keep it running. - After converting to
sdf
you can spawn directly in gazebo usingrosrun gazebo_ros spawn_model -sdf -file myrobot.sdf
(directly from an sdf), orrosrun gazebo_ros spawn_model -urdf -file myrobot.urdf
(from a urdf which is converted to sdf first).- The
-b
option causes thespawn_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 forspawn_model
- The
- 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)
- Since by default gazebo merges fixed links into a single rigid body, you may see fewer bodies
than specified in your
Gazebo From a Launch File
To start gazebo from a launchfile, it typically makes sense to use the basic
empty_world.launch
template from thegazebo_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" />
- 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 ansdf
by thespawn_urdf/spawn_model
node. - Your
package.xml
should contain an<exec_depend>
ongazebo_ros
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.
- For details see Using roslaunch