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:
@@ -49,6 +49,18 @@ bool StateMachineConfig::validate(std::string& error) const
|
||||
"route cursor is refilled anyway, so a larger value can never take effect";
|
||||
return false;
|
||||
}
|
||||
if (settle_timeout < 0.0)
|
||||
{
|
||||
error = "settle_timeout must be >= 0 [s] (0 = finish CANCELLING/PAUSING as soon as the command "
|
||||
"is zero, without waiting for the planner thread)";
|
||||
return false;
|
||||
}
|
||||
if (stopped_measured_linear_eps < 0.0 || stopped_measured_angular_eps < 0.0)
|
||||
{
|
||||
error = "stopped_measured_linear_eps [m/s] and stopped_measured_angular_eps [rad/s] must be "
|
||||
">= 0 (0 = ignore odometry, judge 'stopped' from the issued command)";
|
||||
return false;
|
||||
}
|
||||
if (planner_patience <= 0.0 && max_planning_retries < 0)
|
||||
{
|
||||
// Từ khi lập plan chạy trên thread riêng, hai tham số này là thứ DUY NHẤT phát hiện được planner
|
||||
@@ -121,6 +133,15 @@ std::string StateMachineConfig::describe() const
|
||||
<< '\n';
|
||||
out << " max_planning_retries : " << max_planning_retries
|
||||
<< (max_planning_retries < 0 ? " (unlimited)" : "") << '\n';
|
||||
out << " settle_timeout : " << settle_timeout << " s"
|
||||
<< (settle_timeout > 0.0 ? "" : " (off — CANCELLED/PAUSED as soon as the command is zero)")
|
||||
<< '\n';
|
||||
out << " stopped_measured_eps : " << stopped_measured_linear_eps << " m/s, "
|
||||
<< stopped_measured_angular_eps << " rad/s"
|
||||
<< (stopped_measured_linear_eps > 0.0 || stopped_measured_angular_eps > 0.0
|
||||
? ""
|
||||
: " (off — 'stopped' judged from the issued command)")
|
||||
<< '\n';
|
||||
out << " recovery_enabled : " << (recovery_enabled ? "true" : "false") << '\n';
|
||||
out << " recovery_behavior_cnt : " << recovery_behavior_count << '\n';
|
||||
if (recovery_routes.empty())
|
||||
@@ -389,6 +410,45 @@ bool StateMachine::preemptIfRequested(const StateMachineInput& in, StateMachineO
|
||||
return true;
|
||||
}
|
||||
|
||||
void StateMachine::resumeFromPause(const StateMachineInput& in, StateMachineOutput& out)
|
||||
{
|
||||
// Đặt lại toàn bộ đồng hồ kiên nhẫn: một lần tạm dừng dài không được tính là "planner chậm"
|
||||
// hay "controller hỏng", nếu không thì resume xong là rơi thẳng vào recovery.
|
||||
beginPlanningCycle(in.now);
|
||||
last_valid_control_ = in.now;
|
||||
last_oscillation_reset_ = in.now;
|
||||
sensor_stale_active_ = false;
|
||||
out.reset_oscillation_origin = true;
|
||||
travel_at_last_path_reset_ = 0.0; // origin đặt lại -> mốc recovery_reset_min_travel về 0
|
||||
|
||||
if (state_before_pause_ == NavigationState::kControlling)
|
||||
{
|
||||
// Nạp lại plan cho controller trước khi bám tiếp (2026-09-11). Local planner giữ trạng
|
||||
// thái nội bộ theo thời gian (warm-start trajectory, slew, latch) từ lúc robot còn chạy;
|
||||
// sau một quãng đứng yên trạng thái đó là của một robot khác. `setPlan` là đường reset
|
||||
// duy nhất mà interface LocalPlanner có — plugin reset warm-start theo pose hiện tại ở
|
||||
// đó. Bên gọi giữ nguyên plan mới nhất (`latest_plan_`) suốt lúc tạm dừng.
|
||||
out.apply_plan = true;
|
||||
out.run_controller = true;
|
||||
enter(NavigationState::kControlling, in.now, "resume following the plan", out);
|
||||
}
|
||||
else if (state_before_pause_ == NavigationState::kExecutingActions)
|
||||
{
|
||||
// Action không bị huỷ khi tạm dừng nên không start lại — tick tiếp đúng action dở dang.
|
||||
// Mốc action_patience được gieo lại: một quãng dừng dài không được tính vào trần thời
|
||||
// gian của action, nếu không resume xong là ABORTED oan ngay lập tức.
|
||||
action_started_at_ = in.now;
|
||||
out.resume_action = true; // Handler cộng quãng dừng vào mốc timeout của nó.
|
||||
out.tick_action = true;
|
||||
enter(NavigationState::kExecutingActions, in.now, "resume running the action", out);
|
||||
}
|
||||
else
|
||||
{
|
||||
out.start_planner = true;
|
||||
enter(NavigationState::kPlanning, in.now, "resume planning", out);
|
||||
}
|
||||
}
|
||||
|
||||
StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
{
|
||||
StateMachineOutput out;
|
||||
@@ -442,7 +502,7 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
{
|
||||
out.stop_planner = true;
|
||||
state_before_pause_ = NavigationState::kPlanning;
|
||||
enter(NavigationState::kPaused, in.now, "paused while planning", out);
|
||||
enter(NavigationState::kPausing, in.now, "pausing while planning", out);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -519,7 +579,7 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
{
|
||||
out.stop_planner = true;
|
||||
state_before_pause_ = NavigationState::kControlling;
|
||||
enter(NavigationState::kPaused, in.now, "paused while following the plan", out);
|
||||
enter(NavigationState::kPausing, in.now, "pausing while following the plan", out);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -716,7 +776,7 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
// dài là không an toàn (nó dead-reckon theo thời gian). Resume sẽ lập plan lại từ đầu.
|
||||
out.cancel_recovery = true;
|
||||
state_before_pause_ = NavigationState::kPlanning;
|
||||
enter(NavigationState::kPaused, in.now, "paused while recovering", out);
|
||||
enter(NavigationState::kPausing, in.now, "pausing while recovering", out);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -856,12 +916,14 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
// dead-reckon, còn chạy lại một action thiết bị (nâng/hạ, sạc) từ đầu thì không chắc an
|
||||
// toàn — action không idempotent. Tạm dừng chỉ ngừng tick; resume tick tiếp đúng action đó.
|
||||
state_before_pause_ = NavigationState::kExecutingActions;
|
||||
enter(NavigationState::kPaused, in.now, "paused while running an action", out);
|
||||
out.pause_action = true; // Handler dịch mốc timeout của nó; không dừng thiết bị.
|
||||
enter(NavigationState::kPausing, in.now, "pausing while running an action", out);
|
||||
break;
|
||||
}
|
||||
|
||||
if (in.action == ActionFeedback::kFailed)
|
||||
if (in.action == ActionFeedback::kFailed || in.action == ActionFeedback::kCancelled)
|
||||
{
|
||||
// kCancelled ở đây là lỗi thứ tự gọi (không ai cancel trong EXECUTING_ACTIONS) — xử như hỏng.
|
||||
// Action hỏng không có đường recovery: recovery behavior là công cụ phục hồi NAVIGATION
|
||||
// (dọn costmap, lùi, xoay), không giúp gì được một thiết bị đang hỏng. Kết thúc tường minh
|
||||
// để mission layer quyết định làm gì với phần còn lại của order.
|
||||
@@ -909,6 +971,58 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
break;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------
|
||||
case NavigationState::kPausing:
|
||||
{
|
||||
// Đã nhận pause: stop_planner / cancel_recovery / pause_action đã phát ở cycle vào state,
|
||||
// arbiter đang phát 0. Ở đây chỉ chờ mọi thành phần IM rồi mới sang kPaused — host đọc
|
||||
// PAUSED là "đã dừng hẳn, làm gì cũng được" (2026-09-11). Cùng ba điều kiện quiescent với
|
||||
// kCancelling; `settle_timeout` là trần.
|
||||
if (preemptIfRequested(in, out))
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (in.cancel_requested)
|
||||
{
|
||||
enter(NavigationState::kCancelling, in.now, "cancelled while pausing", out);
|
||||
break;
|
||||
}
|
||||
if (in.resume_requested)
|
||||
{
|
||||
// Chưa kịp dừng hẳn đã được bảo chạy tiếp: đi đúng đường resume của kPaused.
|
||||
resumeFromPause(in, out);
|
||||
break;
|
||||
}
|
||||
|
||||
if (config_.settle_timeout <= 0.0)
|
||||
{
|
||||
if (in.robot_stopped)
|
||||
{
|
||||
enter(NavigationState::kPaused, in.now, "paused: robot stopped", out);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
const bool quiescent = in.robot_stopped && !in.planner_busy && !in.recovery_busy && in.action_terminal;
|
||||
if (quiescent)
|
||||
{
|
||||
enter(NavigationState::kPaused, in.now, "paused: runtime quiescent", out);
|
||||
break;
|
||||
}
|
||||
if (secondsInState(in.now) > config_.settle_timeout)
|
||||
{
|
||||
out.settle_timed_out = true;
|
||||
enter(NavigationState::kPaused, in.now, "paused: settle timeout", out);
|
||||
break;
|
||||
}
|
||||
// Action đã được bảo tạm dừng ở cycle vào state; tick tiếp cho handler thiết bị xác nhận.
|
||||
if (!in.action_terminal)
|
||||
{
|
||||
out.tick_action = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------
|
||||
case NavigationState::kPaused:
|
||||
{
|
||||
@@ -926,34 +1040,7 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
}
|
||||
if (in.resume_requested)
|
||||
{
|
||||
// Đặt lại toàn bộ đồng hồ kiên nhẫn: một lần tạm dừng dài không được tính là "planner chậm"
|
||||
// hay "controller hỏng", nếu không thì resume xong là rơi thẳng vào recovery.
|
||||
beginPlanningCycle(in.now);
|
||||
last_valid_control_ = in.now;
|
||||
last_oscillation_reset_ = in.now;
|
||||
sensor_stale_active_ = false;
|
||||
out.reset_oscillation_origin = true;
|
||||
travel_at_last_path_reset_ = 0.0; // origin đặt lại -> mốc recovery_reset_min_travel về 0
|
||||
|
||||
if (state_before_pause_ == NavigationState::kControlling)
|
||||
{
|
||||
out.run_controller = true;
|
||||
enter(NavigationState::kControlling, in.now, "resume following the plan", out);
|
||||
}
|
||||
else if (state_before_pause_ == NavigationState::kExecutingActions)
|
||||
{
|
||||
// Action không bị huỷ khi tạm dừng nên không start lại — tick tiếp đúng action dở dang.
|
||||
// Mốc action_patience được gieo lại: một quãng dừng dài không được tính vào trần thời
|
||||
// gian của action, nếu không resume xong là ABORTED oan ngay lập tức.
|
||||
action_started_at_ = in.now;
|
||||
out.tick_action = true;
|
||||
enter(NavigationState::kExecutingActions, in.now, "resume running the action", out);
|
||||
}
|
||||
else
|
||||
{
|
||||
out.start_planner = true;
|
||||
enter(NavigationState::kPlanning, in.now, "resume planning", out);
|
||||
}
|
||||
resumeFromPause(in, out);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -962,12 +1049,44 @@ StateMachineOutput StateMachine::update(const StateMachineInput& in)
|
||||
case NavigationState::kCancelling:
|
||||
{
|
||||
// Nguồn vận tốc đã là kNone nên bộ trọng tài phát lệnh 0 NGAY (arbiter cố ý không ramp khi
|
||||
// nguồn là kNone — xem VelocityArbiter::arbitrate); trạng thái này vì vậy kết thúc ngay cycle
|
||||
// robot_stopped bật, không cần thêm timeout.
|
||||
if (in.robot_stopped)
|
||||
// nguồn là kNone — xem VelocityArbiter::arbitrate). Lệnh 0 là điều kiện CẦN, chưa đủ để
|
||||
// báo CANCELLED (2026-09-11): host đọc CANCELLED/PREEMPTED là "đã xong, gửi goal kế được"
|
||||
// — mà goal kế bị `startPlan()` từ chối chừng nào thread planner còn chạy lượt cũ, và bị
|
||||
// tính là hỏng. Ba điều kiện quiescent: lệnh/odom về 0, thread planner rảnh, action đã trả
|
||||
// terminal. `settle_timeout` là trần: quá thì vẫn CANCELLED nhưng báo lỗi lên bên gọi.
|
||||
if (config_.settle_timeout <= 0.0)
|
||||
{
|
||||
// Tắt: hành vi trước 2026-09-11 — chỉ chờ lệnh về 0.
|
||||
if (in.robot_stopped)
|
||||
{
|
||||
finish(NavigationState::kCancelled, NavigationOutcome::kCancelled, in.now,
|
||||
"robot came to a full stop", out);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
const bool quiescent = in.robot_stopped && !in.planner_busy && !in.recovery_busy && in.action_terminal;
|
||||
if (quiescent)
|
||||
{
|
||||
finish(NavigationState::kCancelled, NavigationOutcome::kCancelled, in.now,
|
||||
"robot came to a full stop", out);
|
||||
"robot stopped, planner and recovery idle, action terminal", out);
|
||||
break;
|
||||
}
|
||||
|
||||
if (secondsInState(in.now) > config_.settle_timeout)
|
||||
{
|
||||
out.settle_timed_out = true;
|
||||
finish(NavigationState::kCancelled, NavigationOutcome::kCancelled, in.now,
|
||||
"cancel settle timeout", out);
|
||||
break;
|
||||
}
|
||||
|
||||
// Action đã bị bảo dừng ở cycle vào kCancelling; tick tiếp để handler thiết bị đưa thiết bị
|
||||
// về vị trí an toàn rồi báo terminal. Không cấp vận tốc — phần chốt bất biến cuối hàm ép
|
||||
// velocity_source về kNone cho mọi state phải dừng.
|
||||
if (!in.action_terminal)
|
||||
{
|
||||
out.tick_action = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user