File size: 14,299 Bytes
578663d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""BaseRobot — the actor (embodiment).



A robot declares its assets + the control modes it supports, and how to read/write its own state.

The per-step action→sim pipeline is handled **generically through a controller**: `bind()`

orchestrates the lifecycle — bind the robot to the env, acquire its sim handles, then build + bind a

controller *to the robot* — and `apply_action` / `action_dim` delegate to that controller. A concrete

robot only fills in **hooks**: `on_bind` (grab handles), `build_controller` (the controller for the

active `control_mode`), and — unless it's a single articulation — `actuator_sink` / `actuator_limits`.

It does not re-implement the lifecycle. Controller-less robots (NullRobot, force-driven debug actors)

just leave `build_controller` returning None.



The env is open, so an agent can drop in its own controller at runtime with `set_controller(...)`

(or override `apply_action` for a fully custom path). Each controller writes its own command to sim

through the `actuator_sink` it captured at bind, so a composite can mix command types per joint group.



Heavy imports are deferred so this module imports without AppLauncher.

"""

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, ClassVar

from .config import BaseCfg

if TYPE_CHECKING:
    import torch

    from .controller import BaseController
    from .env import BaseEnv


@dataclass
class BaseRobotCfg(BaseCfg):
    """Thin shared base for robot configs: carries only the one universal selectable, the

    `control_mode`. Concrete robots subclass this and add their own asset / partial body variant (e.g. hands) /

    `fix_root_link` / ee-frame / gains — morphologies differ too much for a fat shared cfg."""

    #: Which of the robot's `control_modes` to use; "" -> the first one the robot declares. The agent
    #: may switch it (a new actuation of the same hardware).
    control_mode: str = ""  # active control mode; '' selects the robot's first

    #: The robot's scene namespace: its asset key in `env.iscene` and (capitalized) its prim name —
    #: "robot" -> `iscene["robot"]` at `{ENV_REGEX_NS}/Robot`. Single-robot envs keep the default; a
    #: composite parent (`MultiRobot`) stamps each child's name ("left", "right", ...) so several
    #: robots coexist in one scene without key/prim collisions. Concrete robots read it through
    #: `BaseRobot.name` / `BaseRobot.prim_name` rather than hardcoding "robot".
    name: str = field(default="robot", kw_only=True)  # scene-asset key / prim namespace for this robot


class BaseRobot(ABC):
    #: Control modes this embodiment supports (e.g. ("joint", "ee_pose", "osc_impedance")).
    #: Declared per concrete robot; the active one is `self.control_mode`.
    control_modes: tuple[str, ...] = ()

    #: Named EGO viewpoints for visual replay (data_engine render.py) — embodiment knowledge:
    #: which body is the wrist, where a lens clears the fingers. Each entry: {"link": <body prim
    #: name>, "eye": (x,y,z), "target": (x,y,z), "focal": mm}, eye/target in the LINK frame; the
    #: camera mounts under that link and rides it. External scene views live on the SCENE's
    #: `CAMERAS` instead.
    CAMERAS: ClassVar[dict[str, dict]] = {}

    def __init__(self, cfg: Any) -> None:
        self.cfg = cfg
        self._env: BaseEnv | None = None
        self.control_mode: str | None = getattr(cfg, "control_mode", None) or (
            self.control_modes[0] if self.control_modes else None
        )
        #: The active controller, built in `bind()` (None for a controller-less robot). The agent may
        #: swap it at runtime via `set_controller()`.
        self.controller: BaseController | None = None
        #: The robot's main articulation handle — set in `on_bind` for the common single-articulation
        #: case (the default `actuator_sink` / `actuator_limits` use it). None until bound.
        self.articulation: Any = None

    # ----- naming (the robot's scene namespace; see BaseRobotCfg.name) --------------------------
    @property
    def name(self) -> str:
        """This robot's scene-asset key (`cfg.name`; "robot" for a plain single-robot env). Concrete

        robots use it in `assets()` and `on_bind` (`env.iscene[self.name]`) so a composite parent can

        re-namespace them just by stamping `cfg.name`."""
        return getattr(self.cfg, "name", None) or "robot"

    @property
    def prim_name(self) -> str:
        """USD prim name under `{ENV_REGEX_NS}`: `cfg.name` with the first letter upper-cased

        ("robot" -> "Robot", "left" -> "Left"), so default single-robot prim paths stay unchanged."""
        n = self.name
        return n[:1].upper() + n[1:]

    # ----- assets / state / description (the robot-specific contract) ---------------------------
    @abstractmethod
    def assets(self) -> dict[str, Any]:
        """`{name: cfg}` for the robot prim(s) (ArticulationCfg / RigidObjectCfg / …)."""

    @abstractmethod
    def reset(self, env_ids: torch.Tensor) -> None:
        """Reset the robot to its home configuration for `env_ids` (also reset `self.controller`)."""

    def get_state(self, env_ids: torch.Tensor) -> dict[str, Any]:
        """Full restorable state, uniform across single-articulation robots: the articulation's

        sim-resident state (root, joint pos/vel, and the actuator setpoints) plus the controller's own

        state (delegated, so it travels with whatever controller is bound).



        To ADD robot-specific state, extend rather than replace — call `super().get_state(env_ids)` for

        this uniform part, add your own keys, and restore them after `super().set_state(...)`:

            def get_state(self, ids):  s = super().get_state(ids); s["tool"] = ...; return s

            def set_state(self, s, ids):  super().set_state(s, ids); self._tool.write_(s["tool"])

        `set_state` reads only the base keys, so it ignores any extra keys a subclass adds (no clash).

        Override fully only if the robot isn't a single articulation (e.g. multi-body)."""
        state: dict[str, Any] = {}
        art = self.articulation
        if art is not None:
            d = art.data
            state["root"] = d.root_state_w[env_ids].clone()
            state["joint_pos"] = d.joint_pos[env_ids].clone()
            state["joint_vel"] = d.joint_vel[env_ids].clone()
            state["joint_pos_target"] = d.joint_pos_target[env_ids].clone()
            state["joint_effort_target"] = d.joint_effort_target[env_ids].clone()
        if self.controller is not None:
            state["controller"] = self.controller.get_state(env_ids)
        return state

    def set_state(self, state: dict[str, Any], env_ids: torch.Tensor) -> None:
        """Restore what `get_state` returned: write the articulation's root + joint state and re-apply

        the actuator setpoints (so a restored robot resumes its commanded motion), then restore the

        controller's state."""
        art = self.articulation
        if art is not None:
            art.write_root_state_to_sim(state["root"], env_ids)
            art.write_joint_state_to_sim(state["joint_pos"], state["joint_vel"], env_ids=env_ids)
            art.set_joint_position_target(state["joint_pos_target"], env_ids=env_ids)
            art.set_joint_effort_target(state["joint_effort_target"], env_ids=env_ids)
        if self.controller is not None and "controller" in state:
            self.controller.set_state(state["controller"], env_ids)

    @abstractmethod
    def describe(self) -> str:
        """**Natural-language** description of the embodiment + its control, for the agent."""

    # ----- lifecycle: env -> robot -> controller (concrete; override the HOOKS, not bind) -------
    def bind(self, env: BaseEnv) -> None:
        """Bind the robot to `env`, then build + bind its controller **to the robot**. The chain:

        the env builds the scene, calls this once → it caches the env, `on_bind` grabs sim handles,

        `build_controller` makes the controller for the active mode, and `set_controller` binds it to

        this robot. Override the hooks (`on_bind` / `build_controller`, or `actuator_sink` /

        `actuator_limits` for non-articulation robots), not this."""
        self._env = env
        self.on_bind(env)
        controller = self.build_controller()
        if controller is not None:
            self.set_controller(controller)

    def on_bind(self, env: BaseEnv) -> None:
        """Hook: grab sim handles once after build (e.g. `self.articulation = env.iscene["robot"]`).

        Runs before the controller is built, so the controller can resolve its joints against the

        handle. No-op by default."""

    def build_controller(self) -> BaseController | None:
        """Hook: the controller for the active `control_mode` (its `joint_ids` / solver are resolved

        when it is bound, in `set_controller`). Return None for a controller-less robot (default)."""
        return None

    def set_controller(self, controller: BaseController) -> None:
        """Bind `controller` to this robot and install it as the active one. The **supported seam**

        for an agent to drop in its own controller at runtime —

        `env.robot.set_controller(MyController(...))` — which takes effect on the next `step()` (the

        env dispatches through `self.robot`). Bypasses `control_modes`: a custom controller need not

        be in the menu."""
        controller.bind(self)
        self.controller = controller

    # ----- action + control rate (generic, via the active controller) ---------------------------
    @property
    def action_dim(self) -> int:
        """Width of the action vector = the active controller's (0 if there is none)."""
        return self.controller.action_dim if self.controller is not None else 0

    @property
    def control_period(self) -> int:
        """Physics substeps the env runs per `env.step` = the active controller's `control_period`

        (1 if there is none, i.e. control at sim rate). For a composite this is its slowest leaf."""
        return self.controller.control_period if self.controller is not None else 1

    def control_dt(self, controller: BaseController) -> float | None:
        """Hook: a robot-level control period (s) that OVERRIDES `controller.cfg.dt`; None -> defer to

        the controller. Takes the controller so a robot can pin per-leaf rates of a composite. Read at

        bind and turned into an integer `control_period`."""
        return None

    def apply_action(self, action: torch.Tensor, substep: int = 0) -> None:
        """Run the active controller, which computes **and writes** its command (no-op without one).

        `substep` is the physics-step index in the `env.step` window; the controller fires only on its

        own subdivision (see `BaseController.apply`). Override for a fully custom action path.



        `action` may be any array-like (numpy / list / torch): controllers assume a float32 torch

        tensor on the sim device, so it is coerced here — the one choke point every path shares

        (`env.step`, direct calls). Agent-written code passes numpy, which used to surface as a

        baffling numpy↔cuda-tensor TypeError deep in the controller's EMA math. A no-op (same

        object back) when the input is already a float32 tensor on the sim device."""
        if self.controller is not None:
            import torch

            action = torch.as_tensor(action, dtype=torch.float32, device=self.env.device)
            if action.dim() == 1:  # tolerate a single unbatched action vector
                action = action.unsqueeze(0)
            self.controller.apply(action, substep)

    def actuator_sink(self, command_type: str):
        """The **write path** for a controller's `command_type`: a callable `(command, joint_ids) ->

        None` that sends `command` to sim through the matching joint-target setter. A controller

        captures this once at bind and writes through it itself — so a `composite` can mix command

        types (effort arms + position hands), each leaf writing its own group. Default uses

        `self.articulation`; override for multi-body or non-articulation robots."""
        art = self.articulation
        setter = {
            "position": art.set_joint_position_target,
            "velocity": art.set_joint_velocity_target,
            "effort": art.set_joint_effort_target,
        }[command_type]
        return lambda command, joint_ids: setter(command, joint_ids=joint_ids)

    def actuator_limits(self, joint_ids: Any) -> dict[str, Any]:
        """Per-joint actuation bounds for `joint_ids`, handed to a controller at bind (`self.limits`)

        so it can clamp / scale / validate — most controllers ignore them and stay general. A snapshot

        from the articulation (re-query for live values). Keys: ``pos`` (n, k, 2 = lower/upper),

        ``vel`` (n, k), ``effort`` (n, k). Override for non-articulation robots."""
        d = self.articulation.data
        return {
            "pos": d.joint_pos_limits[:, joint_ids, :],
            "vel": d.joint_vel_limits[:, joint_ids],
            "effort": d.joint_effort_limits[:, joint_ids],
        }

    @property
    def env(self) -> BaseEnv:
        """The env this robot is bound to. Available after `bind()`; raises if accessed before."""
        if self._env is None:
            raise RuntimeError("robot is not bound to an env yet (call happens before bind())")
        return self._env

    def post_step(self, env_ids: torch.Tensor | None = None) -> None:
        """Step-coupled robot bookkeeping, run by the env **once per physics substep** (after the sim

        advances), so it tracks every physics step even under control decimation. Default no-op.

        Override for things that must track the new state every step (e.g. advancing a controller's

        internal target/integrator). Not for the agent to call."""