File size: 10,416 Bytes
473a751 | 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | """Task-specific profiles for evaluation.
Each profile contains:
- gym_id: Registered Gym environment name (uses DataCollection-v0 so that
evaluation shares exactly the same scene, cameras and reset events as the
data-collection pipeline, including the ``mark_wrist_cam_usd_dirty`` Fabric
workaround)
- action_scale: Scaling factor for arm joint-relative actions
- success_type: How to determine episode success
- phase_names: Human-readable names for task phases (diagnostics)
- has_ik_override: Whether to disable env's built-in IK override
- WhackAMole-specific popup timing overrides
- ConveyorBeltPickAndPlace box ranges for object_in_box check
Helper functions:
- check_success(): Evaluate success for a given task profile
- apply_whack_a_mole_eval_overrides(): Override popup timing for eval
- install_phase_tracker(): Patch _reset_idx to capture phase at termination
"""
from __future__ import annotations
import torch
# ============================================================
# Task Profiles Registry
# ============================================================
TASK_PROFILES: dict[str, dict] = {
"conveyor_belt_pick_and_place": {
"gym_id": "ConveyorBeltPickAndPlace-Franka-DataCollection-v0",
"action_scale": 0.1,
"has_ik_override": True,
"short_circuit_success": True,
"success_type": "object_in_box",
"phase_names": {
0: "approach", 1: "lift", 2: "transport", 3: "release", 4: "return", 5: "done",
},
"box_x_range": (-0.2, 0.2),
"box_y_range": (-0.6, -0.3),
"box_z_range": (0.0, 0.08),
},
"ball_catching": {
"gym_id": "BallCatching-Franka-DataCollection-v0",
"action_scale": 0.1,
"has_ik_override": False,
"short_circuit_success": True,
"success_type": "task_phase_4",
"phase_names": {
0: "wait", 1: "launch", 2: "predict", 3: "position", 4: "catch",
},
},
"rolling_ball_interception": {
"gym_id": "RollingBallInterception-Franka-DataCollection-v0",
"action_scale": 0.1,
"has_ik_override": False,
"short_circuit_success": True,
"success_type": "task_phase_4",
"phase_names": {
0: "wait", 1: "roll", 2: "predict", 3: "position", 4: "catch",
},
},
"whack_a_mole": {
"gym_id": "WhackAMole-Franka-DataCollection-v0",
"action_scale": 0.2,
"has_ik_override": False,
"short_circuit_success": False,
"success_type": "whack_a_mole",
"popup_duration_s": 2.0,
"popup_gap_s": 0.3,
"popup_initial_delay_s": 0.0,
"eval_episode_length_s": 3.0,
"phase_names": {
0: "wait popup", 1: "move", 2: "strike", 3: "return", 4: "done",
},
},
"ball_throwing": {
"gym_id": "BallThrowing-Franka-DataCollection-v0",
"action_scale": 0.1,
"has_ik_override": False,
"short_circuit_success": True,
"success_type": "task_phase_2",
"phase_names": {
0: "swing", 1: "release", 2: "in box",
},
},
"rotating_peg_insertion": {
"gym_id": "RotatingPegInsertion-Franka-DataCollection-v0",
"action_scale": 0.1,
"has_ik_override": False,
"short_circuit_success": True,
"success_type": "task_phase_4",
"phase_names": {
0: "pre-align", 1: "track", 2: "wait alignment", 3: "insert", 4: "done",
},
},
}
def resolve_profile(name_or_gym_id: str) -> dict | None:
"""Look up a profile by short name or gym_id. Returns None if not found."""
if name_or_gym_id in TASK_PROFILES:
return TASK_PROFILES[name_or_gym_id]
for profile in TASK_PROFILES.values():
if profile["gym_id"] == name_or_gym_id:
return profile
return None
# ============================================================
# Success Checking
# ============================================================
def check_success(manager_env, task_profile: dict, env_id: int = 0) -> bool:
"""Evaluate success for a single env according to the task profile."""
st = task_profile["success_type"]
if st == "object_in_box":
obj = manager_env.scene["object"]
local = (
obj.data.root_pos_w[env_id, :3]
- manager_env.scene.env_origins[env_id, :3]
)
bx = task_profile["box_x_range"]
by = task_profile["box_y_range"]
bz = task_profile["box_z_range"]
return bool(
bx[0] <= local[0].item() <= bx[1]
and by[0] <= local[1].item() <= by[1]
and bz[0] <= local[2].item() <= bz[1]
)
elif st == "task_phase_4":
return (
hasattr(manager_env, "task_phase")
and manager_env.task_phase[env_id].item() == 4
)
elif st == "task_phase_2":
return (
hasattr(manager_env, "task_phase")
and manager_env.task_phase[env_id].item() == 2
)
elif st == "whack_a_mole":
return (
hasattr(manager_env, "valid_hits")
and manager_env.valid_hits[env_id].item() > 0
)
return False
# ============================================================
# WhackAMole Eval Overrides
# ============================================================
def apply_whack_a_mole_eval_overrides(manager_env, task_profile: dict | None) -> None:
"""Override popup timing for WhackAMole evaluation."""
if not task_profile or task_profile.get("success_type") != "whack_a_mole":
return
if not hasattr(manager_env, "popup_durations") or not hasattr(
manager_env, "popup_start_times"
):
return
popup_duration_s = float(task_profile.get("popup_duration_s", 1.0))
popup_gap_s = float(task_profile.get("popup_gap_s", 0.3))
popup_initial_delay_s = float(task_profile.get("popup_initial_delay_s", 1.0))
manager_env.popup_durations.fill_(popup_duration_s)
start_times = torch.zeros_like(manager_env.popup_start_times)
start_times[:, 0] = popup_initial_delay_s
for i in range(1, start_times.shape[1]):
start_times[:, i] = start_times[:, i - 1] + popup_duration_s + popup_gap_s
manager_env.popup_start_times.copy_(start_times)
# ============================================================
# Phase Tracker
# ============================================================
def install_phase_tracker(manager_env) -> bool:
"""Patch _reset_idx to capture task_phase at termination time."""
manager_env._phase_at_term = -1
if not hasattr(manager_env, "_reset_idx"):
print(" [WARN] _reset_idx not found - phase_at_term unavailable")
return False
original_reset_idx = manager_env._reset_idx
def _patched_reset_idx(env_ids, *args, **kwargs):
if len(env_ids) > 0 and hasattr(manager_env, "task_phase"):
manager_env._phase_at_term = manager_env.task_phase[env_ids[0]].item()
return original_reset_idx(env_ids, *args, **kwargs)
manager_env._reset_idx = _patched_reset_idx
print(" Phase tracker installed")
return True
# ============================================================
# WhackAMole Debug Diagnostics
# ============================================================
def debug_whack_a_mole_press(manager_env, step_idx: int, env_id: int = 0) -> None:
"""Print press-detection diagnostics for WhackAMole."""
if not hasattr(manager_env, "window_active"):
return
if not manager_env.window_active[env_id]:
return
from isaaclab.sensors import FrameTransformer
robot = manager_env.scene["robot"]
ee_frame: FrameTransformer = manager_env.scene["ee_frame"]
ee_pos_w = ee_frame.data.target_pos_w[env_id, 0, :]
ee_pos_local = ee_pos_w - manager_env.scene.env_origins[env_id]
active_mid = int(manager_env.active_mole_id[env_id].item())
if active_mid < 0:
return
mole_xy = manager_env.mole_positions_local[env_id, active_mid, :2]
mole_z = manager_env.mole_heights[env_id, active_mid].item()
xy_dist = torch.norm(ee_pos_local[:2] - mole_xy).item()
ee_z = ee_pos_local[2].item()
finger_ids = robot.find_joints(["panda_finger.*"])[0]
finger_pos = robot.data.joint_pos[env_id, finger_ids]
finger_width = finger_pos.sum().item()
BOARD_SURFACE_Z = 0.22
XY_THR = 0.035
Z_MAX = BOARD_SURFACE_Z + 0.018
Z_MIN = BOARD_SURFACE_Z - 0.010
GRIP_THR = 0.03
ok_xy = xy_dist < XY_THR
ok_z = Z_MIN < ee_z < Z_MAX
ok_grip = finger_width < GRIP_THR
dwell = manager_env.press_dwell_counter[env_id].item()
hit_reg = manager_env.window_hit_registered[env_id].item()
valid_hits = manager_env.valid_hits[env_id].item()
win_idx = manager_env.current_window_idx[env_id].item()
timer = manager_env.episode_timer[env_id].item()
print(
f" [WAM-DBG] step={step_idx:>4} t={timer:.2f}s win={win_idx} mole={active_mid} "
f"| ee_xy=({ee_pos_local[0].item():.4f},{ee_pos_local[1].item():.4f}) "
f"mole_xy=({mole_xy[0].item():.4f},{mole_xy[1].item():.4f}) "
f"xy_d={xy_dist:.4f} {'OK' if ok_xy else 'FAR':>3} "
f"| ee_z={ee_z:.4f} [{Z_MIN:.3f},{Z_MAX:.3f}] "
f"{'OK' if ok_z else ('HI' if ee_z >= Z_MAX else 'LO'):>2} "
f"| grip_w={finger_width:.4f} {'CLOSED' if ok_grip else 'OPEN':>6} "
f"| dwell={dwell} hit={hit_reg} total_hits={valid_hits}"
)
# ============================================================
# Smoothness Metrics
# ============================================================
def calculate_smoothness(actions_list: list) -> tuple[float, float]:
"""Compute (avg_diff, avg_jerk) from a list of action arrays."""
import numpy as np
if len(actions_list) < 3:
return 0.0, 0.0
actions = np.array(actions_list)
arm_actions = actions[:, :7] if actions.shape[-1] >= 7 else actions
diff1 = np.linalg.norm(np.diff(arm_actions, axis=0), axis=1)
diff2 = np.linalg.norm(np.diff(arm_actions, n=2, axis=0), axis=1)
return float(np.mean(diff1)), float(np.mean(diff2))
|