File size: 1,726 Bytes
c11c9a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)