/********************************************************************* * * Software License Agreement (BSD License) * * move_base2 — cài đặt EnvLogger. * * Author: DuongTD *********************************************************************/ #include #include #include #include #include #include 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::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::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(static_cast(g->cost.target))); appendField(o, "cells_visited", static_cast(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, "pose_jump", v.pose_jump); appendField(o, "pose_jump_restart", v.pose_jump_restart); appendField(o, "pose_jump_dist", v.pose_jump_dist); appendField(o, "progress_proven", v.progress_proven); 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, "idle", v.idle); appendField(o, "confined", v.confined); appendField(o, "region_radius", v.region_radius); 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