[구현] MuJoCo 기초 사용법

MuJoCo

MuJoCo를 파이썬에서 사용하기 위해서 mujoco-py 모듈을 사용해왔다. 이 모듈을 설치하기 위해서는 hidden folder를 만들고, cython build도 번거롭게 진행해주어야 했다. 성공적으로 설치를 끝마쳐도, glfw 라이브러리와 충돌도 일어나 jupyter-notebook 에서 렌더링을 수행해주는 것에 어려움 또한 있었다.

하지만 mujoco==2.1.0 버전 이후로 파이썬 바인딩이 되어, 단순히 pip install 만으로도 라이브러리 사용이 가능해졌다. 그리고 dependency 관련 에러도 많이 있었으나, mujoco로 바인딩 된 후에는 이러한 에러가 모두 사라졌다.

sudo apt-get update
pip3 install mujoco
pip3 install mujoco-python-viewer # Rendering helper utilities
pip3 install mujoco-mjx # (optional: jax binding)

Rendering in server

참고 링크: [1] [2]

MuJoCo render를 위해 어떠한 개발자가 만들어놓은 모듈을 기반으로 코드가 진행된다.

서버에서 사용할 것이라면, 추가적인 모듈을 설치해야 한다. 서버엔 GUI가 따로 없기 때문에 MuJoCo 씬을 렌더링하기 위해서는 pyvirtualdisplay가 필요하다. pyvirtualdisplay를 사용하기 위해서는 Xvfb가 필요하므로 xvfbxvfbwrapper를 추가로 설치해준다.

pip3 install pyvirtualdisplay
sudo apt-get install xvfb
pip3 install xvfbwrapper

렌더링을 위한 코드는 아래와 같다.

import mujoco
import numpy as np
import matplotlib.pyplot as plt
from mujoco_parser import MuJoCoParserClass
print ("MuJoCo version:[%s]"%(mujoco.__version__))

xml_path = '../asset/ur5e/scene_ur5e_rg2.xml'
env = MuJoCoParserClass(name='UR5e',rel_xml_path=xml_path,VERBOSE=True)

env.init_viewer(viewer_title='UR5e with RG2',viewer_width=1200,viewer_height=800,
                viewer_hide_menus=True)
env.update_viewer(azimuth=174.08,distance=2.76,elevation=-33,lookat=[0.1,0.05,0.16],
                  VIS_TRANSPARENT=False,VIS_CONTACTPOINT=True,
                  contactwidth=0.05,contactheight=0.05,contactrgba=np.array([1,0,0,1]),
                  VIS_JOINT=True,jointlength=0.5,jointwidth=0.1,
                  jointrgba=[0.2,0.6,0.8,0.6])
env.reset()

ctrl = np.radians([0,-90,0,0,0,0,0])
while (env.get_sim_time() < 100.0) and env.is_viewer_alive():
    env.step(ctrl=ctrl)
    env.plot_T(p=np.zeros(3),R=np.eye(3,3),PLOT_AXIS=True,axis_len=1.0,axis_width=0.01)
    env.render(render_every=5)
    if env.loop_every(HZ=1): 
        scene_img = env.grab_image()
        plt.figure(figsize=(5,4)); plt.imshow(scene_img)
        plt.title("Tick:[%d] Time:[%.2f]sec"%(env.tick,env.get_sim_time()),fontsize=9)
        plt.show()
env.close_viewer()
print ("Done.")

성공 화면

아래 비디오는 UR5e Manipulator 환경에 대해 rendering 된 결과이다.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • [구현] AprilTag Pose Estimation
  • [miscellaneous] tmux usage
  • [miscellaneous] docker usage
  • [study] Mixture Density Network
  • [miscellaneous] pyenv usage