"""Tracked turns and object clearance must agree before motor admission.""" import math from pathlib import Path import numpy as np import pytest @pytest.fixture def motion(monkeypatch): root = Path(__file__).parents[1] / 'simulation/ai-polygon' monkeypatch.syspath_prepend(str(root)) monkeypatch.syspath_prepend(str(root / 'nav2')) from detector_exclusion import DetectorExclusion from differential_motion import alignment_angle from navigation.footprint import Footprint monitor = Footprint(dict(length_m=1., width_m=.8, max_step_m=.1, max_slope_degrees=20)) def alignment(pose, goal, terrain, detected, monitor): floor = np.array([[i*.05,j*.05,0.,0.] for i in range(-40,41) for j in range(-40,41)]) return alignment_angle(pose, goal, np.concatenate((floor,terrain)), detected, monitor,.37) return alignment, DetectorExclusion(), monitor def pose(yaw=0): return np.array([0., 0., .37, 0., 0., math.sin(yaw/2), math.cos(yaw/2)]) @pytest.mark.parametrize('yaw', [0., .31, -1.3]) @pytest.mark.parametrize('angle', [math.pi/4, -math.pi/2, math.pi]) def test_large_local_direction_change_can_use_zero_translation_turn(motion, yaw, angle): alignment, _, monitor = motion goal = [2*math.cos(yaw+angle), 2*math.sin(yaw+angle), 0.] result = alignment(pose(yaw), goal, np.empty((0,4)), np.empty((0,3)), monitor) assert abs(math.atan2(math.sin(result-angle), math.cos(result-angle))) < 1e-10 assert alignment(pose(yaw+angle), goal, np.empty((0,4)), np.empty((0,3)), monitor) is None def test_turn_checks_intermediate_corner_sweep_and_detector_reserve(motion): alignment, _, monitor = motion # Initially outside the chassis; an intermediate corner hits it during a turn. hazard = np.array([[0., -.59, .3, 1.1]]) assert monitor._clear(monitor._obstacles(hazard, pose()), np.zeros(2), 0.) assert alignment(pose(), [0.,2.,0.], hazard, np.empty((0,3)), monitor) is None # Ordinary body padding fits this turn; the detector's larger reserve does not. point = np.array([[0., -.86, .3]]) assert alignment(pose(), [0.,2.,0.], np.empty((0,4)), np.empty((0,3)), monitor) is not None assert alignment(pose(), [0.,2.,0.], np.empty((0,4)), point, monitor) is None def test_small_course_correction_stays_with_native_path_controller(motion): alignment, _, monitor = motion assert alignment(pose(), [2.,.5,0.], np.empty((0,4)), np.empty((0,3)), monitor) is None @pytest.mark.parametrize('offset', [[0.,0.], [.023,-.017], [13.193,-24.208]]) def test_search_reserve_covers_guard_at_every_heading_without_inventing_ground(motion, offset): _, exclusion, monitor = motion centre = np.asarray(offset) observed = np.array([[*centre, .7]]) original = observed.copy() rover = pose() rover[:2] = centre costs = exclusion.points(observed, rover) assert len(costs) and len(costs) < 400 # Any point of the added square margin, rotated at any heading, is covered # by a lethal published cell. Test the contour and its interior separately. for angle in np.linspace(-math.pi, math.pi, 97): c, s = math.cos(angle), math.sin(angle) axes = np.array([[c,-s],[s,c]]) local = np.array([[x,y] for x in np.linspace(-.3,.3,9) for y in np.linspace(-.3,.3,9)]) samples = local@axes.T+centre nearest = np.min(np.max(np.abs(samples[:,None]-costs[None]),axis=2),axis=1) assert np.all(nearest <= .05/2+1e-9) np.testing.assert_array_equal(observed, original) assert exclusion.points([], rover).shape == (0,2) def test_retreat_creates_turning_room_without_relaxing_object_clearance(motion): alignment, _, monitor = motion obstacle = np.array([[.8,0.,.3]]) terrain = np.column_stack((obstacle,[1.1])) destination = [1.5,1.5,0.] # Too close for the corners to sweep around this object at the start. assert alignment(pose(), destination, terrain, obstacle, monitor) is None # A real measured retreat gives the same requested destination room for a # native in-place turn. No fixed short reverse distance is an admission rule. after_retreat = pose() after_retreat[0] = -.8 turn = alignment(after_retreat, destination, terrain, obstacle, monitor) assert turn is not None and turn > math.pi/6 def test_unknown_future_track_ground_does_not_select_an_incomplete_turn(motion): from differential_motion import alignment_angle _, _, monitor = motion floor = np.array([[i*.05,j*.05,0.,0.] for i in range(-25,26) for j in range(-25,26)]) assert alignment_angle(pose(),[0.,2.,0.],floor,[],monitor,.37) is not None # The current footprint and front are observed; rear-side future contact # ground is unknown. A short initial pivot can fit but the whole turn cannot. hole = floor[~((floor[:,0]<-.2)&(floor[:,1]>.45))] assert alignment_angle(pose(),[0.,2.,0.],hole,[],monitor,.37) is None @pytest.mark.parametrize('helper',['detector_exclusion.py','differential_motion.py','route_support.py']) def test_native_module_identity_includes_behavior_helpers(monkeypatch,helper): from k1link.simulation.ai_polygon.composition import registry root=Path(__file__).parents[1]/'simulation/ai-polygon' def identities(): return {m.module_id:m.implementation_sha256 for m in registry(root).modules} before=identities() original=Path.read_bytes def changed(path): data=original(path) return data+b'\n# distinct implementation\n' if path==root/'nav2'/helper else data monkeypatch.setattr(Path,'read_bytes',changed) after=identities() assert before['simulation-nav2-navigation']!=after['simulation-nav2-navigation'] assert before['simulation-waypoint-mission']==after['simulation-waypoint-mission']