Files
env_monitor_core/README.md
duongtd 92b9bcc924 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
2026-11-27 22:06:59 +07:00

124 lines
8.3 KiB
Markdown

# env_monitor_core
Lõi giám sát môi trường khi robot bám plan. Mỗi control cycle host đưa vào một `EnvSample` (feature hình học đã
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 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
```
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/ # 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
```
## Host và detector
`EnvMonitor` giữ phần chung cho mọi detector: odometer, lọc pose nhảy, grace sau plan mới, ngân sách replan. Mỗi
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`. 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
```yaml
env_monitor:
enabled: true
detectors: # thiếu khoá = contact, stall, unreachable theo đúng thứ tự này
- {name: contact, type: ContactDetector}
- {name: stall, type: StallDetector}
- {name: unreachable, type: UnreachableDetector}
contact: # tham số của detector tên `contact`
action: recover
route: collision # tên route trong recovery/routes
route_mode: step # step | sequence
on_exhausted: lock # lock | abort
...
ContactDetector:
library_path: libenv_monitor_core_contact_detector
```
- Type sai, thiếu `library_path`, thiếu hoặc lệch ABI: log LỖI, bỏ detector đó, các detector khác vẫn chạy.
- Tham số sai (của phần chung hoặc của một detector): `initialize()` trả lỗi, host từ chối khởi động.
- Host trỏ route không tồn tại: host gỡ detector (`remove()`), log LỖI.
Comment đầy đủ từng khoá (tiếng Việt) nằm trong `env_monitor_params.yaml` của cây config đang chạy.
## Viết detector mới trong gói khác
1. `package.xml`/`CMakeLists.txt`: depend `env_monitor_core`, build một `SHARED` library link `env_monitor_core`.
2. Hiện thực `env_monitor_core::EnvDetector` (`initialize`, `describe`, `armed`, `defaultRoute`, `needs`, `reset`,
`update`; tuỳ chọn `appliesCommand`, `onPlanApplied`, `onRecoveryFinished`). Ghi vào `EnvDetectorOutput` phần được
gộp, và chỉ các trường chẩn đoán của chính mình vào `EnvVerdict`.
3. Export: `ENV_MONITOR_CORE_EXPORT_DETECTOR(MyDetector::create, MyDetector)` — sinh `MyDetector` và `MyDetector_abi`.
4. YAML: thêm `{name: ..., type: MyDetector}` vào `detectors`, khối tham số cùng `name`, `MyDetector/library_path`, và
route tương ứng trong `recovery/routes`.
Đổi chữ ký virtual hoặc layout các struct trong `env_detector.h` / `env_types.h` thì phải tăng `kEnvDetectorAbiVersion`.
## Build và test
```bash
cd /home/duongtd/T800_ws
catkin_make --pkg env_monitor_core -DBUILD_ENV_MONITOR_CORE_TESTS=ON
env -u PNKX_NAV_CORE_CONFIG_DIR -u PNKX_NAV_CORE_LIBRARY_PATH ./devel/lib/env_monitor_core/env_monitor_golden_test
env -u PNKX_NAV_CORE_CONFIG_DIR -u PNKX_NAV_CORE_LIBRARY_PATH ./devel/lib/env_monitor_core/env_monitor_test
env -u PNKX_NAV_CORE_CONFIG_DIR -u PNKX_NAV_CORE_LIBRARY_PATH ./devel/lib/env_monitor_core/env_monitor_loading_test
```
`env_monitor_golden_test` so verdict từng cycle của 24 kịch bản với `test/data/env_monitor_golden.txt`, ghi ngày
26/11/2026 từ bản EnvMonitor một khối cũ (trong `move_base2`). Lệch ở đây là đổi hành vi: sửa code, đừng ghi lại file
(`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`.