Skip to content

Commit 9df965d

Browse files
committed
added mujoco shadow hand teleop demo
1 parent 31888bc commit 9df965d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
3+
import mujoco
4+
import numpy as np
5+
import pinocchio as pin
6+
from dex_retargeting.constants import HandType, RetargetingType, RobotName
7+
from mujoco import viewer as mj_viewer
8+
9+
from xrobotoolkit_teleop.utils.dex_hand_utils import DexHandTracker, pico_hand_state_to_mediapipe
10+
from xrobotoolkit_teleop.utils.mujoco_utils import calc_mujoco_ctrl_from_qpos, calc_mujoco_qpos_from_pin_q
11+
from xrobotoolkit_teleop.utils.path_utils import ASSET_PATH
12+
from xrobotoolkit_teleop.utils.xr_client import XrClient
13+
14+
15+
def main():
16+
urdf_path = os.path.join(ASSET_PATH, "shadow_hand/shadow_hand_right.urdf")
17+
xml_path = os.path.join(ASSET_PATH, "shadow_hand/xml/scene_right.xml")
18+
19+
mj_model = mujoco.MjModel.from_xml_path(xml_path)
20+
mj_data = mujoco.MjData(mj_model)
21+
pin_model = pin.buildModelFromUrdf(urdf_path)
22+
23+
xr_client = XrClient()
24+
dextracker = DexHandTracker(
25+
robot_name=RobotName.shadow,
26+
urdf_path=urdf_path,
27+
hand_type=HandType.right,
28+
retargeting_type=RetargetingType.vector,
29+
)
30+
with mj_viewer.launch_passive(mj_model, mj_data) as viewer:
31+
# Set up viewer camera
32+
viewer.cam.azimuth = 180
33+
viewer.cam.elevation = -30
34+
viewer.cam.distance = 1.0
35+
viewer.cam.lookat = [0.2, 0, 0.2]
36+
37+
while True:
38+
right_hand_state = np.array(xr_client.get_hand_tracking_state("right"))
39+
mediapipe_hand_state = pico_hand_state_to_mediapipe(right_hand_state)
40+
pin_q = dextracker.retarget(mediapipe_hand_state)
41+
mj_qpos = calc_mujoco_qpos_from_pin_q(mj_model, pin_model, pin_q)
42+
mj_ctrl = calc_mujoco_ctrl_from_qpos(mj_model, mj_qpos)
43+
mj_data.ctrl[:] = mj_ctrl
44+
mujoco.mj_step(mj_model, mj_data)
45+
viewer.sync()
46+
47+
48+
if __name__ == "__main__":
49+
main()

xrobotoolkit_teleop/utils/dex_hand_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def estimate_frame_from_hand_points(keypoint_3d_array: np.ndarray) -> np.ndarray
312312

313313
# Gram–Schmidt Orthonormalize
314314
x = x_vector - np.sum(x_vector * normal) * normal
315-
x = x / np.linalg.norm(x)
315+
x = x / (np.linalg.norm(x) + 1e-6)
316316
z = np.cross(x, normal)
317317

318318
# We assume that the vector from pinky to index is similar the z axis in MANO convention

0 commit comments

Comments
 (0)