"""Offline near-edge diagnostic from measured edge + saved robot state. No controller import/connection. Does not change the execution safety gate. """ import argparse import json from pathlib import Path import numpy as np from scipy.spatial.transform import Rotation as R from search_cup_poses_offline import ROOT,PoseGuard,hand_cloud from table_edge_constraint import TableEdgeConstraint class EdgePoseGuard(PoseGuard): def geometry(self,q): T=np.eye(4) for i,(a,(O,axis,v,lo,hi)) in enumerate(zip(q,self.chain),1): if not lo<=a<=hi:raise ValueError('Arm joint limit violation') A=np.eye(4);A[:3,:3]=R.from_rotvec(axis*a).as_matrix();T=T@O@A yield f'L{i}_Link',v@T[:3,:3].T+T[:3,3],None yield 'adapter_envelope',T[:3,:3]@self.adapter_center+T[:3,3],self.adapter_radius yield 'conditional_hand_preshape',(self.cloud@self.mount.T+self.hand_center)@T[:3,:3].T+T[:3,3],None def clearance(self,q): worst=(float('inf'),'') for name,p,r in self.geometry(q): gap=self.edge.hull_clearance(p) if r is None else self.edge.sphere_clearance(p,r) ymin,ymax=(p[:,1].min(),p[:,1].max()) if r is None else (p[1]-r,p[1]+r) # Outside the observed edge span, do not infer that below-table # space is free. Revert to the conservative top-plane constraint. if yminself.observed_y[1]: gap=(float(((p-self.edge.top_point)@self.edge.top_normal).min()) if r is None else float((p-self.edge.top_point)@self.edge.top_normal)-r)-self.edge.uncertainty_m if gapguard.observed_y[1]), y_range_m=[float(p[:,1].min()),float(p[:,1].max())], vertices_inside_extrapolated_forbidden_volume=int(np.sum((top<0)&(outward<0))))) else: report['initial_bodies'].append(dict(name=name,edge_clearance_bound_m=guard.edge.sphere_clearance(p,r))) rows=json.loads((cap/'other_poses_offline_report.json').read_text())['candidates'] chosen=next(c for c in rows if c['raise_m']==.04 and c['yaw_relative_saved_deg']==0 and c['status']=='table_segment_pass') target=np.asarray(chosen['pregrasp_joints']);report['lift_candidates']=[] try:report['direct_transit']=guard.check_segment(q,target) except (RuntimeError,ValueError) as exc:report['direct_transit']={'status':'rejected','reason':str(exc)} gap,body=guard.clearance(q) if gap