Spaces:
Running
Running
Download tools/make_g1.py from VectorW/xlerobot-sim: direct link, hf CLI and curl.
- Browser
- Download file 4.44 kB
-
https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/make_g1.py
- Command line
-
hf download hf://spaces/VectorW/xlerobot-sim/tools/make_g1.py
-
curl -L -o make_g1.py https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/make_g1.py
4.44 kB
| """Write the Unitree G1 robot, assets/robots/g1/, from mjswan's demo assets on Hugging Face. | |
| usage: python tools/make_g1.py | |
| mjswan (https://huggingface.co/ttktjmt/mjswan, Apache-2.0) ships a locomotion policy for the 29-DoF G1 | |
| trained with mjlab's velocity task, as ONNX with a JSON sidecar, and the MJCF it was trained against | |
| (MuJoCo Menagerie's G1, BSD-3-Clause, Unitree Robotics). This script downloads them at a pinned revision | |
| and writes the robot so MuJoCo runs the policy as mjlab trained it: | |
| - the 29 torque pass-through actuators become position actuators with the sidecar's stiffness (kp), | |
| damping (kv) and the original torque limits (forcerange), so MuJoCo's implicit integrator does the PD; | |
| - each joint's armature is mjlab's for its motor, which the gains were derived from: | |
| kp = armature * (2 pi * 10 Hz)^2, so armature = kp / (20 pi)^2; | |
| - a velocimeter and a gyro at the pelvis IMU site give the policy's base_lin_vel and base_ang_vel | |
| (mjlab's "robot/imu_lin_vel" and "robot/imu_ang_vel"); | |
| - the keyframe goes (scenes add objects, so its qpos would not fit); src/robots/g1.js poses the robot. | |
| The policy's observation (99 values) and action (29) are documented in src/robots/g1.js. | |
| """ | |
| import json | |
| import math | |
| import re | |
| import urllib.request | |
| from pathlib import Path | |
| REPO = "https://huggingface.co/ttktjmt/mjswan/resolve/64433677553ccb86c6bba76b5238f41da1f87523" | |
| OUT = Path(__file__).resolve().parents[1] / "assets" / "robots" / "g1" | |
| def fetch(path: str) -> bytes: | |
| with urllib.request.urlopen(f"{REPO}/{path}") as r: | |
| return r.read() | |
| def main() -> None: | |
| (OUT / "assets").mkdir(parents=True, exist_ok=True) | |
| config = json.loads(fetch("policies/locomotion.json")) | |
| (OUT / "locomotion.json").write_bytes(json.dumps(config, indent=2).encode() + b"\n") | |
| (OUT / "locomotion.onnx").write_bytes(fetch("policies/locomotion.onnx")) | |
| (OUT / "LICENSE").write_bytes(fetch("scenes/unitree_g1/LICENSE")) | |
| xml = fetch("scenes/unitree_g1/g1.xml").decode() | |
| meshes = sorted(set(re.findall(r'file="([^"]+)"', xml))) | |
| for name in meshes: | |
| target = OUT / "assets" / name | |
| if not target.exists(): | |
| target.write_bytes(fetch(f"scenes/unitree_g1/assets/{name}")) | |
| act = config["actions"]["joint_pos"] | |
| kp, kv = act["stiffness"], act["damping"] | |
| w = 2 * math.pi * 10.0 | |
| # Per-joint armature on each joint element. | |
| def joint(m: re.Match) -> str: | |
| tag = m.group(0) | |
| named = re.search(r'name="([^"]+)"', tag) | |
| if not named or named.group(1) not in kp: | |
| return tag # the default class, or a joint the policy does not drive | |
| armature = f'armature="{kp[named.group(1)] / w**2:.8f}"' | |
| if "armature=" in tag: | |
| return re.sub(r'armature="[^"]*"', armature, tag) | |
| return tag.replace("<joint ", f"<joint {armature} ", 1) | |
| xml = re.sub(r"<joint [^>]*/>", joint, xml) | |
| # Position actuators with the policy's gains and the original torque limits. | |
| def actuator(m: re.Match) -> str: | |
| name, joint_name, lo, hi = m.group(1), m.group(2), m.group(3), m.group(4) | |
| return f'<position name="{name}" joint="{joint_name}" kp="{kp[joint_name]:.6g}" kv="{kv[joint_name]:.6g}" forcerange="{lo} {hi}" inheritrange="1"/>' | |
| xml, n = re.subn(r'<general name="([^"]+)" joint="([^"]+)" ctrllimited="true" ctrlrange="([-\d.]+) ([-\d.]+)"/>', actuator, xml) | |
| assert n == 29, n | |
| xml = xml.replace("<!-- General actuators for direct torque control -->\n", "") | |
| xml = xml.replace("<!-- JS code handles PD control, so these are simple pass-through -->\n", "") | |
| sensors = ( | |
| ' <velocimeter site="imu_in_pelvis" name="imu_lin_vel"/>\n' | |
| ' <gyro site="imu_in_pelvis" name="imu_ang_vel"/>\n' | |
| ) | |
| xml = xml.replace(" <sensor>\n", " <sensor>\n" + sensors, 1) | |
| xml = re.sub(r"\s*<keyframe>.*?</keyframe>", "", xml, flags=re.S) | |
| header = ( | |
| "<!-- The Unitree G1 (29 DoF) for XLeRobot Sim, written by tools/make_g1.py from mjswan's demo assets\n" | |
| " (MuJoCo Menagerie's G1, BSD-3-Clause, see LICENSE): position actuators with the locomotion\n" | |
| " policy's gains, mjlab's joint armature, a pelvis velocimeter and gyro, no keyframe. -->\n" | |
| ) | |
| (OUT / "g1.xml").write_text(header + xml, encoding="utf-8") | |
| print(f"wrote {OUT / 'g1.xml'}: {len(meshes)} meshes, 29 position actuators") | |
| if __name__ == "__main__": | |
| main() | |