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

@@ -147,6 +147,12 @@ void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
readDouble(nh, "planner_frequency", config.planner_frequency);
readDouble(nh, "planner_timeout", config.planner_timeout);
readDouble(nh, "runtime_stats_period", config.runtime_stats_period);
readDouble(nh, "watchdog_period", config.watchdog.period);
readDouble(nh, "local_planner_call_budget", config.watchdog.local_planner_call_budget);
readDouble(nh, "recovery_stall_budget", config.watchdog.recovery_stall_budget);
readDouble(nh, "action_stall_budget", config.watchdog.action_stall_budget);
readDouble(nh, "control_loop_stall_budget", config.watchdog.control_loop_stall_budget);
config.watchdog.planner_timeout = config.planner_timeout;
readDouble(nh, "planner_patience", config.state_machine.planner_patience);
readDouble(nh, "controller_patience", config.state_machine.controller_patience);
@@ -157,6 +163,9 @@ void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
readDouble(nh, "action_patience", config.state_machine.action_patience);
readInt(nh, "max_planning_retries", config.state_machine.max_planning_retries);
readBool(nh, "recovery_behavior_enabled", config.state_machine.recovery_enabled);
readDouble(nh, "settle_timeout", config.state_machine.settle_timeout);
readDouble(nh, "stopped_measured_linear_eps", config.state_machine.stopped_measured_linear_eps);
readDouble(nh, "stopped_measured_angular_eps", config.state_machine.stopped_measured_angular_eps);
// Nghĩa của Twist đi qua arbiter — quyết định đơn vị của cả cụm giới hạn bên dưới. "steering"
// (linear.x = tốc độ bánh steer, angular.z = GÓC lái) là convention của robot lái một bánh
@@ -426,6 +435,10 @@ bool MoveBase2Config::validate(std::string& error) const
error = "planner_timeout is not finite [s]";
return false;
}
if (!watchdog.validate(error))
{
return false;
}
if (!std::isfinite(runtime_stats_period) || runtime_stats_period < 0.0)
{
@@ -525,6 +538,7 @@ std::string MoveBase2Config::describe() const
out << " planner_timeout: " << planner_timeout << " s\n";
out << " runtime_stats_period: " << runtime_stats_period << " s"
<< (runtime_stats_period > 0.0 ? "\n" : " (off)\n");
out << watchdog.describe();
out << " frames: global='" << global_frame << "' base='" << robot_base_frame << "'\n";
out << " require_current_costmap: " << (require_current_costmap ? "true" : "false") << "\n";
out << " namespaces: recovery='" << recovery_namespace << "' actions='" << action_namespace
@@ -571,6 +585,13 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
// Khoá này không có trong schema gen-1 — nó là công cụ chẩn đoán của move_base2. Vẫn đọc ở đây
// để bật được telemetry mà không phải chuyển cả cây config sang schema mới.
readDouble(nh, "runtime_stats_period", runtime_stats_period);
// Watchdog (2026-09-11) — không có trong schema gen-1; thiếu khoá thì default (bật, 0.1 s).
readDouble(nh, "watchdog_period", watchdog.period);
readDouble(nh, "local_planner_call_budget", watchdog.local_planner_call_budget);
readDouble(nh, "recovery_stall_budget", watchdog.recovery_stall_budget);
readDouble(nh, "action_stall_budget", watchdog.action_stall_budget);
readDouble(nh, "control_loop_stall_budget", watchdog.control_loop_stall_budget);
watchdog.planner_timeout = planner_timeout;
readDouble(nh, "planner_patience", state_machine.planner_patience);
readDouble(nh, "controller_patience", state_machine.controller_patience);
readDouble(nh, "sensor_patience", state_machine.sensor_patience);
@@ -578,6 +599,11 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
readDouble(nh, "oscillation_distance", state_machine.oscillation_distance);
readInt(nh, "max_planning_retries", state_machine.max_planning_retries);
readBool(nh, "recovery_behavior_enabled", state_machine.recovery_enabled);
// Không có trong schema gen-1 (bản cũ báo CANCELLED ngay khi lệnh về 0) — đọc để cây config cũ
// vẫn bật được chờ quiescent; thiếu khoá thì dùng default (bật, 2 s).
readDouble(nh, "settle_timeout", state_machine.settle_timeout);
readDouble(nh, "stopped_measured_linear_eps", state_machine.stopped_measured_linear_eps);
readDouble(nh, "stopped_measured_angular_eps", state_machine.stopped_measured_angular_eps);
readString(nh, "global_frame", global_frame);
readString(nh, "robot_base_frame", robot_base_frame);