asimov-mujoco / sim /robot_model.py
nepyope's picture
Locate repository files without resolving Hub cache symlinks
9c1f418 verified
Raw History Blame Contribute Delete
3.67 kB
"""The simulated robot: Asimov 1 from the lerobot/robot-urdfs dataset, adapted for this sim.
The dataset holds Menlo's MJCF in Menlo's livery as a standalone viewer scene: its own
floor, lights and skyboxes, and no actuators (training adds them in Python). This
writes the robot alone, as the G1 robot files are, next to a copy of the scene in
``assets/``, which adds the world:
* meshes resolve from the downloaded dataset
* floor, lights, skyboxes and ground textures removed
* one torque motor per joint, in firmware order, limited to the joint's actuatorfrcrange
* head and wrist cameras, the names the G1 sim publishes
"""
import shutil
import tempfile
import xml.etree.ElementTree as ET # nosec B405
from pathlib import Path
from huggingface_hub import snapshot_download
# Not resolved: in the Hugging Face cache, repository files are symlinks into blobs/.
ROOT = Path(__file__).absolute().parent.parent
# Camera looks along its local -Z. right = -Y, up = +Z looks along the parent +X.
LOOK_FORWARD = "0 -1 0 0 0 1"
CAMERAS = (
# name, parent body, position in the parent, vertical field of view
("head_camera", "neck_pitch_link", "0.09 0 0.06", "90"),
("left_wrist_cam", "left_wrist_yaw_link", "0.09 0 0", "95"),
("right_wrist_cam", "right_wrist_yaw_link", "0.09 0 0", "95"),
)
WORLD_TEXTURES = {"groundplane", "texplane", ""}
WORLD_MATERIALS = {"groundplane"}
def download_robot(config: dict) -> Path:
"""The robot's folder (xml/, meshes/) at the pinned dataset revision, from the Hub cache."""
folder = config["ROBOT_MODEL_PATH"]
root = snapshot_download(
config["ROBOT_MODEL_REPO"],
repo_type="dataset",
revision=config["ROBOT_MODEL_REVISION"],
allow_patterns=[f"{folder}/xml/*", f"{folder}/meshes/*"],
)
return Path(root) / folder
def robot_mjcf(config: dict, robot_dir: Path) -> ET.ElementTree:
tree = ET.parse(robot_dir / "xml" / f"{config['ROBOT_TYPE']}.xml") # nosec B314
root = tree.getroot()
root.set("name", config["ROBOT_TYPE"])
root.find("compiler").set("meshdir", str(robot_dir / "meshes"))
for visual in root.findall("visual"):
root.remove(visual)
asset = root.find("asset")
for texture in asset.findall("texture"):
if texture.get("type") == "skybox" or texture.get("name", "") in WORLD_TEXTURES:
asset.remove(texture)
for material in asset.findall("material"):
if material.get("name") in WORLD_MATERIALS:
asset.remove(material)
worldbody = root.find("worldbody")
for child in list(worldbody):
if child.tag in ("light", "geom"):
worldbody.remove(child)
bodies = {body.get("name"): body for body in root.iter("body")}
for name, parent, pos, fovy in CAMERAS:
ET.SubElement(bodies[parent], "camera", name=name, pos=pos, xyaxes=LOOK_FORWARD, fovy=fovy)
joints = {joint.get("name"): joint for joint in root.iter("joint")}
actuator = ET.SubElement(root, "actuator")
for name in config["JOINT_NAMES"]:
low, high = joints[name].get("actuatorfrcrange").split()
ET.SubElement(actuator, "motor", name=name.removesuffix("_joint"), joint=name, ctrlrange=f"{low} {high}")
return tree
def scene_path(config: dict) -> Path:
"""Write the sim's robot MJCF beside a copy of ROBOT_SCENE and return that scene."""
robot_dir = download_robot(config)
scene = ROOT / config["ROBOT_SCENE"]
out = Path(tempfile.mkdtemp(prefix="asimov-mujoco-"))
shutil.copy(scene, out / scene.name)
robot_mjcf(config, robot_dir).write(out / f"{config['ROBOT_TYPE']}.xml", encoding="unicode")
return out / scene.name