feat: confirm a candidate with reflective tape when the profile asks for it
Geometry alone cannot answer the question that matters in a warehouse. Every
wall corner and every pair of pallets stacked at a right angle is a V of some
size, and enough of them are the right size. None of them carry two strips of
reflective tape at the expected spacing.
Off by default: with tape_enabled false the matcher never reads the intensity
array and nothing about the existing behaviour changes.
On, it is an extra criterion, not a fifth pattern type. Shape still decides
where the apex is and the pose that comes out is still entirely from shape;
the tape only answers whether this is the dock. The check runs after every
geometric gate, because it is the expensive one and geometry has already
thrown out nearly every candidate by then - measured at +0.11 us per scan
against the 508 us the line extraction ahead of it already costs.
On each wing it takes the longest consecutive run of bright rays, then:
- the two run centres must be tape_spacing apart;
- each run must be tape_width long, checked only when the run has 2 or more
rays. One ray tells you it is bright but not how wide, and at the range
where that happens the tape is what you needed most, so the criterion is
skipped rather than guessed.
Runs are searched across the whole ray span the wing covers, not only the rays
that supported the fitted line: tape that stands proud of the wing surface can
be split off into its own segment, and then the very rays being looked for no
longer belong to any wing.
The measured width is always short - the outermost bright rays land inside the
strip, not on its edges, so the span between them misses up to one ray spacing.
At 3 m one nanoscan3 ray spacing is 0.021 m, 17% of a 0.12 m strip. That is
systematic, not noise, so it is corrected: n bright rays span exactly n-1 gaps,
and adding one gap adds half a gap at each edge. The gap comes from the run
itself, so it self-calibrates for range and for how obliquely the plate is
seen, with no extra parameter. On a deliberately coarse 200-ray scan it reads
0.119 m against a true 0.120 m.
tape_min_intensity carries each sensor's own units and has no common scale -
Gazebo passes laser_retro straight through, a real nanoscan3 does not. When the
profile asks for tape and the scan carries no intensities at all, the reason
says so, because that symptom is otherwise identical to "the dock is not in
range" and only one of the two is fixable in config.
This commit is contained in:
@@ -124,6 +124,47 @@ bool DockProfile::validate(std::string& error) const
|
||||
error = "offset_x/offset_y/offset_yaw must be finite";
|
||||
return false;
|
||||
}
|
||||
if (tape_enabled)
|
||||
{
|
||||
if (!positive(tape_spacing))
|
||||
{
|
||||
error = "tape_spacing must be finite and > 0 when tape_enabled";
|
||||
return false;
|
||||
}
|
||||
if (!positive(tape_width))
|
||||
{
|
||||
error = "tape_width must be finite and > 0 when tape_enabled";
|
||||
return false;
|
||||
}
|
||||
if (!positive(tape_spacing_tolerance) || tape_spacing_tolerance >= tape_spacing)
|
||||
{
|
||||
// Dung sai >= chính khoảng cách nghĩa là chấp nhận cả hai dải chồng lên nhau — cổng vô nghĩa.
|
||||
error = "tape_spacing_tolerance must be finite, > 0 and < tape_spacing";
|
||||
return false;
|
||||
}
|
||||
if (!positive(tape_width_tolerance) || tape_width_tolerance >= tape_width)
|
||||
{
|
||||
error = "tape_width_tolerance must be finite, > 0 and < tape_width";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(tape_min_intensity))
|
||||
{
|
||||
error = "tape_min_intensity must be finite";
|
||||
return false;
|
||||
}
|
||||
if (tape_spacing > 2.0 * wing_length)
|
||||
{
|
||||
// Hai dải dán TRÊN hai cánh, nên khoảng cách giữa chúng không thể vượt quá hai lần chiều dài
|
||||
// cánh dù góc mở có rộng đến đâu. Vượt ngưỡng này là đã đo nhầm hoặc gõ nhầm đơn vị.
|
||||
error = "tape_spacing must be <= 2 * wing_length; the tapes sit on the wings";
|
||||
return false;
|
||||
}
|
||||
if (tape_width > wing_length)
|
||||
{
|
||||
error = "tape_width must be <= wing_length; the tape sits along one wing";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (min_wing_points < 2)
|
||||
{
|
||||
// Hai điểm là tối thiểu tuyệt đối để có một đoạn; dưới đó không có gì để khớp.
|
||||
|
||||
@@ -81,6 +81,110 @@ Point nearEndpoint(const Line& line, const Point& apex)
|
||||
return dist(s, apex) < dist(e, apex) ? s : e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @struct TapeRun
|
||||
* @brief Vệt tia sáng dài nhất trên một cánh.
|
||||
*/
|
||||
struct TapeRun
|
||||
{
|
||||
bool found = false;
|
||||
Point centre;
|
||||
double width = 0.0; ///< [m] đã bù lượng tử hoá; 0 khi chỉ có 1 tia
|
||||
bool width_known = false;
|
||||
std::size_t points = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Tìm vệt tia sáng dài nhất mà @p line trải qua.
|
||||
*
|
||||
* Quét toàn bộ khoảng tia từ đầu đến cuối phần đỡ của đoạn, KHÔNG chỉ các tia đã đỡ nó. Dải dán
|
||||
* nổi lên khỏi mặt cánh có thể bị bước tách cắt ra thành đoạn riêng, và khi ấy đúng những tia cần
|
||||
* tìm lại không còn thuộc cánh nào.
|
||||
*
|
||||
* Chiều dài vệt luôn bị ĐO HỤT: tia sáng đầu và tia sáng cuối nằm bên trong dải chứ không đúng ở
|
||||
* hai mép, nên khoảng cách giữa chúng thiếu tới một bước tia. Ở 3 m một bước tia của nanoscan3 là
|
||||
* 0.021 m, tức 17% của dải 0.12 m — đó là sai lệch hệ thống, không phải nhiễu, và bù được: khoảng
|
||||
* cách giữa hai tia sáng ngoài cùng trải qua đúng `n-1` bước, nên cộng thêm một bước là thêm nửa
|
||||
* bước ở mỗi mép. Bước tia lấy từ chính vệt đó, nên nó tự hiệu chỉnh theo tầm lẫn theo độ chếch mà
|
||||
* không cần khai thêm tham số nào.
|
||||
*/
|
||||
TapeRun findTape(const Line& line, const std::vector<float>& intensities, double min_intensity)
|
||||
{
|
||||
TapeRun best;
|
||||
const std::vector<unsigned int>& support = line.getIndices();
|
||||
if (support.empty())
|
||||
{
|
||||
return best;
|
||||
}
|
||||
|
||||
const unsigned int first = support.front();
|
||||
const unsigned int last = support.back();
|
||||
if (static_cast<std::size_t>(last) >= intensities.size())
|
||||
{
|
||||
return best; // Scan và mảng cường độ không cùng độ dài. Không đoán, coi như không có dữ liệu.
|
||||
}
|
||||
|
||||
std::size_t run_begin = 0;
|
||||
std::size_t run_len = 0;
|
||||
std::size_t best_begin = 0;
|
||||
std::size_t best_len = 0;
|
||||
|
||||
for (unsigned int index = first; index <= last; ++index)
|
||||
{
|
||||
const std::array<double, 2> point = line.pointAt(index);
|
||||
const bool bright = intensities[index] >= min_intensity && std::isfinite(point[0]) &&
|
||||
std::isfinite(point[1]);
|
||||
if (bright)
|
||||
{
|
||||
if (run_len == 0)
|
||||
{
|
||||
run_begin = index;
|
||||
}
|
||||
++run_len;
|
||||
if (run_len > best_len)
|
||||
{
|
||||
best_len = run_len;
|
||||
best_begin = run_begin;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
run_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (best_len == 0)
|
||||
{
|
||||
return best;
|
||||
}
|
||||
|
||||
double sum_x = 0.0;
|
||||
double sum_y = 0.0;
|
||||
for (std::size_t k = 0; k < best_len; ++k)
|
||||
{
|
||||
const std::array<double, 2> point = line.pointAt(static_cast<unsigned int>(best_begin + k));
|
||||
sum_x += point[0];
|
||||
sum_y += point[1];
|
||||
}
|
||||
|
||||
best.found = true;
|
||||
best.points = best_len;
|
||||
best.centre.x = sum_x / static_cast<double>(best_len);
|
||||
best.centre.y = sum_y / static_cast<double>(best_len);
|
||||
|
||||
if (best_len >= 2)
|
||||
{
|
||||
const std::array<double, 2> a = line.pointAt(static_cast<unsigned int>(best_begin));
|
||||
const std::array<double, 2> b =
|
||||
line.pointAt(static_cast<unsigned int>(best_begin + best_len - 1));
|
||||
const double span = std::hypot(a[0] - b[0], a[1] - b[1]);
|
||||
best.width = span + span / static_cast<double>(best_len - 1);
|
||||
best.width_known = true;
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
/// @brief Điểm khớp trong [0, 1]: 1 khi lệch bằng 0, 0 khi lệch chạm dung sai.
|
||||
double score(double deviation, double tolerance)
|
||||
{
|
||||
@@ -104,9 +208,26 @@ bool VPatternMatcher::configure(const DockProfile& profile, std::string& error)
|
||||
}
|
||||
|
||||
Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
{
|
||||
static const std::vector<float> kNoIntensities;
|
||||
return match(lines, kNoIntensities);
|
||||
}
|
||||
|
||||
Detection VPatternMatcher::match(const std::vector<Line>& lines,
|
||||
const std::vector<float>& intensities) const
|
||||
{
|
||||
Detection best;
|
||||
|
||||
if (profile_.tape_enabled && intensities.empty())
|
||||
{
|
||||
// Nói thẳng thay vì lặng lẽ không dò ra gì: "cảm biến không gửi cường độ" và "trạm không có
|
||||
// trong tầm" là hai chuyện khác hẳn, và chỉ chuyện đầu sửa được bằng config.
|
||||
best.reason = "profile requires reflective tape but the scan carries no intensity data — "
|
||||
"either the driver does not publish 'intensities' or tape_enabled is wrong";
|
||||
best.segment_count = lines.size();
|
||||
return best;
|
||||
}
|
||||
|
||||
if (lines.size() < 2)
|
||||
{
|
||||
best.reason = "need 2 line segments, scan yielded " + std::to_string(lines.size());
|
||||
@@ -120,6 +241,7 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
std::size_t rejected_length = 0;
|
||||
std::size_t rejected_support = 0;
|
||||
std::size_t rejected_prior = 0;
|
||||
std::size_t rejected_tape = 0;
|
||||
std::size_t rejected_confidence = 0;
|
||||
std::size_t accepted = 0;
|
||||
|
||||
@@ -132,6 +254,14 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
// Ứ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.
|
||||
// Ứng viên đúng hình nhưng trượt cổng phản quang, kèm số ĐO ĐƯỢC. "Không thấy dải nào" và "thấy
|
||||
// hai dải nhưng cách nhau 0.31 m" đòi hai cách xử trí ngược nhau: cái đầu là ngưỡng cường độ hoặc
|
||||
// dải bẩn, cái sau là bắt nhầm vật khác.
|
||||
bool have_tape_miss = false;
|
||||
double tape_miss_spacing = 0.0;
|
||||
std::size_t tape_miss_points[2] = { 0, 0 };
|
||||
Point tape_miss_apex;
|
||||
|
||||
bool have_near = false;
|
||||
double near_penalty = 0.0;
|
||||
double near_vertex = 0.0;
|
||||
@@ -269,6 +399,68 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
}
|
||||
const double yaw = std::atan2(by, bx);
|
||||
|
||||
// Cổng phản quang. Đặt SAU mọi cổng hình học vì nó đắt hơn hẳn — quét lại từng tia của hai
|
||||
// cánh — và hình học đã loại gần hết ứng viên trước khi tới đây.
|
||||
double tape_spacing_score = 1.0;
|
||||
double tape_width_score = 1.0;
|
||||
TapeRun tape1;
|
||||
TapeRun tape2;
|
||||
if (profile_.tape_enabled)
|
||||
{
|
||||
tape1 = findTape(l1, intensities, profile_.tape_min_intensity);
|
||||
tape2 = findTape(l2, intensities, profile_.tape_min_intensity);
|
||||
|
||||
const double measured_spacing =
|
||||
(tape1.found && tape2.found) ? dist(tape1.centre, tape2.centre) : 0.0;
|
||||
|
||||
if (!have_tape_miss || (tape1.points + tape2.points) >
|
||||
(tape_miss_points[0] + tape_miss_points[1]))
|
||||
{
|
||||
have_tape_miss = true;
|
||||
tape_miss_spacing = measured_spacing;
|
||||
tape_miss_points[0] = tape1.points;
|
||||
tape_miss_points[1] = tape2.points;
|
||||
tape_miss_apex = apex;
|
||||
}
|
||||
|
||||
if (!tape1.found || !tape2.found)
|
||||
{
|
||||
++rejected_tape;
|
||||
continue;
|
||||
}
|
||||
|
||||
const double spacing_dev = measured_spacing - profile_.tape_spacing;
|
||||
if (std::fabs(spacing_dev) > profile_.tape_spacing_tolerance)
|
||||
{
|
||||
++rejected_tape;
|
||||
continue;
|
||||
}
|
||||
tape_spacing_score = score(spacing_dev, profile_.tape_spacing_tolerance);
|
||||
|
||||
// Chiều dài chỉ kiểm khi đo được. Một tia thì biết là sáng nhưng không biết rộng bao nhiêu;
|
||||
// bỏ qua tiêu chí ấy là trung thực hơn đoán, và ở tầm xa đó lại đúng là lúc cần dải nhất.
|
||||
for (const TapeRun& tape : { tape1, tape2 })
|
||||
{
|
||||
if (!tape.width_known)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const double width_dev = tape.width - profile_.tape_width;
|
||||
if (std::fabs(width_dev) > profile_.tape_width_tolerance)
|
||||
{
|
||||
++rejected_tape;
|
||||
tape_width_score = -1.0; // đánh dấu trượt, kiểm ngay dưới
|
||||
break;
|
||||
}
|
||||
tape_width_score =
|
||||
std::min(tape_width_score, score(width_dev, profile_.tape_width_tolerance));
|
||||
}
|
||||
if (tape_width_score < 0.0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (profile_.has_prior)
|
||||
{
|
||||
const double d = std::hypot(apex.x - profile_.prior_x, apex.y - profile_.prior_y);
|
||||
@@ -305,7 +497,8 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
const double confidence =
|
||||
std::min({ score(angle_dev, profile_.angle_tolerance),
|
||||
score(len_dev1, profile_.length_tolerance),
|
||||
score(len_dev2, profile_.length_tolerance), support });
|
||||
score(len_dev2, profile_.length_tolerance), support, tape_spacing_score,
|
||||
tape_width_score });
|
||||
|
||||
if (confidence < profile_.min_confidence)
|
||||
{
|
||||
@@ -326,6 +519,12 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
best.measured_vertex_angle = vertex;
|
||||
best.measured_wing_length[0] = len1;
|
||||
best.measured_wing_length[1] = len2;
|
||||
best.measured_tape_spacing =
|
||||
(tape1.found && tape2.found) ? dist(tape1.centre, tape2.centre) : 0.0;
|
||||
best.measured_tape_width[0] = tape1.width;
|
||||
best.measured_tape_width[1] = tape2.width;
|
||||
best.tape_points[0] = tape1.points;
|
||||
best.tape_points[1] = tape2.points;
|
||||
|
||||
// Pose đích = đỉnh dịch theo offset của profile, trong hệ của CHÍNH hình: x dọc pháp tuyến
|
||||
// hướng ra phía robot, y sang trái.
|
||||
@@ -371,6 +570,22 @@ Detection VPatternMatcher::match(const std::vector<Line>& lines) const
|
||||
" candidate(s) matched the shape but fell outside the search gate";
|
||||
}
|
||||
}
|
||||
else if (rejected_tape > 0)
|
||||
{
|
||||
char detail[224] = { 0 };
|
||||
if (have_tape_miss)
|
||||
{
|
||||
std::snprintf(detail, sizeof(detail),
|
||||
" (nearest: %zu + %zu bright ray(s), spacing %.3f m vs %.3f+-%.3f,"
|
||||
" apex at (%.2f, %.2f); threshold tape_min_intensity=%.0f)",
|
||||
tape_miss_points[0], tape_miss_points[1], tape_miss_spacing,
|
||||
profile_.tape_spacing, profile_.tape_spacing_tolerance, tape_miss_apex.x,
|
||||
tape_miss_apex.y, profile_.tape_min_intensity);
|
||||
}
|
||||
best.reason = std::to_string(rejected_tape) +
|
||||
" candidate(s) had the right shape but not the reflective tape signature";
|
||||
best.reason += detail;
|
||||
}
|
||||
else if (rejected_confidence > 0)
|
||||
{
|
||||
best.reason = std::to_string(rejected_confidence) +
|
||||
|
||||
Reference in New Issue
Block a user