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:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user