google-deepmind / mujoco

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

Static world geometry using URDF and Python bindings #333

Closed surgura closed 2 years ago

surgura commented 2 years ago

Using the new Python bindings and URDF, how do I create a robot and a static environment, such as the ground and some walls? I can successfully load a robot using URDF.

<?xml version="1.0" ?>
<robot name="robot_0">
    <link name="origin">
...
</robot>
model = mujoco.MjModel.from_xml_string(urdf, assets)
data = mujoco.MjData(model)

for _ in range(100000):
    mujoco.mj_step(model, data)
    print(data.geom_xpos)

viewer.close()

Do I need to describe the environment in the same URDF too? Or is there an API for adding objects? On the ROS website I cannot find information about adding static geometry to URDF. Thank you in advance.

Additionally, the root link of the robot appears to hang in the air at position (0,0,0). Is there a way to resolve that? The robot behaves as expected in Isaac Gym.

kevinzakka commented 2 years ago

Hi @surgura,

Highly recommend you go through the tutorial first. If you still have questions after that, feel free to post them here and I can try to answer.

surgura commented 2 years ago

Thank you @kevinzakka, I appreciate the link to this tutorial, it has some useful points. However, I do not use dm_control which is the topic of this tutorial. Furthermore the tutorial uses the mujoco specific XML format, whereas I use URDF. Do you maybe know a minimal example of a robot in a static environment using URDF and mujoco?

yuvaltassa commented 2 years ago

Hi @surgura, none of us is a URDF expert so I don't think you'll be able to get a good answer to this from the development team, but here are 2 points I can think of:

  1. URDF is a very bare-bones format and using it would greatly limited in what you could model in MuJoCo. I strongly recommend against this.
  2. SDF, which could be a described as a much improved generalisation of URDF can, amongst many other things, properly describe the robot environment. Here is a work-in-progress tool for converting between SDF and MJCF.
surgura commented 2 years ago

Thank you. One thing is unclear to me: must the complete simulation setup(robot, environment, etc) be defined in one big xml file for mujoco? Or is it possible to programmatically add geometry or load multiple URDF robots at different position? My environment is already procedurally generated, so this would make sense for me.

yuvaltassa commented 2 years ago

MuJoCo has an <include> directive documented here. Unfortunately while usable (here is an example), it is also quite limited. Until we improve native MJCF composability (which is on our roadmap), dm_control remains your best option for procedural composability.

Unless you are very comfortable with C++, in which case you could use the MuJoCo compiler directly to make your models as in the implementation of procedural composite bodies here, but that API is undocumented and unsupported at the moment.

surgura commented 2 years ago

Thank you. I will use the approach described here https://mujoco.readthedocs.io/en/latest/modeling.html?highlight=mjcf#urdf-extensions and load my urdf files, then save them to mjcf and compose a larger mjcf from those.