feat: trigger collision riêng, ngân sách recovery theo dịch chuyển, guard đo bằng thời gian/góc
Năm thay đổi của cùng một chiến dịch: robot đứng chết ở góc trụ và giữa đám đông. Mọi con số dưới đây lấy từ log sim ngày 3-4/9, ghi lại trong Test/COLLISION_GUARD_LOOP_FIX_PLAN.md. 1) Ngân sách recovery nạp lại theo DỊCH CHUYỂN (state_machine) Trước: recovery thành công + nộp path mới thì cursor route reset NGAY. Sim 4/9: guard chặn tại chỗ -> detour lập lại đúng đường cũ (cost y hệt, 0 ms) -> cursor về 0 -> chặn lại sau 2 cycle -> 249 vòng trong 52 s, robot bất động, controller_patience không bao giờ tích đủ vì path_blocked chen trước mỗi 0.2 s. Giờ chỉ nạp lại khi robot đã đi >= recovery_reset_min_travel (0.10 m) kể từ lần nạp trước; chưa đi thì tính như một lần thất bại -> route cạn -> mute -> controller_patience -> controlling_failed -> ABORT có lý do, ~2 s. Mốc theo origin đo quãng đường (reset cùng lúc với nó), nếu không thì 0.18 m sau path mới bị báo "0.00 m" và ABORT oan. 2) Guard loại trừ BASELINE (plan_inspector) Câu hỏi đổi từ "thân có đè LETHAL không" thành "lệnh này có đưa THÊM ô LETHAL vào thân không". Ô đã nằm dưới thân đệm tại pose hiện tại được gom thành baseline (rasterize diện tích ~600 ô, binary_search) và bỏ qua ở các mẫu sau. Sim 4/9: người kề hông ở 0.46 m ngang (thân 0.48 + đệm 0.05) làm mẫu đầu tiên báo "0.05 m" mãi dù PRIEST lệnh đi thẳng, SBPL (không đệm) bảo đi được, robot đứng chết. Trượt song song thì đi được, rẽ vào thì vẫn chặn. 3) Thân robot lấy từ FOOTPRINT COSTMAP collision_*_offset = 0 cả ba -> hỏi PathCheckPort::robotBodyExtents() mỗi cycle (footprint không đệm, cùng hình với SBPL và PRIEST). Đổi footprint lúc chạy là guard đổi theo, không phải chép số vào hai file. Khai tay thì phải đủ cả ba, khai lửng là config sai. Không có nguồn nào thì báo lỗi và KHÔNG kết luận — dừng mù là biến lỗi cấu hình thành robot chết. 4) "Sắp chạm" đo bằng THỜI GIAN và GÓC, không chỉ mét Mét dọc cung không nói lên gì với cung xoay gần tại chỗ: cùng 0.15 m, đi thẳng 0.36 m/s là chạm sau 0.4 s, còn xoay bò v 0.04 omega -0.5 (R 0.08 m) là quay 107 độ và còn 3.8 s — PRIEST đã thôi xoay sau 30-60 độ để thẳng hàng với path. Sim 16:26: 11 lần nổ trong 15 s tại góc trụ, xoay 0.9 rad mất 12 s thay vì 2 s. Giờ chỉ dừng khi t_contact <= collision_stop_time HOẶC turn <= collision_stop_angle; ngoài đó chỉ kẹp v. Đi thẳng (turn ~ 0) không đổi hành vi. Cung cũng không dò quá collision_lookahead_angle. 5) Trigger kCollision RIÊNG cho Phép đo 3 Trước dùng chung path_blocked. Phép đo 1 nổ ở 3 m nên lập lại đường là đủ; Phép đo 3 nổ khi đã sát vật, replan từ đúng pose đó thường trả cùng path hoặc không ra nghiệm (start trong vành 253) — muốn thoát phải ĐỔI POSE, tức back_up đứng trước detour_path. Route, ngân sách và khoá đều riêng. Ưu tiên collision > path_blocked > off_path. Cạn route vẫn là KHOÁ chứ không ABORT (contract 2026-08-05e). Config: tách path_monitor ra file riêng path_monitor_params.yaml, sắp xếp lại theo bốn phép đo; recovery_behaviors_params.yaml sắp xếp lại theo trigger và dọn bốn khoá chết của mô hình detour cũ. Khoá mới: recovery_reset_min_travel, collision_exclude_baseline, collision_stop_time, collision_stop_angle, collision_lookahead_angle. Debug (khoá debug/state): dòng "hit at sample K: <edge> edge touches cell (x, y)" và "body overlaps N LETHAL cell(s) ... letting it move" để phân biệt "góc thân quét tới" với "người bước tới" khi đọc log mà không có costmap. Verify: catkin_make --pkg move_base2 rc=0; ctest -R move_base2 15/15 (MotionCheck 16, CollisionGuard 13, RecoveryBudget 6, config 34). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013rayqed51UoUzE7QDRmM9K
This commit is contained in:
@@ -159,6 +159,38 @@ void readPathMonitor(robot::NodeHandle& nh, ControlLoopConfig::PathMonitorConfig
|
||||
readDouble(monitor_nh, "max_cross_track", monitor.max_cross_track);
|
||||
readDouble(monitor_nh, "grace_after_new_plan", monitor.grace_after_new_plan);
|
||||
|
||||
// Phép đo 3 (2026-09-03): thân sắp chạm vật cản THẬT trên cung lệnh. Khoá riêng, không dùng chung
|
||||
// tham số nào với hai phép đo trên.
|
||||
readBool(monitor_nh, "collision_enabled", monitor.collision_enabled);
|
||||
readDouble(monitor_nh, "collision_lookahead", monitor.collision_lookahead);
|
||||
monitor_nh.param("collision_max_samples", monitor.collision_max_samples,
|
||||
monitor.collision_max_samples);
|
||||
readDouble(monitor_nh, "collision_stop_distance", monitor.collision_stop_distance);
|
||||
monitor_nh.param("collision_confirm_cycles", monitor.collision_confirm_cycles,
|
||||
monitor.collision_confirm_cycles);
|
||||
monitor_nh.param("collision_cost_threshold", monitor.collision_cost_threshold,
|
||||
monitor.collision_cost_threshold);
|
||||
readDouble(monitor_nh, "collision_front_offset", monitor.collision_front_offset);
|
||||
readDouble(monitor_nh, "collision_rear_offset", monitor.collision_rear_offset);
|
||||
readDouble(monitor_nh, "collision_side_offset", monitor.collision_side_offset);
|
||||
readDouble(monitor_nh, "collision_footprint_padding", monitor.collision_footprint_padding);
|
||||
readBool(monitor_nh, "collision_exclude_baseline", monitor.collision_exclude_baseline);
|
||||
readDouble(monitor_nh, "collision_min_speed", monitor.collision_min_speed);
|
||||
readDouble(monitor_nh, "collision_stop_time", monitor.collision_stop_time);
|
||||
readDouble(monitor_nh, "collision_stop_angle", monitor.collision_stop_angle);
|
||||
readDouble(monitor_nh, "collision_lookahead_angle", monitor.collision_lookahead_angle);
|
||||
readDouble(monitor_nh, "collision_brake_decel", monitor.collision_brake_decel);
|
||||
|
||||
// Phép đo 4 (2026-09-04): planner đòi đi mà robot không tiến.
|
||||
readBool(monitor_nh, "progress_enabled", monitor.progress_enabled);
|
||||
readString(monitor_nh, "progress_action", monitor.progress_action);
|
||||
readDouble(monitor_nh, "progress_window", monitor.progress_window);
|
||||
readDouble(monitor_nh, "progress_min_commanded", monitor.progress_min_commanded);
|
||||
readDouble(monitor_nh, "progress_min_efficiency", monitor.progress_min_efficiency);
|
||||
readDouble(monitor_nh, "progress_clear_efficiency", monitor.progress_clear_efficiency);
|
||||
readDouble(monitor_nh, "progress_confirm_time", monitor.progress_confirm_time);
|
||||
readDouble(monitor_nh, "progress_goal_zone", monitor.progress_goal_zone);
|
||||
|
||||
std::vector<std::string> names;
|
||||
monitor_nh.param("profiles", names, std::vector<std::string>{});
|
||||
if (names.empty())
|
||||
@@ -208,6 +240,7 @@ void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
|
||||
readDouble(nh, "sensor_patience", config.state_machine.sensor_patience);
|
||||
readDouble(nh, "oscillation_timeout", config.state_machine.oscillation_timeout);
|
||||
readDouble(nh, "oscillation_distance", config.state_machine.oscillation_distance);
|
||||
readDouble(nh, "recovery_reset_min_travel", config.state_machine.recovery_reset_min_travel);
|
||||
readDouble(nh, "action_patience", config.state_machine.action_patience);
|
||||
readInt(nh, "max_planning_retries", config.state_machine.max_planning_retries);
|
||||
readBool(nh, "recovery_behavior_enabled", config.state_machine.recovery_enabled);
|
||||
@@ -394,11 +427,122 @@ bool MoveBase2Config::validate(std::string& error) const
|
||||
error = "path_monitor/grace_after_new_plan must be >= 0 [s]";
|
||||
return false;
|
||||
}
|
||||
if (!(path_monitor.horizon > 0.0) && !(path_monitor.max_cross_track > 0.0))
|
||||
if (path_monitor.collision_enabled)
|
||||
{
|
||||
// Bật khối mà tắt cả hai phép đo là mâu thuẫn: người khai tưởng giám sát đang chạy.
|
||||
error = "path_monitor is enabled but both checks are off; set horizon > 0 [m] and/or "
|
||||
"max_cross_track > 0 [m], or set enabled: false";
|
||||
const auto bad = [](double v) { return !std::isfinite(v) || v < 0.0; };
|
||||
if (!std::isfinite(path_monitor.collision_lookahead) || path_monitor.collision_lookahead <= 0.0)
|
||||
{
|
||||
error = "path_monitor/collision_lookahead must be > 0 [m]";
|
||||
return false;
|
||||
}
|
||||
if (path_monitor.collision_max_samples < 1)
|
||||
{
|
||||
error = "path_monitor/collision_max_samples must be >= 1";
|
||||
return false;
|
||||
}
|
||||
if (bad(path_monitor.collision_stop_distance) ||
|
||||
path_monitor.collision_stop_distance > path_monitor.collision_lookahead)
|
||||
{
|
||||
error = "path_monitor/collision_stop_distance must be in [0, collision_lookahead] [m]";
|
||||
return false;
|
||||
}
|
||||
if (path_monitor.collision_confirm_cycles < 1)
|
||||
{
|
||||
error = "path_monitor/collision_confirm_cycles must be >= 1";
|
||||
return false;
|
||||
}
|
||||
if (path_monitor.collision_cost_threshold < 1 || path_monitor.collision_cost_threshold > 254)
|
||||
{
|
||||
error = "path_monitor/collision_cost_threshold must be in [1, 254] (254 = LETHAL only)";
|
||||
return false;
|
||||
}
|
||||
if (bad(path_monitor.collision_front_offset) || bad(path_monitor.collision_rear_offset) ||
|
||||
bad(path_monitor.collision_side_offset) || bad(path_monitor.collision_footprint_padding))
|
||||
{
|
||||
error = "path_monitor/collision_*_offset and collision_footprint_padding must be >= 0 [m]";
|
||||
return false;
|
||||
}
|
||||
{
|
||||
// Cả ba = 0: lấy thân từ footprint costmap lúc chạy. Khai tay thì phải đủ cả ba > 0 — mũi = 0
|
||||
// là chỉ dò tâm (dừng khi tâm đã ở trong tường), hông = 0 là bỏ hai cạnh hông.
|
||||
const int declared = (path_monitor.collision_front_offset > 0.0 ? 1 : 0) +
|
||||
(path_monitor.collision_rear_offset > 0.0 ? 1 : 0) +
|
||||
(path_monitor.collision_side_offset > 0.0 ? 1 : 0);
|
||||
if (declared != 0 && declared != 3)
|
||||
{
|
||||
error = "path_monitor/collision_front_offset, collision_rear_offset and "
|
||||
"collision_side_offset must either all be 0 (take the body from the costmap "
|
||||
"footprint) or all be > 0 [m]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!std::isfinite(path_monitor.collision_stop_time) || path_monitor.collision_stop_time <= 0.0)
|
||||
{
|
||||
error = "path_monitor/collision_stop_time must be > 0 [s]";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(path_monitor.collision_stop_angle) || path_monitor.collision_stop_angle <= 0.0 ||
|
||||
path_monitor.collision_stop_angle > M_PI)
|
||||
{
|
||||
error = "path_monitor/collision_stop_angle must be in (0, pi] [rad]";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(path_monitor.collision_lookahead_angle) ||
|
||||
path_monitor.collision_lookahead_angle < path_monitor.collision_stop_angle ||
|
||||
path_monitor.collision_lookahead_angle > M_PI)
|
||||
{
|
||||
error = "path_monitor/collision_lookahead_angle must be in [collision_stop_angle, pi] [rad]";
|
||||
return false;
|
||||
}
|
||||
if (bad(path_monitor.collision_min_speed) || bad(path_monitor.collision_brake_decel))
|
||||
{
|
||||
error = "path_monitor/collision_min_speed [m/s] and collision_brake_decel [m/s^2] must be >= 0";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (path_monitor.progress_enabled)
|
||||
{
|
||||
if (path_monitor.progress_action != "log" && path_monitor.progress_action != "recover")
|
||||
{
|
||||
error = "path_monitor/progress_action must be 'log' or 'recover'";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(path_monitor.progress_window) || path_monitor.progress_window <= 0.0)
|
||||
{
|
||||
error = "path_monitor/progress_window must be > 0 [s]";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(path_monitor.progress_min_commanded) ||
|
||||
path_monitor.progress_min_commanded <= 0.0)
|
||||
{
|
||||
// 0 là đo cả lúc đứng yên: cmd = 0, eff = 0/0 -> mọi lần chờ hợp lệ đều thành "kẹt".
|
||||
error = "path_monitor/progress_min_commanded must be > 0 [m]";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(path_monitor.progress_min_efficiency) ||
|
||||
!std::isfinite(path_monitor.progress_clear_efficiency) ||
|
||||
path_monitor.progress_min_efficiency < 0.0 ||
|
||||
path_monitor.progress_clear_efficiency <= path_monitor.progress_min_efficiency)
|
||||
{
|
||||
// clear == min là cờ nhấp nháy quanh một ngưỡng: bật, xoá, bật... mỗi lượt quét.
|
||||
error = "path_monitor/progress_clear_efficiency must be > progress_min_efficiency >= 0";
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(path_monitor.progress_confirm_time) ||
|
||||
path_monitor.progress_confirm_time < 0.0 ||
|
||||
!std::isfinite(path_monitor.progress_goal_zone) || path_monitor.progress_goal_zone < 0.0)
|
||||
{
|
||||
error = "path_monitor/progress_confirm_time [s] and progress_goal_zone [m] must be >= 0";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!(path_monitor.horizon > 0.0) && !(path_monitor.max_cross_track > 0.0) &&
|
||||
!path_monitor.collision_enabled && !path_monitor.progress_enabled)
|
||||
{
|
||||
// Bật khối mà tắt cả bốn phép đo là mâu thuẫn: người khai tưởng giám sát đang chạy.
|
||||
error = "path_monitor is enabled but all checks are off; set horizon > 0 [m], "
|
||||
"max_cross_track > 0 [m], collision_enabled: true and/or progress_enabled: true, "
|
||||
"or set enabled: false";
|
||||
return false;
|
||||
}
|
||||
if (path_monitor.profiles.empty())
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
|
||||
@@ -196,18 +198,22 @@ bool ControlLoop::configure(const ControlLoopConfig& config, const ControlLoopDe
|
||||
robot::log_error("[move_base2] path_monitor is enabled but no PathCheckPort is wired — path "
|
||||
"monitoring stays OFF.\n");
|
||||
}
|
||||
else if (!config_.path_monitor.path_blocked_enabled && !config_.path_monitor.off_path_enabled)
|
||||
else if (!config_.path_monitor.path_blocked_enabled && !config_.path_monitor.off_path_enabled &&
|
||||
!config_.path_monitor.collision_enabled && !config_.path_monitor.progress_enabled)
|
||||
{
|
||||
robot::log_warning("[move_base2] path_monitor/enabled is true but BOTH sub-monitors are off "
|
||||
"(path_blocked_enabled=false, off_path_enabled=false) — path monitoring "
|
||||
"does nothing.\n");
|
||||
robot::log_warning("[move_base2] path_monitor/enabled is true but ALL sub-monitors are off "
|
||||
"(path_blocked_enabled=false, off_path_enabled=false, "
|
||||
"collision_enabled=false, progress_enabled=false) — path monitoring does "
|
||||
"nothing.\n");
|
||||
}
|
||||
else if (config_.state_machine.recovery_routes.path_blocked.empty() &&
|
||||
config_.state_machine.recovery_routes.off_path.empty())
|
||||
config_.state_machine.recovery_routes.off_path.empty() &&
|
||||
config_.state_machine.recovery_routes.collision.empty() &&
|
||||
config_.state_machine.recovery_routes.oscillation.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] path_monitor is enabled but BOTH recovery routes "
|
||||
"'path_blocked' and 'off_path' are empty (behaviors failed to load?) — path "
|
||||
"monitoring stays OFF.\n");
|
||||
robot::log_error("[move_base2] path_monitor is enabled but the recovery routes "
|
||||
"'path_blocked', 'off_path', 'collision' and 'oscillation' are all empty "
|
||||
"(behaviors failed to load?) — path monitoring stays OFF.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -218,9 +224,30 @@ bool ControlLoop::configure(const ControlLoopConfig& config, const ControlLoopDe
|
||||
const bool off_path_on = config_.path_monitor.off_path_enabled &&
|
||||
config_.path_monitor.max_cross_track > 0.0 &&
|
||||
!config_.state_machine.recovery_routes.off_path.empty();
|
||||
const bool collision_on = config_.path_monitor.collision_enabled &&
|
||||
config_.path_monitor.collision_lookahead > 0.0 &&
|
||||
!config_.state_machine.recovery_routes.collision.empty();
|
||||
char body[64];
|
||||
if (!(config_.path_monitor.collision_front_offset > 0.0) &&
|
||||
!(config_.path_monitor.collision_rear_offset > 0.0) &&
|
||||
!(config_.path_monitor.collision_side_offset > 0.0))
|
||||
{
|
||||
std::snprintf(body, sizeof(body), "costmap footprint");
|
||||
}
|
||||
else
|
||||
{
|
||||
std::snprintf(body, sizeof(body), "%.2f/%.2f/%.2f",
|
||||
config_.path_monitor.collision_front_offset,
|
||||
config_.path_monitor.collision_rear_offset,
|
||||
config_.path_monitor.collision_side_offset);
|
||||
}
|
||||
robot::log_info("[move_base2] path monitor ON (confirm=%.2f s, check=%.2f s):\n"
|
||||
" path_blocked: %s (flag=%s, horizon=%.2f m, route=%zu)\n"
|
||||
" off_path : %s (flag=%s, max_cross_track=%.2f m, route=%zu)\n",
|
||||
" off_path : %s (flag=%s, max_cross_track=%.2f m, route=%zu)\n"
|
||||
" collision : %s (flag=%s, lookahead=%.2f m, stop=%.2f m, confirm=%d "
|
||||
"cycles, threshold=%d, body=%s+%.2f m, brake=%.2f m/s^2, "
|
||||
"baseline=%s, stop_time=%.1f s, stop_angle=%.2f rad, lookahead_angle=%.2f "
|
||||
"rad, route=collision %zu)\n",
|
||||
config_.path_monitor.confirm_time, config_.path_monitor.check_period,
|
||||
blocked_on ? "ON" : "OFF",
|
||||
config_.path_monitor.path_blocked_enabled ? "on" : "off",
|
||||
@@ -229,7 +256,38 @@ bool ControlLoop::configure(const ControlLoopConfig& config, const ControlLoopDe
|
||||
off_path_on ? "ON" : "OFF",
|
||||
config_.path_monitor.off_path_enabled ? "on" : "off",
|
||||
config_.path_monitor.max_cross_track,
|
||||
config_.state_machine.recovery_routes.off_path.size());
|
||||
config_.state_machine.recovery_routes.off_path.size(),
|
||||
collision_on ? "ON" : "OFF",
|
||||
config_.path_monitor.collision_enabled ? "on" : "off",
|
||||
config_.path_monitor.collision_lookahead,
|
||||
config_.path_monitor.collision_stop_distance,
|
||||
config_.path_monitor.collision_confirm_cycles,
|
||||
config_.path_monitor.collision_cost_threshold,
|
||||
body,
|
||||
config_.path_monitor.collision_footprint_padding,
|
||||
config_.path_monitor.collision_brake_decel,
|
||||
config_.path_monitor.collision_exclude_baseline ? "on" : "off",
|
||||
config_.path_monitor.collision_stop_time,
|
||||
config_.path_monitor.collision_stop_angle,
|
||||
config_.path_monitor.collision_lookahead_angle,
|
||||
config_.state_machine.recovery_routes.collision.size());
|
||||
const bool progress_on =
|
||||
config_.path_monitor.progress_enabled &&
|
||||
(config_.path_monitor.progress_action != "recover" ||
|
||||
!config_.state_machine.recovery_routes.oscillation.empty());
|
||||
robot::log_info(" progress : %s (flag=%s, action=%s, window=%.1f s, min_cmd=%.2f m, "
|
||||
"eff<%.2f kẹt / >=%.2f xoá, confirm=%.1f s, goal_zone=%.2f m, "
|
||||
"route=oscillation %zu)\n",
|
||||
progress_on ? "ON" : "OFF",
|
||||
config_.path_monitor.progress_enabled ? "on" : "off",
|
||||
config_.path_monitor.progress_action.c_str(),
|
||||
config_.path_monitor.progress_window,
|
||||
config_.path_monitor.progress_min_commanded,
|
||||
config_.path_monitor.progress_min_efficiency,
|
||||
config_.path_monitor.progress_clear_efficiency,
|
||||
config_.path_monitor.progress_confirm_time,
|
||||
config_.path_monitor.progress_goal_zone,
|
||||
config_.state_machine.recovery_routes.oscillation.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,6 +314,7 @@ void ControlLoop::reset()
|
||||
action_message_.clear();
|
||||
|
||||
latest_plan_.clear();
|
||||
++plan_revision_;
|
||||
planner_running_ = false;
|
||||
backup_global_planner_active_ = false;
|
||||
|
||||
@@ -584,6 +643,7 @@ void ControlLoop::collectPlannerResult()
|
||||
}
|
||||
|
||||
latest_plan_.swap(result.plan);
|
||||
++plan_revision_;
|
||||
// Tuyến THAM CHIẾU của chặng = plan do PLANNER sinh, không phải plan đã bị recovery thay
|
||||
// (contract 2026-08-05e). Detour nối lại vào nó nên robot tự quay về tuyến order thay vì trôi
|
||||
// dần theo từng đường vòng, và `max_deviation` đo đúng hành lang mà fleet đã duyệt.
|
||||
@@ -598,6 +658,141 @@ void ControlLoop::resetPathMonitor()
|
||||
has_last_path_check_ = false;
|
||||
blocked_seen_ = false;
|
||||
off_path_seen_ = false;
|
||||
collision_brake_ = false;
|
||||
collision_blocked_ = false;
|
||||
collision_streak_ = 0;
|
||||
resetProgressMonitor();
|
||||
}
|
||||
|
||||
void ControlLoop::resetProgressMonitor()
|
||||
{
|
||||
progress_head_ = 0;
|
||||
progress_count_ = 0;
|
||||
progress_cmd_accum_ = 0.0;
|
||||
progress_stall_seen_ = false;
|
||||
no_progress_ = false;
|
||||
}
|
||||
|
||||
void ControlLoop::rebuildPlanArcLength()
|
||||
{
|
||||
plan_arclen_.clear();
|
||||
plan_arclen_.reserve(latest_plan_.size());
|
||||
double s = 0.0; // [m]
|
||||
for (std::size_t i = 0; i < latest_plan_.size(); ++i)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
const double dx = latest_plan_[i].pose.position.x - latest_plan_[i - 1].pose.position.x;
|
||||
const double dy = latest_plan_[i].pose.position.y - latest_plan_[i - 1].pose.position.y;
|
||||
s += std::hypot(dx, dy);
|
||||
}
|
||||
plan_arclen_.push_back(s);
|
||||
}
|
||||
plan_arclen_revision_ = plan_revision_;
|
||||
// Mẫu trong ring đo trên plan CŨ: s_plan của chúng không so được với plan mới. Xoá ring, GIỮ
|
||||
// cmd_accum (nó đơn điệu, chỉ dùng qua hiệu số giữa hai mẫu).
|
||||
progress_head_ = 0;
|
||||
progress_count_ = 0;
|
||||
progress_stall_seen_ = false;
|
||||
no_progress_ = false;
|
||||
}
|
||||
|
||||
void ControlLoop::updateProgressMonitor(const robot::Time& now, std::size_t from_index,
|
||||
const robot_geometry_msgs::PoseStamped& robot_pose)
|
||||
{
|
||||
const ControlLoopConfig::PathMonitorConfig& cfg = config_.path_monitor;
|
||||
|
||||
// Mode "recover" cần route oscillation; mode "log" thì không cần gì — chỉ in.
|
||||
const bool route_ready = cfg.progress_action != "recover" ||
|
||||
!config_.state_machine.recovery_routes.oscillation.empty();
|
||||
const bool grace = has_last_plan_applied_ &&
|
||||
(now - last_plan_applied_).toSec() < cfg.grace_after_new_plan;
|
||||
if (!cfg.progress_enabled || !route_ready || latest_plan_.empty())
|
||||
{
|
||||
resetProgressMonitor();
|
||||
return;
|
||||
}
|
||||
if (plan_arclen_revision_ != plan_revision_)
|
||||
{
|
||||
rebuildPlanArcLength();
|
||||
}
|
||||
|
||||
const double dxg = latest_plan_.back().pose.position.x - robot_pose.pose.position.x;
|
||||
const double dyg = latest_plan_.back().pose.position.y - robot_pose.pose.position.y;
|
||||
const double d_goal = std::hypot(dxg, dyg); // [m]
|
||||
const double s_plan =
|
||||
from_index < plan_arclen_.size() ? plan_arclen_[from_index] : plan_arclen_.back(); // [m]
|
||||
|
||||
// Ghi mẫu TRƯỚC khi xét grace/goal_zone: chuỗi mẫu phải liên tục, chỉ có KẾT LUẬN là bị hoãn.
|
||||
progress_ring_[progress_head_] = ProgressSample{ now, s_plan, d_goal, progress_cmd_accum_ };
|
||||
progress_head_ = (progress_head_ + 1) % kProgressRing;
|
||||
progress_count_ = std::min(progress_count_ + 1, kProgressRing);
|
||||
|
||||
if (grace || d_goal < cfg.progress_goal_zone)
|
||||
{
|
||||
progress_stall_seen_ = false;
|
||||
no_progress_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Mẫu MỚI NHẤT mà đã đủ tuổi `progress_window` — cửa sổ đo sát W nhất, không phải mẫu cũ nhất
|
||||
// trong ring (có thể già tới 6 s và làm cửa sổ giãn ra).
|
||||
const ProgressSample* old = nullptr;
|
||||
for (std::size_t k = 1; k <= progress_count_; ++k)
|
||||
{
|
||||
const ProgressSample& sample =
|
||||
progress_ring_[(progress_head_ + kProgressRing - k) % kProgressRing];
|
||||
if ((now - sample.t).toSec() >= cfg.progress_window)
|
||||
{
|
||||
old = &sample;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (old == nullptr)
|
||||
{
|
||||
return; // chưa đủ tuổi cửa sổ: chưa kết luận, giữ nguyên trạng thái
|
||||
}
|
||||
|
||||
const double cmd = progress_cmd_accum_ - old->cmd; // [m] lệnh đã phát
|
||||
const double prog = std::max(s_plan - old->s_plan, old->d_goal - d_goal); // [m] tiến được
|
||||
const double eff = cmd > 1e-6 ? prog / cmd : 0.0; // [-]
|
||||
|
||||
const bool stalled = cmd >= cfg.progress_min_commanded && eff < cfg.progress_min_efficiency;
|
||||
// Xoá cờ CHỈ khi tiến bộ thật. `cmd < min_commanded` là "chưa đo được" chứ không phải "hết kẹt":
|
||||
// sim 4/9 mission 3 — robot kẹt 35 s, tốc độ lệnh lượn quanh đúng ngưỡng 0.1 m/s, cờ bật/tắt 5
|
||||
// lần cho MỘT đợt kẹt. Kẹt xong chậm lại (hoặc planner hold) vẫn là kẹt cho tới khi đi được.
|
||||
const bool cleared = cmd >= cfg.progress_min_commanded && eff >= cfg.progress_clear_efficiency;
|
||||
|
||||
if (stalled)
|
||||
{
|
||||
if (!progress_stall_seen_)
|
||||
{
|
||||
progress_stall_since_ = now;
|
||||
progress_stall_seen_ = true;
|
||||
}
|
||||
if (!no_progress_ && (now - progress_stall_since_).toSec() >= cfg.progress_confirm_time)
|
||||
{
|
||||
no_progress_ = true;
|
||||
// KHÔNG gắn thẻ [debug], KHÔNG cho tắt: đây là chẩn đoán "robot kẹt", in đúng một lần mỗi đợt.
|
||||
robot::log_warning("[move_base2] PathMonitor: no progress — commanded %.2f m in %.1f s but "
|
||||
"advanced %.2f m (eff %.2f), %.2f m to goal — %s.\n",
|
||||
cmd, cfg.progress_window, prog, eff, d_goal,
|
||||
cfg.progress_action == "recover" ? "escalating to recovery"
|
||||
: "LOG ONLY (progress_action: log)");
|
||||
}
|
||||
}
|
||||
else if (cleared)
|
||||
{
|
||||
if (no_progress_ && config_.debug_state)
|
||||
{
|
||||
robot::log_info("[move_base2][debug] PathMonitor: progress resumed (eff %.2f over %.2f m "
|
||||
"commanded).\n", eff, cmd);
|
||||
}
|
||||
progress_stall_seen_ = false;
|
||||
no_progress_ = false;
|
||||
}
|
||||
// Còn lại — cmd < min_commanded (chờ / lết), hoặc eff trong vùng chết min..clear — giữ nguyên
|
||||
// trạng thái (hysteresis). Không kết luận ≠ hết kẹt.
|
||||
}
|
||||
|
||||
bool ControlLoop::planIsBlocked(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
||||
@@ -725,6 +920,257 @@ void ControlLoop::updatePathMonitor(const robot::Time& now,
|
||||
off_path_seen_ = false;
|
||||
off_path_ = false;
|
||||
}
|
||||
|
||||
// --- Phép đo 4: planner đòi đi mà robot không tiến ------------------------------------------
|
||||
updateProgressMonitor(now, from, robot_pose);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
/// @brief Liệt kê các ô baseline "(x, y) (x, y) ..." [m, frame base_link]; "-" khi rỗng.
|
||||
void describeBaselineCells(const MotionBlockResult& result, char* out, std::size_t size)
|
||||
{
|
||||
std::size_t used = 0;
|
||||
out[0] = '\0';
|
||||
if (result.baseline_listed <= 0)
|
||||
{
|
||||
std::snprintf(out, size, "-");
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < result.baseline_listed && used + 1 < size; ++i)
|
||||
{
|
||||
const int n = std::snprintf(out + used, size - used, "%s(%.2f, %.2f)", i > 0 ? " " : "",
|
||||
result.baseline_xy[i][0], result.baseline_xy[i][1]);
|
||||
if (n < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
used += static_cast<std::size_t>(n);
|
||||
}
|
||||
if (result.baseline_cells > result.baseline_listed && used + 1 < size)
|
||||
{
|
||||
std::snprintf(out + used, size - used, " +%d more", result.baseline_cells - result.baseline_listed);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void ControlLoop::applyCollisionGuard(const robot_geometry_msgs::PoseStamped& robot_pose,
|
||||
bool pose_available, robot_geometry_msgs::Twist& candidate)
|
||||
{
|
||||
const ControlLoopConfig::PathMonitorConfig& cfg = config_.path_monitor;
|
||||
|
||||
// Cùng cổng vào với hai phép đo kia, cùng lý do: chỉ đo khi thực sự đang bám plan của một profile
|
||||
// được phép và có đường xử lý. Không có route thì dừng-mà-không-ai-xử là ABORT trá hình.
|
||||
const bool armed = cfg.enabled && cfg.collision_enabled && deps_.path_check != nullptr &&
|
||||
!config_.state_machine.recovery_routes.collision.empty() &&
|
||||
has_active_request_ && pose_available &&
|
||||
state_machine_.state() == NavigationState::kControlling &&
|
||||
cfg.monitors(active_request_.profile);
|
||||
if (!armed)
|
||||
{
|
||||
collision_brake_ = false;
|
||||
collision_blocked_ = false;
|
||||
collision_streak_ = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Thân robot: khai tay đủ ba số, hoặc cả ba = 0 thì lấy từ footprint costmap mỗi cycle (rẻ: một
|
||||
// vector 4 điểm). Không có cả hai nguồn thì đây là "không kết luận được" — không dừng, không xoá
|
||||
// chuỗi xác nhận — nhưng phải kêu, vì guard đang mù mà cấu hình bảo là bật.
|
||||
double front = cfg.collision_front_offset;
|
||||
double rear = cfg.collision_rear_offset;
|
||||
double side = cfg.collision_side_offset;
|
||||
const bool body_from_costmap = !(front > 0.0) && !(rear > 0.0) && !(side > 0.0);
|
||||
if (body_from_costmap)
|
||||
{
|
||||
if (!deps_.path_check->robotBodyExtents(front, rear, side))
|
||||
{
|
||||
robot::log_error_throttle(5.0,
|
||||
"[move_base2] PathMonitor: collision guard has no body geometry — "
|
||||
"the costmap footprint is unavailable and collision_*_offset are "
|
||||
"not set; the commanded arc is NOT checked.\n");
|
||||
collision_brake_ = false;
|
||||
return;
|
||||
}
|
||||
if (!collision_body_logged_ || std::fabs(front - collision_body_front_) > 1e-3 ||
|
||||
std::fabs(rear - collision_body_rear_) > 1e-3 || std::fabs(side - collision_body_side_) > 1e-3)
|
||||
{
|
||||
collision_body_logged_ = true;
|
||||
collision_body_front_ = front;
|
||||
collision_body_rear_ = rear;
|
||||
collision_body_side_ = side;
|
||||
robot::log_info("[move_base2] PathMonitor: collision body from the costmap footprint: "
|
||||
"front=%.3f rear=%.3f side=%.3f m (+%.2f m padding).\n",
|
||||
front, rear, side, cfg.collision_footprint_padding);
|
||||
}
|
||||
}
|
||||
|
||||
MotionCheckParams params;
|
||||
params.lookahead = cfg.collision_lookahead;
|
||||
params.max_samples = cfg.collision_max_samples;
|
||||
params.front_offset = front;
|
||||
params.rear_offset = rear;
|
||||
params.side_offset = side;
|
||||
params.padding = cfg.collision_footprint_padding;
|
||||
params.cost_threshold = static_cast<unsigned char>(cfg.collision_cost_threshold);
|
||||
params.min_speed = cfg.collision_min_speed;
|
||||
params.exclude_baseline = cfg.collision_exclude_baseline;
|
||||
params.lookahead_angle = cfg.collision_lookahead_angle;
|
||||
|
||||
MotionBlockResult result;
|
||||
if (!deps_.path_check->checkMotion(robot_pose, candidate.linear.x, candidate.angular.z, params,
|
||||
result))
|
||||
{
|
||||
// Không kết luận được (TF/costmap) — không biết thì không dừng, cũng không xoá chuỗi xác nhận.
|
||||
collision_brake_ = false;
|
||||
return;
|
||||
}
|
||||
if (result.scanned == 0)
|
||||
{
|
||||
// Xoay tại chỗ / v dưới min_speed: cung suy biến, không có bằng chứng mới.
|
||||
collision_brake_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!result.blocked)
|
||||
{
|
||||
collision_brake_ = false;
|
||||
collision_streak_ = 0;
|
||||
if (collision_blocked_)
|
||||
{
|
||||
collision_blocked_ = false;
|
||||
robot::log_info("[move_base2] PathMonitor: the commanded arc is clear again.\n");
|
||||
}
|
||||
if (result.baseline_cells > 0 && config_.debug_state)
|
||||
{
|
||||
// Người vận hành thấy robot "đang chạm" mà vẫn đi: phải nói rõ vì sao được đi, và ô nào.
|
||||
char cells[160];
|
||||
describeBaselineCells(result, cells, sizeof(cells));
|
||||
robot::log_info_throttle(2.0,
|
||||
"[move_base2][debug] PathMonitor: body overlaps %d LETHAL cell(s) "
|
||||
"but the commanded arc adds none — letting it move (v=%.2f m/s, "
|
||||
"omega=%.2f rad/s); cells in base frame: %s\n",
|
||||
result.baseline_cells, candidate.linear.x, candidate.angular.z,
|
||||
cells);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
collision_hit_distance_ = result.distance;
|
||||
if (result.distance > cfg.collision_stop_distance)
|
||||
{
|
||||
// Xa hơn quãng dừng: chưa phải dừng, nhưng không được lao vào ở tốc độ tối đa. Kẹp theo quãng
|
||||
// còn lại — cùng công thức mọi tầng phanh khác trong stack.
|
||||
collision_brake_ = false;
|
||||
collision_streak_ = 0;
|
||||
if (cfg.collision_brake_decel > 0.0)
|
||||
{
|
||||
const double room = result.distance - cfg.collision_stop_distance;
|
||||
const double cap = std::sqrt(2.0 * cfg.collision_brake_decel * room);
|
||||
if (std::fabs(candidate.linear.x) > cap)
|
||||
{
|
||||
// Kẹp có log (throttle): 4/9 không tách được cap của guard với head_slowdown của planner
|
||||
// vì cap chạy im lặng.
|
||||
robot::log_info_throttle(2.0,
|
||||
"[move_base2] PathMonitor: contact %.2f m along the commanded arc "
|
||||
"— v capped %.2f -> %.2f m/s.\n",
|
||||
result.distance, candidate.linear.x, cap);
|
||||
candidate.linear.x = candidate.linear.x < 0.0 ? -cap : cap;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Trong quãng dừng theo MÉT. Nhưng mét dọc cung chưa phải "sắp chạm" (2026-09-04, run 16:26,
|
||||
// 11 lần nổ trong 15 s khi xoay gần tại chỗ ở góc trụ): cùng 0.15 m cung, đi thẳng 0.36 m/s là
|
||||
// chạm sau 0.4 s, còn xoay bò v 0.04 ω −0.5 (R 0.08 m) là quay 107° và 3.8 s nữa — PRIEST đã
|
||||
// thôi xoay sau 30–60° để thẳng hàng với path. Guard mô hình lệnh là cung không đổi nên không
|
||||
// biết điều đó; thước đo phải là thời gian và góc. Chưa "sắp" thì không dừng, không đếm, không
|
||||
// giơ cờ — chỉ kẹp v để luôn còn >= stop_time tới chạm; guard soi lại mỗi cycle, PRIEST cứ xoay
|
||||
// tiếp thì `turn` tụt dần và tới ngưỡng là dừng thật.
|
||||
const double speed = std::fabs(candidate.linear.x);
|
||||
const double kappa = speed > 1e-6 ? candidate.angular.z / candidate.linear.x : 0.0;
|
||||
const double time_to_contact = speed > 1e-6 ? result.distance / speed
|
||||
: std::numeric_limits<double>::infinity();
|
||||
const double turn_to_contact = std::fabs(kappa) * result.distance;
|
||||
const double creep_cap =
|
||||
std::max(0.0, result.distance - cfg.collision_footprint_padding) / cfg.collision_stop_time;
|
||||
// Kẹp còn thấp hơn min_speed là "không cho đi" trá hình: coi là sắp chạm để đường leo thang
|
||||
// (no_valid_command -> controller_patience) còn chạy, thay vì giữ 0 vô hạn mà không ai biết.
|
||||
const bool imminent = time_to_contact <= cfg.collision_stop_time ||
|
||||
turn_to_contact <= cfg.collision_stop_angle ||
|
||||
creep_cap < cfg.collision_min_speed;
|
||||
if (!imminent)
|
||||
{
|
||||
collision_brake_ = false;
|
||||
collision_streak_ = 0;
|
||||
if (collision_blocked_)
|
||||
{
|
||||
collision_blocked_ = false;
|
||||
robot::log_info("[move_base2] PathMonitor: the commanded arc is clear again.\n");
|
||||
}
|
||||
if (speed > creep_cap)
|
||||
{
|
||||
candidate.linear.x = candidate.linear.x < 0.0 ? -creep_cap : creep_cap;
|
||||
}
|
||||
if (config_.debug_state)
|
||||
{
|
||||
robot::log_info_throttle(1.0,
|
||||
"[move_base2][debug] PathMonitor: contact %.2f m along the arc but "
|
||||
"%.1f s and %.2f rad away — turning, not stopping; v capped %.2f -> "
|
||||
"%.2f m/s (omega %.2f rad/s).\n",
|
||||
result.distance, time_to_contact, turn_to_contact, speed,
|
||||
creep_cap, candidate.angular.z);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Sắp chạm thật: lệnh ra bằng 0 NGAY cycle này (trọng tài phát 0 tức thì), rồi đếm xác nhận.
|
||||
collision_brake_ = true;
|
||||
++collision_streak_;
|
||||
|
||||
// (A) Lệnh vừa bị huỷ thì với state machine nó KHÔNG hợp lệ. Trước đây guard ép lệnh về 0 nhưng
|
||||
// để nguyên `command_valid` của controller: khi trigger path_blocked đã bị khoá (replan thất bại,
|
||||
// 3/9 t=723 và 4/9 t=225) state machine thấy controller vẫn "ổn", controller_patience không bao
|
||||
// giờ chạy, robot đứng chết vô hạn không một dòng log. Báo đúng sự thật thì: trigger còn mở ->
|
||||
// replan như cũ; trigger đã khoá -> controller_patience -> route controlling_failed -> ABORT.
|
||||
controller_feedback_ = ControllerFeedback::kNoValidCommand;
|
||||
|
||||
if (!collision_blocked_ && collision_streak_ >= cfg.collision_confirm_cycles)
|
||||
{
|
||||
collision_blocked_ = true;
|
||||
robot::log_warning("[move_base2] PathMonitor: the body would hit a real obstacle %.2f m along "
|
||||
"the commanded arc (v=%.2f m/s, omega=%.2f rad/s, stop=%.2f m; contact in "
|
||||
"%.1f s / %.2f rad) — stopping to replan (ignoring %d LETHAL cell(s) already "
|
||||
"under the body).\n",
|
||||
result.distance, candidate.linear.x, candidate.angular.z,
|
||||
cfg.collision_stop_distance, time_to_contact, turn_to_contact,
|
||||
result.baseline_cells);
|
||||
if (config_.debug_state)
|
||||
{
|
||||
// Hình học của cú chạm, frame base_link (x tới, y trái): cạnh nào chạm, ô chạm ở đâu, và
|
||||
// những ô đã nằm dưới thân bị bỏ qua. Đây là thứ duy nhất cho phép phân biệt "góc thân quét
|
||||
// tới" với "người bước tới" khi đọc log mà không có costmap.
|
||||
static const char* const kEdge[4] = { "right", "nose", "left", "tail" };
|
||||
char cells[160];
|
||||
describeBaselineCells(result, cells, sizeof(cells));
|
||||
robot::log_info("[move_base2][debug] PathMonitor: hit at sample %d: %s edge touches cell "
|
||||
"(%.2f, %.2f) in base frame; baseline %d cell(s): %s\n",
|
||||
result.sample,
|
||||
result.hit_edge >= 0 && result.hit_edge < 4 ? kEdge[result.hit_edge] : "?",
|
||||
result.hit_x, result.hit_y, result.baseline_cells, cells);
|
||||
}
|
||||
}
|
||||
else if (collision_blocked_)
|
||||
{
|
||||
// (B) Phanh kéo dài không được im: người đọc log phải thấy robot đang bị GIỮ, vì sao, và ai
|
||||
// sẽ leo thang.
|
||||
robot::log_warning_throttle(2.0,
|
||||
"[move_base2] PathMonitor: still braking (%d cycles, contact %.2f m, "
|
||||
"controller wants v=%.2f) — reported as no valid command; if the "
|
||||
"path_blocked trigger is locked, controller_patience escalates.\n",
|
||||
collision_streak_, result.distance, candidate.linear.x);
|
||||
}
|
||||
}
|
||||
|
||||
void ControlLoop::runController(robot_geometry_msgs::Twist& candidate)
|
||||
@@ -815,8 +1261,12 @@ bool ControlLoop::step()
|
||||
input.costmap_current = !costmap_stale;
|
||||
input.robot_stopped = arbiter_.stopped();
|
||||
input.travelled_since_oscillation_reset = travelled;
|
||||
// Phép đo 3 dùng chung route với phép đo 1: cả hai cùng một cách xử lý — lập lại đường.
|
||||
input.path_blocked = path_blocked_;
|
||||
input.collision = collision_blocked_; // Phép đo 3 có trigger/route riêng (2026-09-04)
|
||||
input.off_path = off_path_;
|
||||
// Phép đo 4 chỉ được leo thang ở mode "recover"; mode "log" đã in cảnh báo và dừng ở đó.
|
||||
input.no_progress = no_progress_ && config_.path_monitor.progress_action == "recover";
|
||||
|
||||
// Cùng một chỉ số dùng cho cả cycle khởi động recovery lẫn các cycle tick: state machine chỉ tăng
|
||||
// chỉ số khi behavior kết thúc, nên `nextRecoveryIndex()` chính là behavior sắp chạy hoặc đang
|
||||
@@ -866,6 +1316,7 @@ bool ControlLoop::step()
|
||||
has_active_request_ = true;
|
||||
has_pending_request_ = false;
|
||||
latest_plan_.clear();
|
||||
++plan_revision_;
|
||||
order_plan_.clear();
|
||||
has_outcome_ = false;
|
||||
|
||||
@@ -963,7 +1414,20 @@ bool ControlLoop::step()
|
||||
toString(output.recovery_trigger), config_.state_machine.oscillation_distance);
|
||||
}
|
||||
|
||||
// Recovery vừa nộp path mới nhưng robot chưa dịch chuyển kể từ path mới trước đó: state machine
|
||||
// tính lần đó vào ngân sách route thay vì nạp lại (2026-09-04). Không gate: đây là dấu hiệu robot
|
||||
// đang kẹt tại chỗ và mỗi lượt như vậy tiến gần ABORT thêm một nấc — người vận hành phải thấy.
|
||||
if (output.recovery_reset_denied)
|
||||
{
|
||||
robot::log_warning("[move_base2] recovery produced a new path but the robot has not moved since "
|
||||
"the previous one (%.2f m, need %.2f m) — counting it against the %s route "
|
||||
"budget instead of refilling it.\n",
|
||||
output.recovery_reset_travel, config_.state_machine.recovery_reset_min_travel,
|
||||
toString(output.recovery_trigger));
|
||||
}
|
||||
|
||||
robot_geometry_msgs::Twist candidate;
|
||||
collision_brake_ = false; // chỉ applyCollisionGuard của CYCLE NÀY được bật lại.
|
||||
|
||||
if (output.start_recovery)
|
||||
{
|
||||
@@ -997,6 +1461,7 @@ bool ControlLoop::step()
|
||||
{
|
||||
// Họ recovery sinh lại đường đi: coi kết quả như một plan mới, chờ state machine đẩy xuống.
|
||||
latest_plan_ = tick.path;
|
||||
++plan_revision_;
|
||||
planner_feedback_ = PlannerFeedback::kPlanReady;
|
||||
if (config_.debug_plan)
|
||||
{
|
||||
@@ -1110,6 +1575,11 @@ bool ControlLoop::step()
|
||||
if (output.run_controller && !costmap_stale)
|
||||
{
|
||||
runController(candidate);
|
||||
if (controller_feedback_ == ControllerFeedback::kCommandValid)
|
||||
{
|
||||
// Phép đo 3 của PathMonitor: soi chính lệnh sắp phát, TRƯỚC trọng tài.
|
||||
applyCollisionGuard(robot_pose, pose_available, candidate);
|
||||
}
|
||||
if (controller_feedback_ == ControllerFeedback::kNoValidCommand)
|
||||
{
|
||||
if (config_.debug_skip)
|
||||
@@ -1174,7 +1644,16 @@ bool ControlLoop::step()
|
||||
}
|
||||
|
||||
// --- 4. Lệnh vận tốc ------------------------------------------------------------------------
|
||||
arbiter_.arbitrate(costmap_stale ? VelocitySource::kNone : output.velocity_source, candidate, dt);
|
||||
// Sắp chạm vật cản thật cũng là "không ai được phát": lệnh 0 tức thì, không giảm dần.
|
||||
arbiter_.arbitrate((costmap_stale || collision_brake_) ? VelocitySource::kNone
|
||||
: output.velocity_source,
|
||||
candidate, dt);
|
||||
// Phép đo 4 đo lệnh SAU trọng tài: hold của planner, phanh của Phép đo 3, costmap stale đều ra
|
||||
// 0 ở đây — đó chính là cách nó phân biệt "đang chờ" với "đòi đi mà không tiến".
|
||||
if (dt > 0.0)
|
||||
{
|
||||
progress_cmd_accum_ += std::fabs(arbiter_.lastCommand().linear.x) * dt; // [m]
|
||||
}
|
||||
|
||||
// --- 5. Báo kết quả -------------------------------------------------------------------------
|
||||
if (output.report_outcome)
|
||||
@@ -1231,6 +1710,7 @@ bool ControlLoop::step()
|
||||
{
|
||||
has_active_request_ = false;
|
||||
latest_plan_.clear();
|
||||
++plan_revision_;
|
||||
planner_running_ = false;
|
||||
}
|
||||
cancel_requested_ = false;
|
||||
|
||||
@@ -8,6 +8,12 @@
|
||||
*********************************************************************/
|
||||
#include <move_base2/navigation_runtime.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <move_base2/io/sensor_store_types.h>
|
||||
|
||||
#include <exception>
|
||||
@@ -89,6 +95,77 @@ bool CostmapPathCheck::checkPlan(const std::vector<robot_geometry_msgs::PoseStam
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CostmapPathCheck::checkMotion(const robot_geometry_msgs::PoseStamped& robot_pose, double v,
|
||||
double omega, const MotionCheckParams& params,
|
||||
MotionBlockResult& out) const
|
||||
{
|
||||
if (costmap_ == nullptr || !tf_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
robot_costmap_2d::Costmap2D* grid = costmap_->getCostmap();
|
||||
if (grid == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Pose robot ở frame của cổng pose (thường `map`), lưới ở `odom`: quy đổi cả vị trí lẫn hướng
|
||||
// rồi tích phân cung ngay trong frame lưới — mỗi mẫu không phải tra TF lần nữa.
|
||||
PlanarTransform pose_to_grid;
|
||||
if (!planarTransform(*tf_, robot_pose.header.frame_id, costmap_->getGlobalFrameID(),
|
||||
pose_to_grid))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
double gx = 0.0;
|
||||
double gy = 0.0;
|
||||
pose_to_grid.apply(robot_pose.pose.position.x, robot_pose.pose.position.y, gx, gy);
|
||||
const auto& q = robot_pose.pose.orientation;
|
||||
const double yaw_pose = std::atan2(2.0 * (q.w * q.z + q.x * q.y),
|
||||
1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
||||
const double yaw_grid = yaw_pose + std::atan2(pose_to_grid.sin_yaw, pose_to_grid.cos_yaw);
|
||||
|
||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*(grid->getMutex()));
|
||||
out = move_base2::checkMotion(gx, gy, yaw_grid, v, omega, params, *grid, &baseline_scratch_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CostmapPathCheck::robotBodyExtents(double& front, double& rear, double& side) const
|
||||
{
|
||||
if (costmap_ == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Footprint KHÔNG đệm: guard có collision_footprint_padding riêng, cộng đệm costmap vào nữa là
|
||||
// đếm biên hai lần. Đây cũng là hình mà SBPL/PRIEST dùng, nên ba bên cùng một thân.
|
||||
const std::vector<robot_geometry_msgs::Point>& fp = costmap_->getUnpaddedRobotFootprint();
|
||||
if (fp.size() < 3)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
double max_x = std::numeric_limits<double>::lowest();
|
||||
double min_x = std::numeric_limits<double>::max();
|
||||
double max_abs_y = 0.0;
|
||||
for (const robot_geometry_msgs::Point& p : fp)
|
||||
{
|
||||
if (!std::isfinite(p.x) || !std::isfinite(p.y))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
max_x = std::max(max_x, p.x);
|
||||
min_x = std::min(min_x, p.x);
|
||||
max_abs_y = std::max(max_abs_y, std::fabs(p.y));
|
||||
}
|
||||
if (!(max_x > 0.0) || !(min_x < 0.0) || !(max_abs_y > 0.0))
|
||||
{
|
||||
return false; // suy biến hoặc base_link nằm ngoài thân — không tin được
|
||||
}
|
||||
front = max_x;
|
||||
rear = -min_x;
|
||||
side = max_abs_y;
|
||||
return true;
|
||||
}
|
||||
|
||||
NavigationRuntime::NavigationRuntime() = default;
|
||||
|
||||
NavigationRuntime::~NavigationRuntime()
|
||||
|
||||
@@ -104,6 +104,8 @@ const char* toString(RecoveryTrigger trigger)
|
||||
return "path_blocked";
|
||||
case RecoveryTrigger::kOffPath:
|
||||
return "off_path";
|
||||
case RecoveryTrigger::kCollision:
|
||||
return "collision";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
@@ -122,6 +124,8 @@ const std::vector<std::size_t>& RecoveryRoutes::forTrigger(RecoveryTrigger trigg
|
||||
return path_blocked;
|
||||
case RecoveryTrigger::kOffPath:
|
||||
return off_path;
|
||||
case RecoveryTrigger::kCollision:
|
||||
return collision;
|
||||
}
|
||||
return planning_failed;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <exception>
|
||||
|
||||
#include <robot_costmap_2d/costmap_2d.h>
|
||||
@@ -191,4 +192,226 @@ PlanBlockResult checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& p
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
/// [m] Quãng đầu cung bị bỏ qua — chỗ robot đang đứng. Xem @ref checkMotion.
|
||||
constexpr double kSkipOwnFootprintDist = 0.05;
|
||||
|
||||
/// Một điểm dò: ra ngoài lưới hay chưa có thông tin đều là KHÔNG chạm.
|
||||
bool probeHits(const robot_costmap_2d::Costmap2D& grid, double x, double y,
|
||||
unsigned char threshold, const std::vector<unsigned int>& baseline)
|
||||
{
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
if (!grid.worldToMap(x, y, mx, my))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const unsigned char cost = grid.getCost(mx, my);
|
||||
if (cost == robot_costmap_2d::NO_INFORMATION || cost < threshold)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Ô đã nằm dưới thân tại pose hiện tại: lệnh này không "đưa thêm" nó vào thân.
|
||||
return baseline.empty() || !std::binary_search(baseline.begin(), baseline.end(), grid.getIndex(mx, my));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gom các ô >= @p threshold đang nằm dưới hình chữ nhật thân đệm tại pose hiện tại.
|
||||
*
|
||||
* Duyệt DIỆN TÍCH chứ không phải chu vi: ô nằm sâu trong thân mà chỉ lấy chu vi thì mẫu sau quét
|
||||
* qua nó và tính oan là "mới". Ô được coi là dưới thân khi TÂM ô nằm trong hình chữ nhật nới thêm
|
||||
* nửa ô — cùng độ rộng với những gì probe chu vi (bước nửa ô) có thể chạm tới ở s = 0. Kết quả
|
||||
* được sort để probe tra bằng binary_search. ~600 ô ở lưới 0.05 m; kiểm cost trước khi quy đổi
|
||||
* hình học nên phần lớn ô thoát sớm.
|
||||
*/
|
||||
void collectBaseline(const robot_costmap_2d::Costmap2D& grid, double x, double y, double yaw,
|
||||
double front, double rear, double side, unsigned char threshold,
|
||||
std::vector<unsigned int>& out, MotionBlockResult& result)
|
||||
{
|
||||
out.clear();
|
||||
result.baseline_listed = 0;
|
||||
const double c = std::cos(yaw);
|
||||
const double sn = std::sin(yaw);
|
||||
const double corners_x[4] = { front, front, -rear, -rear };
|
||||
const double corners_y[4] = { side, -side, side, -side };
|
||||
double min_wx = std::numeric_limits<double>::max();
|
||||
double min_wy = std::numeric_limits<double>::max();
|
||||
double max_wx = std::numeric_limits<double>::lowest();
|
||||
double max_wy = std::numeric_limits<double>::lowest();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
const double wx = x + corners_x[i] * c - corners_y[i] * sn;
|
||||
const double wy = y + corners_x[i] * sn + corners_y[i] * c;
|
||||
min_wx = std::min(min_wx, wx);
|
||||
max_wx = std::max(max_wx, wx);
|
||||
min_wy = std::min(min_wy, wy);
|
||||
max_wy = std::max(max_wy, wy);
|
||||
}
|
||||
int mx0 = 0;
|
||||
int my0 = 0;
|
||||
int mx1 = 0;
|
||||
int my1 = 0;
|
||||
grid.worldToMapEnforceBounds(min_wx, min_wy, mx0, my0);
|
||||
grid.worldToMapEnforceBounds(max_wx, max_wy, mx1, my1);
|
||||
const double slack = 0.5 * grid.getResolution();
|
||||
for (int my = my0; my <= my1; ++my)
|
||||
{
|
||||
for (int mx = mx0; mx <= mx1; ++mx)
|
||||
{
|
||||
const unsigned char cost = grid.getCost(static_cast<unsigned int>(mx), static_cast<unsigned int>(my));
|
||||
if (cost == robot_costmap_2d::NO_INFORMATION || cost < threshold)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
double wx = 0.0;
|
||||
double wy = 0.0;
|
||||
grid.mapToWorld(static_cast<unsigned int>(mx), static_cast<unsigned int>(my), wx, wy);
|
||||
const double dx = wx - x;
|
||||
const double dy = wy - y;
|
||||
const double bx = dx * c + dy * sn;
|
||||
const double by = -dx * sn + dy * c;
|
||||
if (bx < -rear - slack || bx > front + slack || std::fabs(by) > side + slack)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
out.push_back(grid.getIndex(static_cast<unsigned int>(mx), static_cast<unsigned int>(my)));
|
||||
if (result.baseline_listed < MotionBlockResult::kMaxListedBaseline)
|
||||
{
|
||||
result.baseline_xy[result.baseline_listed][0] = bx;
|
||||
result.baseline_xy[result.baseline_listed][1] = by;
|
||||
++result.baseline_listed;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::sort(out.begin(), out.end());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
MotionBlockResult checkMotion(double x, double y, double yaw, double v, double omega,
|
||||
const MotionCheckParams& params,
|
||||
const robot_costmap_2d::Costmap2D& grid,
|
||||
std::vector<unsigned int>* baseline_scratch)
|
||||
{
|
||||
MotionBlockResult result;
|
||||
if (std::fabs(v) < params.min_speed || !(params.lookahead > 0.0) || params.max_samples < 1)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
const double direction = v < 0.0 ? -1.0 : 1.0;
|
||||
// Độ cong của cung: omega / v. Lùi thì cung lật chiều cùng với v — công thức giữ nguyên dấu.
|
||||
const double kappa = omega / v;
|
||||
// Bước dò không được lớn hơn một ô lưới: với bước 0.13 m mũi robot ở mẫu này còn cách tường
|
||||
// mỏng 1 ô, mẫu sau đã ở BÊN KIA tường — cả hai đều sạch, robot đâm xuyên. max_samples chỉ là
|
||||
// trần thưa; lưới mịn hơn thì dò dày hơn, tối đa lookahead/resolution mẫu (16 ở 0.8 m / 0.05).
|
||||
double step = params.lookahead / params.max_samples;
|
||||
const double resolution = grid.getResolution();
|
||||
if (resolution > 0.0 && step > resolution)
|
||||
{
|
||||
step = resolution;
|
||||
}
|
||||
const int samples = static_cast<int>(std::ceil(params.lookahead / step - 1.0e-9));
|
||||
const double front = params.front_offset + params.padding;
|
||||
const double rear = params.rear_offset + params.padding;
|
||||
const double side = params.side_offset + params.padding;
|
||||
const double sin0 = std::sin(yaw);
|
||||
const double cos0 = std::cos(yaw);
|
||||
|
||||
std::vector<unsigned int> local_baseline;
|
||||
std::vector<unsigned int>& baseline = baseline_scratch != nullptr ? *baseline_scratch : local_baseline;
|
||||
baseline.clear();
|
||||
if (params.exclude_baseline)
|
||||
{
|
||||
collectBaseline(grid, x, y, yaw, front, rear, side, params.cost_threshold, baseline, result);
|
||||
}
|
||||
result.baseline_cells = static_cast<int>(baseline.size());
|
||||
|
||||
for (int i = 1; i <= samples; ++i)
|
||||
{
|
||||
const double walk = std::min(step * i, params.lookahead); // [m] dọc cung, luôn dương
|
||||
if (walk < kSkipOwnFootprintDist)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (std::fabs(kappa) * walk > params.lookahead_angle)
|
||||
{
|
||||
break; // quay quá góc này thì lệnh đã đổi — phần cung còn lại không phải tương lai thật
|
||||
}
|
||||
// Vị trí trên cung sau quãng `walk` theo chiều `direction`.
|
||||
double px = 0.0;
|
||||
double py = 0.0;
|
||||
double pyaw = yaw;
|
||||
const double s = direction * walk; // quãng có dấu
|
||||
if (std::fabs(kappa) < 1.0e-6)
|
||||
{
|
||||
px = x + s * cos0;
|
||||
py = y + s * sin0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pyaw = yaw + kappa * s;
|
||||
px = x + (std::sin(pyaw) - sin0) / kappa;
|
||||
py = y - (std::cos(pyaw) - cos0) / kappa;
|
||||
}
|
||||
++result.scanned;
|
||||
|
||||
// Dò TOÀN BỘ CHU VI footprint đã đệm tại mẫu này, không phải 5 điểm rời. Năm điểm rời kéo dọc
|
||||
// cung là năm đường quét, giữa chúng hở tới nửa bề rộng thân: hai trụ cách nhau 0.45 m nằm lọt
|
||||
// giữa đường quét mũi (ngang 0) và đường quét hông (ngang ±0.53) — không đường nào chạm, robot
|
||||
// ôm cả hai trụ mới dừng (đo 4/9: near=0.36 m @ +70° = góc trước-trái thân). Bỏ inflation thì
|
||||
// trụ chỉ còn 2-3 ô, dò điểm rời chắc chắn lọt; "dò vật thật" bắt buộc đi kèm "dò kín thân".
|
||||
// Chu vi lấy mẫu dày hơn một ô, và bước dọc cung không quá một ô, nên một ô vật cản muốn vào
|
||||
// trong footprint bắt buộc phải cắt chu vi ở một mẫu nào đó — không còn khe hình học.
|
||||
const double c = std::cos(pyaw);
|
||||
const double sn = std::sin(pyaw);
|
||||
const double spacing = resolution > 0.0 ? 0.5 * resolution : 0.025;
|
||||
// Bốn đỉnh trong khung thân: x dọc heading, y sang trái.
|
||||
const double x_min = -rear;
|
||||
const double x_max = front;
|
||||
const double y_max = side; // side đã gồm padding; side_offset = 0 -> dải rộng 2*padding
|
||||
const double y_min = -side;
|
||||
auto probe_body = [&](double bx, double by) {
|
||||
const double wx = px + bx * c - by * sn;
|
||||
const double wy = py + bx * sn + by * c;
|
||||
if (!probeHits(grid, wx, wy, params.cost_threshold, baseline))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Ghi ô chạm theo frame robot HIỆN TẠI (không phải thân đã dịch tới mẫu s) để log đọc được.
|
||||
const double dx = wx - x;
|
||||
const double dy = wy - y;
|
||||
result.hit_x = dx * cos0 + dy * sin0;
|
||||
result.hit_y = -dx * sin0 + dy * cos0;
|
||||
return true;
|
||||
};
|
||||
auto probe_edge = [&](int edge, double x0, double y0, double x1, double y1) {
|
||||
const double len = std::hypot(x1 - x0, y1 - y0);
|
||||
const int n = std::max(1, static_cast<int>(std::ceil(len / spacing)));
|
||||
for (int e = 0; e <= n; ++e)
|
||||
{
|
||||
const double t = static_cast<double>(e) / n;
|
||||
if (probe_body(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t))
|
||||
{
|
||||
result.hit_edge = edge;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const bool hit = probe_edge(0, x_min, y_min, x_max, y_min) || // cạnh phải
|
||||
probe_edge(1, x_max, y_min, x_max, y_max) || // cạnh mũi
|
||||
probe_edge(2, x_max, y_max, x_min, y_max) || // cạnh trái
|
||||
probe_edge(3, x_min, y_max, x_min, y_min); // cạnh đuôi
|
||||
if (hit)
|
||||
{
|
||||
result.blocked = true;
|
||||
result.sample = i;
|
||||
result.distance = walk;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace move_base2
|
||||
|
||||
@@ -199,6 +199,7 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
|
||||
const std::pair<const char*, RecoveryTrigger> optional_routes[] = {
|
||||
{"path_blocked", RecoveryTrigger::kPathBlocked},
|
||||
{"off_path", RecoveryTrigger::kOffPath},
|
||||
{"collision", RecoveryTrigger::kCollision},
|
||||
};
|
||||
|
||||
const auto is_expected_key = [&expected_routes, &optional_routes](const std::string& key) {
|
||||
@@ -247,6 +248,8 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
|
||||
return &routes_.path_blocked;
|
||||
case RecoveryTrigger::kOffPath:
|
||||
return &routes_.off_path;
|
||||
case RecoveryTrigger::kCollision:
|
||||
return &routes_.collision;
|
||||
}
|
||||
return &routes_.planning_failed;
|
||||
};
|
||||
@@ -341,10 +344,10 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
|
||||
}
|
||||
|
||||
robot::log_info("[move_base2] RecoveryRunner: routes resolved: planning=%zu controlling=%zu "
|
||||
"oscillation=%zu path_blocked=%zu off_path=%zu.",
|
||||
"oscillation=%zu path_blocked=%zu off_path=%zu collision=%zu.",
|
||||
routes_.planning_failed.size(), routes_.controlling_failed.size(),
|
||||
routes_.oscillation.size(), routes_.path_blocked.size(),
|
||||
routes_.off_path.size());
|
||||
routes_.off_path.size(), routes_.collision.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,17 @@ bool StateMachineConfig::validate(std::string& error) const
|
||||
"every cycle counts as oscillating";
|
||||
return false;
|
||||
}
|
||||
if (recovery_reset_min_travel < 0.0)
|
||||
{
|
||||
error = "recovery_reset_min_travel must be >= 0 [m] (0 = a new path always refills the route)";
|
||||
return false;
|
||||
}
|
||||
if (oscillation_distance > 0.0 && recovery_reset_min_travel > oscillation_distance)
|
||||
{
|
||||
error = "recovery_reset_min_travel must be <= oscillation_distance [m]: past that distance every "
|
||||
"route cursor is refilled anyway, so a larger value can never take effect";
|
||||
return false;
|
||||
}
|
||||
if (planner_patience <= 0.0 && max_planning_retries < 0)
|
||||
{
|
||||
// Từ khi lập plan chạy trên thread riêng, hai tham số này là thứ DUY NHẤT phát hiện được planner
|
||||
@@ -79,9 +90,11 @@ bool StateMachineConfig::validate(std::string& error) const
|
||||
|
||||
// Hai route giám sát tuyến được phép RỖNG (= tắt cơ chế tương ứng), nhưng index đã khai thì phải
|
||||
// trỏ vào behavior có thật: index giả sẽ thành `start()` vào một behavior không tồn tại lúc chạy.
|
||||
if (!indices_valid(recovery_routes.path_blocked) || !indices_valid(recovery_routes.off_path))
|
||||
if (!indices_valid(recovery_routes.path_blocked) || !indices_valid(recovery_routes.off_path) ||
|
||||
!indices_valid(recovery_routes.collision))
|
||||
{
|
||||
error = "recovery route 'path_blocked'/'off_path' references a behavior that was not loaded";
|
||||
error = "recovery route 'path_blocked'/'off_path'/'collision' references a behavior that was "
|
||||
"not loaded";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -103,6 +116,9 @@ std::string StateMachineConfig::describe() const
|
||||
out << " action_patience : " << action_patience << " s"
|
||||
<< (action_patience > 0.0 ? "" : " (off — handler times out on its own)") << '\n';
|
||||
out << " oscillation_distance : " << oscillation_distance << " m\n";
|
||||
out << " recovery_reset_min_travel : " << recovery_reset_min_travel << " m"
|
||||
<< (recovery_reset_min_travel > 0.0 ? "" : " (off — a new path always refills the route)")
|
||||
<< '\n';
|
||||
out << " max_planning_retries : " << max_planning_retries
|
||||
<< (max_planning_retries < 0 ? " (unlimited)" : "") << '\n';
|
||||
out << " recovery_enabled : " << (recovery_enabled ? "true" : "false") << '\n';
|
||||
@@ -115,7 +131,10 @@ std::string StateMachineConfig::describe() const
|
||||
{
|
||||
out << " recovery_routes : planning=" << recovery_routes.planning_failed.size()
|
||||
<< " controlling=" << recovery_routes.controlling_failed.size()
|
||||
<< " oscillation=" << recovery_routes.oscillation.size() << '\n';
|
||||
<< " oscillation=" << recovery_routes.oscillation.size()
|
||||
<< " path_blocked=" << recovery_routes.path_blocked.size()
|
||||
<< " off_path=" << recovery_routes.off_path.size()
|
||||
<< " collision=" << recovery_routes.collision.size() << '\n';
|
||||
}
|
||||
return out.str();
|
||||
}
|
||||
@@ -166,6 +185,8 @@ void StateMachine::reset()
|
||||
state_before_recovery_ = NavigationState::kPlanning;
|
||||
recovery_route_cursors_.fill(0);
|
||||
monitor_trigger_suppressed_.fill(false);
|
||||
has_path_reset_mark_ = false;
|
||||
travel_at_last_path_reset_ = 0.0;
|
||||
planning_retries_ = 0;
|
||||
request_has_goal_ = true;
|
||||
action_count_ = 0;
|
||||
@@ -214,6 +235,8 @@ std::size_t cursorSlot(RecoveryTrigger trigger)
|
||||
return 3;
|
||||
case RecoveryTrigger::kOffPath:
|
||||
return 4;
|
||||
case RecoveryTrigger::kCollision:
|
||||
return 5;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -231,7 +254,8 @@ const std::size_t& StateMachine::recoveryRouteCursor(RecoveryTrigger trigger) co
|
||||
|
||||
bool StateMachine::isMonitorTrigger(RecoveryTrigger trigger)
|
||||
{
|
||||
return trigger == RecoveryTrigger::kPathBlocked || trigger == RecoveryTrigger::kOffPath;
|
||||
return trigger == RecoveryTrigger::kPathBlocked || trigger == RecoveryTrigger::kOffPath ||
|
||||
trigger == RecoveryTrigger::kCollision;
|
||||
}
|
||||
|
||||
void StateMachine::suppressMonitorTrigger(RecoveryTrigger trigger, StateMachineOutput& out)
|
||||
@@ -331,6 +355,8 @@ void StateMachine::acceptPendingRequest(const StateMachineInput& in, StateMachin
|
||||
last_oscillation_reset_ = in.now;
|
||||
sensor_stale_active_ = false;
|
||||
monitor_trigger_suppressed_.fill(false);
|
||||
has_path_reset_mark_ = false; // origin đo quãng đường đặt lại ngay dưới -> mốc cũ vô nghĩa
|
||||
travel_at_last_path_reset_ = 0.0;
|
||||
out.reset_oscillation_origin = true;
|
||||
enter(NavigationState::kPlanning, in.now, "new request accepted", out);
|
||||
}
|
||||
@@ -530,6 +556,11 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
// nghĩa là hình thế đã khác, thử vòng lại là đáng. Đứng im vì bị chắn thì không bao giờ tới
|
||||
// đây — đúng ý đồ: không đốt SBPL vào một tình huống chưa đổi.
|
||||
monitor_trigger_suppressed_.fill(false);
|
||||
|
||||
// Origin vừa đặt lại nên travelled_since_oscillation_reset sẽ về 0 ở cycle sau; mốc của
|
||||
// recovery_reset_min_travel phải đi theo, nếu không hiệu số âm sẽ khoá nạp lại vĩnh viễn.
|
||||
has_path_reset_mark_ = false;
|
||||
travel_at_last_path_reset_ = 0.0;
|
||||
}
|
||||
|
||||
if (controller == ControllerFeedback::kGoalReached)
|
||||
@@ -555,22 +586,30 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
// vòng đi đâu nữa — và TRƯỚC nhánh sinh lệnh: đã biết tuyến tắc thì không chạy thêm một cycle
|
||||
// nào về phía vật cản.
|
||||
//
|
||||
// `path_blocked` thắng `off_path` khi cả hai cùng đúng: tuyến bị chắn là NGUYÊN NHÂN, còn
|
||||
// lệch tuyến thường là HỆ QUẢ của việc local planner tự né chính vật cản đó.
|
||||
// Thứ tự ưu tiên khi nhiều cờ cùng đúng: `collision` (2026-09-04, Phép đo 3 — thân sắp chạm
|
||||
// trong 0.2 m) > `path_blocked` (tuyến tắc, 3 m trước) > `off_path`. Gần nhất xử lý trước;
|
||||
// tuyến bị chắn là NGUYÊN NHÂN, còn lệch tuyến thường là HỆ QUẢ của việc local planner tự
|
||||
// né chính vật cản đó. Mỗi trigger có route, cursor và khoá riêng — Phép đo 3 cạn route
|
||||
// không làm Phép đo 1 mất lượt.
|
||||
//
|
||||
// Trigger đang bị KHOÁ (lượt trước đã hết đường vòng, robot chưa đi đủ oscillation_distance)
|
||||
// thì bỏ qua hẳn nhánh này: bám tiếp plan cũ, để controller_patience làm việc của nó.
|
||||
if ((in.path_blocked && !monitor_trigger_suppressed_[cursorSlot(RecoveryTrigger::kPathBlocked)]) ||
|
||||
(in.off_path && !monitor_trigger_suppressed_[cursorSlot(RecoveryTrigger::kOffPath)]))
|
||||
const bool collision_open =
|
||||
in.collision && !monitor_trigger_suppressed_[cursorSlot(RecoveryTrigger::kCollision)];
|
||||
const bool blocked_open =
|
||||
in.path_blocked && !monitor_trigger_suppressed_[cursorSlot(RecoveryTrigger::kPathBlocked)];
|
||||
const bool off_path_open =
|
||||
in.off_path && !monitor_trigger_suppressed_[cursorSlot(RecoveryTrigger::kOffPath)];
|
||||
if (collision_open || blocked_open || off_path_open)
|
||||
{
|
||||
const bool use_blocked =
|
||||
in.path_blocked && !monitor_trigger_suppressed_[cursorSlot(RecoveryTrigger::kPathBlocked)];
|
||||
const RecoveryTrigger trigger =
|
||||
use_blocked ? RecoveryTrigger::kPathBlocked : RecoveryTrigger::kOffPath;
|
||||
const RecoveryTrigger trigger = collision_open ? RecoveryTrigger::kCollision
|
||||
: blocked_open ? RecoveryTrigger::kPathBlocked
|
||||
: RecoveryTrigger::kOffPath;
|
||||
out.stop_planner = true;
|
||||
escalateToRecovery(trigger, in.now,
|
||||
use_blocked ? "the path ahead is blocked"
|
||||
: "the robot drifted off the plan",
|
||||
collision_open ? "the body is about to hit an obstacle"
|
||||
: blocked_open ? "the path ahead is blocked"
|
||||
: "the robot drifted off the plan",
|
||||
out);
|
||||
// Hết đường vòng: escalateToRecovery đã khoá trigger thay vì ABORT — không đổi state, chạy
|
||||
// tiếp xuống nhánh sinh lệnh của cycle này để robot không mất một nhịp điều khiển.
|
||||
@@ -585,6 +624,15 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
{
|
||||
last_valid_control_ = in.now;
|
||||
|
||||
// Phép đo 4 (PathMonitor): controller vẫn ra lệnh hợp lệ nhưng robot không tiến.
|
||||
if (in.no_progress)
|
||||
{
|
||||
out.stop_planner = true;
|
||||
escalateToRecovery(RecoveryTrigger::kOscillation, in.now, "no progress along the plan",
|
||||
out);
|
||||
break;
|
||||
}
|
||||
|
||||
if (config_.oscillation_timeout > 0.0 &&
|
||||
(in.now - last_oscillation_reset_).toSec() > config_.oscillation_timeout)
|
||||
{
|
||||
@@ -683,10 +731,37 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
// robot bám detour 1.1 s (~0.45 m) rồi gặp vật cản thứ hai và ABORT vì thiếu 5 cm.
|
||||
// Chỉ reset cho ca "thành công + path mới": `wait` thành công trivially sau N giây, reset
|
||||
// theo nó là route không bao giờ cạn — robot đứng chờ vô hạn thay vì ABORT báo fail.
|
||||
//
|
||||
// ĐIỀU KIỆN THÊM (2026-09-04): "path mới" chỉ nạp lại ngân sách khi robot ĐÃ DỊCH CHUYỂN ít
|
||||
// nhất recovery_reset_min_travel kể từ lần nạp lại trước. Sim 4/9: collision guard chặn tại
|
||||
// chỗ -> detour lập lại đúng đường cũ (cost y hệt, 0 ms) -> cursor về 0 -> chặn lại sau 2
|
||||
// cycle -> 249 vòng trong 52 s, robot bất động, controller_patience không bao giờ tích đủ vì
|
||||
// path_blocked chen trước mỗi 0.2 s. Một path mới mà robot không đi được một bước nào trên
|
||||
// đó không giải quyết sự cố gì, nên nó ăn vào ngân sách như một lần thất bại: route cạn ->
|
||||
// mute (05e) -> guard vẫn báo no_valid_command -> controller_patience -> controlling_failed
|
||||
// -> cạn -> ABORT có lý do. Có dịch chuyển thật (0.45 m của ca 08-05) thì vẫn nạp lại như cũ.
|
||||
const bool recovered_with_new_path = in.recovery == RecoveryFeedback::kSucceeded &&
|
||||
in.planner == PlannerFeedback::kPlanReady;
|
||||
std::size_t& cursor = recoveryRouteCursor(active_recovery_trigger_);
|
||||
bool refill = false;
|
||||
if (recovered_with_new_path)
|
||||
{
|
||||
const double travelled = in.travelled_since_oscillation_reset - travel_at_last_path_reset_;
|
||||
refill = !has_path_reset_mark_ || config_.recovery_reset_min_travel <= 0.0 ||
|
||||
travelled >= config_.recovery_reset_min_travel;
|
||||
if (refill)
|
||||
{
|
||||
has_path_reset_mark_ = true;
|
||||
travel_at_last_path_reset_ = in.travelled_since_oscillation_reset;
|
||||
}
|
||||
else
|
||||
{
|
||||
out.recovery_reset_denied = true;
|
||||
out.recovery_reset_travel = std::max(0.0, travelled);
|
||||
out.recovery_trigger = active_recovery_trigger_; // để bên gọi log đúng route bị trừ
|
||||
}
|
||||
}
|
||||
if (refill)
|
||||
{
|
||||
cursor = 0;
|
||||
}
|
||||
@@ -731,6 +806,11 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
sensor_stale_active_ = false;
|
||||
last_oscillation_reset_ = in.now;
|
||||
out.reset_oscillation_origin = true;
|
||||
// Origin đo quãng đường đặt lại ngay tại đây, nên từ cycle sau travelled đếm từ 0 kể từ
|
||||
// đúng lúc path mới được áp — chính là quãng mà recovery_reset_min_travel muốn đo. Mốc phải
|
||||
// về 0 theo (GIỮ has_path_reset_mark_): sim 4/9 mission 3 robot đi 0.18 m sau path mới mà
|
||||
// vẫn bị tính "0.00 m" vì mốc còn giữ giá trị của origin cũ.
|
||||
travel_at_last_path_reset_ = 0.0;
|
||||
out.run_controller = true;
|
||||
enter(NavigationState::kControlling, in.now,
|
||||
has_detour_path ? "recovery produced a detour path"
|
||||
@@ -853,6 +933,7 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
last_oscillation_reset_ = in.now;
|
||||
sensor_stale_active_ = false;
|
||||
out.reset_oscillation_origin = true;
|
||||
travel_at_last_path_reset_ = 0.0; // origin đặt lại -> mốc recovery_reset_min_travel về 0
|
||||
|
||||
if (state_before_pause_ == NavigationState::kControlling)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user