fleaven's picture
Add files using upload-large-folder tool
c11c9a2 verified
import numpy as np
import time
import mujoco
import mujoco.viewer
from robot_descriptions.loaders.mujoco import load_robot_description
'''
pip install git+https://github.com/robot-descriptions/robot_descriptions.py.git@main
'''
def mj_play(data, fr):
m = load_robot_description("elf2_mj_description") # load robot description like python module
# or load from local xml file
# m = mujoco.MjModel.from_xml_path('path/to/elf2_dof25.xml')
m.opt.timestep = 1.0/fr
d = mujoco.MjData(m)
len = data.shape[0]
step = 0
with mujoco.viewer.launch_passive(m, d, show_left_ui=False, show_right_ui=False) as viewer:
# set camera
viewer.cam.lookat = d.body("base_link").xpos # camera following "base_link" body
viewer.cam.distance = 5.0
viewer.cam.elevation = -20
#viewer.paused = True
while viewer.is_running() and step<len:
step_start = time.time()
d.qpos = data[step, :7+29]
d.qpos[3] = data[step, 6] # to w first
d.qpos[4:7] = data[step, 3:6]
mujoco.mj_forward(m, d)
viewer.sync()
# Rudimentary time keeping, will drift relative to wall clock.
step += 1
time_until_next_step = m.opt.timestep - (time.time() - step_start)
if time_until_next_step > 0:
time.sleep(time_until_next_step)
def read_rtj(fpath):
#get frame rate from file name
fr = fpath[-12:-9]
fr = int(fr[1:]) if fr[0]=='_' else int(fr)
jpos = np.load(fpath)
jpos[:,2] += 0.793
return jpos,fr
if __name__ == '__main__':
jpos, fr = read_rtj('./bxi_elf2/CMU/01/01_01_poses_120_jpos.npy')
mj_play(jpos, fr)