0
$\begingroup$

I'm trying to write a world by reusing models as much as I can. The world contains 2 similar models "PublicBathroomA" and "PublicBathroomB".
They differ in few things, pose and name which can be easily be changed with the corresponding sub-tags of , and with a link which differ only by its pose.

<model name="PublicBathroomA">
  <pose>
    9.0 3.0 0
    0 0 0
  </pose>
  ...
  <link name="door">
    <pose>-0.3 -0.05 0 0 0 6.07</pose>
    ...
  </link>
</model>

and

<model name="PublicBathroomB">
  <pose>
    12.0 3.0 0
    0 0 0
  </pose>
  ...
  <link name="door">
    <pose>-0.3 -0.05 0 0 0 6.17</pose>
    ...
  </link>
</model>

Do you have an idea on how to manage this link pose ? For the moment, my incomplete solution could be

    <include>
      <uri>model://office/PublicBathroom.sdf</uri>
      <name>PublicBathroomA</name>
      <pose>
        9.0 3.0 0
        0 0 0
      </pose>
    </include>

a working solution can be to generate on the fly the PublicBathroomA.sdf and PublicBathroomB.sdf files from a template and include both files, but it is not the same approach.

$\endgroup$
1
  • $\begingroup$ I tried both Jinja2 and xacro template solutions, with the template appart from the world file. They are really similar (so use the solution you already know). The main difference in my opinion is that Jinja separate more the data from the template use. Also xacro does not manage stream input. $\endgroup$
    – SebL
    Commented Apr 12 at 9:15

1 Answer 1

1
$\begingroup$

If you're in a ROS-aware environment or can install it separately, you might look at using xacro. Then you can define a xacro:macro.

For example (saving the content below as bathroom.sdf.xacro):

<?xml version="1.0"?>
<sdf version="1.11">
<world xmlns:xacro="http://www.ros.org/wiki/xacro">

<!-- Define the xacro:macro for later use -->
<xacro:macro name="public_bathroom" params="identifier=DEFAULT_ID pose='0 0 0 0 0 0' door_link_pose='0 0 0 0 0 0'">
<include>
  <model name="PublicBathroom${identifier}">
    <pose>
      ${pose}
    </pose>
    <sometag>
    </sometag>
    <link name="door">
      <pose>
        ${door_link_pose}
      </pose>
    </link>
  </model>
</include>
</xacro:macro>

<!-- Instantiate three different public bathrooms, the last one default -->
<xacro:public_bathroom identifier="A" pose="9.0 3.0 0 0 0 0" door_link_pose="-0.3 -0.05 0 0 0 6.07"/>
<xacro:public_bathroom identifier="B" pose="12.0 3.0 0 0 0 0" door_link_pose="-0.3 -0.05 0 0 0 6.17"/>
<xacro:public_bathroom/>

</world>
</sdf>

<sometag></sometag> is your common content across all bathrooms.

Running xacro bathroom.sdf.xacro results in this output:

<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- |    This document was autogenerated by xacro from bathroom.sdf.xacro             | -->
<!-- |    EDITING THIS FILE BY HAND IS NOT RECOMMENDED                                 | -->
<!-- =================================================================================== -->
<sdf version="1.11">
  <world>
    <include>
      <model name="PublicBathroomA">
        <pose>
      9.0 3.0 0 0 0 0
    </pose>
        <sometag>
    </sometag>
        <link name="door">
          <pose>
        -0.3 -0.05 0 0 0 6.07
      </pose>
        </link>
      </model>
    </include>
    <include>
      <model name="PublicBathroomB">
        <pose>
      12.0 3.0 0 0 0 0
    </pose>
        <sometag>
    </sometag>
        <link name="door">
          <pose>
        -0.3 -0.05 0 0 0 6.17
      </pose>
        </link>
      </model>
    </include>
    <include>
      <model name="PublicBathroomDEFAULT_ID">
        <pose>
      0 0 0 0 0 0
    </pose>
        <sometag>
    </sometag>
        <link name="door">
          <pose>
        0 0 0 0 0 0
      </pose>
        </link>
      </model>
    </include>
  </world>
</sdf>

I don't know the details of installing xacro without ROS and the limitations there.

It seems like there are a couple of relevant recent PRs to allow xacro to work without ROS, but I haven't tried it:

https://github.com/ros/xacro/pull/329 https://github.com/ros/xacro/pull/340

I ran my example from a ROS 2 Humble environment.

$\endgroup$
2
  • $\begingroup$ Thank you for the answer, i'll have a look at it. At first glance I do not see the advantage over a generic template system like Jinja. $\endgroup$
    – SebL
    Commented Apr 11 at 15:27
  • $\begingroup$ I'm not even sure I would personally advocate for xacro over Jinja or whatever you like in a greenfield scenario $\endgroup$ Commented Apr 12 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.