Note: This tutorial assumes that you have completed the previous tutorials: Building a Visual Robot Model with URDF from Scratch, Building a Movable Robot Model with URDF, Adding Physical and Collision Properties to a URDF Model.
(!) It is appreciated that problems/questions regarding this tutorial are asked on answers.ros.org. Don't forget to include in your question the link to this page, versions of your OS & ROS, and also add appropriate tags.

Using Xacro to Clean Up a URDF File

Description: Learn some tricks to reduce the amount of code in a URDF file using Xacro

Keywords: URDF, Xacro

Tutorial Level: BEGINNER

By now, if you’re following all these steps at home with your own robot design, you might be sick of doing all sorts of math to get very simple robot descriptions to parse correctly. Fortunately, you can use the xacro package to make your life simpler. It does three things that are very helpful.

  • Constants
  • Simple Math
  • Macros

In this tutorial, we take a look at all these shortcuts to help reduce the overall size of the URDF file and make it easier to read and maintain.

Using Xacro

As its name implies, xacro is a macro language. The xacro program runs all of the macros and outputs the result. Typical usage looks something like this:

 rosrun xacro xacro.py model.xacro > model.urdf 

You can also automatically generate the urdf in a launch file. This is convenient because it stays up to date and doesn’t use up hard drive space. However, it does take time to generate, so be aware that your launch file might take longer to start up. (I’m looking at you pr2_description)

   1 <param name="robot_description" 
   2   command="$(find xacro)/xacro.py '$(find pr2_description)/robots/pr2.urdf.xacro'" />

At the top of the URDF file, you must specify a namespace in order for the file to parse properly. For example, these are the first two lines of a valid xacro file:

   1 <?xml version="1.0"?>
   2 <robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="firefighter">

Constants

Let’s take a quick look at our base_link in R2D2.

   1   <link name="base_link">
   2     <visual>
   3       <geometry>
   4         <cylinder length="0.6" radius="0.2"/>
   5       </geometry>
   6       <material name="blue">
   7         <color rgba="0 0 .8 1"/>
   8       </material>
   9     </visual>
  10     <collision>
  11       <geometry>
  12         <cylinder length="0.6" radius="0.2"/>
  13       </geometry>
  14     </collision>
  15   </link>

The information here is a little redundant. We specify the length and radius of the cylinder twice. Worse, if we want to change that, we need to do so in two different places.

Fortunately, xacro allows you to specify properties which act as constants. Instead, of the above code, we can write this.

   1 <xacro:property name="width" value=".2" />
   2 <xacro:property name="bodylen" value=".6" />
   3 <link name="base_link">
   4     <visual>
   5         <geometry>
   6             <cylinder radius="${width}" length="${bodylen}"/>
   7         </geometry>
   8         <material name="blue">
   9             <color rgba="0 0 .8 1"/>
  10         </material>
  11     </visual>
  12     <collision>
  13         <geometry>
  14             <cylinder radius="${width}" length="${bodylen}"/>
  15         </geometry>
  16     </collision>
  17 </link>
  • The two values are specified in the first two lines. They can be defined just about anywhere (assuming valid XML), at any level, before or after they are used. Usually they go at the top.
  • Instead of specifying the actual radius in the geometry element, we use a dollar sign and curly brackets to signify the value.
  • This code will generate the same code shown above.

The value of the contents of the ${} construct are then used to replace the ${}. This means you can combine it with other text in the attribute.

   1 <xacro:property name=”robotname” value=”marvin” />
   2 <link name=”${robotname}s_leg” />

This will generate

   1 <link name=”marvins_leg” />

However, the contents in the ${} don’t have to only be a property, which brings us to our next point...

Math

You can build up arbitrarily complex expressions in the ${} construct using the four basic operations (+,-,*,/), the unary minus, and parenthesis. Exponentiation and modulus are not supported. Examples:

   1 <cylinder radius="${wheeldiam/2}" length=".1"/>
   2 <origin xyz="${reflect*(width+.02)} 0 .25" />

All of the math is done using floats, hence

   1 <link name="${5/6}"/>

evaluates to

   1 <link name="0.833333333333"/>

Macros

Here’s the biggest and most useful component to the xacro package.

Simple Macro

Let’s take a look at a simple useless macro.

   1 <xacro:macro name="default_origin">
   2     <origin xyz="0 0 0" rpy="0 0 0"/>
   3 </xacro:macro>
   4 <xacro:default_origin />

(This is useless, since if the origin is not specified, it has the same value as this.) This code will generate the following.

   1 <origin rpy="0 0 0" xyz="0 0 0"/>
  • The name is not technically a required element, but you need to specify it to be able to use it.
  • Every instance of the <xacro:$NAME /> is replaced with the contents of the xacro:macro tag.

  • Note that even though its not exactly the same (the two attributes have switched order), the generated XML is equivalent.
  • If the xacro with a specified name is not found, it will not be expanded and will NOT generate an error.

Parameterized Macro

You can also parameterize macros so that they don’t generate the same exact text every time. When combined with the math functionality, this is even more powerful.

First, let’s take an example of a simple macro used in R2D2.

   1     <xacro:macro name="default_inertial" params="mass">
   2         <inertial>
   3                 <mass value="${mass}" />
   4                 <inertia ixx="1.0" ixy="0.0" ixz="0.0"
   5                      iyy="1.0" iyz="0.0"
   6                      izz="1.0" />
   7         </inertial>
   8     </xacro:macro>

This can be used with the code

   1 <xacro:default_inertial mass="10"/>

The parameters act just like properties, and you can use them in expressions

You can also use entire blocks as parameters too.

   1 <xacro:macro name="blue_shape" params="name *shape">
   2     <link name="${name}">
   3         <visual>
   4             <geometry>
   5                 <xacro:insert_block name="shape" />
   6             </geometry>
   7             <material name="blue">
   8                 <color rgba="0 0 .8 1"/>
   9             </material>
  10         </visual>
  11         <collision>
  12             <geometry>
  13                 <xacro:insert_block name="shape" />
  14             </geometry>
  15         </collision>
  16     </link>
  17 </xacro:macro>
  18 
  19 <xacro:blue_shape name="base_link">
  20     <cylinder radius=".42" length=".01" />
  21 </xacro:blue_shape>
  • To specify a block parameter, include an asterisk before its parameter name.
  • A block can be inserted using the insert_block command
  • Insert the block as many times as you wish.

Practical Usage

The xacro language is rather flexible in what it allows you to do. Here are a few useful ways that the xacro is used in the R2D2 model, in addition to the default inertial macro shown above.

To see the model generated by a xacro file, run this command: roslaunch urdf_tutorial xacrodisplay.launch model:=urdf/08-macroed.urdf.xacro

Leg macro

Often you want to create multiple similar looking objects in different locations. Often, there will be some symmetry to the locations. You can use a macro and some simple math to reduce the amount of code you have to write, like we do with R2’s two legs.

   1 <xacro:macro name="leg" params="prefix reflect">
   2     <link name="${prefix}_leg">
   3         <visual>
   4             <geometry>
   5                 <box size="${leglen} .2 .1"/>
   6             </geometry>
   7             <origin xyz="0 0 -${leglen/2}" rpy="0 ${pi/2} 0"/>
   8             <material name="white">
   9                 <color rgba="1 1 1 1"/>
  10             </material>
  11         </visual>
  12         <collision>
  13             <geometry>
  14                 <box size="${leglen} .2 .1"/>
  15             </geometry>
  16             <origin xyz="0 0 -${leglen/2}" rpy="0 ${pi/2} 0"/>
  17         </collision>
  18         <xacro:default_inertial mass="10"/>
  19     </link>
  20 
  21     <joint name="base_to_${prefix}_leg" type="fixed">
  22         <parent link="base_link"/>
  23         <child link="${prefix}_leg"/>
  24         <origin xyz="${reflect*(width+.02)} 0 .25" />
  25     </joint>
  26     <!-- A bunch of stuff cut -->
  27 </xacro:macro>
  28 <xacro:leg prefix="right" reflect="1" />
  29 <xacro:leg prefix="left" reflect="-1" />
  • Common Trick 1: Use a name prefix to get two similarly named objects
  • Common Trick 2: Use math to calculate joint origins. In the case that you change the size of your robot, changing a property with some math to calculate the joint offset will save a lot of trouble.
  • Common Trick 3: Using a reflect parameter, and setting it to 1 or -1. See how we use the reflect parameter to put the legs on either side of the body in the base_to_${prefix}_leg origin.

Other tricks

Feel free to append your own tricks here.

Wiki: urdf/Tutorials/Using Xacro to Clean Up a URDF File (last edited 2014-03-19 09:42:44 by GvdHoorn)