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:
69
src/line.cpp
69
src/line.cpp
@@ -133,6 +133,57 @@ void Line::radiusFromEndpoints()
|
||||
}
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Total least squares — closed form, seeds the weighted fit
|
||||
// ================================================================================================
|
||||
|
||||
void Line::totalLeastSqFit()
|
||||
{
|
||||
const std::size_t n = indices_.size();
|
||||
if (n < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
double mean_x = 0.0;
|
||||
double mean_y = 0.0;
|
||||
for (const unsigned int index : indices_)
|
||||
{
|
||||
mean_x += r_data_.xs[index];
|
||||
mean_y += r_data_.ys[index];
|
||||
}
|
||||
mean_x /= static_cast<double>(n);
|
||||
mean_y /= static_cast<double>(n);
|
||||
|
||||
double s_xx = 0.0;
|
||||
double s_xy = 0.0;
|
||||
double s_yy = 0.0;
|
||||
for (const unsigned int index : indices_)
|
||||
{
|
||||
const double dx = r_data_.xs[index] - mean_x;
|
||||
const double dy = r_data_.ys[index] - mean_y;
|
||||
s_xx += dx * dx;
|
||||
s_xy += dx * dy;
|
||||
s_yy += dy * dy;
|
||||
}
|
||||
|
||||
if (s_xx + s_yy < 1e-18)
|
||||
{
|
||||
// Every point on top of every other. No direction to speak of; leave whatever fit is in place.
|
||||
return;
|
||||
}
|
||||
|
||||
// Principal axis of the scatter, then the normal a quarter turn off it. The half-angle form is
|
||||
// exact for a symmetric 2x2 and needs no eigen decomposition.
|
||||
angle_ = piToPi(0.5 * std::atan2(2.0 * s_xy, s_xx - s_yy) + M_PI / 2.0);
|
||||
radius_ = mean_x * std::cos(angle_) + mean_y * std::sin(angle_);
|
||||
if (radius_ < 0.0)
|
||||
{
|
||||
radius_ = -radius_;
|
||||
angle_ = piToPi(angle_ + M_PI);
|
||||
}
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Weighted least squares
|
||||
// ================================================================================================
|
||||
@@ -141,6 +192,18 @@ bool Line::leastSqFit()
|
||||
{
|
||||
calcPointCovariances();
|
||||
|
||||
// Seed from the closed-form fit over all points rather than from the two endpoints. The iteration
|
||||
// is Newton on a non-convex objective: from a poor start it can wander or oscillate and spend the
|
||||
// whole iteration budget, and measured on a V dock plate at 1.7-2.5 m roughly a quarter of all
|
||||
// segments did exactly that. Starting at the answer, it converges in a step or two.
|
||||
//
|
||||
// Keeping the seed also decides what a failed fit falls back to, and the two-endpoint fit is a
|
||||
// poor thing to fall back to: its direction rests on two rays, so it carries several times the
|
||||
// angular noise of a seventeen-point fit, and that noise lands straight on the apex of the V.
|
||||
totalLeastSqFit();
|
||||
const double seed_angle = angle_;
|
||||
const double seed_radius = radius_;
|
||||
|
||||
double prev_radius = 0.0;
|
||||
double prev_angle = 0.0;
|
||||
int iterations = 0;
|
||||
@@ -150,6 +213,9 @@ bool Line::leastSqFit()
|
||||
{
|
||||
if (++iterations > kMaxLeastSqIterations)
|
||||
{
|
||||
angle_ = seed_angle;
|
||||
radius_ = seed_radius;
|
||||
projectEndpoints();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -163,6 +229,9 @@ bool Line::leastSqFit()
|
||||
{
|
||||
// A degenerate run can drive the increment to infinity. Carrying that into the covariance
|
||||
// would poison the merge step, which compares lines through their covariances.
|
||||
angle_ = seed_angle;
|
||||
radius_ = seed_radius;
|
||||
projectEndpoints();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user