fix: the arc pose was defined against the sensor, so it followed the robot

The target point was the point on the fitted circle furthest from the sensor,
and yaw was the direction from there back to the sensor. Both are defined
against WHERE YOU ARE STANDING, so the detector always reported that the dock
faces exactly where the robot happens to be. Measured on one fixed cradle from
five positions:

    view offset   0     10     20     30     40  deg
    yaw error   -0.0  -10.0  -20.0  -30.0  -40.0  deg

The yaw error equals the viewing offset exactly, and the target slid along the
arc with it, from (0.400, 0.000) to (0.306, -0.257). Nothing in the log looked
wrong. It surfaced because the goal moved between missions while the cradle had
not been touched.

A CIRCLE has no axis. Only the physical ARC does, and it is the bisector of the
two endpoint radii. fitCircle now reports mid_angle, and the pose comes from
that: target = C + R*(cos mid, sin mid), yaw = mid + pi. Same five positions:

    yaw error   -0.2   -0.2   -0.3   -2.4   -3.9  deg

I had written the old definition up as a feature - stable when part of the arc
is hidden. It was stable, at the wrong value, by not looking at the shape.

The residual at 30-40 degrees is real and has to be gated, not hidden: an
occluded edge moves the observed midpoint, and the measured relationship is
yaw error ~= (true span - observed span) / 2. So arc_span is now checked on
BOTH sides with its own arc_span_tolerance, rather than being an upper bound
borrowing slack from arc_radius_tolerance. Seeing too little of the arc is as
dangerous as seeing too much, and only one of those was being caught.

This reverses the earlier reasoning that copied the V's asymmetric wing rule.
The analogy does not hold: a V's apex and bisector survive one wing being
short because they are intrinsic, and an arc's axis does not exist at all
without both edges.

ThePoseFollowsTheDockNotTheRobot pins it, and pins it by rotating the DOCK, not
the world. The first draft rotated the whole scene about the sensor, which
leaves the sensor-to-dock relationship unchanged and passes under the old
definition too - it guarded nothing. Rotating the dock in place fails the old
code by 20.05 degrees.

RefusesAnArcWithAnEdgeHiddenBecauseTheAxisWouldBeWrong replaces a test that
asserted the opposite. That test was pinning the bug.
This commit is contained in:
2026-08-26 21:09:26 +07:00
parent 791c838da8
commit 00e5f3e2cb
13 changed files with 19740 additions and 53 deletions

View File

@@ -187,14 +187,17 @@ Detection ArcPatternMatcher::match(const std::vector<Line>& lines) const
continue;
}
// Góc mở quan sát được: sàn là cổng SỐ HỌC (dưới đó bán kính khớp bắt đầu lệch nhanh), trần là
// cổng HÌNH HỌC (cung dài hơn cả cái máng thì là mặt cong khác).
// Sàn số học: dưới ngưỡng này bán kính khớp bắt đầu lệch nhanh, bất kể máng to nhỏ.
if (fit.span < profile_.arc_min_span)
{
++rejected_span;
continue;
}
if (fit.span > profile_.arc_span + profile_.arc_radius_tolerance / std::max(fit.radius, 1e-6))
// Cổng góc mở, HAI PHÍA. Thừa cung = đang nhìn mặt cong khác. Thiếu cung = mép bị che, và khi
// ấy trung điểm cung dịch đi — mà trục của máng lấy từ chính trung điểm đó. Đo được: góc mở
// hụt δ thì hướng sai khoảng δ/2, nên bỏ cổng dưới là để trục sai đi qua trong im lặng.
if (std::fabs(fit.span - profile_.arc_span) > profile_.arc_span_tolerance)
{
++rejected_span;
continue;
@@ -224,14 +227,22 @@ Detection ArcPatternMatcher::match(const std::vector<Line>& lines) const
continue;
}
// Điểm sâu nhất: trên đường tròn, dọc hướng cảm biến -> tâm, ra xa thêm một bán kính. Chỉ phụ
// thuộc tâm và bán kính nên không đổi khi một phần cung bị che.
const double scale = 1.0 + fit.radius / center_range;
const double apex_x = fit.center_x * scale;
const double apex_y = fit.center_y * scale;
// Trục của MÁNG, lấy từ trung điểm cung — phân giác hai bán kính tới hai đầu mút.
//
// Trước đây chỗ này lấy điểm trên đường tròn XA CẢM BIẾN NHẤT, và hướng từ đó về phía cảm biến.
// Cả hai định nghĩa theo VỊ TRÍ NGƯỜI QUAN SÁT, nên bộ dò luôn báo rằng cái máng quay mặt đúng
// vào chỗ robot đang đứng. Đo được: đứng lệch 10/20/30/40 độ thì yaw sai đúng 10/20/30/40 độ,
// và điểm đích trượt dọc cung theo. Nó ổn định thật — nhưng ổn định ở GIÁ TRỊ SAI.
//
// Một ĐƯỜNG TRÒN không có trục nào cả. Chỉ CUNG vật lý mới có, và trục đó là phân giác hai đầu
// mút. Đổi lại: che mất một bên thì trung điểm dịch theo, nên cổng `arc_span` phải kiểm HAI
// PHÍA — thấy thiếu cung cũng nguy hiểm ngang thấy thừa.
const double apex_x = fit.center_x + fit.radius * std::cos(fit.mid_angle);
const double apex_y = fit.center_y + fit.radius * std::sin(fit.mid_angle);
// Hướng từ điểm sâu nhất về phía cảm biến, cùng quy ước với chữ V.
const double yaw = std::atan2(-fit.center_y, -fit.center_x);
// Hướng RA phía miệng máng: ngược với hướng tâm -> mặt cong. Cùng quy ước với chữ V.
const double yaw =
std::atan2(std::sin(fit.mid_angle + M_PI), std::cos(fit.mid_angle + M_PI));
if (profile_.has_prior)
{
@@ -298,10 +309,11 @@ Detection ArcPatternMatcher::match(const std::vector<Line>& lines) const
if (have_near)
{
std::snprintf(detail, sizeof(detail),
" (nearest fit: radius %.3f m vs %.3f+-%.3f, span %.3f rad (min %.3f, max"
" %.3f), rms %.4f m vs max %.4f)",
" (nearest fit: radius %.3f m vs %.3f+-%.3f, span %.3f rad vs %.3f+-%.3f"
" (floor %.3f), rms %.4f m vs max %.4f)",
near_radius, profile_.arc_radius, profile_.arc_radius_tolerance, near_span,
profile_.arc_min_span, profile_.arc_span, near_rms, profile_.arc_max_rms);
profile_.arc_span, profile_.arc_span_tolerance, profile_.arc_min_span,
near_rms, profile_.arc_max_rms);
}
if (rejected_prior > 0)
@@ -330,7 +342,8 @@ Detection ArcPatternMatcher::match(const std::vector<Line>& lines) const
else if (rejected_span > 0)
{
best.reason = std::to_string(rejected_span) +
" arc(s) fitted cleanly but span too little or too much of a circle";
" arc(s) fitted cleanly but span the wrong amount of circle — too little means"
" an edge is hidden and the axis would be wrong, too much means another surface";
best.reason += detail;
}
else if (rejected_rms > 0)

View File

@@ -188,6 +188,19 @@ CircleFitResult fitCircle(const std::vector<double>& xs, const std::vector<doubl
out.rms_error = std::sqrt(sq_sum / static_cast<double>(n));
out.span = angle_max - angle_min;
// Trung điểm cung, quy về (-pi, pi]. `angle_min`/`angle_max` nằm trong hệ đã cộng dồn neo tại
// góc thật của điểm đầu, nên trung điểm của chúng cũng là một góc thật, chỉ lệch bội của 2*pi.
double mid = 0.5 * (angle_min + angle_max);
while (mid > M_PI)
{
mid -= 2.0 * M_PI;
}
while (mid <= -M_PI)
{
mid += 2.0 * M_PI;
}
out.mid_angle = mid;
out.valid = true;
return out;
}

View File

@@ -142,6 +142,13 @@ bool DockProfile::validate(std::string& error) const
error = "arc_span must be finite, > 0 and <= 2*pi rad";
return false;
}
if (!positive(arc_span_tolerance) || arc_span_tolerance >= arc_span)
{
// Dung sai >= chính góc mở nghĩa là chấp nhận cả cung dài 0 — cổng vô nghĩa, mà cổng này là
// thứ duy nhất chặn trục sai.
error = "arc_span_tolerance must be finite, > 0 and < arc_span";
return false;
}
if (!positive(arc_min_span) || arc_min_span > arc_span)
{
// Sàn cao hơn cả góc mở toàn phần thì không cung nào lọt được, kể cả cái máng thật.