robotology / idyntree

Multibody Dynamics Library designed for Free Floating Robots
BSD 3-Clause "New" or "Revised" License
155 stars 65 forks source link

Add option to add names to collision and visual elements in exported URDF #1172

Closed traversaro closed 2 months ago

traversaro commented 3 months ago

@CarlottaSartore (feel free to add more comments) some specific cases when reading URDF in MuJoCo for which it is important to add names to the collision and visual elements exported in URDF.

The iDynTree URDF exporter should support adding automatically this values, and eventually permit to users to set them to specific values.

traversaro commented 3 months ago

fyi @Nicogene @mfussi66

traversaro commented 3 months ago

Actually the code is already there: https://github.com/robotology/idyntree/blob/c6a00762d4bf87a1ecb1fc6655e99369722d2235/src/model_io/codecs/src/URDFModelExport.cpp#L146-L150 . So, the problem is that in creo2urdf we are not setting the collision and visual names, no sure if it could make sense to have an option in the URDF exporter to automatically add collision names if this are not present.

CarlottaSartore commented 3 months ago

Thanks @traversaro !

To give a bit of background about the issue, if the name of either the visual or the collision is not defined, mujoco treated it as an unamed geometry.

This became problematic when the unnamed geometries belongs to a link which is not directly attached to any active joint. Indeed, in this case, mujoco lumps the link to another body, meanwhile the associated collision, if any, is still present in the mujoco file, but as an unamed geometry, hence difficult to manipulate.

This is, for instance, the case for the stickbot model, where the l_foot_rear and l_foot_front link, is connected to the ankle via l_foot_front_ft_sensor, which is fixed.

Thus, the mujoco translation from urdf to mujoco will output this:

<body name="l_ankle_2" pos="-0.05275 -0.0182 -0.035">
<inertial pos="0.0640328 0 -0.0273056" quat="0.418814 0.569732 0.569732 0.418814" mass="1.36117" diaginertia="0.0320042 0.0313731 0.0306311"/>
<joint name="l_ankle_roll" pos="0 0 0" axis="1 0 0" limited="true" range="-0.436332 0.436332" damping="2"/>
<geom size="0.05 0.0585 0.00515" pos="0.016 0 -0.0658" quat="0.707107 0 0 -0.707107" type="box" rgba="1 1 0 1"/>
<geom size="0.05 0.0585 0.00515" pos="0.13525 0 -0.0658" quat="0.707107 0 0 -0.707107" type="box" rgba="1 0 1 1"/>
 </body>

Thus, it will be difficult to read the contact associated with the foot rear or front. If instead the collision name is defined, for instance as l_foot_rear_collision, we obtain:

<body name="r_ankle_2" pos="-0.05275 0.0182 -0.035">
 <inertial pos="0.0640693 0 -0.0273624" quat="0.418764 0.569769 0.569769 0.418764" mass="1.3588" diaginertia="0.0320088 0.0313773 0.0306314"/>
<joint name="r_ankle_roll" pos="0 0 0" axis="-1 0 0" limited="true" range="-0.436332 0.436332" damping="2"/>
<geom name="r_foot_front_col" size="0.05 0.0585 0.00515" pos="0.13525 0 -0.0658" quat="0.707107 0 0 -0.707107" type="box"/>
 <geom name="r_foot_rear_col" size="0.05 0.0585 0.00515" pos="0.016 0 -0.0658" quat="0.707107 0 0 -0.707107" type="box"/>
 </body>

allowing to read correctly the wrenches information.

traversaro commented 3 months ago

fyi @francesco-romano as he may be interested in this.

francesco-romano commented 3 months ago

Yes, it is better to name them univocally if you want to import the model into Mujoco. Note that the name must be unique across visual and collisions (the type is not part of the identifier). For example we prefix the mesh name with visual_ and collision_ when building the model prior to exporting. I think if two names are equal MuJoCo silently add unnamed geoms when parsing the URDF

francesco-romano commented 3 months ago

As @traversaro said the possibility to name the meshes is already there and I use it successfully

traversaro commented 2 months ago

Fixed by https://github.com/robotology/idyntree/pull/1173 .