"""Choose a native in-place turn before translation when the body sweep fits. This is action selection, not a path or velocity controller. Nav2 Spin owns rotation; the live range, support, detector and freshness gates admit each tick. """ import math import numpy as np from navigation.detection_obstacles import DetectionObstacles from navigation.support_surface import SupportSurface def observed_turn_support(angle, terrain, pose, monitor, contact_height): """Test the complete newly entered contact area before choosing Spin. Use the existing contact-cell geometry and support classifier. Retain the per-command discrepancy envelope at every orientation; no floor is filled under the current body and no positive motor permission is returned. """ bounds = monitor.command_bounds(0., math.copysign(.15, angle)) bounds['horizon_seconds'] = abs(angle)/.15 if monitor.contacts is not None: local, _ = monitor.contacts.cells(bounds, math.copysign(.15, angle)) else: # Legacy geometry has no contact bands: conservatively inspect the # whole rotational disk on the existing fixed support-cell lattice. radius = math.hypot(monitor.half_length, monitor.half_width) radius += max(bounds['discrepancy_xy_m']) cells = math.ceil(radius/.1) local = np.array([[i*.1,j*.1] for i in range(-cells,cells+1) for j in range(-cells,cells+1) if math.hypot(i*.1,j*.1)<=radius+.1]) required = (np.abs(local[:,0])>monitor.half_length) | (np.abs(local[:,1])>monitor.half_width) x, y, z, w = pose[3:] yaw = math.atan2(2*(w*z+x*y), 1-2*(y*y+z*z)) axes = np.array([[math.cos(yaw),-math.sin(yaw)],[math.sin(yaw),math.cos(yaw)]]) surface = SupportSurface(terrain, pose, contact_height, monitor.max_step, monitor.max_slope, anchor_radius_m=math.hypot(monitor.half_length,monitor.half_width)) states, _ = monitor.classify_contacts(surface,local[required]@axes.T) return bool(np.all(states=='observed-supported')) def alignment_angle(pose, goal, terrain, detected, monitor, contact_height, *, collision_terrain=None): delta = np.asarray(goal[:2]) - np.asarray(pose[:2]) if np.linalg.norm(delta) <= .4: return None x, y, z, w = pose[3:] yaw = math.atan2(2*(w*z+x*y), 1-2*(y*y+z*z)) bearing = math.atan2(delta[1], delta[0]) - yaw angle = math.atan2(math.sin(bearing), math.cos(bearing)) if abs(angle) < math.pi/6: return None collision = terrain if collision_terrain is None else collision_terrain if not monitor.rotation_clear(angle, collision, pose): return None if collision_terrain is not None: forbidden = terrain[terrain[:, 3] > collision[:, 3]] if not monitor.ground_envelope().rotation_clear(angle, forbidden, pose): return None objects = DetectionObstacles(monitor) hazards = np.column_stack((detected, np.full(len(detected), objects.cost))) if not objects.monitor.rotation_clear(angle, hazards, pose): return None if not observed_turn_support(angle, terrain, pose, monitor, contact_height): return None return angle