Instructions to use lerobot/asimov-mujoco with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LeRobot
How to use lerobot/asimov-mujoco with LeRobot:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Asimov
How to use lerobot/asimov-mujoco with Asimov:
# Run the policy in a browser simulation of the Asimov 1 humanoid (no GPU needed) git clone https://github.com/menloresearch/humanoid-policy-viewer cd humanoid-policy-viewer npm install npm run hf lerobot/asimov-mujoco
- Notebooks
- Google Colab
- Kaggle
Download tests/test_sim.py from lerobot/asimov-mujoco: direct link, hf CLI and curl.
- Browser
- Download file 5.75 kB
-
https://huggingface.co/lerobot/asimov-mujoco/resolve/main/tests/test_sim.py
- Command line
-
hf download hf://lerobot/asimov-mujoco/tests/test_sim.py
-
curl -L -o test_sim.py https://huggingface.co/lerobot/asimov-mujoco/resolve/main/tests/test_sim.py
5.75 kB
| """The sim against the real MuJoCo and the Edge protocol, headless and on spare ports. | |
| MUJOCO_GL=egl python -m unittest discover -s tests -v | |
| """ | |
| import json | |
| import socket | |
| import sys | |
| import time | |
| import unittest | |
| import urllib.request | |
| from pathlib import Path | |
| import mujoco | |
| import numpy as np | |
| import yaml | |
| from asimov_protocol.v1 import asimov_command_pb2, asimov_common_pb2, asimov_state_pb2 | |
| ROOT = Path(__file__).absolute().parent.parent | |
| sys.path.insert(0, str(ROOT)) | |
| from sim.asimov_edge_bridge import joints_to_motors # noqa: E402 | |
| from sim.simulator_factory import SimulatorFactory # noqa: E402 | |
| PORTS = {"COMMAND_PORT": 18850, "STATE_PORT": 18851, "VERSION_PORT": 13000} | |
| def wait_for(predicate, timeout=3.0): | |
| end = time.monotonic() + timeout | |
| while time.monotonic() < end: | |
| if predicate(): | |
| return True | |
| time.sleep(0.02) | |
| return False | |
| class AsimovSimTest(unittest.TestCase): | |
| def setUpClass(cls): | |
| config = yaml.safe_load((ROOT / "config.yaml").read_text()) | |
| config.update(PORTS, PRINT_SCENE_INFORMATION=False, SETPOINT_TIMEOUT_S=0.5) | |
| cls.config = config | |
| cls.state_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| cls.state_sock.bind(("127.0.0.1", PORTS["STATE_PORT"])) | |
| cls.state_sock.settimeout(1.0) | |
| cls.command_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| cls.sim = SimulatorFactory.create_simulator(config=config, onscreen=False, offscreen=False, camera_configs={}) | |
| cls.env = cls.sim.sim_env | |
| cls.model = cls.env.mj_model | |
| cls.sim.start_as_thread() | |
| def tearDownClass(cls): | |
| cls.sim.close() | |
| cls.state_sock.close() | |
| cls.command_sock.close() | |
| def latest_state(self) -> asimov_state_pb2.RobotState: | |
| self.state_sock.setblocking(False) | |
| msg = None | |
| try: | |
| while True: | |
| msg = self.state_sock.recv(65535) | |
| except BlockingIOError: | |
| pass | |
| finally: | |
| self.state_sock.settimeout(1.0) | |
| if msg is None: | |
| msg = self.state_sock.recv(65535) | |
| state = asimov_state_pb2.RobotState() | |
| state.ParseFromString(msg) | |
| return state | |
| def send(self, command: asimov_command_pb2.RobotCommand): | |
| self.command_sock.sendto(command.SerializeToString(), ("127.0.0.1", PORTS["COMMAND_PORT"])) | |
| def test_model_matches_config(self): | |
| self.assertEqual(self.model.nu, len(self.config["JOINT_NAMES"])) | |
| joints = [self.model.joint(self.model.actuator_trnid[i, 0]).name for i in range(self.model.nu)] | |
| self.assertEqual(joints, self.config["JOINT_NAMES"]) | |
| for name in ("head_camera", "left_wrist_cam", "right_wrist_cam", "global_view"): | |
| self.assertGreaterEqual(mujoco.mj_name2id(self.model, mujoco.mjtObj.mjOBJ_CAMERA, name), 0, name) | |
| def test_robot_in_menlo_livery(self): | |
| torso = self.model.body("waist_yaw_link").id | |
| materials = { | |
| self.model.material(self.model.geom_matid[g]).name | |
| for g in range(self.model.ngeom) | |
| if self.model.geom_bodyid[g] == torso and self.model.geom_type[g] == mujoco.mjtGeom.mjGEOM_MESH | |
| } | |
| self.assertEqual(materials, {"asimov_orange", "asimov_black"}) | |
| def test_version_endpoint(self): | |
| url = f"http://127.0.0.1:{PORTS['VERSION_PORT']}/api/version" | |
| facts = json.loads(urllib.request.urlopen(url, timeout=2).read()) | |
| self.assertEqual(facts, {"robot_model": "asimov_1", "robot_os_version": self.config["ROBOT_OS_VERSION"]}) | |
| def test_state_and_trajectory_then_damp(self): | |
| state = self.latest_state() | |
| self.assertEqual(len(state.joint_pos), 25) | |
| self.assertEqual(state.current_mode, asimov_common_pb2.CONTROL_MODE_STAND) | |
| np.testing.assert_allclose(state.projected_gravity, [0, 0, -1], atol=0.1) | |
| np.testing.assert_allclose( | |
| state.joint_pos, joints_to_motors(self.config["STAND_POSE"]), atol=0.15 | |
| ) | |
| command = asimov_command_pb2.RobotCommand(sequence=7, protocol_version=1) | |
| command.all_trajectory.positions.extend(self.config["STAND_POSE"]) | |
| command.all_trajectory.kp.extend(self.config["STAND_KP"]) | |
| command.all_trajectory.kd.extend(self.config["STAND_KD"]) | |
| self.send(command) | |
| self.assertTrue( | |
| wait_for(lambda: self.latest_state().current_mode == asimov_common_pb2.CONTROL_MODE_MOVE) | |
| ) | |
| self.assertEqual(self.latest_state().last_cmd_sequence, 7) | |
| self.assertTrue( | |
| wait_for(lambda: self.latest_state().current_mode == asimov_common_pb2.CONTROL_MODE_DAMP, timeout=2.0) | |
| ) | |
| self.send(asimov_command_pb2.RobotCommand(mode=asimov_common_pb2.CONTROL_MODE_STAND, sequence=8)) | |
| self.assertTrue( | |
| wait_for(lambda: self.latest_state().current_mode == asimov_common_pb2.CONTROL_MODE_STAND) | |
| ) | |
| def test_band_holds_the_robot(self): | |
| self.assertTrue(self.env.elastic_band.enable) | |
| time.sleep(1.0) | |
| height = self.env.mj_data.qpos[2] | |
| self.assertGreater(height, 0.6) | |
| self.assertLess(height, self.config["BAND_HEIGHT"] - 0.01) | |
| class AnkleCouplingTest(unittest.TestCase): | |
| def test_pitch_and_roll_to_motors(self): | |
| joints = np.zeros(25) | |
| joints[4], joints[5] = 0.1, 0.0 | |
| motors = joints_to_motors(joints) | |
| self.assertAlmostEqual(motors[4], 0.202) | |
| self.assertAlmostEqual(motors[5], -0.202) | |
| joints[4], joints[5] = 0.0, 0.1 | |
| motors = joints_to_motors(joints) | |
| self.assertAlmostEqual(motors[4], -0.08) | |
| self.assertAlmostEqual(motors[5], -0.08) | |
| if __name__ == "__main__": | |
| unittest.main() | |