feat: EnvMonitor thay PathMonitor — giám sát môi trường bằng EDT/geodesic/quỹ đạo thật
Bộ giám sát mới trong ControlLoop (core/env_monitor, core/grid_analysis, ports/environment_port, io/env_logger), thay bốn phép đo của PathMonitor (code cũ còn, tắt bằng path_monitor/enabled: false): - kContact: dò tiếp xúc dọc quỹ đạo local planner (thay cung v/omega), bao tốc độ sqrt(2a(d - margin)) có dư, phanh chỉ khi vượt bao, cờ collision khi kề mũi tĩnh 1 s; cổng xoay tại chỗ theo lệnh controller muốn; hysteresis + xác nhận khi nhả; đứng ra sau detour không dịch robot (nhường controller); giữ phanh khi route khoá thì báo no valid command để controller_patience leo thang. - kStall: cost_to_go = geodesic tới điểm thoát 2 m dọc plan + phần plan còn lại, index plan bám cửa sổ, bộ lọc lật liên thông; cờ no_progress chỉ khi cổng dịch chuyển thực mở (robot không đi được đâu trong 12 s dù lệnh >= 1,5 m) — thay ngân sách replan cho stall. - kUnreachable: liên thông tới điểm thoát, đang ở action log. - EnvLogger ghi JSONL mỗi tick nặng; config/runtime/env_monitor_params.yaml đủ khoá, comment tiếng Việt; ControllerPort::measuredVelocity; FakeEnvironmentPort cho test. Sim 5–6/9: hết churn detour, khe 1,25 m đi qua 0,5 m/s, lượt 6/9 10:15 2 detour/142 s không oan. Test: grid_analysis 31, env_monitor 43, plan_inspector 37, walking_skeleton 87, các bộ khác pass. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017xXjFjr2DQ6rhmhHSW4fk4
This commit is contained in:
933
test/env_monitor_test.cpp
Normal file
933
test/env_monitor_test.cpp
Normal file
@@ -0,0 +1,933 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* 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 (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;
|
||||
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]
|
||||
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, 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();
|
||||
}
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <move_base2/ports/clock_port.h>
|
||||
#include <move_base2/ports/controller_port.h>
|
||||
#include <move_base2/ports/costmap_status_port.h>
|
||||
#include <move_base2/ports/environment_port.h>
|
||||
#include <move_base2/ports/mission_port.h>
|
||||
#include <move_base2/ports/path_check_port.h>
|
||||
#include <move_base2/ports/planner_port.h>
|
||||
@@ -982,6 +983,129 @@ private:
|
||||
mutable std::size_t motion_query_count_ = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class FakeEnvironmentPort
|
||||
* @brief Cổng môi trường giả cho EnvMonitor: kịch bản quyết định cost_to_go / tiếp xúc, không cần costmap.
|
||||
*
|
||||
* Mặc định "không kết luận được" cho hình học và quỹ đạo (như cổng gốc thiếu costmap/TF) nhưng CÓ thân
|
||||
* (footprint) — để các test cũ chạy với EnvMonitor bật mà không sinh verdict nào.
|
||||
*/
|
||||
class FakeEnvironmentPort final : public EnvironmentPort
|
||||
{
|
||||
public:
|
||||
bool sampleGeometry(const EnvGeometryRequest& request, EnvGeometry& out) override
|
||||
{
|
||||
(void)request;
|
||||
++geometry_query_count_;
|
||||
if (!geometry_available_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
out = EnvGeometry();
|
||||
out.valid = true;
|
||||
out.free_radius = body_side_;
|
||||
out.cost.conclusive = true;
|
||||
out.cost.reachable = reachable_;
|
||||
out.cost.cost_to_go = reachable_ ? cost_to_go_ : std::numeric_limits<double>::infinity();
|
||||
out.edt_body_min = edt_body_min_;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sweepTrajectory(const EnvTrajectoryRequest& request, SweepResult& out, bool& used_local_plan) override
|
||||
{
|
||||
(void)request;
|
||||
++sweep_query_count_;
|
||||
used_local_plan = false;
|
||||
if (!sweep_available_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
out = SweepResult();
|
||||
out.conclusive = true;
|
||||
out.scanned = 1;
|
||||
out.blocked = contact_blocked_;
|
||||
out.distance = contact_blocked_ ? contact_distance_ : 0.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool robotBodyExtents(double& front, double& rear, double& side) const override
|
||||
{
|
||||
if (!body_available_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
front = body_front_;
|
||||
rear = body_rear_;
|
||||
side = body_side_;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @param distance [m] quãng dọc quỹ đạo tới điểm chạm.
|
||||
void setContact(bool blocked, double distance = 0.1)
|
||||
{
|
||||
sweep_available_ = true;
|
||||
contact_blocked_ = blocked;
|
||||
contact_distance_ = distance;
|
||||
}
|
||||
|
||||
void setSweepAvailable(bool available)
|
||||
{
|
||||
sweep_available_ = available;
|
||||
}
|
||||
|
||||
/// @param cost_to_go [m] chi phí còn lại; @p edt_body_min [m] khoảng trống thân.
|
||||
void setGeometry(bool reachable, double cost_to_go, double edt_body_min = 1.0)
|
||||
{
|
||||
geometry_available_ = true;
|
||||
reachable_ = reachable;
|
||||
cost_to_go_ = cost_to_go;
|
||||
edt_body_min_ = edt_body_min;
|
||||
}
|
||||
|
||||
void setGeometryAvailable(bool available)
|
||||
{
|
||||
geometry_available_ = available;
|
||||
}
|
||||
|
||||
void setBodyExtents(double front, double rear, double side)
|
||||
{
|
||||
body_available_ = true;
|
||||
body_front_ = front;
|
||||
body_rear_ = rear;
|
||||
body_side_ = side;
|
||||
}
|
||||
|
||||
void setBodyAvailable(bool available)
|
||||
{
|
||||
body_available_ = available;
|
||||
}
|
||||
|
||||
std::size_t sweepQueryCount() const
|
||||
{
|
||||
return sweep_query_count_;
|
||||
}
|
||||
|
||||
std::size_t geometryQueryCount() const
|
||||
{
|
||||
return geometry_query_count_;
|
||||
}
|
||||
|
||||
private:
|
||||
bool geometry_available_ = false;
|
||||
bool reachable_ = true;
|
||||
double cost_to_go_ = 10.0; // [m]
|
||||
double edt_body_min_ = 1.0; // [m]
|
||||
bool sweep_available_ = false;
|
||||
bool contact_blocked_ = false;
|
||||
double contact_distance_ = 0.1; // [m]
|
||||
bool body_available_ = true;
|
||||
double body_front_ = 0.58; // [m]
|
||||
double body_rear_ = 0.58; // [m]
|
||||
double body_side_ = 0.48; // [m]
|
||||
std::size_t geometry_query_count_ = 0;
|
||||
std::size_t sweep_query_count_ = 0;
|
||||
};
|
||||
|
||||
class FakeMissionPort final : public MissionPort
|
||||
{
|
||||
public:
|
||||
|
||||
683
test/grid_analysis_test.cpp
Normal file
683
test/grid_analysis_test.cpp
Normal file
@@ -0,0 +1,683 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Kiểm grid_analysis: EDT, mặt nạ tự do, trường địa trắc (cấm cắt góc), cost_to_go theo plan, hành
|
||||
* lang vuông góc với plan, khoảng trống thân, kéo footprint dọc quỹ đạo.
|
||||
*
|
||||
* Lưới 10 x 10 m, ô 0.05 m — cùng resolution với local costmap runtime. Thân 1.166 x 0.96 m như
|
||||
* footprint sim (front = rear = 0.583, side = 0.48), inscribed 0.48.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
#include <nav_test_harness/fake_costmap.h>
|
||||
#include <robot_costmap_2d/cost_values.h>
|
||||
|
||||
#include <move_base2/core/grid_analysis.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using move_base2::analyzeGrid;
|
||||
using move_base2::bodyClearance;
|
||||
using move_base2::computeGeodesic;
|
||||
using move_base2::CorridorParams;
|
||||
using move_base2::CorridorResult;
|
||||
using move_base2::costToGo;
|
||||
using move_base2::CostToGoParams;
|
||||
using move_base2::CostToGoResult;
|
||||
using move_base2::distanceAt;
|
||||
using move_base2::GridScratch;
|
||||
using move_base2::PlanarTransform;
|
||||
using move_base2::scanCorridor;
|
||||
using move_base2::SweepParams;
|
||||
using move_base2::SweepResult;
|
||||
using move_base2::sweepTrajectory;
|
||||
|
||||
constexpr double kRes = 0.05;
|
||||
constexpr double kInscribed = 0.48;
|
||||
constexpr unsigned char kLethal = robot_costmap_2d::LETHAL_OBSTACLE;
|
||||
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||||
|
||||
robot_geometry_msgs::PoseStamped makePose(double x, double y, double yaw = 0.0)
|
||||
{
|
||||
robot_geometry_msgs::PoseStamped pose;
|
||||
pose.header.frame_id = "odom";
|
||||
pose.pose.position.x = x;
|
||||
pose.pose.position.y = y;
|
||||
pose.pose.orientation.z = std::sin(0.5 * yaw);
|
||||
pose.pose.orientation.w = std::cos(0.5 * yaw);
|
||||
return pose;
|
||||
}
|
||||
|
||||
std::vector<robot_geometry_msgs::PoseStamped> linePlan(double from_x, double to_x, double step,
|
||||
double y = 0.0)
|
||||
{
|
||||
std::vector<robot_geometry_msgs::PoseStamped> plan;
|
||||
for (double x = from_x; x <= to_x + 1e-9; x += step)
|
||||
{
|
||||
plan.push_back(makePose(x, y));
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
nav_test_harness::FakeCostmap makeGrid()
|
||||
{
|
||||
return nav_test_harness::FakeCostmap::centered(10.0, kRes);
|
||||
}
|
||||
|
||||
/// Vạch ô lethal thẳng từ (x0,y0) tới (x1,y1), bước nửa ô.
|
||||
void lethalLine(nav_test_harness::FakeCostmap& grid, 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 / (0.5 * kRes))));
|
||||
for (int i = 0; i <= n; ++i)
|
||||
{
|
||||
const double t = static_cast<double>(i) / n;
|
||||
grid.setLethalAt(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t);
|
||||
}
|
||||
}
|
||||
|
||||
bool cellOf(const GridScratch& s, double x, double y, unsigned int& mx, unsigned int& my)
|
||||
{
|
||||
return s.toCell(x, y, mx, my);
|
||||
}
|
||||
|
||||
double geoAt(const GridScratch& s, double x, double y)
|
||||
{
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
if (!cellOf(s, x, y, mx, my))
|
||||
{
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
return s.geo[s.index(mx, my)];
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------ EDT
|
||||
|
||||
TEST(GridAnalysisEdt, EmptyGridIsAllFreeAndInfinitelyFar)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0)));
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
ASSERT_TRUE(cellOf(s, 2.0, -3.0, mx, my));
|
||||
EXPECT_EQ(1, s.free[s.index(mx, my)]);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisEdt, DistanceToASingleLethalCellIsEuclidean)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalAt(0.0, 0.0);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
|
||||
EXPECT_NEAR(0.0, distanceAt(s, 0.0, 0.0), 1e-6);
|
||||
EXPECT_NEAR(1.0, distanceAt(s, 1.0, 0.0), 1.5 * kRes);
|
||||
EXPECT_NEAR(std::hypot(0.5, 0.5), distanceAt(s, 0.5, 0.5), 1.5 * kRes);
|
||||
EXPECT_NEAR(3.0, distanceAt(s, -3.0, 0.0), 1.5 * kRes);
|
||||
|
||||
// Mặt nạ tự do theo inscribed: 0.3 m thì tâm không đặt được, 0.6 m thì được.
|
||||
unsigned int mx = 0;
|
||||
unsigned int my = 0;
|
||||
ASSERT_TRUE(cellOf(s, 0.3, 0.0, mx, my));
|
||||
EXPECT_EQ(0, s.free[s.index(mx, my)]);
|
||||
ASSERT_TRUE(cellOf(s, 0.6, 0.0, mx, my));
|
||||
EXPECT_EQ(1, s.free[s.index(mx, my)]);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisEdt, UnknownAndInflationAreNotObstacles)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setCostAt(0.0, 0.0, robot_costmap_2d::NO_INFORMATION);
|
||||
grid.setCostAt(1.0, 1.0, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0)));
|
||||
EXPECT_TRUE(std::isinf(distanceAt(s, 1.0, 1.0)));
|
||||
}
|
||||
|
||||
TEST(GridAnalysisEdt, OutsideTheGridIsNaN)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_TRUE(std::isnan(distanceAt(s, 7.0, 0.0)));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------- geodesic
|
||||
|
||||
TEST(GridAnalysisGeodesic, OpenFieldMatchesOctileDistance)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
unsigned int tx = 0;
|
||||
unsigned int ty = 0;
|
||||
ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty));
|
||||
std::size_t visited = 0;
|
||||
ASSERT_TRUE(computeGeodesic(tx, ty, s, &visited));
|
||||
EXPECT_GT(visited, 0u);
|
||||
|
||||
EXPECT_NEAR(0.0, geoAt(s, 0.0, 0.0), 1e-6);
|
||||
EXPECT_NEAR(2.0, geoAt(s, 2.0, 0.0), 2.0 * kRes);
|
||||
EXPECT_NEAR(2.0 * std::sqrt(2.0), geoAt(s, 2.0, 2.0), 2.0 * kRes);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisGeodesic, WallForcesTheWayAround)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
lethalLine(grid, 1.0, -1.5, 1.0, 1.5);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
unsigned int tx = 0;
|
||||
unsigned int ty = 0;
|
||||
ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty));
|
||||
ASSERT_TRUE(computeGeodesic(tx, ty, s));
|
||||
|
||||
const double behind = geoAt(s, 2.0, 0.0);
|
||||
EXPECT_TRUE(std::isfinite(behind));
|
||||
// Thẳng là 2.0 m; phải vòng qua đầu tường (1.5 m + inscribed) nên dài hơn hẳn.
|
||||
EXPECT_GT(behind, 3.5);
|
||||
// Ô ngay trong dải cấm quanh tường không có đường (không free).
|
||||
EXPECT_TRUE(std::isinf(geoAt(s, 1.0, 0.0)));
|
||||
}
|
||||
|
||||
TEST(GridAnalysisGeodesic, SealedRoomIsUnreachable)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
lethalLine(grid, -1.5, -1.5, 1.5, -1.5);
|
||||
lethalLine(grid, 1.5, -1.5, 1.5, 1.5);
|
||||
lethalLine(grid, 1.5, 1.5, -1.5, 1.5);
|
||||
lethalLine(grid, -1.5, 1.5, -1.5, -1.5);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
unsigned int tx = 0;
|
||||
unsigned int ty = 0;
|
||||
ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty));
|
||||
ASSERT_TRUE(computeGeodesic(tx, ty, s));
|
||||
EXPECT_TRUE(std::isfinite(geoAt(s, 0.5, 0.5)));
|
||||
EXPECT_TRUE(std::isinf(geoAt(s, 3.0, 3.0)));
|
||||
}
|
||||
|
||||
TEST(GridAnalysisGeodesic, DiagonalWallOfSingleCellsIsNotLeaked)
|
||||
{
|
||||
// Bán kính free ~0 để mặt nạ = "không lethal": tường chéo gồm các ô (i, i) chỉ chạm nhau ở góc.
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
for (int i = -100; i < 100; ++i) // trọn đường chéo của lưới 200 ô, không để hở hai đầu
|
||||
{
|
||||
grid.setLethalAt(i * kRes + 0.5 * kRes, i * kRes + 0.5 * kRes);
|
||||
}
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, 0.01, s));
|
||||
unsigned int tx = 0;
|
||||
unsigned int ty = 0;
|
||||
ASSERT_TRUE(cellOf(s, 1.0, -1.0, tx, ty));
|
||||
ASSERT_TRUE(computeGeodesic(tx, ty, s));
|
||||
// Không cắt góc: bên kia tường chéo phải là +inf, cùng bên thì tới được.
|
||||
EXPECT_TRUE(std::isinf(geoAt(s, -1.0, 1.0)));
|
||||
EXPECT_TRUE(std::isfinite(geoAt(s, 2.0, -2.0)));
|
||||
}
|
||||
|
||||
TEST(GridAnalysisGeodesic, TargetInsideTheForbiddenBandIsRejected)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalAt(0.0, 0.0);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
unsigned int tx = 0;
|
||||
unsigned int ty = 0;
|
||||
ASSERT_TRUE(cellOf(s, 0.2, 0.0, tx, ty));
|
||||
EXPECT_FALSE(computeGeodesic(tx, ty, s));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------- cost to go
|
||||
|
||||
TEST(GridAnalysisCostToGo, StraightPlanInTheWindowEqualsRemainingLength)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-3.0, 3.0, 0.1);
|
||||
|
||||
CostToGoResult r = costToGo(plan, 0, -3.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.reachable);
|
||||
EXPECT_EQ(CostToGoResult::Target::kGoal, r.target);
|
||||
EXPECT_NEAR(6.0, r.cost_to_go, 0.15);
|
||||
EXPECT_NEAR(0.0, r.arc_rest, 1e-9);
|
||||
|
||||
// Robot giữa plan: còn 3 m.
|
||||
r = costToGo(plan, 30, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_NEAR(3.0, r.cost_to_go, 0.15);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, LateralDeviationCostsTheOctileDetour)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-3.0, 3.0, 0.1);
|
||||
// Robot lệch 1.5 m ngang tại x = 0: đường 8-liên-thông tới (3, 0) dài 1.5*sqrt2 + 1.5 = 3.62 m.
|
||||
const CostToGoResult r = costToGo(plan, 30, 0.0, 1.5, CostToGoParams(), PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.reachable);
|
||||
EXPECT_NEAR(3.62, r.cost_to_go, 0.2);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, PlanLeavingTheWindowUsesTheExitPointPlusRemainingArc)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(0.0, 8.0, 0.1); // rời lưới ở x = 5
|
||||
const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.reachable);
|
||||
EXPECT_EQ(CostToGoResult::Target::kExit, r.target);
|
||||
EXPECT_GT(r.arc_rest, 2.9);
|
||||
EXPECT_LT(r.arc_rest, 3.2);
|
||||
EXPECT_NEAR(8.0, r.cost_to_go, 0.2);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, ExitAheadPinsTheTargetAtAFixedArcLength)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(0.0, 8.0, 0.1); // rời lưới ở x = 5
|
||||
CostToGoParams params;
|
||||
params.exit_ahead = 2.0;
|
||||
const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.reachable);
|
||||
EXPECT_EQ(CostToGoResult::Target::kAhead, r.target);
|
||||
EXPECT_EQ(20u, r.target_index); // pose ở x = 2.0
|
||||
EXPECT_NEAR(6.0, r.arc_rest, 0.05); // 8 − 2
|
||||
EXPECT_NEAR(8.0, r.cost_to_go, 0.2); // geo 2 + arc 6: cùng tổng với bản cũ khi đi thẳng
|
||||
// Cửa sổ trôi không kéo T lùi: robot tiến 1 m -> from_index 10 -> T tiến theo (x = 3.0), arc_rest giảm đúng 1 m.
|
||||
const CostToGoResult r2 = costToGo(plan, 10, 1.0, 0.0, params, PlanarTransform(), s);
|
||||
EXPECT_EQ(30u, r2.target_index);
|
||||
EXPECT_NEAR(5.0, r2.arc_rest, 0.05);
|
||||
// Quãng cố định vượt cửa sổ thì vẫn kẹp ở điểm rời cửa sổ (kExit).
|
||||
params.exit_ahead = 7.0;
|
||||
const CostToGoResult r3 = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||
EXPECT_EQ(CostToGoResult::Target::kExit, r3.target);
|
||||
// Goal trong quãng thì T = goal.
|
||||
params.exit_ahead = 2.0;
|
||||
const CostToGoResult r4 = costToGo(linePlan(0.0, 1.0, 0.1), 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||
EXPECT_EQ(CostToGoResult::Target::kGoal, r4.target);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, BlockedExitBacksOffAlongThePlanOrReportsBlocked)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalCircle(3.0, 0.0, 0.3);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
||||
|
||||
// T = (3, 0) nằm trong vật cản; ô free gần nhất dọc plan cách ~0.78 m > backoff 0.5 -> đích bị bịt.
|
||||
CostToGoParams params;
|
||||
params.target_backoff = 0.5;
|
||||
CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_FALSE(r.reachable);
|
||||
EXPECT_EQ(CostToGoResult::Target::kBlockedExit, r.target);
|
||||
|
||||
// Cho lùi 1.0 m thì tìm được T free và có đường.
|
||||
params.target_backoff = 1.0;
|
||||
r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.reachable);
|
||||
EXPECT_EQ(CostToGoResult::Target::kBackedOff, r.target);
|
||||
EXPECT_GT(r.arc_rest, 0.7);
|
||||
EXPECT_NEAR(3.0, r.cost_to_go, 0.2);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, RobotHuggingAnObstacleSnapsToTheNearestFreeCell)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
lethalLine(grid, -3.0, 0.4, 3.0, 0.4); // tường sát bên trái tuyến
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-3.0, 3.0, 0.1, -0.6);
|
||||
// Robot tại (0, 0.1): cách tường 0.3 < inscribed -> ô không free, snap trong 0.35 m.
|
||||
const CostToGoResult r = costToGo(plan, 30, 0.0, 0.1, CostToGoParams(), PlanarTransform(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.robot_in_ring);
|
||||
EXPECT_TRUE(r.reachable);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, EmptyPlanOrRobotOutsideIsInconclusive)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
std::vector<robot_geometry_msgs::PoseStamped> empty;
|
||||
EXPECT_FALSE(costToGo(empty, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s).conclusive);
|
||||
const auto plan = linePlan(0.0, 3.0, 0.1);
|
||||
EXPECT_FALSE(costToGo(plan, 0, 7.0, 0.0, CostToGoParams(), PlanarTransform(), s).conclusive);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCostToGo, TransformIsAppliedToThePlan)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
// Plan trong frame "map" = lưới dịch +10 m: (10..13, 0) map -> (0..3, 0) lưới.
|
||||
const auto plan = linePlan(10.0, 13.0, 0.1);
|
||||
PlanarTransform map_to_grid;
|
||||
map_to_grid.x = -10.0;
|
||||
const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), map_to_grid, s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_NEAR(3.0, r.cost_to_go, 0.15);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------- corridor
|
||||
|
||||
TEST(GridAnalysisCorridor, GapNarrowerThanTheBodyIsABlock)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalCircle(1.0, 0.6, 0.2); // mép trong ở y = +0.4
|
||||
grid.setLethalCircle(1.0, -0.6, 0.2); // mép trong ở y = -0.4 -> khe 0.8 m < thân 0.96
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||
const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||
EXPECT_GT(r.scanned, 0u);
|
||||
EXPECT_NEAR(0.8, r.min_width, 0.12);
|
||||
EXPECT_NEAR(3.0, r.block_dist, 0.25);
|
||||
// Tâm robot không đặt được ở khe (0.4 < 0.48): plan hết sạch trước chỗ khe.
|
||||
EXPECT_LT(r.plan_clear_ahead, 3.0);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCorridor, GapWiderThanTheBodyIsNotABlock)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalCircle(1.0, 0.8, 0.2); // mép trong ở y = +0.6 -> khe 1.2 m
|
||||
grid.setLethalCircle(1.0, -0.8, 0.2);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||
const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||
EXPECT_TRUE(std::isinf(r.block_dist));
|
||||
EXPECT_NEAR(1.2, r.min_width, 0.12);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCorridor, WallOnOneSideWithRoomOnTheOtherIsNotABlock)
|
||||
{
|
||||
// Ca mission 1 ngày 5/9: plan sượt tường nhưng bên kia trống -> hành lang vẫn rộng.
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
lethalLine(grid, 0.0, 0.3, 2.0, 0.3);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||
const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||
EXPECT_TRUE(std::isinf(r.block_dist));
|
||||
EXPECT_NEAR(0.3 + 1.5, r.min_width, 0.12); // trái 0.3 + phải chạm trần 1.5
|
||||
// Tâm cách đầu tường (0, 0.3) đúng 0.48 m tại x = -sqrt(0.48^2 - 0.3^2) = -0.375 -> 1.625 m từ x = -2.
|
||||
EXPECT_NEAR(1.625, r.plan_clear_ahead, 0.15);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisCorridor, HorizonLimitsTheScan)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalCircle(2.5, 0.6, 0.2);
|
||||
grid.setLethalCircle(2.5, -0.6, 0.2);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const auto plan = linePlan(-2.0, 3.0, 0.1);
|
||||
CorridorParams params;
|
||||
params.horizon = 3.0; // khe ở 4.5 m dọc plan: ngoài tầm
|
||||
const CorridorResult r = scanCorridor(plan, 0, params, PlanarTransform(), s);
|
||||
EXPECT_TRUE(std::isinf(r.block_dist));
|
||||
EXPECT_NEAR(3.0, r.plan_clear_ahead, 1e-9);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------- body clearance
|
||||
|
||||
TEST(GridAnalysisBody, ClearanceBesideAWall)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
lethalLine(grid, -2.0, 0.7, 2.0, 0.7);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
// Cạnh trái thân ở y = 0.48, tường ở 0.7 -> 0.22 m.
|
||||
EXPECT_NEAR(0.22, bodyClearance(s, 0.0, 0.0, 0.0, 0.583, 0.583, 0.48), 1.5 * kRes);
|
||||
// Quay 90 độ: mũi (0.583) hướng về tường -> 0.117 m.
|
||||
EXPECT_NEAR(0.117, bodyClearance(s, 0.0, 0.0, M_PI / 2.0, 0.583, 0.583, 0.48), 1.5 * kRes);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisBody, EmptyGridClearanceIsInfinite)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_TRUE(std::isinf(bodyClearance(s, 0.0, 0.0, 0.3, 0.583, 0.583, 0.48)));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------- sweep
|
||||
|
||||
std::vector<robot_geometry_msgs::PoseStamped> straightTrajectory(double length, double step)
|
||||
{
|
||||
std::vector<robot_geometry_msgs::PoseStamped> traj;
|
||||
for (double x = 0.0; x <= length + 1e-9; x += step)
|
||||
{
|
||||
traj.push_back(makePose(x, 0.0, 0.0));
|
||||
}
|
||||
return traj;
|
||||
}
|
||||
|
||||
TEST(GridAnalysisSweep, StraightTrajectoryIntoAWallReportsTheNoseContact)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalAt(1.5, 0.0);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||
SweepParams(), *grid.costmap(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_TRUE(r.blocked);
|
||||
EXPECT_EQ(1, r.hit_edge); // mũi
|
||||
// Mũi đệm 0.633 m chạm ô tại 1.5 m (mép ô ~1.475): còn ~0.84 m đường đi.
|
||||
EXPECT_NEAR(0.84, r.distance, 0.1);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisSweep, ClearTrajectoryIsNotBlocked)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalAt(1.5, 1.5);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const SweepResult r = sweepTrajectory(straightTrajectory(1.4, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||
SweepParams(), *grid.costmap(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_FALSE(r.blocked);
|
||||
EXPECT_GT(r.scanned, 0u);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisSweep, TrajectoryNotStartingAtTheRobotIsInconclusive)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
// Quỹ đạo bắt đầu ở (0,0) nhưng robot ở (1.0, 0): đây là global plan, không phải chuyển động sắp xảy ra.
|
||||
const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 1.0, 0.0, 0.0, 0.3,
|
||||
SweepParams(), *grid.costmap(), s);
|
||||
EXPECT_FALSE(r.conclusive);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisSweep, LethalCellUnderTheBodyIsExcludedByTheBaseline)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalAt(0.3, 0.50); // kề hông trái, trong thân đệm (0.53)
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||
SweepParams(), *grid.costmap(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_GE(r.baseline_cells, 1);
|
||||
EXPECT_FALSE(r.blocked); // đi thẳng: ô đó trượt ra sau, không "thêm" ô mới vào thân
|
||||
|
||||
SweepParams no_baseline;
|
||||
no_baseline.exclude_baseline = false;
|
||||
const SweepResult r2 = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||
no_baseline, *grid.costmap(), s);
|
||||
EXPECT_TRUE(r2.blocked);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisSweep, TrajectoryCurvingAwayFromAWallIsClear)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
// Tường cách 1.3 m: đi thẳng thì mũi đệm (0.633) chạm sau ~0.65 m; cung R 0.4 quay 90 độ có góc
|
||||
// mũi-phải quét xa nhất sqrt(0.633^2 + 0.93^2) = 1.13 m < 1.3 -> không chạm.
|
||||
lethalLine(grid, 1.3, -2.0, 1.3, 2.0);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
// Quỹ đạo quay gắt sang trái: cung R = 0.4 m, 90 độ, kết thúc song song tường ở x ~ 0.4.
|
||||
std::vector<robot_geometry_msgs::PoseStamped> traj;
|
||||
for (int i = 0; i <= 20; ++i)
|
||||
{
|
||||
const double a = (M_PI / 2.0) * i / 20.0;
|
||||
traj.push_back(makePose(0.4 * std::sin(a), 0.4 * (1.0 - std::cos(a)), a));
|
||||
}
|
||||
SweepParams params;
|
||||
params.lookahead = 1.0;
|
||||
const SweepResult straight = sweepTrajectory(straightTrajectory(1.5, 0.1), 0.0, 0.0, 0.0, 0.3,
|
||||
params, *grid.costmap(), s);
|
||||
const SweepResult curve = sweepTrajectory(traj, 0.0, 0.0, 0.0, 0.3, params, *grid.costmap(), s);
|
||||
ASSERT_TRUE(straight.conclusive);
|
||||
EXPECT_TRUE(straight.blocked);
|
||||
ASSERT_TRUE(curve.conclusive);
|
||||
EXPECT_FALSE(curve.blocked);
|
||||
}
|
||||
|
||||
TEST(GridAnalysisSweep, LookaheadStopsTheSweep)
|
||||
{
|
||||
nav_test_harness::FakeCostmap grid = makeGrid();
|
||||
grid.setLethalAt(3.0, 0.0);
|
||||
GridScratch s;
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s));
|
||||
SweepParams params;
|
||||
params.lookahead = 1.5;
|
||||
const SweepResult r = sweepTrajectory(straightTrajectory(4.0, 0.1), 0.0, 0.0, 0.0, 0.3, params,
|
||||
*grid.costmap(), s);
|
||||
ASSERT_TRUE(r.conclusive);
|
||||
EXPECT_FALSE(r.blocked);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------- scratch reuse
|
||||
|
||||
TEST(GridAnalysisScratch, ReusedAcrossGridsWithoutStaleState)
|
||||
{
|
||||
GridScratch s;
|
||||
nav_test_harness::FakeCostmap a = makeGrid();
|
||||
a.setLethalAt(0.0, 0.0);
|
||||
ASSERT_TRUE(analyzeGrid(*a.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_NEAR(0.0, distanceAt(s, 0.0, 0.0), 1e-6);
|
||||
|
||||
nav_test_harness::FakeCostmap b = makeGrid(); // trống
|
||||
ASSERT_TRUE(analyzeGrid(*b.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0)));
|
||||
|
||||
nav_test_harness::FakeCostmap c(100, 60, kRes, -2.5, -1.5); // kích thước khác
|
||||
c.setLethalAt(1.0, 0.0);
|
||||
ASSERT_TRUE(analyzeGrid(*c.costmap(), kLethal, kInscribed, s));
|
||||
EXPECT_EQ(100u, s.width);
|
||||
EXPECT_EQ(60u, s.height);
|
||||
EXPECT_NEAR(1.0, distanceAt(s, 0.0, 0.0), 1.5 * kRes);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------- chi phí
|
||||
|
||||
TEST(GridAnalysisBench, ReportCostOnRuntimeSizedGrid)
|
||||
{
|
||||
// 160 x 160 @ 0.05 m như local costmap runtime, bãi 30 trụ rải ngẫu nhiên. Chỉ in số, không phán
|
||||
// pass/fail: chi phí tuyệt đối phụ thuộc máy; plan (§2.5) dự tính EDT ~0.3 ms + Dijkstra <= 0.4 ms.
|
||||
nav_test_harness::FakeCostmap grid = nav_test_harness::FakeCostmap::centered(8.0, kRes);
|
||||
unsigned int seed = 12345u;
|
||||
for (int i = 0; i < 30; ++i)
|
||||
{
|
||||
seed = seed * 1103515245u + 12345u;
|
||||
const double x = -3.5 + 7.0 * ((seed >> 8) % 1000) / 1000.0;
|
||||
seed = seed * 1103515245u + 12345u;
|
||||
const double y = -3.5 + 7.0 * ((seed >> 8) % 1000) / 1000.0;
|
||||
grid.setLethalCircle(x, y, 0.25);
|
||||
}
|
||||
// Dọn một hành lang trống dọc plan để T và ô robot luôn free -> Dijkstra chạy thật (thăm toàn bộ
|
||||
// thành phần liên thông chứa T = trường hợp xấu nhất về chi phí).
|
||||
// Lấy mẫu tại TÂM ô: bước đúng một ô nhưng đặt trên biên ô thì sai số float bỏ sót ô.
|
||||
for (double x = -0.6 + 0.5 * kRes; x < 4.0; x += kRes)
|
||||
{
|
||||
for (double y = -0.8 + 0.5 * kRes; y < 0.8; y += kRes)
|
||||
{
|
||||
grid.setCostAt(x, y, robot_costmap_2d::FREE_SPACE);
|
||||
}
|
||||
}
|
||||
GridScratch s;
|
||||
const auto plan = linePlan(0.0, 3.9, 0.1);
|
||||
ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // ensure() cấp phát một lần
|
||||
|
||||
constexpr int kIters = 200;
|
||||
auto t0 = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < kIters; ++i)
|
||||
{
|
||||
analyzeGrid(*grid.costmap(), kLethal, kInscribed, s);
|
||||
}
|
||||
auto t1 = std::chrono::steady_clock::now();
|
||||
CostToGoResult last;
|
||||
for (int i = 0; i < kIters; ++i)
|
||||
{
|
||||
last = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s);
|
||||
}
|
||||
auto t2 = std::chrono::steady_clock::now();
|
||||
CorridorResult corr;
|
||||
for (int i = 0; i < kIters; ++i)
|
||||
{
|
||||
corr = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s);
|
||||
}
|
||||
auto t3 = std::chrono::steady_clock::now();
|
||||
SweepResult sw;
|
||||
const auto traj = straightTrajectory(1.5, 0.1);
|
||||
for (int i = 0; i < kIters; ++i)
|
||||
{
|
||||
sw = sweepTrajectory(traj, 0.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s);
|
||||
}
|
||||
auto t4 = std::chrono::steady_clock::now();
|
||||
const auto us = [](auto a, auto b) {
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(b - a).count() / static_cast<double>(kIters);
|
||||
};
|
||||
std::printf("[bench] 160x160: analyzeGrid %.0f us | costToGo %.0f us (visited %zu, reachable %d, "
|
||||
"conclusive %d, in_ring %d, target %d, edt_robot %.2f, edt_T %.2f, cost %.2f) | "
|
||||
"scanCorridor %.0f us (scanned %zu) | sweepTrajectory %.0f us\n",
|
||||
us(t0, t1), us(t1, t2), last.cells_visited, last.reachable ? 1 : 0,
|
||||
last.conclusive ? 1 : 0, last.robot_in_ring ? 1 : 0, static_cast<int>(last.target),
|
||||
distanceAt(s, 0.0, 0.0), distanceAt(s, 3.9, 0.0), last.cost_to_go, us(t2, t3),
|
||||
corr.scanned, us(t3, t4));
|
||||
EXPECT_TRUE(last.conclusive);
|
||||
EXPECT_TRUE(last.reachable);
|
||||
EXPECT_GT(last.cells_visited, 1000u);
|
||||
EXPECT_TRUE(sw.conclusive);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
@@ -28,6 +28,7 @@ namespace
|
||||
using move_base2::checkPlan;
|
||||
using move_base2::crossTrackError;
|
||||
using move_base2::nearestIndex;
|
||||
using move_base2::trackedNearestIndex;
|
||||
using move_base2::PlanarTransform;
|
||||
using move_base2::PlanBlockResult;
|
||||
using move_base2::planarTransform;
|
||||
@@ -202,6 +203,30 @@ TEST(PlanInspectorNearest, PlanFoldingBackOnItselfPicksTheNearerLeg)
|
||||
EXPECT_EQ(nearestIndex(plan, makePose(1.0, 0.95)), 2u);
|
||||
}
|
||||
|
||||
TEST(PlanInspectorNearest, TrackedIndexStaysOnTheCurrentLegOfAFoldedPlan)
|
||||
{
|
||||
// Plan ra 4 m rồi vòng lại song song cách 0.6 m: robot đứng giữa hai đoạn (0.35 m tới đoạn đầu, 0.25 m tới
|
||||
// đoạn về). nearestIndex toàn plan nhảy sang đoạn về (index 2); bám từ hint 0 với cửa sổ 1 m lùi / 3 m tiến
|
||||
// thì đoạn về nằm ngoài 3 m dọc plan -> giữ đoạn đầu.
|
||||
std::vector<robot_geometry_msgs::PoseStamped> plan{ makePose(0.0, 0.0), makePose(4.0, 0.0), makePose(4.0, 0.6),
|
||||
makePose(0.0, 0.6) };
|
||||
const auto robot = makePose(1.0, 0.35);
|
||||
EXPECT_EQ(nearestIndex(plan, robot), 2u);
|
||||
EXPECT_EQ(trackedNearestIndex(plan, robot, 0, 1.0, 3.0), 0u);
|
||||
// Cửa sổ đủ dài thì bằng nearest toàn plan.
|
||||
EXPECT_EQ(trackedNearestIndex(plan, robot, 0, 1.0, 20.0), 2u);
|
||||
}
|
||||
|
||||
TEST(PlanInspectorNearest, TrackedIndexAdvancesAndClampsTheHint)
|
||||
{
|
||||
const auto plan = linePlan(0.0, 5.0, 0.5); // 11 pose, 10 đoạn
|
||||
EXPECT_EQ(trackedNearestIndex(plan, makePose(2.3, 0.1), 2, 1.0, 3.0), 4u); // tiến
|
||||
EXPECT_EQ(trackedNearestIndex(plan, makePose(1.2, 0.0), 4, 1.0, 3.0), 2u); // lùi trong 1 m
|
||||
EXPECT_EQ(trackedNearestIndex(plan, makePose(0.1, 0.0), 8, 1.0, 3.0), 6u); // lùi bị chặn ở 1 m: đoạn 6
|
||||
EXPECT_EQ(trackedNearestIndex(plan, makePose(4.9, 0.0), 99, 1.0, 3.0), 9u); // hint vượt cỡ -> kẹp
|
||||
EXPECT_EQ(trackedNearestIndex({}, makePose(0.0, 0.0), 3, 1.0, 3.0), 0u);
|
||||
}
|
||||
|
||||
TEST(PlanInspectorNearest, ShortPlansAreSafe)
|
||||
{
|
||||
EXPECT_EQ(nearestIndex({}, makePose(0.0, 0.0)), 0u);
|
||||
|
||||
@@ -33,6 +33,7 @@ using move_base2::testing::FakeClockPort;
|
||||
using move_base2::testing::FakeControllerPort;
|
||||
using move_base2::testing::FakeCostmapStatusPort;
|
||||
using move_base2::testing::FakeMissionPort;
|
||||
using move_base2::testing::FakeEnvironmentPort;
|
||||
using move_base2::testing::FakePathCheckPort;
|
||||
using move_base2::testing::FakePlannerPort;
|
||||
using move_base2::testing::FakePosePort;
|
||||
@@ -130,6 +131,7 @@ public:
|
||||
deps_.action = &action_;
|
||||
deps_.costmap_status = &costmap_status_;
|
||||
deps_.path_check = &path_check_;
|
||||
deps_.environment = &environment_;
|
||||
|
||||
std::string error;
|
||||
EXPECT_TRUE(loop_.configure(config, deps_, error)) << error;
|
||||
@@ -179,6 +181,7 @@ public:
|
||||
FakeActionPort action_;
|
||||
FakeCostmapStatusPort costmap_status_;
|
||||
FakePathCheckPort path_check_;
|
||||
FakeEnvironmentPort environment_;
|
||||
ControlLoopDeps deps_;
|
||||
|
||||
private:
|
||||
@@ -1886,6 +1889,120 @@ TEST(WalkingSkeletonCollisionGuard, HeldByTheGuardAfterTheRouteIsSpentEscalatesT
|
||||
EXPECT_TRUE(escalated) << "guard held the robot but nothing escalated";
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
/// @brief P4 (5/9): guard cũ tắt hẳn, kContact của EnvMonitor phanh thật và giơ cờ collision.
|
||||
ControlLoopConfig envContactConfig()
|
||||
{
|
||||
ControlLoopConfig config = monitorConfig();
|
||||
config.path_monitor.enabled = false; // guard cũ tắt hẳn: chỉ EnvMonitor phanh
|
||||
config.env_monitor.enabled = true;
|
||||
config.env_monitor.stall.action = "log";
|
||||
config.env_monitor.unreachable.action = "log";
|
||||
config.env_monitor.contact.action = "recover";
|
||||
config.env_monitor.contact.apply_command = true;
|
||||
config.env_monitor.contact.confirm_cycles = 2;
|
||||
config.env_monitor.contact.trigger_static_time = 0.3; // [s] = 6 cycle, đủ ngắn cho test
|
||||
config.env_monitor.contact.clean_start_margin = 0.2; // [m]
|
||||
config.env_sampling.check_period = 0.01; // [s] < chu kỳ 0.05 -> tick nặng mỗi cycle
|
||||
return config;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(WalkingSkeletonEnvContact, BrakesAfterConfirmThenRaisesCollisionAfterStaticTime)
|
||||
{
|
||||
Fixture fixture(envContactConfig());
|
||||
driveToControlling(fixture);
|
||||
|
||||
// Tiếp xúc 0.10 m trên quỹ đạo, trong clean_start_margin: cycle 1 đếm, cycle 2 phanh -> lệnh 0.
|
||||
fixture.environment_.setContact(true, 0.10);
|
||||
fixture.stepOnce();
|
||||
fixture.stepOnce();
|
||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
|
||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().angular.z, 0.0);
|
||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling) << "phanh nhưng chưa giơ cờ";
|
||||
|
||||
// Kề mũi đứng yên đủ trigger_static_time -> cờ collision -> route collision.
|
||||
int held = 0;
|
||||
for (int i = 0; i < 12 && fixture.loop_.state() == NavigationState::kControlling; ++i)
|
||||
{
|
||||
fixture.stepOnce();
|
||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0) << "cycle " << i;
|
||||
++held;
|
||||
}
|
||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
||||
EXPECT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
||||
EXPECT_GE(held, 4) << "cờ phải chờ đủ trigger_static_time, không nổ cùng nhịp với phanh";
|
||||
}
|
||||
|
||||
TEST(WalkingSkeletonEnvContact, HeldWithTheRouteSpentEscalatesToControllingFailed)
|
||||
{
|
||||
// Cùng chuỗi bắt buộc như guard cũ: cạn route collision -> khoá -> brake giữ 0 nhưng controller
|
||||
// phải bị báo no_valid_command -> controller_patience -> controlling_failed. Không có bước này
|
||||
// robot đứng chết vô hạn với controller "ổn".
|
||||
Fixture fixture(envContactConfig());
|
||||
driveToControlling(fixture);
|
||||
fixture.recovery_.setScript({ RecoveryScript::kFailed });
|
||||
|
||||
fixture.environment_.setContact(true, 0.10);
|
||||
for (int i = 0; i < 12 && fixture.loop_.state() == NavigationState::kControlling; ++i)
|
||||
{
|
||||
fixture.stepOnce();
|
||||
}
|
||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
|
||||
ASSERT_EQ(fixture.recovery_.lastTrigger(), RecoveryTrigger::kCollision);
|
||||
|
||||
for (int i = 0; i < 4 && fixture.loop_.state() == NavigationState::kRecovering; ++i)
|
||||
{
|
||||
fixture.stepOnce();
|
||||
}
|
||||
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling)
|
||||
<< "detour thất bại phải quay về kControlling với route collision đã cạn";
|
||||
|
||||
// onRecoveryFinished xoá chuỗi phanh: tối đa confirm_cycles cycle lệnh có thể khác 0 rồi phanh lại.
|
||||
bool escalated = false;
|
||||
int nonzero_after_settle = 0;
|
||||
for (int i = 0; i < 60 && !escalated; ++i)
|
||||
{
|
||||
fixture.stepOnce();
|
||||
if (i >= 3 && fixture.loop_.lastCommand().linear.x != 0.0)
|
||||
{
|
||||
++nonzero_after_settle;
|
||||
}
|
||||
escalated = fixture.loop_.state() == NavigationState::kRecovering &&
|
||||
fixture.recovery_.lastTrigger() == RecoveryTrigger::kControllingFailed;
|
||||
}
|
||||
EXPECT_TRUE(escalated) << "EnvMonitor held the robot but nothing escalated";
|
||||
EXPECT_EQ(nonzero_after_settle, 0) << "brake must hold once re-confirmed";
|
||||
}
|
||||
|
||||
TEST(WalkingSkeletonEnvContact, ContactBeyondTheStoppingDistanceCapsSpeedWithoutBraking)
|
||||
{
|
||||
Fixture fixture(envContactConfig());
|
||||
driveToControlling(fixture);
|
||||
const double v_free = fixture.loop_.lastCommand().linear.x;
|
||||
ASSERT_GT(v_free, 0.27);
|
||||
|
||||
// 2.0 m phía trước: bao sqrt(2·0.35·1.7) = 1.09 m/s > lệnh -> không đổi, không phanh.
|
||||
fixture.environment_.setContact(true, 2.0);
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
fixture.stepOnce();
|
||||
}
|
||||
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, v_free);
|
||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
||||
|
||||
// 0.40 m: bao sqrt(2·0.35·(0.40 − margin 0.2 − dư 0.1)) = 0.265 m/s. Lệnh bị kẹp xuống đúng bao và GIỮ ở đó:
|
||||
// quãng phanh ở tốc độ đã kẹp = 0.10 m -> còn 0.30 m > margin -> không phải "sắp chạm", không phanh, không cờ.
|
||||
fixture.environment_.setContact(true, 0.40);
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
fixture.stepOnce();
|
||||
EXPECT_NEAR(fixture.loop_.lastCommand().linear.x, std::sqrt(2.0 * 0.35 * 0.10), 1e-3) << "cycle " << i;
|
||||
}
|
||||
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
|
||||
}
|
||||
|
||||
TEST(WalkingSkeletonCollisionGuard, BodyComesFromTheCostmapFootprintWhenOffsetsAreZero)
|
||||
{
|
||||
// Ba offset = 0: thân lấy từ footprint costmap qua cổng. Có footprint -> guard dò và dừng như khai
|
||||
|
||||
Reference in New Issue
Block a user