fix(leg_pose_fit): estimate the taped range bias when every leg is taped, gated by its per-leg posterior std

This commit is contained in:
2026-10-30 10:58:55 +07:00
parent 588ed58b6d
commit a6c09f2386
11 changed files with 456 additions and 21 deletions

View File

@@ -943,6 +943,8 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
noise.range_sigma = limits.range_sigma;
noise.angle_increment = dtheta;
noise.bright_range_bias_sigma = profile_.tape_enabled ? profile_.tape_range_bias_sigma : 0.0;
noise.bright_range_bias_all_max_std = profile_.tape_enabled && profile_.tape_legs == TapeLegs::kAll
? profile_.tape_range_bias_all_max_std : 0.0;
// Xe cùng loại vẫn lệch vài cm do gia công: một cạnh lệch tối đa `cart_dim_tolerance` đẩy mỗi
// chân lệch tối đa tolerance/2 khỏi góc của mô hình. Coi mức đó là 1σ của mô hình, để điểm tin
// không trừ điểm xe nằm trong dung sai mà vẫn trừ điểm xe lệch xa hơn.
@@ -988,6 +990,10 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
break;
}
}
// Preserve the attempted all-bright uncertainty even if a later geometric gate rejects this
// hypothesis. It is diagnostic only and does not affect ranking or the legacy detection.
h.detail.tape_range_bias_std = fit.bright_range_bias_std;
h.detail.tape_range_bias_all_bright = fit.bright_range_bias_all_bright;
int seen = 0;
int partial = 0;
@@ -1204,6 +1210,8 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
d.fit_iterations = fit.iterations;
d.tape_range_bias = fit.bright_range_bias;
d.tape_range_bias_estimated = fit.bright_range_bias_estimated;
d.tape_range_bias_std = fit.bright_range_bias_std;
d.tape_range_bias_all_bright = fit.bright_range_bias_all_bright;
// Đích = tâm dịch theo offset, trong hệ của chính xe: x dọc pháp tuyến cạnh vào hướng ra phía
// robot, y sang trái — cùng công thức V/arc.
@@ -1365,6 +1373,8 @@ LegDetection LegPatternMatcher::match(const robot_line_extraction::ScanGeometry&
if (h.stage == furthest)
{
detail = h.failure;
out.tape_range_bias_std = h.detail.tape_range_bias_std;
out.tape_range_bias_all_bright = h.detail.tape_range_bias_all_bright;
break;
}
}

View File

@@ -594,6 +594,51 @@ PoseFitResult fitCartPose4D(const LegModel& model, const std::vector<FitPoint>&
}
return result;
}
// Posterior covariance at the final state. Ranges from one leg share an offset, so they must not
// count as independent evidence: divide each point's Huber/variance weight by its corner's point
// count. This changes only the uncertainty gate, not the solver's weights or fitted state.
// No numerical damping: adding it here would falsely improve observability.
double biasPosteriorStd(const LegModel& model, const std::vector<FitPoint>& points,
const PoseFitResult& fit, const FitNoise& noise)
{
const Eigen::Vector4d state(fit.pose.x, fit.pose.y, fit.pose.yaw, fit.bright_range_bias);
Eigen::Matrix4d normal = Eigen::Matrix4d::Zero();
std::array<std::size_t, kLegCount> corner_counts{};
for (const FitPoint& point : points)
{
if (point.corner < 0 || point.corner >= kLegCount)
{
return std::numeric_limits<double>::infinity();
}
++corner_counts[static_cast<std::size_t>(point.corner)];
}
for (const FitPoint& point : points)
{
const PointTerm4 term = evaluateWithBias(model, point, state, noise);
const double error = std::fabs(term.r) / term.sigma;
const double huber = error <= kHuberK ? 1.0 : kHuberK / error;
const double count = static_cast<double>(corner_counts[static_cast<std::size_t>(point.corner)]);
normal += (huber / (count * term.sigma * term.sigma)) * term.jacobian * term.jacobian.transpose();
}
normal(3, 3) += 1.0 / (noise.bright_range_bias_sigma * noise.bright_range_bias_sigma);
if (!normal.allFinite())
{
return std::numeric_limits<double>::infinity();
}
Eigen::LDLT<Eigen::Matrix4d> ldlt(normal);
if (ldlt.info() != Eigen::Success || !ldlt.isPositive() ||
(ldlt.vectorD().array() <= 0.0).any())
{
return std::numeric_limits<double>::infinity();
}
const Eigen::Vector4d column = ldlt.solve(Eigen::Vector4d::Unit(3));
if (ldlt.info() != Eigen::Success || !column.allFinite() || column[3] <= 0.0)
{
return std::numeric_limits<double>::infinity();
}
return std::sqrt(column[3]);
}
} // namespace
PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& points,
@@ -624,6 +669,22 @@ PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& po
{
return fitCartPose4D(model, points, initial, noise, max_iterations);
}
if (noise.bright_range_bias_sigma > 0.0 && has_bright_corner && !has_bare_corner &&
noise.bright_range_bias_all_max_std > 0.0)
{
PoseFitResult candidate = fitCartPose4D(model, points, initial, noise, max_iterations);
const double std = candidate.valid ? biasPosteriorStd(model, points, candidate, noise)
: std::numeric_limits<double>::infinity();
if (std::isfinite(std) && std <= noise.bright_range_bias_all_max_std)
{
candidate.bright_range_bias_std = std;
candidate.bright_range_bias_all_bright = true;
return candidate;
}
PoseFitResult legacy = fitCartPose3D(model, points, initial, noise, max_iterations);
legacy.bright_range_bias_std = std;
return legacy;
}
return fitCartPose3D(model, points, initial, noise, max_iterations);
}

View File

@@ -200,6 +200,18 @@ bool LegProfile::validate(std::string& error) const
error = "tape_range_bias_sigma must be finite and in [0, 0.2] [m]";
return false;
}
if (!std::isfinite(tape_range_bias_all_max_std) || tape_range_bias_all_max_std < 0.0 ||
tape_range_bias_all_max_std > 0.1)
{
error = "tape_range_bias_all_max_std must be finite and in [0, 0.1] [m]";
return false;
}
if (tape_range_bias_all_max_std > 0.0 && (!tape_enabled || tape_range_bias_sigma <= 0.0))
{
error = "non-zero tape_range_bias_all_max_std requires tape_enabled: true and "
"tape_range_bias_sigma > 0";
return false;
}
if (!std::isfinite(tape_plateau_min_fraction) || tape_plateau_min_fraction < 0.0 ||
tape_plateau_min_fraction >= 1.0)
{