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:
205
src/io/env_logger.cpp
Normal file
205
src/io/env_logger.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* move_base2 — cài đặt EnvLogger.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <move_base2/io/env_logger.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
namespace
|
||||
{
|
||||
|
||||
/// JSON không có NaN/Inf: ghi null.
|
||||
void appendNumber(std::string& out, double v)
|
||||
{
|
||||
char buf[32];
|
||||
if (std::isfinite(v))
|
||||
{
|
||||
std::snprintf(buf, sizeof(buf), "%.4f", v);
|
||||
out += buf;
|
||||
}
|
||||
else
|
||||
{
|
||||
out += "null";
|
||||
}
|
||||
}
|
||||
|
||||
void appendField(std::string& out, const char* key, double v)
|
||||
{
|
||||
out += '"';
|
||||
out += key;
|
||||
out += "\":";
|
||||
appendNumber(out, v);
|
||||
out += ',';
|
||||
}
|
||||
|
||||
void appendField(std::string& out, const char* key, bool v)
|
||||
{
|
||||
out += '"';
|
||||
out += key;
|
||||
out += "\":";
|
||||
out += v ? "true" : "false";
|
||||
out += ',';
|
||||
}
|
||||
|
||||
void appendField(std::string& out, const char* key, const char* v)
|
||||
{
|
||||
out += '"';
|
||||
out += key;
|
||||
out += "\":\"";
|
||||
for (const char* p = v; *p != '\0'; ++p)
|
||||
{
|
||||
if (*p == '"' || *p == '\\')
|
||||
{
|
||||
out += '\\';
|
||||
}
|
||||
out += *p;
|
||||
}
|
||||
out += "\",";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool EnvLoggerConfig::validate(std::string& error) const
|
||||
{
|
||||
if (!enabled)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (log_dir.empty())
|
||||
{
|
||||
error = "env_monitor/logger/log_dir must not be empty when the logger is enabled";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(flush_period) || flush_period <= 0.0)
|
||||
{
|
||||
error = "env_monitor/logger/flush_period must be > 0 [s]";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
EnvLogger::~EnvLogger()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
|
||||
bool EnvLogger::open(const EnvLoggerConfig& config, std::string& error)
|
||||
{
|
||||
config_ = config;
|
||||
if (!config.enabled)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (::mkdir(config.log_dir.c_str(), 0755) != 0 && errno != EEXIST)
|
||||
{
|
||||
error = "env_monitor/logger: cannot create log_dir '" + config.log_dir + "'";
|
||||
return false;
|
||||
}
|
||||
const auto epoch =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
path_ = config.log_dir + "/envmon_" + std::to_string(epoch) + ".jsonl";
|
||||
out_.open(path_, std::ios::out | std::ios::trunc);
|
||||
if (!out_.is_open())
|
||||
{
|
||||
error = "env_monitor/logger: cannot open '" + path_ + "' for writing";
|
||||
return false;
|
||||
}
|
||||
buffer_.reserve(64 * 1024);
|
||||
return true;
|
||||
}
|
||||
|
||||
void EnvLogger::record(const robot::Time& now, const EnvSample& s, const EnvVerdict& v,
|
||||
const EnvGeometry* g, const char* state, std::uint64_t mission_id,
|
||||
bool used_local_plan)
|
||||
{
|
||||
if (!out_.is_open())
|
||||
{
|
||||
return;
|
||||
}
|
||||
std::string& o = buffer_;
|
||||
o += "{\"v\":1,";
|
||||
appendField(o, "t", now.toSec());
|
||||
o += "\"mission\":" + std::to_string(mission_id) + ",";
|
||||
appendField(o, "state", state);
|
||||
appendField(o, "heavy", s.geometry_valid);
|
||||
appendField(o, "x", s.robot_x);
|
||||
appendField(o, "y", s.robot_y);
|
||||
appendField(o, "v_cmd", s.v_cmd);
|
||||
appendField(o, "w_cmd", s.w_cmd);
|
||||
appendField(o, "v_meas", s.v_meas);
|
||||
appendField(o, "v_controller", s.v_controller);
|
||||
appendField(o, "d_goal", s.d_goal);
|
||||
appendField(o, "costmap_current", s.costmap_current);
|
||||
appendField(o, "controller_valid", s.controller_valid);
|
||||
appendField(o, "cost_to_go", s.cost_conclusive && s.reachable ? s.cost_to_go : std::numeric_limits<double>::quiet_NaN());
|
||||
appendField(o, "cost_conclusive", s.cost_conclusive);
|
||||
appendField(o, "reachable", s.reachable);
|
||||
appendField(o, "edt_body_min", s.edt_body_min);
|
||||
appendField(o, "contact_conclusive", s.contact_conclusive);
|
||||
appendField(o, "traj_contact_dist", s.traj_contact_dist);
|
||||
appendField(o, "used_local_plan", used_local_plan);
|
||||
if (g != nullptr && g->valid)
|
||||
{
|
||||
appendField(o, "geo_to_exit", g->cost.geo_to_exit);
|
||||
appendField(o, "arc_rest", g->cost.arc_rest);
|
||||
appendField(o, "target_kind", static_cast<double>(static_cast<int>(g->cost.target)));
|
||||
appendField(o, "cells_visited", static_cast<double>(g->cost.cells_visited));
|
||||
appendField(o, "robot_in_ring", g->cost.robot_in_ring);
|
||||
appendField(o, "plan_clear_ahead", g->corridor.plan_clear_ahead);
|
||||
appendField(o, "corridor_min_width", g->corridor.min_width);
|
||||
appendField(o, "corridor_block_dist", g->corridor.block_dist);
|
||||
}
|
||||
appendField(o, "eff_geo", v.eff_geo);
|
||||
appendField(o, "cmd_window", v.cmd_window);
|
||||
appendField(o, "brake_dist", v.brake_dist);
|
||||
appendField(o, "v_cap", v.v_cap);
|
||||
appendField(o, "stall_detected", v.stall_detected);
|
||||
appendField(o, "stall_muted", v.stall_muted);
|
||||
appendField(o, "unreachable_detected", v.unreachable_detected);
|
||||
appendField(o, "unreachable_muted", v.unreachable_muted);
|
||||
appendField(o, "contact_imminent", v.contact_imminent);
|
||||
appendField(o, "brake", v.brake);
|
||||
appendField(o, "contact_standdown", v.contact_standdown);
|
||||
appendField(o, "cost_jump", v.cost_jump);
|
||||
appendField(o, "no_progress", v.no_progress);
|
||||
appendField(o, "path_blocked", v.path_blocked);
|
||||
appendField(o, "collision", v.collision);
|
||||
appendField(o, "replan_budget_left", v.replan_budget_left);
|
||||
appendField(o, "net_disp", v.net_disp);
|
||||
appendField(o, "wandering", v.wandering);
|
||||
appendField(o, "event", v.event);
|
||||
o.pop_back(); // dấu phẩy cuối
|
||||
o += "}\n";
|
||||
|
||||
if (!has_last_flush_ || (now - last_flush_).toSec() >= config_.flush_period || buffer_.size() > 48 * 1024)
|
||||
{
|
||||
flush();
|
||||
last_flush_ = now;
|
||||
has_last_flush_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void EnvLogger::flush()
|
||||
{
|
||||
if (out_.is_open() && !buffer_.empty())
|
||||
{
|
||||
out_ << buffer_;
|
||||
out_.flush();
|
||||
buffer_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace move_base2
|
||||
Reference in New Issue
Block a user