fix(leg_pattern): accept attached legs, treat zero range as no echo, rank ties by fit
On the KIT robot (28/10), hiding one trolley leg with a post always made detection fail with "too few legs". The far-right leg stood less than leg_min_depth_gap in front of another object, so its cluster was rejected as "attached". Hiding one more leg left only two free-standing legs. - LegProfile::accept_attached_legs (default false): an attached cluster that passes the width and point gates becomes a weak leg. A weak leg can be assigned to a corner, counts toward min_legs and enters the fit, but it cannot start a hypothesis. So every cart still needs one pair of free-standing legs at the right spacing. - ScanLimits::zero_range_is_no_echo (default false): the KIT driver reports no echo as range 0. The matcher read 0 as "blocked closer than min_range", so every leg edge was marked partial, and a 0 ray at a missing corner always read as occluded. With the flag set, an edge next to a 0 ray is free. A corner that sees only 0 rays carries no evidence. A ray that passes the corner and returns from a surface behind it still rejects the cart. - Confidence ties: fit_term saturates at 1, so two 4-leg hypotheses can both score 1.00. The nearer one used to win, even when it had taken a post standing 9 cm from a leg (3.4 cm off). Ties now go to the lower normalized RMS first. - LegDetection::legs_attached; the reason text says when only attached legs were found at the cart spacing. Tests: leg_pattern_matcher_kit_test replays 6 real KIT scans (test/data/kit_f_scan_trolley_occlusion.txt). leg_pattern_matcher_test adds 4 synthetic cases. With both keys off, behaviour is unchanged except for the tie rule. All 111 tests of this package and the trolley handler pass.
This commit is contained in:
@@ -210,6 +210,10 @@ int LegPatternMatcher::classifyEdge(std::size_t edge, int step) const
|
||||
const Ray& nb = rays_[j];
|
||||
if (!nb.valid)
|
||||
{
|
||||
if (zero_no_echo_ && nb.range <= 0.0)
|
||||
{
|
||||
return 0; // driver báo "không phản hồi" bằng 0: phía ngoài không có gì trả về
|
||||
}
|
||||
// Có phản hồi nhưng gần hơn min_range: có vật sát cảm biến chắn. Không phản hồi hoặc xa hơn
|
||||
// max_range: phía sau trống.
|
||||
return (std::isfinite(nb.range) && nb.range < min_range_) ? 1 : 0;
|
||||
@@ -239,12 +243,15 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
|
||||
const int left = classifyEdge(cluster_.front(), -1);
|
||||
const int right = classifyEdge(cluster_.back(), +1);
|
||||
if (left == 2 || right == 2)
|
||||
const bool attached = (left == 2 || right == 2);
|
||||
if (attached && !profile_.accept_attached_legs)
|
||||
{
|
||||
// Mép liền với vật khác ở cùng tầm: tường, tủ, thanh giằng. Không phải chân đứng tự do.
|
||||
++rejected_attached_;
|
||||
return;
|
||||
}
|
||||
// Còn tới đây mà dính thì là chân YẾU: vẫn phải qua mọi cổng hình dạng bên dưới, nhưng chỉ được
|
||||
// gán vào góc, không lập giả thuyết (xem generateHypotheses).
|
||||
const bool partial = (left == 1 || right == 1);
|
||||
|
||||
double r_min = std::numeric_limits<double>::infinity();
|
||||
@@ -257,6 +264,7 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
// nhất. Lớp thứ hai sau bộ lọc kéo đuôi.
|
||||
LegCandidate leg;
|
||||
leg.partial = partial;
|
||||
leg.attached = attached;
|
||||
leg.r_min = r_min;
|
||||
const double depth_limit = r_min + profile_.maxApparentLegWidth();
|
||||
for (std::size_t idx : cluster_)
|
||||
@@ -273,14 +281,15 @@ void LegPatternMatcher::processCluster(double dtheta)
|
||||
}
|
||||
|
||||
// Dây cung giữa tia đầu và tia cuối luôn ngắn hơn bề rộng biểu kiến thật tới 2 khoảng tia: tia
|
||||
// rơi BÊN TRONG mép vật. Cận dưới phải trừ phần đó, cận trên thì không.
|
||||
// rơi BÊN TRONG mép vật. Cận dưới phải trừ phần đó, cận trên thì không. Mép bị che hay mép dính
|
||||
// đều không cho biết vật thật rộng tới đâu, nên hai loại đó bỏ cận dưới.
|
||||
const Ray& a = rays_[leg.points.front()];
|
||||
const Ray& b = rays_[leg.points.back()];
|
||||
const double chord = std::hypot(b.x - a.x, b.y - a.y);
|
||||
const double upper = profile_.maxApparentLegWidth() + profile_.leg_width_tolerance;
|
||||
const double lower =
|
||||
profile_.minApparentLegWidth() - profile_.leg_width_tolerance - 2.0 * r_min * dtheta;
|
||||
if (chord > upper || (!partial && chord < lower))
|
||||
if (chord > upper || (!partial && !attached && chord < lower))
|
||||
{
|
||||
++rejected_width_;
|
||||
const double miss = chord > upper ? chord - upper : lower - chord;
|
||||
@@ -454,6 +463,13 @@ void LegPatternMatcher::generateHypotheses(double dtheta, const Pose2D& scan_in_
|
||||
{
|
||||
const LegCandidate& a = legs_[i];
|
||||
const LegCandidate& b = legs_[j];
|
||||
if (a.attached || b.attached)
|
||||
{
|
||||
// Chân yếu có thể là một mẩu tường cỡ chân: chỉ cho xác nhận một góc đã có giả thuyết từ
|
||||
// hai chân tự do, không cho tự dựng giả thuyết. Không thì hai chỗ lồi trên tường đúng
|
||||
// khoảng cách đã đủ thành một cặp.
|
||||
continue;
|
||||
}
|
||||
const double dx = b.rough_x - a.rough_x;
|
||||
const double dy = b.rough_y - a.rough_y;
|
||||
const double d = std::hypot(dx, dy);
|
||||
@@ -594,6 +610,8 @@ int LegPatternMatcher::classifyMissingCorner(double px, double py, const ScanLim
|
||||
const double band = profile_.maxApparentLegWidth();
|
||||
bool nearer = false;
|
||||
bool near = false;
|
||||
bool passed = false; // có tia chắc chắn đi qua chỗ chân
|
||||
bool unknown = false; // có tia không phản hồi (range 0, khi zero_no_echo_)
|
||||
for (long i = lo; i <= hi; ++i)
|
||||
{
|
||||
const Ray& r = rays_[i];
|
||||
@@ -603,11 +621,22 @@ int LegPatternMatcher::classifyMissingCorner(double px, double py, const ScanLim
|
||||
}
|
||||
if (!r.valid)
|
||||
{
|
||||
if (zero_no_echo_ && r.range <= 0.0)
|
||||
{
|
||||
// Không phản hồi có thể là tia đi qua, cũng có thể là chân sơn tối/tia sượt không trả về.
|
||||
// Không đủ để nói chân vắng mặt.
|
||||
unknown = true;
|
||||
continue;
|
||||
}
|
||||
if (std::isfinite(r.range) && r.range < limits.min_range)
|
||||
{
|
||||
nearer = true;
|
||||
}
|
||||
continue; // không phản hồi: tia đi qua
|
||||
else
|
||||
{
|
||||
passed = true; // xa hơn max_range hoặc inf: tia đi qua
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (r.range < rp - band)
|
||||
{
|
||||
@@ -617,6 +646,10 @@ int LegPatternMatcher::classifyMissingCorner(double px, double py, const ScanLim
|
||||
{
|
||||
near = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
passed = true; // trúng vật phía sau chỗ chân
|
||||
}
|
||||
}
|
||||
|
||||
if (near)
|
||||
@@ -627,6 +660,10 @@ int LegPatternMatcher::classifyMissingCorner(double px, double py, const ScanLim
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (unknown && !passed)
|
||||
{
|
||||
return 0; // mọi tia đều không phản hồi: không có thông tin
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
@@ -753,12 +790,14 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
|
||||
int seen = 0;
|
||||
int partial = 0;
|
||||
int attached = 0;
|
||||
for (int k = 0; k < kLegCount; ++k)
|
||||
{
|
||||
if (h.legs[k] >= 0)
|
||||
{
|
||||
++seen;
|
||||
partial += legs_[h.legs[k]].partial ? 1 : 0;
|
||||
attached += legs_[h.legs[k]].attached ? 1 : 0;
|
||||
}
|
||||
}
|
||||
if (seen < profile_.min_legs)
|
||||
@@ -788,9 +827,10 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
const double expected = pair < 2 ? profile_.cart_opening :
|
||||
pair < 4 ? profile_.cart_depth :
|
||||
profile_.cartDiagonal();
|
||||
// Tâm thô của chân bị che một phần hoặc chân dính lệch tới nửa bề rộng chân.
|
||||
const double tol = profile_.cart_dim_tolerance + std::max(la.r_min, lb.r_min) * dtheta +
|
||||
(la.partial ? profile_.leg_width / 2.0 : 0.0) +
|
||||
(lb.partial ? profile_.leg_width / 2.0 : 0.0);
|
||||
((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);
|
||||
if (std::fabs(d - expected) > tol)
|
||||
{
|
||||
@@ -916,6 +956,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
const double seen_term = static_cast<double>(seen) / static_cast<double>(kLegCount);
|
||||
const double fit_term = std::max(0.0, std::min(1.0, 2.0 - fit.normalized_rms));
|
||||
h.confidence = 0.5 * seen_term + 0.5 * fit_term;
|
||||
h.normalized_rms = fit.normalized_rms;
|
||||
if (h.confidence < profile_.min_confidence)
|
||||
{
|
||||
h.stage = kStageConfidence;
|
||||
@@ -934,6 +975,7 @@ bool LegPatternMatcher::evaluate(Hypothesis& h, const ScanLimits& limits,
|
||||
d.confidence = h.confidence;
|
||||
d.legs_seen = seen;
|
||||
d.legs_partial = partial;
|
||||
d.legs_attached = attached;
|
||||
d.legs_occluded = occluded;
|
||||
d.legs_distorted = distorted;
|
||||
d.legs_out_of_view = out_of_view;
|
||||
@@ -1019,6 +1061,7 @@ LegDetection LegPatternMatcher::match(const robot_line_extraction::ScanGeometry&
|
||||
angle_min_ = geometry.angle_min;
|
||||
angle_increment_ = geometry.angle_increment;
|
||||
min_range_ = limits.min_range;
|
||||
zero_no_echo_ = limits.zero_range_is_no_echo;
|
||||
|
||||
prepareRays(geometry, ranges, intensities, limits, scan_in_prior_frame);
|
||||
filterShadows(dtheta);
|
||||
@@ -1059,6 +1102,16 @@ LegDetection LegPatternMatcher::match(const robot_line_extraction::ScanGeometry&
|
||||
{
|
||||
out.reason += ", or all were outside the search gate";
|
||||
}
|
||||
std::size_t attached = 0;
|
||||
for (const LegCandidate& leg : legs_)
|
||||
{
|
||||
attached += leg.attached ? 1 : 0;
|
||||
}
|
||||
if (attached > 0)
|
||||
{
|
||||
out.reason += "; " + std::to_string(attached) +
|
||||
" of them attached to a surface (attached legs cannot start a hypothesis)";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -1103,7 +1156,10 @@ LegDetection LegPatternMatcher::match(const robot_line_extraction::ScanGeometry&
|
||||
return out;
|
||||
}
|
||||
|
||||
// Xếp theo điểm tin; hoà thì xe gần cảm biến hơn, rồi theo thứ tự lập — tất định.
|
||||
// Xếp theo điểm tin; hoà thì khớp tốt hơn, rồi xe gần cảm biến hơn, rồi theo thứ tự lập — tất
|
||||
// định. Hoà điểm tin là chuyện thường: fit_term kẹp ở 1 khi RMS chuẩn hoá ≤ 1, nên hai giả thuyết
|
||||
// cùng số chân đều được 1.00 dù một cái lấy nhầm vật đứng sát chân (scan KIT 28/10: cột cạnh chân
|
||||
// xa-phải, rms 2.9 cm so với 1.7 cm, xe lệch 3.4 cm). Thứ tự khớp phải đứng trước khoảng cách.
|
||||
std::stable_sort(accepted.begin(), accepted.end(), [this](std::size_t a, std::size_t b) {
|
||||
const Hypothesis& ha = hypotheses_[a];
|
||||
const Hypothesis& hb = hypotheses_[b];
|
||||
@@ -1111,6 +1167,10 @@ LegDetection LegPatternMatcher::match(const robot_line_extraction::ScanGeometry&
|
||||
{
|
||||
return ha.confidence > hb.confidence;
|
||||
}
|
||||
if (ha.normalized_rms != hb.normalized_rms)
|
||||
{
|
||||
return ha.normalized_rms < hb.normalized_rms;
|
||||
}
|
||||
return std::hypot(ha.pose.x, ha.pose.y) < std::hypot(hb.pose.x, hb.pose.y);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user