"""Bounded commands and conservative checks for the v6 navigation diagnostic."""
import math
def wrap_angle(value):
return math.atan2(math.sin(value), math.cos(value))
def yaw_wxyz(quat):
w,x,y,z=quat
return math.atan2(2*(w*z+x*y),1-2*(y*y+z*z))
def velocity_command(position, yaw, goal, goal_yaw=None):
dx,dy=goal[0]-position[0],goal[1]-position[1]
distance=math.hypot(dx,dy)
speed=min(.22,.8*distance)
scale=speed/distance if distance>.001 else 0.
vx=(dx*math.cos(yaw)+dy*math.sin(yaw))*scale
vy=(-dx*math.sin(yaw)+dy*math.cos(yaw))*scale
heading=goal_yaw if goal_yaw is not None else math.atan2(dy,dx)
wz=max(-.5,min(.5,1.2*wrap_angle(heading-yaw)))
return vx,vy,wz
class RouteTracker:
"""World-frame PI feedback with bounded drift compensation per segment."""
def __init__(self):
self.integral=[0.,0.]
self.heading_integral=0.
self.goal=None
def command(self,position,yaw,goal,segment_start,dt,goal_yaw=None,turn_first=False,heading_hold=False):
values=[*position,*goal,*segment_start,yaw,dt]
if (any(len(v)!=2 for v in (position,goal,segment_start))
or not all(math.isfinite(v) for v in values) or not 0
=.10:
self.integral=[0.,0.]
return 0.,0.,wz
error=[goal[i]-position[i] for i in range(2)]
proposed=[self.integral[i]+.35*error[i]*dt for i in range(2)]
size=math.hypot(*proposed)
if size>.12:proposed=[v*.12/size for v in proposed]
# Freeze integration when saturation and error would push in the same direction.
raw=[.8*error[i]+proposed[i] for i in range(2)]
if math.hypot(*raw)<=.22 or sum(raw[i]*error[i] for i in range(2))<0:
self.integral=proposed
velocity=[.8*error[i]+self.integral[i] for i in range(2)]
size=math.hypot(*velocity)
if size>.22:velocity=[v*.22/size for v in velocity]
vx=velocity[0]*math.cos(yaw)+velocity[1]*math.sin(yaw)
vy=-velocity[0]*math.sin(yaw)+velocity[1]*math.cos(yaw)
return vx,vy,wz
def approach_heading_plan(route,pick_index,pick_yaw,place_yaw,clearance_distance=.4):
"""Face the support before entering its approach, including pick retreat."""
if (len(route)<3 or not 0=place_start:
raise ValueError('Approach turn regions overlap or have no safe entry segment')
plan={i:dict(heading=pick_yaw,phase='pick_approach',turn_first=True)
for i in range(pick_start,pick_end+1)}
plan[pick_start]['phase']='prepare_pick'
for i in range(place_start,len(route)):
plan[i]=dict(heading=place_yaw,phase='place_approach',turn_first=True)
plan[place_start]['phase']='prepare_place'
return plan
def obstacle_meshes(meshes):
"""Exclude only saved floor slabs and rugs from foot-obstacle attribution."""
result=[];seen=set()
for row in meshes:
path=row['prim_path'];name=row['name'];identifier=row['entity_id']
if not path.startswith('/House/') or path in seen:
raise ValueError('Invalid or duplicate saved obstacle path')
seen.add(path)
if identifier is None and name.startswith('floor__'):continue
if identifier is not None and '__rug__' in identifier:continue
result.append(row)
if not result:raise ValueError('Missing obstacle collision meshes')
return result
def segment_distance(point,a,b):
dx,dy=b[0]-a[0],b[1]-a[1]
length=dx*dx+dy*dy
t=max(0.,min(1.,((point[0]-a[0])*dx+(point[1]-a[1])*dy)/length)) if length else 0.
return math.dist(point[:2],(a[0]+t*dx,a[1]+t*dy))
def contact_metrics(names,vectors):
feet=('left_ankle_roll_link','right_ankle_roll_link')
if len(names)!=len(vectors) or not all(name in names for name in feet):
raise ValueError('Incomplete body contact coverage')
forces={}
for name,vector in zip(names,vectors):
if len(vector)!=3 or not all(math.isfinite(v) for v in vector):
raise ValueError('Invalid measured contact force')
forces[name]=math.sqrt(sum(v*v for v in vector))
return dict(nonfoot_scene_contact_N=max((v for n,v in forces.items() if n not in feet),default=0.),
left_foot_contact_N=forces[feet[0]],right_foot_contact_N=forces[feet[1]])
def filtered_contact_peak(vectors):
if not vectors:raise ValueError('Missing filtered foot contacts')
for vector in vectors:
if len(vector)!=3 or not all(math.isfinite(v) for v in vector):
raise ValueError('Nonfinite filtered foot contact')
return max(math.sqrt(sum(v*v for v in vector)) for vector in vectors)
def summarize_navigation(rows,route,*,pick_index,pick_yaw,place_yaw,required_rugs,
reset_count,root_pose_write_count):
reasons=set();next_waypoint=0;previous=None
hold_start={'pick_hold':None,'place_hold':None}
hold_duration={'pick_hold':0.,'place_hold':0.}
rug_forces={identifier:0. for identifier in required_rugs}
minimum_height=math.inf;maximum_tilt=0.;maximum_deviation=0.;maximum_contact=0.;maximum_ankle_contact=0.
if len(route)<2 or not 0.01:raise ValueError('Invalid quaternion')
command=row.get('command_body_velocity',[])
if (len(command)!=3 or not all(math.isfinite(v) for v in command)
or math.hypot(*command[:2])>.220001 or abs(command[2])>.500001):
reasons.add('invalid_command')
w,x,y,z=[v/norm for v in quat]
tilt=math.degrees(math.acos(max(-1.,min(1.,1-2*(x*x+y*y)))))
minimum_height=min(minimum_height,xyz[2]);maximum_tilt=max(maximum_tilt,tilt)
maximum_contact=max(maximum_contact,body_contact)
maximum_ankle_contact=max(maximum_ankle_contact,ankle_contact)
if xyz[2]<.5 or xyz[2]>1.2 or tilt>40:reasons.add('fall_or_excessive_tilt')
if body_contact>20.:reasons.add('nonfoot_scene_collision')
if ankle_contact>20.:reasons.add('ankle_obstacle_collision')
deviation=min(segment_distance(xyz,a,b) for a,b in zip(route,route[1:]))
maximum_deviation=max(maximum_deviation,deviation)
if deviation>.2:reasons.add('left_planned_route')
while next_waypoint.030001:reasons.add('sampling_gap')
if distance>.1 or distance/dt>2.:reasons.add('discontinuous_motion')
previous=row
except (ValueError,TypeError,KeyError,IndexError):
reasons.add('invalid_trace')
hold_start={'pick_hold':None,'place_hold':None};previous=None
try:
if not rows or rows[-1]['time_s']-rows[0]['time_s']<10.:reasons.add('insufficient_duration')
except (KeyError,TypeError):reasons.add('invalid_trace')
if next_waypoint!=len(route):reasons.add('incomplete_route')
if hold_duration['pick_hold']<1.-1.e-6:reasons.add('pick_hold_not_measured')
if hold_duration['place_hold']<2.-1.e-6:reasons.add('place_hold_not_measured')
try:
if not rows or math.dist(rows[-1]['root_xyz'][:2],route[-1])>.08:
reasons.add('place_position_not_reached')
if not rows or abs(wrap_angle(yaw_wxyz(rows[-1]['root_quat_wxyz'])-place_yaw))>.12:
reasons.add('place_facing_not_reached')
except (KeyError,TypeError,ValueError):reasons.add('invalid_trace')
for identifier,force in rug_forces.items():
if force<=1.:reasons.add('unverified_rug_contact:'+identifier)
return dict(passed=not reasons,reasons=sorted(reasons),samples=len(rows),
visited_waypoints=next_waypoint,total_waypoints=len(route),
minimum_root_height_m=minimum_height if math.isfinite(minimum_height) else None,
maximum_tilt_deg=maximum_tilt,maximum_route_deviation_m=maximum_deviation,
maximum_nonfoot_scene_contact_N=maximum_contact,maximum_rug_contact_N=rug_forces,
maximum_ankle_obstacle_contact_N=maximum_ankle_contact,
measured_hold_duration_s=hold_duration,
whole_task_acceptance=False,scope='v6_collision_only_native_controller_navigation_diagnostic')