diff --git a/simulation/ai-polygon/nav2/server.py b/simulation/ai-polygon/nav2/server.py index 158c08d5..0572c17a 100644 --- a/simulation/ai-polygon/nav2/server.py +++ b/simulation/ai-polygon/nav2/server.py @@ -281,18 +281,7 @@ class Navigation: if (key != self.action_key or not continuing or self.path is None or self.node.native_command() is None or entered-self.path_planned_at >= 1.0): - heading = math.atan2(goal[1]-pose[1], goal[0]-pose[0]) - if (value.get('continuous', False) and target is None - and not value.get('recovery_orientation', False) - and orientation is not None and 'direction_xy' in orientation): - # The observed passage selects XY; the declared continuation - # supplies its through-travel heading. Re-aiming the terminal - # body toward a nearby lateral XY each replan makes the - # approach progressively tighter instead of rejoining travel. - # Smac still checks the complete path and footprint, and the - # live support/collision gates retain all motion authority. - heading = math.atan2(*reversed(orientation['direction_xy'])) - path, status = self.node.plan(pose, goal, heading) + path, status = self.node.plan(pose, goal, math.atan2(goal[1]-pose[1],goal[0]-pose[0])) if path is None or len(path.poses) < 2: self.stop() # Humble has no typed planner error code. Preserve its diff --git a/simulation/ai-polygon/navigation/observed_goals.py b/simulation/ai-polygon/navigation/observed_goals.py index 4b9f8a41..6abbde4a 100644 --- a/simulation/ai-polygon/navigation/observed_goals.py +++ b/simulation/ai-polygon/navigation/observed_goals.py @@ -389,6 +389,38 @@ class ObservedGoals: before = np.asarray(prior, dtype=float) cell = tuple(self.surface.cell_keys(before[None])[0]) matched = np.linalg.norm(passage_points - before, axis=1) <= 0.15 + # A routed local passage is a through-intent, not a succession of + # stopping points. Advance its lookahead along the selected + # continuation before the chassis starts chasing a near lateral + # endpoint. A changed visible extent alone must not re-elect the + # lateral centre on every reached point. + # The projected XY is only a query: return an actual permitted + # range sample, with the existing width and obstacle checks. Never + # extrapolate range, terrain permission, or a motion certificate. + if travel_direction is not None and cell in allowed and matched.any(): + continuation = np.asarray(travel_direction, dtype=float) + if (continuation.shape != (2,) or not np.isfinite(continuation).all() + or np.linalg.norm(continuation) <= 1e-9): + raise ValueError("Invalid passage travel direction") + continuation = continuation / np.linalg.norm(continuation) + along = float((before[:2]-pose[:2]) @ continuation) + advance = min(3., remaining)-along + if (along > 0 and advance >= float(self.surface.cell_size[0]) + and all(math.dist(before[:2], failed[:2]) > 0.6 for failed in excluded)): + query = before[:2]+advance*continuation + nearby = np.linalg.norm(candidates[:, :2]-query, axis=1) + for index in np.argsort(nearby, kind="stable"): + if nearby[index] > .15: + break + selected = candidates[index] + clearance = self.approach_clearance(selected[None], pose, geometry, now, + lookahead_m=min(remaining, 3.5))[0] + if clearance < geometry['width_m'] or not supported(selected, + source="continuing-observed-passage"): + continue + self.evidence['approach_clearance_m'] = ( + float(clearance) if math.isfinite(clearance) else None) + return selected.tolist() if (0.8 < math.dist(before[:2], pose[:2]) <= 3.5 and math.dist(before[:2], target) < remaining - 0.1 and cell in allowed and matched.any() diff --git a/tests/test_ai_polygon_observed_goals.py b/tests/test_ai_polygon_observed_goals.py index c016ff07..449b5853 100644 --- a/tests/test_ai_polygon_observed_goals.py +++ b/tests/test_ai_polygon_observed_goals.py @@ -605,3 +605,52 @@ def test_explicit_route_direction_is_separate_from_remote_goal(selector): for target in ([8.,-5.],[8.,0.],[8.,5.],[675.,-9.])] assert goals[0] is not None assert all(np.array_equal(goal,goals[0]) for goal in goals) + + +@pytest.mark.parametrize('angle,shift', [(0,(0,0)),(73,(42,-17)),(-138,(-250,52))]) +def test_observed_through_passage_advances_without_chasing_a_new_lateral_centre(selector,angle,shift): + _,pose,_,_,rotation=road(angle,shift) + xy=np.array([[x,y] for x in np.arange(-1.,9.01,.08) + for y in np.arange(-2.5,4.01,.08)]) + points=np.c_[xy@rotation.T+shift,np.zeros(len(xy))] + geometry={**GEOMETRY,'length_m':1.3636,'max_step_m':.1} + selector.observe(np.c_[points,np.ones(len(points))],1.,pose) + goal=[*(np.asarray(shift)+rotation@[3.,0.]),0.] + original=dict(selector.surface.cells) + # A widened/partially observed section changes its instantaneous midpoint. + # The already selected, clear through-passage must keep its lateral line + # while advancing the local horizon as the chassis makes forward progress. + for forward in [1.,1.7,2.4,3.1]: + position=np.asarray(shift)+rotation@[forward,.25] + moved=[*position,.37,0,0,math.sin(math.radians(angle-10)/2),math.cos(math.radians(angle-10)/2)] + goal=selector.choose(points,moved,position+rotation@[6.,0.],goal,(),geometry,2., + travel_direction=rotation@[1.,0.]) + assert goal is not None + local=(np.asarray(goal[:2])-shift)@rotation + assert local[0]-forward>2.7 + assert abs(local[1])<.15 + assert np.min(np.linalg.norm(points-goal,axis=1))<1e-9 + assert dict(selector.surface.cells)==original + + +def test_through_passage_requires_measured_permission_and_current_clearance(selector): + points,pose,target,_,_=road() + # The first continuation is observed; its farther extrapolation is not. + selector.observe(np.c_[points,np.ones(len(points))],1.,pose) + prior=[1.5,0.,0.] + chosen=selector.choose(points,pose,target,prior,(),GEOMETRY,1.1,travel_direction=[1.,0.]) + assert chosen is not None and chosen[0]>2.7 and abs(chosen[1])<.15 + assert np.min(np.linalg.norm(points-chosen,axis=1))<1e-9 + # A pole on the continued line defeats retention before physical contact. + selector.observe_navigation(clearance_receipt(pose,[[2.3,0.]]),pose,1,'run',1.1) + blocked=selector.choose(points,pose,target,prior,(),GEOMETRY,1.2,travel_direction=[1.,0.]) + assert blocked is None or abs(blocked[1])>.3 + # An explicit mission exclusion still owns the candidate region. + limited=selector.choose(points,pose,target,prior,(),GEOMETRY,1.2, + lambda p:p[:,0]<2.,travel_direction=[1.,0.]) + assert limited is None or limited[0]<2. + # No remembered label can create unmeasured geometry beyond this cloud. + selector.clearance_observation=None + moved=[2.7,0.,.37,0.,0.,0.,1.] + end=selector.choose(points,moved,[8.,0.],chosen,(),GEOMETRY,1.3,travel_direction=[1.,0.]) + assert end is None or end[0]<=points[:,0].max() diff --git a/tests/test_nav2_route_support.py b/tests/test_nav2_route_support.py index bb47d8c5..cab6b350 100644 --- a/tests/test_nav2_route_support.py +++ b/tests/test_nav2_route_support.py @@ -262,51 +262,3 @@ def test_view_spin_is_not_restarted_by_translated_direction_endpoint(moving_brid result = navigation.plan(value) assert len(calls) == int(changed) assert result['controller_generation'] == (8 if changed else 7) - - -@pytest.mark.parametrize('world_yaw', [0., .73, -2.1]) -def test_lateral_passage_goal_keeps_continuation_heading_while_body_approaches(moving_bridge, world_yaw): - navigation, value, _, _ = moving_bridge - axes = np.array([[math.cos(world_yaw), -math.sin(world_yaw)], - [math.sin(world_yaw), math.cos(world_yaw)]]) - origin = np.array([42., -17.]) - goal = origin + axes @ [3., -1.2] - direction = axes @ [1., 0.] - value.update(goal=[*goal, 0.], route_transition_target=None, - orientation_target=dict(key='route-0-1', xy=[*goal], - direction_xy=direction.tolist())) - native_plan = navigation.node.plan - headings = [] - def capture(pose, selected, heading): - headings.append(heading) - np.testing.assert_allclose(selected[:2], goal) - return native_plan(pose, selected, heading) - navigation.node.plan = capture - for along, lateral, body_yaw in [(0., 0., 0.), (1., -.1, -.2), (2., -.5, -.6)]: - xy = origin + axes @ [along, lateral] - a = world_yaw + body_yaw - value['pose'] = [*xy, .37, 0., 0., math.sin(a/2), math.cos(a/2)] - navigation.path_planned_at = float('-inf') - assert navigation.plan(value)['status'] == 'path' - # The observed endpoint still selects the lateral passage. Approaching it - # must not turn its shrinking diagonal into a progressively sharper final - # body heading: this is through travel along one continuation, not parking. - np.testing.assert_allclose(headings, world_yaw, atol=1e-12) - - -@pytest.mark.parametrize('mode', ['unrouted', 'recovery', 'arrival', 'discrete']) -def test_continuation_heading_does_not_override_a_placement_or_recovery(moving_bridge, mode): - navigation, value, _, _ = moving_bridge - value['orientation_target']['direction_xy'] = [1., 0.] - if mode == 'unrouted':value['orientation_target'].pop('direction_xy') - elif mode == 'recovery':value['recovery_orientation'] = True - elif mode == 'arrival':value.update(arrival_target=[20., 0.], visual_goal_available=True) - else:value['continuous'] = False - native_plan = navigation.node.plan - headings = [] - def capture(pose, selected, heading): - headings.append(heading) - return native_plan(pose, selected, heading) - navigation.node.plan = capture - assert navigation.plan(value)['status'] == 'path' - assert headings == [math.atan2(.2, 4.)]