Files
dock_pattern_detector/README.md
duongtd a694d5abe2 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.
2026-08-26 12:38:29 +07:00

106 lines
5.7 KiB
Markdown

# dock_pattern_detector
Tìm **biên dạng của trạm dock** trong một scan LiDAR và trả về **pose robot phải đạt**.
## Ba tầng, gói này là tầng giữa
```
robot_sensor_ports scan THÔ (ranges + intensities)
v
robot_line_extraction ranges -> đoạn thẳng
v
dock_pattern_detector đoạn thẳng + profile -> pose đích trong FRAME CỦA SCAN <-- gói này
v
(adapter) TF sang global -> ghi `dock_target`
```
Lõi này **không biết** TF, action, hay kho cảm biến là gì. Nên test được bằng scan dựng sẵn: dựng
hình trước, chiếu tia sau, đáp án biết trước và sai lệch đo được bằng số.
## Đầu ra là pose ĐÍCH, không phải pose cái sạc
`Detection::x/y/yaw` = **đỉnh hình ⊕ offset của profile**. Đó là chỗ `base_link` phải dừng.
Offset nằm ở đây chứ không ở planner vì `dock_planner` **cố ý không có khoá standoff nào** — nó suy
mép robot áp dock từ *phía robot đang đứng so với goal*, và suy được là nhờ goal ĐÃ là pose robot
phải đạt. Khoảng lùi cũng là thuộc tính của *cái sạc* (độ sâu chấu cắm), nên nó thuộc về profile.
`apex_x/apex_y` giữ lại đỉnh **trước** offset, để chẩn đoán: lệch offset và lệch dò trông giống hệt
nhau nếu chỉ nhìn pose cuối.
## Chữ V — 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 đoạn *kết thúc* tại đó.
3. **Góc trong tại đỉnh** — đ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 trùng nhau ở đúng 90 độ và lệch nhau ở mọi góc khác, nên nhầm là sai âm thầm.
4. **Chiều dài mỗi cánh** so với `wing_length`. Đây là cổng chặn **góc pallet**: pallet có đúng 90
độ, chỉ khác chiều dài cạnh.
5. **Cổng tìm kiếm** — `search_radius` quanh pose kỳ vọng, `search_fov` quanh trục x.
### Hướng: chọn dấu theo cảm biến, không theo thứ tự cánh
`yaw` là phân giác **hướng ra phía cảm biến**. Dấu chọn bằng cách so với vector từ đỉnh về gốc cảm
biến. Chọn theo thứ tự hai cánh thì thứ tự đổi theo phía robot đứng — và lệch 180 độ ở đây nghĩa là
**robot lùi vào tấm sạc thay vì tiến vào**.
### Điểm tin cậy lấy trục TỆ NHẤT
`confidence` = min(khớp góc, khớp cánh 1, khớp cánh 2, số tia đỡ). Không phải trung bình: trung bình
cho một hình sai hẳn góc nhưng đúng chiều dài lọt qua bằng điểm của hai tiêu chí kia.
## Vì sao cổng prior là bắt buộc ngoài hiện trường
Trong kho, **hai pallet vuông góc bất kỳ cũng là một chữ V**. `search_radius`/`search_fov` là cổng
chính chống dương tính giả. `has_prior: false` chấp nhận được trong sim; trên robot thì không.
`candidate_count` đếm số ứng viên vượt hết mọi cổng. `> 1` là tín hiệu cho tầng trên: trong tầm có
nhiều hình giống nhau, và prior mới là thứ quyết định lấy cái nào.
## Khi không dò ra, `reason` nói rõ chuyện gì
Không phải một dòng "detection failed". Sáu lý do, xếp theo mức cụ thể:
| `reason` chứa | nghĩa là | xử trí |
|---|---|---|
| `need 2 line segments` | chỉ thấy một mặt | một cánh bị che, hoặc đứng lệch |
| `do not end at the crossing` | hai tường cắt nhau ở xa | không phải trạm; đây là cổng chống nhầm |
| `wrong angle` | có đỉnh, sai góc mở | sai profile, hoặc vật thể khác |
| `wrong wing length` | đúng góc, sai chiều dài | **rất có thể là góc pallet** |
| `outside the search gate` | hình đúng, sai chỗ | trạm của người khác, hoặc prior sai |
| `too few supporting rays` | ở quá xa | tiến lại gần, hoặc hạ `min_wing_points` |
## Bốn dạng, hiện có một
| `pattern` | trạng thái | cần thêm gì |
|---|---|---|
| `v` | **chạy được** | — |
| `recess` | chưa | chỉ cần đoạn thẳng, lõi đã có |
| `cylinder` | chưa | lõi khớp đường tròn |
| `arc` | chưa | lõi khớp cung |
Khai một `pattern` chưa hiện thực thì `configure()` **từ chối lúc nạp** kèm lý do, thay vì nhận
config rồi không bao giờ dò ra gì — hai thứ đó nhìn giống hệt nhau từ bên ngoài.
## Mọi số trong profile phải là số ĐO
`vertex_angle`, `wing_length`, `offset_x` — đoán một con số ở đây 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. Giá trị mặc định trong code chỉ để test chạy, **không phải** giá
trị cho trạm thật.
## Build và test
```bash
catkin_make --pkg dock_pattern_detector
./devel/lib/dock_pattern_detector/v_pattern_matcher_test # 16 test
```
Test chạy đúng chuỗi mà handler sẽ chạy: dựng thế giới → chiếu tia → `robot_line_extraction` →
matcher. Nhóm **từ chối** quan trọng hơn nhóm dò đúng, và mỗi ca từ chối khẳng định luôn *lý do* —
một test chỉ kiểm `!valid` sẽ pass cả khi extractor không ra đoạn nào, tức matcher chưa hề bị thử.