From 000374791b4ebc72b59ea79bab5b81780c4493c4 Mon Sep 17 00:00:00 2001 From: duongtd Date: Wed, 26 Aug 2026 15:07:24 +0700 Subject: [PATCH] Separate the measured calibration from the derived offset offset_x/y/yaw carried two different things at once. Part of it is geometry that can be worked out on paper - half the robot, the footprint padding, how far the wings project toward the robot, the clearance. The rest is whatever the real station turns out to need: a laser mounted slightly off, odometry bias, a systematic lean in the detection. Folded into one number, changing the footprint silently discards the field correction that was tuned into it, and nobody can look at the value later and say which half is which. The first time someone re-derives the geometry, the calibration is gone and nothing says so. calib_x/y/yaw are added on top, in the same frame, defaulting to zero. Dock, measure how far base_link ended up from where it should have, subtract it here. The geometry stays derivable and the correction stays visible. They are bounded to 0.30 m and 0.20 rad. Field calibration is centimetres and degrees; needing half a metre means offset_x is wrong, and taping over that with a calibration term hides the cause until the next footprint change makes it worse. --- include/dock_pattern_detector/dock_profile.h | 25 +++++++++++ src/dock_profile.cpp | 26 ++++++++++++ src/v_pattern_matcher.cpp | 14 ++++--- test/v_pattern_matcher_test.cpp | 44 ++++++++++++++++++++ 4 files changed, 104 insertions(+), 5 deletions(-) diff --git a/include/dock_pattern_detector/dock_profile.h b/include/dock_pattern_detector/dock_profile.h index 0d6cce7..7168816 100644 --- a/include/dock_pattern_detector/dock_profile.h +++ b/include/dock_pattern_detector/dock_profile.h @@ -112,6 +112,31 @@ struct DockProfile /// [rad] Bù tấm lắp XOAY so với đầu cắm. double offset_yaw = 0.0; + // --- Hiệu chỉnh hiện trường ------------------------------------------------------------------- + // + // Cộng thêm vào `offset_*`, cùng hệ quy chiếu. Tách riêng chứ không gộp vào `offset_*` vì hai + // nhóm này đến từ hai nguồn khác nhau và có vòng đời khác nhau: + // + // offset_* SUY RA được bằng công thức (nửa thân xe + padding + hình chiếu cánh + khe hở). + // Đổi xe, đổi padding hay đổi tấm là phải TÍNH LẠI. + // calib_* ĐO được sau khi dock thật: sai lệch còn lại do cảm biến lắp lệch, odometry lệch, + // hoặc phép dò lệch hệ thống. Không suy ra được, chỉ đo. + // + // Gộp chung thì mỗi lần đổi footprint là mất luôn phần hiệu chỉnh đã đo, và không ai phân biệt + // được con số nào là hình học, con số nào là bù sai số. + // + // Cách dùng: dock thật, đo sai lệch của `base_link` so với chỗ đáng lẽ phải dừng, rồi TRỪ vào + // đây. Dừng xa 2 cm -> calib_x = -0.02. + + /// [m] Hiệu chỉnh dọc pháp tuyến. Dương = dừng xa tấm hơn. + double calib_x = 0.0; + + /// [m] Hiệu chỉnh ngang. Dương = lệch sang trái theo hướng tấm. + double calib_y = 0.0; + + /// [rad] Hiệu chỉnh góc. + double calib_yaw = 0.0; + // --- Cổng chất lượng --------------------------------------------------------------------------- /// Số tia đỡ tối thiểu cho MỘT cánh. Dưới ngưỡng này phép khớp không còn nghĩa thống kê. diff --git a/src/dock_profile.cpp b/src/dock_profile.cpp index 6ed7e65..dac9383 100644 --- a/src/dock_profile.cpp +++ b/src/dock_profile.cpp @@ -13,6 +13,12 @@ namespace dock_pattern_detector namespace { +/// [m] Trần hiệu chỉnh tịnh tiến. Xem lý do ở validate(). +constexpr double kMaxCalibXy = 0.30; + +/// [rad] Trần hiệu chỉnh góc, ~11 độ. +constexpr double kMaxCalibYaw = 0.20; + bool positive(double value) { return std::isfinite(value) && value > 0.0; @@ -124,6 +130,26 @@ bool DockProfile::validate(std::string& error) const error = "offset_x/offset_y/offset_yaw must be finite"; return false; } + if (!std::isfinite(calib_x) || !std::isfinite(calib_y) || !std::isfinite(calib_yaw)) + { + error = "calib_x/calib_y/calib_yaw must be finite"; + return false; + } + // Chặn trên có chủ đích. Hiệu chỉnh hiện trường là centimet và độ; ai gõ 0.5 m vào đây thì thứ + // họ đang bù không phải sai số cảm biến mà là một lỗi hình học ở `offset_*` — và dán băng dính + // lên nó bằng calib là giấu mất nguyên nhân. + if (std::fabs(calib_x) > kMaxCalibXy || std::fabs(calib_y) > kMaxCalibXy) + { + error = "calib_x/calib_y must be within +-" + std::to_string(kMaxCalibXy) + + " m; a larger correction means offset_x/offset_y is wrong, not miscalibrated"; + return false; + } + if (std::fabs(calib_yaw) > kMaxCalibYaw) + { + error = "calib_yaw must be within +-" + std::to_string(kMaxCalibYaw) + + " rad; a larger correction means the pattern geometry is wrong"; + return false; + } if (min_wing_points < 2) { // Hai điểm là tối thiểu tuyệt đối để có một đoạn; dưới đó không có gì để khớp. diff --git a/src/v_pattern_matcher.cpp b/src/v_pattern_matcher.cpp index e873c50..b0a6d12 100644 --- a/src/v_pattern_matcher.cpp +++ b/src/v_pattern_matcher.cpp @@ -273,13 +273,17 @@ Detection VPatternMatcher::match(const std::vector& lines) const best.measured_wing_length[1] = len2; // Pose đích = đỉnh dịch theo offset của profile, trong hệ của CHÍNH hình: x dọc pháp tuyến - // hướng ra phía robot, y sang trái. + // hướng ra phía robot, y sang trái. `calib_*` cộng thẳng vào offset — cùng hệ quy chiếu, chỉ + // khác nguồn gốc: offset suy ra từ hình học, calib đo được ngoài hiện trường. + const double dx = profile_.offset_x + profile_.calib_x; + const double dy = profile_.offset_y + profile_.calib_y; + const double dyaw = profile_.offset_yaw + profile_.calib_yaw; + const double cos_y = std::cos(yaw); const double sin_y = std::sin(yaw); - best.x = apex.x + cos_y * profile_.offset_x - sin_y * profile_.offset_y; - best.y = apex.y + sin_y * profile_.offset_x + cos_y * profile_.offset_y; - best.yaw = std::atan2(std::sin(yaw + profile_.offset_yaw), - std::cos(yaw + profile_.offset_yaw)); + best.x = apex.x + cos_y * dx - sin_y * dy; + best.y = apex.y + sin_y * dx + cos_y * dy; + best.yaw = std::atan2(std::sin(yaw + dyaw), std::cos(yaw + dyaw)); } } diff --git a/test/v_pattern_matcher_test.cpp b/test/v_pattern_matcher_test.cpp index ea6904a..32079da 100644 --- a/test/v_pattern_matcher_test.cpp +++ b/test/v_pattern_matcher_test.cpp @@ -217,6 +217,50 @@ TEST(VPatternMatcher, TheOffsetMovesTheGoalOutTowardTheRobot) EXPECT_NEAR(d.y, 0.00, 0.03); } +TEST(VPatternMatcher, CalibrationShiftsTheGoalOnTopOfTheOffset) +{ + DockProfile p = vProfile(); + p.offset_x = 0.60; // [m] phần HÌNH HỌC, suy ra bằng công thức + p.calib_x = -0.03; // [m] phần ĐO ĐƯỢC: lần dock thật dừng xa 3 cm + p.calib_y = 0.02; // [m] + + Rig rig(p); + ASSERT_TRUE(rig.ok_) << rig.error_; + + const Detection d = rig.run(vPlate(1.20, M_PI / 4.0, 0.30)); + ASSERT_TRUE(d.valid) << d.reason; + + // Đỉnh ở x=1.20; hiệu chỉnh cộng thẳng vào offset, cùng hệ quy chiếu của hình. + // Hình quay 180 độ so với trục x của cảm biến nên +x của hình là -x của cảm biến. + EXPECT_NEAR(d.apex_x, 1.20, 0.02); + EXPECT_NEAR(d.x, 1.20 - (0.60 - 0.03), 0.03); + EXPECT_NEAR(d.y, -0.02, 0.03); +} + +TEST(VPatternMatcher, RefusesACalibrationBigEnoughToBeHidingAGeometryError) +{ + VPatternMatcher matcher; + std::string error; + + DockProfile huge = vProfile(); + huge.calib_x = 0.5; // [m] + // Hiệu chỉnh hiện trường là centimet. Nửa mét nghĩa là `offset_x` sai, và dán băng dính lên nó + // bằng calib là giấu mất nguyên nhân — lần đổi footprint sau sẽ không ai hiểu con số này từ đâu. + EXPECT_FALSE(matcher.configure(huge, error)); + EXPECT_NE(error.find("calib_x"), std::string::npos) << error; + + DockProfile spin = vProfile(); + spin.calib_yaw = 0.6; // [rad] ~34 do + EXPECT_FALSE(matcher.configure(spin, error)); + EXPECT_NE(error.find("calib_yaw"), std::string::npos) << error; + + // Hiệu chỉnh thật thì phải nhận. + DockProfile ok = vProfile(); + ok.calib_x = -0.03; + ok.calib_yaw = 0.02; + EXPECT_TRUE(matcher.configure(ok, error)) << error; +} + TEST(VPatternMatcher, ATiltedPlateIsStillFoundAndItsYawFollows) { Rig rig;