# 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 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 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_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 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 | |---|---|---|---| | `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. ## 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`.