stall/confine_window_s 20 s, confine_radius 1,2 m, confine_cmd_factor 1,5: trong cửa sổ mọi vị trí nằm trong vòng bán kính < 1,2 m quanh tâm của chúng và lệnh tích luỹ >= 1,5 x max(2 x bán kính, disp_min) thì giơ stall (không cần geodesic — đi tới đi lui làm eff lật dấu). Cổng dịch chuyển đo đầu–cuối nên mù với vòng bán kính > 0,3 m; cổng đứng yên chỉ bắt gần như đứng. Replay 8 lượt JSONL 5–6/9: bắt 00:05 (101 s thay vì 206), 10:57 (27 s thay vì 75), 23:37; 0 lần ở 5 lượt sạch. Hệ số lệnh chặn ca bò thẳng chậm sau người. Verdict/JSONL/dòng 1 Hz thêm confined, region_radius. Sim 6/9 15:46: 2 mission tới goal, 0 cờ oan, cổng giam vùng không mở (không có ca loanh quanh trong lượt), kUnreachable 1 cờ đúng (cùng lúc PRIEST khai). Test: env_monitor 47/47 (+2), 10 bộ còn lại pass; YAML ↔ reader 69/69. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017xXjFjr2DQ6rhmhHSW4fk4
1015 lines
36 KiB
C++
1015 lines
36 KiB
C++
/*********************************************************************
|
||
*
|
||
* 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 * 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 * dt;
|
||
}
|
||
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 = x;
|
||
s.robot_y = y;
|
||
s.v_cmd = v_cmd;
|
||
s.v_meas = 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;
|
||
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;
|
||
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, 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);
|
||
}
|
||
|
||
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();
|
||
}
|