diff --git a/simulation/ai-polygon/navigation/observed_goals.py b/simulation/ai-polygon/navigation/observed_goals.py index e7d956db..64763b58 100644 --- a/simulation/ai-polygon/navigation/observed_goals.py +++ b/simulation/ai-polygon/navigation/observed_goals.py @@ -395,7 +395,21 @@ class ObservedGoals: observed = self.clearance_observation obstacles = (observed[1] if observed is not None and 0 <= now-observed[0] <= .8 else np.empty((0, 2))) - for passages in passage_centres(permitted, pose, target, geometry, obstacles): + # A sensor crop edge is not a passage boundary. Preserve the spatial + # extent of still-positive measured surface cells when choosing intent. + # Current range above still owns candidate selection/lateral support; + # this memory cannot certify a drive goal or admit any motion. + remembered = np.array([point for key, point in self.measured.items() + if key in allowed], dtype=float).reshape(-1, 3) + if len(remembered): + distance = np.linalg.norm(remembered[:, :2]-pose[:2], axis=1) + remembered = remembered[(distance <= 6.) & (np.abs(remembered[:, 2] - + (pose[2]-geometry['body_contact_height_m'])) <= geometry.get('max_step_m', .1) + + distance*math.tan(math.radians(geometry['max_slope_degrees'])))] + if candidate_mask is not None: + remembered = remembered[candidate_mask(remembered)] + passage_points = np.concatenate((permitted, remembered)) + for passages in passage_centres(passage_points, pose, target, geometry, obstacles): for passage in passages: distance = np.linalg.norm(candidates[:, :2]-passage['xy'], axis=1) for index in np.argsort(distance, kind="stable"): diff --git a/simulation/ai-polygon/navigation/observed_passages.py b/simulation/ai-polygon/navigation/observed_passages.py index 56f23382..fdf4a882 100644 --- a/simulation/ai-polygon/navigation/observed_passages.py +++ b/simulation/ai-polygon/navigation/observed_passages.py @@ -7,6 +7,7 @@ converted to positive semantic cells. """ import math import numpy as np +from scipy.spatial import cKDTree def passage_centres(points, pose, target, geometry, obstacles): @@ -17,6 +18,7 @@ def passage_centres(points, pose, target, geometry, obstacles): [math.sin(yaw), math.cos(yaw)]]) local = (points[:, :2]-position) @ axes hazards = (obstacles-position) @ axes if len(obstacles) else np.empty((0, 2)) + tree = cKDTree(hazards) if len(hazards) else None width = geometry['width_m'] length = geometry.get('length_m', width) route = (np.asarray(target)-position) @ axes @@ -32,18 +34,31 @@ def passage_centres(points, pose, target, geometry, obstacles): # Larger unsupported intervals remain distinct passages. groups = np.split(lateral, np.flatnonzero(np.diff(lateral) > .3)+1) intervals = [(float(g[0]), float(g[-1])) for g in groups if len(g) >= 3] - near = hazards[(hazards[:, 0] >= .4) - & (hazards[:, 0] <= along+length)] - for boundary in sorted(set(np.round(near[:, 1]/.1).astype(int))): - low, high = boundary*.1-.1, boundary*.1+.1 - intervals = [piece for a,b in intervals for piece in ( - [(a,b)] if high <= a or low >= b else - [(a,min(b,low)),(max(a,high),b)]) if piece[1]>piece[0]] centres=[] for low,high in intervals: if high-low < width: continue - middle=(low+high)/2 + # Rank lateral positions by their worst clearance to the observed + # passage limits and to solids along the whole continuing bearing. + # Keep each obstacle at its actual longitudinal coordinate: a wall + # beside the rover must not become a fictitious barrier ahead. + lateral_samples = np.linspace(low+width/2, high-width/2, + max(2, math.ceil((high-low-width)/.05)+1)) + centre_clearance = np.minimum(lateral_samples-low, high-lateral_samples) + if tree is not None: + delta = np.column_stack((np.full(len(lateral_samples),along), lateral_samples)) + direction = delta / np.linalg.norm(delta,axis=1)[:,None] + radial = np.linspace(length/2, 3.5, 65) + queries = direction[:,None,:]*radial[None,:,None] + distances = tree.query(queries.reshape(-1,2))[0].reshape(len(delta),-1) + centre_clearance = np.minimum(centre_clearance, distances.min(axis=1)) + peak = centre_clearance.max() + maxima = np.flatnonzero(centre_clearance >= peak-.025) + lobes = np.split(maxima, np.flatnonzero(np.diff(maxima)>1)+1) + # Two equally clear sides of an obstacle are separate choices. + # Averaging them would put the goal back through the obstacle. + lobe = min(lobes, key=lambda ids: abs(lateral_samples[ids].mean()-(low+high)/2)) + middle = float(lateral_samples[lobe].mean()) # A broad open area has no nearby pair of boundaries to centre # between. The route can choose a direction within its interior. if high-low >= 2*3.5 and route[0] > .1: diff --git a/tests/test_ai_polygon_observed_goals.py b/tests/test_ai_polygon_observed_goals.py index 145f1b41..d48e4bbc 100644 --- a/tests/test_ai_polygon_observed_goals.py +++ b/tests/test_ai_polygon_observed_goals.py @@ -482,3 +482,31 @@ def test_route_selects_the_relevant_observed_branch(selector): goal=selector.choose(points,pose,[8,sign*5],None,(),GEOMETRY,1.) assert goal is not None and sign*goal[1]>1.5 assert np.min(np.linalg.norm(points-goal,axis=1))<1e-9 + + +@pytest.mark.parametrize("heading", [-25, 0, 25]) +@pytest.mark.parametrize("yaw,shift", [(0,(0,0)),(73,(42,-17))]) +def test_corridor_boundaries_do_not_rotate_with_the_rover(selector,heading,yaw,shift): + _,_,_,_,rotation=road(yaw,shift) + xy=np.array([[x,y] for x in np.arange(.4,6.,.07) + for y in np.arange(-1.5,1.51,.07)]) + points=np.c_[xy@rotation.T+shift,np.zeros(len(xy))] + angle=math.radians(yaw+heading) + pose=[*shift,.37,0,0,math.sin(angle/2),math.cos(angle/2)] + selector.observe(np.c_[points,np.ones(len(points))],1,pose) + wall=np.array([[x,y] for x in np.arange(-.5,6.,.07) for y in [-1.6,1.6]]) + selector.observe_navigation(clearance_receipt(pose,(wall@rotation.T+shift).tolist()), + pose,1,'run',1.) + goal=selector.choose(points,pose,np.array(shift)+rotation@[12,0],None,(),GEOMETRY,1.1) + assert goal is not None + local=(np.array(goal[:2])-shift)@rotation + assert local[0]>2.5 and abs(local[1])<.2 + + +def test_symmetric_obstacle_does_not_average_two_passages_through_its_centre(selector): + points,pose,target,_,_=road() + selector.observe(np.c_[points,np.ones(len(points))],1,pose) + selector.observe_navigation(clearance_receipt(pose,[[1.6,0.]]),pose,1,'run',1.) + goal=selector.choose(points,pose,target,None,(),GEOMETRY,1.1) + assert goal is not None and abs(goal[1])>.35 + assert selector.approach_clearance(np.array([goal]),pose,GEOMETRY,1.1)[0]>.2