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:
2026-08-26 16:46:33 +07:00
parent 5e39ca2639
commit d58d08eda3
6 changed files with 647 additions and 4 deletions

View File

@@ -8,7 +8,8 @@
* 1. dò đúng — đỉnh, hướng, offset, hình nghiêng;
* 2. TỪ CHỐI — đây mới là phần quan trọng: pallet đánh lừa, tường cắt nhau ở xa, hình méo;
* 3. cổng tìm kiếm — prior và quạt;
* 4. nhiều ứng viên, và config hỏng.
* 4. nhiều ứng viên, và config hỏng;
* 5. dải phản quang — dữ liệu cường độ THÊM vào việc dò hình.
*
* Author: DuongTD
*********************************************************************/
@@ -113,6 +114,75 @@ std::vector<Segment> vPlate(double apex_x, double half_angle, double wing, doubl
return { w1, w2 };
}
/// @brief [m] Cự ly từ điểm (@p px, @p py) tới đoạn @p s.
double distToSegment(double px, double py, const Segment& s)
{
const double ex = s.bx - s.ax;
const double ey = s.by - s.ay;
const double len2 = ex * ex + ey * ey;
double t = (len2 > 0.0) ? ((px - s.ax) * ex + (py - s.ay) * ey) / len2 : 0.0;
t = std::max(0.0, std::min(1.0, t));
return std::hypot(px - (s.ax + t * ex), py - (s.ay + t * ey));
}
/**
* @brief Hai dải phản quang dán GIỮA mỗi cánh của tấm dựng bởi @ref vPlate.
*
* Đặt giữa cánh đúng như tấm mẫu trong `dock_pattern_action/sim/models/dock_plate_v`, nên khoảng
* cách hai tâm dải suy được từ hình chứ không phải một con số rời.
*/
std::vector<Segment> vPlateTapes(double apex_x, double half_angle, double wing, double tape_width,
double yaw = 0.0)
{
std::vector<Segment> tapes;
for (const Segment& w : vPlate(apex_x, half_angle, wing, yaw))
{
const double len = std::hypot(w.bx - w.ax, w.by - w.ay);
const double dx = (w.bx - w.ax) / len;
const double dy = (w.by - w.ay) / len;
const double mid_x = w.ax + dx * len / 2.0;
const double mid_y = w.ay + dy * len / 2.0;
Segment tape;
tape.ax = mid_x - dx * tape_width / 2.0;
tape.ay = mid_y - dy * tape_width / 2.0;
tape.bx = mid_x + dx * tape_width / 2.0;
tape.by = mid_y + dy * tape_width / 2.0;
tapes.push_back(tape);
}
return tapes;
}
/**
* @brief Cường độ theo TIA: sáng khi điểm trúng nằm trên một dải, tối ở mọi chỗ khác.
*
* Đúng cách Gazebo trả `laser_retro`: một giá trị cao trên vật khai phản quang, 0 trên mọi vật
* khác. Cảm biến thật trả thang khác, nhưng cái được kiểm ở đây là LUẬT phân tách, không phải thang.
*/
std::vector<float> tapeIntensities(const ScanGeometry& g, const std::vector<double>& ranges,
const std::vector<Segment>& tapes, float bright = 2000.0f)
{
std::vector<float> intensities(g.size, 0.0f);
for (std::size_t i = 0; i < g.size; ++i)
{
if (!std::isfinite(ranges[i]))
{
continue;
}
const double bearing = g.angle_min + static_cast<double>(i) * g.angle_increment;
const double px = ranges[i] * std::cos(bearing);
const double py = ranges[i] * std::sin(bearing);
for (const Segment& tape : tapes)
{
if (distToSegment(px, py, tape) <= 1e-6)
{
intensities[i] = bright;
break;
}
}
}
return intensities;
}
DockProfile vProfile()
{
DockProfile p;
@@ -161,6 +231,21 @@ public:
return matcher_.match(lines_);
}
/// @brief Như trên, kèm dải phản quang: cường độ sinh từ chính các điểm trúng.
Detection run(const std::vector<Segment>& world, const std::vector<Segment>& tapes,
const ScanGeometry& g = geometry())
{
const std::vector<double> ranges = rayCast(g, world);
std::string error;
if (!extractor_.extract(g, ranges, lines_, error))
{
Detection d;
d.reason = error;
return d;
}
return matcher_.match(lines_, tapeIntensities(g, ranges, tapes));
}
bool ok_ = false;
std::string error_;
LineExtractor extractor_;
@@ -494,6 +579,166 @@ TEST(VPatternMatcher, RefusesAProfileThatCanNeverMatch)
EXPECT_DOUBLE_EQ(matcher.profile().wing_length, good.wing_length);
}
// ================================================================================================
// 5. Dải phản quang
// ================================================================================================
namespace
{
/// @brief Tấm chuẩn của nhóm test này: đỉnh ở 1.5 m, mở 90 độ, cánh 0.30 m, dải 0.12 m giữa cánh.
constexpr double kApexX = 1.5; // [m]
constexpr double kHalf = M_PI / 4.0; // [rad]
constexpr double kWing = 0.30; // [m]
constexpr double kTapeWidth = 0.12; // [m]
/// [m] Hai tâm dải nằm ở giữa hai cánh, nên khoảng cách giữa chúng suy ra từ hình.
const double kTapeSpacing = 2.0 * (kWing / 2.0) * std::cos(kHalf);
DockProfile vTapeProfile()
{
DockProfile p = vProfile();
p.tape_enabled = true;
p.tape_spacing = kTapeSpacing;
p.tape_width = kTapeWidth;
p.tape_spacing_tolerance = 0.04; // [m]
p.tape_width_tolerance = 0.05; // [m]
p.tape_min_intensity = 500.0;
return p;
}
} // namespace
TEST(VPatternMatcher, TapeConfirmsAPlateThatCarriesIt)
{
Rig rig(vTapeProfile());
ASSERT_TRUE(rig.ok_) << rig.error_;
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing), vPlateTapes(kApexX, kHalf, kWing,
kTapeWidth));
ASSERT_TRUE(d.valid) << d.reason;
EXPECT_NEAR(d.apex_x, kApexX, 0.02);
EXPECT_NEAR(d.measured_tape_spacing, kTapeSpacing, 0.01);
EXPECT_GT(d.tape_points[0], 1u);
EXPECT_GT(d.tape_points[1], 1u);
}
TEST(VPatternMatcher, TapeRejectsAVOfTheRightShapeThatCarriesNoTape)
{
// Đây là lý do tồn tại của cả tính năng. Hình ở đây ĐÚNG hoàn hảo — cùng góc, cùng chiều dài
// cánh, cùng chỗ — nên mọi cổng hình học đều cho qua. Ngoài kho thì hai tấm pallet kê vuông góc
// dựng ra đúng cái hình này, và không có cách nào phân biệt bằng hình.
Rig rig(vTapeProfile());
ASSERT_TRUE(rig.ok_) << rig.error_;
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing), std::vector<Segment>{}); // không dải nào
EXPECT_FALSE(d.valid);
EXPECT_NE(d.reason.find("reflective tape"), std::string::npos) << d.reason;
}
TEST(VPatternMatcher, TheSamePlateWithoutTapePassesWhenTheTapeCheckIsOff)
{
// Mặt kia của test trên: chính cái hình vừa bị từ chối phải được nhận khi tắt cổng. Không có cặp
// này thì một lỗi làm hỏng hẳn phép dò cũng trông y hệt "cổng phản quang đang làm việc".
Rig rig(vProfile()); // tape_enabled = false, mặc định
ASSERT_TRUE(rig.ok_) << rig.error_;
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing), std::vector<Segment>{});
ASSERT_TRUE(d.valid) << d.reason;
EXPECT_EQ(d.measured_tape_spacing, 0.0);
}
TEST(VPatternMatcher, TapeRejectsStripsAtTheWrongSpacing)
{
// Dải có thật, sáng thật, nhưng dán sai chỗ — dồn ra sát hai đầu mút. Cổng khoảng cách phải bắt
// được, nếu không thì bất kỳ vật phản quang nào trên một chữ V cũng thành cái sạc.
Rig rig(vTapeProfile());
ASSERT_TRUE(rig.ok_) << rig.error_;
std::vector<Segment> tapes;
for (const Segment& w : vPlate(kApexX, kHalf, kWing))
{
const double len = std::hypot(w.bx - w.ax, w.by - w.ay);
const double dx = (w.bx - w.ax) / len;
const double dy = (w.by - w.ay) / len;
Segment tape; // dồn ra đầu mút cánh thay vì giữa cánh
tape.ax = w.bx - dx * kTapeWidth;
tape.ay = w.by - dy * kTapeWidth;
tape.bx = w.bx;
tape.by = w.by;
tapes.push_back(tape);
}
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing), tapes);
EXPECT_FALSE(d.valid);
EXPECT_NE(d.reason.find("reflective tape"), std::string::npos) << d.reason;
}
TEST(VPatternMatcher, TapeOnOnlyOneWingIsNotEnough)
{
Rig rig(vTapeProfile());
ASSERT_TRUE(rig.ok_) << rig.error_;
std::vector<Segment> tapes = vPlateTapes(kApexX, kHalf, kWing, kTapeWidth);
tapes.resize(1);
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing), tapes);
EXPECT_FALSE(d.valid);
EXPECT_NE(d.reason.find("reflective tape"), std::string::npos) << d.reason;
}
TEST(VPatternMatcher, TapeEnabledButNoIntensityDataSaysSoInsteadOfFindingNothing)
{
// Driver không gửi `intensities` và trạm không có trong tầm cho ra cùng một triệu chứng — dò mãi
// không ra. Chỉ một trong hai sửa được bằng config, nên hai ca phải nói ra hai câu khác nhau.
Rig rig(vTapeProfile());
ASSERT_TRUE(rig.ok_) << rig.error_;
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing)); // bản match() không cường độ
EXPECT_FALSE(d.valid);
EXPECT_NE(d.reason.find("no intensity data"), std::string::npos) << d.reason;
}
TEST(VPatternMatcher, TheMeasuredTapeWidthCorrectsForRaySpacing)
{
// Tia sáng ngoài cùng nằm BÊN TRONG dải chứ không đúng ở mép, nên khoảng cách giữa hai tia ngoài
// cùng luôn hụt tới một bước tia. Đây là sai lệch hệ thống chứ không phải nhiễu: quét thưa thì
// hụt nhiều, và nếu không bù thì dải thật bị chấm là hẹp rồi trượt cổng chiều dài.
//
// Quét thưa có chủ đích — 200 tia trên 1.6 rad, khoảng 7 tia trúng dải — để chỗ hụt lớn hơn hẳn
// dung sai đo.
Rig rig(vTapeProfile());
ASSERT_TRUE(rig.ok_) << rig.error_;
const ScanGeometry coarse = geometry(200, -0.8, 0.8);
const Detection d = rig.run(vPlate(kApexX, kHalf, kWing),
vPlateTapes(kApexX, kHalf, kWing, kTapeWidth), coarse);
ASSERT_TRUE(d.valid) << d.reason;
// Một bước tia dọc dải ở tầm này vào khoảng 0.017 m; đo được phải nằm trong ngần ấy quanh 0.12 m.
EXPECT_NEAR(d.measured_tape_width[0], kTapeWidth, 0.02);
EXPECT_NEAR(d.measured_tape_width[1], kTapeWidth, 0.02);
}
TEST(VPatternMatcher, RefusesATapeProfileWithImpossibleNumbers)
{
std::string error;
VPatternMatcher m;
DockProfile p = vTapeProfile();
p.tape_spacing = 5.0; // [m] xa hơn hai lần chiều dài cánh: dải không thể nằm trên cánh
EXPECT_FALSE(m.configure(p, error));
EXPECT_NE(error.find("tape_spacing"), std::string::npos) << error;
p = vTapeProfile();
p.tape_width = 0.9; // [m] dài hơn cả cánh
EXPECT_FALSE(m.configure(p, error));
EXPECT_NE(error.find("tape_width"), std::string::npos) << error;
p = vTapeProfile();
p.tape_spacing_tolerance = p.tape_spacing; // dung sai nuốt trọn phép đo
EXPECT_FALSE(m.configure(p, error));
EXPECT_NE(error.find("tape_spacing_tolerance"), std::string::npos) << error;
}
} // namespace
int main(int argc, char** argv)