File size: 5,054 Bytes
5fc5f70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d72771b
5fc5f70
 
 
 
 
 
 
d72771b
 
5fc5f70
 
d72771b
 
 
5fc5f70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d72771b
 
 
 
 
 
5fc5f70
 
 
d72771b
 
 
 
 
 
5fc5f70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""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)