Files

67 lines
4.0 KiB
Python

"""Offline 4cm candidate verification; no hardware connection or execution mode."""
import json
import xml.etree.ElementTree as ET
import numpy as np
from scipy.spatial.transform import Rotation as R
from search_cup_poses_offline import ROOT,PoseGuard,hand_cloud
def main():
cap=ROOT/'data/left_cup_current'
rows=json.loads((cap/'other_poses_offline_report.json').read_text())['candidates']
candidate=next(c for c in rows if c['raise_m']==.04 and c['yaw_relative_saved_deg']==0
and c['status']=='table_segment_pass')
saved=json.loads((cap/'cup_yaw_plan.json').read_text())['solutions'][0];g=saved['grasp']
table=json.loads((cap/'camera.json').read_text())['table_estimate']
n=np.array(table['normal']);n=n if n[2]>0 else -n
guard=PoseGuard(n/np.linalg.norm(n),table['point']);guard.mount=np.array(saved['mount_rotation'])
q=np.array(candidate['grasp_joints']);qp=np.array(candidate['pregrasp_joints'])
report={'executable':False,'hardware_connected':False,'motion_sent':False,
'candidate_raise_m':.04,'stages':{},'scope':'saved-model tabletop geometry only'}
try:
guard.cloud=hand_cloud(g['hand_pre'])
report['stages']['approach']=guard.check_segment(qp,q)
print('Approach checked',flush=True)
# Bound unobserved displacement between closure samples by chain radius
# times total joint-angle travel, including dependent mimic joints.
delta={name:abs(g['hand_qpos'][name]-v) for name,v in g['hand_pre'].items()}
for joint in ET.parse(ROOT/'assets/l20_left_mj.urdf').getroot().findall('joint'):
m=joint.find('mimic')
if m is not None:delta[joint.get('name')]=abs(float(m.get('multiplier','1')))*delta[m.get('joint')]
steps=100;padding=guard.radius*sum(delta.values())/(2*steps)
worst=float('inf')
for t in np.linspace(0,1,steps+1):
values={k:(1-t)*v+t*g['hand_qpos'][k] for k,v in g['hand_pre'].items()}
guard.cloud=hand_cloud(values);gap,body=guard.clearance(q);worst=min(worst,gap)
if gap<guard.margin+padding:raise RuntimeError(f'Closure rejected: {body} {gap:.4f}m')
report['stages']['closure']={'minimum_gap_m':worst,'interval_bound_m':padding,'samples':steps+1}
print('Closure checked',flush=True)
guard.cloud=hand_cloud(g['hand_qpos'])
p=np.array(candidate['hand_target_m']);rot=R.from_quat(np.roll(g['wrist_quat_wxyz'],-1)).as_matrix()@guard.mount.T
stages=[];seed=q
for height in (.01,.02,.03,.04,.05):
nxt=guard.solve(p+[0,0,height],rot,[seed,q,qp])
if nxt is None:raise RuntimeError(f'Lift local IK not found at {height*1000:.0f}mm')
checks=guard.check_segment(seed,nxt)
stages.append({'height_m':height,'joints':nxt.tolist(),**checks});seed=nxt
print('Lift checked',height,flush=True)
report['stages']['lift']=stages
report['local_table_check']='pass'
except (RuntimeError,ValueError) as e:
report['local_table_check']='blocked';report['reason']=str(e);print(str(e),flush=True)
report['execution_blockers']=[
'Current robot/hand pose is unavailable: initial transit and preshape not verified.',
'Mount rotation remains based on saved comparison-pose assumption.',
'No fresh cup observation or upper grasp contact/holding validation.',
'No robot self-collision, other-arm or cup collision check, nor held-cup geometry during lift.',
'Controller interpolation and ordinary stop preserving enable remain unverified.',
'Existing real executor does not consume this new plan and its safety gate remains locked.']
path=cap/'cup_4cm_verification.json';path.write_text(json.dumps(report,indent=2));print(path,flush=True)
if __name__=='__main__':
# The legacy main() above documents the superseded world-Z mounting guess.
# Never report its PASS as verification of the user-resolved installation.
from replan_pinky_low_4cm import main as verify_selected_mount
verify_selected_mount()