Files
dock_pattern_action/README.md
duongtd 8269e20576 Run the dock detector as an action, from raw scan to goal frame
This is the layer that turns the geometry into something the robot actually
runs. It pulls a raw scan from the shared store, extracts lines, matches the
dock pattern, transforms into the global frame and writes the result as a
static transform - the same output contract FrameSampler has, so the charge
sequence keeps working and nothing in move_base2, dock_planner or
mission_adapters changes. Swapping detectors is one word in the YAML.

It is also the only place in the chain allowed to know about TF, actions and
the sensor store. The three packages beneath it stay free of all that, which
is what lets their tests build a world, cast rays and finish in a millisecond
with no runtime around them.

Three things only break once the pieces are joined, and no core test can see
them:

The scan cursor. A 30 Hz control loop reading a 29 Hz lidar ticks more often
than scans arrive. Without the cursor the same scan counts as several samples,
min_samples stops meaning anything, and the handler commits on one measurement.

TF arriving after the scan. The two come from different sources, so TF running
a few milliseconds behind is routine. The store's cursor has already moved past
that scan, so dropping the sample loses a detection outright every time TF is
late. The detection is now held and transformed on a later tick, without
re-running the match - the shape was already found, only the transform was
missing. The regression test fails without that, which is how it earned its
place.

Transforming at the scan's own stamp rather than at the latest. The pose just
computed belongs to the moment the scan was taken; looking up at another moment
mixes two instants into one measurement. The error from "latest" is small but
nobody knows how small, and a stamp on the wrong clock shows up exactly here.

Rejections happen at boot, not at detection time: no sensor store, a scan_topic
the store does not carry (listing the ones it does - a mistyped topic looks
exactly like a dead sensor), no profiles, a pattern that is not implemented,
a default_profile that does not exist. Every profile is validated, not just the
default one, because a typo in the fifth profile that only surfaces at four in
the afternoon is the worst kind.

An order asking for a profile that does not exist fails and says which ones do.
It does not quietly fall back to the default: using the wrong geometry means
docking in the wrong place, not being slightly slower.

The timeout message says which gate failed. Nothing matched, matched too
rarely, or matched but never settled need opposite responses, and one failure
line cannot carry that.
2026-08-26 13:03:22 +07:00

112 lines
5.6 KiB
Markdown

# dock_pattern_action
Handler dò trạm dock từ **scan LiDAR thô**, nạp qua Boost.DLL như mọi action handler khác.
Đây là tầng **adapter**, và là chỗ duy nhất trong chuỗi dò được phép biết tới TF, action và kho cảm
biến.
## Chuỗi bốn tầng
```
robot_sensor_ports scan THÔ (ranges + intensities), cursor theo sequence
v
robot_line_extraction ranges -> đoạn thẳng
v
dock_pattern_detector đoạn + profile -> pose đích trong FRAME CỦA SCAN
v
gói này TF sang global -> PoseSampleFilter -> ghi `dock_target`
```
Ba gói dưới **không biết** TF/action/kho là gì — nhờ vậy phần hình học test được bằng scan dựng
sẵn, chạy trong một mili giây, không cần dựng runtime.
## Thay `FrameSampler` mà không sửa gì phía dưới
Cùng hợp đồng đầu ra: ghi một TF **tĩnh** vào `output_frame`. Chuỗi `charge` chạy nguyên — không
phải sửa dòng nào của move_base2, dock_planner hay mission_adapters. Chỉ đổi `type:` trong
`action_handlers_params.yaml`:
```yaml
handlers:
- {name: detect, type: DockPatternActionHandler} # thay FrameSamplerActionHandler
```
Khác ở **nguồn**: FrameSampler lấy pose perception đã tính sẵn qua TF; handler này **tự tính** từ
mảng `ranges`.
## Vì sao tự tính lại chắc hơn
FrameSampler chỉ có **một** tín hiệu để lọc: các mẫu có chụm không. Handler này biết nhiều hơn hẳn
ngay tại **một** scan — số tia đỡ mỗi cánh, sai số khớp, góc kẹp lệch bao nhiêu, chiều dài cánh lệch
bao nhiêu. Nó **loại ứng viên sai tại nguồn** thay vì đợi thống kê.
Nên cổng chính chống nhầm nằm ở **matcher**, không ở bộ lọc. Bộ lọc vẫn ở đây cho thứ hạng hai:
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
**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.
**2. TF tới SAU scan.** TF và scan tới từ hai nguồn khác nhau, nên TF chậm hơn scan vài mili giây là
chuyện thường. Cursor của kho đã nhảy qua scan đó rồi, nên bỏ mẫu là **mất hẳn** một phép dò mỗi
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
đượ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**.
## Chọn profile lúc chạy
Order mang `dock_profile` trong `actionParameters`; chặng chỉ-action nhận **nguyên** params của
action gốc (`vda5050_source_adapter.cpp:380`).
Xin profile **không tồn tại** → `start()` trả false kèm danh sách tên có thật. **Không** âm thầm rơi
về default: dùng nhầm hình học là dock vào sai chỗ, không phải chậm hơn một chút.
## Từ chối lúc BOOT
Không đợi tới lúc dò:
- `ctx.sensors == nullptr` — handler này đọc scan thô, không có kho thì vô nghĩa
- `scan_topic` không có trong `sensor_store` → in luôn **danh sách topic có thật**. Gõ sai tên topic
là lỗi phổ biến nhất, và triệu chứng của nó giống hệt cảm biến chết
- không khai `profiles` nào
- `pattern` chưa hiện thực (`cylinder`, `arc`) — nhận nó là dựng một bộ dò không bao giờ tìm thấy gì
- `default_profile` trỏ vào profile không tồn tại
- **mọi** profile được validate, không chỉ profile mặc định — gõ sai ở profile thứ năm mà bốn giờ
chiều mới lộ là kiểu lỗi tệ nhất
## Khi hết giờ, thông điệp nói hỏng ở CỔNG NÀO
Ba ca khác nhau, ba cách xử trí khác nhau:
| thông điệp | nghĩa là |
|---|---|
| `no '<profile>' pattern on '<topic>' — <lý do>` | chưa lần nào khớp; lý do từ matcher chỉ ra cổng nào chặn |
| `detected the pattern only N time(s)` | có khớp nhưng quá thưa so với `sample_window` — lỗi **tần số/cấu hình**, nới `max_spread_*` vô ích |
| `detections unstable (best spread ...)` | khớp đủ nhưng không chụm; log kèm cửa sổ **tốt nhất** từng thấy |
## Config
Xem `test/config/action_handlers_params.yaml`. Nhóm khoá:
- định tuyến: `action_types`, `output_frame`, `scan_topic`
- 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**
- `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
## Build và test
```bash
catkin_make --pkg dock_pattern_action
./devel/lib/dock_pattern_action/dock_pattern_action_test # 16 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.