"""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, }