/********************************************************************* * * Software License Agreement (BSD License) * * Kiểm handler dò: lấy mẫu TF nhiễu, lọc, ghi ra frame ổn định. * * Trọng tâm là ba thứ mà một bản lọc viết vội sẽ sai: gộp góc quanh ±π, chống outlier, và cổng chất * lượng khi dữ liệu tản. Cả ba đều dẫn tới robot lùi vào sai chỗ mà không có lỗi nào được báo. * * Author: DuongTD *********************************************************************/ #include #include #include #include #include #include #include #include namespace { using action_core::ActionContext; using action_core::ActionHandler; using action_core::ActionRegistry; using action_core::ActionStatus; constexpr const char* kGlobalFrame = "map"; constexpr const char* kRawFrame = "charger_raw"; constexpr const char* kOutFrame = "dock_target"; /// tf3::Time chỉ có ctor (sec, nsec) — không có ctor từ double như robot::Time. tf3::Time toTf3Time(double t) { const uint32_t sec = static_cast(t); return tf3::Time(sec, static_cast((t - sec) * 1e9)); } robot_protocol_msgs::Action makeAction(const std::string& frame = kRawFrame, const std::string& action_type = "DetectCharger") { robot_protocol_msgs::Action action; action.actionType = action_type; action.actionId = "detect-1"; if (!frame.empty()) { robot_protocol_msgs::ActionParameter p; p.key = "goal_frame"; p.value = frame; action.actionParameters.push_back(p); } return action; } /// @brief Rig: một buffer TF thật + handler nạp qua đúng đường Boost.DLL. class Rig { public: explicit Rig(const std::string& ns = "actions_detect", const std::string& action_type = "DetectCharger") { ctx.tf = &tf; ctx.global_frame = kGlobalFrame; registry.setContext(ctx); loaded = registry.loadFromConfig(nh, ns); handler = registry.find(action_type); } /// @brief Bơm một mẫu thô vào buffer. `stamp` phải TĂNG, nếu không mẫu bị bỏ (đúng contract). void pushSample(double x, double y, double yaw, double stamp_s) { tf3::TransformStampedMsg t; t.header.stamp = toTf3Time(stamp_s); t.header.frame_id = kGlobalFrame; t.child_frame_id = kRawFrame; t.transform.translation.x = x; t.transform.translation.y = y; t.transform.rotation.z = std::sin(yaw * 0.5); t.transform.rotation.w = std::cos(yaw * 0.5); tf.setTransform(t, "test", /*is_static=*/false); } void pushTransform(const std::string& parent, const std::string& child, double x, double y, double yaw, double stamp_s) { tf3::TransformStampedMsg t; t.header.stamp = toTf3Time(stamp_s); t.header.frame_id = parent; t.child_frame_id = child; t.transform.translation.x = x; t.transform.translation.y = y; t.transform.rotation.z = std::sin(yaw * 0.5); t.transform.rotation.w = std::cos(yaw * 0.5); tf.setTransform(t, "test", /*is_static=*/false); } bool readOutput(double& x, double& y, double& yaw) { try { const auto t = tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time()); x = t.transform.translation.x; y = t.transform.translation.y; const auto& q = t.transform.rotation; yaw = std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z)); return true; } catch (const std::exception&) { return false; } } robot::NodeHandle nh; tf3::BufferCore tf; ActionContext ctx; ActionRegistry registry; ActionHandler* handler = nullptr; 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) { 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) { 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; if (last != ActionStatus::kRunning) { 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; } // ================================================================================================ // Đường thành công // ================================================================================================ TEST(FrameSampler, LoadsAndPublishesAStableFrame) { Rig rig; 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 x = 0.0, y = 0.0, yaw = 0.0; ASSERT_TRUE(rig.readOutput(x, y, yaw)); EXPECT_NEAR(x, 2.0, 0.005); EXPECT_NEAR(y, 1.0, 0.005); EXPECT_NEAR(yaw, 0.5, 0.01); } 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)); 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); double x = 0.0, y = 0.0, yaw = 0.0; ASSERT_TRUE(rig.readOutput(x, y, yaw)); EXPECT_NEAR(x, 2.0, 0.001) << "trung vị phải loại được outlier"; } 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)); // 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); double x = 0.0, y = 0.0, yaw = 0.0; ASSERT_TRUE(rig.readOutput(x, y, yaw)); EXPECT_NEAR(std::fabs(yaw), M_PI, 0.02) << "gộp góc phải dùng vector đơn vị, không phải trung bình"; } TEST(FrameSampler, SimulatedGoalIsFrozenInTheGlobalFrame) { Rig rig("actions_detect_simulated", "DetectPallet"); ASSERT_TRUE(rig.loaded); ASSERT_NE(rig.handler, nullptr); rig.pushTransform(kGlobalFrame, "base_link", 10.0, 5.0, M_PI_2, 100.0); EXPECT_TRUE(rig.handler->start(makeAction("", "DetectPallet"), robot::Time(100.0))); EXPECT_EQ(rig.handler->update(robot::Time(100.0)).status, ActionStatus::kSucceeded); double x = 0.0, y = 0.0, yaw = 0.0; ASSERT_TRUE(rig.readOutput(x, y, yaw)); EXPECT_NEAR(x, 9.8, 1e-6); EXPECT_NEAR(y, 3.5, 1e-6); EXPECT_NEAR(yaw, M_PI_2, 1e-6); // Output là static trong map, không được chạy theo parent ở cycle sau. rig.pushTransform(kGlobalFrame, "base_link", 20.0, 5.0, M_PI_2, 101.0); ASSERT_TRUE(rig.readOutput(x, y, yaw)); EXPECT_NEAR(x, 9.8, 1e-6); EXPECT_NEAR(y, 3.5, 1e-6); } // ================================================================================================ // Đường lỗi — mỗi ca phải fail được // ================================================================================================ TEST(FrameSampler, FailsWhenDetectionIsTooScattered) { 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); 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, FailsWithTooFewSamples) { Rig rig; ASSERT_NE(rig.handler, nullptr); const robot::Time t0 = robot::Time(100.0); ASSERT_TRUE(rig.handler->start(makeAction(), t0)); 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); } TEST(FrameSampler, DuplicateTimestampsDoNotCountAsSamples) { Rig rig; ASSERT_NE(rig.handler, nullptr); 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. 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); } TEST(FrameSampler, FailsWhenTheRawFrameNeverAppears) { Rig rig; ASSERT_NE(rig.handler, nullptr); const robot::Time t0 = robot::Time(100.0); ASSERT_TRUE(rig.handler->start(makeAction(), t0)); EXPECT_EQ(rig.handler->update(robot::Time(100.5)).status, ActionStatus::kRunning); // timeout = 5.0 trong config test. EXPECT_EQ(rig.handler->update(robot::Time(105.1)).status, ActionStatus::kFailed); } TEST(FrameSampler, RefusesToStartWithoutAFrameParameter) { Rig rig; ASSERT_NE(rig.handler, nullptr); // Không có `goal_frame` và config cũng không khai `input_frame` -> không biết dò cái gì. EXPECT_FALSE(rig.handler->start(makeAction(""), robot::Time(100.0))); } TEST(FrameSampler, RefusesToLoadWithoutATfBuffer) { // Handler cần TF; thiếu thì phải từ chối lúc NẠP, không phải lúc order đầu tiên tới. robot::NodeHandle nh; ActionRegistry registry; // context mặc định: tf == nullptr EXPECT_FALSE(registry.loadFromConfig(nh, "actions_detect")); EXPECT_EQ(registry.size(), 0u); } TEST(FrameSampler, RefusesOutputFrameEqualToInputFrame) { Rig rig("actions_detect_same_frame"); EXPECT_FALSE(rig.loaded) << "ghi đè chính frame thô là tranh chấp với bên publish nó"; EXPECT_EQ(rig.registry.size(), 0u); } TEST(FrameSampler, RefusesTimeoutShorterThanTheSamplingWindow) { Rig rig("actions_detect_bad_timeout"); EXPECT_FALSE(rig.loaded) << "action sẽ LUÔN hỏng — gần như chắc chắn gõ nhầm"; EXPECT_EQ(rig.registry.size(), 0u); } } // namespace int main(int argc, char** argv) { #ifdef ACTION_CORE_TEST_CONFIG_DIR setenv("PNKX_NAV_CORE_CONFIG_DIR", ACTION_CORE_TEST_CONFIG_DIR, 0); #endif #ifdef ACTION_CORE_TEST_LIBRARY_DIR setenv("PNKX_NAV_CORE_LIBRARY_PATH", ACTION_CORE_TEST_LIBRARY_DIR, 0); #endif testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }