Preserve native command ownership for a moving direction hint

This commit is contained in:
DCCONSTRUCTIONS
2026-10-10 00:46:23 +03:00
parent 41fa11e6c8
commit 04b940e8e1
4 changed files with 70 additions and 7 deletions
+44 -1
View File
@@ -17,6 +17,7 @@ def bridge(monkeypatch):
root = Path(__file__).parents[1] / 'simulation/ai-polygon'
monkeypatch.syspath_prepend(str(root))
monkeypatch.syspath_prepend(str(root / 'nav2'))
from navigation.arrival_approach import validate_orientation, orientation_identity
from navigation.footprint import Footprint
from navigation.route_observation import route_observation
from route_support import route_support
@@ -24,7 +25,8 @@ def bridge(monkeypatch):
tree = ast.parse((root / 'nav2/server.py').read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == 'Navigation')
scope = dict(np=np, math=math, time=time, route_support=route_support,
route_observation=route_observation, validate_orientation=lambda _: None,
route_observation=route_observation, validate_orientation=validate_orientation,
orientation_identity=orientation_identity,
alignment_angle=lambda *args: None)
exec(compile(ast.Module(body=[cls], type_ignores=[]), str(root / 'nav2/server.py'), 'exec'), scope)
navigation = scope['Navigation'].__new__(scope['Navigation'])
@@ -219,3 +221,44 @@ def test_planner_rejection_revokes_inflight_command_before_native_stop(moving_br
result = navigation.tick(dict(pose=value['pose'], pose_age_seconds=0., measured_twist=[.4,0.,0.]))
assert result == dict(speed_mps=0., yaw_rate_rps=0., reason='changed-path')
assert seen == [None]
@pytest.mark.parametrize('changed', [False, True])
def test_directional_route_translation_preserves_owner_but_turn_revokes_it(moving_bridge, changed):
navigation, value, _, seen = moving_bridge
orientation = dict(key='route-0-1', xy=[6., 0.], direction_xy=[1., 0.])
navigation.path_context = navigation.forward_context(value, orientation)
# The rover moves one metre. Its six-metre bearing endpoint moves too.
# Repeated sensor refreshes must not cancel FollowPath or its live command.
value['orientation_target'] = {**orientation, 'xy': [7., 0.]}
if changed:
value['orientation_target'].update(xy=[1., 6.], direction_xy=[0., 1.])
result = navigation.plan(value)
assert result['controller_generation'] == (8 if changed else 7)
assert seen[0]['continuing'] is (not changed)
assert (seen[0]['command'] is not None) is (not changed)
@pytest.mark.parametrize('direction', [None, [0., 0.], [2., 0.], [True, 0.], [float('nan'), 0.], [1.]])
def test_orientation_direction_must_be_finite_unit_vector(bridge, direction):
navigation, _, value = bridge
value['orientation_target'] = dict(key='route-0-1', xy=[6., 0.], direction_xy=direction)
with pytest.raises(ValueError, match='orientation direction'):
navigation.plan(value)
@pytest.mark.parametrize('changed', [False, True])
def test_view_spin_is_not_restarted_by_translated_direction_endpoint(moving_bridge, changed):
navigation, value, _, _ = moving_bridge
identity = navigation._plan.__globals__['orientation_identity']
orientation = dict(key='route-0-1', xy=[6., 0.], direction_xy=[1., 0.])
navigation.action_key = ('spin', *identity(orientation))
calls = []
navigation.node.spin = lambda *args: calls.append(args)
value.update(translation_allowed=False,
orientation_target={**orientation, 'xy': [7., 0.]})
if changed:
value['orientation_target'].update(xy=[1., 6.], direction_xy=[0., 1.])
result = navigation.plan(value)
assert len(calls) == int(changed)
assert result['controller_generation'] == (8 if changed else 7)