Mujoco Tutorial

2 minute read

Mujoco is a physics-engine which efficiently simulates multi-body dynamics with contacts. It became famous when it started to be used frequently in the reinforcement learning communities. Especially, OpenAI used this engine in their openai-gym.

Now, Deepmind is managing the development of this library, and made it to be used for free.

Main Features

  • Multi-Joint dynamics with Contact
  • Freely available by DeepMind in October 2021
  • Key-Features
    • Generalized coordinates combined with modern contact dynamics
    • Soft, convex and analytically-invertable contact dynamics
    • Tendon geometry
    • General actuation model
    • Reconfigurable computation pipeline
    • Interactive simulation and visualization

C/C++ Library

Install

1export LD_LIBRARY_PATH="~/mujoco/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"

or you can put library file together with your execution file.

Download archive files and extract where you want.

Just add library path on your environment.

ex) edit ~/.bashrc on linux. if folder you extract is mujoco

1export LD_LIBRARY_PATH="~/mujoco/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"

or you can put library file together with your execution file.

Test Execution

1./simulate ../model/humanoid/humanoid.xml

Simple Tutorial

Deepmind provides mujoco documentation officially. But, it’s like dictionary; not easy to start with this!

Fortunately, Pranav Bhounsule in the UNIVERSITY of ILLINOIS CHICAGO provides fantastic lectures about mujoco.(He also provides various valuable lectures on robotics and control..) We can start with this step-by-step.

Python Bindings

Mujoco provides python bindings officially supported by Deepmind from 2.1.2.

Before this, there was the previous python bindings named as mujoco-py managed by OpenAI. Of course, there were more getting started tutorials about mujoco-py.

However, thankfully, Prannav Bhounsule also started to provide lectures on official mujoco python bindings.

Install

1pip install mujoco

Simple Example

https://github.com/BolunDai0216/PyMuJoCoBase uploaded starter code and examples inspired by the C code developed by Prannav Bhounsule’s MuJoCo Bootcamp. (Before Prannav developed lectures of python-version)

With this reference, I write down the simplest bone for minimum simulation.

 1import mujoco
 2import glfw
 3
 4def init_window(max_width, max_height):
 5    glfw.init()
 6    window = glfw.create_window(width=max_width, height=max_height,
 7                                       title='Demo', monitor=None,
 8                                       share=None)
 9    glfw.make_context_current(window)
10    return window
11
12window = init_window(1200, 900)
13
14model = mujoco.MjModel.from_xml_path('../model/humanoid/humanoid.xml')
15# model = mujoco.MjModel.from_xml_path('../model/ur5/ur5.xml')
16# model = mujoco.MjModel.from_xml_path('../model/PR2/pr2.xml')
17
18data = mujoco.MjData(model)
19context = mujoco.MjrContext(model, mujoco.mjtFontScale.mjFONTSCALE_100)
20
21width, height = glfw.get_framebuffer_size(window)
22viewport = mujoco.MjrRect(0, 0, width, height)
23
24scene = mujoco.MjvScene(model, 1000)
25camera = mujoco.MjvCamera()
26mujoco.mjv_updateScene(
27    model, data, mujoco.MjvOption(), mujoco.MjvPerturb(),
28    camera, mujoco.mjtCatBit.mjCAT_ALL, scene)
29
30while(not glfw.window_should_close(window)):
31    mujoco.mj_step(model, data)
32
33    mujoco.mjv_updateScene(
34        model, data, mujoco.MjvOption(), None,
35        camera, mujoco.mjtCatBit.mjCAT_ALL, scene)
36    mujoco.mjr_render(viewport, scene, context)
37
38    glfw.swap_buffers(window)
39    glfw.poll_events()
40
41glfw.terminate()

Reference

[1] MuJoCo Bootcamp

[2] MuJoCo

[3] https://github.com/deepmind/mujoco

[4] studywolf