Look beyond short local goals and stabilize observed surface intent
This commit is contained in:
@@ -6,8 +6,10 @@ the native planner still admits the complete commanded body sweep.
|
||||
"""
|
||||
|
||||
import math
|
||||
from collections import OrderedDict
|
||||
|
||||
import numpy as np
|
||||
from scipy.spatial import cKDTree
|
||||
|
||||
from .semantic_surface import SemanticSurface
|
||||
|
||||
@@ -15,13 +17,25 @@ from .semantic_surface import SemanticSurface
|
||||
class ObservedGoals:
|
||||
def __init__(self, *, reverse_heading_tolerance=math.pi/4):
|
||||
self.reverse_heading_tolerance = reverse_heading_tolerance
|
||||
self.surface = SemanticSurface(retain_near=True)
|
||||
# Intent and native terrain use the same bounded, capture-deduplicated
|
||||
# evidence rule. A single conflicting segmentation must not cancel a
|
||||
# progressing path that repeated observations still support. Geometry
|
||||
# and live swept-body guards remain immediate and independent.
|
||||
self.surface = SemanticSurface(retain_near=True, fuse_observations=True)
|
||||
self.measured = {}
|
||||
self.seen_semantics = OrderedDict()
|
||||
self.evidence = None
|
||||
self.clearance_observation = None
|
||||
self.clearance_index = None
|
||||
|
||||
def observe(self, samples, captured_at, pose):
|
||||
self.surface.observe(samples, captured_at, pose)
|
||||
def observe(self, samples, captured_at, pose, *, frame_id=None, capture_id=None):
|
||||
if frame_id is not None and frame_id in self.seen_semantics:
|
||||
return
|
||||
self.surface.observe(samples, captured_at, pose, evidence_id=capture_id)
|
||||
if frame_id is not None:
|
||||
self.seen_semantics[frame_id] = True
|
||||
while len(self.seen_semantics) > 128:
|
||||
self.seen_semantics.popitem(last=False)
|
||||
sample = np.asarray(samples, dtype=float).reshape(-1, 4)
|
||||
for key, point in zip(self.surface.cell_keys(sample), sample, strict=True):
|
||||
self.measured[tuple(key)] = point[:3].copy()
|
||||
@@ -46,7 +60,7 @@ class ObservedGoals:
|
||||
raise ValueError("Invalid observed clearance guidance")
|
||||
self.clearance_observation = (captured_at, points.copy())
|
||||
|
||||
def approach_clearance(self, candidates, pose, geometry, now):
|
||||
def approach_clearance(self, candidates, pose, geometry, now, *, lookahead_m=None):
|
||||
"""Minimum obstacle distance along candidate approaches, in metres.
|
||||
|
||||
These straight segments rank spatial intentions; they are not paths.
|
||||
@@ -58,34 +72,40 @@ class ObservedGoals:
|
||||
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]
|
||||
if not len(candidates):
|
||||
return distances
|
||||
if self.clearance_index is None or self.clearance_index[0] is not observed:
|
||||
self.clearance_index = (observed, cKDTree(observed[1]))
|
||||
tree = self.clearance_index[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
|
||||
squared = np.sum(offsets*offsets, axis=1)
|
||||
if lookahead_m is not None:
|
||||
# Compare bearings over one common horizon. A short destination
|
||||
# before an obstacle must not hide that obstacle from intent cost.
|
||||
# This extends a scoring query, never the measured drive goal.
|
||||
length = np.full(len(candidates), lookahead_m)
|
||||
start = np.minimum(length, geometry.get("length_m", geometry["width_m"])/2)
|
||||
# Nearest-obstacle distance is 1-Lipschitz. Sampling at <= 5 cm and
|
||||
# subtracting half the actual spacing gives a conservative continuous
|
||||
# segment lower bound (error <= 2.5 cm), without an N*M hazard matrix.
|
||||
# This is a soft ranking cost only, never a swept-body safety receipt.
|
||||
for first in range(0, len(candidates), 128):
|
||||
last = min(first+128, len(candidates))
|
||||
# Exact point-to-segment distance without an N*M*2 temporary or
|
||||
# a threaded BLAS call for a two-coordinate product.
|
||||
projection = (direction[first:last, 0, None]*offsets[None, :, 0]
|
||||
+ direction[first:last, 1, None]*offsets[None, :, 1])
|
||||
along = np.clip(projection, start[first:last, None], length[first:last, None])
|
||||
square_distance = squared[None] + along*(along-2*projection)
|
||||
distances[first:last] = np.sqrt(np.maximum(0., np.min(square_distance, axis=1)))
|
||||
steps = np.maximum(1, np.ceil((length[first:last]-start[first:last])/.05).astype(int))
|
||||
spacing = (length[first:last]-start[first:last])/steps
|
||||
along = (start[first:last, None]
|
||||
+ np.minimum(np.arange(int(steps.max())+1)[None], steps[:, None])
|
||||
* spacing[:, None])
|
||||
queries = xy + along[:, :, None]*direction[first:last, None]
|
||||
nearest = tree.query(queries.reshape(-1, 2), workers=1)[0].reshape(along.shape)
|
||||
distances[first:last] = np.maximum(0., nearest.min(axis=1)-spacing/2)
|
||||
return distances
|
||||
|
||||
def clearance_cost(self, candidates, pose, geometry, now):
|
||||
clearance = self.approach_clearance(candidates, pose, geometry, now)
|
||||
def clearance_cost(self, candidates, pose, geometry, now, *, lookahead_m=None):
|
||||
clearance = self.approach_clearance(candidates, pose, geometry, now,
|
||||
lookahead_m=lookahead_m)
|
||||
return 4*geometry['width_m']*np.exp(-clearance/geometry['width_m']), clearance
|
||||
|
||||
def boundary_cost(self, candidates, geometry, now):
|
||||
@@ -318,7 +338,9 @@ class ObservedGoals:
|
||||
and (candidate_mask is None or candidate_mask(before[None])[0])
|
||||
and supported(before)):
|
||||
retain = before
|
||||
retained_clearance = (self.approach_clearance(retain[None], pose, geometry, now)[0]
|
||||
horizon = min(remaining, 3.5)
|
||||
retained_clearance = (self.approach_clearance(
|
||||
retain[None], pose, geometry, now, lookahead_m=horizon)[0]
|
||||
if retain is not None else None)
|
||||
if retain is not None and retained_clearance >= geometry['width_m']:
|
||||
# The selection rule below cannot replace this still supported
|
||||
@@ -333,7 +355,6 @@ class ObservedGoals:
|
||||
# and make successive measured goals oscillate across the corridor.
|
||||
# This reference only ranks actual permitted returns. It is never a
|
||||
# generated drive goal, a straight-line route or a clearance receipt.
|
||||
horizon = min(remaining, 3.5)
|
||||
reference = pose[:2] + forward * horizon
|
||||
# A smooth local attraction, normalized to metres like boundary_cost.
|
||||
# Its gradient diminishes near the reference so centimetres of extra
|
||||
@@ -345,7 +366,8 @@ 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)
|
||||
clearance_cost, clearances = self.clearance_cost(
|
||||
candidates, pose, geometry, now, lookahead_m=horizon)
|
||||
score += clearance_cost
|
||||
for index in np.argsort(score, kind="stable"):
|
||||
if supported(candidates[index]):
|
||||
|
||||
@@ -567,7 +567,8 @@ class ComposedInference:
|
||||
semantic_observation = inputs.get("source.semantic-observation")
|
||||
if observed_goals is not None and semantic_observation is not None:
|
||||
for sample in semantic_observation:
|
||||
observed_goals.observe(sample["samples"], sample["captured_at"], sample["pose"])
|
||||
observed_goals.observe(sample["samples"], sample["captured_at"], sample["pose"],
|
||||
frame_id=sample.get("frame_id"), capture_id=sample.get("capture_id"))
|
||||
elif observed_goals is not None:
|
||||
calibration = inputs["source.camera.calibration"]
|
||||
points = inputs["source.lidar"]
|
||||
|
||||
Reference in New Issue
Block a user