Gazebo | Ignition | Community
Ask Your Question

Creating N links using a For Loop in Xacro

asked 2014-08-20 05:00:02 -0500

Zamzami gravatar image

updated 2014-08-20 06:51:31 -0500

I wonder if it is possible to construct a for loop to build N wheels sub-assemblies for a mobile robot in a xacro.urdf file.

I have already created an URDF using Xacro for the mobile robot and the wheel sub-assembly but I need to generate N wheels on each side of the mobile robot.

Any ideas, turnarounds or out-of-the-box solutions?


edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2014-09-02 16:12:35 -0500

scpeters gravatar image

updated 2014-09-02 16:14:00 -0500

An alternative to xacro with urdf is to use SDF with embedded ruby (see the rigid_cart model for an example).

Use the following command to generate the sdf file from the rsdf template:

erb model.rsdf > model.sdf

edit flag offensive delete link more


Thank you scpeters, i'm not familiar with ruby but it seems to be an efficient turnaround solution and merits the effort.

Zamzami gravatar imageZamzami ( 2014-09-03 05:56:58 -0500 )edit

answered 2017-02-04 11:03:36 -0500

benelot gravatar image

If you have an if statement and you can call a macro within itself, you can build recursion with a counter which breaks the recursion as it reaches zero.

<xacro:macro name="loop" params="links_qty">
    <!-- call some macros or do other things -->
    <xacro:if value="${links_qty}">
        <xacro:loop links_qty="${links_qty-1}" />

<xacro:loop links_qty="15" />

I just tested if this works and it does.

edit flag offensive delete link more

answered 2014-08-27 06:36:20 -0500

Airuno2L gravatar image

To my knowledge, "if" statements are the only logic expressions currently implemented in xacro. I think having a wheel macro and using a single line for each wheel is the best you can do using just xacro, which isn't too bad.

Can you explain the reason you need this capability? If I really had to have something like this, I would probably attempt to use python to generate the single line macro calls and insert them into the xacro file.

edit flag offensive delete link more


I need to model a vehicle with infinite number of contact points (straight line) on each side of the vehicle. xacro's capability of a single line per wheel is nice but i don't think it will be efficient enough for creating hundreds of wheels.

Zamzami gravatar imageZamzami ( 2014-09-03 05:51:00 -0500 )edit

You can use ERB with SDF. ERB gives you a complete programming language in which to specify a model. See scpeters reply above for an example.

nkoenig gravatar imagenkoenig ( 2014-09-03 11:20:29 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-08-20 05:00:02 -0500

Seen: 3,138 times

Last updated: Feb 04 '17