Skip to content

MuJoCo physics engine plugin #299

@traversaro

Description

@traversaro

Desired behavior

MuJoCo, a physics engine that is quite used in the Reinforcement Learning community is now free to use will be open sourced soon, see https://deepmind.com/blog/announcements/mujoco and https://community.gazebosim.org/t/deep-mind-bought-mujoco-and-is-open-sourcing-it . It would be cool to have a ignition-physics plugin for it, even because one standard reply in review of any paper sent to RL conferences is "Did you compared/tested it with MuJoCo?".

Implementation suggestion

I wanted to understand a bit how MuJoCo works, so I implemented an extremly hackish "proof of concept" of ignition-physics/MuJoCo integration. While first attempt is a big hack and may be problematic to continue on, it may be useful to other people trying to do this in a more proper way.

With extremly hackish I mean that the following features are not supported:

  • Floating Base models
  • Tree models
  • Collisions
  • Joint limits
  • Joint friction
  • Adding bodies/joint/models
  • Set control joint force
  • Getting the joint state

And probably many more feature I am not thinking about.
Furthermore, attributes were added without proper getters/setters. However, with this I was able to get some basic fixed base chain to simulate some basic passive dynamics.

Additional context

A video of the MVP simulating a fixed base double pendulum with Gazebo (if you look in the video, you can see also the MJCF file automatically generated from ignition-physics calls):

ignition_gazebo_mujoco_mvp.mp4

The code shown in the video is part of the mujoco branch in three ignition libraries:

To run it, just install mujoco with the following instructions:

cd ~
wget https://mujoco.org/download/mujoco210-linux-x86_64.tar.gz
tar -zxvf mujoco210-linux-x86_64.tar.gz

After that, add the following code in your setup.sh or .bashrc:

export CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:~/mujoco210

(this will ensure that mujoco is correctly found by the FindIgnMuJoCo.cmake script added in ign-cmake).

Then follow the instructions in https://ignitionrobotics.org/docs/fortress/install_ubuntu_src, but use the file https://raw.githubusercontent.com/traversaro/gazebodistro/mujoco/collection-fortress.yaml in vcs instead of the official one. Then after completing the installation, you can run the example world with the command:

ign gazebo -v 4 --render-engine ogre --physics ignition-physics-mujoco-plugin fixed_dbl_pendulum.sdf

(you can omit --render-engine ogre if ogre2 works fine for you).

To explain how I did proceed, I did the following:

  • I copied the tpe engine in a mujoco to have the necessary boilerplate to start working
  • I added to the mujocolib library (obtained after renaming the tpeliblibrary) the features that were not in tpe that I was interested in, such as inertial parameters for links and joints
  • I added in the mujoco-plugin library the features I was interested in (for example, Joint features)
  • As MuJoCo do not have support for building the model while the simulation is ongoing, I added in mujocolib::World the World::RegeneratedMuJoCoDataStructures, that will re-generated a MuJoCo's MJCF file whenever it was detected that the structure encoded in the mujocolib::World instance and in its child elements was changed (ideally, for the elements that were there before the state should be carried over, but this is not implemented in this MVP). Most of the MuJoCo-specific code (including the on-the fly generation of MJCF files) is here.
  • I modified mujocolib::World::Step to actually step the MuJoCo simulation, and copy the link state back in the mujocolib::World instance, from where then the ign-physics APIs access it. Some MuJoCo-specific code is also here).

Alternatives considered

Something that I noticed is that most of the logic related to handling entities is probably something that could be reused for many more physics engines that like MuJoCo have the following properties:

  • Do not have an API to add bodies/models on the fly, so whenever a new link/joint/model is added or removed, need to re-generate their simulation from scratch (if I am not wrong, this is kind of similar to the problem that were encounter in Integrate Bullet #44).
  • Do not have a shared ptr for each entity.
    Something that could make sense could be to have some simpler interface for these kind of physics engines.
Pinned by azeey

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesthelp wantedExtra attention is needed

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions