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>
85 lines
5.5 KiB
Markdown
85 lines
5.5 KiB
Markdown
# Mô hình thread và quyền sở hữu
|
|
|
|
## Bảng thread
|
|
|
|
| Thread | Sở hữu | Chu kỳ | Ghi chú |
|
|
|---|---|---|---|
|
|
| `control` | state machine, bộ trọng tài vận tốc, controller, recovery, action, **cmd_vel** | `controller_frequency` | **Thread duy nhất được phát cmd_vel.** Toàn bộ `ControlLoop::step()` chạy ở đây; heartbeat `kControlLoop` cuối mỗi step |
|
|
| `planner` | global planner (`PlannerRunner::threadBody`), hộp thư plan | theo biến điều kiện (`startPlan`) | Một lượt `makePlan` tại một thời điểm. `cancelPlan()` / `swapPlanner()` (khi còn lượt dở) / destructor đặt `discard_` **và** gọi `Preemptable::preempt()` trên `running_planner_` — instance thread đang ở trong `makePlan`, không phải `active_` (swap có thể đã đổi `active_`) — nếu plugin hỗ trợ (2026-09-11); SBPL thoát trong ms thay vì chạy hết `allocated_time` |
|
|
| `detour worker` | một lượt `makePlan` của `DetourPathRecovery` | theo attempt | Thuộc plugin `sbpl_recovery` (2026-09-11). Hợp lệ vì behavior họ `kPath` không phát vận tốc; `onUpdate` chỉ khởi động/poll, `onCancelRequested()` preempt + join |
|
|
| `watchdog` | đọc `ComponentStatusBoard`, kết luận treo | `watchdog_period` | Chỉ atomic load, không lock, không chạm runner. Control thread treo → `getFeedback()` báo `LOST` từ thread host (2026-09-11) |
|
|
| `mission_event` | hàng đợi sự kiện mission | theo sự kiện | Thuộc mission framework, không thuộc gói này |
|
|
| `mission_exec` | điều phối chặng mission | theo sự kiện | Thuộc mission framework. Chỉ gọi callback, **không được block** |
|
|
| host (ROS / C#) | nhận dữ liệu sensor, gọi API `BaseNavigation` | tuỳ host | Ghi vào vùng dữ liệu sensor qua mutex |
|
|
|
|
## Bất biến quan trọng nhất
|
|
|
|
> Chỉ thread `control` được phát cmd_vel.
|
|
|
|
Đây là lý do recovery được **tick từ control thread** thay vì chạy trong thread riêng. Recovery thế
|
|
hệ 2 có thể phát vận tốc; nếu nó chạy ở thread riêng thì sẽ có hai bộ điều khiển cùng ghi vào một
|
|
đường lệnh, và không có cách nào xác định được lệnh nào tới trước. Đó là hai bộ điều khiển tranh
|
|
nhau, không phải hai tác vụ song song.
|
|
|
|
Bất biến này được củng cố ở hai chỗ, độc lập nhau:
|
|
|
|
1. **Cấu trúc:** state machine cho ra đúng một `velocity_source` mỗi cycle, và `run_controller` với
|
|
`tick_recovery` không bao giờ cùng bật.
|
|
2. **Cổng ra:** mọi lệnh đều đi qua `VelocityArbiter`, và bộ trọng tài chỉ được gọi một lần trong
|
|
`ControlLoop::step()`.
|
|
|
|
## Mutex
|
|
|
|
| Mutex | Bảo vệ | Ai giữ |
|
|
|---|---|---|
|
|
| `NavigationServer::data_mutex_` | bản đồ tĩnh, laser scan, point cloud, depth camera, odometry, footprint | host khi ghi, control thread khi đọc |
|
|
| mutex của costmap | dữ liệu costmap | phần nối dây; **lấy lại mỗi lần dùng, không cache** |
|
|
|
|
`ControlLoop` và `StateMachine` **không** có mutex nào và cố ý như vậy: chúng chỉ chạy trên control
|
|
thread. Thêm mutex vào đó sẽ che mất việc có ai đó gọi sai thread.
|
|
|
|
## Con trỏ costmap
|
|
|
|
Con trỏ lấy từ `getCostmap()` là **non-owning và có thể bị thay giữa hai cycle**. Cache lại chính là
|
|
nguyên nhân lỗi double-free đã ghi nhận trong workspace. Quy tắc áp cho mọi hiện thực port:
|
|
|
|
- Lấy lại con trỏ ở đầu mỗi lần dùng.
|
|
- Không giữ tham chiếu qua nhiều cycle.
|
|
- Không giữ tham chiếu qua ranh giới thread.
|
|
|
|
## Bảng trạng thái thành phần (2026-09-11)
|
|
|
|
`ComponentStatusBoard` (`core/component_status.h`) là nguồn sự thật "ai đang bận / đang dừng / đã
|
|
treo". Quyền ghi theo hàng, mỗi hàng đúng MỘT chủ:
|
|
|
|
| Hàng | Chủ ghi | Thread ghi |
|
|
|---|---|---|
|
|
| `kGlobalPlanner` | `PlannerRunner` | planner (busy/idle) + control (`stopping` trong `cancelPlan`) — đều dưới `mutex_` |
|
|
| `kLocalPlanner` | `ControlLoop` (bọc quanh `computeVelocityCommands`) | control |
|
|
| `kRecovery` | `RecoveryRunner` | control |
|
|
| `kAction` | `ActionRunner` | control |
|
|
| `kControlLoop` | `ControlLoop` (heartbeat cuối `step()`) | control |
|
|
|
|
Đọc: `ControlLoop` mỗi cycle (quiescence cho `CANCELLING`/`PAUSING`), `Watchdog` từ thread riêng, host
|
|
qua C API `navigation_get_component_status()`. Mọi field atomic, relaxed; không lock trên đường control.
|
|
|
|
Watchdog **không ghi** vào bảng (giữ luật một chủ): kết luận treo nằm trong cờ riêng của nó.
|
|
|
|
## Lịch sử: từ một thread tới bốn
|
|
|
|
Bản dựng khung chạy đồng bộ một thread. Thread planner đã được thêm với mô hình hộp thư
|
|
(`startPlan`/`pollPlan`/`cancelPlan`); `PlannerFeedback` không đổi — state machine không biết plan
|
|
được tính đồng bộ hay bất đồng bộ. Detour worker và watchdog thêm 2026-09-11 (xem
|
|
`PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md`).
|
|
|
|
## Vòng đời
|
|
|
|
| Đối tượng | Ai sở hữu |
|
|
|---|---|
|
|
| `NavigationServer` | host, qua `shared_ptr` do factory plugin trả về |
|
|
| `ControlLoop`, `StateMachine`, `VelocityArbiter` | `NavigationServer`, theo giá trị |
|
|
| Mọi cổng (`ControlLoopDeps`) | **không sở hữu**; phải sống lâu hơn `ControlLoop` |
|
|
|
|
`ControlLoopDeps` chỉ chứa con trỏ trần và điều này là cố ý: nó nói rõ rằng control loop không sở hữu
|
|
gì cả. Bên nối dây chịu trách nhiệm giữ các cổng sống đủ lâu.
|