Pick the majority cluster so a TF correction cannot stall a detection

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.
This commit is contained in:
2026-08-26 09:18:49 +07:00
parent 5d878a78e1
commit 8acbc5b0b0
3 changed files with 544 additions and 10 deletions

View File

@@ -97,6 +97,66 @@ actions_detect_hold:
hold_time: 0.5 # [s]
timeout: 5.0 # [s]
# Lọc cụm chống TF nhảy: mẫu gom theo khoảng cách tới TÂM cụm, chỉ cụm quá bán còn tươi được chốt.
actions_detect_cluster:
handlers:
- {name: detect, type: FrameSamplerActionHandler}
detect:
action_types: [DetectCharger]
output_frame: dock_target
settle_delay: 0.2 # [s]
sample_window: 1.0 # [s]
min_samples: 10
max_spread_xy: 0.03 # [m]
max_spread_yaw: 0.05 # [rad]
timeout: 5.0 # [s]
jump_threshold: 0.15 # [m] > max_spread_xy; TF nhảy trong test là 0.55 m
cluster_recency: 0.3 # [s]
min_cluster_fraction: 0.6
min_cluster_samples: 5
# jump_threshold <= max_spread_xy: mẫu tốt trong tolerance bị xé cụm -> configure() phải từ chối.
actions_detect_cluster_bad_threshold:
handlers:
- {name: detect, type: FrameSamplerActionHandler}
detect:
action_types: [DetectCharger]
output_frame: dock_target
max_spread_xy: 0.03
jump_threshold: 0.02
# min_cluster_fraction phải > 0.5 — đúng 0.5 thì hai cụm 50/50 cùng đạt, mất tính duy nhất.
actions_detect_cluster_bad_fraction:
handlers:
- {name: detect, type: FrameSamplerActionHandler}
detect:
action_types: [DetectCharger]
output_frame: dock_target
jump_threshold: 0.15
min_cluster_fraction: 0.5
# Sàn cụm cao hơn min_samples: dữ liệu hoàn hảo cũng không bao giờ chốt được -> từ chối.
actions_detect_cluster_bad_floor:
handlers:
- {name: detect, type: FrameSamplerActionHandler}
detect:
action_types: [DetectCharger]
output_frame: dock_target
min_samples: 10
jump_threshold: 0.15
min_cluster_samples: 20
# Bỏ lọc có chủ đích: chốt ngay mẫu THỨ HAI, không cửa sổ, không độ tản.
actions_detect_unfiltered:
handlers:
- {name: detect, type: FrameSamplerActionHandler}
detect:
action_types: [DetectCharger]
output_frame: dock_target
filter_enabled: false
settle_delay: 0.2 # [s]
timeout: 5.0 # [s]
# output_frame trùng input_frame -> configure() phải từ chối.
actions_detect_same_frame:
handlers: