fix(leg_pattern): segment mirror-taped legs by intensity and estimate their range bias
Mirror tape on the KIT lidar blooms: a taped leg returns a saturated core plus a skirt of dim rays whose range climbs smoothly to the background. Clustering by geometry first made taped legs attached (no depth jump at the edge) or too wide for the width gate, so the robot found the cart in 6 of 227 scans at 1.2 m. The saturated core also reads 1-3 cm short. All changes sit behind tape_enabled plus their own key; with the keys at 0 or tape off the detector runs exactly as before. - tape_bloom_max_angle [rad]: bright runs become leg candidates before geometric clustering. The bloom skirt is walked and masked, and the edge class comes from the walk. A bright core spread over more than the leg's apparent width in range is rejected: it is a leg corrupted by a nearer mirror's bloom. - tape_range_bias_sigma [m]: the pose fit estimates one common range bias for bright points (x, y, yaw, b) with a Gaussian prior. This only happens when at least one bare leg anchors the range; otherwise the 3-DoF fit is unchanged. |b| > 0.1 m invalidates the fit. Pair spacing and measured dimensions use bias-corrected leg centres. - Real KIT scans with intensities: 9 from the 2-leg-taped run (11:06) and 2 with all four legs taped (12:12).
This commit is contained in:
@@ -224,6 +224,7 @@ int LegPatternMatcher::classifyEdge(std::size_t edge, int step) const
|
||||
}
|
||||
if (nb.shadow)
|
||||
{
|
||||
// Không bỏ tape_filtered: chân trần cạnh bloom vẫn phải so range bình thường.
|
||||
return 0; // còn kéo đuôi sau khi đã bỏ qua tối đa: một chuỗi điểm lai dẫn ra nền phía sau
|
||||
}
|
||||
if (nb.range >= r_edge + profile_.leg_min_depth_gap)
|
||||
@@ -239,10 +240,18 @@ int LegPatternMatcher::classifyEdge(std::size_t edge, int step) const
|
||||
|
||||
void LegPatternMatcher::processCluster(double dtheta)
|
||||
{
|
||||
++clusters_;
|
||||
if (cluster_.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
processPoints(cluster_, classifyEdge(cluster_.front(), -1), classifyEdge(cluster_.back(), +1),
|
||||
dtheta);
|
||||
}
|
||||
|
||||
const int left = classifyEdge(cluster_.front(), -1);
|
||||
const int right = classifyEdge(cluster_.back(), +1);
|
||||
void LegPatternMatcher::processPoints(const std::vector<std::size_t>& points, int left, int right,
|
||||
double dtheta)
|
||||
{
|
||||
++clusters_;
|
||||
const bool attached = (left == 2 || right == 2);
|
||||
if (attached && !profile_.accept_attached_legs)
|
||||
{
|
||||
@@ -255,7 +264,7 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
const bool partial = (left == 1 || right == 1);
|
||||
|
||||
double r_min = std::numeric_limits<double>::infinity();
|
||||
for (std::size_t idx : cluster_)
|
||||
for (std::size_t idx : points)
|
||||
{
|
||||
r_min = std::min(r_min, rays_[idx].range);
|
||||
}
|
||||
@@ -267,7 +276,7 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
leg.attached = attached;
|
||||
leg.r_min = r_min;
|
||||
const double depth_limit = r_min + profile_.maxApparentLegWidth();
|
||||
for (std::size_t idx : cluster_)
|
||||
for (std::size_t idx : points)
|
||||
{
|
||||
if (rays_[idx].range <= depth_limit)
|
||||
{
|
||||
@@ -331,6 +340,62 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
legs_.push_back(std::move(leg));
|
||||
}
|
||||
|
||||
int LegPatternMatcher::walkTapeBloom(std::size_t edge, int step,
|
||||
std::vector<std::size_t>& skirt) const
|
||||
{
|
||||
const double r0 = rays_[edge].range;
|
||||
double previous = r0;
|
||||
const double requested_steps = profile_.tape_bloom_max_angle / std::fabs(angle_increment_);
|
||||
const std::size_t max_steps = !std::isfinite(requested_steps) ||
|
||||
requested_steps >= static_cast<double>(rays_.size())
|
||||
? rays_.size()
|
||||
: static_cast<std::size_t>(requested_steps);
|
||||
long j = static_cast<long>(edge) + step;
|
||||
const long n = static_cast<long>(rays_.size());
|
||||
for (std::size_t walked = 0; walked < max_steps; ++walked)
|
||||
{
|
||||
if (j < 0 || j >= n)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const Ray& ray = rays_[j];
|
||||
if (!ray.valid)
|
||||
{
|
||||
if (zero_no_echo_ && ray.range <= 0.0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return std::isfinite(ray.range) && ray.range < min_range_ ? 1 : 0;
|
||||
}
|
||||
if (!ray.in_fan)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (ray.bright)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if (ray.range >= r0 + profile_.leg_min_depth_gap)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (ray.range <= r0 - profile_.leg_min_depth_gap)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (ray.range < previous - 0.005)
|
||||
{
|
||||
return ray.range < r0 - 0.01 ? 1 : 2;
|
||||
}
|
||||
|
||||
skirt.push_back(static_cast<std::size_t>(j));
|
||||
previous = ray.range;
|
||||
j += step;
|
||||
}
|
||||
return 2; // Hết góc cho phép khi vẫn còn bề mặt ở gần lõi: mép dính.
|
||||
}
|
||||
|
||||
void LegPatternMatcher::buildCandidates(double dtheta)
|
||||
{
|
||||
legs_.clear();
|
||||
@@ -340,8 +405,89 @@ void LegPatternMatcher::buildCandidates(double dtheta)
|
||||
rejected_width_ = 0;
|
||||
rejected_points_ = 0;
|
||||
rejected_tape_ = 0;
|
||||
rejected_bloom_spread_ = 0;
|
||||
width_miss_ = std::numeric_limits<double>::infinity();
|
||||
truncated_ = false;
|
||||
tape_run_.clear();
|
||||
left_skirt_.clear();
|
||||
right_skirt_.clear();
|
||||
|
||||
if (profile_.tape_enabled && profile_.tape_bloom_max_angle > 0.0)
|
||||
{
|
||||
const auto consumeRun = [&](const std::vector<std::size_t>& bright_run) {
|
||||
double min_range = std::numeric_limits<double>::infinity();
|
||||
double max_range = -std::numeric_limits<double>::infinity();
|
||||
for (std::size_t idx : bright_run)
|
||||
{
|
||||
min_range = std::min(min_range, rays_[idx].range);
|
||||
max_range = std::max(max_range, rays_[idx].range);
|
||||
}
|
||||
// Giới hạn vật lý: một chân không thể có độ trải cự ly lớn hơn bề rộng biểu kiến của nó.
|
||||
// Cổng này cần ngưỡng nằm trên lõi: KIT ở 8000 có độ trải lõi thật tối đa 0.041 m (5501 lõi),
|
||||
// còn ở 4000 lõi thật đã tới 0.068 m; vì vậy cấu hình KIT cần 8000. Độ trải lớn hơn thường là
|
||||
// lõi bị bloom của gương gần hơn làm méo, không phải một chân thật.
|
||||
if (max_range - min_range > profile_.maxApparentLegWidth())
|
||||
{
|
||||
for (std::size_t idx : bright_run)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
++rejected_bloom_spread_;
|
||||
return;
|
||||
}
|
||||
|
||||
if (bright_run.size() < std::max(profile_.tape_min_points, profile_.min_leg_points))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
left_skirt_.clear();
|
||||
right_skirt_.clear();
|
||||
const int left = walkTapeBloom(bright_run.front(), -1, left_skirt_);
|
||||
const int right = walkTapeBloom(bright_run.back(), +1, right_skirt_);
|
||||
for (std::size_t idx : bright_run)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
for (std::size_t idx : left_skirt_)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
for (std::size_t idx : right_skirt_)
|
||||
{
|
||||
rays_[idx].tape_filtered = true;
|
||||
}
|
||||
processPoints(bright_run, left, right, dtheta);
|
||||
};
|
||||
|
||||
for (std::size_t i = 0; i < rays_.size(); ++i)
|
||||
{
|
||||
const Ray& ray = rays_[i];
|
||||
const bool bright_in_fan = ray.valid && ray.in_fan && ray.bright;
|
||||
if (!bright_in_fan)
|
||||
{
|
||||
if (!tape_run_.empty())
|
||||
{
|
||||
consumeRun(tape_run_);
|
||||
tape_run_.clear();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!tape_run_.empty() &&
|
||||
std::fabs(ray.range - rays_[tape_run_.back()].range) > profile_.leg_cluster_gap)
|
||||
{
|
||||
consumeRun(tape_run_);
|
||||
tape_run_.clear();
|
||||
}
|
||||
tape_run_.push_back(i);
|
||||
}
|
||||
if (!tape_run_.empty())
|
||||
{
|
||||
consumeRun(tape_run_);
|
||||
tape_run_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t gap = 0; // số tia không dùng được liên tiếp kể từ điểm cuối của cụm
|
||||
for (std::size_t i = 0; i < rays_.size(); ++i)
|
||||
@@ -400,6 +546,14 @@ std::string LegPatternMatcher::describeClusterRejections() const
|
||||
{
|
||||
text += ", " + std::to_string(rejected_tape_) + " without reflective tape";
|
||||
}
|
||||
if (profile_.tape_enabled && profile_.tape_bloom_max_angle > 0.0 &&
|
||||
rejected_bloom_spread_ > 0)
|
||||
{
|
||||
text += ", " + std::to_string(rejected_bloom_spread_) +
|
||||
" bright core(s) spread over more than " +
|
||||
format("%.3f", profile_.maxApparentLegWidth()) +
|
||||
" m in range (bloom from a nearer mirror)";
|
||||
}
|
||||
if (rejected_width_ > 0 && std::isfinite(width_miss_))
|
||||
{
|
||||
text += format(" (closest width miss %.3f m; leg_width %.3f m)", width_miss_,
|
||||
@@ -743,6 +897,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
FitNoise noise;
|
||||
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;
|
||||
// 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.
|
||||
@@ -769,6 +924,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
p.x = rays_[idx].x;
|
||||
p.y = rays_[idx].y;
|
||||
p.corner = k;
|
||||
p.bright = rays_[idx].bright;
|
||||
fit_points_.push_back(p);
|
||||
}
|
||||
}
|
||||
@@ -776,7 +932,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
if (!fit.valid)
|
||||
{
|
||||
h.stage = kStageFit;
|
||||
h.failure = "pose fit diverged";
|
||||
h.failure = fit.reason.empty() ? "pose fit diverged" : fit.reason;
|
||||
return false;
|
||||
}
|
||||
const std::array<int, kLegCount> before = h.legs;
|
||||
@@ -807,6 +963,23 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tạo tâm thô đã hiệu chỉnh theo giả thuyết hiện tại; legs_ dùng chung giữa các giả thuyết nên
|
||||
// không ghi bias trở lại LegCandidate.
|
||||
const auto adjustedRough = [&](int leg_index) {
|
||||
const LegCandidate& leg = legs_[leg_index];
|
||||
std::array<double, 2> centre = { leg.rough_x, leg.rough_y };
|
||||
if (fit.bright_range_bias_estimated && leg.bright >= profile_.tape_min_points)
|
||||
{
|
||||
const double range = std::hypot(centre[0], centre[1]);
|
||||
if (range > 1e-9)
|
||||
{
|
||||
centre[0] += fit.bright_range_bias * centre[0] / range;
|
||||
centre[1] += fit.bright_range_bias * centre[1] / range;
|
||||
}
|
||||
}
|
||||
return centre;
|
||||
};
|
||||
|
||||
// --- Khoảng cách giữa các chân đã gán ------------------------------------------------------------
|
||||
// Khớp cứng luôn ra MỘT pose, kể cả khi bốn "chân" là chân của hai xe kề nhau đứng lệch vài cm so
|
||||
// với mô hình: phần dư dàn đều, điểm tin vẫn có thể qua. Kiểm thẳng từng cặp chặn ca đó, cùng
|
||||
@@ -831,7 +1004,9 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
const double tol = profile_.cart_dim_tolerance + std::max(la.r_min, lb.r_min) * dtheta +
|
||||
((la.partial || la.attached) ? profile_.leg_width / 2.0 : 0.0) +
|
||||
((lb.partial || lb.attached) ? profile_.leg_width / 2.0 : 0.0);
|
||||
const double d = std::hypot(la.rough_x - lb.rough_x, la.rough_y - lb.rough_y);
|
||||
const std::array<double, 2> ca = adjustedRough(ia);
|
||||
const std::array<double, 2> cb = adjustedRough(ib);
|
||||
const double d = std::hypot(ca[0] - cb[0], ca[1] - cb[1]);
|
||||
if (std::fabs(d - expected) > tol)
|
||||
{
|
||||
h.stage = kStageFit;
|
||||
@@ -862,6 +1037,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
int inside = 0;
|
||||
for (const Ray& r : rays_)
|
||||
{
|
||||
// Bloom vẫn là phản hồi vật lý và tham gia cổng lòng xe; chỉ bỏ shadow như cũ.
|
||||
if (!r.valid || r.shadow)
|
||||
{
|
||||
continue;
|
||||
@@ -981,6 +1157,8 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
d.legs_out_of_view = out_of_view;
|
||||
d.fit_rms = fit.rms;
|
||||
d.fit_iterations = fit.iterations;
|
||||
d.tape_range_bias = fit.bright_range_bias;
|
||||
d.tape_range_bias_estimated = fit.bright_range_bias_estimated;
|
||||
|
||||
// Đí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.
|
||||
@@ -994,9 +1172,9 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
const auto measure = [&](int a, int b, double& sum, int& count) {
|
||||
if (h.legs[a] >= 0 && h.legs[b] >= 0)
|
||||
{
|
||||
const LegCandidate& la = legs_[h.legs[a]];
|
||||
const LegCandidate& lb = legs_[h.legs[b]];
|
||||
sum += std::hypot(la.rough_x - lb.rough_x, la.rough_y - lb.rough_y);
|
||||
const std::array<double, 2> ca = adjustedRough(h.legs[a]);
|
||||
const std::array<double, 2> cb = adjustedRough(h.legs[b]);
|
||||
sum += std::hypot(ca[0] - cb[0], ca[1] - cb[1]);
|
||||
++count;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <dock_pattern_detector/leg_pose_fit.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
@@ -203,6 +204,108 @@ double robustCost(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
|
||||
FitPoint correctedPoint(const FitPoint& point, double bias)
|
||||
{
|
||||
FitPoint corrected = point;
|
||||
if (point.bright)
|
||||
{
|
||||
const double range = std::hypot(point.x, point.y);
|
||||
if (range > 1e-9)
|
||||
{
|
||||
const double scale = (range + bias) / range;
|
||||
corrected.x *= scale;
|
||||
corrected.y *= scale;
|
||||
}
|
||||
}
|
||||
return corrected;
|
||||
}
|
||||
|
||||
double residualWithBias(const LegModel& model, const FitPoint& point, double x, double y,
|
||||
double yaw, double bias, bool& on_ray)
|
||||
{
|
||||
return residual(model, correctedPoint(point, bias), x, y, yaw, on_ray);
|
||||
}
|
||||
|
||||
struct PointTerm4
|
||||
{
|
||||
double r = 0.0; // [m] phần dư
|
||||
Eigen::Vector4d jacobian; // ∂r/∂(x, y, yaw, b)
|
||||
double sigma = kMinSigma; // [m]
|
||||
};
|
||||
|
||||
PointTerm4 evaluateWithBias(const LegModel& model, const FitPoint& point,
|
||||
const Eigen::Vector4d& state, const FitNoise& noise)
|
||||
{
|
||||
PointTerm4 term;
|
||||
bool on_ray = false;
|
||||
term.r = residualWithBias(model, point, state.x(), state.y(), state.z(), state.w(), on_ray);
|
||||
const auto probe = [&](double x, double y, double yaw, double bias) {
|
||||
bool ignored = false;
|
||||
return residualWithBias(model, point, x, y, yaw, bias, ignored);
|
||||
};
|
||||
term.jacobian.x() =
|
||||
(probe(state.x() + kStepTranslation, state.y(), state.z(), state.w()) -
|
||||
probe(state.x() - kStepTranslation, state.y(), state.z(), state.w())) /
|
||||
(2.0 * kStepTranslation);
|
||||
term.jacobian.y() =
|
||||
(probe(state.x(), state.y() + kStepTranslation, state.z(), state.w()) -
|
||||
probe(state.x(), state.y() - kStepTranslation, state.z(), state.w())) /
|
||||
(2.0 * kStepTranslation);
|
||||
term.jacobian.z() =
|
||||
(probe(state.x(), state.y(), state.z() + kStepRotation, state.w()) -
|
||||
probe(state.x(), state.y(), state.z() - kStepRotation, state.w())) /
|
||||
(2.0 * kStepRotation);
|
||||
term.jacobian.w() =
|
||||
(probe(state.x(), state.y(), state.z(), state.w() + kStepTranslation) -
|
||||
probe(state.x(), state.y(), state.z(), state.w() - kStepTranslation)) /
|
||||
(2.0 * kStepTranslation);
|
||||
|
||||
const FitPoint corrected = correctedPoint(point, state.w());
|
||||
if (on_ray)
|
||||
{
|
||||
term.sigma = std::max(kMinSigma, noise.range_sigma);
|
||||
return term;
|
||||
}
|
||||
|
||||
double nx = -term.jacobian.x();
|
||||
double ny = -term.jacobian.y();
|
||||
const double nn = std::hypot(nx, ny);
|
||||
const double range = std::hypot(corrected.x, corrected.y);
|
||||
if (nn > 1e-9 && range > 1e-9)
|
||||
{
|
||||
nx /= nn;
|
||||
ny /= nn;
|
||||
const double ux = corrected.x / range;
|
||||
const double uy = corrected.y / range;
|
||||
const double along = nx * ux + ny * uy;
|
||||
const double across = -nx * uy + ny * ux;
|
||||
const double sigma_across = range * noise.angle_increment / std::sqrt(12.0);
|
||||
const double var = along * along * noise.range_sigma * noise.range_sigma +
|
||||
across * across * sigma_across * sigma_across;
|
||||
term.sigma = std::max(kMinSigma, std::sqrt(var));
|
||||
}
|
||||
else
|
||||
{
|
||||
term.sigma = std::max(kMinSigma, noise.range_sigma);
|
||||
}
|
||||
return term;
|
||||
}
|
||||
|
||||
double robustCostWithBias(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Eigen::Vector4d& state, const FitNoise& noise)
|
||||
{
|
||||
double cost = 0.0;
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
const PointTerm4 term = evaluateWithBias(model, point, state, noise);
|
||||
const double error = std::fabs(term.r) / term.sigma;
|
||||
cost += (error <= kHuberK) ? 0.5 * error * error
|
||||
: kHuberK * error - 0.5 * kHuberK * kHuberK;
|
||||
}
|
||||
const double prior = state.w() / noise.bright_range_bias_sigma;
|
||||
return cost + 0.5 * prior * prior;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
LegModel LegModel::fromProfile(const LegProfile& profile)
|
||||
@@ -306,8 +409,10 @@ bool procrustes2D(const std::vector<std::array<double, 2>>& model,
|
||||
return true;
|
||||
}
|
||||
|
||||
PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
namespace
|
||||
{
|
||||
PoseFitResult fitCartPose3D(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
{
|
||||
PoseFitResult result;
|
||||
result.pose = initial;
|
||||
@@ -394,4 +499,132 @@ PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& po
|
||||
return result;
|
||||
}
|
||||
|
||||
PoseFitResult fitCartPose4D(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
{
|
||||
PoseFitResult result;
|
||||
result.pose = initial;
|
||||
result.bright_range_bias_estimated = true;
|
||||
if (points.size() < 3)
|
||||
{
|
||||
result.reason = "4-DoF pose fit requires at least three points";
|
||||
return result;
|
||||
}
|
||||
|
||||
Eigen::Vector4d state(initial.x, initial.y, initial.yaw, 0.0);
|
||||
for (int iter = 0; iter < max_iterations; ++iter)
|
||||
{
|
||||
Eigen::Matrix4d normal = Eigen::Matrix4d::Zero();
|
||||
Eigen::Vector4d rhs = Eigen::Vector4d::Zero();
|
||||
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 weight = huber / (term.sigma * term.sigma);
|
||||
normal += weight * term.jacobian * term.jacobian.transpose();
|
||||
rhs -= weight * term.jacobian * term.r;
|
||||
}
|
||||
|
||||
const double prior_jacobian = 1.0 / noise.bright_range_bias_sigma;
|
||||
normal(3, 3) += prior_jacobian * prior_jacobian;
|
||||
rhs(3) -= state.w() * prior_jacobian * prior_jacobian;
|
||||
normal += 1e-9 * (1.0 + normal.trace()) * Eigen::Matrix4d::Identity();
|
||||
Eigen::Vector4d step = normal.ldlt().solve(rhs);
|
||||
if (!step.allFinite())
|
||||
{
|
||||
result.reason = "4-DoF pose fit diverged (non-finite step)";
|
||||
return result;
|
||||
}
|
||||
|
||||
const double cost_now = robustCostWithBias(model, points, state, noise);
|
||||
bool accepted = false;
|
||||
for (int halving = 0; halving <= kMaxHalvings; ++halving)
|
||||
{
|
||||
Eigen::Vector4d candidate = state + step;
|
||||
candidate.z() = wrapAngle(candidate.z());
|
||||
if (robustCostWithBias(model, points, candidate, noise) < cost_now)
|
||||
{
|
||||
state = candidate;
|
||||
accepted = true;
|
||||
break;
|
||||
}
|
||||
step *= 0.5;
|
||||
}
|
||||
result.iterations = iter + 1;
|
||||
if (!accepted)
|
||||
{
|
||||
result.converged = true;
|
||||
break;
|
||||
}
|
||||
if (std::hypot(step.x(), step.y()) < kStopTranslation &&
|
||||
std::fabs(step.z()) < kStopRotation && std::fabs(step.w()) < kStopTranslation)
|
||||
{
|
||||
result.converged = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const double model_var = noise.model_sigma > 0.0 ? noise.model_sigma * noise.model_sigma : 0.0;
|
||||
double sum_r2 = 0.0;
|
||||
double sum_e2 = 0.0;
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
const PointTerm4 term = evaluateWithBias(model, point, state, noise);
|
||||
sum_r2 += term.r * term.r;
|
||||
sum_e2 += (term.r * term.r) / (term.sigma * term.sigma + model_var);
|
||||
}
|
||||
const double count = static_cast<double>(points.size());
|
||||
result.rms = std::sqrt(sum_r2 / count);
|
||||
result.normalized_rms = std::sqrt(sum_e2 / count);
|
||||
result.pose.x = state.x();
|
||||
result.pose.y = state.y();
|
||||
result.pose.yaw = state.z();
|
||||
result.bright_range_bias = state.w();
|
||||
if (std::fabs(state.w()) > 0.1)
|
||||
{
|
||||
result.reason = "estimated bright range bias " + std::to_string(state.w()) +
|
||||
" m exceeds the 0.1 m safety limit";
|
||||
return result;
|
||||
}
|
||||
result.valid = state.allFinite() && std::isfinite(result.rms);
|
||||
if (!result.valid)
|
||||
{
|
||||
result.reason = "4-DoF pose fit produced a non-finite result";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
PoseFitResult fitCartPose(const LegModel& model, const std::vector<FitPoint>& points,
|
||||
const Pose2D& initial, const FitNoise& noise, int max_iterations)
|
||||
{
|
||||
std::array<bool, kLegCount> corner_seen = { { false, false, false, false } };
|
||||
std::array<bool, kLegCount> corner_has_bright = { { false, false, false, false } };
|
||||
std::array<bool, kLegCount> corner_all_non_bright = { { true, true, true, true } };
|
||||
for (const FitPoint& point : points)
|
||||
{
|
||||
if (point.corner < 0 || point.corner >= kLegCount)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const std::size_t corner = static_cast<std::size_t>(point.corner);
|
||||
corner_seen[corner] = true;
|
||||
corner_has_bright[corner] = corner_has_bright[corner] || point.bright;
|
||||
corner_all_non_bright[corner] = corner_all_non_bright[corner] && !point.bright;
|
||||
}
|
||||
bool has_bright_corner = false;
|
||||
bool has_bare_corner = false;
|
||||
for (std::size_t corner = 0; corner < corner_seen.size(); ++corner)
|
||||
{
|
||||
has_bright_corner = has_bright_corner || corner_has_bright[corner];
|
||||
has_bare_corner = has_bare_corner || (corner_seen[corner] && corner_all_non_bright[corner]);
|
||||
}
|
||||
if (noise.bright_range_bias_sigma > 0.0 && has_bright_corner && has_bare_corner)
|
||||
{
|
||||
return fitCartPose4D(model, points, initial, noise, max_iterations);
|
||||
}
|
||||
return fitCartPose3D(model, points, initial, noise, max_iterations);
|
||||
}
|
||||
|
||||
} // namespace dock_pattern_detector
|
||||
|
||||
@@ -188,6 +188,23 @@ bool LegProfile::validate(std::string& error) const
|
||||
error = "tape_min_intensity must be finite and tape_min_points >= 1";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(tape_bloom_max_angle) || tape_bloom_max_angle < 0.0 ||
|
||||
tape_bloom_max_angle >= 0.5)
|
||||
{
|
||||
error = "tape_bloom_max_angle must be finite and in [0, 0.5) [rad]";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(tape_range_bias_sigma) || tape_range_bias_sigma < 0.0 ||
|
||||
tape_range_bias_sigma > 0.2)
|
||||
{
|
||||
error = "tape_range_bias_sigma must be finite and in [0, 0.2] [m]";
|
||||
return false;
|
||||
}
|
||||
if (!tape_enabled && (tape_bloom_max_angle > 0.0 || tape_range_bias_sigma > 0.0))
|
||||
{
|
||||
error = "non-zero tape_bloom_max_angle or tape_range_bias_sigma requires tape_enabled: true";
|
||||
return false;
|
||||
}
|
||||
if (std::fabs(cart_opening - cart_depth) < 2.0 * cart_dim_tolerance)
|
||||
{
|
||||
// Xe gần vuông: hình học không biết cạnh nào là cạnh vào. Dò ra cạnh bên là robot chui ngang
|
||||
|
||||
Reference in New Issue
Block a user