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:
@@ -14,9 +14,10 @@ Khi cần đổi hành vi: sửa tài liệu này trước, sửa test, rồi m
|
||||
| `PLANNING` | không ai (0) | nhận yêu cầu; recovery của `planning_failed` (hoặc lượt leo thang từ chính `PLANNING`) vừa chạy xong; tiếp tục sau tạm dừng | có plan hợp lệ → `CONTROLLING`; global planner chính fail/plan rỗng và có backup chưa dùng → đổi sang backup, lập plan lại; backup fail (hoặc không có backup) / quá `planner_patience` / quá `max_planning_retries` → còn pose thì `RECOVERING(planning_failed)`, **mất pose thì `RECOVERING(controlling_failed)`** — mất dữ liệu dùng ngân sách dữ liệu, không phải ngân sách "không tìm được đường" |
|
||||
| `CONTROLLING` | **local planner** | có plan hợp lệ; recovery leo thang từ `CONTROLLING` vừa xong (bám tiếp plan cũ, hoặc áp path detour); tiếp tục sau tạm dừng | `isGoalReached` → `SUCCEEDED` (hết action) hoặc `EXECUTING_ACTIONS` (còn action — D8); quá `controller_patience` → `RECOVERING(controlling_failed)`; quá `oscillation_timeout` → `RECOVERING(oscillation)`. **Không sinh được lệnh (kể cả mất pose) thì KHÔNG đổi state** (contract 2026-08-04): robot dừng tại chỗ, ở nguyên `CONTROLLING` chờ đồng hồ kiên nhẫn; plan mới trong lúc chạy đến từ replan nền theo `planner_frequency` và được thay tại chỗ |
|
||||
| `RECOVERING` | **recovery behavior** | ba trigger ở trên | tick trả `succeeded`/`failed` → cursor của **route thuộc trigger đó** tăng 1, rồi (2026-08-04b): behavior nộp path detour (`kPath`) → `CONTROLLING` áp path đó; trigger `planning_failed` hoặc leo thang từ `PLANNING` → `PLANNING` lập plan lại; còn lại (controlling/oscillation từ `CONTROLLING`) → `CONTROLLING` bám tiếp plan cũ, KHÔNG replan; hết route khi định vào → `ABORTED` |
|
||||
| `EXECUTING_ACTIONS` | không ai (0) — **D8** | tới goal mà yêu cầu còn action; nhận yêu cầu `has_goal == false` | action xong mà còn action kế → start action kế (ở nguyên state); action cuối `succeeded` → `SUCCEEDED`; một action `failed` → `ABORTED` (không qua recovery); quá `action_patience` (nếu bật) → cancel action + `ABORTED`; `cancel()` → `CANCELLING`; `pause()` → `PAUSED` (không huỷ action) |
|
||||
| `PAUSED` | không ai (0) | `pause()` từ `PLANNING`/`CONTROLLING`/`RECOVERING`/`EXECUTING_ACTIONS` | `resume()` → về state trước đó; `cancel()` → `CANCELLING` |
|
||||
| `CANCELLING` | không ai (0) | `cancel()` từ mọi state đang chạy | robot đã dừng → `CANCELLED` |
|
||||
| `EXECUTING_ACTIONS` | không ai (0) — **D8** | tới goal mà yêu cầu còn action; nhận yêu cầu `has_goal == false` | action xong mà còn action kế → start action kế (ở nguyên state); action cuối `succeeded` → `SUCCEEDED`; một action `failed` → `ABORTED` (không qua recovery); quá `action_patience` (nếu bật) → cancel action + `ABORTED`; `cancel()` → `CANCELLING`; `pause()` → `PAUSING` → `PAUSED` (không huỷ action) |
|
||||
| `PAUSING` | không ai (0) | `pause()` từ `PLANNING`/`CONTROLLING`/`RECOVERING`/`EXECUTING_ACTIONS` (2026-09-11). Ngay cycle vào: `stop_planner` / `cancel_recovery` / (pha 3) `pause_action`, lệnh = 0 | **quiescent** (`robot_stopped` **và** `!planner_busy` **và** `!recovery_busy` **và** `action_terminal`) → `PAUSED`; quá `settle_timeout` → `PAUSED` + cờ `settle_timed_out`; `settle_timeout = 0` → chỉ chờ `robot_stopped`. `resume()` → về state trước (chưa kịp dừng gì cũng không sao); `cancel()` → `CANCELLING`; goal mới → preempt như `PAUSED`. Host **không** thấy `PAUSED` ở đây — thấy tên state trước + `feed_back_str` "pausing…" |
|
||||
| `PAUSED` | không ai (0) | `PAUSING` đã im hẳn | `resume()` → về state trước đó (về `CONTROLLING` thì kèm `apply_plan`: nạp lại plan để local planner reset warm-start — 2026-09-11, phương án (b) đã chốt); `cancel()` → `CANCELLING` |
|
||||
| `CANCELLING` | không ai (0) | `cancel()` từ mọi state đang chạy | **quiescent** (2026-09-11): `robot_stopped` **và** `!planner_busy` **và** `!recovery_busy` **và** `action_terminal` → `CANCELLED`; quá `settle_timeout` → `CANCELLED` + cờ `settle_timed_out` (bên gọi log lỗi). `settle_timeout = 0` → chỉ chờ `robot_stopped` như trước. Chừng nào `!action_terminal` thì `tick_action` để handler dừng thiết bị an toàn, không cấp vận tốc |
|
||||
| `SUCCEEDED` / `ABORTED` / `CANCELLED` | không ai (0) | như trên | terminal; báo kết quả rồi về `IDLE` ở cycle kế tiếp |
|
||||
|
||||
## Thứ tự ưu tiên trong một cycle
|
||||
@@ -35,7 +36,7 @@ Khi cần đổi hành vi: sửa tài liệu này trước, sửa test, rồi m
|
||||
một lời gọi blocking. Bắt buộc như vậy vì recovery thế hệ 2 trả kết quả từng tick và có thể phát
|
||||
vận tốc — nghĩa là quyền phát cmd_vel phải chuyển tay từ local planner sang recovery và ngược lại.
|
||||
|
||||
2. **`PAUSED` và `CANCELLING` là state thật**, không phải cờ đọc rải rác trong vòng lặp. Đường huỷ ở
|
||||
2. **`PAUSING`/`PAUSED` và `CANCELLING` là state thật**, không phải cờ đọc rải rác trong vòng lặp. Đường huỷ ở
|
||||
bản cũ được viết hai lần, ở nhánh `try` và nhánh `catch`, gần 80 dòng giống hệt nhau.
|
||||
|
||||
3. **`SUCCEEDED`/`ABORTED`/`CANCELLED` là state**, không phải `return` giữa hàm. Đây là cơ chế giữ
|
||||
@@ -193,12 +194,37 @@ Bốn tính chất sau được chốt lại ở cuối mỗi lần chuyển sta
|
||||
4. `report_outcome` bật đúng một lần cho mỗi yêu cầu — kể cả yêu cầu có action: chỉ báo sau action
|
||||
cuối (D8).
|
||||
|
||||
## Vì sao `CANCELLING` luôn kết thúc
|
||||
## Vì sao `CANCELLING` luôn kết thúc — và vì sao nó phải chờ (2026-09-11)
|
||||
|
||||
`CANCELLING` chờ `robot_stopped`. Vì nguồn vận tốc ở state này đã là `kNone` và bộ trọng tài phát
|
||||
lệnh 0 tức thì, `robot_stopped` thành true ngay ở cycle kế tiếp. Do đó state này kết thúc sau hữu hạn
|
||||
cycle mà không cần thêm tham số timeout nào.
|
||||
Nguồn vận tốc ở state này là `kNone`, bộ trọng tài phát lệnh 0 **tức thì, không ramp**
|
||||
(`VelocityArbiter::arbitrate`, ba lý do ghi tại chỗ; giảm tốc theo động học là việc của driver bánh
|
||||
xe). Lệnh 0 là điều kiện cần. Trước 2026-09-11 nó cũng là điều kiện đủ, và đó là một lời nói dối với
|
||||
host: `CANCELLED`/`PREEMPTED` được báo trong lúc thread global planner còn chạy lượt `makePlan` đã
|
||||
huỷ (SBPL chạy tới hết `allocated_time`, `cancelPlan()` chỉ đặt cờ vứt kết quả). Host đọc PREEMPTED
|
||||
là "xong, gửi goal kế được"; goal kế bị `startPlan()` từ chối vì thread còn bận, control loop dịch
|
||||
thành `kFailed`, `max_planning_retries: 0` leo thang ngay → `ABORTED` sau 3 s dù chưa lập plan lần
|
||||
nào (`robot_app.log` 2026-09-11, mission 3 → 4).
|
||||
|
||||
Ở bước nối dây sau, `robot_stopped` nên được lấy từ vận tốc **đo được** (odometry) thay vì từ lệnh
|
||||
đã phát. Đó là lý do nó là một trường dữ liệu vào của state machine chứ không phải thứ state machine
|
||||
tự tính.
|
||||
Từ 2026-09-11 `CANCELLING` rời đi khi **quiescent** — ba điều kiện cùng đúng:
|
||||
|
||||
| Điều kiện | Nguồn | Nghĩa |
|
||||
|---|---|---|
|
||||
| `robot_stopped` | control loop: odom nếu có (`stopped_measured_*_eps`), không thì lệnh đã phát | bánh xe đã đứng, không chỉ lệnh = 0 |
|
||||
| `!planner_busy` | `PlannerPort::isPlanning()` **hoặc** hàng `kGlobalPlanner` của `ComponentStatusBoard` ≠ idle | thread planner rảnh, kể cả lượt đã huỷ |
|
||||
| `!recovery_busy` | hàng `kRecovery` của bảng (RecoveryRunner ghi; detour worker pha 2) | behavior và worker của nó đã thoát |
|
||||
| `action_terminal` | hàng `kAction` của bảng: idle hoặc paused (pha 3 mới có handshake; không có bảng → `true`) | handler đã dừng thiết bị xong |
|
||||
|
||||
`ComponentStatusBoard` (`core/component_status.h`, 2026-09-11) là nguồn sự thật: mỗi runner tự ghi
|
||||
hàng của mình (busy / stopping / idle / paused / hung) bằng atomic từ thread của nó; control loop
|
||||
ghi hàng `kLocalPlanner` (bọc quanh `computeVelocityCommands`) và heartbeat `kControlLoop` cuối
|
||||
mỗi `step()`. Watchdog (pha 4) đọc bảng từ thread riêng để bắt thành phần treo — kể cả chính
|
||||
control thread. Cùng bộ điều kiện dùng cho `PAUSING`.
|
||||
|
||||
`settle_timeout` [s] là trần: quá thì vẫn `CANCELLED` nhưng output mang
|
||||
`settle_timed_out` đúng một cycle và control loop **log lỗi, không có khoá tắt** — có plugin
|
||||
không cắt được lượt đang chạy hoặc odom không về 0. Đặt 0 = hành vi cũ. Host nhìn thấy đúng cặp
|
||||
`PREEMPTING`/`PREEMPTED` đã có trong `move_base_core::State`; chỉ thời điểm đổi.
|
||||
|
||||
Hệ quả cho tầng trên: `startPlan()` bị từ chối trong lúc `isPlanning()` là **`kBusy`, không phải
|
||||
`kFailed`** (`control_loop.cpp`, cùng ngày). `kBusy` giữ `PLANNING`, không tăng `planning_retries_`;
|
||||
`planner_patience` vẫn là trần cho "bận quá lâu" — một plugin treo thật vẫn bị phát hiện.
|
||||
|
||||
Reference in New Issue
Block a user