Spaces:
Running
Running
Download tools/objects.py from VectorW/xlerobot-sim: direct link, hf CLI and curl.
- Browser
- Download file 5.05 kB
-
https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/objects.py
- Command line
-
hf download hf://spaces/VectorW/xlerobot-sim/tools/objects.py
-
curl -L -o objects.py https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/objects.py
5.05 kB
| """Small objects to pick up, shared by the generated scenes (tools/make_kitchen.py, tools/make_tabletop.py). | |
| The same shapes, masses and material names in every scene, so the page's renderers style them alike | |
| (src/render/materials.js) and the skills grasp them alike. Each function returns the MJCF lines of one | |
| free body standing (or lying) on a surface at height `z`, turned `yaw` radians about the vertical. | |
| """ | |
| import math | |
| # Round objects get rolling friction (condim 6): without it a sphere set rolling by the smallest | |
| # bounce keeps going. | |
| ROUND = 'condim="6" friction="1.2 0.02 0.002"' | |
| FLAT = 'condim="4" friction="1.2 0.05 0.01"' | |
| MATERIALS = { | |
| "apple": "0.75 0.1 0.08 1", "orange": "0.95 0.5 0.08 1", "cube": "0.95 0.78 0.1 1", "can": "0.8 0.1 0.15 1", | |
| "mug": "0.2 0.45 0.75 1", "bowl": "0.95 0.95 0.93 1", "plate": "0.97 0.97 0.95 1", "banana": "0.96 0.85 0.3 1", | |
| "cup": "0.3 0.7 0.5 1", "bottle": "0.15 0.45 0.3 1", "book": "0.6 0.15 0.2 1", | |
| "yogurt": "0.95 0.95 0.98 1", "juice_box": "0.95 0.55 0.1 1", "can_2": "0.15 0.4 0.8 1", "lemon": "0.98 0.88 0.25 1", | |
| } | |
| def f(*v: float) -> str: | |
| return " ".join(f"{x:.4g}" for x in v) | |
| def body(name: str, pos, geoms: list[str], mass: float, yaw: float = 0.0, shares: list[float] | None = None) -> list[str]: | |
| """A free body; its mass split among the geoms equally, or by `shares`.""" | |
| turn = f' quat="{f(math.cos(yaw / 2), 0, 0, math.sin(yaw / 2))}"' if yaw else "" | |
| lines = [f' <body name="{name}" pos="{f(*pos)}"{turn}>', " <freejoint/>"] | |
| shares = shares or [1 / len(geoms)] * len(geoms) | |
| for g, share in zip(geoms, shares): | |
| lines.append(f' {g.replace("/>", f" mass=\"{mass * share:.4g}\" contype=\"1\" conaffinity=\"1\"/>")}') | |
| lines.append(" </body>") | |
| return lines | |
| def apple(x, y, z, yaw=0.0, name="apple"): | |
| return body(name, (x, y, z + 0.025), [f'<geom type="sphere" size="0.025" material="apple" {ROUND}/>'], 0.08, yaw) | |
| def orange(x, y, z, yaw=0.0, name="orange"): | |
| return body(name, (x, y, z + 0.024), [f'<geom type="sphere" size="0.024" material="orange" {ROUND}/>'], 0.07, yaw) | |
| def cube(x, y, z, yaw=0.0, name="cube"): | |
| return body(name, (x, y, z + 0.02), [f'<geom type="box" size="0.02 0.02 0.02" material="cube" {FLAT}/>'], 0.04, yaw) | |
| def can(x, y, z, yaw=0.0, name="can", material="can"): | |
| return body(name, (x, y, z + 0.045), [f'<geom type="cylinder" size="0.025 0.045" material="{material}" {ROUND}/>'], 0.06, yaw) | |
| def lemon(x, y, z, yaw=0.0, name="lemon"): | |
| return body(name, (x, y, z + 0.022), [f'<geom type="sphere" size="0.022" material="lemon" {ROUND}/>'], 0.06, yaw) | |
| def mug(x, y, z, yaw=0.0, name="mug"): | |
| """A mug (9.5 cm tall) with its handle toward `yaw`: 5.9 cm across at most, handle and all, so the open | |
| gripper's fingers clear it whichever way it stands.""" | |
| return body(name, (x, y, z + 0.0475), [ | |
| f'<geom type="cylinder" size="0.026 0.0475" material="mug" {FLAT}/>', | |
| f'<geom type="box" pos="0.029 0 0.005" size="0.004 0.004 0.028" material="mug" {FLAT}/>', | |
| ], 0.08, yaw, shares=[0.93, 0.07]) # the handle is a small part of its weight | |
| def bowl(x, y, z, yaw=0.0, name="bowl"): | |
| """A bowl to put things in: a base and a ring of ten wall segments; too wide to pick up.""" | |
| ring = [] | |
| for k in range(10): | |
| a = 2 * math.pi * k / 10 | |
| ring.append(f'<geom type="box" pos="{f(0.062 * math.cos(a), 0.062 * math.sin(a), 0.028)}" euler="0 0 {a:.4f}" size="0.006 0.021 0.022" material="bowl" condim="4"/>') | |
| return body(name, (x, y, z + 0.003), ['<geom type="cylinder" pos="0 0 0.004" size="0.05 0.004" material="bowl" condim="4"/>', *ring], 0.3, yaw) | |
| def plate(x, y, z, yaw=0.0, name="plate"): | |
| """A plate to put things on; too wide to pick up.""" | |
| return body(name, (x, y, z + 0.01), ['<geom type="cylinder" size="0.1 0.006" material="plate" condim="4"/>'], 0.25, yaw) | |
| def banana(x, y, z, yaw=0.0, name="banana"): | |
| """A banana lying along `yaw`.""" | |
| return body(name, (x, y, z + 0.017), [f'<geom type="capsule" fromto="-0.06 0 0 0.06 0.012 0" size="0.017" material="banana" {ROUND}/>'], 0.1, yaw) | |
| def cup(x, y, z, yaw=0.0, name="cup"): | |
| return body(name, (x, y, z + 0.045), [f'<geom type="cylinder" size="0.03 0.045" material="cup" {ROUND}/>'], 0.06, yaw) | |
| def bottle(x, y, z, yaw=0.0, name="bottle"): | |
| return body(name, (x, y, z + 0.085), [f'<geom type="cylinder" size="0.03 0.085" material="bottle" {ROUND}/>'], 0.2, yaw) | |
| def book(x, y, z, yaw=0.0, name="book"): | |
| return body(name, (x, y, z + 0.012), ['<geom type="box" size="0.08 0.11 0.012" material="book" condim="4"/>'], 0.3, yaw) | |
| def yogurt(x, y, z, yaw=0.0, name="yogurt"): | |
| return body(name, (x, y, z + 0.035), [f'<geom type="cylinder" size="0.03 0.035" material="yogurt" {ROUND}/>'], 0.12, yaw) | |
| def juice_box(x, y, z, yaw=0.0, name="juice_box"): | |
| """A juice carton, its wide side along `yaw`.""" | |
| return body(name, (x, y, z + 0.06), [f'<geom type="box" size="0.03 0.022 0.06" material="juice_box" {FLAT}/>'], 0.2, yaw) | |