gazebosim / gz-physics

Abstract physics interface designed to support simulation and rapid development of robot applications.
https://gazebosim.org
Apache License 2.0
63 stars 38 forks source link

MuJoCo physics engine plugin #299

Open traversaro opened 2 years ago

traversaro commented 2 years ago

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:

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):

https://user-images.githubusercontent.com/1857049/138121453-a804e232-53f7-44ee-b4d1-dd4959d60ec0.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:

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:

mjcarroll commented 2 years ago

Well done @traversaro! Very impressive work, look forward to trying it out.

traversaro commented 2 years ago

Well done @traversaro! Very impressive work, look forward to trying it out.

Do not expect too much from it! I do not think it works for anything except for the fixed_dbl_pendulum.sdf (that I had to add ad-hoc to Ignition Gazebo). 😄

PeterMitrano commented 2 years ago

Incredible, I was just thinking about this earlier today. Very exciting to see other are interested and prototyping it.

scpeters commented 2 years ago

this is awesome!

mabelzhang commented 2 years ago

Much desired. Thank you for putting in the work and sharing it!! So many possibilities.

stefanbuettner commented 2 years ago

Awesome! Was about to ask whether there are any plans of integrating MuJoCo. Having it as a physics engine in Gazebo would be great :thumbsup:

traversaro commented 2 years ago

Awesome! Was about to ask whether there are any plans of integrating MuJoCo. Having it as a physics engine in Gazebo would be great 👍

Just to clarify, this is just a proof of concept that I do not plan to (at the moment) to develop further, so at the moment I do not know if there are any plans to integrate MuJoCo in Ignition Gazebo. However if someone is willing to invest the time to develop a MuJoCo plugin, I would be happy to provide guidance/explain more in depth the proof of concept.

stefanbuettner commented 2 years ago

Yeah, I didn't expect this to be a kickoff for the integration. Actually it was supposed to be a question: Are there any plans to officially support MuJoCo as a physics engine in ignition-physics?

traversaro commented 2 years ago

I think this functionality would be quite useful for this: https://github.com/gazebosim/gz-sim/pull/1560 , as mujoco does not easily provide a way to incrementally build a model (see https://github.com/gazebosim/gz-physics/pull/298#issuecomment-1171056948 for a longer discussion).