Xacro

Also known as: XML Macros

On this page 6

Xacro, short for XML Macros, is a macro language used in the Robot Operating System (ROS) to write compact, parameterized robot descriptions that are expanded into plain URDF files. In robotics simulation and visualization, Xacro lets engineers define a robot once with properties, macros, math expressions and file includes, instead of repeating hundreds of lines of nearly identical XML for every link and joint.

Xacro is a preprocessing step, not a separate description format. Tools such as Gazebo, RViz and MoveIt never read Xacro directly; they receive the generated URDF, usually produced at launch time and published as the robot_description parameter.

How Xacro expands into URDF

Xacro works by text-level expansion of an XML document: the xacro processor reads a file with the .urdf.xacro or .xacro extension, evaluates every Xacro construct and writes a standard URDF tree with all macros resolved. The output is identical in structure to a hand-written URDF, so downstream tools need no special support.

In ROS 2 the processor is distributed as the xacro package and is usually called from a Python launch file. Engineers can also run it manually to inspect the expanded URDF, which is the fastest way to confirm that every macro produced the intended links and joints.

Expansion happens in a defined order. The processor first resolves includes, then evaluates properties and arguments, then substitutes macro calls and finally evaluates conditional blocks. Expressions inside ${...} are evaluated with a restricted Python interpreter, so values such as ${wheel_radius * 2} or ${pi/2} become literal numbers in the result.

The main language elements and their purposes are listed below.

ConstructPurposeExample use
xacro:propertyDefine a named constantLink length, mass or color
xacro:macroDefine a reusable parameterized blockOne wheel or one finger
xacro:argAccept values from the command lineSelect a gripper variant
xacro:includeInsert another fileShared materials or sensor modules
xacro:if and xacro:unlessInclude blocks conditionallyAdd a camera only when enabled
Core Xacro constructs

Why Xacro matters for robot description

Xacro exists because URDF has no variables, loops or reuse. A six-wheeled rover or a five-finger hand written in raw URDF repeats the same link, joint, inertial and collision definitions many times, and a single dimension change must be edited in dozens of places.

With Xacro, a wheel becomes one macro with parameters for name, position and side, and the robot calls it six times. Single-source dimensions reduce copy errors, and derived values such as inertia tensors can be computed from mass and geometry instead of typed by hand. Teams also split large robots into modular files for the base, arm, gripper and sensors, which lets different engineers work in parallel.

The table compares the effort for typical description tasks.

TaskRaw URDFWith Xacro
Change a link lengthEdit every dependent originEdit one property
Add a fourth identical legCopy and rename dozens of linesAdd one macro call
Swap a gripper variantMaintain separate filesPass a launch argument
Compute box inertiaCalculate values externallyUse an inline expression
Reuse a sensor across robotsDuplicate the XMLInclude a shared module
Raw URDF compared with Xacro

Xacro in the simulation workflow

In a typical ROS 2 simulation workflow, Xacro is the entry point of the robot model. A launch file runs the xacro command, passes arguments such as the simulator name or sensor options, and publishes the resulting URDF for the robot state publisher, the simulator spawner and motion planning.

Xacro is especially useful for simulation-specific extensions. Gazebo plugins, ros2_control hardware interfaces and sensor noise parameters can be placed in conditional blocks, so the same description serves both the physical robot and its digital twin. A boolean argument like use_sim selects simulated or real hardware interfaces without forking the model.

The neighboring description formats differ in scope and role.

FormatRoleMain users
XacroMacro preprocessor that emits URDFROS and ROS 2 projects
URDFKinematic tree with links and jointsRViz, MoveIt, robot state publisher
SDFormatFull world and model descriptionGazebo simulation
MJCFPhysics-oriented model formatMuJoCo simulation
Xacro and related robot description formats

Xacro cannot express closed kinematic loops or full world scenes, because its output is limited by what URDF itself supports. Gazebo converts URDF to SDFormat internally, and other simulators provide their own importers.

Common mistakes and good practice

Most Xacro errors come from namespace, scope and evaluation-order issues rather than from the robot geometry itself. Because errors surface only after expansion, debugging the generated URDF is a key skill.

SymptomLikely causeFix
Unknown tag errorMissing xacro namespace declarationDeclare the namespace in the root robot element
Duplicate link namesMacro called twice with the same prefixAdd a prefix parameter to every name
Undefined propertyProperty defined after its use or out of scopeDefine properties before includes and calls
Wrong joint orientationDegrees used instead of radiansUse radians or the pi constant
Robot falls apart in simulationZero or tiny inertial valuesCompute inertia from mass and shape
Frequent Xacro problems and fixes
  • Always validate the output with check_urdf or by viewing it in RViz before simulation.
  • Prefix every name inside macros so a component can be instantiated multiple times.
  • Keep macros small and focused so each one describes a single physical module.
  • Avoid heavy logic in Xacro; complex generation belongs in a script that writes parameters.

Understanding it simply

Xacro is like a recipe template for describing a robot. Instead of writing out every wheel, joint and part separately, an engineer writes the description of one wheel once and then says use this wheel four times, here, here, here and here.

A small program then turns the template into the full, long description that simulators and visualization tools understand. If a wheel size changes, the engineer edits one number and every wheel updates automatically.

Applied in practice

A warehouse robotics team develops a mobile manipulator in two versions: one with a parallel gripper and one with a vacuum tool. Their original URDF had grown to about 2,000 lines, and a change to the base width once left two wheels misplaced in simulation for a week.

The team rewrites the model in Xacro with separate files for the base, arm, tools and sensors. Wheels and arm links become macros, the base width becomes a single property, and launch arguments named tool and use_sim select the end effector and whether Gazebo plugins or real ros2_control drivers are loaded.

The top-level file shrinks to under 300 lines, both tool variants come from one source, and the simulated and physical robots now share identical kinematics. New engineers can add a sensor by including one module file, and continuous integration expands every variant and runs check_urdf on each commit.