Wait for the detection to converge instead of judging it once
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.
This commit is contained in:
@@ -123,32 +123,59 @@ public:
|
||||
bool loaded = false;
|
||||
};
|
||||
|
||||
/// @brief Chạy handler qua trọn một lượt: bơm @p n mẫu quanh (x,y,yaw) với nhiễu @p noise.
|
||||
ActionStatus runSampling(Rig& rig, int n, double x, double y, double yaw, double noise_xy,
|
||||
double noise_yaw = 0.0)
|
||||
// Config test: settle_delay = 0.2, sample_window = 1.0, min_samples = 10, timeout = 5.0.
|
||||
// -> lượt dò bắt đầu tại t = 100.0; cửa sổ đầu tiên được chấm tại t = 101.2; timeout tại t = 105.0.
|
||||
constexpr double kStart = 100.0;
|
||||
constexpr double kTickPeriod = 0.01; // [s] ~100 Hz, dày hơn control loop thật cho chắc
|
||||
constexpr double kFirstTick = 100.3; // [s] ngay sau settle_delay
|
||||
constexpr double kFirstVerdict = 101.2; // [s] settle_delay + sample_window
|
||||
constexpr double kTimeoutAt = 105.0; // [s]
|
||||
|
||||
/**
|
||||
* @brief Chạy handler từ @p kFirstTick tới @p t_end, mỗi tick gọi @p make để bơm một mẫu.
|
||||
*
|
||||
* Dừng ngay khi handler ra khỏi kRunning và ghi thời điểm đó vào @p stopped_at — cửa sổ trượt khiến
|
||||
* "chốt lúc nào" trở thành một phần của hành vi cần kiểm, không chỉ "chốt hay không".
|
||||
*
|
||||
* @param make void(Rig&, int tick_index, double now_s)
|
||||
*/
|
||||
template <typename MakeSample>
|
||||
ActionStatus runTicks(Rig& rig, double t_end, MakeSample make, double* stopped_at = nullptr)
|
||||
{
|
||||
const robot::Time t0 = robot::Time(100.0);
|
||||
EXPECT_TRUE(rig.handler->start(makeAction(), t0));
|
||||
|
||||
// settle_delay = 0.2, sample_window = 1.0 trong config test.
|
||||
double stamp = 100.0;
|
||||
ActionStatus last = ActionStatus::kRunning;
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
int i = 0;
|
||||
for (double t = kFirstTick; t <= t_end + 1e-9; t += kTickPeriod, ++i)
|
||||
{
|
||||
stamp += 0.01;
|
||||
// Nhiễu răng cưa: đổi dấu mỗi mẫu — biên độ đúng bằng `noise`, không phụ thuộc bộ sinh ngẫu nhiên.
|
||||
const double s = (i % 2 == 0) ? 1.0 : -1.0;
|
||||
rig.pushSample(x + s * noise_xy, y - s * noise_xy, yaw + s * noise_yaw, stamp);
|
||||
last = rig.handler->update(robot::Time(100.3 + i * 0.01)).status;
|
||||
make(rig, i, t);
|
||||
last = rig.handler->update(robot::Time(t)).status;
|
||||
if (last != ActionStatus::kRunning)
|
||||
{
|
||||
if (stopped_at != nullptr)
|
||||
{
|
||||
*stopped_at = t;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Quá cửa sổ lấy mẫu -> handler chốt kết quả.
|
||||
return rig.handler->update(robot::Time(101.5)).status;
|
||||
/// @brief Lượt dò tiêu chuẩn: mẫu quanh (x, y, yaw) với nhiễu răng cưa biên độ @p noise_xy.
|
||||
ActionStatus runSampling(Rig& rig, double x, double y, double yaw, double noise_xy,
|
||||
double noise_yaw = 0.0, double t_end = 101.5,
|
||||
double* stopped_at = nullptr)
|
||||
{
|
||||
EXPECT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
return runTicks(
|
||||
rig, t_end,
|
||||
[&](Rig& r, int i, double t) {
|
||||
// Nhiễu răng cưa: đổi dấu mỗi mẫu — biên độ đúng bằng `noise`, không phụ thuộc bộ sinh ngẫu
|
||||
// nhiên. Stamp lấy luôn theo `t` nên luôn tăng, không mẫu nào bị coi là trùng.
|
||||
const double s = (i % 2 == 0) ? 1.0 : -1.0;
|
||||
r.pushSample(x + s * noise_xy, y - s * noise_xy, yaw + s * noise_yaw, t);
|
||||
},
|
||||
stopped_at);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
@@ -161,7 +188,12 @@ TEST(FrameSampler, LoadsAndPublishesAStableFrame)
|
||||
ASSERT_TRUE(rig.loaded);
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
|
||||
EXPECT_EQ(runSampling(rig, 30, 2.0, 1.0, 0.5, 0.002), ActionStatus::kSucceeded);
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runSampling(rig, 2.0, 1.0, 0.5, 0.002, 0.0, 101.5, &stopped_at),
|
||||
ActionStatus::kSucceeded);
|
||||
|
||||
// Dò tốt ngay từ đầu thì chốt ở cửa sổ ĐẦU TIÊN — cửa sổ trượt không được làm chậm ca thuận lợi.
|
||||
EXPECT_NEAR(stopped_at, kFirstVerdict, 0.03);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
@@ -175,19 +207,18 @@ TEST(FrameSampler, MedianRejectsAnOutlier)
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
|
||||
const robot::Time t0 = robot::Time(100.0);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), t0));
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
double stamp = 100.0;
|
||||
for (int i = 0; i < 30; ++i)
|
||||
{
|
||||
stamp += 0.01;
|
||||
// Một mẫu nhận nhầm chân kệ khác, lệch 1 m. Trung bình sẽ bị kéo ~3 cm; trung vị thì không.
|
||||
const double x = (i == 7) ? 3.0 : 2.0;
|
||||
rig.pushSample(x, 1.0, 0.0, stamp);
|
||||
rig.handler->update(robot::Time(100.3 + i * 0.01));
|
||||
}
|
||||
ASSERT_EQ(rig.handler->update(robot::Time(101.5)).status, ActionStatus::kSucceeded);
|
||||
// Outlier đặt ở GIỮA cửa sổ được chấm (t ~ 100.9), không phải ở đầu lượt: cửa sổ trượt chỉ dài
|
||||
// 1 s nên mẫu của 0.3 s đầu đã bị cắt ra ngoài trước khi có phán quyết nào.
|
||||
ASSERT_EQ(runTicks(rig, 101.5,
|
||||
[](Rig& r, int, double t) {
|
||||
// Một mẫu nhận nhầm chân kệ khác, lệch 1 m. Trung bình bị kéo ~1 cm; trung vị
|
||||
// thì không.
|
||||
const double x = (std::fabs(t - 100.9) < 0.005) ? 3.0 : 2.0;
|
||||
r.pushSample(x, 1.0, 0.0, t);
|
||||
}),
|
||||
ActionStatus::kSucceeded);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
@@ -199,19 +230,15 @@ TEST(FrameSampler, AveragesYawAcrossTheWrapAround)
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
|
||||
const robot::Time t0 = robot::Time(100.0);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), t0));
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Mẫu nằm hai bên đường cắt ±π. Trung bình SỐ HỌC cho ra ~0 — lệch 180°, robot vào trạm ngược đầu.
|
||||
double stamp = 100.0;
|
||||
for (int i = 0; i < 30; ++i)
|
||||
{
|
||||
stamp += 0.01;
|
||||
const double yaw = (i % 2 == 0) ? (M_PI - 0.01) : (-M_PI + 0.01);
|
||||
rig.pushSample(2.0, 1.0, yaw, stamp);
|
||||
rig.handler->update(robot::Time(100.3 + i * 0.01));
|
||||
}
|
||||
ASSERT_EQ(rig.handler->update(robot::Time(101.5)).status, ActionStatus::kSucceeded);
|
||||
ASSERT_EQ(runTicks(rig, 101.5,
|
||||
[](Rig& r, int i, double t) {
|
||||
const double yaw = (i % 2 == 0) ? (M_PI - 0.01) : (-M_PI + 0.01);
|
||||
r.pushSample(2.0, 1.0, yaw, t);
|
||||
}),
|
||||
ActionStatus::kSucceeded);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
@@ -241,54 +268,117 @@ TEST(FrameSampler, SimulatedGoalIsFrozenInTheGlobalFrame)
|
||||
EXPECT_NEAR(y, 3.5, 1e-6);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Cửa sổ trượt — chờ hội tụ thay vì phán một lần
|
||||
// ================================================================================================
|
||||
|
||||
TEST(FrameSampler, RetriesUntilTheDetectionConverges)
|
||||
{
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Perception bám nhầm trong 2 s đầu (±0.10 m) rồi ổn định. Bản phán-một-lần fail ngay tại
|
||||
// t = 101.2; bản cửa sổ trượt phải chờ tới khi cửa sổ sạch mẫu xấu.
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runTicks(
|
||||
rig, 105.2,
|
||||
[](Rig& r, int i, double t) {
|
||||
const double s = (i % 2 == 0) ? 1.0 : -1.0;
|
||||
const double noise = (t < 102.0) ? 0.10 : 0.001;
|
||||
r.pushSample(2.0 + s * noise, 1.0, 0.0, t);
|
||||
},
|
||||
&stopped_at),
|
||||
ActionStatus::kSucceeded);
|
||||
|
||||
// Không được chốt trước khi cửa sổ hết mẫu xấu: chốt sớm nghĩa là chấp nhận chính dữ liệu tản.
|
||||
EXPECT_GT(stopped_at, 102.0);
|
||||
EXPECT_LT(stopped_at, kTimeoutAt);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
ASSERT_TRUE(rig.readOutput(x, y, yaw));
|
||||
EXPECT_NEAR(x, 2.0, 0.005) << "phải chốt theo dữ liệu ĐÃ ổn định, không lẫn giai đoạn nhiễu";
|
||||
EXPECT_NEAR(y, 1.0, 0.005);
|
||||
}
|
||||
|
||||
TEST(FrameSampler, HoldTimeDelaysTheCommit)
|
||||
{
|
||||
// hold_time = 0.5 trong namespace này. Cùng dữ liệu tốt như LoadsAndPublishesAStableFrame, nhưng
|
||||
// phải giữ trạng thái đạt thêm 0.5 s mới được chốt — chặn cửa sổ tình cờ chụm.
|
||||
Rig rig("actions_detect_hold");
|
||||
ASSERT_TRUE(rig.loaded);
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runSampling(rig, 2.0, 1.0, 0.0, 0.002, 0.0, 103.0, &stopped_at),
|
||||
ActionStatus::kSucceeded);
|
||||
EXPECT_GE(stopped_at, kFirstVerdict + 0.5 - 0.03) << "chốt trước hold_time là bỏ qua cổng giữ";
|
||||
EXPECT_LE(stopped_at, kFirstVerdict + 0.5 + 0.05);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Đường lỗi — mỗi ca phải fail được
|
||||
// ================================================================================================
|
||||
|
||||
TEST(FrameSampler, FailsWhenDetectionIsTooScattered)
|
||||
TEST(FrameSampler, FailsWhenDetectionStaysScatteredUntilTheTimeout)
|
||||
{
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
|
||||
// max_spread_xy = 0.03 trong config test; nhiễu ±0.05 cho độ tản 0.1 m.
|
||||
EXPECT_EQ(runSampling(rig, 30, 2.0, 1.0, 0.0, 0.05), ActionStatus::kFailed);
|
||||
// max_spread_xy = 0.03 trong config test; nhiễu ±0.05 cho độ tản 0.14 m. Tản suốt cả `timeout`
|
||||
// nên KHÔNG cửa sổ nào đạt — đây mới là ca đáng fail.
|
||||
double stopped_at = 0.0;
|
||||
EXPECT_EQ(runSampling(rig, 2.0, 1.0, 0.0, 0.05, 0.0, 105.2, &stopped_at),
|
||||
ActionStatus::kFailed);
|
||||
EXPECT_GE(stopped_at, kTimeoutAt) << "phải dùng hết timeout để chờ hội tụ, không bỏ cuộc sớm";
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
EXPECT_FALSE(rig.readOutput(x, y, yaw)) << "dữ liệu tản thì KHÔNG được ghi frame nào";
|
||||
}
|
||||
|
||||
TEST(FrameSampler, FailsWhenTheFrameNeverStopsMoving)
|
||||
{
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
// Mỗi mẫu chụm với mẫu kề nó, nhưng cả chuỗi trôi 0.5 m/s: cửa sổ 1 s trải 0.5 m. Đây là dạng
|
||||
// hỏng mà "so mẫu này với mẫu trước" bỏ lọt còn độ tản cửa sổ thì bắt được.
|
||||
EXPECT_EQ(runTicks(rig, 105.2,
|
||||
[](Rig& r, int i, double t) {
|
||||
r.pushSample(2.0 + i * 0.005, 1.0, 0.0, t);
|
||||
}),
|
||||
ActionStatus::kFailed);
|
||||
|
||||
double x = 0.0, y = 0.0, yaw = 0.0;
|
||||
EXPECT_FALSE(rig.readOutput(x, y, yaw)) << "đích còn trôi thì KHÔNG được chốt";
|
||||
}
|
||||
|
||||
TEST(FrameSampler, FailsWithTooFewSamples)
|
||||
{
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
|
||||
const robot::Time t0 = robot::Time(100.0);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), t0));
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
rig.pushSample(2.0, 1.0, 0.0, 100.5);
|
||||
rig.handler->update(robot::Time(100.5));
|
||||
|
||||
// min_samples = 10 trong config test.
|
||||
EXPECT_EQ(rig.handler->update(robot::Time(101.5)).status, ActionStatus::kFailed);
|
||||
// min_samples = 10 trong config test. Một mẫu duy nhất thì không cửa sổ nào chấm được, và tới
|
||||
// timeout phải fail — không được im lặng chờ mãi.
|
||||
EXPECT_EQ(rig.handler->update(robot::Time(101.5)).status, ActionStatus::kRunning);
|
||||
EXPECT_EQ(rig.handler->update(robot::Time(105.1)).status, ActionStatus::kFailed);
|
||||
}
|
||||
|
||||
TEST(FrameSampler, DuplicateTimestampsDoNotCountAsSamples)
|
||||
{
|
||||
Rig rig;
|
||||
ASSERT_NE(rig.handler, nullptr);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(kStart)));
|
||||
|
||||
const robot::Time t0 = robot::Time(100.0);
|
||||
ASSERT_TRUE(rig.handler->start(makeAction(), t0));
|
||||
|
||||
// TF cập nhật chậm hơn control loop: cùng một pose bị đọc 30 lần. Không lọc theo timestamp thì
|
||||
// min_samples mất hết ý nghĩa và một mẫu duy nhất trông như ba mươi mẫu.
|
||||
// TF cập nhật chậm hơn control loop: cùng một pose bị đọc hàng trăm lần. Không lọc theo timestamp
|
||||
// thì min_samples mất hết ý nghĩa và một mẫu duy nhất trông như một cửa sổ đầy.
|
||||
rig.pushSample(2.0, 1.0, 0.0, 100.5);
|
||||
for (int i = 0; i < 30; ++i)
|
||||
{
|
||||
rig.handler->update(robot::Time(100.3 + i * 0.01));
|
||||
}
|
||||
|
||||
EXPECT_EQ(rig.handler->update(robot::Time(101.5)).status, ActionStatus::kFailed);
|
||||
EXPECT_EQ(runTicks(rig, 105.2, [](Rig&, int, double) {}), ActionStatus::kFailed);
|
||||
}
|
||||
|
||||
TEST(FrameSampler, FailsWhenTheRawFrameNeverAppears)
|
||||
|
||||
Reference in New Issue
Block a user