Preserve admitted command ownership across route path refinements
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user