Accept a V whose far wing is foreshortened by the viewing angle

A run in simulation matched the pattern 23 times in 15 seconds out of roughly
435 scans - about five percent - and every miss reported the right vertex angle
with the wrong wing length. Measuring it: noise is not the cause (93 to 100
percent at the sensor's own 3 mm sigma) and neither is range (clean detections
out to 5 m). Viewing angle is. Turn the plate and the far wing goes nearly
edge-on to the beam, so few rays land on it and the extracted segment comes out
short. Past thirty degrees the detection rate falls to zero.

Requiring both wings at full length only works when the robot happens to face
the plate squarely, which is not something the approach guarantees. Occlusion
only ever shortens a wing; it cannot lengthen one. So the rule is no longer
symmetric: no wing may be LONGER than wing_length + tolerance, and at least one
must be within tolerance of it. A pallet corner still fails, because a pallet
has both sides long - that gate is untouched, and the test that covers it still
passes.

Confidence scores the length of the long wing rather than the short one. The
short wing is allowed to be short by the rule above, so penalising it again is
charging twice for the same thing, and it created a cliff: candidates cleared
the length gate only to fall at min_confidence. "Too little of it is visible to
trust" is already carried by the support term, which counts the rays on the
thinner wing and degrades smoothly.

With min_wing_points at 5, thirty degrees at 2.5 m goes from 0 to 77 percent,
forty degrees at 1.2 m from 0 to 80, fifty degrees from 0 to 82.

The rejection message also carries the measured vertex angle, both wing lengths
and the apex position now. Without them, "a wing 0.22 m because something is in
the way" and "a wing 0.80 m because that is a pallet" print the same line.
This commit is contained in:
duongtd
2026-08-26 15:24:31 +07:00
parent bb38260c07
commit cea149cae9
2 changed files with 124 additions and 12 deletions

View File

@@ -129,6 +129,29 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
Point gated_apex;
double gated_dist = 0.0;
// Ứng viên trượt SÁT NHẤT ở cổng góc / cổng chiều dài, kèm số ĐO ĐƯỢC. Chỉ đếm số cặp thì không
// biết nên nới dung sai hay sửa chỗ khác — mà "cánh ngắn vì bị che" và "cánh dài vì bắt nhầm
// vật" đòi hai cách xử trí ngược nhau.
bool have_near = false;
double near_penalty = 0.0;
double near_vertex = 0.0;
double near_len[2] = { 0.0, 0.0 };
Point near_apex;
const auto noteNearMiss = [&](double penalty, double vertex, double l1v, double l2v,
const Point& apex_pt) {
if (have_near && penalty >= near_penalty)
{
return;
}
have_near = true;
near_penalty = penalty;
near_vertex = vertex;
near_len[0] = l1v;
near_len[1] = l2v;
near_apex = apex_pt;
};
for (std::size_t i = 0; i + 1 < lines.size(); ++i)
{
for (std::size_t j = i + 1; j < lines.size(); ++j)
@@ -181,17 +204,49 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
if (std::fabs(angle_dev) > profile_.angle_tolerance)
{
++rejected_angle;
noteNearMiss(std::fabs(angle_dev) / profile_.angle_tolerance, vertex, len1, len2, apex);
continue;
}
const double len_dev1 = len1 - profile_.wing_length;
const double len_dev2 = len2 - profile_.wing_length;
if (std::fabs(len_dev1) > profile_.length_tolerance ||
std::fabs(len_dev2) > profile_.length_tolerance)
// Luật chiều dài KHÔNG đối xứng, và đó là chủ đích.
//
// Nhìn tấm chếch thì cánh xa gần như song song với tia: rất ít tia trúng nó, đoạn trích ra
// NGẮN hơn thật. Che khuất chỉ làm ngắn đi, không bao giờ dài ra. Bắt cả hai cánh đúng
// `wing_length` nghĩa là chỉ dò được khi robot đứng gần chính diện — đo được: nghiêng 40 độ
// là tỉ lệ trúng về 0.
//
// Nên:
// - KHÔNG cánh nào được DÀI hơn `wing_length + tol`. Đây là cổng giữ nguyên tác dụng chặn
// góc pallet: pallet có CẢ HAI cạnh dài, nên nó vẫn bị loại.
// - ÍT NHẤT MỘT cánh phải đúng `wing_length ± tol`. Vẫn phải nhìn thấy trọn một cánh, nếu
// không thì hai mẩu ngắn bất kỳ gặp nhau ở 90 độ cũng lọt.
const double longest = std::max(len1, len2);
const double shortest = std::min(len1, len2);
if (longest > profile_.wing_length + profile_.length_tolerance ||
longest < profile_.wing_length - profile_.length_tolerance)
{
++rejected_length;
noteNearMiss(std::fabs(longest - profile_.wing_length) / profile_.length_tolerance, vertex,
len1, len2, apex);
continue;
}
if (shortest > profile_.wing_length + profile_.length_tolerance)
{
++rejected_length;
noteNearMiss(std::fabs(shortest - profile_.wing_length) / profile_.length_tolerance, vertex,
len1, len2, apex);
continue;
}
// Điểm tin chấm chiều dài theo cánh DÀI, không phải cánh ngắn. Cánh ngắn được luật ở trên
// cho phép ngắn — trừ điểm nó nữa là phạt hai lần cùng một chuyện, và tạo ra vách: ứng viên
// vừa lọt cổng chiều dài lại rớt ngay cổng độ tin.
//
// Việc "nhìn được ít quá nên đừng tin" đã có `support` lo: nó tính theo số tia đỡ của cánh ÍT
// tia nhất, nên nhìn càng chếch điểm càng xuống — giảm dần, không phải vách.
const double len_dev1 = longest - profile_.wing_length;
const double len_dev2 = len_dev1;
// Phân giác, rồi lật cho hướng RA phía cảm biến. Chọn dấu bằng vị trí cảm biến chứ không bằng
// thứ tự hai cánh: thứ tự đổi theo phía robot đứng, và lệch 180 độ ở đây là robot lùi vào
@@ -320,15 +375,27 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
best.reason = std::to_string(rejected_confidence) +
" candidate(s) matched but scored below min_confidence";
}
else if (rejected_length > 0)
else if (rejected_length > 0 || rejected_angle > 0)
{
best.reason = std::to_string(rejected_length) + " pair(s) had the right vertex angle but the "
"wrong wing length";
}
else if (rejected_angle > 0)
{
best.reason =
std::to_string(rejected_angle) + " pair(s) met at a vertex but at the wrong angle";
// Số ĐO ĐƯỢC của ứng viên trượt sát nhất. Không có nó thì "cánh 0.22 m vì bị che" và
// "cánh 0.80 m vì đó là góc pallet" ra cùng một dòng log.
char detail[224] = { 0 };
if (have_near)
{
std::snprintf(detail, sizeof(detail),
" (nearest: vertex %.3f rad vs %.3f+-%.3f, wings %.3f / %.3f m vs %.3f+-%.3f,"
" apex at (%.2f, %.2f) range %.2f m)",
near_vertex, profile_.vertex_angle, profile_.angle_tolerance, near_len[0],
near_len[1], profile_.wing_length, profile_.length_tolerance, near_apex.x,
near_apex.y, std::hypot(near_apex.x, near_apex.y));
}
best.reason = rejected_length > 0
? std::to_string(rejected_length) +
" pair(s) had the right vertex angle but the wrong wing length"
: std::to_string(rejected_angle) +
" pair(s) met at a vertex but at the wrong angle";
best.reason += detail;
}
else if (rejected_vertex_gap > 0)
{