google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
8.06k stars 806 forks source link

Using the replicate tag on a rangefinder sensor to simulate a LIDAR #1654

Closed bgill92 closed 5 months ago

bgill92 commented 5 months ago

Hi,

I'm a robotics engineer at PickNik Robotics and we're trying to use MuJoCo for simulating a mobile manipulator. An important sensor for mobile robotics is the LIDAR. When researching if it was possible to simulate a LIDAR in MuJoCo, I came across this issue: #688. It doesn't seem like there has been much progress since last year.

Earlier this month version 3.1.5 was released which introduced the replicate meta tag. Initially, I tried using it as followed:

<sensor>
    <replicate count="360" offset="0 0 0" euler="0.0174533 0 0">
      <rangefinder site='lidar'/>
    </replicate>
</sensor>

but I get the following error when launching MuJoCo Viewer:

image

To get the "rangefinder array" to work properly, I had to make a site at the desired link of the kinematic tree, something akin to:

<replicate count="360" offset="0 0 0" euler="0.0174533 0 0">
  <site name="lidar" size="0.01" pos="0.05 0 0" rgba="0.5 0.5 0.5 1"/>
</replicate>

and then in the sensor portion of the MJCF file, populate 360 individual LIDARs. This is very cumbersome, so I ended up writing a Python script to automate the process. This ended up working, as I was able to get a reasonable looking rangefinder array, i.e. a LIDAR:

image

So my question is:

Is it the intent that the replicate tag cannot be used for replicating sensors, or is this a bug?

yuvaltassa commented 5 months ago

replicate only works on kinematic tree elements. If you replicate the site that your sensor is attached to, the sensor will be automatically replicated and correctly namespaced.

This is well documented, do you not agree? Let us know if the documentation could be improved. Perhaps we should add a sensor to the example model?

https://mujoco.readthedocs.io/en/latest/XMLreference.html#replicate-r

yuvaltassa commented 5 months ago

Before replicate:

<mujoco>
  <worldbody>
    <replicate count="2" offset="0 1 0" euler="90 0 0">
      <replicate count="2" sep="-" offset="1 0 0" euler="0 90 0">
        <geom name="Alice" size=".1"/>
      </replicate>
    </replicate>
  </worldbody>
  <sensor>
    <accelerometer name="Bilal" site="Alice"/>
  </sensor>
</mujoco>

After:

<mujoco>
  <worldbody>
    <geom name="Alice-00" size="0.1"/>
    <geom name="Alice-10" size="0.1" pos="1 0 0" quat="1 0 1 0"/>
    <geom name="Alice-01" size="0.1" pos="0 1 0" quat="1 1 0 0"/>
    <geom name="Alice-11" size="0.1" pos="1 1 0" quat="0.5 0.5 0.5 0.5"/>
  </worldbody>
  <sensor>
    <accelerometer name="Bilal-00" site="Alice-00"/>
    <accelerometer name="Bilal-10" site="Alice-10"/>
    <accelerometer name="Bilal-01" site="Alice-01"/>
    <accelerometer name="Bilal-11" site="Alice-11"/>
  </sensor>
</mujoco>