stall/confine_window_s 20 s, confine_radius 1,2 m, confine_cmd_factor 1,5: trong cửa sổ mọi vị trí nằm trong vòng bán kính < 1,2 m quanh tâm của chúng và lệnh tích luỹ >= 1,5 x max(2 x bán kính, disp_min) thì giơ stall (không cần geodesic — đi tới đi lui làm eff lật dấu). Cổng dịch chuyển đo đầu–cuối nên mù với vòng bán kính > 0,3 m; cổng đứng yên chỉ bắt gần như đứng. Replay 8 lượt JSONL 5–6/9: bắt 00:05 (101 s thay vì 206), 10:57 (27 s thay vì 75), 23:37; 0 lần ở 5 lượt sạch. Hệ số lệnh chặn ca bò thẳng chậm sau người. Verdict/JSONL/dòng 1 Hz thêm confined, region_radius. Sim 6/9 15:46: 2 mission tới goal, 0 cờ oan, cổng giam vùng không mở (không có ca loanh quanh trong lượt), kUnreachable 1 cờ đúng (cùng lúc PRIEST khai). Test: env_monitor 47/47 (+2), 10 bộ còn lại pass; YAML ↔ reader 69/69. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017xXjFjr2DQ6rhmhHSW4fk4
842 lines
30 KiB
C++
842 lines
30 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* move_base2 — cài đặt lõi EnvMonitor (xem header).
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <move_base2/core/env_monitor.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <sstream>
|
|
|
|
namespace move_base2
|
|
{
|
|
namespace
|
|
{
|
|
|
|
bool validAction(const std::string& action)
|
|
{
|
|
return action == "log" || action == "recover";
|
|
}
|
|
|
|
bool finitePositive(double v)
|
|
{
|
|
return std::isfinite(v) && v > 0.0;
|
|
}
|
|
|
|
bool finiteNonNegative(double v)
|
|
{
|
|
return std::isfinite(v) && v >= 0.0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// ------------------------------------------------------------------------------------------ config
|
|
|
|
bool EnvMonitorConfig::validate(std::string& error) const
|
|
{
|
|
if (!finiteNonNegative(grace_after_new_plan))
|
|
{
|
|
error = "env_monitor/grace_after_new_plan must be >= 0 [s]";
|
|
return false;
|
|
}
|
|
if (!validAction(stall.action) || !validAction(unreachable.action) || !validAction(contact.action))
|
|
{
|
|
error = "env_monitor/*/action must be \"log\" or \"recover\"";
|
|
return false;
|
|
}
|
|
if (!finitePositive(stall.window_s) || !finitePositive(stall.window_m))
|
|
{
|
|
error = "env_monitor/stall/window_s and window_m must be > 0";
|
|
return false;
|
|
}
|
|
if (!finitePositive(stall.min_commanded))
|
|
{
|
|
error = "env_monitor/stall/min_commanded must be > 0 [m] (0 would turn waiting into stalling)";
|
|
return false;
|
|
}
|
|
if (!std::isfinite(stall.eff_on) || !std::isfinite(stall.eff_off) || stall.eff_off <= stall.eff_on)
|
|
{
|
|
error = "env_monitor/stall/eff_off must be > eff_on (hysteresis)";
|
|
return false;
|
|
}
|
|
if (!finiteNonNegative(stall.confirm) || !finiteNonNegative(stall.goal_zone) ||
|
|
!finiteNonNegative(stall.slip_on) || !finiteNonNegative(stall.slip_time) ||
|
|
!finiteNonNegative(stall.min_commanded_since_goal) || !finiteNonNegative(stall.retry_backoff) ||
|
|
!finiteNonNegative(stall.jump_reject) || !finitePositive(stall.disp_window_s) ||
|
|
!finiteNonNegative(stall.disp_min) || !finiteNonNegative(stall.disp_min_commanded) ||
|
|
!finiteNonNegative(stall.idle_window_s) || !finiteNonNegative(stall.confine_radius) ||
|
|
!finitePositive(stall.confine_window_s) || !std::isfinite(stall.confine_cmd_factor) ||
|
|
stall.confine_cmd_factor < 1.0)
|
|
{
|
|
error = "env_monitor/stall: confirm, goal_zone, slip_on, slip_time, min_commanded_since_goal, "
|
|
"retry_backoff must be >= 0";
|
|
return false;
|
|
}
|
|
if (!finiteNonNegative(unreachable.static_on) || !finitePositive(unreachable.clean_start_margin) ||
|
|
!finiteNonNegative(unreachable.retry_backoff))
|
|
{
|
|
error = "env_monitor/unreachable: static_on >= 0 [s], clean_start_margin > 0 [m], retry_backoff >= 0";
|
|
return false;
|
|
}
|
|
if (contact.confirm_cycles < 1)
|
|
{
|
|
error = "env_monitor/contact/confirm_cycles must be >= 1";
|
|
return false;
|
|
}
|
|
if (!finiteNonNegative(contact.trigger_static_time) || !finitePositive(contact.clean_start_margin) ||
|
|
!finitePositive(contact.brake_decel) || !finiteNonNegative(contact.min_speed) ||
|
|
!finiteNonNegative(contact.min_check_speed) || !finiteNonNegative(contact.release_hysteresis) ||
|
|
contact.release_confirm_cycles < 1 || !finiteNonNegative(contact.envelope_margin) ||
|
|
!finiteNonNegative(contact.standdown_min_travel) || !finiteNonNegative(contact.standdown_travel))
|
|
{
|
|
error = "env_monitor/contact: trigger_static_time >= 0, clean_start_margin > 0, brake_decel > 0, "
|
|
"min_speed >= 0, min_check_speed >= 0, release_hysteresis >= 0, release_confirm_cycles >= 1, "
|
|
"envelope_margin >= 0, standdown_min_travel >= 0, standdown_travel >= 0";
|
|
return false;
|
|
}
|
|
if (!finiteNonNegative(replan.min_travel) || !std::isfinite(replan.escalate_factor) ||
|
|
replan.escalate_factor < 1.0 || !finiteNonNegative(replan.escalate_window_m) ||
|
|
!finiteNonNegative(replan.escalate_max) || (replan.escalate_max > 0.0 && replan.escalate_max < replan.min_travel))
|
|
{
|
|
error = "env_monitor/replan_budget: min_travel >= 0, escalate_factor >= 1, escalate_window_m >= 0, "
|
|
"escalate_max = 0 or >= min_travel";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string EnvMonitorConfig::describe() const
|
|
{
|
|
std::ostringstream out;
|
|
out << "env_monitor " << (enabled ? "ON" : "OFF") << " (grace " << grace_after_new_plan << " s):\n";
|
|
out << " stall : " << (stall.enabled ? "on" : "off") << " action=" << stall.action
|
|
<< " window=" << stall.window_s << " s/" << stall.window_m << " m min_cmd=" << stall.min_commanded
|
|
<< " m eff<" << stall.eff_on << " ket / >=" << stall.eff_off << " xoa confirm=" << stall.confirm
|
|
<< " s goal_zone=" << stall.goal_zone << " m slip>=" << stall.slip_on << " m/s " << stall.slip_time
|
|
<< " s cmd_since_goal>=" << stall.min_commanded_since_goal << " m backoff=" << stall.retry_backoff
|
|
<< " s jump_reject=" << stall.jump_reject << " m disp_gate=" << stall.disp_min << " m/"
|
|
<< stall.disp_window_s << " s (cmd>=" << stall.disp_min_commanded << " m) idle=" << stall.idle_window_s
|
|
<< " s confine=" << stall.confine_radius << " m/" << stall.confine_window_s << " s x" << stall.confine_cmd_factor
|
|
<< "\n";
|
|
out << " unreachable : " << (unreachable.enabled ? "on" : "off") << " action=" << unreachable.action
|
|
<< " static_on=" << unreachable.static_on << " s clean_margin=" << unreachable.clean_start_margin
|
|
<< " m backoff=" << unreachable.retry_backoff << " s\n";
|
|
out << " contact : " << (contact.enabled ? "on" : "off") << " action=" << contact.action
|
|
<< " confirm=" << contact.confirm_cycles << " cycles trigger_static=" << contact.trigger_static_time
|
|
<< " s clean_margin=" << contact.clean_start_margin << " m decel=" << contact.brake_decel
|
|
<< " m/s^2 envelope=" << (contact.envelope ? "on" : "off") << " min_speed=" << contact.min_speed
|
|
<< " m/s min_check_speed=" << contact.min_check_speed << " m/s release=" << contact.release_hysteresis
|
|
<< " m/" << contact.release_confirm_cycles << " cycles envelope_margin=" << contact.envelope_margin
|
|
<< " m standdown=" << contact.standdown_min_travel << "/" << contact.standdown_travel
|
|
<< " m apply_command=" << (contact.apply_command ? "YES" : "no (shadow)") << "\n";
|
|
out << " replan : min_travel=" << replan.min_travel << " m x" << replan.escalate_factor
|
|
<< " within " << replan.escalate_window_m << " m, max " << replan.escalate_max << " m";
|
|
return out.str();
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------------------- lifecycle
|
|
|
|
bool EnvMonitor::configure(const EnvMonitorConfig& config, std::string& error)
|
|
{
|
|
if (!config.validate(error))
|
|
{
|
|
configured_ = false;
|
|
return false;
|
|
}
|
|
config_ = config;
|
|
configured_ = true;
|
|
reset();
|
|
return true;
|
|
}
|
|
|
|
void EnvMonitor::reset()
|
|
{
|
|
cmd_accum_ = 0.0;
|
|
travel_total_ = 0.0;
|
|
has_last_pose_ = false;
|
|
has_goal_ = false;
|
|
cmd_at_goal_ = 0.0;
|
|
has_plan_time_ = false;
|
|
clearWindow();
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
slip_seen_ = false;
|
|
has_stall_backoff_ = false;
|
|
baseline_pending_ = true;
|
|
baseline_unreachable_ = false;
|
|
unreach_seen_ = false;
|
|
unreach_fired_ = false;
|
|
has_unreach_backoff_ = false;
|
|
contact_streak_ = 0;
|
|
release_streak_ = 0;
|
|
has_prev_heavy_ = false;
|
|
clearPosWindow();
|
|
braking_ = false;
|
|
contact_close_ = false;
|
|
contact_fired_ = false;
|
|
detour_from_contact_ = false;
|
|
detour_mark_travel_ = 0.0;
|
|
standdown_ = false;
|
|
standdown_mark_travel_ = 0.0;
|
|
standdown_clean_streak_ = 0;
|
|
has_replan_mark_ = false;
|
|
replan_mark_travel_ = 0.0;
|
|
budget_travel_ = config_.replan.min_travel;
|
|
has_prev_replan_ = false;
|
|
prev_replan_travel_ = 0.0;
|
|
}
|
|
|
|
void EnvMonitor::onGoalAccepted(const robot::Time& now)
|
|
{
|
|
(void)now;
|
|
const double cmd = cmd_accum_;
|
|
const double travel = travel_total_;
|
|
const bool had_pose = has_last_pose_;
|
|
const double lx = last_x_;
|
|
const double ly = last_y_;
|
|
reset();
|
|
// Odometer và lệnh tích luỹ là đơn điệu, chỉ dùng qua hiệu số: giữ lại để mốc không nhảy.
|
|
cmd_accum_ = cmd;
|
|
travel_total_ = travel;
|
|
has_last_pose_ = had_pose;
|
|
last_x_ = lx;
|
|
last_y_ = ly;
|
|
has_goal_ = true;
|
|
cmd_at_goal_ = cmd_accum_;
|
|
}
|
|
|
|
void EnvMonitor::onPlanApplied(const robot::Time& now)
|
|
{
|
|
has_plan_time_ = true;
|
|
plan_time_ = now;
|
|
clearWindow();
|
|
clearPosWindow(); // cửa sổ dịch chuyển bắt đầu lại: plan mới là một lượt thử mới
|
|
has_prev_heavy_ = false;
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
// Plan mới có baseline riêng: chỉ nổ "mất liên thông" vì thứ xảy ra SAU khi plan được lập (R2).
|
|
baseline_pending_ = true;
|
|
baseline_unreachable_ = false;
|
|
unreach_seen_ = false;
|
|
unreach_fired_ = false;
|
|
}
|
|
|
|
void EnvMonitor::onRecoveryFinished(const robot::Time& now, EnvRecoveryOutcome outcome, bool from_contact)
|
|
{
|
|
switch (outcome)
|
|
{
|
|
case EnvRecoveryOutcome::kSucceededWithPath:
|
|
{
|
|
// Detour do chính cờ collision: nếu robot không rời chỗ mà lại "sắp chạm" thì lần sau đứng ra.
|
|
detour_from_contact_ = from_contact;
|
|
detour_mark_travel_ = travel_total_;
|
|
// R3/R5: im tới khi đi đủ budget_travel_ kể từ đây; lặp trong escalate_window_m thì nhân lên.
|
|
if (has_prev_replan_ && travel_total_ - prev_replan_travel_ < config_.replan.escalate_window_m)
|
|
{
|
|
budget_travel_ *= config_.replan.escalate_factor;
|
|
if (config_.replan.escalate_max > 0.0)
|
|
{
|
|
budget_travel_ = std::min(budget_travel_, config_.replan.escalate_max);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
budget_travel_ = config_.replan.min_travel;
|
|
}
|
|
has_prev_replan_ = true;
|
|
prev_replan_travel_ = travel_total_;
|
|
has_replan_mark_ = true;
|
|
replan_mark_travel_ = travel_total_;
|
|
break;
|
|
}
|
|
case EnvRecoveryOutcome::kSucceeded:
|
|
break;
|
|
case EnvRecoveryOutcome::kFailed:
|
|
has_stall_backoff_ = true;
|
|
stall_backoff_until_ = now + robot::Duration(config_.stall.retry_backoff);
|
|
has_unreach_backoff_ = true;
|
|
unreach_backoff_until_ = now + robot::Duration(config_.unreachable.retry_backoff);
|
|
break;
|
|
}
|
|
// Mọi kết cục đều kết thúc một pha dừng: chuỗi xác nhận cũ không còn giá trị.
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
unreach_seen_ = false;
|
|
unreach_fired_ = false;
|
|
contact_streak_ = 0;
|
|
release_streak_ = 0;
|
|
braking_ = false;
|
|
contact_close_ = false;
|
|
contact_fired_ = false;
|
|
}
|
|
|
|
// -------------------------------------------------------------------------------------------- window
|
|
|
|
void EnvMonitor::clearWindow()
|
|
{
|
|
ring_head_ = 0;
|
|
ring_count_ = 0;
|
|
}
|
|
|
|
void EnvMonitor::clearPosWindow()
|
|
{
|
|
pos_head_ = 0;
|
|
pos_count_ = 0;
|
|
wandering_ = false;
|
|
idle_ = false;
|
|
confined_ = false;
|
|
region_radius_ = std::numeric_limits<double>::quiet_NaN();
|
|
net_disp_ = std::numeric_limits<double>::quiet_NaN();
|
|
}
|
|
|
|
bool EnvMonitor::oldestPos(const robot::Time& now, double window_s, PosSample& out) const
|
|
{
|
|
// Mẫu MỚI NHẤT đã đủ tuổi window_s — cùng lý do với oldestInWindow.
|
|
for (std::size_t k = 1; k <= pos_count_; ++k)
|
|
{
|
|
const PosSample& p = pos_ring_[(pos_head_ + kPosRing - k) % kPosRing];
|
|
if ((now - p.t).toSec() >= window_s)
|
|
{
|
|
out = p;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool EnvMonitor::oldestInWindow(const robot::Time& now, HeavySample& out) const
|
|
{
|
|
// Mẫu MỚI NHẤT đã đủ tuổi window_s hoặc đủ xa window_m lệnh — cửa sổ sát ngưỡng nhất, không phải
|
|
// mẫu cũ nhất trong ring (cửa sổ giãn ra làm eff mượt giả).
|
|
for (std::size_t k = 1; k <= ring_count_; ++k)
|
|
{
|
|
const HeavySample& s = ring_[(ring_head_ + kRing - k) % kRing];
|
|
const bool old_enough = (now - s.t).toSec() >= config_.stall.window_s;
|
|
const bool far_enough = cmd_accum_ - s.cmd >= config_.stall.window_m;
|
|
if (old_enough || far_enough)
|
|
{
|
|
out = s;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// -------------------------------------------------------------------------------------------- update
|
|
|
|
EnvVerdict EnvMonitor::update(const EnvSample& s)
|
|
{
|
|
EnvVerdict v;
|
|
if (!configured_ || !config_.enabled)
|
|
{
|
|
return v;
|
|
}
|
|
|
|
// Tích luỹ lệnh và quãng đường — mọi cửa sổ, cổng, ngân sách đều đo bằng hai con số này.
|
|
if (s.dt > 0.0 && std::isfinite(s.v_cmd))
|
|
{
|
|
cmd_accum_ += std::fabs(s.v_cmd) * s.dt;
|
|
}
|
|
if (std::isfinite(s.robot_x) && std::isfinite(s.robot_y))
|
|
{
|
|
if (has_last_pose_)
|
|
{
|
|
travel_total_ += std::hypot(s.robot_x - last_x_, s.robot_y - last_y_);
|
|
}
|
|
last_x_ = s.robot_x;
|
|
last_y_ = s.robot_y;
|
|
has_last_pose_ = true;
|
|
}
|
|
if (has_replan_mark_)
|
|
{
|
|
v.replan_budget_left = std::max(0.0, budget_travel_ - (travel_total_ - replan_mark_travel_));
|
|
}
|
|
|
|
updateContact(s, v);
|
|
|
|
if (!s.has_plan || !s.costmap_current)
|
|
{
|
|
// Không có gì để đo tiến độ; xoá chuỗi để không nối "đúng liên tục" qua quãng gián đoạn.
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
unreach_seen_ = false;
|
|
unreach_fired_ = false;
|
|
return v;
|
|
}
|
|
updateStall(s, v);
|
|
updateUnreachable(s, v);
|
|
return v;
|
|
}
|
|
|
|
void EnvMonitor::updateStall(const EnvSample& s, EnvVerdict& v)
|
|
{
|
|
const EnvMonitorConfig::Stall& c = config_.stall;
|
|
if (!c.enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Cửa sổ dịch chuyển thực: lấy mẫu vị trí mỗi tick nặng, không phụ thuộc cost/plan/mặt nạ.
|
|
if (s.geometry_valid && std::isfinite(s.robot_x) && std::isfinite(s.robot_y))
|
|
{
|
|
pos_ring_[pos_head_] = PosSample{ s.now, s.robot_x, s.robot_y, cmd_accum_ };
|
|
pos_head_ = (pos_head_ + 1) % kPosRing;
|
|
pos_count_ = std::min(pos_count_ + 1, kPosRing);
|
|
PosSample old;
|
|
if (oldestPos(s.now, c.disp_window_s, old))
|
|
{
|
|
net_disp_ = std::hypot(s.robot_x - old.x, s.robot_y - old.y);
|
|
wandering_ = (cmd_accum_ - old.cmd >= c.disp_min_commanded) && net_disp_ < c.disp_min;
|
|
}
|
|
else
|
|
{
|
|
net_disp_ = std::numeric_limits<double>::quiet_NaN();
|
|
wandering_ = false;
|
|
}
|
|
// Cổng đứng yên: không cần lệnh đủ — PRIEST bò 0,05 m/s tại chỗ 30 s cũng là kẹt.
|
|
idle_ = false;
|
|
PosSample older;
|
|
if (c.idle_window_s > 0.0 && c.disp_min > 0.0 && oldestPos(s.now, c.idle_window_s, older))
|
|
{
|
|
idle_ = std::hypot(s.robot_x - older.x, s.robot_y - older.y) < c.disp_min;
|
|
}
|
|
// Cổng giam vùng: bán kính của tập vị trí trong confine_window_s quanh tâm của chúng, so với lệnh tích luỹ.
|
|
confined_ = false;
|
|
region_radius_ = std::numeric_limits<double>::quiet_NaN();
|
|
if (c.confine_radius > 0.0 && c.disp_min > 0.0)
|
|
{
|
|
double sx = 0.0;
|
|
double sy = 0.0;
|
|
std::size_t n = 0;
|
|
double oldest_cmd = cmd_accum_;
|
|
double oldest_age = 0.0;
|
|
for (std::size_t k = 1; k <= pos_count_; ++k)
|
|
{
|
|
const PosSample& p = pos_ring_[(pos_head_ + kPosRing - k) % kPosRing];
|
|
const double age = (s.now - p.t).toSec();
|
|
if (age > c.confine_window_s)
|
|
{
|
|
break;
|
|
}
|
|
sx += p.x;
|
|
sy += p.y;
|
|
++n;
|
|
oldest_cmd = p.cmd;
|
|
oldest_age = age;
|
|
}
|
|
if (n >= 2 && oldest_age >= c.confine_window_s - 0.5)
|
|
{
|
|
const double cx = sx / static_cast<double>(n);
|
|
const double cy = sy / static_cast<double>(n);
|
|
double radius = 0.0;
|
|
for (std::size_t k = 1; k <= n; ++k)
|
|
{
|
|
const PosSample& p = pos_ring_[(pos_head_ + kPosRing - k) % kPosRing];
|
|
radius = std::max(radius, std::hypot(p.x - cx, p.y - cy));
|
|
}
|
|
region_radius_ = radius;
|
|
const double cmd = cmd_accum_ - oldest_cmd;
|
|
confined_ = radius < c.confine_radius &&
|
|
cmd >= c.confine_cmd_factor * std::max(2.0 * radius, c.disp_min);
|
|
}
|
|
}
|
|
}
|
|
v.net_disp = net_disp_;
|
|
v.wandering = wandering_;
|
|
v.idle = idle_;
|
|
v.confined = confined_;
|
|
v.region_radius = region_radius_;
|
|
|
|
// Mẫu nặng mới: ghi vào ring nếu có cost hữu hạn. +inf (mất liên thông tạm) không ghi — không kết
|
|
// luận từ mẫu thiếu.
|
|
if (s.geometry_valid && s.cost_conclusive && s.reachable && std::isfinite(s.cost_to_go))
|
|
{
|
|
// Lật liên thông: cost đổi nhiều hơn robot có thể đã đi trong một tick -> mặt nạ free vừa đóng/mở một khe,
|
|
// không phải tiến độ. Bắt đầu đo lại; kết luận kẹt cũ (nếu có) cũng không còn cơ sở.
|
|
if (c.jump_reject > 0.0 && has_prev_heavy_ &&
|
|
std::fabs(s.cost_to_go - prev_heavy_cost_) > c.jump_reject + (travel_total_ - prev_heavy_travel_))
|
|
{
|
|
clearWindow();
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
v.cost_jump = true;
|
|
}
|
|
has_prev_heavy_ = true;
|
|
prev_heavy_cost_ = s.cost_to_go;
|
|
prev_heavy_travel_ = travel_total_;
|
|
ring_[ring_head_] = HeavySample{ s.now, s.cost_to_go, cmd_accum_ };
|
|
ring_head_ = (ring_head_ + 1) % kRing;
|
|
ring_count_ = std::min(ring_count_ + 1, kRing);
|
|
|
|
HeavySample old;
|
|
if (oldestInWindow(s.now, old))
|
|
{
|
|
const double cmd = cmd_accum_ - old.cmd;
|
|
v.cmd_window = cmd;
|
|
if (cmd >= c.min_commanded)
|
|
{
|
|
v.eff_geo = (old.cost - s.cost_to_go) / cmd;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Trượt: lệnh đi mà bánh không quay (vật thấp, bumper). Chỉ khi có v_meas.
|
|
bool slipping = false;
|
|
if (c.slip_on > 0.0 && std::isfinite(s.v_meas))
|
|
{
|
|
const bool slip_now = std::fabs(s.v_cmd) - std::fabs(s.v_meas) >= c.slip_on;
|
|
if (slip_now)
|
|
{
|
|
if (!slip_seen_)
|
|
{
|
|
slip_seen_ = true;
|
|
slip_since_ = s.now;
|
|
}
|
|
slipping = (s.now - slip_since_).toSec() >= c.slip_time;
|
|
}
|
|
else
|
|
{
|
|
slip_seen_ = false;
|
|
}
|
|
}
|
|
|
|
const bool grace = has_plan_time_ && (s.now - plan_time_).toSec() < config_.grace_after_new_plan;
|
|
const bool near_goal = std::isfinite(s.d_goal) && s.d_goal < c.goal_zone;
|
|
if (grace || near_goal)
|
|
{
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
return;
|
|
}
|
|
|
|
const bool eff_known = !std::isnan(v.eff_geo);
|
|
const bool stalled = (eff_known && v.eff_geo < c.eff_on) || slipping;
|
|
const bool cleared = eff_known && v.eff_geo >= c.eff_off && !slipping;
|
|
if (stalled)
|
|
{
|
|
if (!stall_seen_)
|
|
{
|
|
stall_seen_ = true;
|
|
stall_since_ = s.now;
|
|
}
|
|
if (!stall_fired_ && (s.now - stall_since_).toSec() >= c.confirm)
|
|
{
|
|
stall_fired_ = true;
|
|
v.event = slipping ? "stall: wheels slip against the command" : "stall: cost_to_go not decreasing";
|
|
}
|
|
}
|
|
else if (cleared)
|
|
{
|
|
if (stall_fired_)
|
|
{
|
|
v.event = "stall cleared: progress resumed";
|
|
}
|
|
stall_seen_ = false;
|
|
stall_fired_ = false;
|
|
}
|
|
// Còn lại (không đo được, hoặc eff trong vùng chết): giữ trạng thái — không kết luận ≠ hết kẹt.
|
|
// Đứng yên suốt idle_window_s, hoặc đi nhiều mà bị giam trong vùng nhỏ suốt confine_window_s, là kẹt kể cả
|
|
// khi geodesic không đo được (lệnh quá ít để có eff) hoặc đang lật dấu vì robot đi tới đi lui.
|
|
if ((idle_ || confined_) && !stall_fired_)
|
|
{
|
|
stall_fired_ = true;
|
|
stall_seen_ = true;
|
|
stall_since_ = s.now;
|
|
v.event = idle_ ? "stall: no net displacement for the idle window"
|
|
: "stall: confined to a small region while commanded";
|
|
}
|
|
|
|
v.stall_detected = stall_fired_;
|
|
if (!stall_fired_)
|
|
{
|
|
return;
|
|
}
|
|
// Cổng đầu chặng, ngân sách replan, backoff sau lượt hỏng.
|
|
bool muted = false;
|
|
if (has_goal_ && cmd_accum_ - cmd_at_goal_ < c.min_commanded_since_goal)
|
|
{
|
|
muted = true;
|
|
}
|
|
if (c.disp_min > 0.0)
|
|
{
|
|
// Cổng dịch chuyển thực thay ngân sách replan: cửa sổ vị trí đã bắt đầu lại ở plan mới, nên "sau detour
|
|
// vẫn quần tại chỗ disp_window_s" mới được nổ lại; né tự do (dịch chuyển >= disp_min) không bao giờ nổ.
|
|
if (!wandering_ && !idle_ && !confined_)
|
|
{
|
|
muted = true;
|
|
}
|
|
}
|
|
else if (has_replan_mark_ && travel_total_ - replan_mark_travel_ < budget_travel_)
|
|
{
|
|
muted = true;
|
|
}
|
|
if (has_stall_backoff_ && s.now < stall_backoff_until_)
|
|
{
|
|
muted = true;
|
|
}
|
|
v.stall_muted = muted;
|
|
v.no_progress = !muted && c.action == "recover";
|
|
}
|
|
|
|
void EnvMonitor::updateUnreachable(const EnvSample& s, EnvVerdict& v)
|
|
{
|
|
const EnvMonitorConfig::Unreachable& c = config_.unreachable;
|
|
if (!c.enabled)
|
|
{
|
|
return;
|
|
}
|
|
// Trạng thái chỉ đổi ở tick nặng có kết luận; verdict thì phải ra MỖI cycle từ trạng thái, nếu
|
|
// không cờ nhấp nháy giữa hai tick 5 Hz và state machine 20 Hz thấy cờ bật/tắt.
|
|
if (s.geometry_valid && s.cost_conclusive)
|
|
{
|
|
// Baseline của plan: mẫu kết luận đầu tiên sau khi nhận plan. Plan đã không liên thông ngay từ
|
|
// đầu thì đó là chuyện của SBPL/planner đã lập nó — monitor không cãi trên cùng snapshot (R2).
|
|
if (baseline_pending_)
|
|
{
|
|
baseline_pending_ = false;
|
|
baseline_unreachable_ = !s.reachable;
|
|
}
|
|
if (s.reachable)
|
|
{
|
|
if (unreach_fired_)
|
|
{
|
|
v.event = "unreachable cleared: the exit is connected again";
|
|
}
|
|
unreach_seen_ = false;
|
|
unreach_fired_ = false;
|
|
baseline_unreachable_ = false; // từ đây mất liên thông là chuyện mới
|
|
}
|
|
else
|
|
{
|
|
const bool grace = has_plan_time_ && (s.now - plan_time_).toSec() < config_.grace_after_new_plan;
|
|
const bool clean = std::isfinite(s.edt_body_min) && s.edt_body_min >= c.clean_start_margin;
|
|
if (baseline_unreachable_ || grace || !clean)
|
|
{
|
|
// Không tích: chưa phải "xấu đi", hoặc robot đã lọt vành (SBPL không có chỗ bắt đầu — việc
|
|
// của kContact + controller_patience).
|
|
unreach_seen_ = false;
|
|
}
|
|
else
|
|
{
|
|
if (!unreach_seen_)
|
|
{
|
|
unreach_seen_ = true;
|
|
unreach_since_ = s.now;
|
|
}
|
|
if (!unreach_fired_ && (s.now - unreach_since_).toSec() >= c.static_on)
|
|
{
|
|
unreach_fired_ = true;
|
|
v.event = "unreachable: no path to the plan exit inside the window";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
v.unreachable_detected = unreach_fired_;
|
|
if (!unreach_fired_)
|
|
{
|
|
return;
|
|
}
|
|
bool muted = false;
|
|
if (has_replan_mark_ && travel_total_ - replan_mark_travel_ < budget_travel_)
|
|
{
|
|
muted = true;
|
|
}
|
|
if (has_unreach_backoff_ && s.now < unreach_backoff_until_)
|
|
{
|
|
muted = true;
|
|
}
|
|
v.unreachable_muted = muted;
|
|
v.path_blocked = !muted && c.action == "recover";
|
|
}
|
|
|
|
void EnvMonitor::updateContact(const EnvSample& s, EnvVerdict& v)
|
|
{
|
|
const EnvMonitorConfig::Contact& c = config_.contact;
|
|
if (!c.enabled)
|
|
{
|
|
return;
|
|
}
|
|
double v_ref = std::fabs(s.v_cmd);
|
|
if (std::isfinite(s.v_meas))
|
|
{
|
|
v_ref = std::max(v_ref, std::fabs(s.v_meas));
|
|
}
|
|
v.brake_dist = v_ref * v_ref / (2.0 * c.brake_decel);
|
|
|
|
if (!s.contact_conclusive || !s.costmap_current)
|
|
{
|
|
// Không biết thì không dừng, cũng không xoá chuỗi (cùng hợp đồng với guard cũ).
|
|
v.brake = braking_; // đang phanh thì giữ phanh tới khi có bằng chứng ngược lại
|
|
return;
|
|
}
|
|
if (standdown_)
|
|
{
|
|
// Đang đứng ra: không phanh, không cờ, không bao. Chỉ theo dõi điều kiện vũ trang lại.
|
|
v.contact_standdown = true;
|
|
if (std::isfinite(s.traj_contact_dist))
|
|
{
|
|
standdown_clean_streak_ = 0;
|
|
}
|
|
else
|
|
{
|
|
++standdown_clean_streak_;
|
|
}
|
|
const bool travelled = travel_total_ - standdown_mark_travel_ >= c.standdown_travel;
|
|
if (travelled || standdown_clean_streak_ >= c.release_confirm_cycles)
|
|
{
|
|
standdown_ = false;
|
|
standdown_clean_streak_ = 0;
|
|
v.contact_standdown = false;
|
|
v.event = travelled ? "contact: re-armed after moving on" : "contact: re-armed, the trajectory is clear";
|
|
}
|
|
return;
|
|
}
|
|
if (!std::isfinite(s.traj_contact_dist))
|
|
{
|
|
contact_streak_ = 0;
|
|
if (braking_ && ++release_streak_ < c.release_confirm_cycles)
|
|
{
|
|
// Một mẫu sạch chưa là sạch: local planner đổi quỹ đạo mỗi cycle, sạch 1 cycle rồi chạm lại là
|
|
// chuyện thường (run 5/9 22:35: 4 lần nhả/phanh trong 3 s). Giữ phanh và cờ tới khi sạch liên tiếp.
|
|
v.brake = true;
|
|
v.collision = contact_fired_ && c.action == "recover";
|
|
return;
|
|
}
|
|
if (braking_ || contact_fired_)
|
|
{
|
|
v.event = "contact cleared: the trajectory is free again";
|
|
}
|
|
braking_ = false;
|
|
release_streak_ = 0;
|
|
contact_close_ = false;
|
|
contact_fired_ = false;
|
|
return;
|
|
}
|
|
|
|
// Bao tốc độ: |v| <= sqrt(2 a (d - margin)) để luôn còn dừng được trên đất sạch.
|
|
// Bao giữ thêm envelope_margin so với ngưỡng phanh: lệnh nằm trên bao thì quãng phanh còn dư đúng chừng
|
|
// đó, nhiễu quỹ đạo vài cm không đẩy sang "sắp chạm". Trong margin thì 0; giữa margin và margin+dư thì bò
|
|
// min_speed.
|
|
if (c.envelope)
|
|
{
|
|
if (s.traj_contact_dist <= c.clean_start_margin)
|
|
{
|
|
v.v_cap = 0.0;
|
|
}
|
|
else
|
|
{
|
|
const double room = s.traj_contact_dist - c.clean_start_margin - c.envelope_margin;
|
|
v.v_cap = std::max(c.min_speed, room > 0.0 ? std::sqrt(2.0 * c.brake_decel * room) : 0.0);
|
|
}
|
|
}
|
|
|
|
// Xoay tại chỗ / bò mà monitor chưa phanh: quỹ đạo local planner lúc này là một cú xoay, góc thân
|
|
// quét vào ô kề bên là chuyện của controller (nó tự kiểm footprint), không phải bài toán quãng phanh.
|
|
// Phanh ở đây là chặn robot xoay ra khỏi vật đang kề mũi, rồi 1 s sau giơ cờ và gọi detour từ đúng
|
|
// chỗ đứng — vòng churn đầu chặng của guard cũ (mission 5/6 shadow 5/9). Đã phanh rồi thì vẫn xét
|
|
// tiếp để "kề mũi đứng yên" còn nổ được.
|
|
double v_intent = v_ref;
|
|
if (std::isfinite(s.v_controller))
|
|
{
|
|
v_intent = std::max(v_intent, std::fabs(s.v_controller));
|
|
}
|
|
if (v_intent < c.min_check_speed && !braking_)
|
|
{
|
|
contact_streak_ = 0;
|
|
release_streak_ = 0;
|
|
contact_close_ = false;
|
|
contact_fired_ = false;
|
|
return;
|
|
}
|
|
|
|
// Hai câu hỏi khác nhau:
|
|
// - "sắp chạm" = còn dừng kịp không (theo quãng phanh) -> PHANH. Đứng lại rồi thì quãng phanh về 0
|
|
// và câu này tự hết đúng, robot được bò tiếp dưới bao tốc độ.
|
|
// - "đang kề vật" = tiếp xúc trong đúng clean_start_margin bất kể tốc độ -> nếu còn đó đủ
|
|
// trigger_static_time thì vật đứng yên trước mũi, không phải người đi qua -> giơ CỜ.
|
|
// Bao tốc độ đặt lệnh đúng lên biên v_cap = sqrt(2a(d - margin)), tức d - quãng phanh == margin: nếu
|
|
// "sắp chạm" là <= thì mỗi lần bao kẹp xong là phanh, nhả, kẹp, phanh (chattering 4 cycle). Chỉ phanh
|
|
// khi vượt bao quá 1 mm — lệnh nằm trên bao là đang giảm tốc đúng luật, không phải sắp chạm.
|
|
// Trong margin thì bao đã là 0: đó là một cú dừng của monitor dù quãng phanh bằng 0 — phải được gọi
|
|
// là phanh để có sự kiện, để giữ 0 tức thì qua trọng tài và để cổng xoay không nuốt nó.
|
|
constexpr double kEnvelopeTol = 1e-3; // [m]
|
|
// Đã kề thì còn kề tới margin + release_hysteresis: quỹ đạo nhảy 0,08 <-> 0,25 m quanh margin không
|
|
// được reset đồng hồ giơ cờ, cũng không được nhả phanh.
|
|
const double close_limit = c.clean_start_margin + (contact_close_ ? c.release_hysteresis : 0.0);
|
|
const bool close = s.traj_contact_dist <= close_limit;
|
|
const bool imminent = close || s.traj_contact_dist - v.brake_dist < c.clean_start_margin - kEnvelopeTol;
|
|
v.contact_imminent = imminent;
|
|
if (imminent)
|
|
{
|
|
release_streak_ = 0;
|
|
++contact_streak_;
|
|
if (!braking_ && contact_streak_ >= c.confirm_cycles)
|
|
{
|
|
if (c.standdown_min_travel > 0.0 && detour_from_contact_ &&
|
|
travel_total_ - detour_mark_travel_ < c.standdown_min_travel)
|
|
{
|
|
// Vừa detour từ đúng chỗ này vì chính cú chạm này, robot chưa rời chỗ: phanh + cờ lần nữa chỉ lặp
|
|
// lại churn (3 detour rồi ABORT). Nhường controller — nó là lưới an toàn duy nhất tới khi robot đi
|
|
// được standdown_travel hoặc quỹ đạo sạch.
|
|
standdown_ = true;
|
|
standdown_mark_travel_ = travel_total_;
|
|
standdown_clean_streak_ = 0;
|
|
detour_from_contact_ = false;
|
|
contact_streak_ = 0;
|
|
release_streak_ = 0;
|
|
contact_close_ = false;
|
|
contact_fired_ = false;
|
|
v.contact_standdown = true;
|
|
v.contact_imminent = false;
|
|
v.v_cap = std::numeric_limits<double>::infinity();
|
|
v.event = "contact: standing down — the detour from this spot did not move the robot, the controller "
|
|
"is the only safety net until it travels on";
|
|
return;
|
|
}
|
|
braking_ = true;
|
|
v.event = "contact: braking to zero";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
contact_streak_ = 0;
|
|
if (braking_ && ++release_streak_ >= c.release_confirm_cycles)
|
|
{
|
|
braking_ = false;
|
|
release_streak_ = 0;
|
|
v.event = "contact receded: releasing the brake";
|
|
}
|
|
}
|
|
v.brake = braking_;
|
|
|
|
if (close)
|
|
{
|
|
if (!contact_close_)
|
|
{
|
|
contact_close_ = true;
|
|
close_since_ = s.now;
|
|
}
|
|
if (!contact_fired_ && (s.now - close_since_).toSec() >= c.trigger_static_time)
|
|
{
|
|
contact_fired_ = true;
|
|
v.event = "contact persists while stopped: asking for a replan";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (contact_fired_)
|
|
{
|
|
v.event = "contact cleared: the obstacle moved away";
|
|
}
|
|
contact_close_ = false;
|
|
contact_fired_ = false;
|
|
}
|
|
v.collision = contact_fired_ && c.action == "recover";
|
|
}
|
|
|
|
} // namespace move_base2
|