File size: 4,364 Bytes
3ea69ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Grader for the pc_gpu scene: the graphics card seated in the PCIe x16 slot.

Rubric stages (weights and modes live in RUBRIC):
    grasped  the card has been taken in the weld-on-closure grip (`scene.grasp_held`) — the
             scene's own grasp state; a milestone, latched. Also true once the card is
             aligned in the slot (a card in the slot was necessarily carried there), so the
             rung nests under `aligned`. Under an embodiment with no gripper (the weld
             contract disabled, e.g. robot="null") the grasp state does not exist and the
             rung reads from that implication alone
    aligned  the card lined up on the slot — origin within `align_xy` of the seated point,
             up axis within `align_axis_deg` of the slot axis, length axis within
             `align_yaw_deg` of the slot heading: the scene's own three non-depth seat gates
    pressed  tab depth below the slot mouth (`slot_mouth_z`, the scene's own `engaged()`)
             as a fraction of `seat_depth`, counted only while aligned — so it is 1.0 iff
             the scene calls the card seated
Every stage is per env. The rungs nest (seated => aligned => grasped), so progress reaches
1.0 exactly when the card is seated, and for a grader built after the pick as well as one
that watched it. The rear-panel pass-through is NOT a rung: the cutout's geometry is not in
the scene cfg, so it cannot be read from state without an invented tolerance. Success is
the scene's `seated()` per env — depth, xy, tilt AND heading gates — read when the
delivery finishes.
"""

from __future__ import annotations

import math

from robobench.core import BaseGrader
# baked by data_engine init: grade against the LOCAL scene copy (../scene/scene.py),
# exec'd at most once per process under a fixed module key.
import importlib.util as _ilu
import sys as _sys
from pathlib import Path as _Path

if "datagen_local_scene" not in _sys.modules:
    import robobench as _rb
    _rb.discover()
    _p = _Path(__file__).resolve().parent.parent / "scene" / "scene.py"
    _spec = _ilu.spec_from_file_location("datagen_local_scene", _p)
    _m = _ilu.module_from_spec(_spec)
    _sys.modules["datagen_local_scene"] = _m
    _spec.loader.exec_module(_m)
PcGpuAssemblyScene = _sys.modules["datagen_local_scene"].PcGpuAssemblyScene


class PcGpuAssemblyGrader(BaseGrader):
    """The card seated in its slot — pressed to depth, centred, upright, heading along the slot.

    Ladder: grasped 0.33 · aligned 0.67 · pressed 1.00 — three equal rungs for the three
    steps of the task (take the card, line it up on the slot, press it home); the last rung
    is a linear ramp of tab depth from the slot mouth (`slot_mouth_z`) to `seat_depth`.
    """

    SCENE = PcGpuAssemblyScene
    RUBRIC = (("grasped", 1, "once"), ("aligned", 1), ("pressed", 1))
    scene: PcGpuAssemblyScene

    def setup(self) -> None:
        import torch

        # `grasp_held` exists only while the weld contract is live (a gripper on the stage).
        self._has_grasp = hasattr(self.scene, "grasp_held")
        self._grasped = torch.zeros(self.num_envs, dtype=torch.bool,
                                    device=self.scene.engaged().device)  # latch: card ever held

    def check_success(self):
        return self.scene.seated()  # (num_envs,)

    def _aligned(self):
        """(num_envs,) bool: the scene's xy + tilt + heading seat gates, without depth."""
        c = self.scene.cfg
        rel = self.scene._card_offset_in_case()  # (n, 3), zero at the seated pose — privileged
        xy_ok = rel[:, :2].norm(dim=-1) <= c.align_xy
        up_ok = self.scene._axis_cos(2) >= math.cos(math.radians(c.align_axis_deg))
        yaw_ok = self.scene._axis_cos(0) >= math.cos(math.radians(c.align_yaw_deg))
        return xy_ok & up_ok & yaw_ok

    # ---- rubric stages — each returns a (num_envs,) value in [0, 1] ---------------------
    def grasped(self):
        if self._has_grasp:
            self._grasped |= self.scene.grasp_held.any(dim=1)
        return (self._grasped | self._aligned()).float()

    def aligned(self):
        return self._aligned().float()

    def pressed(self):
        depth = self.scene.engaged()  # (n,) tab depth below the slot mouth (m)
        return self._aligned() * (depth / self.scene.cfg.seat_depth).clamp(0.0, 1.0)