fix: seed the weighted fit with total least squares, not two endpoints

The iterative weighted fit started from the line through the first and last
supporting point. That is a Newton iteration on a non-convex objective started
from a poor guess, and measured on a V dock plate at 1.7-2.5 m with 3 mm range
noise, 22-24% of all segments spent the whole 50-iteration budget without
converging.

Seeding from a closed-form total-least-squares fit over every supporting point
takes that to 0%: from the answer, the iteration converges in a step or two.

The seed also decides what a segment keeps when the iteration does give up, and
two endpoints are a poor thing to fall back on - the direction of the whole
segment then rests on the range noise of exactly two rays, several times the
angular error of a seventeen-point fit, and on a V that error lands straight on
the apex because the apex is where the two wings intersect.

Measured over 400 scans per case:

                       endpoint seed    TLS seed
  fits converged         76-78%          100%
  apex yaw sd @1.7 m     0.0116 rad      0.0055 rad
  apex y sd   @1.7 m     0.0084 m        0.0040 m
  apex yaw sd @2.5 m     0.0163 rad      0.0087 rad
  apex y sd   @2.5 m     0.0114 m        0.0061 m

Detection rate stays 100%. This matters end to end: the docking action needs
five detections agreeing within 0.030 m and 0.050 rad, and on the robot the
spread was sitting at 0.031-0.039 m, just over the line, so a run took 11.9 s
to find a window that happened to fall inside instead of converging at once.

TotalLeastSquaresIsAnchoredByEveryPointNotJustTheTwoEnds pins the property that
makes the seed worth having, by comparing the two fits directly on a wall whose
two end rays are pushed apart while the nineteen between them stay clean.
This commit is contained in:
2026-08-26 16:03:36 +07:00
parent d8f11d2df6
commit fc4c5a9a12
3 changed files with 127 additions and 0 deletions

View File

@@ -305,6 +305,51 @@ TEST(LineExtractor, TheLeastSquaresLoopIsBounded)
SUCCEED();
}
TEST(Line, TotalLeastSquaresIsAnchoredByEveryPointNotJustTheTwoEnds)
{
// Mầm của phép tinh chỉnh quyết định hai chuyện: nó có hội tụ không, và còn lại gì nếu không.
// Mầm qua hai đầu mút để hướng của cả đoạn tựa lên đúng HAI tia, nên nhiễu tầm của hai tia đó
// vào thẳng góc của đoạn — rồi vào thẳng đỉnh chữ V, vì đỉnh là giao của hai cánh.
//
// Dựng đúng tình huống phân biệt hai cách: tường phẳng, 19 điểm giữa sạch, chỉ hai đầu mút bị
// đẩy ngược chiều nhau. Phản đối xứng nên nó XOAY đoạn chứ không tịnh tiến. Khớp hai đầu mút
// nghiêng theo trọn vẹn; khớp đủ điểm bị 19 điểm giữa neo lại.
const ScanGeometry g = geometry(21, -0.3, 0.3);
std::vector<double> ranges = wallScan(g, 2.0);
ranges.front() += 0.03; // [m] đẩy ra
ranges.back() -= 0.03; // [m] kéo vào
robot_line_extraction::CachedData c_data;
robot_line_extraction::RangeData r_data;
std::vector<unsigned int> indices;
for (std::size_t i = 0; i < g.size; ++i)
{
const double bearing = bearingOf(g, i);
c_data.bearings.push_back(bearing);
c_data.cos_bearings.push_back(std::cos(bearing));
c_data.sin_bearings.push_back(std::sin(bearing));
c_data.indices.push_back(static_cast<unsigned int>(i));
r_data.ranges.push_back(ranges[i]);
r_data.xs.push_back(ranges[i] * std::cos(bearing));
r_data.ys.push_back(ranges[i] * std::sin(bearing));
indices.push_back(static_cast<unsigned int>(i));
}
Line line(c_data, r_data, tightParams(), indices);
line.endpointFit();
const double endpoint_angle = line.getAngle();
line.totalLeastSqFit();
const double tls_angle = line.getAngle();
// Pháp tuyến thật của tường ở 0. Hai đầu bị đẩy kéo khớp hai đầu mút đi hẳn; khớp đủ điểm chịu
// chưa tới một phần ba mức đó.
EXPECT_GT(std::fabs(endpoint_angle), 0.040);
EXPECT_LT(std::fabs(tls_angle), 0.020);
EXPECT_LT(std::fabs(tls_angle), std::fabs(endpoint_angle) / 3.0);
}
TEST(LineExtractor, ALineThatFailedToRefineIsStillReturned)
{
// Không hội tụ KHÔNG phải lý do vứt đoạn. Đây là bản vá cho một hồi quy có thật: bản trước bỏ