The handler was 1114 lines, of which roughly 200 were "read a pose from TF".
The rest was the filter: a sliding window, cluster rejection for localization
jumps, robust spread, a hold timer, and the diagnostics that make a failed
detection explainable. A detector that finds a dock plate in a laser scan
produces a pose per scan and needs all of that too, and writing it twice is
worse than the duplication - the two copies drift, and a fix lands in one.
PoseSampleFilter now owns the window, the clustering, the spread measurement,
the hold clock, the best-window record and the throttled progress log. The
handler keeps what is actually specific to being a TF source: the lookup and
its stamp-based dedupe, goal_frame, settle_delay and timeout, the simulated
goal path, writing the result back as a static transform, and the three
distinct timeout messages.
The line between them is source versus filtering. last_stamp_ stays with the
handler for that reason - deduplicating on a TF stamp is a property of that
source, and a detector reading from the shared sensor store will dedupe on an
arrival counter instead.
filter_enabled moves into the filter's config rather than staying with the
handler, because it decides whether the cluster keys are validated at all.
Split across two owners, that is one rule two places have to remember, and the
first time they disagree a config that used to load stops loading.
The filter validates its own keys and logs them with the instance name, so the
messages an operator has learned to read come out identical.
Behaviour is unchanged and the test files are untouched: 22 of 22 frame sampler
tests pass as they are. That is the point of doing it this way - the tests
cover exactly the code that moved, so passing them without editing them is what
distinguishes a refactor from a rewrite.
Both detectors now read the same YAML keys, so sample_window, max_spread_xy and
jump_threshold are learned once rather than once per detector.
Localization correcting map->odom in the middle of a detection moves samples
that were already collected into a different frame. The window then holds two
clusters half a metre apart, the spread over the whole window stays above the
limit for the rest of the timeout, and the action fails even though each
cluster on its own is perfectly tight and the detector never made a mistake.
jump_threshold > 0 turns on clustering. Samples are grouped by distance to the
cluster CENTRE rather than to the nearest member: a sample caught while TF was
interpolating sits between the two clusters and single-linkage would use it as
a bridge to merge them back into one. A cluster may be committed only if it
holds at least min_cluster_fraction of the window, at least
min_cluster_samples in absolute terms, and has a sample newer than
cluster_recency seconds.
min_cluster_fraction is required to be > 0.5 so at most one cluster can ever
qualify - no tie-break rule is needed, and two frames fighting 50/50 leave no
winner, which is the right answer: with two centres 0.55 m apart, guessing
docks half a metre off. The recency test is what separates the stale majority
from the live one; a cluster that is large but cold is the pose from before
the correction, in a frame localization has already discarded. Spread is then
measured on the winning cluster only, so max_spread_xy/yaw keep their role as
the safety gate.
configure() rejects the combinations that can never succeed: jump_threshold
below max_spread_xy would split good in-tolerance samples into fragments and
fail on perfect data, min_cluster_fraction at exactly 0.5 loses uniqueness,
and min_cluster_samples above min_samples can never be reached.
filter_enabled: false is the deliberate escape hatch - commit the second
sample and skip the window, spread and clustering entirely. The second and
not the first, because the first lookup after the action starts can return a
detection left in the buffer from before the robot stopped; a second sample
with a different stamp proves the source is alive and the data is new.
settle_delay and timeout still apply, and the timeout check drops to
settle_delay alone since that path no longer needs a window.
Defaults keep the previous behaviour: jump_threshold 0 means no clustering,
filter_enabled true means the full filter.
The gate used to fire at settle_delay + sample_window and abort, throwing away
whatever remained of the timeout. Four field runs failed there with spreads
between 1.3 m and 3.2 m while the detector reported the pallet to within a
millimetre, and a retry seconds later at the same spot succeeded - the
detection needed longer to settle than the window allowed, and the handler was
not willing to wait.
samples_ is now a sliding window of sample_window seconds: each tick adds the
new sample and drops the expired ones. Once the window is full the spread is
evaluated every tick, and a spread over the limit only means "not yet" - the
handler keeps sampling until it converges or the timeout runs out. The timeout
becomes the real deadline instead of the window.
hold_time requires the spread to stay under the limit for that long before the
result is published, so a momentarily quiet window cannot be mistaken for a
settled one. 0 keeps the previous behaviour of publishing on the first good
window.
Convergence is logged once a second, not per tick, so it is possible to see
whether the sample set is tightening without drowning a 30 Hz loop. median and
robustSpread now take their buffer by reference and sort it in place, since
they run on every tick rather than once per detection.
The spread number alone cannot separate two failures that need opposite
responses: a handful of samples landing far away while the rest agree, which
is a mis-detection at the source, versus every sample drifting, which means
the detection genuinely is not settled and the robot should re-detect or move
closer.
Four field runs hit this gate with spreads between 1.3 m and 3.2 m while the
detector itself reported the pallet to within a millimetre and the robot stood
still. Each run eliminated one theory and offered nothing to replace it,
because a single scalar cannot say where the samples went.
On failure the handler now logs the sample count, the filtered centre, the
deviation at p50, p90 and max, and the bounding box in the fixed frame.
Comparing p50 against p90 separates the two cases, and the box says where the
stray samples ended up - sitting on the robot's own position means something
very different from being scattered around the target.
Runs once per failed detection, never on the sampling path.