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.
| Construct | Purpose | Example use |
|---|---|---|
| xacro:property | Define a named constant | Link length, mass or color |
| xacro:macro | Define a reusable parameterized block | One wheel or one finger |
| xacro:arg | Accept values from the command line | Select a gripper variant |
| xacro:include | Insert another file | Shared materials or sensor modules |
| xacro:if and xacro:unless | Include blocks conditionally | Add a camera only when enabled |
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.
| Task | Raw URDF | With Xacro |
|---|---|---|
| Change a link length | Edit every dependent origin | Edit one property |
| Add a fourth identical leg | Copy and rename dozens of lines | Add one macro call |
| Swap a gripper variant | Maintain separate files | Pass a launch argument |
| Compute box inertia | Calculate values externally | Use an inline expression |
| Reuse a sensor across robots | Duplicate the XML | Include a shared module |
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.
| Format | Role | Main users |
|---|---|---|
| Xacro | Macro preprocessor that emits URDF | ROS and ROS 2 projects |
| URDF | Kinematic tree with links and joints | RViz, MoveIt, robot state publisher |
| SDFormat | Full world and model description | Gazebo simulation |
| MJCF | Physics-oriented model format | MuJoCo simulation |
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.
| Symptom | Likely cause | Fix |
|---|---|---|
| Unknown tag error | Missing xacro namespace declaration | Declare the namespace in the root robot element |
| Duplicate link names | Macro called twice with the same prefix | Add a prefix parameter to every name |
| Undefined property | Property defined after its use or out of scope | Define properties before includes and calls |
| Wrong joint orientation | Degrees used instead of radians | Use radians or the pi constant |
| Robot falls apart in simulation | Zero or tiny inertial values | Compute inertia from mass and shape |
- 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.