Find a dock plate in a laser scan and say where the robot must stop

The detection chain had a gap between line segments and a navigation goal.
This is that step: segments plus the geometry of one charger, out comes the
pose base_link has to reach, in the scan's own frame.

Pure geometry, no TF and no action, so it is testable the same way the line
extractor is - build the world, cast the rays, and the answer is known before
the code runs. The tests exercise the real chain end to end, from synthetic
world through robot_line_extraction into the matcher.

The output is the goal pose, not the charger's pose. dock_planner deliberately
carries no standoff key: it derives which edge of the robot meets the dock from
which side of the goal the robot stands on, and that only works because the
goal already is where base_link must end up. The standoff is also a property of
the charger - how deep its contacts sit - so it belongs to the profile.
apex_x/apex_y keep the pre-offset vertex, because a wrong offset and a wrong
detection look identical from the final pose alone.

Matching a V:

The vertex is the intersection of the two fitted lines, not the midpoint of
their endpoints. A line is a least-squares fit over tens of rays; an endpoint
is one ray and much noisier.

Both segments must END near that intersection. Any two non-parallel walls cross
somewhere, and without this gate a corridor corner reads as a dock.

The vertex angle is measured between the two wing vectors from the apex, not
between the two normals. The two agree at exactly 90 degrees and diverge
everywhere else, so confusing them is a silent error at every other opening.

Wing length is what rejects a pallet corner: a pallet has a perfect right
angle, it just has metre-long sides.

Yaw is the bisector pointing back at the sensor, and its sign is chosen by
comparing against the sensor position rather than by the order of the two
wings. That order flips with which side the robot approaches from, and being
180 degrees out here means reversing into the plate instead of driving in.

Confidence is the worst of the three criteria, not their mean. A mean lets a
shape with a badly wrong angle through on the strength of the other two.

The search gate is not optional in a warehouse: any two perpendicular pallets
are a V. candidate_count reports how many survived every gate, so an ambiguous
scene is visible to the layer above rather than silently resolved.

A failed detection reports which gate it failed. "Nothing there" and "something
there but the wrong length" need opposite responses, and one failure line
cannot carry that.

Declaring a pattern that is not implemented is refused at configure time.
Accepting it would produce a detector that never finds anything, which looks
exactly like a charger out of range.
This commit is contained in:
duongtd
2026-08-26 12:38:29 +07:00
commit a694d5abe2
8 changed files with 1482 additions and 0 deletions

View File

@@ -0,0 +1,174 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* dock_pattern_detector — hình học của MỘT loại trạm dock, và kết quả một lượt khớp.
*
* Author: DuongTD
*********************************************************************/
#ifndef DOCK_PATTERN_DETECTOR_DOCK_PROFILE_H_
#define DOCK_PATTERN_DETECTOR_DOCK_PROFILE_H_
#include <cmath>
#include <cstddef>
#include <string>
namespace dock_pattern_detector
{
/**
* @enum PatternType
* @brief Biên dạng của mục tiêu dock.
*
* Mỗi giá trị là một matcher riêng. Thêm dạng mới = thêm một giá trị ở đây + một matcher, không
* đụng handler và không đụng lõi khớp đường thẳng.
*/
enum class PatternType
{
kV, ///< Hai cánh gặp nhau tại một đỉnh. Tấm dẫn hướng chữ V.
kRecess, ///< Mặt phẳng có rãnh/gờ vuông góc: ba đoạn, hai góc ~90 độ.
kCylinder, ///< Trụ tròn. CẦN lõi khớp đường tròn — chưa hiện thực.
kArc ///< Cung tròn hở. CẦN lõi khớp cung — chưa hiện thực.
};
const char* toString(PatternType pattern);
/// @brief Đọc tên dạng từ YAML. @return false nếu tên không có thật.
bool patternFromString(const std::string& name, PatternType& out, std::string& error);
/**
* @struct DockProfile
* @brief Hình học và offset của MỘT cái sạc.
*
* Một profile cho một loại trạm. Order chọn profile lúc chạy qua `actionParameters`, nên hai trạm
* khác hình dùng chung một handler.
*
* @warning Mọi số ở đây phải là số **ĐO** trên trạm thật. Đoán một `vertex_angle` là dò trúng một
* hình không tồn tại, hoặc trượt cái hình có thật.
*/
struct DockProfile
{
PatternType pattern = PatternType::kV;
// --- Hình chữ V --------------------------------------------------------------------------------
/**
* [rad] Góc TRONG tại đỉnh, giữa hai cánh. Tấm mở 90 độ thì đặt 1.5708.
*
* Đo giữa hai vector cánh xuất phát từ đỉnh, không phải giữa hai pháp tuyến — hai cách đo trùng
* nhau ở đúng 90 độ và lệch nhau ở mọi góc khác, nên nhầm là sai âm thầm.
*/
double vertex_angle = M_PI / 2.0;
/// [m] Chiều dài MỘT cánh, từ đỉnh ra đầu mút.
double wing_length = 0.20;
/// [rad] Sai lệch góc đỉnh còn chấp nhận.
double angle_tolerance = 0.17;
/// [m] Sai lệch chiều dài cánh còn chấp nhận.
double length_tolerance = 0.05;
/**
* [m] Hai đầu cánh phải nằm trong ngần này quanh giao điểm hai đường.
*
* Cổng này phân biệt một chữ V thật với hai bức tường **cắt nhau ở xa**: hai đường bất kỳ không
* song song đều có giao điểm, nhưng chỉ chữ V mới có hai đầu đoạn nằm ngay tại đó.
*/
double vertex_gap = 0.05;
// --- Cổng tìm kiếm -----------------------------------------------------------------------------
/**
* Có pose kỳ vọng của trạm hay không (trong frame của scan).
*
* `false` = tìm khắp scan. Chấp nhận được trong sim; ngoài kho thì hai pallet vuông góc bất kỳ
* cũng là một chữ V, nên đây là cổng chính chống dương tính giả.
*/
bool has_prior = false;
double prior_x = 0.0; ///< [m] trong frame của scan
double prior_y = 0.0; ///< [m]
/// [m] Ứng viên cách pose kỳ vọng xa hơn ngần này thì loại. Chỉ dùng khi `has_prior`.
double search_radius = 1.5;
/// [rad] Nửa góc quạt quanh trục x của cảm biến. <= 0 = không giới hạn quạt.
double search_fov = 0.0;
// --- Offset đầu ra -----------------------------------------------------------------------------
/**
* [m] Lùi từ ĐỈNH hình ra pose robot phải đạt, dọc pháp tuyến hướng RA phía robot.
*
* Offset nằm ở đây chứ không ở planner: `dock_planner` cố ý không có khoá standoff nào và suy mép
* robot áp dock từ phía robot đứng so với goal — suy được là nhờ goal ĐÃ là pose robot phải đạt.
* Và khoảng lùi là thuộc tính của *cái sạc* (độ sâu chấu cắm), nên nó thuộc về profile.
*/
double offset_x = 0.0;
/// [m] Bù tấm lắp lệch NGANG so với đầu cắm. Trong hệ của chính hình, y sang trái.
double offset_y = 0.0;
/// [rad] Bù tấm lắp XOAY so với đầu cắm.
double offset_yaw = 0.0;
// --- Cổng chất lượng ---------------------------------------------------------------------------
/// Số tia đỡ tối thiểu cho MỘT cánh. Dưới ngưỡng này phép khớp không còn nghĩa thống kê.
std::size_t min_wing_points = 5;
/**
* [-] Độ tin tối thiểu để nhận ứng viên, trong [0, 1].
*
* Điểm tin tổng hợp mức khớp góc, chiều dài và số tia đỡ. Nâng lên là khắt khe hơn với hình méo.
*/
double min_confidence = 0.5;
/**
* @brief Kiểm miền giá trị.
* @param[out] error Khoá nào sai và vì sao.
*
* Kiểm lúc nạp config chứ không phải lúc dò: một profile không thể khớp được sẽ biểu hiện thành
* "dò mãi không ra", nhìn giống hệt trạm không có trong tầm.
*/
bool validate(std::string& error) const;
};
/**
* @struct Detection
* @brief Kết quả một lượt khớp, trong **frame của scan**.
*
* Quy sang global frame là việc của tầng adapter — lõi này không biết TF là gì.
*/
struct Detection
{
bool valid = false;
/// Pose ĐÍCH: đỉnh hình đã cộng offset của profile. Đây là thứ đi ra `dock_target`.
double x = 0.0; // [m]
double y = 0.0; // [m]
double yaw = 0.0; // [rad]
/// Đỉnh hình TRƯỚC offset, giữ lại để chẩn đoán — lệch offset và lệch dò trông giống nhau.
double apex_x = 0.0; // [m]
double apex_y = 0.0; // [m]
/// [-] Trong [0, 1]. 1 = khớp hoàn hảo cả góc, chiều dài lẫn số tia đỡ.
double confidence = 0.0;
/// [rad] Góc đỉnh đo được. So với `vertex_angle` của profile để biết hình méo cỡ nào.
double measured_vertex_angle = 0.0;
/// [m] Chiều dài hai cánh đo được.
double measured_wing_length[2] = { 0.0, 0.0 };
/// Số ứng viên vượt hết mọi cổng. > 1 nghĩa là trong tầm có nhiều hình giống nhau.
std::size_t candidate_count = 0;
/// Vì sao không nhận, khi `valid == false`. Một câu người đọc hiểu được, không phải mã lỗi.
std::string reason;
};
} // namespace dock_pattern_detector
#endif // DOCK_PATTERN_DETECTOR_DOCK_PROFILE_H_

View File

@@ -0,0 +1,80 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* dock_pattern_detector — tìm tấm dẫn hướng chữ V trong một tập đoạn thẳng.
*
* Author: DuongTD
*********************************************************************/
#ifndef DOCK_PATTERN_DETECTOR_V_PATTERN_MATCHER_H_
#define DOCK_PATTERN_DETECTOR_V_PATTERN_MATCHER_H_
#include <string>
#include <vector>
#include <robot_line_extraction/line.h>
#include <dock_pattern_detector/dock_profile.h>
namespace dock_pattern_detector
{
/**
* @class VPatternMatcher
* @brief Ghép hai đoạn thẳng thành một chữ V và trả về pose đích.
*
* Hình học thuần. Đầu vào là danh sách đoạn của `robot_line_extraction`, đầu ra là một pose trong
* **frame của scan**. Không TF, không action, không kho cảm biến — nên test được bằng scan dựng
* sẵn, biết trước đáp án.
*
* ## Luật khớp
*
* Duyệt mọi cặp đoạn. Với mỗi cặp:
*
* 1. **Đỉnh = giao điểm hai ĐƯỜNG đã khớp**, không phải trung điểm hai đầu mút. Đường là kết quả
* bình phương tối thiểu trên hàng chục tia; đầu mút là một tia đơn lẻ và nhiễu hơn hẳn.
* 2. **Hai đầu đoạn phải nằm sát đỉnh** (`vertex_gap`). Đây là cổng phân biệt chữ V thật với hai
* bức tường cắt nhau ở xa: hai đường không song song nào cũng có giao điểm, nhưng chỉ chữ V mới
* có hai đầu đoạn nằm ngay tại đó.
* 3. **Góc trong tại đỉnh** đo giữa hai vector cánh xuất phát từ đỉnh, so với `vertex_angle`.
* 4. **Chiều dài mỗi cánh** so với `wing_length`.
* 5. **Cổng prior** — ứng viên ra ngoài `search_radius` quanh pose kỳ vọng, hoặc ngoài quạt
* `search_fov`, thì loại. Không có cổng này thì trong kho, hai pallet vuông góc bất kỳ cũng là
* một chữ V.
*
* ## Hướng
*
* `yaw` là **phân giác hướng RA phía cảm biến**. Chọn dấu bằng cách so với vector từ đỉnh về gốc
* cảm biến, 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 tường.
*
* @note Không thread-safe. Một matcher cho một consumer.
*/
class VPatternMatcher
{
public:
VPatternMatcher() = default;
/// @brief Nhận profile. Sai miền thì giữ bộ cũ và trả false.
bool configure(const DockProfile& profile, std::string& error);
const DockProfile& profile() const
{
return profile_;
}
/**
* @brief Tìm chữ V trong @p lines.
*
* @return Kết quả luôn hợp lệ để đọc. `valid == false` kèm `reason` nói rõ vì sao — "chưa thấy"
* và "thấy nhưng méo" là hai chuyện khác nhau, và người vận hành cần biết là chuyện nào.
*/
Detection match(const std::vector<robot_line_extraction::Line>& lines) const;
private:
DockProfile profile_;
};
} // namespace dock_pattern_detector
#endif // DOCK_PATTERN_DETECTOR_V_PATTERN_MATCHER_H_