File size: 7,058 Bytes
5f38e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
r"""Abstract base class, capability flags, and registry for external-physics adapters.

The scientific core is fully self-contained. Adapters here let the same
:class:`Robot`, :class:`Pose`, :class:`Wrench`, and trajectory objects be
pushed into an external physics engine (MuJoCo, PyBullet) or onto a ROS 2
transport for hardware-in-the-loop integration. The framework continues
working with zero adapters installed; only the registry probe changes its
output.

Lifecycle::

    with make_backend("mujoco", robot=ipanema_class()) as backend:
        backend.set_pose(initial_pose)
        for k in range(steps):
            backend.apply_wrench(W)
            backend.step(dt)
            state = backend.read_state()

The context-manager form guarantees ``close()`` runs even if the body
throws. Adapters that aren't capable of an operation declare it through
:attr:`PhysicsBackend.capabilities`; calling an unsupported method raises
:class:`NotImplementedError` with a message naming the backend and the
missing capability flag --- callers should consult capabilities first
rather than catch the exception.
"""

from __future__ import annotations

from abc import ABC, abstractmethod
from enum import Flag, auto
from typing import TYPE_CHECKING, Any, Literal

if TYPE_CHECKING:                                           # pragma: no cover
    from cdpr.core.frames import Pose, Wrench
    from cdpr.dynamics.rigid_body import PlatformState
    from cdpr.geometry.robot import Robot
    from numpy.typing import NDArray


BackendName = Literal["ros2", "gazebo", "mujoco", "pybullet", "isaac_sim"]


# ---------------------------------------------------------------------------
# Capability flags
# ---------------------------------------------------------------------------

class AdapterCapability(Flag):
    """Optional features an adapter may declare."""

    NONE = 0
    LOAD_ROBOT = auto()
    SET_POSE = auto()
    READ_STATE = auto()
    STEP_PHYSICS = auto()
    APPLY_WRENCH = auto()
    APPLY_CABLE_TENSIONS = auto()
    READ_CABLE_LENGTHS = auto()
    EXPORT_POSE_STREAM = auto()


# ---------------------------------------------------------------------------
# Base class
# ---------------------------------------------------------------------------

class PhysicsBackend(ABC):
    """Contract every external-physics adapter satisfies.

    Subclasses must:
    * set :attr:`name` to one of the registered backend names,
    * declare :attr:`capabilities` (a non-empty :class:`AdapterCapability`),
    * implement :meth:`load_robot` (called from ``__init__``),
    * implement whichever lifecycle methods their capabilities advertise.

    The class is a plain :class:`abc.ABC` --- no dataclass --- so subclasses
    are free to define their own state (e.g. native handles, observation
    buffers) without fighting decorator ordering.
    """

    name: BackendName

    def __init__(self, robot: "Robot") -> None:
        if not isinstance(self.capabilities, AdapterCapability):
            raise TypeError(
                f"{type(self).__name__} must set capabilities to an AdapterCapability."
            )
        if AdapterCapability.LOAD_ROBOT in self.capabilities:
            self.load_robot(robot)

    # --- declared by subclasses --------------------------------------

    @property
    @abstractmethod
    def capabilities(self) -> AdapterCapability: ...

    @abstractmethod
    def load_robot(self, robot: "Robot") -> None: ...

    # --- lifecycle (concrete defaults raise NotImplementedError) -----

    def set_pose(self, pose: "Pose") -> None:
        self._unsupported("set_pose", AdapterCapability.SET_POSE)

    def read_state(self) -> "PlatformState":
        self._unsupported("read_state", AdapterCapability.READ_STATE)

    def step(self, dt: float) -> None:
        self._unsupported("step", AdapterCapability.STEP_PHYSICS)

    def apply_wrench(self, wrench: "Wrench") -> None:
        self._unsupported("apply_wrench", AdapterCapability.APPLY_WRENCH)

    def apply_cable_tensions(self, tensions: "NDArray") -> None:
        self._unsupported("apply_cable_tensions", AdapterCapability.APPLY_CABLE_TENSIONS)

    def read_cable_lengths(self) -> "NDArray":
        self._unsupported("read_cable_lengths", AdapterCapability.READ_CABLE_LENGTHS)

    def close(self) -> None:
        """Release any native resources. Idempotent."""
        pass

    # --- context manager ---------------------------------------------

    def __enter__(self) -> "PhysicsBackend":
        return self

    def __exit__(self, exc_type, exc, tb) -> None:
        self.close()

    # --- helpers ------------------------------------------------------

    def _unsupported(self, op: str, flag: AdapterCapability) -> None:
        raise NotImplementedError(
            f"{self.name!r} adapter does not implement {op!r} "
            f"(missing capability {flag.name})."
        )

    def has(self, flag: AdapterCapability) -> bool:
        """Test for a capability flag."""
        return flag in self.capabilities


# ---------------------------------------------------------------------------
# Registry & factory
# ---------------------------------------------------------------------------

_BACKENDS: dict[BackendName, str] = {
    "ros2":      "cdpr.adapters.ros2",
    "gazebo":    "cdpr.adapters.gazebo",
    "mujoco":    "cdpr.adapters.mujoco",
    "pybullet":  "cdpr.adapters.pybullet",
    "isaac_sim": "cdpr.adapters.isaac_sim",
}


def available_backends() -> dict[BackendName, bool]:
    """Probe every registered backend without raising on missing SDKs."""
    import importlib
    status: dict[BackendName, bool] = {}
    for name, module_path in _BACKENDS.items():
        try:
            mod = importlib.import_module(module_path)
            status[name] = bool(getattr(mod, "IS_AVAILABLE", False))
        except Exception:
            status[name] = False
    return status


def make_backend(name: BackendName, *, robot: "Robot", **kwargs: Any) -> PhysicsBackend:
    """Construct the named backend, importing it lazily.

    Raises :class:`cdpr.core.exceptions.MissingAdapterDependency` if the
    backend module declares ``IS_AVAILABLE = False``.
    """
    import importlib
    from cdpr.core.exceptions import MissingAdapterDependency

    if name not in _BACKENDS:
        raise ValueError(f"Unknown backend {name!r}; choose from {list(_BACKENDS)}.")
    module = importlib.import_module(_BACKENDS[name])
    if not getattr(module, "IS_AVAILABLE", False):
        # Each stub module exposes a `_require` that raises with a hint.
        require = getattr(module, "_require", None)
        if require is not None:
            require()                                       # always raises
        raise MissingAdapterDependency(name, "see adapter module for install hint")
    cls = getattr(module, "Adapter", None)
    if cls is None:
        raise NotImplementedError(
            f"{name!r} backend reports IS_AVAILABLE=True but defines no Adapter class."
        )
    return cls(robot=robot, **kwargs)