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

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

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

1438 lines
52 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Kiểm lõi EnvMonitor bằng bảng: chuỗi EnvSample giả ở 20 Hz (tick nặng mỗi 4 cycle = 5 Hz), đo
* verdict. Không costmap, không TF — đúng như lõi được thiết kế để kiểm.
*
* Quy ước harness: robot đi dọc x với v_cmd; `cost_rate` là đạo hàm của cost_to_go theo thời gian
* (−v = tiến đúng, eff 1.0; 0 = lệnh mà không tiến, eff 0).
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <limits>
#include <string>
#include <move_base2/core/env_monitor.h>
namespace
{
using move_base2::EnvMonitor;
using move_base2::EnvMonitorConfig;
using move_base2::EnvRecoveryOutcome;
using move_base2::EnvSample;
using move_base2::EnvVerdict;
constexpr double kInf = std::numeric_limits<double>::infinity();
constexpr double kNaN = std::numeric_limits<double>::quiet_NaN();
class Harness
{
public:
explicit Harness(EnvMonitorConfig cfg = EnvMonitorConfig())
{
std::string error;
ok = monitor.configure(cfg, error);
if (!ok)
{
ADD_FAILURE() << "configure failed: " << error;
}
monitor.onGoalAccepted(now());
monitor.onPlanApplied(now());
}
robot::Time now() const
{
return robot::Time(t);
}
EnvVerdict step(double v_cmd, double cost_rate, bool move = true)
{
t += dt;
++cycle;
if (orbit_radius > 0.0)
{
orbit_angle += v_cmd * motion_scale * dt / orbit_radius; // đi vòng tròn với tốc độ lệnh, bánh vẫn lăn
x = orbit_x0 + orbit_radius * std::cos(orbit_angle);
y = orbit_radius * std::sin(orbit_angle);
}
else if (wander_span > 0.0)
{
x = wander_x0 + wander_span * std::sin(2.0 * M_PI * (t - 100.0) / 4.0); // đi vòng trong hộp, bánh vẫn lăn
}
else if (move)
{
x += v_cmd * motion_scale * dt;
}
cost = cost_tracks_x ? cost_at_origin - x : cost + cost_rate * dt;
EnvSample s;
s.now = now();
s.dt = dt;
s.costmap_current = costmap_current;
s.has_plan = has_plan;
s.robot_x = pose_unavailable ? kNaN : x;
s.robot_y = pose_unavailable ? kNaN : y;
s.v_cmd = v_cmd;
s.v_meas = velocity_unavailable ? kNaN : (std::isnan(v_meas_override) ? (move ? v_cmd : 0.0) : v_meas_override);
s.v_controller = v_controller_override;
s.d_goal = d_goal;
s.geometry_valid = (cycle % 4 == 0);
s.cost_conclusive = conclusive;
s.reachable = reachable;
s.cost_to_go = reachable ? cost : kInf;
s.edt_body_min = edt_body;
s.contact_conclusive = contact_conclusive;
s.traj_contact_dist = contact_dist;
last = monitor.update(s);
if (last.event[0] != '\0')
{
events += std::string(last.event) + "\n";
}
return last;
}
void run(double seconds, double v_cmd, double cost_rate, bool move = true)
{
const int n = static_cast<int>(std::lround(seconds / dt));
for (int i = 0; i < n; ++i)
{
step(v_cmd, cost_rate, move);
}
}
/// Chạy tới khi verdict thoả @p pred hoặc hết @p max_seconds; trả thời gian đã chạy (âm = không thoả).
template <class Pred>
double runUntil(double max_seconds, double v_cmd, double cost_rate, Pred pred, bool move = true)
{
const int n = static_cast<int>(std::lround(max_seconds / dt));
for (int i = 0; i < n; ++i)
{
step(v_cmd, cost_rate, move);
if (pred(last))
{
return (i + 1) * dt;
}
}
return -1.0;
}
bool ok = false;
EnvMonitor monitor;
double t = 100.0;
const double dt = 0.05;
int cycle = 0;
double x = 0.0;
double y = 0.0;
double cost = 12.0;
double edt_body = 1.0;
bool reachable = true;
bool conclusive = true;
bool has_plan = true;
bool costmap_current = true;
bool pose_unavailable = false;
bool velocity_unavailable = false;
double d_goal = 12.0;
double contact_dist = kInf;
bool contact_conclusive = true;
double v_meas_override = kNaN;
double v_controller_override = kNaN;
double wander_span = 0.0; ///< [m] > 0: x dao động quanh wander_x0 với biên này (quần tại chỗ, không trượt bánh).
double wander_x0 = 0.0;
double orbit_radius = 0.0; ///< [m] > 0: đi vòng tròn bán kính này quanh (orbit_x0, 0) với tốc độ v_cmd.
double orbit_x0 = 0.0;
double orbit_angle = 0.0;
/// [-] Robot đi v_cmd·scale theo đồng hồ của monitor. Sim chạy chậm hơn thời gian thực (RTF < 1): đồng hồ monitor là
/// đồng hồ tường, robot đi theo giờ sim -> scale = RTF.
double motion_scale = 1.0;
/// true: cost_to_go = cost_at_origin - x (lối ra ở phía +x; cost là hàm của vị trí nên đi vòng thì cost dao động
/// theo). Bỏ qua cost_rate.
bool cost_tracks_x = false;
double cost_at_origin = 12.0; ///< [m]
EnvVerdict last;
std::string events;
};
EnvMonitorConfig recoverAll()
{
EnvMonitorConfig cfg;
cfg.stall.action = "recover";
cfg.unreachable.action = "recover";
cfg.contact.action = "recover";
cfg.stall.disp_min = 0.0; // các test geodesic/ngân sách cũ chạy không cổng dịch chuyển; cổng có test riêng
return cfg;
}
/// Cổng dịch chuyển bật với cửa sổ ngắn để test chạy nhanh.
EnvMonitorConfig recoverWithDispGate()
{
EnvMonitorConfig cfg = recoverAll();
cfg.stall.disp_min = 0.6; // [m]
cfg.stall.disp_window_s = 6.0; // [s]
cfg.stall.disp_min_commanded = 1.0; // [m]
cfg.stall.idle_window_s = 10.0; // [s]
cfg.stall.confine_window_s = 8.0; // [s]
cfg.stall.confine_radius = 1.2; // [m]
cfg.stall.confine_cmd_factor = 1.5;
return cfg;
}
// ------------------------------------------------------------------------------------------ config
TEST(EnvMonitorConfig, DefaultsValidateAndDescribe)
{
EnvMonitorConfig cfg;
std::string error;
EXPECT_TRUE(cfg.validate(error)) << error;
EXPECT_NE(std::string::npos, cfg.describe().find("stall"));
}
TEST(EnvMonitorConfig, RejectsHysteresisInversionActionTypoAndZeroMinCommanded)
{
std::string error;
EnvMonitorConfig cfg;
cfg.stall.eff_off = cfg.stall.eff_on;
EXPECT_FALSE(cfg.validate(error));
cfg = EnvMonitorConfig();
cfg.unreachable.action = "recovery";
EXPECT_FALSE(cfg.validate(error));
cfg = EnvMonitorConfig();
cfg.stall.min_commanded = 0.0;
EXPECT_FALSE(cfg.validate(error));
cfg = EnvMonitorConfig();
cfg.contact.brake_decel = 0.0;
EXPECT_FALSE(cfg.validate(error));
cfg = EnvMonitorConfig();
cfg.contact.min_check_speed = -0.1;
EXPECT_FALSE(cfg.validate(error));
cfg = EnvMonitorConfig();
cfg.replan.escalate_factor = 0.5;
EXPECT_FALSE(cfg.validate(error));
}
TEST(EnvMonitorConfig, PoseJumpRejectMustBeNonNegativeAndIsDescribed)
{
std::string error;
EnvMonitorConfig cfg;
cfg.pose_jump_reject = 0.0; // 0 = tắt, hợp lệ
EXPECT_TRUE(cfg.validate(error)) << error;
cfg.pose_jump_reject = -0.1;
EXPECT_FALSE(cfg.validate(error));
cfg.pose_jump_reject = kNaN;
EXPECT_FALSE(cfg.validate(error));
cfg = EnvMonitorConfig();
EXPECT_NE(std::string::npos, cfg.describe().find("pose_jump>0.5"));
EXPECT_NE(std::string::npos, cfg.describe().find("progress_veto=on"));
}
TEST(EnvMonitorConfig, InvalidConfigLeavesTheMonitorUnconfiguredAndSilent)
{
EnvMonitorConfig cfg;
cfg.stall.eff_off = 0.1;
EnvMonitor m;
std::string error;
EXPECT_FALSE(m.configure(cfg, error));
EXPECT_FALSE(m.configured());
EnvSample s;
s.now = robot::Time(1.0);
const EnvVerdict v = m.update(s);
EXPECT_FALSE(v.no_progress);
EXPECT_FALSE(v.brake);
}
// ------------------------------------------------------------------------------------------- stall
TEST(EnvMonitorStall, SteadyProgressNeverStalls)
{
Harness h(recoverAll());
h.run(20.0, 0.5, -0.5); // eff = 1.0 suốt
EXPECT_FALSE(h.last.stall_detected);
EXPECT_FALSE(h.last.no_progress);
EXPECT_NEAR(1.0, h.last.eff_geo, 0.05);
}
TEST(EnvMonitorStall, CommandedWithoutProgressFiresAfterWindowAndConfirm)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5); // 1.5 m lệnh: qua cổng đầu chặng, cửa sổ có dữ liệu
// Từ đây lệnh 0.5 m/s nhưng cost đứng: cửa sổ 1.5 m lệnh (= 3 s) + confirm 1 s.
const double took = h.runUntil(10.0, 0.5, 0.0, [](const EnvVerdict& v) { return v.stall_detected; });
EXPECT_GT(took, 3.0);
EXPECT_LT(took, 5.0);
EXPECT_TRUE(h.last.no_progress);
EXPECT_LT(h.last.eff_geo, 0.2);
EXPECT_NE(std::string::npos, h.events.find("stall: cost_to_go not decreasing"));
}
TEST(EnvMonitorStall, LogActionReportsButDoesNotRaiseTheFlag)
{
EnvMonitorConfig cfg = recoverAll();
cfg.stall.action = "log";
Harness h(cfg);
h.run(3.0, 0.5, -0.5);
h.run(6.0, 0.5, 0.0);
EXPECT_TRUE(h.last.stall_detected);
EXPECT_FALSE(h.last.no_progress);
EXPECT_FALSE(h.last.stall_muted);
}
TEST(EnvMonitorStall, HoldingStillIsWaitingNotStalling)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.run(10.0, 0.0, 0.0); // planner hold: lệnh 0
EXPECT_FALSE(h.last.stall_detected);
}
TEST(EnvMonitorStall, HysteresisHoldsInTheDeadBandAndClearsAboveEffOff)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.run(6.0, 0.5, 0.0);
ASSERT_TRUE(h.last.stall_detected);
// eff 0.35: trong vùng chết 0.2–0.5 -> giữ kẹt.
h.run(4.0, 0.5, -0.175);
EXPECT_TRUE(h.last.stall_detected);
// eff 0.8 -> xoá.
h.run(4.0, 0.5, -0.4);
EXPECT_FALSE(h.last.stall_detected);
EXPECT_NE(std::string::npos, h.events.find("stall cleared"));
}
TEST(EnvMonitorStall, GraceAfterANewPlanResetsTheWindow)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.run(6.0, 0.5, 0.0);
ASSERT_TRUE(h.last.stall_detected);
h.monitor.onPlanApplied(h.now());
h.run(0.8, 0.5, 0.0);
EXPECT_FALSE(h.last.stall_detected); // trong grace
EXPECT_TRUE(std::isnan(h.last.eff_geo)); // cửa sổ đã xoá, chưa đủ mẫu
// Vẫn kẹt trên plan mới: nổ lại sau cửa sổ + confirm.
const double took = h.runUntil(10.0, 0.5, 0.0, [](const EnvVerdict& v) { return v.stall_detected; });
EXPECT_GT(took, 2.5);
EXPECT_LT(took, 5.0);
}
TEST(EnvMonitorStall, MinCommandedSinceGoalMutesEarlyFires)
{
EnvMonitorConfig cfg = recoverAll();
cfg.stall.window_m = 0.3; // cửa sổ ngắn để kẹt đo được trước khi qua cổng 1.0 m
cfg.stall.window_s = 1.0;
Harness h(cfg);
h.run(2.6, 0.3, 0.0); // 0.78 m lệnh (grace 1 s + cửa sổ 1 s + confirm 1 s), chưa qua cổng 1.0 m
EXPECT_TRUE(h.last.stall_detected);
EXPECT_TRUE(h.last.stall_muted);
EXPECT_FALSE(h.last.no_progress);
h.run(2.0, 0.3, 0.0); // tổng 1.2 m lệnh
EXPECT_TRUE(h.last.stall_detected);
EXPECT_FALSE(h.last.stall_muted);
EXPECT_TRUE(h.last.no_progress);
}
TEST(EnvMonitorStall, ReplanBudgetMutesUntilTheRobotTravelsAndEscalatesWhenRepeated)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.run(6.0, 0.5, 0.0);
ASSERT_TRUE(h.last.no_progress);
// Detour thành công -> im tới khi đi 1.5 m.
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath);
h.monitor.onPlanApplied(h.now());
EXPECT_NEAR(1.5, h.monitor.replanBudget(), 1e-9);
h.run(1.5, 0.5, 0.0); // grace 1 s + kẹt tiếp; đi 0.75 m
h.run(4.0, 0.5, 0.0); // đi thêm 2 m (tổng 2.75 m sau detour) -> hết mute
EXPECT_TRUE(h.last.stall_detected);
EXPECT_FALSE(h.last.stall_muted);
// Detour thứ hai chỉ 2.75 m sau lần trước (< 10 m): ngân sách nhân đôi.
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath);
h.monitor.onPlanApplied(h.now());
EXPECT_NEAR(3.0, h.monitor.replanBudget(), 1e-9);
h.run(1.5, 0.5, 0.0);
h.run(3.0, 0.5, 0.0); // đi 2.25 m < 3.0 -> vẫn mute
EXPECT_TRUE(h.last.stall_detected);
EXPECT_TRUE(h.last.stall_muted);
EXPECT_GT(h.last.replan_budget_left, 0.0);
}
TEST(EnvMonitorStall, ReplanBudgetEscalationIsCapped)
{
EnvMonitorConfig cfg = recoverAll();
cfg.replan.escalate_max = 6.0;
Harness h(cfg);
for (int i = 0; i < 4; ++i)
{
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath);
h.monitor.onPlanApplied(h.now());
h.run(0.5, 0.5, 0.0); // 0.25 m giữa hai detour: trong escalate_window
}
EXPECT_NEAR(6.0, h.monitor.replanBudget(), 1e-9); // 1.5 -> 3 -> 6 -> 6, không lên 12
EnvMonitorConfig uncapped = recoverAll();
uncapped.replan.escalate_max = 0.0;
Harness h2(uncapped);
for (int i = 0; i < 4; ++i)
{
h2.monitor.onRecoveryFinished(h2.now(), EnvRecoveryOutcome::kSucceededWithPath);
h2.monitor.onPlanApplied(h2.now());
h2.run(0.5, 0.5, 0.0);
}
EXPECT_NEAR(12.0, h2.monitor.replanBudget(), 1e-9);
std::string error;
EnvMonitorConfig bad = recoverAll();
bad.replan.escalate_max = 1.0; // < min_travel 1.5
EXPECT_FALSE(bad.validate(error));
}
TEST(EnvMonitorStall, FailedRecoveryBacksOff)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.run(6.0, 0.5, 0.0);
ASSERT_TRUE(h.last.no_progress);
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kFailed);
h.run(2.0, 0.5, 0.0); // chưa đủ cửa sổ lại (chuỗi xoá) -> có thể chưa detected; chạy tiếp
h.run(3.0, 0.5, 0.0);
// Trong 3 s backoff sau lượt hỏng: kể cả detected cũng bị mute.
EXPECT_FALSE(h.events.find("stall: cost_to_go") == std::string::npos);
Harness h2(recoverAll());
h2.run(3.0, 0.5, -0.5);
h2.run(6.0, 0.5, 0.0);
h2.monitor.onRecoveryFinished(h2.now(), EnvRecoveryOutcome::kFailed);
bool muted_seen = false;
for (int i = 0; i < 58; ++i) // 2.9 s: nằm trọn trong backoff 3 s
{
h2.step(0.5, 0.0);
if (h2.last.stall_detected)
{
EXPECT_TRUE(h2.last.stall_muted);
muted_seen = true;
}
}
EXPECT_TRUE(muted_seen);
h2.run(2.0, 0.5, 0.0);
EXPECT_TRUE(h2.last.stall_detected);
EXPECT_FALSE(h2.last.stall_muted);
}
TEST(EnvMonitorStall, WheelSlipIsAStallEvenWhenCostCannotBeMeasured)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.conclusive = false; // hình học không kết luận được
h.v_meas_override = 0.0; // bánh đứng
const double took = h.runUntil(5.0, 0.4, 0.0, [](const EnvVerdict& v) { return v.stall_detected; }, false);
EXPECT_GT(took, 1.9); // slip_time 1 s + confirm 1 s
EXPECT_LT(took, 3.0);
EXPECT_NE(std::string::npos, h.events.find("wheels slip"));
}
TEST(EnvMonitorStall, NearTheGoalIsNotMeasured)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.d_goal = 0.3;
h.run(8.0, 0.3, 0.0);
EXPECT_FALSE(h.last.stall_detected);
}
TEST(EnvMonitorStall, TransientUnreachableSamplesAreSkippedNotCounted)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
// Một tick nặng mất liên thông giữa chuỗi tiến đều: không ghi vào cửa sổ, eff không nhảy.
h.reachable = false;
h.run(0.2, 0.5, -0.5);
h.reachable = true;
h.run(2.0, 0.5, -0.5);
EXPECT_FALSE(h.last.stall_detected);
EXPECT_FALSE(h.last.unreachable_detected);
EXPECT_GT(h.last.eff_geo, 0.5);
}
TEST(EnvMonitorStall, DisplacementGateMutesAGeodesicStallWhileTheRobotMovesOn)
{
// Run 6/9 00:05 (95 s): robot chạy 0.5 m/s lệch plan để né, geodesic bảo kẹt -> cờ oan. Cổng dịch chuyển: robot đã
// đi 0.5 m/s x 6 s = 3 m -> không phải bế tắc.
Harness h(recoverWithDispGate());
h.run(9.0, 0.5, 0.0); // cost đứng nhưng x tiến đều
EXPECT_TRUE(h.last.stall_detected);
EXPECT_TRUE(h.last.stall_muted);
EXPECT_FALSE(h.last.no_progress);
EXPECT_FALSE(h.last.wandering);
EXPECT_GT(h.last.net_disp, 2.0);
}
TEST(EnvMonitorStall, WanderingInABoxPassesTheGateAfterTheWindow)
{
// Run 6/9 00:05 (109–206 s): hộp 0.9 x 0.5 m, lệnh 6 m, 97 s mới nổ vì ngân sách đo theo quãng đường.
Harness h(recoverWithDispGate());
h.wander_span = 0.25; // hộp 0.5 m
h.run(3.0, 0.4, 0.0);
EXPECT_FALSE(h.last.no_progress) << "cửa sổ dịch chuyển chưa đủ 6 s";
const double took = h.runUntil(10.0, 0.4, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0);
EXPECT_LT(took + 3.0, 7.5); // ~window 6 s (+ grace 1 s đầu)
EXPECT_TRUE(h.last.wandering);
EXPECT_LT(h.last.net_disp, 0.6);
}
TEST(EnvMonitorStall, NewPlanRestartsTheDisplacementWindowAndReplacesTheBudget)
{
Harness h(recoverWithDispGate());
h.wander_span = 0.25;
ASSERT_GT(h.runUntil(12.0, 0.4, 0.0, [](const EnvVerdict& v) { return v.no_progress; }), 0.0);
// Detour thành công -> plan mới: cửa sổ vị trí bắt đầu lại, ngân sách replan KHÔNG câm (cổng thay nó).
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath);
h.monitor.onPlanApplied(h.now());
h.run(3.0, 0.4, 0.0);
EXPECT_FALSE(h.last.no_progress) << "vừa có plan mới, chưa đủ cửa sổ dịch chuyển";
const double took = h.runUntil(10.0, 0.4, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0);
EXPECT_LT(took + 3.0, 7.5); // nổ lại sau ~window, không phải sau 1.5 m quãng đường x leo thang
}
TEST(EnvMonitorStall, IdleGateFiresWhenTheRobotCreepsInPlaceWithoutEnoughCommand)
{
// Run 6/9 10:59: PRIEST bò 0.03–0.15 m/s tại chỗ 30 s, lệnh 12 s chỉ 0.1–0.4 m -> cổng dịch chuyển câm, geodesic không
// đo được (cmd < min_commanded). Cổng đứng yên: 10 s (test) không dịch quá 0.6 m -> nổ, không cần eff.
Harness h(recoverWithDispGate());
h.wander_span = 0.05; // gần như đứng yên, bánh lăn chút
h.run(1.5, 0.05, 0.0); // qua grace + cổng 1 m đầu chặng chưa đạt -> cần cmd_since_goal >= 1.0: cấp bằng run dài hơn
// cmd_since_goal 1.0 m ở 0.05 m/s là 20 s — dùng cấu hình cổng đầu chặng nhỏ cho test.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h2(cfg);
h2.wander_span = 0.05;
h2.run(8.0, 0.05, 0.0);
EXPECT_FALSE(h2.last.no_progress) << "chưa đủ idle_window_s";
const double took = h2.runUntil(6.0, 0.05, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0);
EXPECT_LT(took + 8.0, 11.5);
EXPECT_TRUE(h2.last.idle);
EXPECT_NE(std::string::npos, h2.events.find("no net displacement")) << h2.events;
}
TEST(EnvMonitorStall, ConfinementGateCatchesOrbitingInALocalMinimum)
{
// Loanh quanh: vòng tròn bán kính 0.6 m ở 0.3 m/s (chu vi 3.8 m, 8 s đi 2.4 m > 1.5 x 1.2 m). Đầu–cuối cửa sổ 6 s có
// lúc > 0.6 m nên cổng dịch chuyển đóng; cổng giam vùng mở sau 8 s dù geodesic không kết luận (cost đứng).
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.6;
h.run(4.0, 0.3, 0.0);
EXPECT_FALSE(h.last.no_progress);
const double took = h.runUntil(10.0, 0.3, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0);
EXPECT_LT(took + 4.0, 9.5);
EXPECT_TRUE(h.last.confined);
// Cửa sổ 8 s chỉ phủ 2.4 m cung của vòng 3.8 m -> tâm mẫu lệch khỏi tâm vòng, bán kính đo 0.6–0.9 m, vẫn < 1.2.
EXPECT_GT(h.last.region_radius, 0.4);
EXPECT_LT(h.last.region_radius, 1.2);
// Geodesic (cost đứng) có thể đã giơ stall trước; cổng giam vùng là thứ mở cờ. Dù đường nào, sự kiện phải có "stall".
EXPECT_NE(std::string::npos, h.events.find("stall:")) << h.events;
}
TEST(EnvMonitorStall, ConfinementGateIgnoresASlowStraightCrawl)
{
// Bò thẳng 0.1 m/s sau người: 8 s đi 0.8 m, bán kính vùng 0.4 m, lệnh 0.8 < 1.5 x max(0.8, 0.6) -> không giam.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
cfg.stall.idle_window_s = 0.0; // tách riêng cổng giam vùng
Harness h(cfg);
h.run(20.0, 0.1, 0.0);
EXPECT_FALSE(h.last.confined);
EXPECT_FALSE(h.last.no_progress);
}
// --- Sim SEHC 6/11 20:34:43: "confined" oan khi robot đang chạy thẳng (LANE_ROUTE_PLANNER_PLAN.md mục 8) ---------------
// Sáu test đầu chỉ dùng API có từ trước. HEAD fail hai test tái hiện và ProgressProofExpiresWhenCostCannotBeMeasured
// ngay ở điều kiện đầu vì đã báo kẹt vòng plan; ba test kẹt thật còn lại pass ở cả hai bản.
TEST(EnvMonitorStall, RelocalizationWhileDrivingStraightIsNotAStall)
{
// p2_sim.log dòng 929–1072: robot chạy thẳng 0.3 m/s; sim RTF ~0.63 nên theo đồng hồ monitor robot chỉ đi 0.19 m/s
// (eff ~0.63 suốt mission). 20:34:34.7 AMCL "Setting pose" kéo pose lùi 1.2 m, bộ lọc hội tụ kéo thêm 0.3 m trong 1 s.
// Bản cũ: cú dời được tính là robot đi -> quỹ đạo 20 s gấp lên chính nó, bán kính vùng 1.78 -> 1.20 m -> +1.5 s stall
// geodesic (shadow), +5.6 s cleared, +8.65 s "confined to a small region" + FLAG -> detour_path vô ích.
// Cấu hình = YAML runtime (trùng mặc định struct) với action recover.
EnvMonitorConfig cfg;
cfg.stall.action = "recover";
Harness h(cfg);
h.motion_scale = 0.63;
h.cost_tracks_x = true;
h.run(19.0, 0.3, 0.0); // từ lúc nhận goal tới lúc đặt lại pose
ASSERT_FALSE(h.last.no_progress);
ASSERT_EQ(std::string::npos, h.events.find("stall")) << h.events;
const double travelled = h.monitor.travelledTotal();
h.x -= 1.2; // AMCL "Setting pose": một cycle
for (int i = 0; i < 20; ++i)
{
h.x -= 0.3 / 20.0 + 0.3 * h.motion_scale * h.dt; // hội tụ: pose trôi lùi 0.3 m trong 1 s dù robot vẫn chạy
h.step(0.3, 0.0);
}
bool flagged = false;
for (int i = 0; i < 800; ++i) // 40 s: dài hơn mọi cửa sổ (đứng yên 25 s, giam vùng 20 s)
{
h.step(0.3, 0.0);
flagged = flagged || h.last.no_progress;
}
EXPECT_FALSE(flagged) << h.events;
EXPECT_EQ(std::string::npos, h.events.find("stall")) << h.events;
// Cú dời 1.2 m không phải quãng đi: odometer chỉ tăng theo trôi hội tụ (0.3 m) + robot chạy 40 s x 0.19 m/s.
EXPECT_LT(h.monitor.travelledTotal() - travelled, 0.3 + 40.0 * 0.3 * h.motion_scale + 0.25);
}
TEST(EnvMonitorStall, ProgressAlongALoopInThePlanIsNotConfinement)
{
// Planner chỉ đi tiến vẽ vòng quay đầu bán kính 0.6 m; robot bám đúng vòng và cost_to_go giảm 1:1 với quãng đi
// (eff 1.0). Vị trí 8 s nằm gọn trong vòng 1.2 m, lệnh 2.4 m >= 1.5 x đường kính: hình học y như kẹt, nhưng robot
// đang tiến dọc plan -> không được nổ.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.6;
bool flagged = false;
bool confined_seen = false;
for (int i = 0; i < 252; ++i) // 12.6 s = một vòng 3.8 m ở 0.3 m/s
{
h.step(0.3, -0.3);
flagged = flagged || h.last.no_progress;
confined_seen = confined_seen || h.last.confined;
}
EXPECT_TRUE(confined_seen) << "kịch bản phải mở được cổng hình học, nếu không test này không kiểm gì";
EXPECT_FALSE(flagged) << h.events;
EXPECT_EQ(std::string::npos, h.events.find("stall")) << h.events;
}
TEST(EnvMonitorStall, OrbitingInALocalMinimumStillFiresWhenCostFollowsThePose)
{
// Kẹt thật: quay vòng bán kính 0.6 m trước lối ra ở phía +x. cost_to_go là hàm của vị trí nên dao động theo vòng, eff
// lật dấu -> luôn có mẫu eff thấp, phủ quyết tiến độ không bao giờ mở. Mọi pha xuất phát: nổ trong ~cửa sổ giam
// vùng 8 s như bản cũ, không trễ thêm.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
for (const double phase : { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 })
{
Harness h(cfg);
h.orbit_radius = 0.6;
h.orbit_angle = phase;
h.cost_tracks_x = true;
const double took = h.runUntil(20.0, 0.3, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0) << "phase " << phase << "\n" << h.events;
EXPECT_LT(took, 9.5) << "phase " << phase << "\n" << h.events;
}
}
TEST(EnvMonitorStall, BackAndForthInALocalMinimumStillFires)
{
// Kẹt thật: đi tới lui 1 m dọc hướng lối ra (tới gần vật, lùi, lại tới). cost_to_go theo vị trí.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.wander_span = 0.5;
h.cost_tracks_x = true;
const double took = h.runUntil(20.0, 0.4, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0) << h.events;
EXPECT_LT(took, 9.5) << h.events;
}
TEST(EnvMonitorStall, RealStallAfterARelocalizationIsStillDetected)
{
// Kẹt quay vòng, giữa chừng localization đặt lại pose 1.3 m: cửa sổ đo lại từ cú nhảy, vẫn nổ sau ~cửa sổ.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.6;
h.cost_tracks_x = true;
h.run(5.0, 0.3, 0.0);
ASSERT_FALSE(h.last.no_progress);
h.orbit_x0 -= 1.3; // cả vòng dời theo pose
const double took = h.runUntil(20.0, 0.3, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0) << h.events;
EXPECT_LT(took, 9.5) << h.events;
}
TEST(EnvMonitorStall, ProgressProofExpiresWhenCostCannotBeMeasured)
{
// Bằng chứng tiến độ không được nằm lì: đang tiến dọc vòng của plan thì cost hết đo được (mất liên thông, costmap
// không kết luận) mà robot vẫn quay vòng -> sau window_s không còn bằng chứng -> cổng giam vùng nổ.
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.6;
h.run(9.0, 0.3, -0.3);
ASSERT_FALSE(h.last.no_progress) << h.events;
h.conclusive = false;
const double took = h.runUntil(10.0, 0.3, 0.0, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0) << h.events;
EXPECT_LT(took, cfg.stall.window_s + 1.0) << h.events;
EXPECT_NE(std::string::npos, h.events.find("confined")) << h.events;
}
TEST(EnvMonitorStall, PoseJumpIsReportedNotCountedAndCanBeSwitchedOff)
{
EnvMonitorConfig cfg = recoverWithDispGate();
Harness h(cfg);
h.run(7.0, 0.3, -0.3);
ASSERT_FALSE(std::isnan(h.last.net_disp)); // cửa sổ dịch chuyển 6 s đã đầy
const double before = h.monitor.travelledTotal();
h.x -= 1.2;
h.step(0.3, -0.3); // cycle này robot còn đi tới 0.015 m
EXPECT_TRUE(h.last.pose_jump);
EXPECT_TRUE(h.last.pose_jump_restart);
EXPECT_NEAR(1.185, h.last.pose_jump_dist, 1e-6);
EXPECT_NEAR(before, h.monitor.travelledTotal(), 1e-9);
EXPECT_TRUE(std::isnan(h.last.net_disp)); // cửa sổ vị trí đã bắt đầu lại
h.step(0.3, -0.3);
EXPECT_FALSE(h.last.pose_jump); // sườn, không phải trạng thái
// Bước sửa thường của localization (0.2 m) không phải cú nhảy.
h.x += 0.2;
h.step(0.3, -0.3);
EXPECT_FALSE(h.last.pose_jump);
// 0 = tắt: cú dời tính như quãng đi (bản cũ).
cfg.pose_jump_reject = 0.0;
Harness h2(cfg);
h2.run(7.0, 0.3, -0.3);
const double before2 = h2.monitor.travelledTotal();
h2.x -= 1.2;
h2.step(0.3, -0.3);
EXPECT_FALSE(h2.last.pose_jump);
EXPECT_NEAR(before2 + 1.185, h2.monitor.travelledTotal(), 1e-6);
}
TEST(EnvMonitorStall, ProgressVetoReportsTheProofAndCanBeSwitchedOff)
{
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.6;
h.run(9.0, 0.3, -0.3);
EXPECT_TRUE(h.last.confined);
EXPECT_TRUE(h.last.progress_proven);
EXPECT_FALSE(h.last.no_progress);
// false = bản cũ: cổng giam vùng nổ theo hình học bất kể cost_to_go.
cfg.stall.progress_veto = false;
Harness h2(cfg);
h2.orbit_radius = 0.6;
const double took = h2.runUntil(12.0, 0.3, -0.3, [](const EnvVerdict& v) { return v.no_progress; });
EXPECT_GT(took, 0.0);
EXPECT_NE(std::string::npos, h2.events.find("confined")) << h2.events;
}
TEST(EnvMonitorStall, IdleStillFiresWhenCostRepeatedlyFallsAndJumps)
{
// Bánh báo đúng lệnh nhưng thân đứng; costmap liên tục giảm cost rồi nhảy lên, tạo eff tốt giả.
EnvMonitorConfig cfg;
cfg.stall.action = "recover";
Harness h(cfg);
h.motion_scale = 0.0;
bool saw_proof = false;
bool saw_idle_flag = false;
double flag_time = -1.0;
for (int i = 0; i < 600; ++i)
{
if (i > 0 && i % 80 == 0)
{
h.cost += 1.6;
}
h.step(0.4, -0.4);
saw_proof = saw_proof || h.last.progress_proven;
if (h.last.no_progress && h.last.idle)
{
saw_idle_flag = true;
flag_time = (i + 1) * h.dt;
break;
}
}
EXPECT_TRUE(saw_proof); // cần kiểm cả trường hợp tiến độ giả từng được chứng minh
EXPECT_TRUE(saw_idle_flag) << h.events;
EXPECT_GT(flag_time, 0.0);
EXPECT_LT(flag_time, cfg.stall.idle_window_s + 3.0);
}
TEST(EnvMonitorStall, CostJumpInvalidatesProgressProof)
{
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.6;
h.run(9.0, 0.3, -0.3);
ASSERT_TRUE(h.last.progress_proven);
h.cost += 2.0;
bool jumped = false;
for (int i = 0; i < 4 && !jumped; ++i)
{
h.step(0.3, -0.3);
jumped = h.last.cost_jump;
}
ASSERT_TRUE(jumped);
EXPECT_FALSE(h.last.progress_proven);
h.run(cfg.stall.confine_window_s - 0.5, 0.3, -0.3);
EXPECT_FALSE(h.last.progress_proven);
}
TEST(EnvMonitorStall, IdleIsNotVetoedByPositiveProgressProof)
{
// Giới hạn đã chấp nhận: quay hai vòng tại chỗ trong idle_window_s bị báo đứng yên; giả định không có plan hợp lệ như vậy.
EnvMonitorConfig cfg;
cfg.stall.action = "recover";
Harness h(cfg);
h.motion_scale = 0.0;
bool flagged = false;
double flag_time = -1.0;
for (int i = 0; i < 600 && !flagged; ++i)
{
h.step(0.4, -0.4);
flagged = h.last.no_progress && h.last.idle && h.last.progress_proven;
if (flagged)
{
flag_time = (i + 1) * h.dt;
}
}
EXPECT_TRUE(flagged) << h.events;
EXPECT_GT(flag_time, 0.0);
EXPECT_LT(flag_time, cfg.stall.idle_window_s + 3.0);
}
TEST(EnvMonitorStall, RepeatedPoseJumpsDoNotKeepRestartingStallWindows)
{
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.orbit_radius = 0.5;
h.cost_tracks_x = true;
h.run(5.0, 0.3, 0.0);
ASSERT_FALSE(h.last.no_progress);
h.orbit_x0 -= 1.3;
h.step(0.3, 0.0);
ASSERT_TRUE(h.last.pose_jump);
ASSERT_TRUE(h.last.pose_jump_restart);
h.run(5.95, 0.3, 0.0);
h.orbit_x0 += 1.3;
h.step(0.3, 0.0);
EXPECT_TRUE(h.last.pose_jump);
EXPECT_FALSE(h.last.pose_jump_restart);
bool flagged = h.last.no_progress;
double flag_time = flagged ? 6.0 : -1.0;
for (int i = 0; i < 480 && !flagged; ++i)
{
if (i > 0 && i % 120 == 0)
{
h.orbit_x0 = h.orbit_x0 == 0.0 ? -1.3 : 0.0;
}
h.step(0.3, 0.0);
flagged = h.last.no_progress;
if (flagged)
{
flag_time = 6.0 + (i + 1) * h.dt;
}
}
EXPECT_TRUE(flagged) << h.events;
EXPECT_GT(flag_time, 0.0);
EXPECT_LT(flag_time, 25.0); // [s] từ cú nhảy đầu
}
TEST(EnvMonitorStall, KeptPoseJumpIsNotTravelOrAFreeMaskCostJump)
{
EnvMonitorConfig cfg = recoverWithDispGate();
Harness h(cfg);
h.orbit_radius = 0.6;
h.cost_tracks_x = true;
h.run(5.0, 0.3, 0.0);
h.orbit_x0 -= 1.3;
h.step(0.3, 0.0);
ASSERT_TRUE(h.last.pose_jump_restart);
h.run(5.95, 0.3, 0.0);
const double before = h.monitor.travelledTotal();
h.orbit_x0 += 1.3;
h.step(0.3, 0.0);
ASSERT_TRUE(h.last.pose_jump);
EXPECT_FALSE(h.last.pose_jump_restart);
EXPECT_NEAR(before, h.monitor.travelledTotal(), 1e-9);
for (int i = 0; i < 4; ++i)
{
h.step(0.3, 0.0); // gồm mẫu nặng đầu tiên sau cú nhảy
EXPECT_FALSE(h.last.cost_jump);
}
// Một thay đổi cost lớn hơn cả ngưỡng và phần bù pose vẫn phải bị loại.
h.cost_tracks_x = false;
h.cost += 3.0;
bool cost_jump = false;
for (int i = 0; i < 4 && !cost_jump; ++i)
{
h.step(0.3, 0.0);
cost_jump = h.last.cost_jump;
}
EXPECT_TRUE(cost_jump);
}
TEST(EnvMonitorStall, PoseGapUsesElapsedTimeAndCountsRealTravel)
{
Harness h;
h.run(1.0, 0.3, -0.3);
const double before = h.monitor.travelledTotal();
h.pose_unavailable = true;
h.run(1.0, 0.3, -0.3);
h.monitor.onGoalAccepted(h.now()); // giữ mốc pose và vận tốc đã thấy trong khoảng mất pose
h.run(1.0, 0.3, -0.3);
EXPECT_NEAR(before, h.monitor.travelledTotal(), 1e-9);
h.pose_unavailable = false;
h.step(0.3, -0.3);
EXPECT_FALSE(h.last.pose_jump);
EXPECT_NEAR(0.615, h.monitor.travelledTotal() - before, 1e-6);
}
TEST(EnvMonitorStall, PoseJumpCanRestartAgainAfterCooldown)
{
Harness h(recoverWithDispGate());
h.run(1.0, 0.3, -0.3);
h.x -= 1.3;
h.step(0.3, -0.3);
ASSERT_TRUE(h.last.pose_jump_restart);
h.run(10.5, 0.3, -0.3); // cooldown = max(10, 8, 6) s
h.x += 1.3;
h.step(0.3, -0.3);
EXPECT_TRUE(h.last.pose_jump);
EXPECT_TRUE(h.last.pose_jump_restart);
}
TEST(EnvMonitorStall, NewGoalAllowsTheNextPoseJumpToRestartWindows)
{
Harness h;
h.run(1.0, 0.3, -0.3);
h.x += 1.3;
h.step(0.3, -0.3);
ASSERT_TRUE(h.last.pose_jump);
ASSERT_TRUE(h.last.pose_jump_restart);
h.monitor.onGoalAccepted(h.now());
h.x -= 1.3;
h.step(0.3, -0.3);
EXPECT_TRUE(h.last.pose_jump);
EXPECT_TRUE(h.last.pose_jump_restart);
}
TEST(EnvMonitorStall, PoseGapRetainsEarlierPeakVelocityAcrossNewGoal)
{
Harness h;
h.run(1.0, 0.3, -0.3);
const double before = h.monitor.travelledTotal();
h.pose_unavailable = true;
h.motion_scale = 3.0; // thân đi 0.3 m/s, lệnh giả 0.1 m/s
h.v_meas_override = 0.8;
h.run(1.0, 0.1, -0.3);
h.monitor.onGoalAccepted(h.now());
h.v_meas_override = 0.1;
h.run(1.0, 0.1, -0.3);
h.pose_unavailable = false;
h.step(0.1, -0.3);
EXPECT_FALSE(h.last.pose_jump);
EXPECT_NEAR(0.615, h.monitor.travelledTotal() - before, 1e-6);
}
TEST(EnvMonitorStall, NonFiniteVelocitiesAllowNormalSmallPoseStep)
{
Harness h;
h.run(1.0, 0.3, -0.3);
const double before = h.monitor.travelledTotal();
h.x += 0.1;
h.velocity_unavailable = true;
h.step(kNaN, 0.0, false);
EXPECT_FALSE(h.last.pose_jump);
EXPECT_NEAR(before + 0.1, h.monitor.travelledTotal(), 1e-6);
}
TEST(EnvMonitorStall, IdleGateStaysClosedWhileTheRobotMoves)
{
EnvMonitorConfig cfg = recoverWithDispGate();
cfg.stall.min_commanded_since_goal = 0.1;
Harness h(cfg);
h.run(15.0, 0.3, -0.3); // đi đều 4.5 m
EXPECT_FALSE(h.last.idle);
EXPECT_FALSE(h.last.no_progress);
h.run(15.0, 0.3, 0.0); // đi đều nhưng cost đứng: geodesic bảo kẹt, hai cổng đều đóng -> câm
EXPECT_TRUE(h.last.stall_detected);
EXPECT_FALSE(h.last.idle);
EXPECT_FALSE(h.last.no_progress);
}
TEST(EnvMonitorStall, CostJumpRestartsTheWindowInsteadOfStalling)
{
// Run 5/9 23:37: geo_to_exit nhảy +3.2 m trong một tick (mặt nạ free đổi liên thông) khi robot vẫn đi tốt
// -> eff −2.7 -> detour oan. Với jump_reject 1.0: xoá cửa sổ, không stall; đo lại từ đầu.
Harness h(recoverAll());
h.run(4.5, 0.5, -0.5); // grace 1 s + cửa sổ 4 s/1.5 m đầy
for (int i = 0; i < 4 && std::isnan(h.last.eff_geo); ++i)
{
h.step(0.5, -0.5); // eff chỉ có ở tick nặng (mỗi 4 cycle)
}
EXPECT_GT(h.last.eff_geo, 0.8);
h.cost += 3.0; // nhảy
h.step(0.5, -0.5);
bool jumped = false;
for (int i = 0; i < 4 && !jumped; ++i)
{
jumped = h.last.cost_jump;
if (!jumped)
{
h.step(0.5, -0.5);
}
}
EXPECT_TRUE(jumped);
h.run(2.0, 0.5, -0.5); // tiếp tục đi tốt sau cú nhảy
EXPECT_FALSE(h.last.stall_detected);
EXPECT_FALSE(h.last.no_progress);
// Không có bộ lọc: cùng kịch bản là stall.
EnvMonitorConfig cfg = recoverAll();
cfg.stall.jump_reject = 0.0;
Harness h2(cfg);
h2.run(3.0, 0.5, -0.5);
h2.cost += 3.0;
h2.run(2.0, 0.5, -0.5);
EXPECT_TRUE(h2.last.stall_detected);
}
TEST(EnvMonitorStall, RealStallAfterAJumpIsStillDetected)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.cost += 3.0;
h.step(0.5, -0.5);
// Sau cú nhảy robot đòi đi mà cost đứng: cửa sổ mới cần window_s 4 s + confirm 1 s.
const double took = h.runUntil(8.0, 0.5, 0.0, [](const EnvVerdict& v) { return v.stall_detected; });
EXPECT_GT(took, 4.0);
EXPECT_LT(took, 6.0);
}
TEST(EnvMonitorStall, NoPlanOrStaleCostmapClearsTheChain)
{
Harness h(recoverAll());
h.run(3.0, 0.5, -0.5);
h.run(6.0, 0.5, 0.0);
ASSERT_TRUE(h.last.stall_detected);
h.costmap_current = false;
h.step(0.5, 0.0);
EXPECT_FALSE(h.last.stall_detected);
EXPECT_FALSE(h.last.no_progress);
}
// ------------------------------------------------------------------------------------- unreachable
TEST(EnvMonitorUnreachable, FiresAfterStaticTimeOnCleanGround)
{
Harness h(recoverAll());
h.run(2.0, 0.5, -0.5); // baseline: liên thông
h.reachable = false;
const double took = h.runUntil(5.0, 0.3, 0.0, [](const EnvVerdict& v) { return v.path_blocked; });
EXPECT_GT(took, 0.9);
EXPECT_LT(took, 1.5);
EXPECT_TRUE(h.last.unreachable_detected);
h.reachable = true;
h.run(0.4, 0.3, -0.3);
EXPECT_FALSE(h.last.unreachable_detected);
EXPECT_NE(std::string::npos, h.events.find("unreachable cleared"));
}
TEST(EnvMonitorUnreachable, RobotAlreadyInsideTheBandDoesNotFire)
{
Harness h(recoverAll());
h.run(2.0, 0.5, -0.5);
h.reachable = false;
h.edt_body = 0.1; // < clean_start_margin: SBPL không có chỗ bắt đầu
h.run(4.0, 0.3, 0.0);
EXPECT_FALSE(h.last.unreachable_detected);
EXPECT_FALSE(h.last.path_blocked);
}
TEST(EnvMonitorUnreachable, PlanBornUnreachableIsNotArguedWith)
{
// R2: plan vừa nhận đã không liên thông -> SBPL/planner đã lập trên cùng dữ liệu, monitor im.
Harness h(recoverAll());
h.reachable = false;
h.run(1.5, 0.3, 0.0); // qua grace, baseline = không liên thông
h.run(5.0, 0.3, 0.0);
EXPECT_FALSE(h.last.unreachable_detected);
// Liên thông trở lại rồi mất lần nữa: đó là chuyện MỚI -> nổ.
h.reachable = true;
h.run(0.4, 0.3, -0.3);
h.reachable = false;
h.run(2.0, 0.3, 0.0);
EXPECT_TRUE(h.last.unreachable_detected);
}
TEST(EnvMonitorUnreachable, LogActionDoesNotRaiseTheFlag)
{
EnvMonitorConfig cfg = recoverAll();
cfg.unreachable.action = "log";
Harness h(cfg);
h.run(2.0, 0.5, -0.5);
h.reachable = false;
h.run(3.0, 0.3, 0.0);
EXPECT_TRUE(h.last.unreachable_detected);
EXPECT_FALSE(h.last.path_blocked);
}
TEST(EnvMonitorUnreachable, ReplanBudgetAppliesToo)
{
Harness h(recoverAll());
h.run(2.0, 0.5, -0.5);
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath);
h.monitor.onPlanApplied(h.now());
h.run(1.2, 0.3, -0.3); // baseline liên thông sau grace
h.reachable = false;
h.run(3.0, 0.3, 0.0); // đi 0.9 m < 1.5 m ngân sách
EXPECT_TRUE(h.last.unreachable_detected);
EXPECT_TRUE(h.last.unreachable_muted);
EXPECT_FALSE(h.last.path_blocked);
}
// ---------------------------------------------------------------------------------------- contact
TEST(EnvMonitorContact, BrakesWhenContactIsWithinTheStoppingDistanceAndFlagsWhenItPersists)
{
Harness h(recoverAll());
h.contact_dist = 0.5; // v 0.5 -> quãng phanh 0.357; 0.5 - 0.357 = 0.143 <= margin 0.2 -> sắp chạm
h.step(0.5, -0.5);
EXPECT_TRUE(h.last.contact_imminent);
EXPECT_FALSE(h.last.brake); // confirm 2 cycle
h.step(0.5, -0.5);
EXPECT_TRUE(h.last.brake);
EXPECT_FALSE(h.last.collision);
EXPECT_NEAR(std::sqrt(2.0 * 0.35 * 0.2), h.last.v_cap, 1e-6); // dư bao 0.1: room = 0.5 - 0.2 - 0.1
// Đứng lại (v = 0) với vật còn cách 0.5 m: quãng phanh về 0 -> KHÔNG còn "sắp chạm". Nhả phanh sau
// release_confirm_cycles (4) cycle liên tiếp, rồi robot được bò dưới bao tốc độ; không có cờ vì vật chưa kề mũi.
h.run(0.15, 0.0, 0.0, false); // 3 cycle: vẫn giữ
EXPECT_TRUE(h.last.brake);
h.step(0.0, 0.0, false); // cycle 4: nhả
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.collision);
EXPECT_NE(std::string::npos, h.events.find("contact receded"));
EXPECT_NEAR(std::sqrt(2.0 * 0.35 * 0.2), h.last.v_cap, 1e-6); // dư bao 0.1: room = 0.5 - 0.2 - 0.1
// Vật bước tới sát mũi (0.15 m <= margin 0.2) và đứng đó. Robot đang đứng (v = 0, chưa phanh) thì monitor
// không xét (min_check_speed) — controller tự lo; controller muốn tiến 0.3 m/s là phanh lại ngay, và
// sau 1 s kề mũi giơ cờ collision.
h.contact_dist = 0.15;
const double took = h.runUntil(3.0, 0.3, 0.0, [](const EnvVerdict& v) { return v.collision; }, false);
EXPECT_GT(took, 0.9);
EXPECT_LT(took, 1.2);
EXPECT_TRUE(h.last.brake);
EXPECT_NEAR(0.0, h.last.v_cap, 1e-9);
// Vật đi rồi: phanh và cờ cùng giữ thêm release_confirm_cycles (4) cycle sạch liên tiếp rồi mới hạ —
// một mẫu sạch chưa là sạch khi local planner đổi quỹ đạo mỗi cycle.
h.contact_dist = kInf;
h.run(0.15, 0.0, 0.0, false);
EXPECT_TRUE(h.last.brake);
EXPECT_TRUE(h.last.collision);
h.step(0.0, 0.0, false);
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.collision);
EXPECT_NE(std::string::npos, h.events.find("contact cleared"));
}
TEST(EnvMonitorContact, PivotingOrCreepingNextToAnObstacleIsTheControllersBusiness)
{
// Robot vừa nhận goal, đứng cách vật 8 cm, xoay tại chỗ / bò ra (v 0.07 < min_check_speed 0.10): quỹ đạo
// local planner có góc thân quét vào ô kề bên. Guard cũ ghim robot ở đây rồi gọi detour từ chính chỗ
// đó (churn mission 5/6 shadow 5/9). Monitor mới: không phanh, không cờ, không sự kiện.
Harness h(recoverAll());
h.contact_dist = 0.08;
h.run(2.0, 0.07, 0.0, false);
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.collision);
EXPECT_FALSE(h.last.contact_imminent);
EXPECT_TRUE(h.events.empty()) << h.events;
EXPECT_NEAR(0.0, h.last.v_cap, 1e-9); // bao tốc độ vẫn tính: vật trong margin -> muốn tiến là 0
// Bắt đầu tiến thật (0.4 m/s) vào vật: phanh sau confirm 2 cycle, và vì đã phanh nên đứng lại (v = 0)
// vẫn xét tiếp -> 1 s kề mũi -> cờ collision. Cổng tốc độ không che được tiếp xúc thật.
h.step(0.4, 0.0, false);
h.step(0.4, 0.0, false);
EXPECT_TRUE(h.last.brake);
const double took = h.runUntil(3.0, 0.0, 0.0, [](const EnvVerdict& v) { return v.collision; }, false);
EXPECT_GT(took, 0.0);
EXPECT_LT(took, 1.2);
EXPECT_TRUE(h.last.brake);
}
TEST(EnvMonitorContact, CommandCappedToZeroWhileTheControllerStillPushesIsABrakeNotAPivot)
{
// Bao tốc độ đã kẹp lệnh về 0 trong margin (v_cmd = 0, v_đo = 0) nhưng controller vẫn đòi 0.3 m/s:
// KHÔNG phải xoay tại chỗ — phải phanh có sự kiện rồi giơ cờ, không dừng câm.
Harness h(recoverAll());
h.contact_dist = 0.10;
h.v_controller_override = 0.3;
h.step(0.0, 0.0, false);
h.step(0.0, 0.0, false);
EXPECT_TRUE(h.last.brake);
EXPECT_NE(std::string::npos, h.events.find("braking to zero"));
const double took = h.runUntil(3.0, 0.0, 0.0, [](const EnvVerdict& v) { return v.collision; }, false);
EXPECT_GT(took, 0.0);
EXPECT_LT(took, 1.2);
// Controller thôi đòi đi (xoay tại chỗ) NHƯNG monitor đã phanh: vẫn giữ tới khi quỹ đạo sạch đủ lâu.
h.v_controller_override = 0.0;
h.step(0.0, 0.0, false);
EXPECT_TRUE(h.last.brake);
h.contact_dist = kInf;
h.run(0.2, 0.0, 0.0, false);
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.collision);
}
TEST(EnvMonitorContact, TrajectoryJitterAroundTheMarginHoldsTheBrakeAndStillRaisesTheFlag)
{
// Run 5/9 22:35 (mission 1, 70–71 s): PRIEST đổi quỹ đạo mỗi cycle, d nhảy 0.08 <-> 0.25 m quanh margin
// 0.2 -> phanh/nhả 6 lần trong 1.2 s, đồng hồ kề mũi reset liên tục, cờ không bao giờ nổ. Với hysteresis
// 0.1 m: đã kề thì 0.25 vẫn là kề -> phanh giữ, cờ nổ sau 1 s, đúng một sự kiện phanh và một sự kiện cờ.
Harness h(recoverAll());
h.v_controller_override = 0.3;
double fired_at = -1.0;
for (int i = 0; i < 40; ++i)
{
h.contact_dist = (i % 2 == 0) ? 0.08 : 0.25;
h.step(0.0, 0.0, false);
if (h.last.collision && fired_at < 0.0)
{
fired_at = (i + 1) * h.dt;
}
if (i >= 2)
{
EXPECT_TRUE(h.last.brake) << "cycle " << i;
}
}
EXPECT_GT(fired_at, 0.9);
EXPECT_LT(fired_at, 1.3);
auto count = [&](const char* needle) {
int n = 0;
for (std::size_t pos = h.events.find(needle); pos != std::string::npos; pos = h.events.find(needle, pos + 1))
{
++n;
}
return n;
};
EXPECT_EQ(1, count("braking to zero")) << h.events;
EXPECT_EQ(1, count("persists")) << h.events;
EXPECT_EQ(0, count("receded")) << h.events;
EXPECT_EQ(0, count("cleared")) << h.events;
}
TEST(EnvMonitorContact, RidingTheEnvelopeIsNotImminent)
{
// Lệnh đúng bằng bao: quãng phanh = d - margin - dư -> còn dư 0.1 m so với ngưỡng phanh -> không phanh.
Harness h(recoverAll());
h.contact_dist = 0.5;
const double v_cap = std::sqrt(2.0 * 0.35 * 0.2);
h.run(0.5, v_cap, 0.0, false);
EXPECT_FALSE(h.last.contact_imminent);
EXPECT_FALSE(h.last.brake);
EXPECT_NEAR(v_cap, h.last.v_cap, 1e-6);
// Giữa margin và margin + dư: bò min_speed, chưa phanh khi đang bò.
h.contact_dist = 0.25;
h.run(0.5, 0.05, 0.0, false);
EXPECT_NEAR(0.05, h.last.v_cap, 1e-9);
EXPECT_FALSE(h.last.brake);
}
TEST(EnvMonitorContact, StandsDownWhenTheDetourFromThisSpotDidNotMoveTheRobot)
{
// m1/m8 run 5/9 22:58: cờ -> detour -> robot không rời chỗ -> chạm lại -> cờ -> detour ... 3 lần rồi ABORT.
Harness h(recoverAll());
h.contact_dist = 0.10;
h.v_controller_override = 0.3;
const double took = h.runUntil(3.0, 0.0, 0.0, [](const EnvVerdict& v) { return v.collision; }, false);
ASSERT_GT(took, 0.0);
// Detour do cờ collision thành công, robot vẫn đứng nguyên.
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath, true);
h.events.clear();
h.step(0.0, 0.0, false);
h.step(0.0, 0.0, false); // confirm 2 cycle -> thay vì phanh: đứng ra
EXPECT_TRUE(h.last.contact_standdown);
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.collision);
EXPECT_TRUE(std::isinf(h.last.v_cap));
EXPECT_NE(std::string::npos, h.events.find("standing down")) << h.events;
// Đứng ra suốt 2 s dù chạm 0.07 m liên tục: không phanh, không cờ, không sự kiện mới. (v_cmd 0 + v_controller
// 0.3 để không kích slip của kStall — harness không mô phỏng bánh lăn.)
h.events.clear();
h.contact_dist = 0.07;
h.run(2.0, 0.0, 0.0, false);
EXPECT_TRUE(h.last.contact_standdown);
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.collision);
EXPECT_TRUE(h.events.empty()) << h.events;
// Robot đi được 0.35 m: vũ trang lại, chạm 0.07 m ở 0.2 m/s -> phanh như thường.
h.x += 0.35;
h.step(0.2, 0.0, true);
EXPECT_FALSE(h.last.contact_standdown);
EXPECT_NE(std::string::npos, h.events.find("re-armed after moving on")) << h.events;
h.step(0.2, 0.0, true);
h.step(0.2, 0.0, true);
EXPECT_TRUE(h.last.brake);
}
TEST(EnvMonitorContact, StandDownEndsWhenTheTrajectoryStaysClear)
{
Harness h(recoverAll());
h.contact_dist = 0.10;
h.v_controller_override = 0.3;
ASSERT_GT(h.runUntil(3.0, 0.0, 0.0, [](const EnvVerdict& v) { return v.collision; }, false), 0.0);
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath, true);
h.step(0.0, 0.0, false);
h.step(0.0, 0.0, false);
ASSERT_TRUE(h.last.contact_standdown);
h.contact_dist = kInf;
h.run(0.15, 0.1, 0.0, false); // 3 cycle sạch: chưa
EXPECT_TRUE(h.last.contact_standdown);
h.step(0.1, 0.0, false); // cycle 4: vũ trang lại
EXPECT_FALSE(h.last.contact_standdown);
EXPECT_NE(std::string::npos, h.events.find("re-armed, the trajectory is clear")) << h.events;
}
TEST(EnvMonitorContact, DetourThatMovedTheRobotDoesNotDisarmTheNextContact)
{
Harness h(recoverAll());
h.contact_dist = 0.10;
h.v_controller_override = 0.3;
ASSERT_GT(h.runUntil(3.0, 0.0, 0.0, [](const EnvVerdict& v) { return v.collision; }, false), 0.0);
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath, true);
h.contact_dist = kInf;
h.run(1.0, 0.3, 0.0, true); // đi 0.3 m trên đường mới, sạch
h.contact_dist = 0.10; // chạm mới ở chỗ khác -> phanh bình thường, không đứng ra
h.step(0.3, 0.0, false);
h.step(0.3, 0.0, false);
EXPECT_TRUE(h.last.brake);
EXPECT_FALSE(h.last.contact_standdown);
}
TEST(EnvMonitorContact, DetourFromAnotherTriggerDoesNotArmTheStandDown)
{
// controlling_failed / PRIEST tự khai: detour không phải do cờ collision -> chạm sau đó vẫn phanh + cờ.
Harness h(recoverAll());
h.monitor.onRecoveryFinished(h.now(), EnvRecoveryOutcome::kSucceededWithPath, false);
h.contact_dist = 0.10;
h.v_controller_override = 0.3;
h.step(0.0, 0.0, false);
h.step(0.0, 0.0, false);
EXPECT_TRUE(h.last.brake);
EXPECT_FALSE(h.last.contact_standdown);
}
TEST(EnvMonitorContact, OneCleanSampleDoesNotReleaseTheBrake)
{
Harness h(recoverAll());
h.contact_dist = 0.10;
h.step(0.5, 0.0, false);
h.step(0.5, 0.0, false);
ASSERT_TRUE(h.last.brake);
// Sạch 3 cycle rồi chạm lại: vẫn phanh, không có sự kiện nhả.
h.contact_dist = kInf;
h.run(0.15, 0.0, 0.0, false);
EXPECT_TRUE(h.last.brake);
h.contact_dist = 0.10;
h.step(0.0, 0.0, false);
EXPECT_TRUE(h.last.brake);
EXPECT_EQ(std::string::npos, h.events.find("cleared")) << h.events;
// Sạch 4 cycle liên tiếp: nhả.
h.contact_dist = kInf;
h.run(0.2, 0.0, 0.0, false);
EXPECT_FALSE(h.last.brake);
EXPECT_NE(std::string::npos, h.events.find("cleared"));
}
TEST(EnvMonitorContact, MinCheckSpeedZeroRestoresTheOldGuardBehaviour)
{
EnvMonitorConfig cfg = recoverAll();
cfg.contact.min_check_speed = 0.0;
Harness h(cfg);
h.contact_dist = 0.08;
h.step(0.07, 0.0, false);
h.step(0.07, 0.0, false);
EXPECT_TRUE(h.last.brake);
}
TEST(EnvMonitorContact, FarContactOnlyCapsSpeed)
{
Harness h(recoverAll());
h.contact_dist = 2.0;
h.run(1.0, 0.5, -0.5);
EXPECT_FALSE(h.last.contact_imminent);
EXPECT_FALSE(h.last.brake);
EXPECT_NEAR(std::sqrt(2.0 * 0.35 * 1.7), h.last.v_cap, 1e-6); // room = 2.0 - margin 0.2 - dư 0.1
h.contact_dist = kInf;
h.step(0.5, -0.5);
EXPECT_TRUE(std::isinf(h.last.v_cap));
}
TEST(EnvMonitorContact, InconclusiveSweepNeitherBrakesNorReleases)
{
Harness h(recoverAll());
h.contact_dist = 0.3;
h.run(0.2, 0.5, -0.5);
ASSERT_TRUE(h.last.brake);
h.contact_conclusive = false;
h.step(0.0, 0.0, false);
EXPECT_TRUE(h.last.brake); // không biết thì giữ nguyên, không nhả
Harness h2(recoverAll());
h2.contact_conclusive = false;
h2.contact_dist = 0.1;
h2.run(1.0, 0.5, -0.5);
EXPECT_FALSE(h2.last.brake); // không biết thì cũng không dừng
}
TEST(EnvMonitorContact, MeasuredSpeedExtendsTheStoppingDistance)
{
Harness h(recoverAll());
h.contact_dist = 0.6;
h.v_meas_override = 0.6; // lệnh 0.2 nhưng đo 0.6 -> quãng phanh theo 0.6 = 0.514 -> sắp chạm
h.run(0.2, 0.2, -0.2);
EXPECT_TRUE(h.last.contact_imminent);
EXPECT_NEAR(0.6 * 0.6 / (2.0 * 0.35), h.last.brake_dist, 1e-9);
}
// ---------------------------------------------------------------------------------------- disabled
TEST(EnvMonitorSwitches, DisabledMonitorIsSilent)
{
EnvMonitorConfig cfg = recoverAll();
cfg.enabled = false;
Harness h(cfg);
h.contact_dist = 0.1;
h.reachable = false;
h.run(8.0, 0.5, 0.0);
EXPECT_FALSE(h.last.brake);
EXPECT_FALSE(h.last.no_progress);
EXPECT_FALSE(h.last.path_blocked);
EXPECT_TRUE(std::isinf(h.last.v_cap));
}
TEST(EnvMonitorSwitches, EachVerdictCanBeDisabledAlone)
{
EnvMonitorConfig cfg = recoverAll();
cfg.stall.enabled = false;
cfg.contact.enabled = false;
Harness h(cfg);
h.run(3.0, 0.5, -0.5);
h.contact_dist = 0.1;
h.reachable = false;
h.run(6.0, 0.5, 0.0);
EXPECT_FALSE(h.last.stall_detected);
EXPECT_FALSE(h.last.brake);
EXPECT_TRUE(h.last.path_blocked);
}
} // namespace
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}