File size: 3,926 Bytes
8ca7ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Frozen scalar dimensions only. Never opens robot assets or collision geometry."""

from __future__ import annotations

import math
from copy import deepcopy
from pathlib import Path
from typing import Any

import yaml

ALIASES = {"unitree_g1_43dof": "g1", "unitree_g1": "g1", "rp1_3": "rp1"}
CATALOG = yaml.safe_load(
    (Path(__file__).resolve().parents[2] / "configs/robots/task_constraints_v1.yaml").read_text(
        encoding="utf-8"
    )
)["robots"]


def get_task_constraints(robot_id: str | None = None) -> dict[str, Any]:
    key = ALIASES.get(robot_id or "default_all", robot_id or "default_all")
    if key != "default_all":
        if key not in CATALOG:
            raise ValueError(f"unknown task constraint robot: {robot_id}")
        return {"profile_id": key, **deepcopy(CATALOG[key])}
    profiles = list(CATALOG.values())
    result = {"profile_id": key, "members": list(CATALOG)}
    for field in (
        "body_width_m",
        "body_depth_m",
        "carry_corridor_width_m",
        "carry_turn_diameter_m",
    ):
        result[field] = max(p[field] for p in profiles)
    result["max_grip_dimension_m"] = min(p["max_grip_dimension_m"] for p in profiles)
    result["support_height_range_m"] = [
        max(p["support_height_range_m"][0] for p in profiles),
        min(p["support_height_range_m"][1] for p in profiles),
    ]
    result["depth_table"] = [
        {
            "height_m": row["height_m"],
            **{
                field: min(p["depth_table"][index][field] for p in profiles)
                for field in ("comfort_m", "reachable_m", "edge_m")
            },
        }
        for index, row in enumerate(profiles[0]["depth_table"])
    ]
    result["depth_models"] = [deepcopy(p["depth_model"]) for p in profiles]
    return result


def depth_bands(profile: dict[str, Any], height: float) -> tuple[float, float, float]:
    """DEPTH-RULES comfort/reachable/edge depths at the actual support height.

    The table is a reference snapshot only. Universal profiles intersect the
    individual continuous functions, never their parameters or sampled rows.
    """
    if not math.isfinite(height):
        raise ValueError("support height must be finite")
    models = profile.get("depth_models") or [profile["depth_model"]]
    values = []
    for model in models:
        center = height + model["reference_object_half_height_m"]
        low, high = model["shoulder_height_posture_envelope_m"]
        shoulder = min(max(center, low), high)
        vertical = center - shoulder
        deductions = (
            model["front_to_shoulder_m"]
            + model["body_edge_clearance_m"]
            + model["per_side_hand_reserve_m"]
        )
        values.append(
            tuple(
                max(
                    0.0,
                    math.sqrt(
                        max(
                            0.0,
                            (
                                model["k"][tier] * model["arm_total_to_tool_m"]
                                + model["contact_offset_m"]
                            )
                            ** 2
                            - vertical**2,
                        )
                    )
                    - deductions,
                )
                for tier in ("comfort", "reachable", "edge")
            )
        )
    return tuple(min(row[i] for row in values) for i in range(3))


def depth_limits(profile: dict[str, Any], height: float) -> tuple[float, float]:
    """Keep the existing two-band API; edge is available through depth_bands."""
    return depth_bands(profile, height)[:2]


def pending_constraints(robot_id: str | None, enabled: bool = True) -> dict[str, Any]:
    return {
        "enabled": enabled,
        "status": "pending_geometry" if enabled else "disabled",
        "profile": get_task_constraints(robot_id),
        "checks": [],
        "resampling_count": 0,
    }