|
| 1 | +"""Frame transformer sensor. |
| 2 | +
|
| 3 | +Outputs the pose of one or more target frames relative to a source frame, using forward |
| 4 | +kinematics over the link state already maintained by ``Articulation``. Both source and |
| 5 | +target frames support a configurable local-frame offset (position + quaternion) so a sensor |
| 6 | +can probe a specific point on a link (e.g. an end-effector grasp pose) without requiring the |
| 7 | +MJCF to expose a dedicated body. |
| 8 | +
|
| 9 | +Stateless: ``_compute_data`` reads ``rs.link_pos`` / ``rs.link_quat_w`` directly each tick |
| 10 | +and composes per-target offsets cached at ``bind`` time. No buffers, no integrators, no |
| 11 | +finite-difference — ``update`` and ``reset`` only need the base-class cache invalidation. |
| 12 | +""" |
| 13 | + |
| 14 | +from dataclasses import dataclass, field |
| 15 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +import torch |
| 18 | + |
| 19 | +from genelab.sensor.sensor import Sensor, SensorCfg |
| 20 | +from genelab.utils.math import quat_apply, quat_mul, subtract_frame_transforms |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from genelab.envs.manager_based_rl_env import ManagerBasedRlEnv |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class TargetFrameCfg: |
| 28 | + """A single target frame in a :class:`FrameTransformerSensorCfg`. |
| 29 | +
|
| 30 | + ``link_name`` resolves to a link index at ``bind``. ``offset_pos`` / ``offset_quat`` apply |
| 31 | + a rigid offset in the link's local frame so the same link can host multiple targets at |
| 32 | + different attachment sites. ``name`` is the user-facing identifier — defaults to |
| 33 | + ``link_name`` when blank — and is exposed via ``FrameTransformerSensor.target_names``. |
| 34 | + """ |
| 35 | + |
| 36 | + link_name: str = "" |
| 37 | + name: str = "" |
| 38 | + offset_pos: tuple[float, float, float] = (0.0, 0.0, 0.0) |
| 39 | + offset_quat: tuple[float, float, float, float] = (1.0, 0.0, 0.0, 0.0) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class FrameTransformerSensorCfg(SensorCfg): |
| 44 | + """Configuration for ``FrameTransformerSensor``. |
| 45 | +
|
| 46 | + ``source_link_name`` is the reference frame the targets are expressed in. ``target_frames`` |
| 47 | + is order-preserving — the output's ``N`` axis follows the cfg order so downstream |
| 48 | + observation terms can slice by position. |
| 49 | + """ |
| 50 | + |
| 51 | + source_link_name: str = "" |
| 52 | + source_offset_pos: tuple[float, float, float] = (0.0, 0.0, 0.0) |
| 53 | + source_offset_quat: tuple[float, float, float, float] = (1.0, 0.0, 0.0, 0.0) |
| 54 | + target_frames: tuple[TargetFrameCfg, ...] = field(default_factory=tuple) |
| 55 | + |
| 56 | + def build(self) -> "FrameTransformerSensor": |
| 57 | + return FrameTransformerSensor(self) |
| 58 | + |
| 59 | + |
| 60 | +@dataclass |
| 61 | +class FrameTransformerData: |
| 62 | + target_pos_w: torch.Tensor # (B, N, 3) target positions in world frame |
| 63 | + target_quat_w: torch.Tensor # (B, N, 4) target orientations in world frame |
| 64 | + target_pos_source: torch.Tensor # (B, N, 3) target positions in source frame |
| 65 | + target_quat_source: torch.Tensor # (B, N, 4) target orientations in source frame |
| 66 | + |
| 67 | + |
| 68 | +class FrameTransformerSensor(Sensor[FrameTransformerData]): |
| 69 | + def __init__(self, cfg: FrameTransformerSensorCfg) -> None: |
| 70 | + super().__init__(cfg) |
| 71 | + self._cfg_typed = cfg |
| 72 | + self._source_idx: int = -1 |
| 73 | + self._source_offset_pos: torch.Tensor | None = None |
| 74 | + self._source_offset_quat: torch.Tensor | None = None |
| 75 | + self._target_indices: torch.Tensor | None = None |
| 76 | + self._target_offsets_pos: torch.Tensor | None = None |
| 77 | + self._target_offsets_quat: torch.Tensor | None = None |
| 78 | + self._target_names: tuple[str, ...] = () |
| 79 | + |
| 80 | + @property |
| 81 | + def target_names(self) -> tuple[str, ...]: |
| 82 | + return self._target_names |
| 83 | + |
| 84 | + def bind(self, env: "ManagerBasedRlEnv") -> None: |
| 85 | + super().bind(env) |
| 86 | + if not self._cfg_typed.source_link_name: |
| 87 | + raise ValueError( |
| 88 | + f"FrameTransformerSensorCfg(name={self._cfg.name!r}) requires source_link_name" |
| 89 | + ) |
| 90 | + if not self._cfg_typed.target_frames: |
| 91 | + raise ValueError( |
| 92 | + f"FrameTransformerSensorCfg(name={self._cfg.name!r}) requires at least one target frame" |
| 93 | + ) |
| 94 | + try: |
| 95 | + self._source_idx = env.link_names.index(self._cfg_typed.source_link_name) |
| 96 | + except ValueError as exc: |
| 97 | + raise ValueError( |
| 98 | + f"sensor {self._cfg.name!r}: source link {self._cfg_typed.source_link_name!r} " |
| 99 | + f"not in env.link_names={env.link_names!r}" |
| 100 | + ) from exc |
| 101 | + unresolved: list[str] = [ |
| 102 | + t.link_name for t in self._cfg_typed.target_frames if t.link_name not in env.link_names |
| 103 | + ] |
| 104 | + if unresolved: |
| 105 | + raise ValueError( |
| 106 | + f"sensor {self._cfg.name!r}: target link(s) {unresolved!r} not in " |
| 107 | + f"env.link_names={env.link_names!r}" |
| 108 | + ) |
| 109 | + device = env.device |
| 110 | + self._source_offset_pos = torch.tensor( |
| 111 | + self._cfg_typed.source_offset_pos, dtype=torch.float32, device=device |
| 112 | + ) |
| 113 | + self._source_offset_quat = torch.tensor( |
| 114 | + self._cfg_typed.source_offset_quat, dtype=torch.float32, device=device |
| 115 | + ) |
| 116 | + self._target_indices = torch.tensor( |
| 117 | + [env.link_names.index(t.link_name) for t in self._cfg_typed.target_frames], |
| 118 | + dtype=torch.long, |
| 119 | + device=device, |
| 120 | + ) |
| 121 | + self._target_offsets_pos = torch.tensor( |
| 122 | + [list(t.offset_pos) for t in self._cfg_typed.target_frames], |
| 123 | + dtype=torch.float32, |
| 124 | + device=device, |
| 125 | + ) |
| 126 | + self._target_offsets_quat = torch.tensor( |
| 127 | + [list(t.offset_quat) for t in self._cfg_typed.target_frames], |
| 128 | + dtype=torch.float32, |
| 129 | + device=device, |
| 130 | + ) |
| 131 | + self._target_names = tuple( |
| 132 | + t.name if t.name else t.link_name for t in self._cfg_typed.target_frames |
| 133 | + ) |
| 134 | + |
| 135 | + def _compute_data(self) -> FrameTransformerData: |
| 136 | + assert ( |
| 137 | + self._env is not None |
| 138 | + and self._source_offset_pos is not None |
| 139 | + and self._source_offset_quat is not None |
| 140 | + and self._target_indices is not None |
| 141 | + and self._target_offsets_pos is not None |
| 142 | + and self._target_offsets_quat is not None |
| 143 | + ) |
| 144 | + rs = self._env.robot_state |
| 145 | + b = self._env.num_envs |
| 146 | + n = self._target_indices.shape[0] |
| 147 | + |
| 148 | + # Source pose in world frame, then composed with the source-local offset. |
| 149 | + src_link_pos = rs.link_pos[:, self._source_idx] # (B, 3) |
| 150 | + src_link_quat = rs.link_quat_w[:, self._source_idx] # (B, 4) |
| 151 | + src_offset_pos_b = self._source_offset_pos.unsqueeze(0).expand(b, 3).contiguous() |
| 152 | + src_offset_quat_b = self._source_offset_quat.unsqueeze(0).expand(b, 4).contiguous() |
| 153 | + source_pos_w = src_link_pos + quat_apply(src_link_quat, src_offset_pos_b) |
| 154 | + source_quat_w = quat_mul(src_link_quat, src_offset_quat_b) |
| 155 | + |
| 156 | + # Per-target link pose, then composed with the target-local offset. |
| 157 | + tgt_link_pos = rs.link_pos[:, self._target_indices] # (B, N, 3) |
| 158 | + tgt_link_quat = rs.link_quat_w[:, self._target_indices] # (B, N, 4) |
| 159 | + tgt_offset_pos_b = self._target_offsets_pos.unsqueeze(0).expand(b, n, 3).contiguous() |
| 160 | + tgt_offset_quat_b = self._target_offsets_quat.unsqueeze(0).expand(b, n, 4).contiguous() |
| 161 | + target_pos_w = tgt_link_pos + quat_apply(tgt_link_quat, tgt_offset_pos_b) |
| 162 | + target_quat_w = quat_mul(tgt_link_quat, tgt_offset_quat_b) |
| 163 | + |
| 164 | + # Express each target in the source frame: t_source = inv(source) @ t_target. |
| 165 | + source_pos_expanded = source_pos_w.unsqueeze(1).expand(b, n, 3).contiguous() |
| 166 | + source_quat_expanded = source_quat_w.unsqueeze(1).expand(b, n, 4).contiguous() |
| 167 | + target_pos_source, target_quat_source = subtract_frame_transforms( |
| 168 | + source_pos_expanded, source_quat_expanded, target_pos_w, target_quat_w |
| 169 | + ) |
| 170 | + return FrameTransformerData( |
| 171 | + target_pos_w=target_pos_w, |
| 172 | + target_quat_w=target_quat_w, |
| 173 | + target_pos_source=target_pos_source, |
| 174 | + target_quat_source=target_quat_source, |
| 175 | + ) |
0 commit comments