/********************************************************************* * * Software License Agreement (BSD License) * * Kiểm dò xe hàng 4 chân trên scan THẬT của lidar KIT (`/f_scan`, 1600 tia quanh 360°, trả range 0 * khi không có phản hồi). Lần lượt một cột 6×6 cm che từng chân; chân xa-phải luôn dính một vật * phía sau nó. Đây là ca đã fail trên robot ngày 28/10: chân dính bị loại, che thêm một chân là còn * hai chân sạch. * * `reference` trong file dữ liệu là kết quả dò của chính robot trong cùng bố trí, không phải đáp án * đo bằng thước — nên dung sai test rộng hơn test tổng hợp. * * Author: DuongTD *********************************************************************/ #include #include #include #include #include #include #include namespace { using dock_pattern_detector::LegDetection; using dock_pattern_detector::LegPatternMatcher; using dock_pattern_detector::LegProfile; using dock_pattern_detector::LegShape; using dock_pattern_detector::Pose2D; using dock_pattern_detector::ScanLimits; constexpr double kDeg = M_PI / 180.0; struct RecordedScan { std::string name; robot_line_extraction::ScanGeometry geometry; std::vector ranges; double ref_x = 0.0; // [m] double ref_y = 0.0; // [m] double ref_yaw = 0.0; // [rad] }; std::vector loadScans() { std::vector scans; std::ifstream in(std::string(DOCK_PATTERN_DETECTOR_TEST_DATA_DIR) + "/kit_f_scan_trolley_occlusion.txt"); std::string line; while (std::getline(in, line)) { if (line.empty() || line[0] == '#') { continue; } std::istringstream header(line); std::string tag; RecordedScan s; header >> tag >> s.name >> s.geometry.angle_min >> s.geometry.angle_increment >> s.geometry.size >> s.ref_x >> s.ref_y >> s.ref_yaw; if (tag != "scan" || !std::getline(in, line)) { break; } std::istringstream values(line); double r = 0.0; while (values >> r) { s.ranges.push_back(r); } scans.push_back(s); } return scans; } const RecordedScan& scanNamed(const std::vector& scans, const std::string& name) { for (const RecordedScan& s : scans) { if (s.name == name) { return s; } } ADD_FAILURE() << "no scan '" << name << "' in the data file"; static const RecordedScan kEmpty; return kEmpty; } /// @brief Profile đang chạy trên robot KIT (số tâm–tâm đo tay, dung sai gia công 6 cm). LegProfile kitTrolley(bool accept_attached_legs) { LegProfile p; p.cart_opening = 0.48; p.cart_depth = 0.762; p.cart_dim_tolerance = 0.06; p.leg_shape = LegShape::kBox; p.leg_width = 0.030; p.leg_width_tolerance = 0.03; p.min_legs = 3; p.accept_attached_legs = accept_attached_legs; return p; } LegDetection detect(const RecordedScan& s, bool accept_attached_legs, bool zero_is_no_echo) { LegPatternMatcher matcher; std::string error; EXPECT_TRUE(matcher.configure(kitTrolley(accept_attached_legs), error)) << error; ScanLimits limits; limits.min_range = 0.10; limits.max_range = 3.0; limits.range_sigma = 0.01; limits.zero_range_is_no_echo = zero_is_no_echo; return matcher.match(s.geometry, s.ranges, {}, limits, Pose2D()); } double yawError(double a, double b) { return std::fabs(std::atan2(std::sin(a - b), std::cos(a - b))); } const char* const kHiddenLegScans[] = { "entry_left_hidden", "entry_right_hidden", "far_left_hidden", "close_entry_right_hidden", "close_entry_left_hidden" }; TEST(LegPatternMatcherKit, DataFileLoads) { const std::vector scans = loadScans(); ASSERT_EQ(scans.size(), 6u); for (const RecordedScan& s : scans) { EXPECT_EQ(s.ranges.size(), s.geometry.size) << s.name; } } TEST(LegPatternMatcherKit, AllFourLegsVisibleIsFoundEitherWay) { // Cột đứng cạnh chân xa-phải 9 cm cũng ghép được thành "chân xa-phải". Khi tắt khoá, cả hai giả // thuyết qua mọi cổng và cùng điểm tin 1.00; giả thuyết lấy nhầm cột gần cảm biến hơn nhưng khớp // tệ hơn. Phải chọn theo mức khớp, không theo khoảng cách. const std::vector scans = loadScans(); const RecordedScan& s = scanNamed(scans, "far_right_leg_beside_post"); for (int mode = 0; mode < 2; ++mode) { const bool on = mode == 1; const LegDetection d = detect(s, on, on); ASSERT_TRUE(d.valid) << "keys " << (on ? "on" : "off") << ": " << d.reason; EXPECT_EQ(d.legs_seen, 4); EXPECT_NEAR(d.center_x, s.ref_x, 0.02) << "keys " << (on ? "on" : "off"); EXPECT_NEAR(d.center_y, s.ref_y, 0.02) << "keys " << (on ? "on" : "off"); EXPECT_LT(yawError(d.center_yaw, s.ref_yaw), 1.5 * kDeg) << "keys " << (on ? "on" : "off"); } } TEST(LegPatternMatcherKit, OneHiddenLegFailsWithoutTheNewKeys) { // Hành vi trước 28/10, giữ nguyên khi tắt cả hai khoá: chân xa-phải dính vật phía sau bị loại, // nên che thêm một chân là chỉ còn hai chân sạch. const std::vector scans = loadScans(); for (const char* name : kHiddenLegScans) { const LegDetection d = detect(scanNamed(scans, name), false, false); EXPECT_FALSE(d.valid) << name << ": unexpectedly found at (" << d.center_x << ", " << d.center_y << ")"; } } TEST(LegPatternMatcherKit, OneHiddenLegIsFoundWithAttachedLegsAndNoEcho) { const std::vector scans = loadScans(); for (const char* name : kHiddenLegScans) { const RecordedScan& s = scanNamed(scans, name); const LegDetection d = detect(s, true, true); ASSERT_TRUE(d.valid) << name << ": " << d.reason; EXPECT_EQ(d.legs_seen, 3) << name; EXPECT_EQ(d.legs_attached, 1) << name << ": the far-right leg is the attached one"; // Chân bị cột che: cột gần hơn chỗ chân (occluded) hoặc nằm trong dải range của chân (distorted). EXPECT_EQ(d.legs_occluded + d.legs_distorted, 1) << name; EXPECT_NEAR(d.center_x, s.ref_x, 0.02) << name; EXPECT_NEAR(d.center_y, s.ref_y, 0.02) << name; EXPECT_LT(yawError(d.center_yaw, s.ref_yaw), 1.5 * kDeg) << name; } } TEST(LegPatternMatcherKit, ZeroRangeAsNoEchoClearsTheFalsePartialFlags) { // Driver trả 0 cho tia không phản hồi. Đọc 0 là "bị chắn sát cảm biến" thì mọi mép chân kề tia 0 // thành mép bị che: cả bốn chân partial dù đứng trơ trọi. const std::vector scans = loadScans(); const RecordedScan& s = scanNamed(scans, "far_right_leg_beside_post"); const LegDetection blocked = detect(s, false, false); const LegDetection no_echo = detect(s, false, true); ASSERT_TRUE(blocked.valid) << blocked.reason; ASSERT_TRUE(no_echo.valid) << no_echo.reason; EXPECT_EQ(blocked.legs_partial, 4); EXPECT_LT(no_echo.legs_partial, blocked.legs_partial); } } // namespace int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }