|
| 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() |
0 commit comments