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:
@@ -654,6 +654,23 @@ void ControlLoop::collectPlannerResult()
|
||||
planner_feedback_ = PlannerFeedback::kPlanReady;
|
||||
}
|
||||
|
||||
bool ControlLoop::robotStopped() const
|
||||
{
|
||||
const StateMachineConfig& sm = config_.state_machine;
|
||||
const bool use_measured = sm.stopped_measured_linear_eps > 0.0 || sm.stopped_measured_angular_eps > 0.0;
|
||||
|
||||
robot_geometry_msgs::Twist measured;
|
||||
if (use_measured && deps_.controller != nullptr && deps_.controller->measuredVelocity(measured))
|
||||
{
|
||||
// Odom đo được thì nó là sự thật về bánh xe; lệnh đã phát chỉ là ý định. NaN từ odom rơi vào
|
||||
// nhánh "chưa dừng" (so sánh với NaN luôn false) — an toàn: settle_timeout là trần.
|
||||
return std::fabs(measured.linear.x) <= sm.stopped_measured_linear_eps &&
|
||||
std::fabs(measured.linear.y) <= sm.stopped_measured_linear_eps &&
|
||||
std::fabs(measured.angular.z) <= sm.stopped_measured_angular_eps;
|
||||
}
|
||||
return arbiter_.stopped();
|
||||
}
|
||||
|
||||
void ControlLoop::updateEnvMonitor(const robot::Time& now, double dt,
|
||||
const robot_geometry_msgs::PoseStamped& robot_pose, bool pose_available,
|
||||
bool costmap_stale)
|
||||
@@ -841,6 +858,28 @@ void ControlLoop::runController(robot_geometry_msgs::Twist& candidate)
|
||||
{
|
||||
candidate = robot_geometry_msgs::Twist();
|
||||
|
||||
// Hàng local_planner của bảng trạng thái: bận trong suốt lượt gọi plugin (đồng bộ, cùng thread),
|
||||
// rảnh khi ra. Watchdog (pha 4) dùng nó để bắt local planner treo — control thread không tự bắt
|
||||
// được chính mình. RAII để mọi đường thoát (kể cả exception từ plugin) đều trả về idle.
|
||||
struct LocalPlannerBusyScope
|
||||
{
|
||||
explicit LocalPlannerBusyScope(ComponentStatusBoard* board) : board_(board)
|
||||
{
|
||||
if (board_ != nullptr)
|
||||
{
|
||||
board_->enter(Component::kLocalPlanner, ComponentPhase::kBusy);
|
||||
}
|
||||
}
|
||||
~LocalPlannerBusyScope()
|
||||
{
|
||||
if (board_ != nullptr)
|
||||
{
|
||||
board_->enter(Component::kLocalPlanner, ComponentPhase::kIdle);
|
||||
}
|
||||
}
|
||||
ComponentStatusBoard* board_;
|
||||
} local_planner_busy(deps_.components);
|
||||
|
||||
// Giữ nguyên thứ tự của interface được bọc: hỏi đã tới đích trước, chỉ khi chưa mới tính lệnh.
|
||||
if (deps_.controller->isGoalReached())
|
||||
{
|
||||
@@ -948,7 +987,28 @@ bool ControlLoop::step()
|
||||
input.action_message = action_message_;
|
||||
input.pose_available = pose_available;
|
||||
input.costmap_current = !costmap_stale;
|
||||
input.robot_stopped = arbiter_.stopped();
|
||||
input.robot_stopped = robotStopped();
|
||||
// Điều kiện quiescent của kCancelling/kPausing (2026-09-11). `isPlanning()` là true cả khi lượt
|
||||
// đang chạy đã bị `cancelPlan()` — plugin không cắt được thì thread vẫn bận, và đó chính là thứ
|
||||
// host phải chờ. Bảng trạng thái thành phần (nếu có) bổ sung hàng recovery/action mà cổng không
|
||||
// hỏi được; hàng global_planner của bảng chỉ CỘNG thêm, cổng vẫn là câu hỏi gốc. Action: chưa có
|
||||
// handshake cancelling→cancelled ở cổng (pha 3) — không có bảng thì coi là terminal ngay.
|
||||
{
|
||||
const ComponentStatusBoard* board = deps_.components;
|
||||
input.planner_busy = deps_.planner->isPlanning() ||
|
||||
(board != nullptr && !board->isIdle(Component::kGlobalPlanner));
|
||||
input.recovery_busy = board != nullptr && !board->isIdle(Component::kRecovery);
|
||||
if (board != nullptr)
|
||||
{
|
||||
const ComponentPhase action_phase = board->phase(Component::kAction);
|
||||
input.action_terminal =
|
||||
action_phase == ComponentPhase::kIdle || action_phase == ComponentPhase::kPaused;
|
||||
}
|
||||
else
|
||||
{
|
||||
input.action_terminal = true;
|
||||
}
|
||||
}
|
||||
input.travelled_since_oscillation_reset = travelled;
|
||||
// Bốn cổng của state machine do EnvMonitor cấp (Test/ENV_MONITOR_PLAN.md): cờ chỉ khác false khi
|
||||
// verdict tương ứng có `action: recover`. `off_path` không còn nguồn nào (phép đo cũ đã bỏ 6/9/2026).
|
||||
@@ -980,6 +1040,21 @@ bool ControlLoop::step()
|
||||
}
|
||||
}
|
||||
|
||||
if (output.settle_timed_out)
|
||||
{
|
||||
// LỖI, không có khoá tắt: CANCELLED/PAUSED được báo ra mà runtime CHƯA im hẳn. Host sẽ tin và
|
||||
// gửi goal kế; goal kế chờ được nhờ kBusy, nhưng nguyên nhân gốc (plugin không cắt được
|
||||
// makePlan, odom không về 0, action không trả terminal) phải lộ ra ở đây, kèm thành phần nào.
|
||||
robot::log_error("[move_base2] settle timeout %.1f s while %s (mission %llu): finishing anyway — "
|
||||
"planner_busy=%s recovery_busy=%s action_terminal=%s robot_stopped=%s. A plugin "
|
||||
"is not honouring cancel/pause, or odometry never reads zero.\n",
|
||||
config_.state_machine.settle_timeout,
|
||||
output.state == NavigationState::kPaused ? "PAUSING" : "CANCELLING",
|
||||
static_cast<unsigned long long>(has_active_request_ ? active_request_.mission_sequence_id : 0),
|
||||
input.planner_busy ? "yes" : "no", input.recovery_busy ? "yes" : "no",
|
||||
input.action_terminal ? "yes" : "no", input.robot_stopped ? "yes" : "no");
|
||||
}
|
||||
|
||||
// Cờ một-lần đã được state machine tiêu thụ xong ở lời gọi trên.
|
||||
pause_requested_ = false;
|
||||
resume_requested_ = false;
|
||||
@@ -1182,6 +1257,14 @@ bool ControlLoop::step()
|
||||
{
|
||||
deps_.action->cancel();
|
||||
}
|
||||
if (output.pause_action && deps_.action != nullptr)
|
||||
{
|
||||
deps_.action->pause();
|
||||
}
|
||||
if (output.resume_action && deps_.action != nullptr)
|
||||
{
|
||||
deps_.action->resume();
|
||||
}
|
||||
|
||||
if (output.start_action)
|
||||
{
|
||||
@@ -1214,6 +1297,13 @@ bool ControlLoop::step()
|
||||
robot::log_info("[move_base2] %s\n", tick.message.c_str());
|
||||
}
|
||||
break;
|
||||
case ActionTick::Status::kCancelled:
|
||||
// Handler thiết bị đã về vị trí an toàn sau cancel (pha 3). Chỉ gặp trong CANCELLING/PAUSING;
|
||||
// hàng kAction về idle → quiescent. Một dòng info: đây là sườn kết thúc, không lặp.
|
||||
action_feedback_ = ActionFeedback::kCancelled;
|
||||
robot::log_info("[move_base2] action stopped safely after cancel%s%s\n",
|
||||
tick.message.empty() ? "" : ": ", tick.message.c_str());
|
||||
break;
|
||||
case ActionTick::Status::kFailed:
|
||||
action_feedback_ = ActionFeedback::kFailed;
|
||||
// Handler đã dựng sẵn lý do cụ thể ("thiếu mẫu", "tản quá", "không publish được"). Trước
|
||||
@@ -1365,10 +1455,27 @@ bool ControlLoop::step()
|
||||
}
|
||||
else if (output.start_planner)
|
||||
{
|
||||
// Không khởi động được (chưa có planner, pose hỏng...). Coi như một lượt hỏng để đồng hồ
|
||||
// kiên nhẫn tiếp tục chạy, thay vì đứng im ở kPlanning vô hạn. Riêng replan NỀN không khởi
|
||||
// động được thì bỏ qua — plan cũ vẫn hiệu lực, nhịp sau thử lại.
|
||||
planner_feedback_ = PlannerFeedback::kFailed;
|
||||
// Không khởi động được. Hai nguyên nhân khác hẳn nhau, không được gộp (bài học log
|
||||
// 2026-09-11: cancel lúc SBPL đang chạy, goal kế bị coi là hỏng và ABORTED sau 3 s dù chưa
|
||||
// lập plan lần nào):
|
||||
// - thread planner còn chạy lượt CŨ đã bị cancelPlan() mà plugin không cắt được → BẬN,
|
||||
// cycle sau thử lại; planner_patience vẫn là trần nếu bận quá lâu;
|
||||
// - chưa có planner, pose hỏng... → HỎNG thật, đồng hồ kiên nhẫn/retry chạy như cũ.
|
||||
// Riêng replan NỀN không khởi động được thì bỏ qua — plan cũ vẫn hiệu lực, nhịp sau thử lại.
|
||||
if (deps_.planner->isPlanning())
|
||||
{
|
||||
planner_feedback_ = PlannerFeedback::kBusy;
|
||||
robot::log_warning_throttle(5.0,
|
||||
"[move_base2] planner thread still busy with a cancelled attempt; "
|
||||
"waiting for it before planning mission %llu (planner_patience "
|
||||
"%.1f s is the ceiling).\n",
|
||||
static_cast<unsigned long long>(active_request_.mission_sequence_id),
|
||||
config_.state_machine.planner_patience);
|
||||
}
|
||||
else
|
||||
{
|
||||
planner_feedback_ = PlannerFeedback::kFailed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1452,6 +1559,14 @@ bool ControlLoop::step()
|
||||
cancel_requested_ = false;
|
||||
}
|
||||
|
||||
// Heartbeat của chính control thread — thứ duy nhất cho watchdog (thread khác) biết vòng điều
|
||||
// khiển còn quay. Đặt ở cuối step(): một cycle kẹt giữa chừng (detour SBPL, plugin treo) không
|
||||
// nhích được mốc này.
|
||||
if (deps_.components != nullptr)
|
||||
{
|
||||
deps_.components->progress(Component::kControlLoop);
|
||||
}
|
||||
|
||||
// false = "yêu cầu hiện tại vừa kết thúc, không còn gì để chạy". Preempt CŨNG báo kết quả (cho
|
||||
// chặng cũ) nhưng ngay lập tức nhận chặng mới — lúc đó runtime vẫn đang chạy, nên không được trả
|
||||
// false. Điều kiện đúng là "vào state terminal", không phải "có báo kết quả".
|
||||
|
||||
Reference in New Issue
Block a user