Preserve admitted command ownership across route path refinements

This commit is contained in:
DCCONSTRUCTIONS
2026-10-09 20:37:04 +03:00
parent 286e1fbf33
commit b2faab8ada
4 changed files with 145 additions and 27 deletions
@@ -88,7 +88,14 @@ def main():
before = bridge.command_sequence
continuous_updates.set()
refresh_ms=[]
for _ in range(6):
refresh_goals = []
for index in range(6):
# Measured local destinations advance inside one route leg. Use
# actual different Smac paths, not six copies of one endpoint.
goal = [3. + .15*index, 1. + .05*(index%2)]
planned, status = bridge.plan(pose, goal, math.atan2(goal[1], goal[0]))
assert planned is not None and len(planned.poses) > 1
refresh_goals.append(goal)
began=time.monotonic()
assert bridge.follow(planned, continuing=True) is not None
refresh_ms.append((time.monotonic()-began)*1000)
@@ -96,7 +103,8 @@ def main():
continuous_updates.clear()
assert continuity_samples and all(x is not None for x in continuity_samples)
report['uninterrupted_command_samples'] = len(continuity_samples)
report['same_goal_refresh_ms'] = refresh_ms
report['refined_goal_refresh_ms'] = refresh_ms
report['refined_goals'] = refresh_goals
with bridge.condition:
updates = [row for row in bridge.history if row[0] > before]
assert updates and all(row[2:] != (0., 0.) for row in updates), updates
+5 -4
View File
@@ -2,7 +2,8 @@
The caller spins this node and owns observation/command leases. Repeating TF
uses the original measurement stamp. FollowPath updates use Nav2's native
preemption; command admission resumes after feedback for the new goal.
preemption within one caller-bound route context. A new motion task requires
fresh action feedback before command admission resumes.
"""
import math
import threading
@@ -254,8 +255,8 @@ class RosNavigation(Node):
token = object()
received = threading.Event()
with self.condition:
# Updating geometry for the same goal preserves the native
# command stream. A changed goal still needs new feedback.
# Refining the path within the same caller-bound route context
# preserves its native stream. New tasks need fresh feedback.
self.pending_feedback = None if continuing else token
if not continuing:
self.latest_command = None
@@ -280,7 +281,7 @@ class RosNavigation(Node):
raise RuntimeError('Nav2 path update rejected')
self.action_handle, self.terminal = handle, handle.get_result_async()
if continuing:
# This same-goal stream is already admitted during preemption.
# This continuing stream is already admitted during preemption.
# Waiting for its feedback only delays the next sensor frame;
# accept() still fences rejection and late action acceptance.
return self.native_command()
+45 -21
View File
@@ -45,6 +45,8 @@ class Navigation:
self.collision = ObservedCollision(node, self.monitor.max_step, 2)
self.plan_lock, self.owner_lock = threading.Lock(), threading.Lock()
self.action_key = None
self.command_generation = 0
self.path_context = None
self.completed_orientation = None
self.attempted_alignment = None
self.path = None
@@ -68,20 +70,41 @@ class Navigation:
with self.control.lock:
plan = self.control.plans.get(self.control.path_id)
with self.owner_lock:
if plan is None or plan['result'].get('controller_goal') != self.action_key:
if plan is None or plan['result'].get('controller_generation') != self.command_generation:
return None
return self.node.native_command()
def tick(self, value):
self.node.observe_pose(value['pose'], float(value['pose_age_seconds']), value['measured_twist'])
with self.owner_lock:
owner = self.action_key
owner = self.command_generation
result = self.control.tick(value)
with self.owner_lock:
if owner != self.action_key:
if owner != self.command_generation:
result.update(speed_mps=0., yaw_rate_rps=0., reason='changed-path')
return result
def revoke_command(self):
with self.owner_lock:
self.command_generation += 1
self.action_key = None
def stop(self):
self.revoke_command()
self.node.stop()
@staticmethod
def forward_context(value, orientation):
# Only refinements within the same ordinary route leg may share a
# native command stream. Recovery, an unbound intent, a changed speed
# limit or terminal placement all require a new action/feedback fence.
if (orientation is None or value.get('recovery_orientation', False)
or not value.get('continuous', False)):
return None
target = value.get('arrival_target')
return (orientation['key'], tuple(orientation['xy']),
value['max_speed_mps'], tuple(target) if target is not None else None)
@staticmethod
def reply(reason, *, status='waiting-for-plan', **extra):
return dict(speed_mps=0., yaw_rate_rps=0., status=status, path=[],
@@ -166,9 +189,7 @@ class Navigation:
if math.dist(pose[:2], target) <= self.inputs['mission']['arrival_radius_m']:
assessment = self.monitor.assess_pose(terrain, pose, value['body_contact_height_m'])
if assessment['admitted']:
self.node.stop()
with self.owner_lock:
self.action_key = None
self.stop()
return self.reply('arrival', status='arrival-accepted', arrival={**assessment,
'observation':dict(pose=pose.tolist(), target_xy=list(target),
simulation_time_seconds=value['simulation_time_seconds'],
@@ -209,7 +230,7 @@ class Navigation:
and orientation_key != self.completed_orientation
and not value['translation_allowed'])
if not value['translation_allowed'] and not orienting:
self.node.stop()
self.stop()
return self.reply('no-permitted-goal', status='blocked', route_observation=route)
if orienting:
automatic = aligning or turn is not None
@@ -218,8 +239,7 @@ class Navigation:
angle = math.atan2(math.sin(heading-yaw_of(pose)), math.cos(heading-yaw_of(pose)))
key = alignment_key if automatic else orientation_key
if key != self.action_key:
with self.owner_lock:
self.action_key = None
self.revoke_command()
# Existing mission budgets use simulation time. Do not invent
# a second wall-clock maneuver budget in a slower simulation.
# The unchanged command/data leases still gate every output.
@@ -236,40 +256,42 @@ class Navigation:
heading = yaw_of(pose)
rear = -float(delta @ [math.cos(heading), math.sin(heading)])
if rear <= 0:
self.node.stop()
self.stop()
return self.reply('reverse-goal-is-not-behind', status='blocked')
if key != self.action_key:
with self.owner_lock:
self.action_key = None
self.revoke_command()
self.node.backup(rear, min(limit,.1), 0.)
local_path = [[0.,0.,0.],[-rear,0.,0.]]
else:
# A sensor refresh is not a new motion goal. ROS feedback fences
# a path update; the existing capture leases and raw range gates
# still validate every command against the caller's observation.
# Local endpoint refinements can share one route command stream.
# A different route/recovery context gets a fresh feedback fence;
# every tick still uses its capture leases and raw range gates.
key = ('path', tuple(goal[:2]), limit)
completed = self.completed_action(key, route)
if completed is not None:
return completed
continuing = key == self.action_key
context = self.forward_context(value, orientation)
continuing = ((key == self.action_key and context == self.path_context)
or (following and context is not None and context == self.path_context))
# Match the pinned Nav2 BT's 1 Hz ComputePathToPose cadence.
# The path is an intent, not a sensor sample: terrain, costmaps,
# capture leases and command gates still refresh above/every tick.
if (not continuing or self.path is None or self.node.native_command() is None
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):
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.node.stop()
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:
with self.owner_lock:
self.action_key = None
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,
@@ -286,7 +308,7 @@ class Navigation:
return self.reply('native-action-complete' if terminal and terminal.status==GoalStatus.STATUS_SUCCEEDED
else 'waiting-for-controller', route_observation=route)
result = dict(speed_mps=native[1], yaw_rate_rps=native[2], status='path', path=local_path,
controller_goal=key,
controller_goal=key, controller_generation=self.command_generation,
route_observation=route, diagnostic=dict(terrain=observed['metrics'], controller='nav2'))
# This is a proposal returned to the mission layer; /control supplies
# the separately admitted command bound to current pose/range/intent.
@@ -334,6 +356,8 @@ def main():
current_observation_id=observed['observation_id'] if observed else None,
current_metrics=observed['metrics'] if observed else None,
current_detected=observed['detected'].tolist() if observed else [],
current_collision_terrain=observed['collision_terrain'].tolist() if observed else [],
current_surface_terrain=observed['terrain'].tolist() if observed else [],
geometry=observed['physical_terrain'].tolist() if observed else []))
return
ready = process.poll() is None and terrain_ready.service_is_ready() and not navigation.failure