diff --git a/simulation/ai-polygon/nav2/server.py b/simulation/ai-polygon/nav2/server.py index 0572c17a..188a6689 100644 --- a/simulation/ai-polygon/nav2/server.py +++ b/simulation/ai-polygon/nav2/server.py @@ -283,17 +283,33 @@ class Navigation: or entered-self.path_planned_at >= 1.0): 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 - # terminal abort without inventing an obstacle diagnosis. - feedback = (dict(planning_result=dict(status='aborted', goal_xy=goal[:2].tolist())) - if status == GoalStatus.STATUS_ABORTED else {}) - return self.reply('no-path', status='blocked', route_observation=route, **feedback) - if not continuing: - self.revoke_command() - self.node.follow(path, continuing=continuing) - self.path, self.path_planned_at = path, entered - self.path_context = context + native = self.node.native_command() + # A rejected extension does not invalidate the executing + # path. Never restart it or recycle its old velocity: keep + # only a live same-leg action, admitted below against this + # observation and against current range on every tick. + if (status == GoalStatus.STATUS_ABORTED and following and continuing + and context is not None and value.get('arrival_target') is None + and key != self.action_key and self.path is not None + and len(self.path.poses) >= 2 and self.node.terminal is not None + and not self.node.terminal.done() and native is not None + and time.monotonic()-native[0] <= .25): + receipts['refinement_result'] = dict(status='aborted', + goal_xy=goal[:2].tolist(), executing_goal_xy=list(self.action_key[1])) + key = self.action_key + else: + self.stop() + # Humble has no typed planner error code. Preserve its + # terminal abort without inventing an obstacle diagnosis. + feedback = (dict(planning_result=dict(status='aborted', goal_xy=goal[:2].tolist())) + if status == GoalStatus.STATUS_ABORTED else {}) + return self.reply('no-path', status='blocked', route_observation=route, **feedback) + else: + if not continuing: + self.revoke_command() + self.node.follow(path, continuing=continuing) + self.path, self.path_planned_at = path, entered + self.path_context = context path = self.path c, s = math.cos(yaw_of(pose)), math.sin(yaw_of(pose)) local_path = [[(p.pose.position.x-pose[0])*c+(p.pose.position.y-pose[1])*s, diff --git a/tests/test_nav2_route_support.py b/tests/test_nav2_route_support.py index cab6b350..a7c62555 100644 --- a/tests/test_nav2_route_support.py +++ b/tests/test_nav2_route_support.py @@ -27,7 +27,7 @@ def bridge(monkeypatch): scope = dict(np=np, math=math, time=time, route_support=route_support, route_observation=route_observation, validate_orientation=validate_orientation, orientation_identity=orientation_identity, - alignment_angle=lambda *args: None) + alignment_angle=lambda *args, **kwargs: None) exec(compile(ast.Module(body=[cls], type_ignores=[]), str(root / 'nav2/server.py'), 'exec'), scope) navigation = scope['Navigation'].__new__(scope['Navigation']) navigation.failure = None @@ -262,3 +262,93 @@ 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.fixture +def rejected_extension(moving_bridge): + navigation, value, planned, seen = moving_bridge + path, _ = navigation.node.plan(value['pose'], [3., 0., 0.], 0.) + navigation.path, navigation.path_planned_at = path, time.monotonic() + navigation.node.plan = lambda *args: (None, 6) + return navigation, value, path, seen + + +def test_rejected_extension_preserves_live_action_and_admits_current_observation(rejected_extension): + navigation, value, path, seen = rejected_extension + accepted, stopped = [], [] + navigation.control.accept = lambda *args, **kwargs: accepted.append((args, kwargs)) + navigation.node.stop = lambda: stopped.append(True) + result = navigation.plan(value) + assert result['controller_goal'] == ('path', (3., 0.), 1.) + assert result['controller_generation'] == 7 + assert result['refinement_result'] == dict(status='aborted', goal_xy=[4., .2], + executing_goal_xy=[3., 0.]) + assert 'planning_result' not in result and 'action_result' not in result + assert result['status'] == 'path' and result['speed_mps'] == .4 + assert navigation.path is path and not seen and not stopped + observed = navigation.terrain.observe(value) + assert accepted[0][0][0] is value + assert accepted[0][0][2] is observed['terrain'] + assert accepted[0][1]['collision_terrain'] is observed['collision_terrain'] + # Completion stays terminal; the rejection must not resurrect FollowPath. + navigation.node.terminal.set_result(SimpleNamespace(status=4)) + value['goal'] = [3., 0., 0.] + done = navigation.plan(value) + assert done['action_result']['status'] == 'succeeded' + assert done['speed_mps'] == 0. and not seen + + +@pytest.mark.parametrize('condition', [ + 'same-goal', 'leg', 'direction', 'limit', 'recovery', 'unbound', 'placement', + 'same-placement', 'discrete', 'completed', 'aborted', 'no-command', + 'stale-command', 'short-path', 'cancelled-planning']) +def test_rejected_extension_cannot_preserve_invalid_or_unrelated_action(rejected_extension, condition): + navigation, value, _, seen = rejected_extension + if condition == 'same-goal': value['goal'] = [3., 0., 0.] + elif condition == 'leg': value['orientation_target']['key'] = 'route-1-2' + elif condition == 'direction': value['orientation_target']['xy'] = [20., 4.] + elif condition == 'limit': value['max_speed_mps'] = .3 + elif condition == 'recovery': value['recovery_orientation'] = True + elif condition == 'unbound': value['orientation_target'] = None + elif condition in {'placement', 'same-placement'}: + value['arrival_target'] = [20., 0.] + if condition == 'same-placement': + navigation.path_context = navigation.forward_context(value, value['orientation_target']) + elif condition == 'discrete': value['continuous'] = False + elif condition in {'completed', 'aborted'}: + def plan(*args): + navigation.node.terminal.set_result(SimpleNamespace(status=4 if condition == 'completed' else 6)) + return None, 6 + navigation.node.plan = plan + elif condition == 'no-command': navigation.node.native_command = lambda: None + elif condition == 'stale-command': navigation.node.native_command = lambda: (time.monotonic()-1., .4, .02) + elif condition == 'short-path': navigation.path.poses = navigation.path.poses[:1] + else: navigation.node.plan = lambda *args: (None, 5) + navigation.path_planned_at = float('-inf') + stopped = [] + navigation.node.stop = lambda: stopped.append(navigation.native_command()) + result = navigation.plan(value) + assert result['status'] == 'blocked' and result['speed_mps'] == result['yaw_rate_rps'] == 0. + assert result['diagnostic']['failure'] == 'no-path' + assert 'refinement_result' not in result and not seen + assert stopped == [None] and navigation.command_generation == 8 + + +def test_continued_path_uses_live_collision_gate_after_refinement_failure(rejected_extension): + from navigation.continuous_control import ContinuousControl + from navigation.support_policy import SupportPolicy + navigation, value, _, _ = rejected_extension + observed = navigation.terrain.observe(value) + value['points'] = observed['terrain'][:, :3] + gate = ContinuousControl(navigation.monitor, SupportPolicy(navigation.monitor), + lambda _: None, lambda *_: None, navigation.native_command, native_only=True, body_top_m=1.) + navigation.control = gate + navigation.plan(value) + tick = dict(pose=value['pose'], pose_age_seconds=0., perception_age_seconds=0., + enabled=True, observation_id=value['observation_id'], speed_mps=0.) + assert gate.tick(tick)['speed_mps'] > 0. + gate.observe(dict(geometry_age_seconds=0., range_sequence=[1, 2], + points=np.r_[value['points'], [[.525, 0., .5]]])) + result = gate.tick(tick) + assert result['speed_mps'] == result['yaw_rate_rps'] == 0. + assert result['reason'] == 'obstacle'