|
3 | 3 |
|
4 | 4 | """Task/pair retargeting configuration (the retarget-descriptor YAML schema). |
5 | 5 |
|
6 | | -The descriptor is now self-contained: it carries its own task structure (a per-EEF list of |
7 | | -:class:`Subtask`) instead of referencing an external task descriptor. Each subtask declares how it ends |
8 | | -(:class:`SubtaskEnd` -- a gripper or signal event, offsettable) and, optionally, which object the EEF |
9 | | -tracks over it (:class:`SubtaskObjectTracking`), so segmentation and object-centric planning come |
| 6 | +The descriptor is self-contained: it carries its own task structure (a per-EEF list of core |
| 7 | +:class:`~autodata_interfaces.tasks.subtask_spec.Subtask`, with retarget-specific fields in |
| 8 | +:class:`RetargetSubtaskAlgoParams`) instead of referencing an external task descriptor. Each subtask declares |
| 9 | +how it ends (:class:`SubtaskEnd` -- a gripper or signal event, offsettable) and, optionally, which object the |
| 10 | +EEF tracks over it (:class:`SubtaskObjectTracking`), so segmentation and object-centric planning come |
10 | 11 | straight from this file. Retargeting replays each recorded source trajectory 1:1 onto the target robot. |
11 | 12 | """ |
12 | 13 |
|
|
15 | 16 | from pathlib import Path |
16 | 17 | from typing import Any |
17 | 18 |
|
| 19 | +from autodata_interfaces.tasks.subtask_spec import Subtask, SubtaskAlgoParams |
18 | 20 | from autodata_utils.yaml_utils import UniqueKeySafeLoader |
19 | 21 |
|
20 | 22 |
|
@@ -218,47 +220,58 @@ def parse(cls, value: Any) -> "Offset | None": |
218 | 220 |
|
219 | 221 |
|
220 | 222 | @dataclass |
221 | | -class Subtask: |
222 | | - """One subtask in an EEF's sequence. |
| 223 | +class RetargetSubtaskAlgoParams(SubtaskAlgoParams): |
| 224 | + """Retargeting-specific parameters of a core :class:`~autodata_interfaces.tasks.subtask_spec.Subtask`. |
| 225 | +
|
| 226 | + A retarget subtask is a core ``Subtask`` (``object_ref``, ``description``) whose ``algo_params`` is an |
| 227 | + instance of this class. ``subtask_end`` generalizes the core ``subtask_term_signal``: besides a signal |
| 228 | + edge it can end on a source gripper event or a fixed length, with an offset. |
223 | 229 |
|
224 | 230 | ``name`` (optional) is a stable identifier for the subtask; when given it must be unique across all |
225 | | - subtasks of all EEFs. ``object_ref`` / ``frame_ref`` name the object or frame the subtask is planned |
226 | | - relative to; they default the reference frame a per-subtask :class:`Offset` acts in. ``subtask_end`` is |
227 | | - required except on the last subtask of an EEF (which runs to the end of the trajectory). |
228 | | - ``object_tracking`` (optional) makes the EEF track an object over the whole subtask instead of |
229 | | - following the source EEF path. |
| 231 | + subtasks of all EEFs. ``frame_ref`` names the frame the subtask is planned relative to when it has no |
| 232 | + ``object_ref``; together they default the reference frame a per-subtask :class:`Offset` acts in. |
| 233 | + ``subtask_end`` is required except on the last subtask of an EEF (which runs to the end of the |
| 234 | + trajectory). ``object_tracking`` (optional) makes the EEF track an object over the whole subtask instead |
| 235 | + of following the source EEF path. ``offset`` (optional) is a per-subtask SE(3) pose offset applied to |
| 236 | + this EEF's commanded trajectory over the subtask's span. |
230 | 237 | """ |
231 | 238 |
|
232 | 239 | name: str | None = None |
233 | | - object_ref: str | None = None |
234 | 240 | frame_ref: str | None = None |
235 | | - description: str = "" |
236 | 241 | subtask_end: SubtaskEnd | None = None |
237 | 242 | object_tracking: SubtaskObjectTracking | None = None |
238 | | - # Per-subtask SE(3) pose offset applied to this EEF's commanded trajectory over the subtask's span |
239 | | - # (see :class:`Offset`). None = no offset. |
240 | | - offset: "Offset | None" = None |
| 243 | + offset: Offset | None = None |
241 | 244 |
|
242 | | - @classmethod |
243 | | - def parse(cls, value: dict) -> "Subtask": |
244 | | - """Parse one subtask and its nested descriptors. |
245 | 245 |
|
246 | | - Args: |
247 | | - value: Mapping of subtask field values. |
| 246 | +# Core ``Subtask`` fields a retarget descriptor may set; the rest drive generation-time segment selection |
| 247 | +# and noise, which retargeting (a 1:1 replay) does not use, so they are rejected rather than ignored. |
| 248 | +_CORE_SUBTASK_KEYS = ("object_ref", "description") |
248 | 249 |
|
249 | | - Returns: |
250 | | - Parsed subtask descriptor. |
251 | | - """ |
252 | | - assert isinstance(value, dict), f"each subtask must be a section (dict), got {type(value).__name__}." |
253 | | - data = dict(value) |
254 | | - allowed = {f.name for f in fields(cls)} |
255 | | - unknown = set(data) - allowed |
256 | | - assert not unknown, f"unknown subtask keys {sorted(unknown)}; allowed: {sorted(allowed)}." |
257 | | - if data.get("subtask_end") is not None: |
258 | | - data["subtask_end"] = SubtaskEnd.parse(data["subtask_end"]) |
259 | | - data["object_tracking"] = SubtaskObjectTracking.parse(data.get("object_tracking")) |
260 | | - data["offset"] = Offset.parse(data.get("offset")) |
261 | | - return cls(**data) |
| 250 | + |
| 251 | +def parse_subtask(value: dict) -> Subtask: |
| 252 | + """Parse one descriptor subtask into a core :class:`Subtask` with :class:`RetargetSubtaskAlgoParams`. |
| 253 | +
|
| 254 | + The descriptor keeps a flat layout: ``object_ref`` / ``description`` fill the core fields, and every |
| 255 | + other key fills the retarget ``algo_params``. |
| 256 | +
|
| 257 | + Args: |
| 258 | + value: Mapping of subtask field values. |
| 259 | +
|
| 260 | + Returns: |
| 261 | + Parsed subtask. |
| 262 | + """ |
| 263 | + assert isinstance(value, dict), f"each subtask must be a section (dict), got {type(value).__name__}." |
| 264 | + algo_keys = {f.name for f in fields(RetargetSubtaskAlgoParams)} |
| 265 | + allowed = set(_CORE_SUBTASK_KEYS) | algo_keys |
| 266 | + unknown = set(value) - allowed |
| 267 | + assert not unknown, f"unknown subtask keys {sorted(unknown)}; allowed: {sorted(allowed)}." |
| 268 | + algo = {key: value[key] for key in algo_keys if key in value} |
| 269 | + if algo.get("subtask_end") is not None: |
| 270 | + algo["subtask_end"] = SubtaskEnd.parse(algo["subtask_end"]) |
| 271 | + algo["object_tracking"] = SubtaskObjectTracking.parse(algo.get("object_tracking")) |
| 272 | + algo["offset"] = Offset.parse(algo.get("offset")) |
| 273 | + core = {key: value[key] for key in _CORE_SUBTASK_KEYS if value.get(key) is not None} |
| 274 | + return Subtask(**core, algo_params=RetargetSubtaskAlgoParams(**algo)) |
262 | 275 |
|
263 | 276 |
|
264 | 277 | @dataclass |
@@ -336,7 +349,8 @@ class RetargetConfig: |
336 | 349 | # Default object-tracking interpolation, overridable per subtask (see DefaultObjectTracking). |
337 | 350 | default_object_tracking: "dict | DefaultObjectTracking" = field(default_factory=DefaultObjectTracking) |
338 | 351 | # Per-EEF subtask lists (keyed by *source* EEF name; eef_name_map renames onto the target). Each entry |
339 | | - # is parsed into a Subtask. Segmentation and object tracking come from these. |
| 352 | + # is parsed into a core Subtask with RetargetSubtaskAlgoParams. Segmentation and object tracking come from |
| 353 | + # these. |
340 | 354 | subtasks: dict[str, list[Subtask]] = field(default_factory=dict) |
341 | 355 | # Bimanual/multi-arm synchronization: a list of barrier groups, each a list of subtask *names* that must |
342 | 356 | # conclude together. Every EEF that reaches its named segment holds at that pose until all segments in |
@@ -366,18 +380,28 @@ def __post_init__(self) -> None: |
366 | 380 | """Parse nested descriptors and validate synchronization barriers.""" |
367 | 381 | self.default_object_tracking = DefaultObjectTracking.parse(self.default_object_tracking) |
368 | 382 | self.subtasks = { |
369 | | - eef: [st if isinstance(st, Subtask) else Subtask.parse(st) for st in entries] |
| 383 | + eef: [st if isinstance(st, Subtask) else parse_subtask(st) for st in entries] |
370 | 384 | for eef, entries in (self.subtasks or {}).items() |
371 | 385 | } |
372 | | - names = [st.name for entries in self.subtasks.values() for st in entries if st.name is not None] |
| 386 | + for entries in self.subtasks.values(): |
| 387 | + for st in entries: |
| 388 | + assert isinstance( |
| 389 | + st.algo_params, RetargetSubtaskAlgoParams |
| 390 | + ), f"subtask algo_params must be RetargetSubtaskAlgoParams, got {type(st.algo_params).__name__}." |
| 391 | + names = [ |
| 392 | + st.algo_params.name |
| 393 | + for entries in self.subtasks.values() |
| 394 | + for st in entries |
| 395 | + if st.algo_params.name is not None |
| 396 | + ] |
373 | 397 | duplicates = sorted({name for name in names if names.count(name) > 1}) |
374 | 398 | assert not duplicates, f"subtask names must be unique across all subtasks; duplicates: {duplicates}" |
375 | 399 |
|
376 | 400 | by_name = { |
377 | | - st.name: (eef, i) |
| 401 | + st.algo_params.name: (eef, i) |
378 | 402 | for eef, entries in self.subtasks.items() |
379 | 403 | for i, st in enumerate(entries) |
380 | | - if st.name is not None |
| 404 | + if st.algo_params.name is not None |
381 | 405 | } |
382 | 406 | # Synchronization barriers: names must exist; a group's members belong to different EEFs; each name |
383 | 407 | # is in at most one group; and the declaration order is a valid schedule -- for each EEF, the groups |
|
0 commit comments