feat: lifecycle contract — cancel/pause must reach true quiescence before reporting done

Root cause (from a C++ host log, 2026-09-11): cancel during PLANNING only set
a discard flag on the planner thread, which ran to allocated_time before
CANCELLED could be reported; a queued next mission landed on a still-busy
planner and timed out too. Same class of bug existed for pause, recovery, and
implicitly for a legitimate ABORTED read while a component was still
unwinding. Full audit, decisions, and phased design in
docs/PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md; old-vs-new behaviour per state in
docs/LIFECYCLE_BEHAVIOUR_MATRIX.md.

Runtime quiescence, not just "command sent":
- kPausing (new state) mirrors kCancelling: PAUSED/CANCELLED is only entered
  once planner and recovery are idle, the action reports terminal, and the
  robot is stopped per odometry (robot_stopped, stopped_measured_linear/
  angular_eps) — not merely "the command we sent was zero". settle_timeout
  bounds the wait; exceeding it still finishes but logs an ungated error
  naming which component was still busy, so a plugin that doesn't honour
  cancel/pause is visible instead of silently swallowed.
- PlannerRunner::running_planner_ (new): the instance the planner thread is
  actually inside makePlan() with, tracked separately from active_ — active_
  can already point at a different instance after swapPlanner() while the
  old attempt is still running. cancelPlan(), swapPlanner() (when a job is
  running_ or only pending_ and not yet picked up), and the destructor all
  preempt through this pointer now; the old code preempted through active_
  and could silently preempt the wrong (idle) instance after a profile swap.
- ComponentStatusBoard + per-component ComponentPhase (idle/busy/stopping/
  paused/hung), read by PlannerRunner, RecoveryRunner, ActionRunner and
  ControlLoop instead of ad hoc bools — this is what makes "did everything
  actually stop" answerable at all, and what the new Watchdog thread reads
  to flag a component busy-without-progress as hung (logged once at the
  edge) without the control loop needing to detect its own stall.
- Resume: PLANNING replans from the current pose. CONTROLLING re-issues
  setPlan() with the plan already in hand (decided: setPlan again, not a
  fresh plan) — the local planner needs nothing else since it already runs
  once per control cycle; a fresh plan on resume was considered and
  rejected as unnecessary work for a plan that's still valid.
- Command-to-zero on cancel/pause stays immediate (no deceleration ramp) —
  existing VelocityArbiter behaviour, left as is per a scoped decision.

Depends on: Test/sbpl (bd89924), pnkx_nav_core (2b0993b, robot_nav_core::
Preemptable + component_status_probe.h), Test/sbpl_lattice_planner (7456e99),
Test/recovery_core (254c7fc, onCancelRequested + ABI guard), Test/
sbpl_recovery (ea79686, worker-thread cancel), Test/action_core (669c3f7,
pause/resume + kCancelled), Test/dock_pattern_action (cc1ea74).

Verified: 13 gtest binaries / 528 tests, 0 failures. Runtime on the dev
machine (Gazebo + real ROS host + VDA5050 over MQTT), 6 scenarios — cancel
during PLANNING, pause/resume during PLANNING and CONTROLLING (incl.
submit-while-still-winding-down), pause-then-cancel during PLANNING and
CONTROLLING: no ABORTED/RECOVERING in any run, preempt lands within
hundredths of a millisecond of entering CANCELLING/PAUSING, PAUSED/CANCELLED
only reported once the robot is actually stopped per odometry. Full numbers
in the plan doc section 6.

Excluded from this commit (pre-existing, unrelated):
config/runtime/recovery_behaviors_params.yaml (allocated_time 1.0->2.0,
SBPLDetourPlanner tuning predating this work).

Not yet done (see plan doc "Trạng thái"): config tuning (phase 0, pending
per a scoping decision); runtime scenarios for cancel mid-detour and a 30 s
pause mid-action; commit to rb_ws is a separate step, not done here.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 22:07:34 +07:00
parent 7d00c5ab66
commit 8bc4895a60
41 changed files with 4751 additions and 95 deletions

View File

@@ -130,6 +130,10 @@ public:
deps_.action = &action_;
deps_.costmap_status = &costmap_status_;
deps_.environment = &environment_;
deps_.components = &components_;
planner_.attachStatusBoard(&components_);
recovery_.attachStatusBoard(&components_);
action_.attachStatusBoard(&components_);
std::string error;
EXPECT_TRUE(loop_.configure(config, deps_, error)) << error;
@@ -179,6 +183,7 @@ public:
FakeActionPort action_;
FakeCostmapStatusPort costmap_status_;
FakeEnvironmentPort environment_;
move_base2::ComponentStatusBoard components_;
ControlLoopDeps deps_;
private:
@@ -814,8 +819,11 @@ TEST(ControlLoop, PauseAndResumeMidControlling)
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPaused);
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPausing);
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPaused);
// Dừng lâu rồi tiếp tục: không được vì thế mà rơi vào recovery.
fixture.clock_.advance(30.0);
@@ -826,6 +834,433 @@ TEST(ControlLoop, PauseAndResumeMidControlling)
EXPECT_GT(fixture.loop_.lastCommand().linear.x, 0.0);
}
TEST(ControlLoop, ResumeMidControllingReappliesThePlanToTheController)
{
// Local planner giữ warm-start/slew theo thời gian; sau 30 s đứng yên phải nạp lại plan để
// plugin reset theo pose hiện tại (2026-09-11).
Fixture fixture;
fixture.controller_.setScript({ControllerScript::kOk});
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
const std::size_t set_plan_before = fixture.controller_.setPlanCount();
ASSERT_EQ(set_plan_before, 1u);
fixture.loop_.requestPause();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kPaused);
EXPECT_EQ(fixture.controller_.setPlanCount(), set_plan_before) << "pause must not touch the plan";
fixture.clock_.advance(30.0);
fixture.loop_.requestResume();
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kControlling);
EXPECT_EQ(fixture.controller_.setPlanCount(), set_plan_before + 1)
<< "resume from CONTROLLING must call setPlan() again so the plugin resets its warm start";
EXPECT_GT(fixture.loop_.lastCommand().linear.x, 0.0);
}
// ================================================================================================
// Cancel phải dừng THẬT (2026-09-11): CANCELLED chỉ khi thread planner rảnh và odom về 0.
// Tái hiện log: cancel lúc SBPL đang chạy → host thấy CANCELLED sau 33 ms → gửi goal kế →
// startPlan() bị từ chối vì thread còn bận → bị coi là hỏng → recovery → ABORTED.
// ================================================================================================
namespace
{
/// Fixture với planner "chậm" (10 cycle) và KHÔNG preempt được — đúng SBPL trước pha 2.
ControlLoopConfig busyPlannerConfig()
{
ControlLoopConfig config = baseConfig();
config.state_machine.planner_patience = 5.0; // [s] đủ dài để kBusy không bị patience cắt
config.state_machine.max_planning_retries = 0; // như config trên máy chạy log
config.state_machine.settle_timeout = 2.0; // [s]
return config;
}
constexpr std::size_t kSlowPlannerCycles = 10;
/// Đưa fixture tới PLANNING với một lượt lập plan đang bay, chưa có kết quả.
void driveToPlanningInFlight(Fixture& fixture, std::uint64_t mission_id)
{
fixture.planner_.setLatencyCycles(kSlowPlannerCycles);
fixture.planner_.setCancelKeepsBusy(true);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, mission_id), reason)) << reason;
fixture.loop_.step(); // accept → PLANNING, startPlan()
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step(); // pollPlan: chưa có
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kPlanning);
ASSERT_TRUE(fixture.planner_.isPlanning());
}
} // namespace
TEST(ControlLoop, CancelWhilePlannerStaysBusyHoldsCancellingUntilTheThreadIsIdle)
{
Fixture fixture(busyPlannerConfig());
driveToPlanningInFlight(fixture, 7);
// reset() lúc configure và accept_request đều gọi cancelPlan() (dọn lượt cũ) — đếm tương đối.
const std::size_t cancels_before = fixture.planner_.cancelCount();
fixture.loop_.requestCancel();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kCancelling);
EXPECT_EQ(fixture.planner_.cancelCount(), cancels_before + 1);
EXPECT_TRUE(fixture.planner_.isPlanning()) << "the fake models a planner that cannot be preempted";
// Chừng nào thread còn bận: giữ CANCELLING, lệnh = 0, KHÔNG báo kết quả. Cycle mà thread rảnh
// (pollPlan thấy lượt cũ xong) cũng chính là cycle CANCELLED — không chậm thêm vòng nào.
std::size_t cycles_held = 0;
for (; cycles_held < 50; ++cycles_held)
{
const bool busy_before = fixture.planner_.isPlanning();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
if (fixture.loop_.state() == NavigationState::kCancelled)
{
EXPECT_TRUE(busy_before) << "must not finish before the very cycle the thread frees";
EXPECT_FALSE(fixture.planner_.isPlanning());
break;
}
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelling) << "cycle " << cycles_held;
EXPECT_TRUE(fixture.planner_.isPlanning()) << "held CANCELLING although the thread is idle";
EXPECT_EQ(fixture.mission_.reportCountFor(7), 0u) << "CANCELLED reported while planner busy";
}
EXPECT_GE(cycles_held, 3u) << "the fake planner should have stayed busy for several cycles";
EXPECT_LT(cycles_held, 50u) << "never reached CANCELLED";
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelled);
EXPECT_STREQ(fixture.loop_.lastOutcome(), "CANCELLED");
EXPECT_EQ(fixture.mission_.reportCountFor(7), 1u);
}
TEST(ControlLoop, GoalSentRightAfterCancelWaitsForTheBusyPlannerInsteadOfFailing)
{
// Đúng chuỗi sự kiện trong log: cancel mission 3, mission 4 tới 0.2 s sau. Không được có
// RECOVERING/ABORTED; mission 4 phải tới CONTROLLING sau khi thread rảnh.
Fixture fixture(busyPlannerConfig());
driveToPlanningInFlight(fixture, 3);
fixture.loop_.requestCancel();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kCancelling);
// Host không đợi PREEMPTED (hành vi cũ của khung chương trình): gửi goal kế ngay.
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(5.0, 4), reason)) << reason;
const std::size_t make_plan_before = fixture.planner_.makePlanCount();
bool saw_recovering = false;
bool saw_aborted = false;
bool reached_controlling = false;
for (int i = 0; i < 60 && !reached_controlling; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
const NavigationState s = fixture.loop_.state();
saw_recovering = saw_recovering || s == NavigationState::kRecovering;
saw_aborted = saw_aborted || s == NavigationState::kAborted;
reached_controlling = s == NavigationState::kControlling;
}
EXPECT_FALSE(saw_recovering) << "a busy planner thread was treated as a failed planning attempt";
EXPECT_FALSE(saw_aborted);
EXPECT_TRUE(reached_controlling);
EXPECT_EQ(fixture.planner_.makePlanCount(), make_plan_before + 1)
<< "exactly one new planning attempt once the thread was free";
EXPECT_EQ(fixture.mission_.reportCountFor(3), 1u) << "mission 3 reported once (CANCELLED)";
EXPECT_EQ(fixture.mission_.reportCountFor(4), 0u) << "mission 4 is still running";
}
TEST(ControlLoop, PauseWhilePlanningThenResumeWaitsForTheBusyPlanner)
{
// pause lúc PLANNING đi cùng đường cancelPlan(); resume → PLANNING → startPlan() bị từ chối →
// phải là kBusy, không phải kFailed.
Fixture fixture(busyPlannerConfig());
driveToPlanningInFlight(fixture, 8);
fixture.loop_.requestPause();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kPausing)
<< "planner thread still busy: PAUSED must not be reported yet";
ASSERT_TRUE(fixture.planner_.isPlanning());
fixture.loop_.requestResume();
bool saw_recovering = false;
bool reached_controlling = false;
for (int i = 0; i < 60 && !reached_controlling; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
saw_recovering = saw_recovering || fixture.loop_.state() == NavigationState::kRecovering;
reached_controlling = fixture.loop_.state() == NavigationState::kControlling;
}
EXPECT_FALSE(saw_recovering);
EXPECT_TRUE(reached_controlling);
EXPECT_EQ(fixture.mission_.reportCountFor(8), 0u);
}
TEST(ControlLoop, NewGoalPreemptingABusyPlannerWaitsInsteadOfFailing)
{
// MissionManager::submit() tự huỷ mission đang chạy — không cần cancel vẫn dính cùng đường.
Fixture fixture(busyPlannerConfig());
driveToPlanningInFlight(fixture, 11);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(6.0, 12), reason)) << reason;
bool saw_recovering = false;
bool reached_controlling = false;
for (int i = 0; i < 60 && !reached_controlling; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
saw_recovering = saw_recovering || fixture.loop_.state() == NavigationState::kRecovering;
reached_controlling = fixture.loop_.state() == NavigationState::kControlling;
}
EXPECT_FALSE(saw_recovering);
EXPECT_TRUE(reached_controlling);
EXPECT_EQ(fixture.mission_.reportCountFor(11), 1u) << "mission 11 reported once (PREEMPTED)";
EXPECT_EQ(fixture.mission_.reportCountFor(12), 0u);
}
TEST(ControlLoop, BusyPlannerStillCountsAgainstPlannerPatience)
{
// kBusy không phải vé miễn phí: plugin treo thật thì planner_patience vẫn phải leo thang.
ControlLoopConfig config = busyPlannerConfig();
config.state_machine.planner_patience = 0.2; // [s] = 4 cycle
Fixture fixture(config);
fixture.planner_.setLatencyCycles(100); // treo "vĩnh viễn" so với patience
fixture.planner_.setCancelKeepsBusy(true);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 20), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
// Goal kế chen ngang (preempt) trong lúc thread treo: chờ bằng kBusy tối đa planner_patience rồi
// mới recovery. Dùng preempt thay vì cancel để không phải đợi settle_timeout ở đây.
ASSERT_TRUE(fixture.loop_.submit(makeRequest(5.0, 21), reason)) << reason;
bool saw_recovering = false;
for (int i = 0; i < 30 && !saw_recovering; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
saw_recovering = fixture.loop_.state() == NavigationState::kRecovering;
}
EXPECT_TRUE(saw_recovering) << "a planner that never returns must still hit planner_patience";
}
TEST(ControlLoop, CancelSettleTimeoutFinishesCancelledEvenIfThePlannerNeverFrees)
{
ControlLoopConfig config = busyPlannerConfig();
config.state_machine.settle_timeout = 0.2; // [s] = 4 cycle
Fixture fixture(config);
fixture.planner_.setLatencyCycles(1000);
fixture.planner_.setCancelKeepsBusy(true);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 30), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.requestCancel();
std::size_t cycles = 0;
bool running = true;
while (running && cycles < 20)
{
running = fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
++cycles;
}
EXPECT_FALSE(running);
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelled);
EXPECT_TRUE(fixture.planner_.isPlanning()) << "the thread is still busy — settle timeout fired";
EXPECT_GE(cycles, 4u);
EXPECT_LE(cycles, 7u) << "CANCELLED must arrive right after settle_timeout";
EXPECT_EQ(fixture.mission_.reportCountFor(30), 1u);
}
TEST(ControlLoop, CancellingWaitsForMeasuredVelocityToReadZero)
{
// Lệnh về 0 ngay cycle đầu, nhưng odom còn 0.3 m/s (driver đang giảm tốc) → chưa CANCELLED.
Fixture fixture;
fixture.controller_.setScript({ControllerScript::kOk});
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 40), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
fixture.controller_.setOdometry(0.3);
fixture.loop_.requestCancel();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kCancelling);
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
for (int i = 0; i < 5; ++i)
{
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelling) << "cycle " << i;
}
EXPECT_EQ(fixture.mission_.reportCountFor(40), 0u);
fixture.controller_.setOdometry(0.0);
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelled);
EXPECT_EQ(fixture.mission_.reportCountFor(40), 1u);
}
TEST(ControlLoop, PauseWhilePlannerBusyHoldsPausingUntilTheThreadIsIdle)
{
// PAUSED nói thật (2026-09-11): pause nhận trong 1 cycle (lệnh 0), nhưng PAUSED chỉ khi thread
// planner đã nhả — cùng cơ chế với CANCELLING.
Fixture fixture(busyPlannerConfig());
driveToPlanningInFlight(fixture, 50);
fixture.loop_.requestPause();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kPausing);
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
EXPECT_EQ(fixture.components_.phase(move_base2::Component::kGlobalPlanner),
move_base2::ComponentPhase::kStopping)
<< "the fake planner reports 'stopping' after a cancel it cannot honour";
std::size_t held = 0;
for (; held < 50; ++held)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
if (fixture.loop_.state() == NavigationState::kPaused)
{
break;
}
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPausing) << "cycle " << held;
EXPECT_TRUE(fixture.planner_.isPlanning());
}
EXPECT_GE(held, 3u);
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPaused);
EXPECT_FALSE(fixture.planner_.isPlanning());
EXPECT_EQ(fixture.components_.phase(move_base2::Component::kGlobalPlanner),
move_base2::ComponentPhase::kIdle);
}
TEST(ControlLoop, CancellingWaitsForARecoveryWorkerReportedThroughTheStatusBoard)
{
// Recovery có worker thread (detour, pha 2): cancel() không kết thúc được ngay → hàng kRecovery
// giữ 'stopping' → CANCELLING phải chờ, dù cổng planner đã rảnh và lệnh đã 0.
Fixture fixture;
fixture.planner_.setScript({PlannerScript::kFail});
fixture.recovery_.setScript({RecoveryScript::kRunning});
fixture.recovery_.setCancelKeepsBusy(true);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 60), reason)) << reason;
for (int i = 0; i < 40 && fixture.loop_.state() != NavigationState::kRecovering; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
}
ASSERT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
fixture.loop_.requestCancel();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kCancelling);
ASSERT_TRUE(fixture.recovery_.stopping());
for (int i = 0; i < 5; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelling) << "cycle " << i;
}
EXPECT_EQ(fixture.mission_.reportCountFor(60), 0u);
fixture.recovery_.settleCancel(); // worker giả đã thoát
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelled);
EXPECT_EQ(fixture.mission_.reportCountFor(60), 1u);
}
TEST(ControlLoop, StatusBoardTracksLocalPlannerAndControlLoopHeartbeat)
{
Fixture fixture;
fixture.controller_.setScript({ControllerScript::kOk});
const std::uint64_t before =
fixture.components_.at(move_base2::Component::kControlLoop).progress_ticks.load();
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
// Heartbeat nhích mỗi step(); local planner về idle sau mỗi lượt gọi (không để "bận" treo).
EXPECT_GE(fixture.components_.at(move_base2::Component::kControlLoop).progress_ticks.load(),
before + 2);
EXPECT_EQ(fixture.components_.phase(move_base2::Component::kLocalPlanner),
move_base2::ComponentPhase::kIdle);
EXPECT_GT(fixture.components_.at(move_base2::Component::kLocalPlanner).progress_ticks.load(), 0u)
<< "computeVelocityCommands must have been bracketed busy/idle at least once";
}
TEST(ControlLoop, MeasuredVelocityIsIgnoredWhenTheEpsSwitchesAreZero)
{
ControlLoopConfig config = baseConfig();
config.state_machine.stopped_measured_linear_eps = 0.0;
config.state_machine.stopped_measured_angular_eps = 0.0;
Fixture fixture(config);
fixture.controller_.setScript({ControllerScript::kOk});
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 41), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
fixture.controller_.setOdometry(0.3); // odom nói còn chạy — nhưng công tắc tắt
fixture.loop_.requestCancel();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelled)
<< "with eps = 0 'stopped' is judged from the issued command, as before 2026-09-11";
}
// ================================================================================================
// Nhiều chặng liên tiếp
// ================================================================================================
@@ -926,6 +1361,77 @@ TEST(ControlLoopActions, ActionFailureAbortsAndReportsOnce)
EXPECT_EQ(fixture.recovery_.startCount(), 0u) << "a failed action must not drag recovery in";
}
TEST(ControlLoopActions, PauseAndResumeAreForwardedToTheActionPort)
{
// Pha 3 (2026-09-11): handler có timeout riêng phải biết pause để dịch mốc; không báo thì 20 s
// đứng chờ bị tính vào timeout dò 15 s → kFailed → ABORTED oan ngay sau resume.
Fixture fixture;
fixture.action_.setScript({ActionScript::kRunning});
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeActionOnlyRequest(1, 14), reason)) << reason;
fixture.stepOnce();
fixture.stepOnce();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kExecutingActions);
const std::size_t updates_before_pause = fixture.action_.updateCount();
fixture.loop_.requestPause();
fixture.stepOnce();
EXPECT_EQ(fixture.action_.pauseCount(), 1u);
EXPECT_EQ(fixture.action_.cancelCount(), 0u) << "pause must not cancel the action";
fixture.stepOnce(); // hàng kAction = paused → quiescent
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPaused);
EXPECT_EQ(fixture.action_.updateCount(), updates_before_pause) << "no ticks while paused";
fixture.clock_.advance(30.0);
fixture.loop_.requestResume();
fixture.stepOnce();
EXPECT_EQ(fixture.action_.resumeCount(), 1u);
EXPECT_EQ(fixture.loop_.state(), NavigationState::kExecutingActions);
EXPECT_GT(fixture.action_.updateCount(), updates_before_pause) << "ticking resumed";
EXPECT_EQ(fixture.action_.startCount(), 1u) << "the action must not be restarted";
}
TEST(ControlLoopActions, CancelWaitsForADeviceHandlerToReportCancelled)
{
// Handler thiết bị cần 3 tick sau cancel() để về vị trí an toàn: CANCELLING tick tiếp, không
// cấp vận tốc, và CANCELLED chỉ được báo sau tick kCancelled.
Fixture fixture;
fixture.action_.setScript({ActionScript::kRunning});
fixture.action_.setCancelNeedsTicks(3);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeActionOnlyRequest(1, 15), reason)) << reason;
fixture.stepOnce();
fixture.stepOnce();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kExecutingActions);
fixture.loop_.requestCancel();
fixture.stepOnce();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kCancelling);
EXPECT_EQ(fixture.action_.cancelCount(), 1u);
EXPECT_EQ(fixture.components_.phase(move_base2::Component::kAction),
move_base2::ComponentPhase::kStopping);
std::size_t held = 0;
for (; held < 10; ++held)
{
if (fixture.loop_.state() == NavigationState::kCancelled)
{
break;
}
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelling) << "cycle " << held;
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
EXPECT_EQ(fixture.mission_.reportCountFor(15), 0u) << "CANCELLED reported before the device was safe";
fixture.stepOnce();
}
EXPECT_GE(held, 3u) << "the handler asked for 3 ticks after cancel";
EXPECT_EQ(fixture.loop_.state(), NavigationState::kCancelled);
EXPECT_EQ(fixture.mission_.reportCountFor(15), 1u);
EXPECT_EQ(fixture.components_.phase(move_base2::Component::kAction),
move_base2::ComponentPhase::kIdle);
}
TEST(ControlLoopActions, SubmitRejectsActionRequestWhenActionPortMissing)
{
Fixture fixture;