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>
19 KiB
Ma trận hành vi pause / resume / cancel theo từng state — bản cũ và sau plan
Ngày lập: 2026-09-11. Mục đích: đối chiếu hành vi đang chạy (bản cũ) với hành vi sau plan
PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md, từng state một, để soát plan có đi đúng hướng.
0. Phiên bản được gọi là "cũ"
pnkx_nav_core commit bd026b94c8 (2026-09-09, nhánh dev_recovery_and_manager_mission) nạp
runtime qua submodule src/Navigations/Packages/move_base2 ở commit 180c0e1c. Đây cũng là
con trỏ rb_ws/pnkx_nav_core đang giữ. move_base2 HEAD hiện tại (7d00c5a) chỉ khác 180c0e1c
một commit về rotate/relative_yaw trong control_loop.cpp — state machine và mọi đường
lifecycle giống hệt. Nên "cũ" dưới đây = code state_machine.cpp / control_loop.cpp /
navigation_server.cpp trước các sửa pha 1 (chưa commit) của 2026-09-11. Số dòng trích theo bản cũ.
Runtime này không có move_base thế hệ 1 nữa (không có state CLEARING thật; CLEARING chỉ
còn là tên host-facing của RECOVERING).
1. Lệnh đi vào lõi như thế nào (cả cũ lẫn mới)
host (thread bất kỳ) navigation_cancel() / _pause() / _resume()
→ NavigationServer::cancel()/pause()/resume() chỉ ghi cờ dưới data_mutex_, TRẢ VỀ NGAY
→ control thread, spinOnce() kế: drainLifecycleRequests() (navigation_server.cpp:1010-1047)
cancel thắng mọi thứ; pause và resume: lệnh gọi SAU thắng (mỗi lệnh xoá cờ đối nghịch)
→ ControlLoop::requestCancel()/requestPause()/requestResume() đặt cờ một-lần
→ ControlLoop::step() → StateMachine::update(input) cờ được tiêu thụ đúng một cycle
Thứ tự ưu tiên trong một cycle ở mọi state đang chạy (STATE_MACHINE.md):
cancel → pause → goal mới (preempt) → plan mới → phản hồi controller/recovery/action → ngưỡng.
Độ trễ danh nghĩa: ≤ 1 cycle (33 ms @ 30 Hz). Độ trễ thật = thời gian control thread đang bị chặn — quan trọng ở mục 3.4 (detour) và 2.2 (local planner solve dài).
cancel() từ host còn gọi missionLayer().cancel() → MissionManager::cancel() → xoá hàng đợi,
state mission CANCELLED ngay tại lời gọi, không chờ lõi. pause()/resume() không lan
xuống mission layer (cố ý, navigation_server.cpp:1050-1054).
Host nhìn thấy (NavFeedback.navigation_state, toHostState navigation_server.cpp:266-298):
| Lõi | Host |
|---|---|
kIdle |
PENDING (hoặc kết quả đã chốt: SUCCEEDED/ABORTED/PREEMPTED) |
kPlanning |
PLANNING |
kControlling |
CONTROLLING |
kRecovering |
CLEARING |
kExecutingActions |
ACTIVE |
kPaused |
PAUSED |
kCancelling |
PREEMPTING |
kSucceeded / kAborted / kCancelled |
SUCCEEDED / ABORTED / PREEMPTED (chốt tới yêu cầu kế) |
2. Ma trận theo state
Quy ước cột: Cũ = 180c0e1c; Pha 1 = đã code 2026-09-11, chờ build; Pha 2/3 = trong
plan, chưa làm. Ô để trống = không đổi so với cột trái.
2.1 IDLE (không có yêu cầu) và các state terminal (SUCCEEDED/ABORTED/CANCELLED)
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause | Lõi bỏ qua (không nhánh nào đọc pause_requested ở kIdle, state_machine.cpp:423-431). Host vẫn thấy PENDING |
||
| resume | Bỏ qua | ||
| cancel | Lõi bỏ qua (CancelWithoutRequestIsIgnored). Nhưng missionLayer().cancel() vẫn xoá hàng đợi mission và đặt MissionState::CANCELLED |
||
| goal mới | Nhận ngay → PLANNING (hoặc EXECUTING_ACTIONS nếu chỉ có action) |
Terminal chỉ sống một cycle rồi về IDLE; lệnh tới trong cycle đó bị bỏ qua như IDLE.
2.2 PLANNING — global planner đang lập plan trên thread planner
Bối cảnh plugin: CustomPlanner fail 100 % với mission qua REST (makePlan(start, goal) không hỗ
trợ) → rơi xuống backup SBPLLatticePlanner; một lượt SBPL tối đa allocated_time × 3 (10 s × 3
với cây config trên máy chạy log). PlannerRunner::cancelPlan() chỉ đặt discard_
(planner_runner.cpp:466-477), không cắt được makePlan đang chạy.
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause | stop_planner → cancelPlan() (chỉ discard, SBPL chạy tiếp) → PAUSED, state_before_pause_ = kPlanning (:441-446). Lệnh 0 tức thì. Host: PAUSED |
Pha 2: cancelPlan() → preempt() → SBPL thoát trong vài ms |
|
| resume (từ pause ở đây) | → PLANNING, start_planner (:948-952). startPlan() bị từ chối vì thread còn bận → control loop dịch thành kFailed (control_loop.cpp:1361-1372) → max_planning_retries: 0 → RECOVERING(planning_failed) ngay → wait → clear → ABORTED nếu thread còn bận quá ~11 s (cây dev) / ~27 s (máy log). Cùng bug với cancel |
startPlan() từ chối khi isPlanning() → kBusy: giữ PLANNING, không đếm retry, thử lại mỗi cycle, log throttle 5 s. planner_patience vẫn là trần |
Pha 2: thread đã rảnh từ lúc pause → startPlan() OK ngay |
| cancel | stop_planner (discard) → CANCELLING (:435-439) → cycle kế robot_stopped (lệnh = 0) → CANCELLED sau 33 ms trong khi SBPL còn chạy nhiều giây. Host thấy PREEMPTED, gửi goal kế → goal kế kFailed → ABORTED (log robot_app.log 893.444→896.911) |
CANCELLING giữ tới khi quiescent: lệnh/odom = 0 và !isPlanning() và action terminal; trần settle_timeout 2 s, quá trần vẫn CANCELLED + log_error. Host thấy PREEMPTING suốt lúc chờ. Goal kế gửi sớm → kBusy, không abort |
Pha 2: SBPL thoát trong ms → CANCELLED sau 1–2 cycle, lần này là thật |
| goal mới (preempt) | preemptIfRequested (:450): stop_planner (discard), báo PREEMPTED cho goal cũ, nhận goal mới → PLANNING → startPlan() từ chối → kFailed → như resume. MissionManager::submit() tự huỷ mission đang chạy nên host không cần cancel vẫn dính |
kBusy → chờ thread rảnh rồi lập plan |
Pha 2: thread rảnh ngay |
2.3 CONTROLLING — local planner phát lệnh mỗi cycle
Bối cảnh plugin: local planner (Priest, Hybrid, HybridDocking) chạy đồng bộ trên control
thread mỗi cycle (computeVelocityCommands); Priest solve 30–48 ms/cycle 33 ms. Không có
pause()/resume()/reset() trong robot_nav_core2::LocalPlanner. Replan nền (planner_frequency)
có thể có một lượt đang bay trên thread planner.
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause | stop_planner (discard lượt replan nền nếu có) → PAUSED, state_before_pause_ = kControlling (:518-523). Controller không được gọi nữa; arbiter phát 0 tức thì, không ramp (velocity_arbiter.cpp:256-266). Trạng thái nội bộ của plugin (warm-start, slew, latch) đóng băng nguyên trạng. Host: PAUSED |
||
| resume | → CONTROLLING với run_controller (:938-942), không setPlan lại, không hook nào cho plugin. Đồng hồ controller_patience/oscillation gieo lại. Plugin bám tiếp với warm-start của lúc robot còn chạy 0.4 m/s; arbiter ramp từ 0 theo max_accel_x nên không giật, nhưng plugin có thể tính infeasible vài cycle. Nếu robot bị đẩy đi trong lúc pause thì plan cũ lỗi thời |
(b) đang code: thêm apply_plan → setPlan(plan cũ) → Priest reset core_, warm, latch, slew theo pose hiện tại (priest_controller.cpp:534-553) rồi bám tiếp |
(a) đề xuất, chưa chốt: resume → PLANNING, lập plan mới từ pose hiện tại → setPlan(plan mới) → CONTROLLING. Một lượt planner (0.3–4 s) đổi lấy plan luôn khớp vị trí thật; đồng nhất với resume từ PLANNING/RECOVERING |
| cancel | stop_planner → CANCELLING (:512-516) → lệnh 0 tức thì → cycle kế CANCELLED. robot_stopped lấy từ lệnh đã phát (control_loop.cpp:951), bánh xe còn lăn theo giảm tốc của driver. Host thấy PREEMPTED khi robot còn trượt |
robot_stopped ưu tiên odom (stopped_measured_*_eps 0.02): CANCELLING giữ tới khi bánh xe đứng thật (trần 2 s). Replan nền còn bay → !isPlanning() cũng phải chờ |
Pha 2: replan nền bị preempt ngay |
| goal mới | preempt: stop_planner, PREEMPTED cho goal cũ, → PLANNING goal mới. Nếu replan nền đang bay → startPlan() từ chối → kFailed → nguy cơ như 2.2 |
kBusy |
Pha 2 |
| Local planner đang solve (30–48 ms) khi lệnh tới | Lệnh chờ solve xong (control thread bận) — trễ ≤ 1 cycle thêm |
2.4 RECOVERING — behavior được tick từ control thread
Hai họ behavior khác nhau về mức độ dừng được:
(i) wait, clear, rotate, back_up — tick ngắn, không chặn control thread.
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause | cancel_recovery → RecoveryRunner::cancel() → RecoveryBehavior::cancel() chỉ đặt cancel_requested_ (recovery_behavior.cpp:169-172); onCancel() không bao giờ chạy (tick kế không tới — recovery_runner.cpp:492). Nguồn vận tốc kNone → arbiter 0 tức thì. Recovery bị huỷ, state_before_pause_ = kPlanning (:713-720) — cố ý: behavior dead-reckon theo thời gian, không được để dở qua quãng dừng |
Pha 2: cancel() gọi thêm hook đồng bộ onCancelRequested() (no-op với họ này) |
|
| resume | → PLANNING, lập plan lại từ đầu (:948-952). Cursor route giữ nguyên |
(nếu planner thread bận vì lượt trước — kBusy) |
|
| cancel | cancel_recovery → CANCELLING (:707-711) → cycle kế CANCELLED. rotate 0.4 rad/s → 0 trong 33 ms (12 rad/s² lệnh), back_up 0.1 m/s → 0 (3 m/s²). Doc onCancel() hứa "plugin ramp về 0" nhưng không chạy — chọn giữ 0 tức thì (quyết định A), sửa doc ở pha 2 |
odom phải về 0 mới CANCELLED |
|
| goal mới | preempt: cancel_recovery + stop_planner, PREEMPTED cho goal cũ → PLANNING |
(ii) detour_path (DetourPathRecovery, sbpl_recovery) — makePlan SBPL đồng bộ trong
onUpdate trên control thread (detour_path_recovery.cpp:451-501), allocated_time 0.4 × tới
3 lượt replan = 1.2 s treo/tick, lặp attempts_per_run 3. Route dùng nó: controlling_failed,
path_blocked, off_path, collision (+ oscillation cây dev).
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
pause / cancel / goal mới tới trong lúc makePlan đang chạy |
Không được xử lý cho tới khi makePlan trả về: spinOnce() không chạy → drainLifecycleRequests() không chạy → cờ nằm trong cancel_requested_ tới 1.2 s. Robot đứng yên (kPath → kNone, lệnh 0 đã phát trước đó) nên không chạy tự do, nhưng cancel trễ tới 1.2 s |
(không đổi — pha 1 không chạm) | Pha 2: makePlan sang worker thread, onUpdate chỉ poll → control thread không bao giờ kẹt; onCancelRequested() → planner_->preempt() → worker thoát trong ms. Sau đó hành vi như (i) |
sau khi makePlan trả về |
như (i) |
2.5 EXECUTING_ACTIONS — action của mission (D8), robot đứng yên
Bối cảnh: handler tick từ control thread, không block, tự timeout theo elapsed = now - started_at_
(wall). ActionHandler không có pause()/resume(); cancel() trả void;
ActionRunner::cancel() gọi handler->cancel() rồi quên handler ngay (action_runner.cpp:159-166).
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause | PAUSED, không huỷ action (:853-862) — cố ý: action thiết bị không idempotent. Chỉ ngừng tick. Host: PAUSED. Nhưng đồng hồ timeout của handler vẫn chạy theo wall time |
Pha 3: ActionHandler::pause(now) → handler ghi paused_at_ |
|
| resume | → EXECUTING_ACTIONS, tick tiếp đúng action đó, không start() lại (:943-947); action_started_at_ (cho action_patience) gieo lại. Handler thấy elapsed gồm cả quãng dừng → pause 30 s giữa detect_pallet (timeout 15 s) → tick đầu kFailed → ABORTED thẳng, không qua recovery, clear_queue_on_failure xoá cả order |
Pha 3: resume(now) → started_at_ += now - paused_at_ → không tính thời gian đứng |
|
| cancel | cancel_action → ActionRunner::cancel() → handler->cancel() + active_ = nullptr → CANCELLING (:847-851) → cycle kế CANCELLED. Với 5 handler hiện có (perception/no-op) là đúng. Handler thiết bị tương lai: PREEMPTED báo trong lúc thiết bị còn đang về vị trí an toàn |
action_terminal control loop truyền true — với port hiện tại là giá trị đúng duy nhất (runner đã quên handler). State machine đã sẵn nhánh tick_action khi !action_terminal |
Pha 3: ActionStatus::kCancelled; runner giữ handler, tick tới khi terminal; control loop truyền action_terminal thật; CANCELLING chờ nó |
| goal mới | preempt: cancel_action, PREEMPTED cho mission cũ → PLANNING mission mới |
2.6 PAUSED
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause (lần nữa) | Bỏ qua | ||
| resume | Về state_before_pause_: kControlling → CONTROLLING (2.3); kExecutingActions → tick tiếp (2.5); kPlanning (gồm cả pause từ RECOVERING) → PLANNING + start_planner (:927-955). Mọi đồng hồ kiên nhẫn gieo lại |
2.3 (b) hoặc (a) | |
| cancel | → CANCELLING (:922-925) → cycle kế CANCELLED. Nếu pause tới từ PLANNING, thread planner có thể còn bận lượt cũ — CANCELLED vẫn báo ngay (nói dối) |
CANCELLING chờ !isPlanning() (test CancelFromPausedAlsoWaitsForThePlannerThread) |
|
| goal mới | preempt được từ PAUSED (:917-920): stop_planner/cancel_recovery/cancel_action theo state trước đó, PREEMPTED cho goal cũ → PLANNING goal mới (không cần resume). Thread bận → kFailed |
kBusy |
2.7 CANCELLING
| Lệnh | Cũ | Pha 1 | Pha 2/3 |
|---|---|---|---|
| pause / resume | Bỏ qua (không nhánh nào đọc) | ||
| cancel (lần nữa) | Bỏ qua | ||
| goal mới | Không preempt (kCancelling không gọi preemptIfRequested): yêu cầu nằm chờ ở pending_request_, được nhận khi về IDLE sau CANCELLED |
Chờ lâu hơn (tới khi quiescent) rồi mới được nhận → sang PLANNING với thread đã rảnh |
|
| kết thúc | robot_stopped (lệnh = 0) → CANCELLED sau 1 cycle |
quiescent hoặc settle_timeout |
2.8 Profile docking / rotate / go_straight
Cùng state machine, chỉ khác planner được nạp (DockPlanner + HybridDockingLocalPlanner,
TwoPointsPlanner). Mọi ô trên áp dụng nguyên. Riêng resume từ CONTROLLING với
HybridDockingLocalPlanner: chưa đọc setPlan của nó — với (a) sẽ là DockPlanner lập lại từ
pose hiện tại tới marker, với (b) là setPlan(plan cũ); cả hai cần chạy thử.
3. Điểm plan cố ý khác bản cũ — soát ở đây
| # | Hành vi cũ | Plan đổi thành | Lý do | Pha |
|---|---|---|---|---|
| 1 | startPlan() từ chối = kFailed |
kBusy khi isPlanning() |
"bận" ≠ "hỏng"; gốc của ABORTED oan | 1 (đã code) |
| 2 | CANCELLED sau 1 cycle khi lệnh = 0 |
CANCELLING chờ quiescent (odom = 0, planner idle, action terminal), trần settle_timeout |
host đọc PREEMPTED là "xong" |
1 (đã code) |
| 3 | robot_stopped từ lệnh |
từ odom, lùi về lệnh khi không có odom; eps = 0 = tắt |
bánh xe còn lăn sau lệnh 0 | 1 (đã code) |
| 4 | resume từ CONTROLLING không setPlan |
(b) setPlan(plan cũ) — đã code; hoặc (a) về PLANNING lập plan mới — chờ chốt |
plugin reset warm-start; (a) còn khớp pose thật sau pause dài | 1 |
| 5 | cancelPlan() chỉ discard |
Preemptable::preempt() → SBPL thoát trong ms |
"dừng ở đó" thật | 2 |
| 6 | onCancel() recovery là dead code; detour treo control thread |
onCancelRequested() đồng bộ; detour worker thread + preempt |
cancel/pause tới được trong lúc detour | 2 |
| 7 | Cancel = 0 tức thì, không ramp | giữ nguyên (quyết định A); sửa doc onCancel() |
đúng nghĩa "dừng hết luôn"; giảm tốc là việc của driver | 2 (doc) |
| 8 | Action: pause không báo handler; cancel không handshake | pause/resume(now) + kCancelled |
pause dài → ABORTED oan; thiết bị tương lai | 3 |
| 9 | Plugin .so cũ nạp vào runtime mới → crash vtable |
ABI guard _abi symbol, registry từ chối |
đổi vtable ở 2 và 3 | 2, 3 |
| 10 | MissionState::CANCELLED ngay tại lời gọi |
giữ nguyên; host nhìn NavigationState::PREEMPTING (đã có sẵn) |
C API không lộ MissionState; không thêm enum |
1 |
Không đổi: pause huỷ recovery (2.4), pause đóng băng action (2.5), cancel từ mọi state về
CANCELLING, preempt từ mọi state đang chạy trừ CANCELLING, thứ tự ưu tiên cancel > pause > goal
mới, pause/resume không lan xuống mission layer, lệnh 0 tức thì.
4. Yêu cầu của bạn đối chiếu
| Yêu cầu | Trạng thái (cập nhật 2026-09-12) |
|---|---|
| Đang planning / setPlan / recovery / action → cancel hoặc pause dừng ở đó | setPlan, recovery (i), action: có sẵn. Planning: pha 2 xong, đo runtime — cancel/pause @ PLANNING → preempted after +0.03 ms, CANCELLED/PAUSED trong 1–2 tick (R1, R2, R4). Detour: code + unit test xong, runtime chưa chạy |
| Pause rồi resume → set plan lại và gọi planning lại | Resume từ PLANNING: gọi planning lại — đo R2 (PLANNING: resume planning +27 ms, SBPL chạy lại). Resume từ CONTROLLING: đã chốt (b) setPlan(plan cũ) — đo R3 (setPlan: 1423 poses, Priest reseed từ vận tốc đo, không giật) |
| Local planner chạy theo chu kỳ → chỉ chạy tiếp | Đo R3: pause → lệnh 0 tức thì, PAUSED sau khi odom < 0.02 m/s (538 ms từ 0.115 m/s); resume → setPlan + chạy tiếp cùng cycle |
Báo đúng: CANCELLED/PAUSED chỉ khi mọi thứ thật sự im |
Đo R1–R5: mọi CANCELLED/PAUSED đều kèm planner=idle controller=idle recovery=idle + robot đứng theo odom; R5 cancel lúc còn PAUSING (bánh chưa dừng) → CANCELLING: cancelled while pausing → CANCELLED chỉ khi đứng (+250 ms) |
Bảng số đo đầy đủ: PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md mục 6, "Kết quả runtime đã chạy".