fix(simulation): consume Nav2 local action completion

This commit is contained in:
DCCONSTRUCTIONS
2026-10-08 14:00:54 +03:00
parent 6a89f809a4
commit bc915c01e1
5 changed files with 149 additions and 3 deletions
@@ -213,6 +213,41 @@ def main():
# Warm the costmaps through normal planning before invoking a behavior; # Warm the costmaps through normal planning before invoking a behavior;
# lifecycle activation alone does not publish their first observation. # lifecycle activation alone does not publish their first observation.
assert result['controller_goal'][0]=='path',result assert result['controller_goal'][0]=='path',result
# Synthetic measured odometry enters Nav2's unchanged 40 cm region,
# while remaining outside the legacy mission recovery's 30 cm region.
# A capture refresh must return completion, not restart FollowPath.
reached=[.65,0.,.37,0.,0.,0.,1.]
with condition:
state.update(pose=reached,enabled=True)
retreat.update(pose=reached,observation_id='local-action-complete')
time.sleep(.15)
deadline=time.monotonic()+3
completion_plans=[]
while True:
result=request('/plan',retreat)
completion_plans.append(result)
if result.get('action_result') is not None:
break
assert time.monotonic()<deadline,completion_plans
time.sleep(.1)
expected=dict(status='succeeded',kind='path',goal_xy=[1.,0.])
assert result['action_result']==expected,result
assert result['speed_mps']==result['yaw_rate_rps']==0.,result
assert result['route_observation']['observation']['pose']==reached,result
for index in range(3):
retreat['observation_id']='completed-refresh-'+str(index)
repeated=request('/plan',retreat)
assert repeated.get('action_result')==expected,repeated
assert repeated['speed_mps']==repeated['yaw_rate_rps']==0.,repeated
report['local_action_completion']=completion_plans
# A different goal must start its own action, not inherit that result.
retreat.update(goal=[1.8,0.,0.],observation_id='after-completion-new-goal')
changed=request('/plan',retreat)
assert changed['status']=='path' and 'action_result' not in changed,changed
report['new_goal_after_completion']=changed
with condition:
state.update(pose=[0.,0.,.37,0.,0.,0.,1.],enabled=False)
retreat['pose']=[0.,0.,.37,0.,0.,0.,1.]
retreat.update(goal=[-1.,0.,0.],allow_reverse=True,observation_id='retreat-full-ground') retreat.update(goal=[-1.,0.,0.],allow_reverse=True,observation_id='retreat-full-ground')
result=request('/plan',retreat) result=request('/plan',retreat)
assert result['status']=='path' and result['controller_goal'][0]=='backup',result assert result['status']=='path' and result['controller_goal'][0]=='backup',result
+21
View File
@@ -83,6 +83,18 @@ class Navigation:
return dict(speed_mps=0., yaw_rate_rps=0., status=status, path=[], return dict(speed_mps=0., yaw_rate_rps=0., status=status, path=[],
diagnostic=dict(failure=reason), **extra) diagnostic=dict(failure=reason), **extra)
def completed_action(self, key, route):
terminal = self.node.terminal
if (key == self.action_key and key[0] in {'path', 'backup'}
and terminal is not None and terminal.done()
and terminal.result().status == GoalStatus.STATUS_SUCCEEDED):
# Preserve the native terminal result before a sensor refresh can
# submit the same FollowPath again. This is local-action completion,
# not whole-body arrival admission and never a motion proposal.
return self.reply('native-action-complete', route_observation=route,
action_result=dict(status='succeeded', kind=key[0], goal_xy=list(key[1])))
return None
def plan(self, value): def plan(self, value):
entered = time.monotonic() entered = time.monotonic()
if self.failure: if self.failure:
@@ -170,6 +182,9 @@ class Navigation:
self.node.stop() self.node.stop()
return self.reply('reverse-goal-is-not-behind', status='blocked') return self.reply('reverse-goal-is-not-behind', status='blocked')
key = ('backup', tuple(goal[:2]), limit) key = ('backup', tuple(goal[:2]), limit)
completed = self.completed_action(key, route)
if completed is not None:
return completed
if key != self.action_key: if key != self.action_key:
with self.owner_lock: with self.owner_lock:
self.action_key = None self.action_key = None
@@ -180,6 +195,9 @@ class Navigation:
# a path update; the existing capture leases and raw range gates # a path update; the existing capture leases and raw range gates
# still validate every command against the caller's observation. # still validate every command against the caller's observation.
key = ('path', tuple(goal[:2]), limit) key = ('path', tuple(goal[:2]), limit)
completed = self.completed_action(key, route)
if completed is not None:
return completed
continuing = key == self.action_key continuing = key == self.action_key
# Match the pinned Nav2 BT's 1 Hz ComputePathToPose cadence. # Match the pinned Nav2 BT's 1 Hz ComputePathToPose cadence.
# The path is an intent, not a sensor sample: terrain, costmaps, # The path is an intent, not a sensor sample: terrain, costmaps,
@@ -202,6 +220,9 @@ class Navigation:
for p in path.poses] for p in path.poses]
with self.owner_lock: with self.owner_lock:
self.action_key = key self.action_key = key
completed = self.completed_action(key, route)
if completed is not None:
return completed
native = self.node.native_command() native = self.node.native_command()
if native is None: if native is None:
terminal = self.node.terminal.result() if self.node.terminal and self.node.terminal.done() else None terminal = self.node.terminal.result() if self.node.terminal and self.node.terminal.done() else None
@@ -194,6 +194,7 @@ class WaypointMission:
attempt=self.attempts, attempt=self.attempts,
ended_seconds=seconds, ended_seconds=seconds,
failure=copy.deepcopy(self.recovery.failure), failure=copy.deepcopy(self.recovery.failure),
completion=copy.deepcopy(self.recovery.completion),
) )
self.failed_goals = list(self.recovery.excluded) self.failed_goals = list(self.recovery.excluded)
self.anchor_time = seconds self.anchor_time = seconds
@@ -35,6 +35,7 @@ class ObservedRecovery:
self.blockage = None self.blockage = None
self.blocked_option = False self.blocked_option = False
self.failure = None self.failure = None
self.completion = None
@property @property
def active(self): def active(self):
@@ -54,21 +55,36 @@ class ObservedRecovery:
self.blockage = None self.blockage = None
self.blocked_option = False self.blocked_option = False
self.failure = None self.failure = None
self.completion = None
def observe_navigation(self, result, pose, seconds, option_key, execution_inputs_sha256): def observe_navigation(self, result, pose, seconds, option_key, execution_inputs_sha256):
"""Retire a consistently vetoed intention, never grant motion. """Consume bound completion or retire a consistently vetoed intention.
Missing/old data and planner startup are not evidence of an obstacle. Missing/old data and planner startup are not evidence of an obstacle.
Bind both confirmations to this option, current sector, pose and run. Bind both confirmations to this option, current sector, pose and run.
""" """
if (self.phase != "executing" or option_key != (self.sequence, self.option) if self.phase != "executing" or option_key != (self.sequence, self.option):
or not self.view_current(pose, seconds)):
return return
observation = result.get("route_observation", {}).get("observation") observation = result.get("route_observation", {}).get("observation")
expected = dict(pose=list(pose), simulation_time_seconds=seconds, expected = dict(pose=list(pose), simulation_time_seconds=seconds,
execution_inputs_sha256=execution_inputs_sha256) execution_inputs_sha256=execution_inputs_sha256)
if observation != expected: if observation != expected:
return return
completion = result.get("action_result")
if (self.selected is not None and self.selected.get("kind") != "orient"
and result.get("status") == "waiting-for-plan"
and result.get("speed_mps") == result.get("yaw_rate_rps") == 0.
and completion == dict(status="succeeded",
kind="backup" if self.selected["reverse"] else "path",
goal_xy=list(self.selected["goal"][:2]))):
# Nav2 owns completion of its local action. A second distance
# threshold can reject a successfully reached, discretized goal.
# This ends intent only; it neither admits a command nor confirms
# mission arrival. No camera permission is needed to stop an action.
self.completion = completion
return
if not self.view_current(pose, seconds):
return
failure = result.get("diagnostic", {}).get("failure") failure = result.get("diagnostic", {}).get("failure")
hard_veto = failure in { hard_veto = failure in {
"no-path", "footprint", "observed-unsafe-support", "no-path", "footprint", "observed-unsafe-support",
@@ -132,6 +148,9 @@ class ObservedRecovery:
"xy": list(self.selected["goal"][:2])} "xy": list(self.selected["goal"][:2])}
def tick(self, pose, seconds, choose): def tick(self, pose, seconds, choose):
if self.completion is not None:
self.phase = "idle"
return None, "replanning"
stationary = False stationary = False
if self.previous is not None: if self.previous is not None:
before, stamp = self.previous before, stamp = self.previous
+70
View File
@@ -194,3 +194,73 @@ def test_repeated_fresh_body_veto_retires_option_without_waiting_progress_timeou
assert r.excluded == [selected['goal']] assert r.excluded == [selected['goal']]
r.tick(p,now+2,choose) r.tick(p,now+2,choose)
assert not r.blocked_option and r.blockage is None assert not r.blocked_option and r.blockage is None
def completed_path(position, seconds, goal):
return dict(status='waiting-for-plan', speed_mps=0., yaw_rate_rps=0.,
diagnostic=dict(failure='native-action-complete'),
action_result=dict(status='succeeded', kind='path', goal_xy=goal[:2]),
route_observation=dict(observation=dict(pose=position,
simulation_time_seconds=seconds, execution_inputs_sha256='run')))
def test_native_local_completion_returns_to_mission_without_rejecting_goal():
mission = WaypointMission([[10, 0]])
def choose(*_):
return [3., 0., 0.]
selected = dict(goal=[2., 0., 0.], reverse=False)
def maneuver(*_):
return selected
mission.update(pose(), 0, choose, choose_maneuver=maneuver)
mission.update(pose(), 8, choose, choose_maneuver=maneuver)
survey(mission.recovery, pose(), maneuver, 8)
# A native local action can succeed outside the legacy 30 cm test. It is
# completion of that action only, never the mission arrival receipt.
position, now = pose(1.61), 12.
receipt = completed_path(position, now, selected['goal'])
r = mission.recovery
r.observe_navigation(receipt, position, now, (r.sequence, r.option), 'run')
goal, intent = mission.update(position, now, choose, choose_maneuver=maneuver)
assert (goal, intent['state']) == (None, 'replanning')
assert not r.active and r.excluded == [[3., 0., 0.]] and mission.index == 0
assert intent['last_recovery_attempt']['completion'] == receipt['action_result']
goal, intent = mission.update(position, now+.1, choose, choose_maneuver=maneuver)
assert goal == [3., 0., 0.] and intent['state'] == 'following'
assert mission.fault is None
@pytest.mark.parametrize('mismatch', ['run', 'time', 'pose', 'option', 'goal', 'kind',
'failed', 'missing', 'nonzero', 'status'])
def test_unbound_or_unsuccessful_result_cannot_complete_local_recovery(mismatch):
r = ObservedRecovery()
r.begin(pose(), 0, [])
selected = dict(goal=[2., 0., 0.], reverse=False)
def choose(*_):
return selected
survey(r, pose(), choose)
position, now = pose(1.61), 2.
receipt = completed_path(position, now, selected['goal'])
key, run = (r.sequence, r.option), 'run'
if mismatch == 'run':
run = 'another-run'
elif mismatch == 'time':
now += .1
elif mismatch == 'pose':
position = pose(1.6)
elif mismatch == 'option':
key = (r.sequence, r.option+1)
elif mismatch == 'goal':
receipt['action_result']['goal_xy'] = [3., 0.]
elif mismatch == 'kind':
receipt['action_result']['kind'] = 'spin'
elif mismatch == 'failed':
receipt['action_result']['status'] = 'aborted'
elif mismatch == 'missing':
receipt.pop('action_result')
elif mismatch == 'nonzero':
receipt['speed_mps'] = .1
elif mismatch == 'status':
receipt['status'] = 'blocked'
r.observe_navigation(receipt, position, now, key, run)
r.tick(position, now+.1, choose)
assert r.active and r.selected == selected