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:
@@ -316,6 +316,179 @@ TEST(FrameSampler, HoldTimeDelaysTheCommit)
|
||||
EXPECT_LE(stopped_at, kFirstVerdict + 0.5 + 0.05);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Lọc cụm — TF nhảy làm cửa sổ chứa nhiều hệ toạ độ
|
||||
// ================================================================================================
|
||||
|
||||
TEST(FrameSampler, ClusterFilterIgnoresAMinorityOfJumpedSamples)
|
||||
{
|
||||
Rig rig("actions_detect_cluster");
|
||||
ASSERT_TRUE(rig.loaded);
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// TF flicker: ~10% mẫu văng sang hệ toạ độ khác cách 0.55 m — đúng hình dạng dữ liệu hiện trường
|
||||
// khi backend localization sửa map->odom qua lại. robustSpread cắt phân vị 90 không chịu nổi 10%
|
||||
// lệch CÓ CẤU TRÚC thế này (spread ~1.1 m, fail suốt timeout); lọc cụm phải coi đây là ca thuận
|
||||
// lợi: cụm chính 90% chụm hoàn hảo.
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runTicks(rig, 101.5,
|
||||
[](Rig& r, int i, double t) {
|
||||
const double s = (i % 2 == 0) ? 1.0 : -1.0;
|
||||
const double jump = (i % 10 == 0) ? 0.55 : 0.0;
|
||||
r.pushSample(2.0 + jump + s * 0.002, 1.0 - s * 0.002, 0.0, t);
|
||||
},
|
||||
&stopped_at),
|
||||
ActionStatus::kSucceeded);
|
||||
|
||||
// Cụm chính quá bán và chụm ngay từ đầu — phải chốt ở cửa sổ ĐẦU TIÊN, không được chờ thêm.
|
||||
EXPECT_NEAR(stopped_at, kFirstVerdict, 0.03);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
EXPECT_NEAR(x, 2.0, 0.005) << "tâm phải là cụm chính, mẫu nhảy không được kéo";
|
||||
EXPECT_NEAR(y, 1.0, 0.005);
|
||||
}
|
||||
|
||||
TEST(FrameSampler, ClusterFilterRefusesWithoutAMajority)
|
||||
{
|
||||
Rig rig("actions_detect_cluster");
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Hai hệ toạ độ giằng co 50/50, mỗi cụm TỰ NÓ chụm hoàn hảo. Không phe nào quá bán -> không được
|
||||
// chọn bừa một trong hai tâm cách nhau 0.55 m; phải chờ hết timeout rồi fail.
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runTicks(rig, 105.2,
|
||||
[](Rig& r, int i, double t) {
|
||||
const double jump = (i % 2 == 0) ? 0.55 : 0.0;
|
||||
r.pushSample(2.0 + jump, 1.0, 0.0, t);
|
||||
},
|
||||
&stopped_at),
|
||||
ActionStatus::kFailed);
|
||||
EXPECT_GE(stopped_at, kTimeoutAt);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
EXPECT_FALSE(rig.readOutput(x, y, yaw)) << "không quá bán thì KHÔNG được ghi frame nào";
|
||||
}
|
||||
|
||||
TEST(FrameSampler, ClusterFilterFollowsTheFreshClusterAfterATfCorrection)
|
||||
{
|
||||
Rig rig("actions_detect_cluster");
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Trước 101.0: dò tản (±0.05 -> spread ~0.14, một cụm duy nhất nhưng không bao giờ đạt ngưỡng).
|
||||
// Từ 101.0: TF đã sửa HẲN, mẫu chụm quanh vị trí mới cách 0.55 m. Handler phải chốt theo cụm
|
||||
// mới; cụm cũ dù còn đông trong cửa sổ nhưng đã NGUỘI (quá cluster_recency không nhận mẫu).
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runTicks(rig, 105.2,
|
||||
[](Rig& r, int i, double t) {
|
||||
if (t < 101.0)
|
||||
{
|
||||
const double s = (i % 2 == 0) ? 1.0 : -1.0;
|
||||
r.pushSample(2.0 + s * 0.05, 1.0, 0.0, t);
|
||||
}
|
||||
else
|
||||
{
|
||||
r.pushSample(2.55, 1.0, 0.0, t);
|
||||
}
|
||||
},
|
||||
&stopped_at),
|
||||
ActionStatus::kSucceeded);
|
||||
|
||||
// Cụm mới phải chiếm 60% cửa sổ 1.0 s -> sớm nhất ~101.6. Muộn hơn nhiều là fraction/recency sai.
|
||||
EXPECT_GT(stopped_at, 101.55);
|
||||
EXPECT_LT(stopped_at, 101.9);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
EXPECT_NEAR(x, 2.55, 0.005) << "phải chốt theo hệ toạ độ SAU correction, không phải cụm cũ";
|
||||
}
|
||||
|
||||
TEST(FrameSampler, InterpolatedSamplesDoNotBridgeClusters)
|
||||
{
|
||||
Rig rig("actions_detect_cluster");
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// TF nội suy lúc nhảy sinh chuỗi mẫu LƠ LỬNG nối hai cụm, mỗi bước 0.14 m < jump_threshold 0.15.
|
||||
// Đo kiểu single-linkage thì chuỗi này xích cả hai cụm thành một cụm khổng lồ "quá bán" với tâm
|
||||
// sai; đo tới TÂM cụm thì mắt xích thứ hai đã cách tâm cụm chính 0.28 m và phải đứng riêng.
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runTicks(rig, 101.5,
|
||||
[](Rig& r, int i, double t) {
|
||||
double x = 2.0;
|
||||
switch (i % 25)
|
||||
{
|
||||
case 0: x = 2.14; break;
|
||||
case 1: x = 2.28; break;
|
||||
case 2: x = 2.42; break;
|
||||
case 3: x = 2.55; break;
|
||||
default: break;
|
||||
}
|
||||
r.pushSample(x, 1.0, 0.0, t);
|
||||
},
|
||||
&stopped_at),
|
||||
ActionStatus::kSucceeded);
|
||||
EXPECT_NEAR(stopped_at, kFirstVerdict, 0.03);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
EXPECT_NEAR(x, 2.0, 0.005) << "mẫu nội suy không được nối cụm lẫn kéo tâm";
|
||||
}
|
||||
|
||||
TEST(FrameSampler, RefusesInvalidClusterConfigs)
|
||||
{
|
||||
EXPECT_FALSE(Rig("actions_detect_cluster_bad_threshold").loaded)
|
||||
<< "jump_threshold <= max_spread_xy xé mẫu tốt thành nhiều cụm — phải chặn lúc boot";
|
||||
EXPECT_FALSE(Rig("actions_detect_cluster_bad_fraction").loaded)
|
||||
<< "fraction = 0.5 mất tính duy nhất của cụm quá bán";
|
||||
EXPECT_FALSE(Rig("actions_detect_cluster_bad_floor").loaded)
|
||||
<< "sàn cụm cao hơn min_samples thì dữ liệu hoàn hảo cũng không bao giờ chốt";
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// filter_enabled: false — chốt ngay mẫu thứ hai
|
||||
// ================================================================================================
|
||||
|
||||
TEST(FrameSampler, UnfilteredModeCommitsTheSecondSample)
|
||||
{
|
||||
Rig rig("actions_detect_unfiltered");
|
||||
ASSERT_TRUE(rig.loaded);
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Mẫu THỨ NHẤT có thể là detection cũ còn sót trong buffer — cố ý đặt nó ở chỗ sai hẳn. Handler
|
||||
// không được chốt theo nó, và không phải chờ cửa sổ nào: mẫu thứ hai tới là chốt ngay tick đó.
|
||||
rig.pushSample(9.9, 9.9, 3.0, 100.3);
|
||||
EXPECT_EQ(rig.handler->update(robot::Time(100.3)).status, ActionStatus::kRunning);
|
||||
|
||||
rig.pushSample(2.0, 1.0, 0.5, 100.4);
|
||||
EXPECT_EQ(rig.handler->update(robot::Time(100.4)).status, ActionStatus::kSucceeded);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
EXPECT_NEAR(x, 2.0, 1e-6) << "phải chốt đúng mẫu THỨ HAI, không phải mẫu đầu";
|
||||
EXPECT_NEAR(y, 1.0, 1e-6);
|
||||
EXPECT_NEAR(yaw, 0.5, 1e-6);
|
||||
}
|
||||
|
||||
TEST(FrameSampler, UnfilteredModeStillFailsWithASingleFrozenSample)
|
||||
{
|
||||
Rig rig("actions_detect_unfiltered");
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Đúng MỘT mẫu rồi nguồn đứng im (stamp không đổi): không có mẫu thứ hai xác nhận nguồn còn
|
||||
// sống -> phải chờ hết timeout rồi fail, không được chốt mẫu đơn độc.
|
||||
rig.pushSample(2.0, 1.0, 0.0, 100.3);
|
||||
EXPECT_EQ(runTicks(rig, 105.2, [](Rig&, int, double) {}), ActionStatus::kFailed);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
EXPECT_FALSE(rig.readOutput(x, y, yaw));
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Đường lỗi — mỗi ca phải fail được
|
||||
// ================================================================================================
|
||||
|
||||
Reference in New Issue
Block a user