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

@@ -240,6 +240,39 @@ TEST(StateMachineConfig, RejectsResolvedRouteWithAnOutOfRangeBehavior)
EXPECT_NE(error.find("loaded behavior"), std::string::npos);
}
TEST(StateMachineConfig, RejectsNegativeCancelSettleTimeout)
{
StateMachineConfig config = baseConfig();
config.settle_timeout = -1.0;
std::string error;
EXPECT_FALSE(config.validate(error));
EXPECT_NE(error.find("settle_timeout"), std::string::npos);
}
TEST(StateMachineConfig, RejectsNegativeStoppedMeasuredEps)
{
StateMachineConfig linear = baseConfig();
linear.stopped_measured_linear_eps = -0.01;
std::string error;
EXPECT_FALSE(linear.validate(error));
EXPECT_NE(error.find("stopped_measured_linear_eps"), std::string::npos);
StateMachineConfig angular = baseConfig();
angular.stopped_measured_angular_eps = -0.01;
EXPECT_FALSE(angular.validate(error));
EXPECT_NE(error.find("stopped_measured_angular_eps"), std::string::npos);
}
TEST(StateMachineConfig, ZeroCancelSettleTimeoutAndZeroEpsAreValidOffSwitches)
{
StateMachineConfig config = baseConfig();
config.settle_timeout = 0.0;
config.stopped_measured_linear_eps = 0.0;
config.stopped_measured_angular_eps = 0.0;
std::string error;
EXPECT_TRUE(config.validate(error)) << error;
}
TEST(StateMachineConfig, DescribeMentionsEveryParameter)
{
const std::string text = baseConfig().describe();
@@ -249,6 +282,8 @@ TEST(StateMachineConfig, DescribeMentionsEveryParameter)
EXPECT_NE(text.find("action_patience"), std::string::npos);
EXPECT_NE(text.find("max_planning_retries"), std::string::npos);
EXPECT_NE(text.find("recovery_reset_min_travel"), std::string::npos);
EXPECT_NE(text.find("settle_timeout"), std::string::npos);
EXPECT_NE(text.find("stopped_measured_eps"), std::string::npos);
}
TEST(StateMachine, RefusesToRunBeforeConfigure)
@@ -444,7 +479,7 @@ TEST(StateMachinePlanning, PauseMovesToPausedAndResumeComesBack)
StateMachineInput pause;
pause.pause_requested = true;
EXPECT_EQ(driver.tick(pause).state, NavigationState::kPaused);
EXPECT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput resume;
resume.resume_requested = true;
@@ -903,7 +938,7 @@ TEST(StateMachineControlling, PauseThenResumeReturnsToControlling)
StateMachineInput pause;
pause.pause_requested = true;
EXPECT_EQ(driver.tick(pause).state, NavigationState::kPaused);
EXPECT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput resume;
resume.resume_requested = true;
@@ -921,7 +956,7 @@ TEST(StateMachineControlling, LongPauseDoesNotBlowControllerPatience)
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPaused);
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
driver.advance(60.0);
@@ -1108,7 +1143,7 @@ TEST(StateMachineRecovering, PauseMidRecoveryCancelsBehaviorAndResumesToPlanning
pause.pause_requested = true;
const StateMachineOutput paused = driver.tick(pause);
EXPECT_EQ(paused.state, NavigationState::kPaused);
EXPECT_EQ(paused.state, NavigationState::kPausing);
EXPECT_TRUE(paused.cancel_recovery)
<< "keeping a behavior half-finished across a long pause is unsafe";
@@ -1147,7 +1182,7 @@ TEST(StateMachinePaused, CancelFromPausedGoesToCancelling)
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPaused);
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput cancel;
cancel.cancel_requested = true;
@@ -1161,7 +1196,9 @@ TEST(StateMachinePaused, StaysPausedIndefinitelyWithoutResume)
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPaused);
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
// Cycle kế: mọi thành phần im (input mặc định) → PAUSED thật.
ASSERT_EQ(driver.idleTick().state, NavigationState::kPaused);
for (int i = 0; i < 50; ++i)
{
@@ -1194,6 +1231,430 @@ TEST(StateMachineCancelling, WaitsForRobotToStopBeforeCancelled)
EXPECT_EQ(out.outcome, NavigationOutcome::kCancelled);
}
// ================================================================================================
// kCancelling chờ QUIESCENT (2026-09-11): lệnh về 0 là điều kiện cần, chưa đủ. Host đọc
// CANCELLED/PREEMPTED là "xong rồi, gửi goal kế được" — báo trong lúc thread planner còn chạy lượt
// cũ thì goal kế bị startPlan() từ chối và bị coi là hỏng (log 2026-09-11: ABORTED sau 3 s).
// ================================================================================================
TEST(StateMachineCancelling, HoldsWhilePlannerThreadIsStillBusy)
{
Driver driver;
StateMachineInput request;
request.has_pending_request = true;
ASSERT_EQ(driver.tick(request).state, NavigationState::kPlanning);
StateMachineInput cancel;
cancel.cancel_requested = true;
cancel.planner_busy = true; // SBPL đang chạy, cancelPlan() không cắt được
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
StateMachineInput still_busy;
still_busy.robot_stopped = true;
still_busy.planner_busy = true;
for (int i = 0; i < 10; ++i)
{
driver.advance(0.05);
const StateMachineOutput out = driver.tick(still_busy);
EXPECT_EQ(out.state, NavigationState::kCancelling) << "left CANCELLING at cycle " << i;
EXPECT_FALSE(out.report_outcome);
EXPECT_EQ(out.velocity_source, VelocitySource::kNone);
EXPECT_FALSE(out.start_planner);
}
StateMachineInput idle;
idle.robot_stopped = true;
idle.planner_busy = false;
driver.advance(0.05);
const StateMachineOutput out = driver.tick(idle);
EXPECT_EQ(out.state, NavigationState::kCancelled);
EXPECT_TRUE(out.report_outcome);
EXPECT_EQ(out.outcome, NavigationOutcome::kCancelled);
EXPECT_FALSE(out.settle_timed_out);
}
TEST(StateMachineCancelling, GivesUpAfterSettleTimeoutAndFlagsIt)
{
StateMachineConfig config = baseConfig();
config.settle_timeout = 0.5; // [s]
Driver driver(config);
driver.driveToControlling();
StateMachineInput cancel;
cancel.cancel_requested = true;
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
StateMachineInput busy;
busy.robot_stopped = true;
busy.planner_busy = true;
// 0.45 s: chưa quá trần.
driver.advance(0.45);
EXPECT_EQ(driver.tick(busy).state, NavigationState::kCancelling);
EXPECT_FALSE(driver.last().settle_timed_out);
// 0.55 s: quá trần → CANCELLED nhưng cờ lỗi bật đúng cycle báo kết quả.
driver.advance(0.10);
const StateMachineOutput out = driver.tick(busy);
EXPECT_EQ(out.state, NavigationState::kCancelled);
EXPECT_TRUE(out.report_outcome);
EXPECT_EQ(out.outcome, NavigationOutcome::kCancelled);
EXPECT_TRUE(out.settle_timed_out);
// Cờ chỉ sống một cycle: cycle kế về kIdle không mang nó theo.
EXPECT_FALSE(driver.idleTick().settle_timed_out);
}
TEST(StateMachineCancelling, HoldsWhileOdometrySaysTheRobotIsStillMoving)
{
Driver driver;
driver.driveToControlling();
StateMachineInput cancel;
cancel.cancel_requested = true;
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
// Lệnh đã về 0 (bên gọi tính vào robot_stopped) nhưng odom vẫn đọc 0.3 m/s → chưa xong.
StateMachineInput rolling;
rolling.robot_stopped = false;
rolling.planner_busy = false;
driver.advance(0.05);
EXPECT_EQ(driver.tick(rolling).state, NavigationState::kCancelling);
StateMachineInput stopped;
stopped.robot_stopped = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(stopped).state, NavigationState::kCancelled);
}
TEST(StateMachineCancelling, KeepsTickingTheActionUntilItReportsTerminal)
{
// Handler thiết bị cần vài tick sau cancel() để đưa thiết bị về vị trí an toàn. Trong lúc đó
// state machine tick tiếp nhưng KHÔNG cấp vận tốc — bất biến 3 giữ nguyên.
Driver driver;
StateMachineInput request;
request.has_pending_request = true;
request.pending_request_has_goal = false;
request.pending_request_action_count = 1;
ASSERT_EQ(driver.tick(request).state, NavigationState::kExecutingActions);
StateMachineInput cancel;
cancel.cancel_requested = true;
cancel.action_terminal = false;
const StateMachineOutput entering = driver.tick(cancel);
ASSERT_EQ(entering.state, NavigationState::kCancelling);
EXPECT_TRUE(entering.cancel_action);
StateMachineInput settling;
settling.robot_stopped = true;
settling.planner_busy = false;
settling.action_terminal = false;
for (int i = 0; i < 3; ++i)
{
driver.advance(0.05);
const StateMachineOutput out = driver.tick(settling);
EXPECT_EQ(out.state, NavigationState::kCancelling);
EXPECT_TRUE(out.tick_action) << "cycle " << i;
EXPECT_FALSE(out.run_controller);
EXPECT_FALSE(out.tick_recovery);
EXPECT_EQ(out.velocity_source, VelocitySource::kNone);
}
StateMachineInput terminal = settling;
terminal.action_terminal = true;
driver.advance(0.05);
const StateMachineOutput out = driver.tick(terminal);
EXPECT_EQ(out.state, NavigationState::kCancelled);
EXPECT_FALSE(out.tick_action);
EXPECT_TRUE(out.report_outcome);
}
TEST(StateMachineCancelling, ZeroSettleTimeoutKeepsTheLegacyBehaviour)
{
// Tắt = hành vi trước 2026-09-11: chỉ chờ lệnh về 0, mặc kệ planner còn bận.
StateMachineConfig config = baseConfig();
config.settle_timeout = 0.0;
Driver driver(config);
driver.driveToControlling();
StateMachineInput cancel;
cancel.cancel_requested = true;
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
StateMachineInput stopped_but_busy;
stopped_but_busy.robot_stopped = true;
stopped_but_busy.planner_busy = true;
driver.advance(0.05);
const StateMachineOutput out = driver.tick(stopped_but_busy);
EXPECT_EQ(out.state, NavigationState::kCancelled);
EXPECT_FALSE(out.settle_timed_out);
}
TEST(StateMachineCancelling, CancelFromPausedAlsoWaitsForThePlannerThread)
{
// Pause lúc PLANNING đã cancelPlan() nhưng thread có thể còn bận; cancel từ PAUSED phải chờ nốt.
Driver driver;
StateMachineInput request;
request.has_pending_request = true;
ASSERT_EQ(driver.tick(request).state, NavigationState::kPlanning);
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput cancel;
cancel.cancel_requested = true;
cancel.planner_busy = true;
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
StateMachineInput busy;
busy.robot_stopped = true;
busy.planner_busy = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(busy).state, NavigationState::kCancelling);
StateMachineInput idle = busy;
idle.planner_busy = false;
driver.advance(0.05);
EXPECT_EQ(driver.tick(idle).state, NavigationState::kCancelled);
}
TEST(StateMachinePaused, ResumeFromControllingReappliesThePlanToTheController)
{
// Local planner giữ warm-start/slew theo thời gian; sau một quãng đứng yên phải nạp lại plan để
// plugin reset theo pose hiện tại. Resume từ CONTROLLING vì vậy mang apply_plan (2026-09-11).
Driver driver;
driver.driveToControlling();
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
driver.advance(30.0);
StateMachineInput resume;
resume.resume_requested = true;
const StateMachineOutput out = driver.tick(resume);
EXPECT_EQ(out.state, NavigationState::kControlling);
EXPECT_TRUE(out.apply_plan);
EXPECT_TRUE(out.run_controller);
}
TEST(StateMachinePaused, ResumeFromPlanningDoesNotReapplyAnyPlan)
{
// Chưa từng có plan nào → không có gì để nạp lại; resume chỉ khởi động lại planner.
Driver driver;
StateMachineInput request;
request.has_pending_request = true;
ASSERT_EQ(driver.tick(request).state, NavigationState::kPlanning);
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput resume;
resume.resume_requested = true;
const StateMachineOutput out = driver.tick(resume);
EXPECT_EQ(out.state, NavigationState::kPlanning);
EXPECT_FALSE(out.apply_plan);
EXPECT_TRUE(out.start_planner);
}
// ================================================================================================
// kPausing (2026-09-11): pause nhận xong là lệnh 0 ngay, nhưng PAUSED chỉ khi mọi thành phần im —
// cùng ba điều kiện quiescent với kCancelling. Host đọc PAUSED là "dừng rồi, làm gì cũng được".
// ================================================================================================
TEST(StateMachinePausing, HoldsWhilePlannerThreadIsStillBusyThenSettles)
{
Driver driver;
StateMachineInput request;
request.has_pending_request = true;
ASSERT_EQ(driver.tick(request).state, NavigationState::kPlanning);
StateMachineInput pause;
pause.pause_requested = true;
pause.planner_busy = true;
const StateMachineOutput entering = driver.tick(pause);
ASSERT_EQ(entering.state, NavigationState::kPausing);
EXPECT_TRUE(entering.stop_planner);
EXPECT_EQ(entering.velocity_source, VelocitySource::kNone);
StateMachineInput busy;
busy.robot_stopped = true;
busy.planner_busy = true;
for (int i = 0; i < 10; ++i)
{
driver.advance(0.05);
const StateMachineOutput out = driver.tick(busy);
EXPECT_EQ(out.state, NavigationState::kPausing) << "cycle " << i;
EXPECT_EQ(out.velocity_source, VelocitySource::kNone);
EXPECT_FALSE(out.start_planner);
EXPECT_FALSE(out.settle_timed_out);
}
StateMachineInput idle;
idle.robot_stopped = true;
driver.advance(0.05);
const StateMachineOutput out = driver.tick(idle);
EXPECT_EQ(out.state, NavigationState::kPaused);
EXPECT_FALSE(out.settle_timed_out);
}
TEST(StateMachinePausing, HoldsWhileRecoveryIsStillStopping)
{
Driver driver;
driver.driveToControlling();
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput recovering;
recovering.robot_stopped = true;
recovering.recovery_busy = true; // detour worker chưa thoát
driver.advance(0.05);
EXPECT_EQ(driver.tick(recovering).state, NavigationState::kPausing);
StateMachineInput idle;
idle.robot_stopped = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(idle).state, NavigationState::kPaused);
}
TEST(StateMachinePausing, GivesUpAfterSettleTimeoutAndFlagsIt)
{
StateMachineConfig config = baseConfig();
config.settle_timeout = 0.5; // [s]
Driver driver(config);
driver.driveToControlling();
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput busy;
busy.robot_stopped = true;
busy.planner_busy = true;
driver.advance(0.45);
EXPECT_EQ(driver.tick(busy).state, NavigationState::kPausing);
driver.advance(0.10);
const StateMachineOutput out = driver.tick(busy);
EXPECT_EQ(out.state, NavigationState::kPaused);
EXPECT_TRUE(out.settle_timed_out);
EXPECT_FALSE(driver.idleTick().settle_timed_out) << "one-cycle flag";
}
TEST(StateMachinePausing, ResumeBeforeSettledReturnsToThePreviousState)
{
Driver driver;
driver.driveToControlling();
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput resume;
resume.resume_requested = true;
resume.planner_busy = true; // chưa im mà đã được bảo chạy tiếp — không phải chờ
const StateMachineOutput out = driver.tick(resume);
EXPECT_EQ(out.state, NavigationState::kControlling);
EXPECT_TRUE(out.apply_plan);
EXPECT_TRUE(out.run_controller);
}
TEST(StateMachinePausing, CancelBeforeSettledGoesToCancellingAndKeepsWaiting)
{
Driver driver;
StateMachineInput request;
request.has_pending_request = true;
ASSERT_EQ(driver.tick(request).state, NavigationState::kPlanning);
StateMachineInput pause;
pause.pause_requested = true;
pause.planner_busy = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput cancel;
cancel.cancel_requested = true;
cancel.planner_busy = true;
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
StateMachineInput busy;
busy.robot_stopped = true;
busy.planner_busy = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(busy).state, NavigationState::kCancelling);
StateMachineInput idle;
idle.robot_stopped = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(idle).state, NavigationState::kCancelled);
}
TEST(StateMachinePausing, NewGoalWhilePausingPreemptsLikePaused)
{
Driver driver;
driver.driveToControlling();
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput new_goal;
new_goal.has_pending_request = true;
const StateMachineOutput out = driver.tick(new_goal);
EXPECT_EQ(out.state, NavigationState::kPlanning);
EXPECT_TRUE(out.report_outcome);
EXPECT_EQ(out.outcome, NavigationOutcome::kPreempted);
EXPECT_TRUE(out.accept_request);
}
TEST(StateMachinePausing, ZeroSettleTimeoutSettlesOnRobotStoppedOnly)
{
StateMachineConfig config = baseConfig();
config.settle_timeout = 0.0;
Driver driver(config);
driver.driveToControlling();
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput stopped_but_busy;
stopped_but_busy.robot_stopped = true;
stopped_but_busy.planner_busy = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(stopped_but_busy).state, NavigationState::kPaused);
}
TEST(StateMachineCancelling, HoldsWhileRecoveryIsStillStopping)
{
Driver driver;
driver.driveToControlling();
StateMachineInput cancel;
cancel.cancel_requested = true;
ASSERT_EQ(driver.tick(cancel).state, NavigationState::kCancelling);
StateMachineInput recovering;
recovering.robot_stopped = true;
recovering.recovery_busy = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(recovering).state, NavigationState::kCancelling);
StateMachineInput idle;
idle.robot_stopped = true;
driver.advance(0.05);
EXPECT_EQ(driver.tick(idle).state, NavigationState::kCancelled);
}
TEST(StateMachineTerminal, OutcomeIsReportedExactlyOnce)
{
Driver driver;
@@ -1393,18 +1854,57 @@ TEST(StateMachineActions, PauseDuringActionFreezesWithoutCancelling)
StateMachineInput pause;
pause.pause_requested = true;
const StateMachineOutput paused = driver.tick(pause);
EXPECT_EQ(paused.state, NavigationState::kPaused);
EXPECT_EQ(paused.state, NavigationState::kPausing);
EXPECT_FALSE(paused.cancel_action) << "the action is not idempotent — pausing must not cancel it";
EXPECT_FALSE(paused.tick_action);
EXPECT_TRUE(paused.pause_action) << "the handler must be told, so its own timeout clock stops";
StateMachineInput resume;
resume.resume_requested = true;
const StateMachineOutput resumed = driver.tick(resume);
EXPECT_EQ(resumed.state, NavigationState::kExecutingActions);
EXPECT_TRUE(resumed.tick_action) << "resume keeps ticking the unfinished action";
EXPECT_TRUE(resumed.resume_action) << "the handler must be told the pause is over";
EXPECT_FALSE(resumed.start_action) << "an action already in progress must not be started again";
}
TEST(StateMachineActions, PausingWithAnActionWaitsForTheHandlerToAcknowledge)
{
// Handler chưa xác nhận pause (action_terminal=false) → PAUSING tick tiếp; xác nhận → PAUSED.
Driver driver;
ASSERT_EQ(driver.tick(requestWithActions(1, /*has_goal=*/false)).state,
NavigationState::kExecutingActions);
StateMachineInput pause;
pause.pause_requested = true;
pause.action_terminal = false;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
StateMachineInput not_yet;
not_yet.robot_stopped = true;
not_yet.action_terminal = false;
driver.advance(0.05);
const StateMachineOutput held = driver.tick(not_yet);
EXPECT_EQ(held.state, NavigationState::kPausing);
EXPECT_TRUE(held.tick_action);
EXPECT_EQ(held.velocity_source, VelocitySource::kNone);
StateMachineInput acked = not_yet;
acked.action_terminal = true; // hàng kAction = paused
driver.advance(0.05);
EXPECT_EQ(driver.tick(acked).state, NavigationState::kPaused);
}
TEST(StateMachineActions, CancelledFeedbackInsideExecutingActionsIsTreatedAsFailure)
{
// Lỗi thứ tự gọi (không ai cancel trong EXECUTING_ACTIONS) — không được im lặng chạy tiếp.
Driver driver;
ASSERT_EQ(driver.tick(requestWithActions(1, /*has_goal=*/false)).state,
NavigationState::kExecutingActions);
const StateMachineOutput out = driver.tick(actionFb(ActionFeedback::kCancelled));
EXPECT_EQ(out.state, NavigationState::kAborted);
}
TEST(StateMachineActions, ActionPatienceIsDisabledByDefault)
{
Driver driver;
@@ -1451,7 +1951,7 @@ TEST(StateMachineActions, PauseRearmsActionPatienceClock)
StateMachineInput pause;
pause.pause_requested = true;
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPaused);
ASSERT_EQ(driver.tick(pause).state, NavigationState::kPausing);
// Dừng lâu hơn hẳn action_patience rồi mới resume — quãng dừng không được tính vào trần.
driver.advance(30.0);
@@ -1556,8 +2056,8 @@ TEST(StateMachineInvariants, EveryStateHasANonEmptyName)
{
const NavigationState states[] = {
NavigationState::kIdle, NavigationState::kPlanning, NavigationState::kControlling,
NavigationState::kRecovering, NavigationState::kExecutingActions, NavigationState::kPaused,
NavigationState::kCancelling, NavigationState::kSucceeded, NavigationState::kAborted,
NavigationState::kRecovering, NavigationState::kExecutingActions, NavigationState::kPausing,
NavigationState::kPaused, NavigationState::kCancelling, NavigationState::kSucceeded, NavigationState::kAborted,
NavigationState::kCancelled,
};