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.
209 lines
6.2 KiB
C++
209 lines
6.2 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* dock_pattern_detector — khớp đường tròn Taubin.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <dock_pattern_detector/circle_fit.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
namespace dock_pattern_detector
|
|
{
|
|
namespace
|
|
{
|
|
|
|
/**
|
|
* @brief Trần lặp Newton khi tìm nghiệm nhỏ nhất của đa thức đặc trưng.
|
|
*
|
|
* Newton ở đây xuất phát từ 0 và hội tụ trong vài vòng với dữ liệu lành. Trần này là để một chuỗi
|
|
* điểm suy biến không treo control loop — cùng lý do với trần lặp trong `robot_line_extraction`.
|
|
*/
|
|
constexpr int kMaxNewtonIterations = 30;
|
|
|
|
/// [m] Dưới ngưỡng này thì tán xạ của chuỗi điểm coi như bằng 0: mọi điểm chồng lên nhau.
|
|
constexpr double kMinScatter = 1e-12;
|
|
|
|
} // namespace
|
|
|
|
CircleFitResult fitCircle(const std::vector<double>& xs, const std::vector<double>& ys)
|
|
{
|
|
CircleFitResult out;
|
|
|
|
const std::size_t n = std::min(xs.size(), ys.size());
|
|
if (n < 3)
|
|
{
|
|
return out; // Dưới ba điểm thì không có đường tròn nào để nói tới.
|
|
}
|
|
|
|
// Dời về trọng tâm trước khi làm gì khác. Toạ độ trong frame cảm biến thường quanh vài mét, còn
|
|
// bán kính dock quanh vài chục centimet; khớp thẳng trên số gốc là bình phương một đại lượng lớn
|
|
// rồi trừ đi nhau, mất hết chữ số có nghĩa.
|
|
double mean_x = 0.0;
|
|
double mean_y = 0.0;
|
|
for (std::size_t i = 0; i < n; ++i)
|
|
{
|
|
mean_x += xs[i];
|
|
mean_y += ys[i];
|
|
}
|
|
mean_x /= static_cast<double>(n);
|
|
mean_y /= static_cast<double>(n);
|
|
|
|
double m_xx = 0.0;
|
|
double m_yy = 0.0;
|
|
double m_xy = 0.0;
|
|
double m_xz = 0.0;
|
|
double m_yz = 0.0;
|
|
double m_zz = 0.0;
|
|
double m_z = 0.0;
|
|
|
|
for (std::size_t i = 0; i < n; ++i)
|
|
{
|
|
const double u = xs[i] - mean_x;
|
|
const double v = ys[i] - mean_y;
|
|
const double z = u * u + v * v;
|
|
m_xx += u * u;
|
|
m_yy += v * v;
|
|
m_xy += u * v;
|
|
m_xz += u * z;
|
|
m_yz += v * z;
|
|
m_zz += z * z;
|
|
m_z += z;
|
|
}
|
|
|
|
const double inv_n = 1.0 / static_cast<double>(n);
|
|
m_xx *= inv_n;
|
|
m_yy *= inv_n;
|
|
m_xy *= inv_n;
|
|
m_xz *= inv_n;
|
|
m_yz *= inv_n;
|
|
m_zz *= inv_n;
|
|
m_z *= inv_n;
|
|
|
|
if (m_xx + m_yy < kMinScatter)
|
|
{
|
|
return out; // Mọi điểm chồng lên nhau. Không có hướng, càng không có đường tròn.
|
|
}
|
|
|
|
// Nghiệm nhỏ nhất của đa thức đặc trưng Taubin, tìm bằng Newton từ 0.
|
|
const double cov_xy = m_xx * m_yy - m_xy * m_xy;
|
|
const double var_z = m_zz - m_z * m_z;
|
|
|
|
const double a3 = 4.0 * m_z;
|
|
const double a2 = -3.0 * m_z * m_z - m_zz;
|
|
const double a1 = var_z * m_z + 4.0 * cov_xy * m_z - m_xz * m_xz - m_yz * m_yz;
|
|
const double a0 = m_xz * (m_xz * m_yy - m_yz * m_xy) + m_yz * (m_yz * m_xx - m_xz * m_xy) -
|
|
var_z * cov_xy;
|
|
const double a22 = a2 + a2;
|
|
const double a33 = a3 + a3 + a3;
|
|
|
|
double x = 0.0;
|
|
double y = a0;
|
|
for (int iter = 0; iter < kMaxNewtonIterations; ++iter)
|
|
{
|
|
const double dy = a1 + x * (a22 + a33 * x);
|
|
if (std::fabs(dy) < 1e-18)
|
|
{
|
|
break; // Đạo hàm phẳng: không có bước Newton nào có nghĩa, dùng nghiệm đang có.
|
|
}
|
|
const double x_new = x - y / dy;
|
|
if (!std::isfinite(x_new) || x_new == x)
|
|
{
|
|
break;
|
|
}
|
|
const double y_new = std::fabs(a0 + x_new * (a1 + x_new * (a2 + x_new * a3)));
|
|
if (y_new >= std::fabs(y))
|
|
{
|
|
break; // Không cải thiện nữa. Nhận nghiệm đang có thay vì đi lang thang.
|
|
}
|
|
x = x_new;
|
|
y = y_new;
|
|
}
|
|
|
|
const double det = x * x - x * m_z + cov_xy;
|
|
if (!std::isfinite(det) || std::fabs(det) < 1e-15)
|
|
{
|
|
// Định thức về 0 khi chuỗi điểm gần như THẲNG HÀNG: đường tròn khớp nhất là đường thẳng, tức
|
|
// tâm ở vô cùng. Trả về không hợp lệ thay vì một tâm khổng lồ mà người gọi tưởng là thật.
|
|
return out;
|
|
}
|
|
|
|
const double center_u = (m_xz * (m_yy - x) - m_yz * m_xy) / det / 2.0;
|
|
const double center_v = (m_yz * (m_xx - x) - m_xz * m_xy) / det / 2.0;
|
|
const double radius = std::sqrt(center_u * center_u + center_v * center_v + m_z);
|
|
|
|
if (!std::isfinite(center_u) || !std::isfinite(center_v) || !std::isfinite(radius) ||
|
|
radius <= 0.0)
|
|
{
|
|
return out;
|
|
}
|
|
|
|
out.center_x = center_u + mean_x;
|
|
out.center_y = center_v + mean_y;
|
|
out.radius = radius;
|
|
out.point_count = n;
|
|
|
|
// Sai số khớp và góc mở, đo lại trên chính đường tròn vừa tìm được.
|
|
double sq_sum = 0.0;
|
|
double angle_min = 0.0;
|
|
double angle_max = 0.0;
|
|
double prev_angle = 0.0;
|
|
double unwrapped = 0.0;
|
|
|
|
for (std::size_t i = 0; i < n; ++i)
|
|
{
|
|
const double dx = xs[i] - out.center_x;
|
|
const double dy = ys[i] - out.center_y;
|
|
const double d = std::hypot(dx, dy) - radius;
|
|
sq_sum += d * d;
|
|
|
|
// Góc mở phải cộng dồn theo chuỗi rồi mới lấy biên, không lấy min/max của atan2 thô: một cung
|
|
// vắt qua +-pi sẽ cho ra góc mở gần 2*pi trong khi nó chỉ vài độ.
|
|
const double angle = std::atan2(dy, dx);
|
|
if (i == 0)
|
|
{
|
|
prev_angle = angle;
|
|
unwrapped = angle;
|
|
angle_min = angle;
|
|
angle_max = angle;
|
|
continue;
|
|
}
|
|
double step = angle - prev_angle;
|
|
while (step > M_PI)
|
|
{
|
|
step -= 2.0 * M_PI;
|
|
}
|
|
while (step < -M_PI)
|
|
{
|
|
step += 2.0 * M_PI;
|
|
}
|
|
unwrapped += step;
|
|
prev_angle = angle;
|
|
angle_min = std::min(angle_min, unwrapped);
|
|
angle_max = std::max(angle_max, unwrapped);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} // namespace dock_pattern_detector
|