User Avatar

John Vial

1y ago

I Write About Simplifying Robotics For Beginners | PhD In Robotics

How To Use Substitution Args In XACRO Files So You Can Reuse Your Robots URDF's And Avoid XACRO erorrs
John Vial

One frustrating thing about ROS 2 control is that your robot's URDF needs to be changed depending upon whether you are running in simulation or real world.

XACRO allows roboticists to script their URDF's so they can treat it more like a programming language. The problem is that its documentation is abit sparse. So here are some quick notes so you can never have XACRO troubles again.

Using Args

In XACRO, you can pass in command line args, and specify them with the tag xacro:arg.

You probably want to use those args in a xacro:if statement to switch your ros2 controllers out. Doing something like this (where you want target to be a string specifying gazebo or real ):

<xacro:if value="$(arg target)" >

But if you use this, then you are forced to set target to a boolean, i.e. target:=true.

This works for simple cases, but sometimes you want more complex comparisons (say if you target multiple simulation back ends).

Using Python

The value= property can actually evaluate python code, the only condition is that it must return a boolean.

To evaluate python code you need to put it inside of a ${ }, for example:

<xacro:if value="${5 <= 2}" >

This clearly very powerful, but just adding the arg target will result in an error:

<xacro:if value="${arg target == 'gazebo'}" >

Using Args In Xacro Python Eval

The trick is to notice that to evaluate a python expression you use curly braces { but to evaluate an arg you use curved brackets (

It's ugly, but the solution is to combine both, such as:

<xacro:if value="${$(arg gazebo) == 'gazebo'}" >

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers