Batch exact ground support queries in the control loop
This commit is contained in:
@@ -38,8 +38,17 @@ class SupportSurface:
|
||||
if not ground.any():
|
||||
return points[ground]
|
||||
# Match the observed range voxel resolution; no scene-dependent scale.
|
||||
_, inverse = np.unique(np.floor(points[:, :2] / .06).astype(int),
|
||||
axis=0, return_inverse=True)
|
||||
columns = np.floor(points[:, :2] / .06).astype(np.int64)
|
||||
# A bounded sensor cloud can group XY columns with a scalar integer
|
||||
# key. This preserves exact column membership without NumPy's costly
|
||||
# structured-row sort on every control tick. Fall back for wide inputs.
|
||||
extent = [int(columns[:, i].max())-int(columns[:, i].min())+1 for i in (0, 1)]
|
||||
if extent[0] * extent[1] <= np.iinfo(np.int64).max:
|
||||
relative = columns - columns.min(axis=0)
|
||||
keys = relative[:, 0] * extent[1] + relative[:, 1]
|
||||
_, inverse = np.unique(keys, return_inverse=True)
|
||||
else:
|
||||
_, inverse = np.unique(columns, axis=0, return_inverse=True)
|
||||
lowest = np.full(int(inverse.max()) + 1, np.inf)
|
||||
np.minimum.at(lowest, inverse[ground], points[ground, 2])
|
||||
obstacle_top = np.full(len(lowest), -np.inf)
|
||||
@@ -94,27 +103,37 @@ class SupportSurface:
|
||||
height discontinuity retains the raw veto. Unknown ground is unchanged.
|
||||
"""
|
||||
offsets = np.asarray(offsets, dtype=float).reshape(-1, 2)
|
||||
states, reasons = self.classify(offsets)
|
||||
states, reasons = states.astype(object), Counter(reasons)
|
||||
states, raw_reasons = self._classify_details(offsets)
|
||||
states, reasons = states.astype(object), Counter(raw_reasons)
|
||||
spacing, radius = .05, .15
|
||||
delta = np.array([[x*spacing, y*spacing] for x in range(-3,4)
|
||||
for y in range(-3,4) if x*x+y*y<=9])
|
||||
critical = math.floor(2*radius*(contact_width_m/3)/spacing**2)
|
||||
for i in np.flatnonzero(states == 'observed-unsafe'):
|
||||
point = offsets[i]
|
||||
_, raw = self.classify([point])
|
||||
reason = next(iter(raw))
|
||||
if reason not in {'slope', 'roughness'}:
|
||||
continue
|
||||
key = (tuple(np.round(point,12)), float(contact_width_m))
|
||||
candidates = np.flatnonzero((states == 'observed-unsafe')
|
||||
& np.isin(raw_reasons, ['slope', 'roughness']))
|
||||
keys = {i: (tuple(np.round(offsets[i], 12)), float(contact_width_m))
|
||||
for i in candidates}
|
||||
pending = {}
|
||||
for i, key in keys.items():
|
||||
if key not in self.contact_results:
|
||||
_, surrounding = self.classify(point+delta)
|
||||
forbidden = set(surrounding)-{'observed-patch','slope','roughness'}
|
||||
count = surrounding.get('slope',0)+surrounding.get('roughness',0)
|
||||
self.contact_results[key] = not forbidden and count<=critical
|
||||
pending.setdefault(key, offsets[i])
|
||||
items = list(pending.items())
|
||||
# Classify each identical 29-point disk in bounded batches. Re-entering
|
||||
# the native wrapper per cell scanned the full return cloud hundreds
|
||||
# of times; batching changes neither samples nor the relief rule.
|
||||
for start in range(0, len(items), 64):
|
||||
batch = items[start:start+64]
|
||||
queries = np.array([point for _, point in batch])[:, None, :] + delta
|
||||
_, surrounding = self._classify_details(queries.reshape(-1, 2))
|
||||
surrounding = surrounding.reshape(len(batch), len(delta))
|
||||
permitted = np.isin(surrounding, ['observed-patch', 'slope', 'roughness'])
|
||||
counts = np.isin(surrounding, ['slope', 'roughness']).sum(axis=1)
|
||||
for (key, _), allowed in zip(batch, permitted.all(axis=1) & (counts <= critical), strict=True):
|
||||
self.contact_results[key] = bool(allowed)
|
||||
for i, key in keys.items():
|
||||
if self.contact_results[key]:
|
||||
states[i] = 'observed-supported'
|
||||
reasons[reason] -= 1
|
||||
reasons[raw_reasons[i]] -= 1
|
||||
reasons['observed-local-relief'] += 1
|
||||
return states, {key:value for key,value in reasons.items() if value}
|
||||
|
||||
@@ -158,6 +177,10 @@ class SupportSurface:
|
||||
|
||||
def classify(self, offsets):
|
||||
"""Return observed-supported, observed-unsafe or unknown per XY sample."""
|
||||
states, reasons = self._classify_details(offsets)
|
||||
return states, dict(Counter(reasons))
|
||||
|
||||
def _classify_details(self, offsets):
|
||||
offsets = np.asarray(offsets, dtype=float).reshape(-1, 2)
|
||||
if not np.isfinite(offsets).all():
|
||||
raise ValueError("Invalid support samples")
|
||||
@@ -165,7 +188,7 @@ class SupportSurface:
|
||||
states, reasons, raised = self.native.classify(offsets)
|
||||
for point in raised + self.pose[:3]:
|
||||
self.raised_obstacles[tuple(np.round(point, 6))] = point.tolist()
|
||||
return states, dict(Counter(reasons))
|
||||
return states, reasons
|
||||
keys = [tuple(point) for point in np.round(offsets, 12)]
|
||||
pending = {}
|
||||
for point, sample_key in zip(offsets, keys):
|
||||
@@ -200,7 +223,7 @@ class SupportSurface:
|
||||
for item, state, reason in zip(batch, states, reasons))
|
||||
values = [self.results[key] for key in keys]
|
||||
return (np.array([v[0] for v in values], dtype=str),
|
||||
dict(Counter(v[1] for v in values)))
|
||||
np.array([v[1] for v in values], dtype=str))
|
||||
|
||||
def hazard_samples(self, unsafe_offsets):
|
||||
"""Bind rejected surface samples to real returns for path-search costs.
|
||||
|
||||
@@ -115,3 +115,47 @@ def test_support_guard_reduces_same_arc_or_stops_before_unknown(monitor):
|
||||
assert (v, w, scale) == (0, 0, 0) and not support["admitted"]
|
||||
blocked = np.vstack((ground(), [[0, 0, 0.3, 0.3]]))
|
||||
assert monitor.regulate_supported_command(0.2, 0, blocked, POSE, 0.37)[:3] == (0, 0, 0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("translation", [(0., 0.), (-125., 48.), (1e9, -1e9)])
|
||||
def test_ground_columns_keep_relief_and_only_remove_measured_wall_layer(monitor, translation):
|
||||
from navigation.support_surface import SupportSurface
|
||||
# Separate columns, including negative coordinates, must not alias. Raised
|
||||
# returns only suppress higher low-cost samples in their own XY column.
|
||||
points = np.array([
|
||||
[-.10, -.10, 0., 0.], [-.10, -.10, .04, 0.],
|
||||
[-.10, -.10, .09, 0.], [-.10, -.10, .8, 1.],
|
||||
[-.10, .10, .09, 0.], [.10, -.10, .09, 0.],
|
||||
[.10, .10, -.4, 0.], [.10, .10, -.32, 0.],
|
||||
[.10, .10, -.1, 1.],
|
||||
])
|
||||
points[:, :2] += translation
|
||||
assert np.array_equal(SupportSurface.ground_layer(points, .1), points[[0, 1, 4, 5, 6], :3])
|
||||
|
||||
|
||||
def test_contact_batch_does_not_mix_neighbouring_relief_or_missing_support(monitor):
|
||||
from collections import Counter
|
||||
from navigation.support_surface import SupportSurface
|
||||
terrain = ground()
|
||||
terrain[:, 2] = .065 * np.sin(terrain[:, 0] * 25) * np.cos(terrain[:, 1] * 20)
|
||||
terrain[terrain[:, 0] > .9, 2] -= .4
|
||||
terrain = terrain[~((terrain[:, 0] < -.9) & (terrain[:, 1] > .4))]
|
||||
samples = np.array([[x, y] for x in np.arange(-1.4, 1.41, .14)
|
||||
for y in np.arange(-1.4, 1.41, .14)])
|
||||
def surface():
|
||||
return SupportSurface(terrain, POSE, .37, .1, math.tan(math.radians(25)))
|
||||
batched, individual = surface(), surface()
|
||||
states, reasons = batched.classify_contacts(samples, .22)
|
||||
expected_states, expected_reasons = [], Counter()
|
||||
for sample in samples:
|
||||
single_states, single_reasons = individual.classify_contacts([sample], .22)
|
||||
expected_states.extend(single_states)
|
||||
expected_reasons.update(single_reasons)
|
||||
assert np.array_equal(states, expected_states)
|
||||
assert reasons == dict(expected_reasons)
|
||||
assert "unknown" in states and "observed-unsafe" in states
|
||||
# Cache hits and repeated query ordering do not share another cell's veto.
|
||||
order = np.r_[np.arange(len(samples)-1, -1, -1), np.arange(12)]
|
||||
repeated, counts = batched.classify_contacts(samples[order], .22)
|
||||
assert np.array_equal(repeated, states[order])
|
||||
assert sum(counts.values()) == len(order)
|
||||
|
||||
Reference in New Issue
Block a user