Files
move_base2/src/io/env_logger.cpp
duongtd 5882082172 fix(env_monitor): ignore relocalization pose jumps, veto confined gate on proven progress
Sim 6/11 20:34: AMCL "Setting pose" pulled the map pose back 1.2 m while the
robot drove straight. EnvMonitor counted the jump as travel, the 20 s position
window folded onto itself and the confined gate fired a false stall.

- env_monitor/pose_jump_reject [m] (default 0.5, 0 = off): a pose step larger
  than this plus the reachable distance since the last valid pose is a reset,
  not travel. Stall windows restart at most once per longest position window;
  repeated jumps are reported but keep the windows. The jump distance is added
  to the cost_jump allowance. The control loop passes NaN when there is no pose
  and logs each jump as a warning.
- env_monitor/stall/progress_veto (default true): the confined gate does not
  fire while every eff measured in confine_window_s is >= eff_off. The idle gate
  is not vetoed; a cost jump breaks the proof.
- Tests: env_monitor_test 66 (3 new cases fail on the old code),
  config_validation_test 37.

Config runtime: the two keys above; lane_route_planner zone_deviation_weight and
output_endpoint_spins (R2.12), route_too_long_* marked as compatibility keys;
DirectionalLayer type unquoted in the no-virtual-walls global plugin list.
2026-11-12 10:23:23 +07:00

213 lines
5.9 KiB
C++

/*********************************************************************
*
* 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, "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