Guide local goals around observed obstacles and finish native recovery
This commit is contained in:
@@ -18,6 +18,7 @@ class ObservedGoals:
|
||||
self.surface = SemanticSurface(retain_near=True)
|
||||
self.measured = {}
|
||||
self.evidence = None
|
||||
self.clearance_observation = None
|
||||
|
||||
def observe(self, samples, captured_at, pose):
|
||||
self.surface.observe(samples, captured_at, pose)
|
||||
@@ -30,6 +31,59 @@ class ObservedGoals:
|
||||
if key in self.surface.cells
|
||||
and self.surface.retained(key, self.surface.cells[key][1], captured_at)}
|
||||
|
||||
def observe_navigation(self, result, pose, seconds, execution_inputs_sha256, captured_at):
|
||||
"""Use native observed geometry for intent, never as motion admission."""
|
||||
receipt = result.get("clearance_observation")
|
||||
expected = dict(pose=list(pose), simulation_time_seconds=seconds,
|
||||
execution_inputs_sha256=execution_inputs_sha256)
|
||||
if not isinstance(receipt, dict) or receipt.get("observation") != expected:
|
||||
return
|
||||
points = np.asarray(receipt.get("obstacles_xy"), dtype=float)
|
||||
if not points.size:
|
||||
points = np.empty((0, 2))
|
||||
if (points.ndim != 2 or points.shape[1] != 2 or len(points) > 30000
|
||||
or not np.isfinite(points).all() or not math.isfinite(captured_at)):
|
||||
raise ValueError("Invalid observed clearance guidance")
|
||||
self.clearance_observation = (captured_at, points.copy())
|
||||
|
||||
def approach_clearance(self, candidates, pose, geometry, now):
|
||||
"""Minimum obstacle distance along candidate approaches, in metres.
|
||||
|
||||
These straight segments rank spatial intentions; they are not paths.
|
||||
Smac still chooses the actual path and every commanded sweep must pass
|
||||
live body, support, semantic and freshness admission. No new free cells
|
||||
or positive surface labels are created here.
|
||||
"""
|
||||
distances = np.full(len(candidates), np.inf)
|
||||
observed = self.clearance_observation
|
||||
if observed is None or not 0 <= now-observed[0] <= .8 or not len(observed[1]):
|
||||
return distances
|
||||
obstacles = observed[1]
|
||||
xy = np.asarray(pose[:2])
|
||||
delta = candidates[:, :2]-xy
|
||||
length = np.linalg.norm(delta, axis=1)
|
||||
# The currently occupied body is shared by every approach. Compare the
|
||||
# corridor ahead of it, preserving the same full-body live veto below.
|
||||
start = np.minimum(length, geometry.get("length_m", geometry["width_m"])/2)
|
||||
direction = delta / np.maximum(length[:, None], 1e-9)
|
||||
low = np.minimum(xy, candidates[:, :2].min(0))-4*geometry['width_m']
|
||||
high = np.maximum(xy, candidates[:, :2].max(0))+4*geometry['width_m']
|
||||
obstacles = obstacles[np.all((obstacles >= low) & (obstacles <= high), axis=1)]
|
||||
if not len(obstacles):
|
||||
return distances
|
||||
offsets = obstacles-xy
|
||||
for first in range(0, len(candidates), 128):
|
||||
last = min(first+128, len(candidates))
|
||||
along = np.clip(direction[first:last] @ offsets.T,
|
||||
start[first:last, None], length[first:last, None])
|
||||
difference = (offsets[None]-along[:, :, None]*direction[first:last, None])
|
||||
distances[first:last] = np.sqrt(np.min(np.sum(difference*difference, axis=2), axis=1))
|
||||
return distances
|
||||
|
||||
def clearance_cost(self, candidates, pose, geometry, now):
|
||||
clearance = self.approach_clearance(candidates, pose, geometry, now)
|
||||
return 4*geometry['width_m']*np.exp(-clearance/geometry['width_m']), clearance
|
||||
|
||||
def boundary_cost(self, candidates, geometry, now):
|
||||
"""Soft clearance preference, like an inflation potential in a costmap.
|
||||
|
||||
@@ -137,6 +191,7 @@ class ObservedGoals:
|
||||
turning = np.where(reverse, math.pi-np.abs(errors), np.abs(errors))
|
||||
score = np.linalg.norm(candidates[:, :2]-target, axis=1) + .2*turning + .15*distance
|
||||
score += self.boundary_cost(candidates, geometry, now)
|
||||
score += self.clearance_cost(candidates, pose, geometry, now)[0]
|
||||
# Recovery actions need a fair turn, as in Nav2's RoundRobin recovery
|
||||
# subtree. Distance-to-task alone can spend every retry on almost the
|
||||
# same forward approach before ever considering a measured retreat.
|
||||
@@ -250,6 +305,7 @@ class ObservedGoals:
|
||||
|
||||
# A lens switch cannot replace a currently measured, permitted goal.
|
||||
# A new prohibition, missing current range or changed corridor can.
|
||||
retain = None
|
||||
if prior is not None:
|
||||
before = np.asarray(prior, dtype=float)
|
||||
matched = np.linalg.norm(candidates - before, axis=1) <= 0.15
|
||||
@@ -257,7 +313,7 @@ class ObservedGoals:
|
||||
and all(math.dist(before[:2], failed[:2]) > 0.6 for failed in excluded)
|
||||
and (candidate_mask is None or candidate_mask(before[None])[0])
|
||||
and supported(before)):
|
||||
return before.tolist()
|
||||
retain = before
|
||||
# Score the task direction at the existing 3.5 m observation horizon.
|
||||
# Distance to a remote endpoint mostly rewards longitudinal advance:
|
||||
# slightly farther side returns can beat a clear straight destination
|
||||
@@ -276,7 +332,27 @@ class ObservedGoals:
|
||||
bearing = np.arctan2(candidates[:,1]-pose[1],candidates[:,0]-pose[0])-heading
|
||||
score = (distance_to_target + .2*np.abs(np.arctan2(np.sin(bearing),np.cos(bearing)))
|
||||
+ self.boundary_cost(candidates, geometry, now))
|
||||
clearance_cost, clearances = self.clearance_cost(candidates, pose, geometry, now)
|
||||
score += clearance_cost
|
||||
retained_clearance = (self.approach_clearance(retain[None], pose, geometry, now)[0]
|
||||
if retain is not None else None)
|
||||
for index in np.argsort(score, kind="stable"):
|
||||
if supported(candidates[index]):
|
||||
return candidates[index].tolist()
|
||||
selected = candidates[index]
|
||||
if retain is not None and not (
|
||||
retained_clearance < geometry['width_m']
|
||||
and clearances[index] > retained_clearance + .1
|
||||
):
|
||||
selected = retain
|
||||
supported(retain)
|
||||
if self.evidence is not None:
|
||||
self.evidence['approach_clearance_m'] = (
|
||||
float(self.approach_clearance(selected[None], pose, geometry, now)[0])
|
||||
if self.clearance_observation is not None
|
||||
and 0 <= now-self.clearance_observation[0] <= .8
|
||||
and len(self.clearance_observation[1]) else None)
|
||||
if self.evidence is not None and not math.isfinite(
|
||||
self.evidence.get('approach_clearance_m') or 0.):
|
||||
self.evidence['approach_clearance_m'] = None
|
||||
return selected.tolist()
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user