Align native planning clearance and admit observed tracked pivots
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"""Real heading improvement toward a native detour is progress, not task arrival."""
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from k1link.simulation.ai_polygon.mission_policy import WaypointMission
|
||||
|
||||
|
||||
def pose(yaw=0.):
|
||||
return [0.,0.,.37,0.,0.,math.sin(yaw/2),math.cos(yaw/2)]
|
||||
|
||||
|
||||
def receipt(position, seconds, goal):
|
||||
return dict(status='path',speed_mps=0.,yaw_rate_rps=.35,
|
||||
controller_goal=['align',goal[:2],1.],route_observation=dict(observation=dict(
|
||||
pose=position,simulation_time_seconds=seconds,execution_inputs_sha256='run')))
|
||||
|
||||
|
||||
def test_local_turn_away_from_global_bearing_keeps_existing_progress_watchdog_alive():
|
||||
mission = WaypointMission([[10.,0.]])
|
||||
goal = [0.,2.,0.]
|
||||
for second in range(16):
|
||||
position = pose(.04*second)
|
||||
mission.update(position,float(second),lambda *_:goal,
|
||||
choose_maneuver=lambda *_:None)
|
||||
mission.observe_alignment(receipt(position,float(second),goal),position,float(second),'run')
|
||||
assert not mission.recovery.active and mission.attempts==0
|
||||
assert mission.best_distance==10. and mission.index==0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid',['old','other-goal','moving','unbound'])
|
||||
def test_only_the_bound_current_local_spin_can_supply_a_progress_direction(invalid):
|
||||
mission = WaypointMission([[10.,0.]])
|
||||
goal = [0.,2.,0.]
|
||||
position = pose()
|
||||
mission.update(position,2.,lambda *_:goal)
|
||||
result = receipt(position,2.,goal)
|
||||
if invalid=='old':
|
||||
result['route_observation']['observation']['simulation_time_seconds']=1.
|
||||
elif invalid=='other-goal':
|
||||
result['controller_goal'][1]=[0.,3.]
|
||||
elif invalid=='moving':
|
||||
result['speed_mps']=.1
|
||||
else:
|
||||
result['route_observation']['observation']['execution_inputs_sha256']='other'
|
||||
mission.observe_alignment(result,position,2.,'run')
|
||||
assert mission.progress_orientation_target(2.)==mission.orientation_target()
|
||||
|
||||
|
||||
def test_alignment_receipt_expires_and_repeated_yaw_cannot_extend_a_stall():
|
||||
mission = WaypointMission([[10.,0.]])
|
||||
goal = [0.,2.,0.]
|
||||
for second in range(12):
|
||||
position = pose(.2 if second%2 else 0.)
|
||||
mission.update(position,float(second),lambda *_:goal,
|
||||
choose_maneuver=lambda *_:None)
|
||||
mission.observe_alignment(receipt(position,float(second),goal),position,float(second),'run')
|
||||
assert mission.attempts==1
|
||||
assert mission.best_distance==10. and mission.index==0
|
||||
assert mission.progress_orientation_target(14.)==mission.orientation_target()
|
||||
|
||||
|
||||
def test_routed_mission_retains_bound_native_turn_progress_over_route_body_approach():
|
||||
from k1link.simulation.ai_polygon.route_mission import RoutedMission
|
||||
|
||||
# Isolate the routed progress adapter from graph construction and travel.
|
||||
mission = RoutedMission.__new__(RoutedMission)
|
||||
WaypointMission.__init__(mission, [[10.,0.]])
|
||||
position = pose()
|
||||
goal = [0.,2.,0.]
|
||||
mission.goal = goal
|
||||
mission.last_pose, mission.last_time = tuple(position), 2.
|
||||
mission.body_approach = (('route',0), (3.,0.), 2.)
|
||||
mission.observe_alignment(receipt(position,2.,goal),position,2.,'run')
|
||||
direction = mission.progress_orientation_target(2.2)
|
||||
assert direction['xy'] == [0.,2.]
|
||||
assert direction['key'] == ('native-align',(0.,2.))
|
||||
@@ -0,0 +1,120 @@
|
||||
"""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'])
|
||||
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']
|
||||
Reference in New Issue
Block a user