From edc6b3043128f7d53184359d84c75955f33698af Mon Sep 17 00:00:00 2001 From: duongtd Date: Tue, 27 Oct 2026 13:09:30 +0700 Subject: [PATCH] feat(v_pattern): add v_shape gate to tell a concave dock V from a convex corner New profile key v_shape (any | concave | convex), default any = old behaviour. A corner of a cabinet or pallet with the same vertex_angle and wing_length passes every other gate; only the opening direction tells it from a dock that wraps the robot. v_shape is rejected on a non-v pattern. A rejection by orientation has its own reason string. Tests added. --- include/dock_pattern_detector/dock_profile.h | 34 ++++++++ .../dock_pattern_detector/v_pattern_matcher.h | 9 +- src/dock_profile.cpp | 43 ++++++++++ src/v_pattern_matcher.cpp | 27 +++++- test/v_pattern_matcher_test.cpp | 86 ++++++++++++++++++- 5 files changed, 193 insertions(+), 6 deletions(-) diff --git a/include/dock_pattern_detector/dock_profile.h b/include/dock_pattern_detector/dock_profile.h index c165cda..567dca5 100644 --- a/include/dock_pattern_detector/dock_profile.h +++ b/include/dock_pattern_detector/dock_profile.h @@ -36,6 +36,27 @@ const char* toString(PatternType pattern); /// @brief Đọc tên dạng từ YAML. @return false nếu tên không có thật. bool patternFromString(const std::string& name, PatternType& out, std::string& error); +/** + * @enum VShape + * @brief Lòng chữ V mở về phía nào so với cảm biến. Chỉ có nghĩa khi `pattern == kV`. + * + * Phân biệt trạm dock chữ V (thường LÕM — tấm ÔM lấy robot, robot lùi vào giữa hai cánh) với một + * góc LỒI cùng kích thước của vật khác (góc tủ, góc pallet — cạnh nào cũng nhô ra phía cảm biến). + * Không có cổng này, `vertex_angle` + `wing_length` đúng số là đủ để một góc tủ 90 độ, cạnh 0.3 m + * lọt qua y như trạm thật. + */ +enum class VShape +{ + kAny, ///< Nhận cả hai hướng — hành vi trước khi có khoá này. + kConcave, ///< Chỉ nhận V LÕM: lòng V mở VỀ phía cảm biến (trạm ôm robot). + kConvex ///< Chỉ nhận V LỒI: lòng V mở RA XA cảm biến (góc lồi của vật khác). +}; + +const char* toString(VShape shape); + +/// @brief Đọc tên hướng từ YAML (any / concave / convex). @return false nếu tên không có thật. +bool vShapeFromString(const std::string& name, VShape& out, std::string& error); + /** * @struct DockProfile * @brief Hình học và offset của MỘT cái sạc. @@ -77,6 +98,19 @@ struct DockProfile */ double vertex_gap = 0.05; + /** + * Lòng V phải mở về hướng nào so với cảm biến. Xem @ref VShape. + * + * `kAny` (mặc định) = nhận cả hai hướng, giống hành vi trước khi có khoá này — không chỉnh gì + * thì không đổi hành vi cũ. + * + * ► Chỉ đặt `kConcave` hoặc `kConvex` sau khi đã XÁC NHẬN bằng scan thật: so khoảng cách từ cảm + * biến tới đỉnh với khoảng cách tới hai đầu mút cánh. Mút GẦN cảm biến hơn đỉnh = LÕM; mút XA + * hơn đỉnh = LỒI. Đoán sai hướng ở đây là loại luôn trạm thật, giống hệt triệu chứng "dò mãi + * không ra". + */ + VShape v_shape = VShape::kAny; + // --- Cung tròn lõm (pattern: arc) ---------------------------------------------------------------- // // Máng cong ôm lấy robot: tâm đường tròn nằm PHÍA CẢM BIẾN, mặt cong ở xa hơn tâm. Đây là thứ diff --git a/include/dock_pattern_detector/v_pattern_matcher.h b/include/dock_pattern_detector/v_pattern_matcher.h index d4a865e..353a7b1 100644 --- a/include/dock_pattern_detector/v_pattern_matcher.h +++ b/include/dock_pattern_detector/v_pattern_matcher.h @@ -38,10 +38,15 @@ namespace dock_pattern_detector * có hai đầu đoạn nằm ngay tại đó. * 3. **Góc trong tại đỉnh** đo giữa hai vector cánh xuất phát từ đỉnh, so với `vertex_angle`. * 4. **Chiều dài mỗi cánh** so với `wing_length`. - * 5. **Dải phản quang** (chỉ khi `tape_enabled`) — ứng viên đã đúng hình còn phải mang hai vệt + * 5. **Hướng lõm/lồi** (`v_shape`, mặc định `kAny` = bỏ qua bước này) — so dấu của tích vô hướng + * giữa phân giác (chưa lật) và vector về phía cảm biến để biết lòng V mở về cảm biến (LÕM) hay + * ra xa cảm biến (LỒI), rồi loại ứng viên sai hướng. Một góc tủ hoặc góc pallet cùng + * `vertex_angle`/`wing_length` với trạm thật vẫn là một chữ V hợp lệ ở bốn cổng trên — chỉ + * hướng mới phân biệt được. + * 6. **Dải phản quang** (chỉ khi `tape_enabled`) — ứng viên đã đúng hình còn phải mang hai vệt * tia sáng, cách nhau đúng `tape_spacing`. Xem @ref match(const std::vector&, const * std::vector&). - * 6. **Cổng prior** — ứng viên ra ngoài `search_radius` quanh pose kỳ vọng, hoặc ngoài quạt + * 7. **Cổng prior** — ứng viên ra ngoài `search_radius` quanh pose kỳ vọng, hoặc ngoài quạt * `search_fov`, thì loại. Không có cổng này thì trong kho, hai pallet vuông góc bất kỳ cũng là * một chữ V. * diff --git a/src/dock_profile.cpp b/src/dock_profile.cpp index e4cb13c..35d4fe9 100644 --- a/src/dock_profile.cpp +++ b/src/dock_profile.cpp @@ -63,6 +63,42 @@ bool patternFromString(const std::string& name, PatternType& out, std::string& e return false; } +const char* toString(VShape shape) +{ + switch (shape) + { + case VShape::kAny: + return "any"; + case VShape::kConcave: + return "concave"; + case VShape::kConvex: + return "convex"; + } + return "unknown"; +} + +bool vShapeFromString(const std::string& name, VShape& out, std::string& error) +{ + if (name == "any") + { + out = VShape::kAny; + return true; + } + if (name == "concave") + { + out = VShape::kConcave; + return true; + } + if (name == "convex") + { + out = VShape::kConvex; + return true; + } + + error = "unknown v_shape '" + name + "'; known values are: any, concave, convex"; + return false; +} + bool DockProfile::validate(std::string& error) const { if (!std::isfinite(vertex_angle) || vertex_angle <= 0.0 || vertex_angle >= M_PI) @@ -101,6 +137,13 @@ bool DockProfile::validate(std::string& error) const error = "vertex_gap must be < wing_length, otherwise any two crossing walls pass as a V"; return false; } + if (v_shape != VShape::kAny && pattern != PatternType::kV) + { + // Nói thẳng thay vì nhận config rồi không bao giờ có hiệu lực: cổng lõm/lồi chỉ VPatternMatcher + // đọc, một profile `pattern: arc` mang khoá này là đang khai một thứ sẽ bị lờ đi im lặng. + error = "v_shape is only meaningful when pattern is 'v'"; + return false; + } if (has_prior) { if (!std::isfinite(prior_x) || !std::isfinite(prior_y)) diff --git a/src/v_pattern_matcher.cpp b/src/v_pattern_matcher.cpp index 691e709..5d08b7b 100644 --- a/src/v_pattern_matcher.cpp +++ b/src/v_pattern_matcher.cpp @@ -247,6 +247,7 @@ Detection VPatternMatcher::match(const std::vector& lines, std::size_t rejected_vertex_gap = 0; std::size_t rejected_angle = 0; std::size_t rejected_length = 0; + std::size_t rejected_shape = 0; std::size_t rejected_support = 0; std::size_t rejected_prior = 0; std::size_t rejected_tape = 0; @@ -400,7 +401,21 @@ Detection VPatternMatcher::match(const std::vector& lines, by /= bn; // Gốc cảm biến là (0, 0) trong frame của scan, nên vector từ đỉnh về phía cảm biến là -apex. - if (bx * (-apex.x) + by * (-apex.y) < 0.0) + // TRƯỚC khi lật: b đang chỉ vào LÒNG chữ V (xem class doc). Lòng V đã hướng VỀ cảm biến + // (dot >= 0) là V LÕM — tấm ôm lấy robot; lòng V hướng RA XA cảm biến (dot < 0) là V LỒI — + // góc ngoài của một vật khác (tủ, pallet). Phải đọc dấu NGAY ĐÂY, trước khi bước lật dưới + // xoá mất nó bằng cách luôn quay b về phía cảm biến. + const double dot_to_sensor = bx * (-apex.x) + by * (-apex.y); + const bool opens_to_sensor = dot_to_sensor >= 0.0; // true = lõm, false = lồi + + if ((profile_.v_shape == VShape::kConcave && !opens_to_sensor) || + (profile_.v_shape == VShape::kConvex && opens_to_sensor)) + { + ++rejected_shape; + continue; + } + + if (dot_to_sensor < 0.0) { bx = -bx; by = -by; @@ -599,6 +614,16 @@ Detection VPatternMatcher::match(const std::vector& lines, best.reason = std::to_string(rejected_confidence) + " candidate(s) matched but scored below min_confidence"; } + else if (rejected_shape > 0) + { + // Nói rõ profile đang đòi hướng nào — "đúng hình nhưng sai hướng" đòi xử trí khác hẳn "sai + // góc/chiều dài": đây không phải lỗi dung sai, mà là một vật khác hình chữ V thật (góc tủ, + // góc pallet lồi ra phía cảm biến thay vì lõm vào). + best.reason = std::to_string(rejected_shape) + + " candidate(s) had the right vertex angle and wing length but the wrong " + "concave/convex orientation (profile requires v_shape=" + + toString(profile_.v_shape) + ")"; + } else if (rejected_length > 0 || rejected_angle > 0) { // Số ĐO ĐƯỢC của ứng viên trượt sát nhất. Không có nó thì "cánh 0.22 m vì bị che" và diff --git a/test/v_pattern_matcher_test.cpp b/test/v_pattern_matcher_test.cpp index 2d53477..e89eaa2 100644 --- a/test/v_pattern_matcher_test.cpp +++ b/test/v_pattern_matcher_test.cpp @@ -30,6 +30,7 @@ using dock_pattern_detector::Detection; using dock_pattern_detector::DockProfile; using dock_pattern_detector::PatternType; using dock_pattern_detector::VPatternMatcher; +using dock_pattern_detector::VShape; using robot_line_extraction::Line; using robot_line_extraction::LineExtractionParams; using robot_line_extraction::LineExtractor; @@ -90,10 +91,15 @@ std::vector rayCast(const ScanGeometry& g, const std::vector& w * @param half_angle [rad] độ nghiêng mỗi cánh so với trục y * @param wing [m] chiều dài một cánh * @param yaw [rad] xoay cả tấm quanh đỉnh + * @param concave false (mặc định) = mút cánh XA cảm biến hơn đỉnh, tức V LỒI — y hệt hình mọi + * test khác trong file này vẫn dùng. true = mút cánh GẦN cảm biến hơn đỉnh, tức + * V LÕM — lòng V mở về phía cảm biến, như trạm ôm robot. * - * Góc TRONG tại đỉnh = pi - 2*half_angle. `half = pi/4` cho tấm mở 90 độ. + * Góc TRONG tại đỉnh = pi - 2*half_angle. `half = pi/4` cho tấm mở 90 độ. Đổi `concave` chỉ lật + * dấu offset x của mút cánh; góc đỉnh và chiều dài cánh giữ nguyên. */ -std::vector vPlate(double apex_x, double half_angle, double wing, double yaw = 0.0) +std::vector vPlate(double apex_x, double half_angle, double wing, double yaw = 0.0, + bool concave = false) { const double c = std::cos(yaw); const double s = std::sin(yaw); @@ -102,7 +108,8 @@ std::vector vPlate(double apex_x, double half_angle, double wing, doubl oy = s * (x - apex_x) + c * y; }; - const double tip_x = apex_x + wing * std::sin(half_angle); + const double wing_dir = concave ? -1.0 : 1.0; + const double tip_x = apex_x + wing_dir * wing * std::sin(half_angle); const double tip_y = wing * std::cos(half_angle); Segment w1; @@ -395,6 +402,79 @@ TEST(VPatternMatcher, APalletCornerIsNotADock) EXPECT_NE(d.reason.find("wing length"), std::string::npos) << d.reason; } +// ------------------------------------------------------------------------------------------------ +// 2b. Hướng lõm/lồi — phân biệt trạm thật với góc lồi CÙNG kích thước (tủ, pallet) +// ------------------------------------------------------------------------------------------------ + +TEST(VPatternMatcher, VShapeDefaultsToAnyAndAcceptsBothOrientations) +{ + // vProfile() không đặt v_shape, nên mặc định phải là kAny — không chỉnh gì thì không đổi hành vi + // cũ. Chữ V lồi (hình mọi test "dò đúng" khác đang dùng) và chữ V lõm đều phải lọt. + DockProfile p = vProfile(); + ASSERT_EQ(p.v_shape, VShape::kAny); + + Rig rig_convex(p); + ASSERT_TRUE(rig_convex.ok_) << rig_convex.error_; + const Detection convex = rig_convex.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.0, /*concave=*/false)); + EXPECT_TRUE(convex.valid) << convex.reason; + + Rig rig_concave(p); + ASSERT_TRUE(rig_concave.ok_) << rig_concave.error_; + const Detection concave = rig_concave.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.0, /*concave=*/true)); + EXPECT_TRUE(concave.valid) << concave.reason; +} + +TEST(VPatternMatcher, ConcaveProfileAcceptsAConcaveVAndRejectsAConvexOne) +{ + DockProfile p = vProfile(); + p.v_shape = VShape::kConcave; + + // Lòng V mở VỀ cảm biến — trạm ôm robot. Cùng góc đỉnh, cùng chiều dài cánh với mọi test khác. + Rig rig_ok(p); + ASSERT_TRUE(rig_ok.ok_) << rig_ok.error_; + const Detection ok = rig_ok.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.0, /*concave=*/true)); + EXPECT_TRUE(ok.valid) << ok.reason; + + // Góc LỒI cùng kích thước (tủ, pallet) — đây chính là dương tính giả mà khoá này phải chặn. + Rig rig_reject(p); + ASSERT_TRUE(rig_reject.ok_) << rig_reject.error_; + const Detection rejected = rig_reject.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.0, /*concave=*/false)); + EXPECT_FALSE(rejected.valid); + EXPECT_NE(rejected.reason.find("orientation"), std::string::npos) << rejected.reason; +} + +TEST(VPatternMatcher, ConvexProfileAcceptsAConvexVAndRejectsAConcaveOne) +{ + DockProfile p = vProfile(); + p.v_shape = VShape::kConvex; + + // Lòng V mở RA XA cảm biến — góc lồi của vật (tủ, pallet), hoặc một trạm dock kiểu mỏ hàn. + Rig rig_ok(p); + ASSERT_TRUE(rig_ok.ok_) << rig_ok.error_; + const Detection ok = rig_ok.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.0, /*concave=*/false)); + EXPECT_TRUE(ok.valid) << ok.reason; + + // V lõm cùng kích thước phải bị loại khi profile đòi lồi. + Rig rig_reject(p); + ASSERT_TRUE(rig_reject.ok_) << rig_reject.error_; + const Detection rejected = rig_reject.run(vPlate(1.20, M_PI / 4.0, 0.30, 0.0, /*concave=*/true)); + EXPECT_FALSE(rejected.valid); + EXPECT_NE(rejected.reason.find("orientation"), std::string::npos) << rejected.reason; +} + +TEST(VPatternMatcher, RefusesAVShapeProfileOnAnArcPattern) +{ + // v_shape chỉ VPatternMatcher đọc; đặt nó trên profile 'arc' là khai một khoá sẽ bị lờ đi im + // lặng. validate() phải bắt lúc nạp config, không phải lúc dò. + DockProfile p; + p.pattern = PatternType::kArc; + p.v_shape = VShape::kConcave; + + std::string error; + EXPECT_FALSE(p.validate(error)); + EXPECT_NE(error.find("v_shape"), std::string::npos) << error; +} + TEST(VPatternMatcher, TwoWallsThatCrossFarFromTheirEndsAreNotAV) { Rig rig;