feat: EnvMonitor thay PathMonitor — giám sát môi trường bằng EDT/geodesic/quỹ đạo thật

Bộ giám sát mới trong ControlLoop (core/env_monitor, core/grid_analysis, ports/environment_port,
io/env_logger), thay bốn phép đo của PathMonitor (code cũ còn, tắt bằng path_monitor/enabled: false):

- kContact: dò tiếp xúc dọc quỹ đạo local planner (thay cung v/omega), bao tốc độ sqrt(2a(d - margin))
  có dư, phanh chỉ khi vượt bao, cờ collision khi kề mũi tĩnh 1 s; cổng xoay tại chỗ theo lệnh
  controller muốn; hysteresis + xác nhận khi nhả; đứng ra sau detour không dịch robot (nhường
  controller); giữ phanh khi route khoá thì báo no valid command để controller_patience leo thang.
- kStall: cost_to_go = geodesic tới điểm thoát 2 m dọc plan + phần plan còn lại, index plan bám cửa
  sổ, bộ lọc lật liên thông; cờ no_progress chỉ khi cổng dịch chuyển thực mở (robot không đi được
  đâu trong 12 s dù lệnh >= 1,5 m) — thay ngân sách replan cho stall.
- kUnreachable: liên thông tới điểm thoát, đang ở action log.
- EnvLogger ghi JSONL mỗi tick nặng; config/runtime/env_monitor_params.yaml đủ khoá, comment tiếng
  Việt; ControllerPort::measuredVelocity; FakeEnvironmentPort cho test.

Sim 5–6/9: hết churn detour, khe 1,25 m đi qua 0,5 m/s, lượt 6/9 10:15 2 detour/142 s không oan.
Test: grid_analysis 31, env_monitor 43, plan_inspector 37, walking_skeleton 87, các bộ khác pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017xXjFjr2DQ6rhmhHSW4fk4
This commit is contained in:
2026-09-06 10:30:31 +07:00
parent f2097380e4
commit 261cc20ad4
26 changed files with 5589 additions and 7 deletions

View File

@@ -0,0 +1,77 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — ghi feature + verdict của EnvMonitor ra JSONL (Test/ENV_MONITOR_PLAN.md §2.6).
*
* Mục đích: replay offline để chỉnh ngưỡng không cần sim, và là dữ liệu huấn luyện ML sau này. Mỗi
* dòng là một tick nặng (5 Hz) hoặc một sườn sự kiện. Tắt mặc định; khi tắt không cấp phát, không mở
* file.
*
* Ghi trên control thread: dòng ~400 byte, gom vào buffer và flush mỗi `flush_period` — một lần
* `write()` vài kB mỗi giây. Nếu đo thấy ảnh hưởng chu kỳ thì chuyển sang thread riêng.
*
* Author: DuongTD
*********************************************************************/
#ifndef MOVE_BASE2_IO_ENV_LOGGER_H_
#define MOVE_BASE2_IO_ENV_LOGGER_H_
#include <cstdint>
#include <fstream>
#include <string>
#include <robot/time.h>
#include <move_base2/core/env_monitor.h>
#include <move_base2/ports/environment_port.h>
namespace move_base2
{
struct EnvLoggerConfig
{
bool enabled = false;
std::string log_dir = "/tmp/env_monitor"; ///< tạo nếu chưa có; file `envmon_<epoch>.jsonl`.
double flush_period = 1.0; ///< [s]
bool validate(std::string& error) const;
};
class EnvLogger
{
public:
EnvLogger() = default;
~EnvLogger();
EnvLogger(const EnvLogger&) = delete;
EnvLogger& operator=(const EnvLogger&) = delete;
/// @return false kèm lý do nếu không mở được file. Không mở gì khi `config.enabled == false`.
bool open(const EnvLoggerConfig& config, std::string& error);
bool isOpen() const
{
return out_.is_open();
}
const std::string& path() const
{
return path_;
}
/// @brief Một dòng JSON. @p geometry có thể null (tick nhẹ). @p state là tên state machine.
void record(const robot::Time& now, const EnvSample& sample, const EnvVerdict& verdict,
const EnvGeometry* geometry, const char* state, std::uint64_t mission_id,
bool used_local_plan);
void flush();
private:
EnvLoggerConfig config_;
std::ofstream out_;
std::string path_;
std::string buffer_;
robot::Time last_flush_;
bool has_last_flush_ = false;
};
} // namespace move_base2
#endif // MOVE_BASE2_IO_ENV_LOGGER_H_