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
SDFwith the links and joints in your model - When gazebo starts, it loads a
worldfile. Theworldfile can contain models and you can alsospawnadditional 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
gazebomaterial- This tag is different from the
URDFmaterial tag
- This tag is different from the
- Typically, the
gazeboparts are placed in a separate file and included by the main urdfxacrofile- 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
urdffiles ingazebo. They automatically are converted tosdffiles.- Start with a
xacrofile. Convert to a urdf withxacroand thengazebo converts it to an =sdfand loads the sdf - When done in a launchfile, you never see the intervening
urdforsdffiles. 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
xacroconversion will be easier to track down this way. - Does not have the overhead of launching
rvizto check xacroerror 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 callxacroand view your robot
URDF to SDF
- Use the
gzcommand to convertURDFfiles tosdffiles for gazebogz sdf -p myrobot.urdf > myrobot.sdf
- Inspect the resulting
sdffile. 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
gazebomaterialtag 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
gazebotags 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
sdfyou 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
-boption causes thespawn_modelnode to run until it is killed. When it is killed, the model will be deleted from gazebo. - Use
--helpto see all the options forspawn_model
- The
- Use the
viewmenu 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
viewmenu 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.launchtemplate from thegazebo_rospackage 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_descriptionparameter and will then be automatically converted to ansdfby thespawn_urdf/spawn_modelnode. - Your
package.xmlshould contain an<exec_depend>ongazebo_ros If you want users of your package to be able to use your gazebo resources,
package.xmlshould<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