feat: IntrusionDetector plugin and EnvContext host services

- env_context.h: non-owning TF buffer, sensor reader and frame names;
  host calls setContext() after initialize()
- IntrusionDetector reads raw LaserScan, looks up TF at the scan stamp
  and flags new points close to the unpadded footprint; a point counts
  only if the reference scan shows that spot was free
- stall detector, env types, golden data and tests updated
This commit is contained in:
2026-11-27 22:06:59 +07:00
parent 93d3996629
commit 92b9bcc924
17 changed files with 1409 additions and 66 deletions

View File

@@ -4,7 +4,7 @@ Lõi giám sát môi trường khi robot bám plan. Mỗi control cycle host đ
tính sẵn: `cost_to_go`, `reachable`, `edt_body_min`, khoảng tiếp xúc dọc quỹ đạo), lõi trả một `EnvVerdict`: bit
"detector nào đòi recovery", lệnh phanh, bao tốc độ, và số chẩn đoán cho log/JSONL.
Lõi thuần: không costmap, không TF, không log trong cycle. Host (hiện là `move_base2`) gom feature, gọi lõi, nối kết
Lõi không phụ thuộc costmap; detector có thể dùng TF và cảm biến qua context không sở hữu. Host (hiện là `move_base2`) gom feature, gọi lõi, nối kết
quả vào state machine và trọng tài vận tốc.
## Cấu trúc
@@ -12,12 +12,13 @@ quả vào state machine và trọng tài vận tốc.
```
include/env_monitor_core/
├── env_types.h # EnvSample, EnvVerdict, EnvRecoveryOutcome — dữ liệu một cycle
├── env_context.h # TF, sensor reader và tên frame (không sở hữu)
├── env_detector.h # interface EnvDetector + EnvNeeds, EnvShared, EnvFrame, EnvDetectorOutput, EnvRouteSpec
├── env_detector_abi.h # kEnvDetectorAbiVersion + ENV_MONITOR_CORE_EXPORT_DETECTOR
├── env_monitor_config.h # tham số (phần chung + ba detector có sẵn), đọc/kiểm/mô tả, toYaml
└── env_monitor.h # host EnvMonitor: phần chung, nạp và gộp detector
src/ # host + config
plugins/ # ba detector có sẵn, mỗi cái một .so
plugins/ # các detector có sẵn, mỗi cái một .so
test/ # env_monitor_test (66), env_monitor_golden_test, env_monitor_loading_test
```
@@ -28,14 +29,16 @@ verdict là một `EnvDetector` nạp bằng boost::dll:
| Detector | Phát hiện | Route mặc định | Cạn route |
|---|---|---|---|
| `IntrusionDetector` | điểm mới sát mép thân không padding | `intrusion` | `lock` |
| `ContactDetector` | thân sắp chạm trên quỹ đạo thật; phanh, bao tốc độ | `collision` | `lock` |
| `StallDetector` | lệnh đi mà robot không dịch chuyển / không tới gần lối ra | `oscillation` | `abort` |
| `UnreachableDetector` | không còn đường trong cửa sổ tới điểm thoát | `path_blocked` | `lock` |
Mỗi cycle host gọi mọi detector theo thứ tự `env_monitor/detectors`, rồi gộp: bit `fired_mask` theo từng detector,
`brake` OR, `v_cap` min, `event` của detector sau ghi đè detector trước. `brake_applied` / `v_cap_applied` chỉ lấy
từ detector có `apply_command`. Detector không gọi port nào: host chỉ lấy feature mà ít nhất một detector cần
(`needs()`), mỗi cycle một lần.
từ detector có `apply_command`. Host gọi `setContext()` sau `initialize()` và trước `update()`; TF buffer và sensor
reader phải sống lâu hơn monitor. Host chỉ lấy feature mà ít nhất một detector cần (`needs()`), mỗi cycle một lần;
`IntrusionDetector` tự đọc scan và tra TF qua context.
## YAML
@@ -89,3 +92,32 @@ env -u PNKX_NAV_CORE_CONFIG_DIR -u PNKX_NAV_CORE_LIBRARY_PATH ./devel/lib/env_mo
(`ENV_MONITOR_CORE_GOLDEN_RECORD=1`) để test xanh.
Nhánh standalone của `pnkx_nav_core` cần `add_subdirectory` gói này trong `CMakeLists.txt` gốc, trước `move_base2`.
## IntrusionDetector
`IntrusionDetector` tự đọc `LaserScan` thô từ sensor reader và tra TF tại stamp scan; host cấp footprint không
padding trong base frame. Pose base lấy từ TF mới nhất. Các pose là biến đổi tf3 đầy đủ, hỗ trợ lidar lắp úp. Mỗi topic giữ lịch sử riêng: điểm nằm ngoài thân, cách mép
không quá `zone_distance`, chỉ được tính là mới nếu cả ba tia lân cận của scan tham chiếu chứng minh vị trí đó từng
trống. Vì thế tia bị che, NaN hoặc thiếu không tạo bằng chứng mới. Mỗi topic có bộ đếm riêng, không gộp điểm giữa
các lidar; khi một topic có đủ `min_points` trong `confirm_scans` scan liên tiếp, `intrusion_detected` bật. `action: recover` mới phát cờ recovery; detector không phanh hoặc
kẹp tốc độ. Route mặc định là `intrusion`, chế độ `step`, hết lượt thì `lock`.
| Khoá dưới `env_monitor/intrusion` | Mặc định | Ý nghĩa |
|---|---:|---|
| `enabled` | `true` | Bật detector. |
| `action` | `log` | `log` hoặc `recover`. |
| `topics` | `[/f_scan, /b_scan]` | Danh sách topic scan, không rỗng. |
| `zone_distance` | `0.10` | [m] Khoảng cách tối đa từ mép thân, thuộc `(0, 0.5]`. |
| `compare_age` | `0.3` | [s] Tuổi tối thiểu của scan tham chiếu, lớn hơn 0. |
| `free_margin` | `0.05` | [m] Khoảng trống vượt sau điểm, không âm. |
| `min_points` | `3` | Số điểm mới tối thiểu trong một scan, ít nhất 1. |
| `confirm_scans` | `2` | Số scan liên tiếp của cùng topic có đủ điểm, ít nhất 1. |
| `history_max_age` | `1.0` | [s] Thời gian giữ scan mỗi topic, lớn hơn `compare_age`. |
| `max_scan_age` | `0.3` | [s] Độ lệch thời gian scan so với hiện tại tối đa, lớn hơn 0. |
| `max_tf_gap` | `0.1` | [s] Khi TF tại stamp không có, chỉ nhận TF mới nhất nếu lệch không quá ngưỡng này; lớn hơn 0. |
| `max_check_speed` | `0.15` | [m/s] Chỉ xét cặp scan khi lidar tự đi chậm hơn mức này; chạy nhanh hơn thì cặp đó không tính và chuỗi xác nhận về 0. Lớn hơn 0. |
| `max_check_yaw_rate` | `0.3` | [rad/s] Như trên cho tốc độ quay. Lớn hơn 0. |
| `debug/events` | `true` | Log một dòng ở sườn phát hiện, gồm số điểm, phía và khoảng cách. |
Khai báo `{name: intrusion, type: IntrusionDetector}` trong danh sách detector, và `IntrusionDetector/library_path` là
`libenv_monitor_core_intrusion_detector`. Cần định nghĩa route `intrusion` trong host khi dùng `action: recover`.