feat: pass scan intensities to the matcher and load the tape profile keys

The tape gate reads intensity by ray index, so it is only correct when the
intensity array and the ranges array come from the same scan and have the same
length. The handler hands scan->intensities straight across - not rebuilt, not
buffered, not remembered from an earlier scan.

That is a joint, and joints are what the unit tests below cannot see, so it has
its own coverage: ATapeProfileOnAScanWithoutIntensitiesSaysWhy feeds the exact
plate being looked for on a scan that carries no intensities and requires the
handler to say "no intensity data". A handler that passed a fabricated array
would never produce that line.

One log line per action run, on the first detection that matches, and only when
the tape check is on: how many bright rays each wing returned, the measured
spacing and widths, each against the profile value and tolerance. This is the
number whoever installs a dock needs - tape stuck a few centimetres off still
detects today and then fails outright once it is dusty or damp, and without
this line nobody knows it was already at the edge. Once, because it is a
commissioning number and not a heartbeat.
This commit is contained in:
2026-08-26 16:46:46 +07:00
parent 633291054b
commit 487396a491
4 changed files with 193 additions and 8 deletions

View File

@@ -44,7 +44,7 @@ Nên cổng chính chống nhầm nằm ở **matcher**, không ở bộ lọc.
rung giữa các scan, và cú nhảy `map->odom` khi localization sửa giữa lúc dò — robot đứng yên (D8)
nên cú nhảy đó vẫn dịch pose đã publish.
## Ba chỗ chỉ hỏng khi GHÉP, không test lõi nào thấy
## Bốn chỗ chỉ hỏng khi GHÉP, không test lõi nào thấy
**1. Cursor scan.** Control loop 30 Hz đọc lidar 29 Hz. Không có cursor thì cùng một scan bị đếm
thành nhiều mẫu, `min_samples` mất hết ý nghĩa và handler chốt trên **một** phép đo.
@@ -54,7 +54,14 @@ chuyện thường. Cursor của kho đã nhảy qua scan đó rồi, nên bỏ
lần TF trễ. Phép dò được **giữ lại** và quy đổi ở tick sau, không dò lại (hình đã khớp, chỉ thiếu
phép quy đổi). Có test canh: `ADetectionSurvivesTfArrivingAfterTheScan`.
**3. Quy TF tại đúng stamp của scan**, không phải lúc mới nhất. Pose vừa tính ứng với thời điểm scan
**3. `intensities` phải là mảng CỦA CHÍNH scan đó.** Cổng dải phản quang đọc cường độ theo chỉ số
tia, nên nó chỉ đúng khi mảng cường độ và mảng `ranges` là của cùng một scan, cùng độ dài. Handler
đưa thẳng `scan->intensities` sang matcher — không dựng lại, không đệm. Có test canh:
`ATapeProfileOnAScanWithoutIntensitiesSaysWhy` bơm đúng cái tấm đang dò nhưng scan không mang cường
độ, và đòi handler nói ra `no intensity data`. Nếu handler đưa vào một mảng bịa thì test đó không
thấy được câu ấy.
**4. Quy TF tại đúng stamp của scan**, không phải lúc mới nhất. Pose vừa tính ứng với thời điểm scan
được chụp; tra ở thời điểm khác là trộn hai thời điểm vào một phép đo. Sai số của "mới nhất" nhỏ
thật, nhưng không ai biết nhỏ bao nhiêu — và **stamp lệch hệ thời gian sẽ hiện ra đúng ở đây**.
@@ -97,6 +104,10 @@ Xem `test/config/action_handlers_params.yaml`. Nhóm khoá:
- thời gian: `settle_delay`, `timeout`
- chọn profile: `default_profile`, `profile_param_key`
- `profiles/<tên>/` — hình học + offset của **từng cái sạc**
- `profiles/<tên>/tape_*` — dải phản quang, mặc định **tắt**. Bật thì ứng viên đã đúng hình còn phải
mang hai vệt tia sáng cách nhau đúng `tape_spacing`. Luật khớp ở
[`dock_pattern_detector`](../dock_pattern_detector/README.md); ở tầng này chỉ là chuyện mảng
`intensities` của scan có đi tới matcher hay không.
- `line_extraction/` — theo **cảm biến**, không theo sạc
- toàn bộ khoá của `PoseSampleFilter`, **cùng tên** như FrameSampler
@@ -104,8 +115,8 @@ Xem `test/config/action_handlers_params.yaml`. Nhóm khoá:
```bash
catkin_make --pkg dock_pattern_action
./devel/lib/dock_pattern_action/dock_pattern_action_test # 16 test
./devel/lib/dock_pattern_action/dock_pattern_action_test # 19 test
```
Test dùng **kho thật + buffer tf3 thật + plugin nạp qua Boost.DLL**; chỉ scan là dựng sẵn. Đó là
cách bắt được ba lỗi ghép nối ở trên — không test lõi nào thấy chúng.
cách bắt được bốn lỗi ghép nối ở trên — không test lõi nào thấy chúng.

View File

@@ -188,6 +188,7 @@ public:
last_reason_.clear();
have_reason_log_ = false;
detections_ = 0;
tape_reported_ = false;
std::string profile_name = default_profile_;
const auto it = std::find_if(action.actionParameters.begin(), action.actionParameters.end(),
@@ -349,6 +350,13 @@ private:
nh_param(p_nh, "offset_yaw", profile.offset_yaw);
nh_param(p_nh, "min_confidence", profile.min_confidence);
nh_param(p_nh, "tape_enabled", profile.tape_enabled);
nh_param(p_nh, "tape_spacing", profile.tape_spacing);
nh_param(p_nh, "tape_width", profile.tape_width);
nh_param(p_nh, "tape_spacing_tolerance", profile.tape_spacing_tolerance);
nh_param(p_nh, "tape_width_tolerance", profile.tape_width_tolerance);
nh_param(p_nh, "tape_min_intensity", profile.tape_min_intensity);
int min_wing_points = static_cast<int>(profile.min_wing_points);
nh_param(p_nh, "min_wing_points", min_wing_points);
profile.min_wing_points = static_cast<std::size_t>(std::max(0, min_wing_points));
@@ -460,7 +468,9 @@ private:
return;
}
const dock_pattern_detector::Detection detection = matcher_.match(lines_);
// `intensities` đi thẳng, không đổi kiểu: message đã là std::vector<float>. Rỗng khi driver
// không gửi cường độ — matcher tự nói ra điều đó nếu profile lại đang đòi dải phản quang.
const dock_pattern_detector::Detection detection = matcher_.match(lines_, scan->intensities);
if (!detection.valid)
{
noteReason(now, detection.reason);
@@ -469,6 +479,22 @@ private:
// Hình đã khớp. Giữ lại kèm stamp và frame của scan; phép quy TF làm ở resolvePending() vì nó
// có thể phải CHỜ.
// Một dòng DUY NHẤT cho mỗi lượt action, ở phép dò đầu tiên khớp được, và chỉ khi dải phản
// quang đang bật. Đây là số người lắp trạm cần để đặt dung sai: dán dải lệch vài centimet vẫn
// dò ra hôm nay rồi trượt hẳn khi trời ẩm hoặc dải bẩn bớt sáng, mà không có dòng này thì
// không ai biết nó vốn đã sát mép. Chỉ in một lần vì nó là số hiệu chỉnh, không phải nhịp chạy.
if (matcher_.profile().tape_enabled && !tape_reported_)
{
tape_reported_ = true;
robot::log_info("[dock_pattern] '%s': reflective tape: %zu + %zu bright ray(s), spacing "
"%.3f m (profile %.3f+-%.3f), widths %.3f / %.3f m (profile %.3f+-%.3f).",
name_.c_str(), detection.tape_points[0], detection.tape_points[1],
detection.measured_tape_spacing, matcher_.profile().tape_spacing,
matcher_.profile().tape_spacing_tolerance, detection.measured_tape_width[0],
detection.measured_tape_width[1], matcher_.profile().tape_width,
matcher_.profile().tape_width_tolerance);
}
pending_ = detection;
pending_frame_ = scan->header.frame_id;
pending_stamp_ = tf3::Time(scan->header.stamp.sec, scan->header.stamp.nsec);
@@ -635,6 +661,7 @@ private:
std::string pending_frame_;
tf3::Time pending_stamp_;
bool pending_valid_ = false;
bool tape_reported_ = false;
std::string last_reason_;
robot::Time last_reason_log_;

View File

@@ -50,6 +50,24 @@ actions_dock:
min_confidence: 0.3
offset_x: 0.50
# Cùng hình với v_std, khác đúng một chuyện: bắt buộc thấy dải phản quang.
v_tape:
pattern: v
vertex_angle: 1.5708 # [rad]
wing_length: 0.30 # [m]
angle_tolerance: 0.20 # [rad]
length_tolerance: 0.06 # [m]
vertex_gap: 0.05 # [m]
min_wing_points: 5
min_confidence: 0.3
offset_x: 0.60 # [m] lùi ra trước mặt sạc, giống v_std
tape_enabled: true
tape_spacing: 0.2121 # [m] hai dải ở giữa hai cánh 0.30 m của tấm mở 90 độ
tape_width: 0.12 # [m]
tape_spacing_tolerance: 0.04
tape_width_tolerance: 0.05
tape_min_intensity: 500.0
# scan_topic không có trong kho -> phải từ chối LÚC BOOT kèm danh sách topic có thật.
actions_dock_bad_topic:
handlers:

View File

@@ -64,8 +64,37 @@ std::vector<Segment> vPlate(double apex_x, double half_angle, double wing)
return { { apex_x, 0.0, tip_x, tip_y }, { apex_x, 0.0, tip_x, -tip_y } };
}
/// @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.
std::vector<Segment> vPlateTapes(double apex_x, double half_angle, double wing, double tape_width)
{
std::vector<Segment> tapes;
for (const Segment& w : vPlate(apex_x, half_angle, wing))
{
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;
tapes.push_back({ mid_x - dx * tape_width / 2.0, mid_y - dy * tape_width / 2.0,
mid_x + dx * tape_width / 2.0, mid_y + dy * tape_width / 2.0 });
}
return tapes;
}
robot_sensor_msgs::LaserScan makeScan(const std::vector<Segment>& world, double stamp_s,
std::size_t rays = 600)
std::size_t rays = 600,
const std::vector<Segment>& tapes = {})
{
robot_sensor_msgs::LaserScan scan;
scan.header.frame_id = kScanFrame;
@@ -101,6 +130,31 @@ robot_sensor_msgs::LaserScan makeScan(const std::vector<Segment>& world, double
}
}
// `intensities` để RỖNG khi không có dải nào — đúng như một driver không đo cường độ, và đó là
// ca mà handler phải nói ra thay vì im lặng dò không xong.
if (!tapes.empty())
{
scan.intensities.assign(rays, 0.0f);
for (std::size_t i = 0; i < rays; ++i)
{
if (!std::isfinite(scan.ranges[i]))
{
continue;
}
const double bearing = scan.angle_min + static_cast<double>(i) * scan.angle_increment;
const double px = scan.ranges[i] * std::cos(bearing);
const double py = scan.ranges[i] * std::sin(bearing);
for (const Segment& tape : tapes)
{
if (distToSegment(px, py, tape) <= 1e-6)
{
scan.intensities[i] = 2000.0f; // đúng `laser_retro` của model dock_plate_v
break;
}
}
}
}
return scan;
}
@@ -164,9 +218,33 @@ public:
tf.setTransform(t, "test", /*is_static=*/false);
}
void push(const std::vector<Segment>& world, double stamp_s)
void push(const std::vector<Segment>& world, double stamp_s,
const std::vector<Segment>& tapes = {})
{
robot_sensor_ports::put(store, kScanTopic, makeScan(world, stamp_s), robot::Time(stamp_s));
robot_sensor_ports::put(store, kScanTopic, makeScan(world, stamp_s, 600, tapes),
robot::Time(stamp_s));
}
/// @brief Như @ref feed nhưng scan mang thêm dải phản quang.
ActionStatus feedWithTape(const std::vector<Segment>& world, const std::vector<Segment>& tapes,
int count, double start_s = 10.0, double period = 0.1)
{
ActionStatus status = ActionStatus::kRunning;
for (int i = 0; i < count; ++i)
{
const double t = start_s + period * i;
setSensorPose(0.0, 0.0, 0.0, t);
push(world, t, tapes);
const action_core::ActionTick tick = handler->update(robot::Time(t));
status = tick.status;
last_message = tick.message;
++ticks;
if (status != ActionStatus::kRunning)
{
break;
}
}
return status;
}
/// @brief Bơm @p count scan rồi tick, đúng nhịp mà control loop chạy.
@@ -413,6 +491,57 @@ TEST(DockPatternAction, RefusesAPatternThatIsNotImplementedYet)
EXPECT_EQ(rig.handler, nullptr);
}
TEST(DockPatternAction, CarriesScanIntensitiesThroughToTheMatcher)
{
// Kiểm ĐƯỜNG DẪN DỮ LIỆU, không phải luật khớp — luật đã có test riêng bên
// dock_pattern_detector. Ở đây câu hỏi là: mảng `intensities` của scan có thật sự đi từ kho cảm
// biến tới matcher không, hay handler đang đưa vào một mảng rỗng.
Rig rig;
ASSERT_TRUE(rig.store_ok_);
ASSERT_TRUE(rig.loaded);
ASSERT_TRUE(rig.handler->start(makeAction("v_tape"), robot::Time(10.0)));
EXPECT_EQ(rig.feedWithTape(vPlate(1.20, M_PI / 4.0, 0.30),
vPlateTapes(1.20, M_PI / 4.0, 0.30, 0.12), 8),
ActionStatus::kSucceeded)
<< rig.last_message;
const tf3::TransformStampedMsg out = rig.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time());
EXPECT_NEAR(out.transform.translation.x, 0.60, 0.05);
EXPECT_NEAR(out.transform.translation.y, 0.00, 0.05);
}
TEST(DockPatternAction, ATapeProfileOnAScanWithoutIntensitiesSaysWhy)
{
// Cùng tấm, cùng profile, khác đúng một chuyện: scan không mang cường độ — như driver không đo
// được. Nếu handler đưa vào matcher một mảng bịa thay vì mảng của scan thì test này KHÔNG thấy
// được câu đó, nên nó chính là bằng chứng đường dẫn dữ liệu là thật.
Rig rig;
ASSERT_TRUE(rig.store_ok_);
ASSERT_TRUE(rig.loaded);
// Chạy quá `timeout` (5 s) để action kết thúc — lý do chỉ đi ra ngoài khi nó KẾT THÚC.
ASSERT_TRUE(rig.handler->start(makeAction("v_tape"), robot::Time(10.0)));
EXPECT_EQ(rig.feed(vPlate(1.20, M_PI / 4.0, 0.30), 60), ActionStatus::kFailed);
EXPECT_NE(rig.last_message.find("no intensity data"), std::string::npos) << rig.last_message;
}
TEST(DockPatternAction, ATapeProfileRefusesAPlateThatCarriesNoTape)
{
// Tấm đúng hình hoàn hảo, scan CÓ mang cường độ, nhưng không dải nào sáng. Đây là ca ngoài kho:
// hai pallet kê vuông góc dựng ra đúng hình cái sạc.
Rig rig;
ASSERT_TRUE(rig.store_ok_);
ASSERT_TRUE(rig.loaded);
// Dải nằm ngoài tấm nên không tia nào trúng — scan vẫn có mảng `intensities`, toàn 0.
const std::vector<Segment> elsewhere = { { 5.0, 5.0, 5.1, 5.1 } };
ASSERT_TRUE(rig.handler->start(makeAction("v_tape"), robot::Time(10.0)));
EXPECT_EQ(rig.feedWithTape(vPlate(1.20, M_PI / 4.0, 0.30), elsewhere, 60), ActionStatus::kFailed);
EXPECT_NE(rig.last_message.find("reflective tape"), std::string::npos) << rig.last_message;
}
TEST(DockPatternAction, RefusesADefaultProfileThatDoesNotExist)
{
Rig rig("actions_dock_bad_default");