From efa8f4bb48db0412f3bf6936e48dd03ccc31bfe0 Mon Sep 17 00:00:00 2001 From: duongtd Date: Wed, 26 Aug 2026 20:15:05 +0700 Subject: [PATCH] feat: route a profile to the matcher its pattern names, and add an arc dock model The handler held one concrete VPatternMatcher, so a profile declaring pattern: arc could not reach the arc matcher at all. It now picks by pattern at start(), reads the arc_* keys from YAML, and calls whichever matcher the profile named. An explicit switch, not a virtual interface: the set of patterns is a closed enum, each matcher weighs one DockProfile, and adding a shape is one member and two branches - cheaper than an abstraction layer over two implementations. Each matcher refuses a profile that is not its own, so a branch missing from that switch shows up at BOOT with the pattern named, not at runtime as "it never detects". Intensities go only to the V matcher. findTape walks the ray span of a fitted line segment and an arc produces none, so an arc profile that sets tape_enabled is already refused when the config loads. sim/models/dock_cradle_arc is a concave cradle, radius 0.40 m over 2.0 rad, for testing this in Gazebo. Its origin sits ON the deepest point, unlike dock_plate_v whose origin is 0.2121 m off the apex and has caused confusion: the detector reports the deepest point, so here that is the same number you spawn at, with nothing to subtract. It is 24 flat facets rather than a mesh, because Gazebo classic has no arc primitive and a mesh means a binary file in the repo. The approximation error is the sagitta of one chord, 0.35 mm - an order below the 3 mm range noise, so the circle fit cannot tell it from a real curve. Two tests pin the routing rather than the matching rules, which have their own tests in dock_pattern_detector: a cradle under an arc profile writes the goal frame at the deepest point, and a V plate under the same arc profile is rejected. Without the second one, a change that broke arc detection outright would look exactly like the gate doing its job. --- plugins/dock_pattern_action_handler.cpp | 47 +++- sim/launch/spawn_dock_cradle.launch | 37 ++++ sim/models/dock_cradle_arc/model.config | 9 + sim/models/dock_cradle_arc/model.sdf | 283 ++++++++++++++++++++++++ test/config/action_handlers_params.yaml | 12 + test/dock_pattern_action_test.cpp | 46 ++++ 6 files changed, 426 insertions(+), 8 deletions(-) create mode 100644 sim/launch/spawn_dock_cradle.launch create mode 100644 sim/models/dock_cradle_arc/model.config create mode 100644 sim/models/dock_cradle_arc/model.sdf diff --git a/plugins/dock_pattern_action_handler.cpp b/plugins/dock_pattern_action_handler.cpp index 7a8c800..203f996 100644 --- a/plugins/dock_pattern_action_handler.cpp +++ b/plugins/dock_pattern_action_handler.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -217,8 +218,19 @@ public: return false; } + // Chọn bộ khớp theo biên dạng của profile. Một switch tường minh chứ không phải interface ảo: + // tập biên dạng là một enum ĐÓNG, mỗi bộ khớp chỉ nặng bằng một DockProfile, và thêm dạng mới + // là thêm một member cộng hai nhánh — rẻ hơn một tầng ảo cho hai hiện thực. + // + // Mỗi bộ khớp tự từ chối profile không thuộc về nó, nên nếu switch này quên một nhánh thì lỗi + // hiện ra lúc BOOT kèm tên biên dạng, không phải lúc chạy dưới dạng "dò mãi không ra". std::string error; - if (!matcher_.configure(found->second, error)) + active_pattern_ = found->second.pattern; + const bool configured = + (active_pattern_ == dock_pattern_detector::PatternType::kArc) + ? arc_matcher_.configure(found->second, error) + : v_matcher_.configure(found->second, error); + if (!configured) { robot::log_error("[dock_pattern] '%s': profile '%s' is invalid: %s", name_.c_str(), profile_name.c_str(), error.c_str()); @@ -350,6 +362,16 @@ private: nh_param(p_nh, "offset_yaw", profile.offset_yaw); nh_param(p_nh, "min_confidence", profile.min_confidence); + nh_param(p_nh, "arc_radius", profile.arc_radius); + nh_param(p_nh, "arc_radius_tolerance", profile.arc_radius_tolerance); + nh_param(p_nh, "arc_span", profile.arc_span); + nh_param(p_nh, "arc_min_span", profile.arc_min_span); + nh_param(p_nh, "arc_max_rms", profile.arc_max_rms); + + int min_arc_points = static_cast(profile.min_arc_points); + nh_param(p_nh, "min_arc_points", min_arc_points); + profile.min_arc_points = static_cast(std::max(0, min_arc_points)); + nh_param(p_nh, "tape_enabled", profile.tape_enabled); nh_param(p_nh, "tape_spacing", profile.tape_spacing); nh_param(p_nh, "tape_width", profile.tape_width); @@ -470,7 +492,13 @@ private: // `intensities` đi thẳng, không đổi kiểu: message đã là std::vector. Rỗng khi driver // không gửi cường độ — matcher tự nói ra điều đó nếu profile lại đang đòi dải phản quang. - const dock_pattern_detector::Detection detection = matcher_.match(lines_, scan->intensities); + // + // Cung tròn không nhận cường độ: `findTape` quét theo chỉ số tia của một ĐOẠN THẲNG, mà cung + // không sinh ra đoạn nào. Profile `arc` bật `tape_enabled` đã bị từ chối từ lúc boot. + const dock_pattern_detector::Detection detection = + (active_pattern_ == dock_pattern_detector::PatternType::kArc) + ? arc_matcher_.match(lines_) + : v_matcher_.match(lines_, scan->intensities); if (!detection.valid) { noteReason(now, detection.reason); @@ -483,16 +511,17 @@ private: // quang đang bật. Đây là số người lắp trạm cần để đặt dung sai: dán dải lệch vài centimet vẫn // dò ra hôm nay rồi trượt hẳn khi trời ẩm hoặc dải bẩn bớt sáng, mà không có dòng này thì // không ai biết nó vốn đã sát mép. Chỉ in một lần vì nó là số hiệu chỉnh, không phải nhịp chạy. - if (matcher_.profile().tape_enabled && !tape_reported_) + if (active_pattern_ != dock_pattern_detector::PatternType::kArc && + v_matcher_.profile().tape_enabled && !tape_reported_) { tape_reported_ = true; robot::log_info("[dock_pattern] '%s': reflective tape: %zu + %zu bright ray(s), spacing " "%.3f m (profile %.3f+-%.3f), widths %.3f / %.3f m (profile %.3f+-%.3f).", name_.c_str(), detection.tape_points[0], detection.tape_points[1], - detection.measured_tape_spacing, matcher_.profile().tape_spacing, - matcher_.profile().tape_spacing_tolerance, detection.measured_tape_width[0], - detection.measured_tape_width[1], matcher_.profile().tape_width, - matcher_.profile().tape_width_tolerance); + detection.measured_tape_spacing, v_matcher_.profile().tape_spacing, + v_matcher_.profile().tape_spacing_tolerance, + detection.measured_tape_width[0], detection.measured_tape_width[1], + v_matcher_.profile().tape_width, v_matcher_.profile().tape_width_tolerance); } pending_ = detection; @@ -645,7 +674,9 @@ private: std::map profiles_; robot_line_extraction::LineExtractor extractor_; - dock_pattern_detector::VPatternMatcher matcher_; + dock_pattern_detector::VPatternMatcher v_matcher_; + dock_pattern_detector::ArcPatternMatcher arc_matcher_; + dock_pattern_detector::PatternType active_pattern_ = dock_pattern_detector::PatternType::kV; PoseSampleFilter filter_; robot::Time started_at_; diff --git a/sim/launch/spawn_dock_cradle.launch b/sim/launch/spawn_dock_cradle.launch new file mode 100644 index 0000000..2640b5c --- /dev/null +++ b/sim/launch/spawn_dock_cradle.launch @@ -0,0 +1,37 @@ + + + + + + + + + + + + + diff --git a/sim/models/dock_cradle_arc/model.config b/sim/models/dock_cradle_arc/model.config new file mode 100644 index 0000000..d0585a2 --- /dev/null +++ b/sim/models/dock_cradle_arc/model.config @@ -0,0 +1,9 @@ + + + dock_cradle_arc + 1.0 + model.sdf + + Mang cong lom ban kinh 0.40 m, mo 2.0 rad. Goc model dat tai DIEM SAU NHAT cua mang. + + diff --git a/sim/models/dock_cradle_arc/model.sdf b/sim/models/dock_cradle_arc/model.sdf new file mode 100644 index 0000000..84b39e7 --- /dev/null +++ b/sim/models/dock_cradle_arc/model.sdf @@ -0,0 +1,283 @@ + + + + + true + + + -0.170046 -0.327294 0.300 0 0 -0.958333 + 0.02 0.033324 0.6 + 0 + + + -0.170046 -0.327294 0.300 0 0 -0.958333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.143601 -0.307017 0.300 0 0 -0.875000 + 0.02 0.033324 0.6 + 0 + + + -0.143601 -0.307017 0.300 0 0 -0.875000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.118936 -0.284610 0.300 0 0 -0.791667 + 0.02 0.033324 0.6 + 0 + + + -0.118936 -0.284610 0.300 0 0 -0.791667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.096221 -0.260228 0.300 0 0 -0.708333 + 0.02 0.033324 0.6 + 0 + + + -0.096221 -0.260228 0.300 0 0 -0.708333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.075615 -0.234039 0.300 0 0 -0.625000 + 0.02 0.033324 0.6 + 0 + + + -0.075615 -0.234039 0.300 0 0 -0.625000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.057260 -0.206226 0.300 0 0 -0.541667 + 0.02 0.033324 0.6 + 0 + + + -0.057260 -0.206226 0.300 0 0 -0.541667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.041284 -0.176982 0.300 0 0 -0.458333 + 0.02 0.033324 0.6 + 0 + + + -0.041284 -0.176982 0.300 0 0 -0.458333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.027797 -0.146509 0.300 0 0 -0.375000 + 0.02 0.033324 0.6 + 0 + + + -0.027797 -0.146509 0.300 0 0 -0.375000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.016894 -0.115020 0.300 0 0 -0.291667 + 0.02 0.033324 0.6 + 0 + + + -0.016894 -0.115020 0.300 0 0 -0.291667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.008649 -0.082732 0.300 0 0 -0.208333 + 0.02 0.033324 0.6 + 0 + + + -0.008649 -0.082732 0.300 0 0 -0.208333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.003121 -0.049870 0.300 0 0 -0.125000 + 0.02 0.033324 0.6 + 0 + + + -0.003121 -0.049870 0.300 0 0 -0.125000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.000347 -0.016662 0.300 0 0 -0.041667 + 0.02 0.033324 0.6 + 0 + + + -0.000347 -0.016662 0.300 0 0 -0.041667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.000347 0.016662 0.300 0 0 0.041667 + 0.02 0.033324 0.6 + 0 + + + -0.000347 0.016662 0.300 0 0 0.041667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.003121 0.049870 0.300 0 0 0.125000 + 0.02 0.033324 0.6 + 0 + + + -0.003121 0.049870 0.300 0 0 0.125000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.008649 0.082732 0.300 0 0 0.208333 + 0.02 0.033324 0.6 + 0 + + + -0.008649 0.082732 0.300 0 0 0.208333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.016894 0.115020 0.300 0 0 0.291667 + 0.02 0.033324 0.6 + 0 + + + -0.016894 0.115020 0.300 0 0 0.291667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.027797 0.146509 0.300 0 0 0.375000 + 0.02 0.033324 0.6 + 0 + + + -0.027797 0.146509 0.300 0 0 0.375000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.041284 0.176982 0.300 0 0 0.458333 + 0.02 0.033324 0.6 + 0 + + + -0.041284 0.176982 0.300 0 0 0.458333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.057260 0.206226 0.300 0 0 0.541667 + 0.02 0.033324 0.6 + 0 + + + -0.057260 0.206226 0.300 0 0 0.541667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.075615 0.234039 0.300 0 0 0.625000 + 0.02 0.033324 0.6 + 0 + + + -0.075615 0.234039 0.300 0 0 0.625000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.096221 0.260228 0.300 0 0 0.708333 + 0.02 0.033324 0.6 + 0 + + + -0.096221 0.260228 0.300 0 0 0.708333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.118936 0.284610 0.300 0 0 0.791667 + 0.02 0.033324 0.6 + 0 + + + -0.118936 0.284610 0.300 0 0 0.791667 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.143601 0.307017 0.300 0 0 0.875000 + 0.02 0.033324 0.6 + 0 + + + -0.143601 0.307017 0.300 0 0 0.875000 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + -0.170046 0.327294 0.300 0 0 0.958333 + 0.02 0.033324 0.6 + 0 + + + -0.170046 0.327294 0.300 0 0 0.958333 + 0.02 0.033324 0.6 + 0.15 0.35 0.6 10.2 0.45 0.75 1 + + + + diff --git a/test/config/action_handlers_params.yaml b/test/config/action_handlers_params.yaml index 81e31da..c961d4e 100644 --- a/test/config/action_handlers_params.yaml +++ b/test/config/action_handlers_params.yaml @@ -68,6 +68,18 @@ actions_dock: tape_width_tolerance: 0.05 tape_min_intensity: 500.0 + # Máng cong lõm — biên dạng thứ hai, đi qua ArcPatternMatcher chứ không phải VPatternMatcher. + cradle: + pattern: arc + arc_radius: 0.40 # [m] + arc_radius_tolerance: 0.06 # [m] + arc_span: 2.2 # [rad] góc mở toàn phần của máng + arc_min_span: 0.52 # [rad] sàn để tin phép khớp + arc_max_rms: 0.010 # [m] cổng chính chống nhầm góc tường thành cung + min_arc_points: 8 + min_confidence: 0.3 + offset_x: 0.60 # [m] lùi ra trước mặt máng + # scan_topic không có trong kho -> phải từ chối LÚC BOOT kèm danh sách topic có thật. actions_dock_bad_topic: handlers: diff --git a/test/dock_pattern_action_test.cpp b/test/dock_pattern_action_test.cpp index ee816d2..3d5e548 100644 --- a/test/dock_pattern_action_test.cpp +++ b/test/dock_pattern_action_test.cpp @@ -64,6 +64,20 @@ std::vector vPlate(double apex_x, double half_angle, double wing) return { { apex_x, 0.0, tip_x, tip_y }, { apex_x, 0.0, tip_x, -tip_y } }; } +/// @brief Máng cong LÕM: tâm ở @p center_x trên trục x, mặt cong ở XA hơn tâm. +std::vector concaveCradle(double center_x, double r, double span, int facets = 300) +{ + std::vector out; + for (int i = 0; i + 1 < facets; ++i) + { + const double t0 = -span / 2.0 + span * i / (facets - 1); + const double t1 = -span / 2.0 + span * (i + 1) / (facets - 1); + out.push_back({ center_x + r * std::cos(t0), r * std::sin(t0), center_x + r * std::cos(t1), + r * std::sin(t1) }); + } + return out; +} + /// @brief [m] Cự ly từ điểm (@p px, @p py) tới đoạn @p s. double distToSegment(double px, double py, const Segment& s) { @@ -542,6 +556,38 @@ TEST(DockPatternAction, ATapeProfileRefusesAPlateThatCarriesNoTape) EXPECT_NE(rig.last_message.find("reflective tape"), std::string::npos) << rig.last_message; } +TEST(DockPatternAction, DispatchesToTheArcMatcherWhenTheProfileAsksForOne) +{ + // Kiểm ĐƯỜNG ĐỊNH TUYẾN, không phải luật khớp — luật đã có test riêng bên dock_pattern_detector. + // Câu hỏi ở đây: profile khai `pattern: arc` có thật sự chạy qua ArcPatternMatcher không, hay + // handler vẫn đưa nó cho bộ khớp chữ V. Nếu đưa nhầm, bộ khớp chữ V nay TỪ CHỐI ngay lúc start, + // nên test này fail ở dòng start chứ không phải im lặng không dò ra gì. + Rig rig; + ASSERT_TRUE(rig.store_ok_); + ASSERT_TRUE(rig.loaded); + + ASSERT_TRUE(rig.handler->start(makeAction("cradle"), robot::Time(10.0))); + EXPECT_EQ(rig.feed(concaveCradle(1.20, 0.40, 2.0), 8), ActionStatus::kSucceeded) + << rig.last_message; + + // Tâm ở 1.20, bán kính 0.40 -> điểm sâu nhất ở 1.60; profile lùi 0.60 m ra phía robot. + const tf3::TransformStampedMsg out = rig.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time()); + EXPECT_NEAR(out.transform.translation.x, 1.00, 0.05); + EXPECT_NEAR(out.transform.translation.y, 0.00, 0.05); +} + +TEST(DockPatternAction, AnArcProfileRefusesAVPlate) +{ + // Mặt kia: cùng handler, cùng profile arc, nhưng trong tầm là một tấm chữ V. Không có cặp này + // thì một lỗi làm hỏng hẳn phép dò cung cũng trông y hệt "cổng đang làm việc". + Rig rig; + ASSERT_TRUE(rig.store_ok_); + ASSERT_TRUE(rig.loaded); + + ASSERT_TRUE(rig.handler->start(makeAction("cradle"), robot::Time(10.0))); + EXPECT_EQ(rig.feed(vPlate(1.20, M_PI / 4.0, 0.30), 60), ActionStatus::kFailed); +} + TEST(DockPatternAction, RefusesADefaultProfileThatDoesNotExist) { Rig rig("actions_dock_bad_default");