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:
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <move_base2/control_loop.h>
|
||||
#include <move_base2/io/sensor_gateway.h>
|
||||
#include <move_base2/io/watchdog.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
@@ -44,9 +45,20 @@ struct MoveBase2Config
|
||||
/// [Hz] Nhịp lập plan lại khi đang bám plan. 0 = chỉ lập khi cần (theo yêu cầu của state machine).
|
||||
double planner_frequency = 0.0;
|
||||
|
||||
/// [s] Trần thời gian chờ một lần lập plan trước khi coi là hỏng. <= 0 = không giới hạn.
|
||||
/**
|
||||
* [s] Trần một lượt makePlan trên thread planner TRƯỚC KHI watchdog kết luận planner TREO (2026-09-11
|
||||
* — trước đó khoá này được đọc, in ra, và không ai dùng). Phải LỚN hơn thời gian một lượt hợp lệ
|
||||
* dài nhất: với SBPL là `allocated_time` (đã chia ngân sách chung từ 2026-09-11). <= 0 = không
|
||||
* giám sát. Đây là chẩn đoán, không phải điều khiển: state machine vẫn dùng `planner_patience`.
|
||||
*/
|
||||
double planner_timeout = 5.0;
|
||||
|
||||
/**
|
||||
* Watchdog (2026-09-11): thread riêng đọc bảng trạng thái thành phần, kết luận "treo" theo ngân
|
||||
* sách từng hàng. `watchdog.planner_timeout` được chép từ @ref planner_timeout khi nạp.
|
||||
*/
|
||||
WatchdogConfig watchdog;
|
||||
|
||||
// --- Telemetry --------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <robot_geometry_msgs/PoseStamped.h>
|
||||
#include <robot_geometry_msgs/Twist.h>
|
||||
|
||||
#include <move_base2/core/component_status.h>
|
||||
#include <move_base2/core/navigation_request.h>
|
||||
#include <move_base2/core/state_machine.h>
|
||||
#include <move_base2/core/velocity_arbiter.h>
|
||||
@@ -70,6 +71,14 @@ struct ControlLoopDeps
|
||||
* hoàn toàn, mọi bộ test dùng cổng giả rơi vào nhánh này.
|
||||
*/
|
||||
EnvironmentPort* environment = nullptr;
|
||||
|
||||
/**
|
||||
* Bảng trạng thái thành phần (2026-09-11). **Có thể null** — null thì quiescence của
|
||||
* CANCELLING/PAUSING chỉ dựa vào `PlannerPort::isPlanning()` và odom (hành vi pha 1a), recovery/
|
||||
* action coi như đã im. Có bảng thì runner ghi, control loop đọc thêm hàng recovery/action và tự
|
||||
* ghi hàng local_planner + control_loop (heartbeat cho watchdog).
|
||||
*/
|
||||
ComponentStatusBoard* components = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -272,6 +281,12 @@ public:
|
||||
return state_machine_.state();
|
||||
}
|
||||
|
||||
/// @brief State sẽ quay về sau PAUSING/PAUSED — host giữ tên state này trong lúc PAUSING.
|
||||
NavigationState stateBeforePause() const
|
||||
{
|
||||
return state_machine_.stateBeforePause();
|
||||
}
|
||||
|
||||
/// @brief Đang có yêu cầu chạy dở (kể cả đang pause) — false ngay khi kết quả được báo.
|
||||
bool hasActiveRequest() const
|
||||
{
|
||||
@@ -340,6 +355,15 @@ private:
|
||||
*/
|
||||
void collectPlannerResult();
|
||||
|
||||
/**
|
||||
* @brief Robot đã dừng chưa — ưu tiên vận tốc ĐO ĐƯỢC, lùi về lệnh đã phát khi không có odom.
|
||||
*
|
||||
* Lệnh đã phát về 0 không có nghĩa là bánh xe đã đứng: driver còn giảm tốc. Host đọc CANCELLED
|
||||
* lúc đó rồi gửi goal kế là gửi cho một robot đang trượt. Ngưỡng và công tắc:
|
||||
* `StateMachineConfig::stopped_measured_*_eps` (0 = bỏ odom).
|
||||
*/
|
||||
bool robotStopped() const;
|
||||
|
||||
/// @brief Gọi controller và lấy lệnh ứng viên.
|
||||
void runController(robot_geometry_msgs::Twist& candidate);
|
||||
|
||||
|
||||
196
include/move_base2/core/component_status.h
Normal file
196
include/move_base2/core/component_status.h
Normal file
@@ -0,0 +1,196 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* move_base2 — bảng trạng thái thành phần: mỗi thread/plugin tự khai nó đang làm gì.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#ifndef MOVE_BASE2_CORE_COMPONENT_STATUS_H_
|
||||
#define MOVE_BASE2_CORE_COMPONENT_STATUS_H_
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
|
||||
/**
|
||||
* @enum Component
|
||||
* @brief Các thành phần có thể "bận" độc lập với state machine.
|
||||
*
|
||||
* Thứ tự là chỉ số hàng trong @ref ComponentStatusBoard — thêm vào CUỐI, trước kCount, không xen
|
||||
* giữa (C API pha 4 phơi chỉ số này ra host).
|
||||
*/
|
||||
enum class Component : std::uint8_t
|
||||
{
|
||||
kGlobalPlanner = 0, ///< Thread lập plan của PlannerRunner (một lượt makePlan).
|
||||
kLocalPlanner, ///< Lời gọi computeVelocityCommands trên control thread.
|
||||
kRecovery, ///< Behavior đang chạy (và worker thread của detour, pha 2).
|
||||
kAction, ///< Handler đang chạy.
|
||||
kCostmapGlobal, ///< Map-update thread của global costmap (chỉ heartbeat).
|
||||
kCostmapLocal, ///< Map-update thread của local costmap (chỉ heartbeat).
|
||||
kControlLoop, ///< Chính control thread — heartbeat cuối mỗi step().
|
||||
kCount
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum ComponentPhase
|
||||
* @brief Thành phần đang ở đâu trong vòng đời của một lượt việc.
|
||||
*/
|
||||
enum class ComponentPhase : std::uint8_t
|
||||
{
|
||||
kIdle = 0, ///< Không có việc.
|
||||
kBusy, ///< Đang làm việc.
|
||||
kStopping, ///< Đã được bảo dừng (preempt/cancel) nhưng chưa thoát.
|
||||
kPaused, ///< Đã xác nhận tạm dừng (chỉ action, pha 3).
|
||||
kHung ///< Watchdog kết luận: bận quá ngân sách mà không nhích (pha 4).
|
||||
};
|
||||
|
||||
const char* toString(Component component);
|
||||
const char* toString(ComponentPhase phase);
|
||||
|
||||
/**
|
||||
* @struct ComponentStatus
|
||||
* @brief Một hàng của bảng. Mọi field là atomic, ghi relaxed — không lock, không alloc.
|
||||
*
|
||||
* Mốc thời gian là `steady_clock` [ns]: bảng phải so sánh được giữa các thread mà không phụ thuộc
|
||||
* đồng hồ giả của control loop (ClockPort). Đọc `phase_since_ns` để biết bận bao lâu,
|
||||
* `last_progress_ns` để biết lần cuối "nhích" — hai thứ khác nhau: SBPL bận 4 s nhưng nhích mỗi
|
||||
* vài ms là bình thường; bận 4 s mà không nhích 4 s là treo.
|
||||
*/
|
||||
struct ComponentStatus
|
||||
{
|
||||
std::atomic<ComponentPhase> phase{ComponentPhase::kIdle};
|
||||
std::atomic<std::int64_t> phase_since_ns{0};
|
||||
std::atomic<std::int64_t> last_progress_ns{0};
|
||||
std::atomic<std::uint64_t> progress_ticks{0};
|
||||
|
||||
ComponentStatus() = default;
|
||||
ComponentStatus(const ComponentStatus&) = delete;
|
||||
ComponentStatus& operator=(const ComponentStatus&) = delete;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class ComponentStatusBoard
|
||||
* @brief Nguồn sự thật duy nhất về "thành phần nào đang bận / đang dừng / đã treo".
|
||||
*
|
||||
* Quyền ghi theo hàng, mỗi hàng đúng MỘT chủ: PlannerRunner ghi kGlobalPlanner (từ cả control
|
||||
* thread lẫn thread lập plan, nhưng đều trong code của nó), RecoveryRunner ghi kRecovery,
|
||||
* ActionRunner ghi kAction, ControlLoop ghi kLocalPlanner và kControlLoop. Cổng giả trong test ghi
|
||||
* đúng hàng mà runner thật sẽ ghi. Hai chủ cho một hàng là race — không được.
|
||||
*
|
||||
* Ai đọc: ControlLoop mỗi cycle (quiescence cho CANCELLING/PAUSING), Watchdog (pha 4) từ thread
|
||||
* riêng, host qua C API (pha 4). Đọc chỉ là load relaxed.
|
||||
*
|
||||
* Vòng đời: NavigationRuntime sở hữu, sống lâu hơn mọi runner; runner giữ con trỏ non-owning, null
|
||||
* = không khai (mọi lời gọi thành no-op).
|
||||
*/
|
||||
class ComponentStatusBoard
|
||||
{
|
||||
public:
|
||||
static constexpr std::size_t kRows = static_cast<std::size_t>(Component::kCount);
|
||||
|
||||
ComponentStatusBoard() = default;
|
||||
ComponentStatusBoard(const ComponentStatusBoard&) = delete;
|
||||
ComponentStatusBoard& operator=(const ComponentStatusBoard&) = delete;
|
||||
|
||||
/// @brief Mốc thời gian dùng cho mọi hàng [ns, steady_clock].
|
||||
static std::int64_t nowNs()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Chủ hàng đổi phase. Đặt lại `phase_since_ns` và tính là một nhịp tiến độ.
|
||||
*
|
||||
* Không kiểm hợp lệ chuyển phase: bảng ghi lại sự thật do chủ hàng khai, không phải state
|
||||
* machine.
|
||||
*/
|
||||
void enter(Component component, ComponentPhase phase)
|
||||
{
|
||||
ComponentStatus& row = at(component);
|
||||
const std::int64_t now = nowNs();
|
||||
row.phase.store(phase, std::memory_order_relaxed);
|
||||
row.phase_since_ns.store(now, std::memory_order_relaxed);
|
||||
row.last_progress_ns.store(now, std::memory_order_relaxed);
|
||||
row.progress_ticks.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
/// @brief Chủ hàng báo "còn sống, đang nhích". Rẻ: hai store, một fetch_add.
|
||||
void progress(Component component)
|
||||
{
|
||||
ComponentStatus& row = at(component);
|
||||
row.last_progress_ns.store(nowNs(), std::memory_order_relaxed);
|
||||
row.progress_ticks.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
ComponentPhase phase(Component component) const
|
||||
{
|
||||
return at(component).phase.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
/// @brief [s] Đã ở phase hiện tại bao lâu, tính tới bây giờ.
|
||||
double secondsInPhase(Component component) const
|
||||
{
|
||||
return static_cast<double>(nowNs() - at(component).phase_since_ns.load(std::memory_order_relaxed)) * 1e-9;
|
||||
}
|
||||
|
||||
/// @brief [s] Từ lần "nhích" gần nhất tới bây giờ.
|
||||
double secondsSinceProgress(Component component) const
|
||||
{
|
||||
return static_cast<double>(nowNs() - at(component).last_progress_ns.load(std::memory_order_relaxed)) * 1e-9;
|
||||
}
|
||||
|
||||
bool isIdle(Component component) const
|
||||
{
|
||||
return phase(component) == ComponentPhase::kIdle;
|
||||
}
|
||||
|
||||
bool allIdle(std::initializer_list<Component> components) const
|
||||
{
|
||||
for (Component c : components)
|
||||
{
|
||||
if (!isIdle(c))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool anyHung() const
|
||||
{
|
||||
for (std::size_t i = 0; i < kRows; ++i)
|
||||
{
|
||||
if (rows_[i].phase.load(std::memory_order_relaxed) == ComponentPhase::kHung)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ComponentStatus& at(Component component)
|
||||
{
|
||||
return rows_[static_cast<std::size_t>(component)];
|
||||
}
|
||||
|
||||
const ComponentStatus& at(Component component) const
|
||||
{
|
||||
return rows_[static_cast<std::size_t>(component)];
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<ComponentStatus, kRows> rows_;
|
||||
};
|
||||
|
||||
} // namespace move_base2
|
||||
|
||||
#endif // MOVE_BASE2_CORE_COMPONENT_STATUS_H_
|
||||
@@ -40,7 +40,8 @@ enum class NavigationState
|
||||
kControlling, ///< Đang bám plan. Local planner là nguồn vận tốc.
|
||||
kRecovering, ///< Đang chạy một recovery behavior. Recovery là nguồn vận tốc.
|
||||
kExecutingActions, ///< Đang chạy action của mission tại chỗ (D8). Vận tốc = 0.
|
||||
kPaused, ///< Tạm dừng theo yêu cầu. Vận tốc = 0.
|
||||
kPausing, ///< Đã nhận pause, lệnh = 0, đang chờ mọi thành phần im (2026-09-11). Host vẫn thấy tên state trước.
|
||||
kPaused, ///< Tạm dừng theo yêu cầu, mọi thành phần đã im. Vận tốc = 0.
|
||||
kCancelling, ///< Đang giảm tốc để huỷ. Vận tốc = 0, chờ robot dừng hẳn.
|
||||
kSucceeded, ///< Terminal: đạt goal và mọi action đã xong.
|
||||
kAborted, ///< Terminal: hết cách, không đạt được goal hoặc action thất bại.
|
||||
|
||||
@@ -56,7 +56,8 @@ enum class ActionFeedback
|
||||
kIdle, ///< Chưa chạy action ở cycle này.
|
||||
kRunning,
|
||||
kSucceeded, ///< Action HIỆN TẠI xong; còn action kế tiếp hay không do state machine quyết.
|
||||
kFailed
|
||||
kFailed,
|
||||
kCancelled ///< Handler đã dừng thiết bị an toàn sau cancel (pha 3). Chỉ gặp trong CANCELLING/PAUSING.
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -117,6 +118,28 @@ struct StateMachineConfig
|
||||
/// Số lần lập plan hỏng liên tiếp tối đa. < 0 = không giới hạn (chỉ chặn bằng planner_patience).
|
||||
int max_planning_retries = -1;
|
||||
|
||||
/**
|
||||
* [s] Trần chờ ở kCancelling VÀ kPausing cho tới khi runtime QUIESCENT — thread global planner
|
||||
* idle, recovery idle, action đã trả terminal (cancel) / xác nhận tạm dừng (pause), robot đứng
|
||||
* yên theo vận tốc đo được. <= 0 = tắt: kết thúc ngay khi robot_stopped như trước 2026-09-11
|
||||
* (khi đó CANCELLED/PAUSED có thể được báo trong lúc một lượt makePlan đã huỷ vẫn còn chiếm thread
|
||||
* planner nhiều giây — host tin là xong và gửi goal kế, goal kế bị coi là hỏng).
|
||||
*
|
||||
* Quá trần thì vẫn kết thúc nhưng bên gọi phải log LỖI: có plugin/cấu hình khiến cancel/pause
|
||||
* không dừng được thật.
|
||||
*/
|
||||
double settle_timeout = 2.0;
|
||||
|
||||
/**
|
||||
* [m/s] Ngưỡng coi robot đã dừng theo vận tốc ĐO ĐƯỢC (odom). Chỉ dùng khi bên gọi có odom; đặt
|
||||
* 0 = bỏ qua odom, quay về đo bằng lệnh đã phát (arbiter). Odom nhiễu giữ CANCELLING lâu hơn thì
|
||||
* settle_timeout là trần.
|
||||
*/
|
||||
double stopped_measured_linear_eps = 0.02;
|
||||
|
||||
/// [rad/s] Như @ref stopped_measured_linear_eps cho trục quay.
|
||||
double stopped_measured_angular_eps = 0.02;
|
||||
|
||||
/// Số recovery behavior đã nạp được. 0 = không có đường phục hồi, lỗi là ABORTED ngay.
|
||||
std::size_t recovery_behavior_count = 0;
|
||||
|
||||
@@ -193,9 +216,33 @@ struct StateMachineInput
|
||||
*/
|
||||
bool costmap_current = true;
|
||||
|
||||
/// Robot đã dừng hẳn chưa (|v| <= ngưỡng). Dùng để rời kCancelling.
|
||||
/// Robot đã dừng hẳn chưa (|v| <= ngưỡng). Bên gọi ưu tiên vận tốc ĐO ĐƯỢC (odom), thiếu odom
|
||||
/// thì lấy lệnh đã phát. Một trong ba điều kiện rời kCancelling.
|
||||
bool robot_stopped = true;
|
||||
|
||||
/**
|
||||
* Thread planner còn đang chạy một lượt makePlan (kể cả lượt đã bị huỷ mà plugin không cắt được).
|
||||
*
|
||||
* Điều kiện thứ hai để rời kCancelling: CANCELLED chỉ được báo khi thread này rảnh, nếu không
|
||||
* host tin là xong và gửi goal kế trong lúc `startPlan()` còn bị từ chối. Bên gọi lấy từ
|
||||
* `PlannerPort::isPlanning()`.
|
||||
*/
|
||||
bool planner_busy = false;
|
||||
|
||||
/// Recovery behavior còn đang chạy hoặc đang dừng (kể cả worker thread của detour, pha 2). Bên gọi
|
||||
/// lấy từ bảng trạng thái thành phần. Điều kiện quiescent cùng nhóm với @ref planner_busy.
|
||||
bool recovery_busy = false;
|
||||
|
||||
/**
|
||||
* Action đang chạy đã trả trạng thái terminal sau khi bị `cancel_action` (hoặc không có action).
|
||||
*
|
||||
* Điều kiện thứ ba để rời kCancelling. Chừng nào còn false, state machine tiếp tục `tick_action`
|
||||
* trong kCancelling để handler thiết bị có cơ hội đưa thiết bị về vị trí an toàn rồi báo
|
||||
* `kCancelled` — không cấp vận tốc. Mặc định true: cổng action hiện chưa có handshake (pha 3),
|
||||
* bên gọi truyền true.
|
||||
*/
|
||||
bool action_terminal = true;
|
||||
|
||||
/**
|
||||
* Tuyến phía trước bị chắn (EnvMonitor kUnreachable: không còn đường trong cửa sổ tới điểm thoát, đã
|
||||
* tĩnh đủ `static_on`).
|
||||
@@ -260,6 +307,8 @@ struct StateMachineOutput
|
||||
bool start_action = false; ///< D8: khởi động action thứ @ref action_index của yêu cầu hiện tại.
|
||||
bool tick_action = false; ///< D8: gọi update() của action đang chạy.
|
||||
bool cancel_action = false; ///< D8: yêu cầu action đang chạy dừng an toàn.
|
||||
bool pause_action = false; ///< Báo handler tạm dừng (dịch đồng hồ timeout của nó) — 2026-09-11.
|
||||
bool resume_action = false; ///< Báo handler tiếp tục sau tạm dừng — 2026-09-11.
|
||||
|
||||
/// Chỉ có nghĩa khi @ref start_action — action nào trong danh sách của yêu cầu cần khởi động.
|
||||
std::size_t action_index = 0;
|
||||
@@ -298,6 +347,13 @@ struct StateMachineOutput
|
||||
bool recovery_reset_denied = false;
|
||||
double recovery_reset_travel = 0.0; ///< [m] chỉ có nghĩa khi @ref recovery_reset_denied.
|
||||
|
||||
/**
|
||||
* Cycle này kCancelling → kCancelled hoặc kPausing → kPaused xảy ra vì quá `settle_timeout` chứ
|
||||
* KHÔNG phải vì runtime đã quiescent. Bật đúng MỘT cycle. Bên gọi phải 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 bao giờ về 0.
|
||||
*/
|
||||
bool settle_timed_out = false;
|
||||
|
||||
/// Nguồn được phép phát vận tốc ở cycle này. Luôn có đúng một nguồn.
|
||||
VelocitySource velocity_source = VelocitySource::kNone;
|
||||
|
||||
@@ -369,6 +425,12 @@ public:
|
||||
return action_index_;
|
||||
}
|
||||
|
||||
/// @brief State sẽ quay về sau kPausing/kPaused. Host dùng để giữ tên state cũ trong lúc PAUSING.
|
||||
NavigationState stateBeforePause() const
|
||||
{
|
||||
return state_before_pause_;
|
||||
}
|
||||
|
||||
/// @brief Số lần lập plan hỏng liên tiếp tính từ lần vào kPlanning gần nhất.
|
||||
int planningRetries() const
|
||||
{
|
||||
@@ -423,6 +485,9 @@ private:
|
||||
void finish(NavigationState terminal, NavigationOutcome outcome, const robot::Time& now,
|
||||
const char* reason, StateMachineOutput& out);
|
||||
|
||||
/// @brief Đường resume chung của kPausing và kPaused: gieo lại đồng hồ, về @ref state_before_pause_.
|
||||
void resumeFromPause(const StateMachineInput& in, StateMachineOutput& out);
|
||||
|
||||
/// Cursor của route cho @p trigger trong request hiện hành.
|
||||
std::size_t& recoveryRouteCursor(RecoveryTrigger trigger);
|
||||
const std::size_t& recoveryRouteCursor(RecoveryTrigger trigger) const;
|
||||
|
||||
110
include/move_base2/io/watchdog.h
Normal file
110
include/move_base2/io/watchdog.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* move_base2 — watchdog: thread riêng đọc bảng trạng thái thành phần và kết luận "treo".
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#ifndef MOVE_BASE2_IO_WATCHDOG_H_
|
||||
#define MOVE_BASE2_IO_WATCHDOG_H_
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <move_base2/core/component_status.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
|
||||
/**
|
||||
* @struct WatchdogConfig
|
||||
* @brief Ngân sách "bận mà không nhích" cho từng thành phần. <= 0 = không giám sát hàng đó.
|
||||
*
|
||||
* "Nhích" = `ComponentStatusBoard::progress()` của chủ hàng. Bận lâu nhưng vẫn nhích là bình thường
|
||||
* (SBPL 4 s vẫn nhích mỗi expand); bận mà không nhích quá ngân sách là treo.
|
||||
*/
|
||||
struct WatchdogConfig
|
||||
{
|
||||
double period = 0.1; ///< [s] chu kỳ kiểm; 0 = tắt watchdog.
|
||||
double planner_timeout = 5.0; ///< [s] kGlobalPlanner — phải > allocated_time × số lượt replan của SBPL.
|
||||
double local_planner_call_budget = 0.5; ///< [s] kLocalPlanner — một lời gọi computeVelocityCommands.
|
||||
double recovery_stall_budget = 5.0; ///< [s] kRecovery.
|
||||
double action_stall_budget = 0.0; ///< [s] kAction — mặc định tắt: handler tự timeout.
|
||||
double control_loop_stall_budget = 0.5; ///< [s] kControlLoop — heartbeat cuối step(); 15 cycle @ 30 Hz.
|
||||
|
||||
bool validate(std::string& error) const;
|
||||
std::string describe() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class Watchdog
|
||||
* @brief Phát hiện thành phần treo từ một thread KHÁC thành phần bị nghi.
|
||||
*
|
||||
* Control thread không tự phát hiện được chính nó treo — đó là lý do phải có thread này. Nó chỉ
|
||||
* đọc atomics của @ref ComponentStatusBoard, không lock, không chạm runner; kết luận "treo" giữ
|
||||
* trong cờ riêng của watchdog (một hàng của bảng chỉ có MỘT chủ ghi — watchdog không ghi vào đó).
|
||||
*
|
||||
* Log: `log_error` đúng một lần ở sườn treo (không có khoá tắt), `log_warning` khi hồi.
|
||||
*/
|
||||
class Watchdog
|
||||
{
|
||||
public:
|
||||
Watchdog() = default;
|
||||
~Watchdog();
|
||||
Watchdog(const Watchdog&) = delete;
|
||||
Watchdog& operator=(const Watchdog&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Khởi động thread giám sát. @p board non-owning, phải sống lâu hơn watchdog.
|
||||
* @return false nếu config sai hoặc đã chạy.
|
||||
*/
|
||||
bool start(const ComponentStatusBoard* board, const WatchdogConfig& config, std::string& error);
|
||||
|
||||
void stop();
|
||||
|
||||
bool running() const
|
||||
{
|
||||
return running_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
/// @brief Thành phần @p c đang bị kết luận treo.
|
||||
bool hung(Component c) const
|
||||
{
|
||||
return hung_[static_cast<std::size_t>(c)].load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
bool anyHung() const;
|
||||
|
||||
/// @brief Control thread không nhích quá ngân sách — host phải thấy LOST và tự zero cmd_vel.
|
||||
bool controlLoopHung() const
|
||||
{
|
||||
return hung(Component::kControlLoop);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Chạy MỘT vòng kiểm ngay trên thread gọi (không cần thread riêng) — cho test và cho
|
||||
* host muốn tự tick. Cùng logic với thread nền.
|
||||
*/
|
||||
void checkOnce();
|
||||
|
||||
/// @brief Một dòng tóm tắt các thành phần đang treo, rỗng nếu không có.
|
||||
std::string report() const;
|
||||
|
||||
private:
|
||||
double budgetFor(Component c) const;
|
||||
void threadBody();
|
||||
|
||||
const ComponentStatusBoard* board_ = nullptr;
|
||||
WatchdogConfig config_;
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::array<std::atomic<bool>, ComponentStatusBoard::kRows> hung_{};
|
||||
};
|
||||
|
||||
} // namespace move_base2
|
||||
|
||||
#endif // MOVE_BASE2_IO_WATCHDOG_H_
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <move_base2/bridges/mission_adapter_bridge.h>
|
||||
#include <move_base2/bridges/mission_layer.h>
|
||||
#include <move_base2/config/move_base2_config.h>
|
||||
#include <move_base2/core/component_status.h>
|
||||
#include <move_base2/io/runtime_stats.h>
|
||||
#include <move_base2/control_loop.h>
|
||||
#include <move_base2/io/costmap_exporter.h>
|
||||
@@ -276,6 +277,16 @@ public:
|
||||
return stats_.get();
|
||||
}
|
||||
|
||||
/// @brief Bảng trạng thái thành phần — watchdog và host đọc từ đây (chỉ atomic load).
|
||||
const ComponentStatusBoard& components() const
|
||||
{
|
||||
return components_;
|
||||
}
|
||||
ComponentStatusBoard& components()
|
||||
{
|
||||
return components_;
|
||||
}
|
||||
|
||||
robot_costmap_2d::Costmap2DROBOT* globalCostmap()
|
||||
{
|
||||
return global_costmap_.get();
|
||||
@@ -342,6 +353,12 @@ private:
|
||||
/// Dựng trong buildCostmaps() ngay sau khi đọc config, vì nó phải chụp được thread mà costmap tạo.
|
||||
std::unique_ptr<RuntimeStats> stats_;
|
||||
|
||||
/**
|
||||
* Bảng trạng thái thành phần (2026-09-11). Khai TRƯỚC mọi runner để huỷ SAU chúng: runner giữ
|
||||
* con trỏ non-owning và thread lập plan có thể ghi vào bảng cho tới khi PlannerRunner join xong.
|
||||
*/
|
||||
ComponentStatusBoard components_;
|
||||
|
||||
/// Guard "không đi mù": hỏi costmap điều khiển xem observation buffer còn hạn không.
|
||||
CostmapStatusAdapter costmap_status_;
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <move_base2/core/navigation_request.h>
|
||||
#include <move_base2/io/runtime_stats.h>
|
||||
#include <move_base2/io/sensor_gateway.h>
|
||||
#include <move_base2/io/watchdog.h>
|
||||
#include <robot_sensor_ports/sensor_store.h>
|
||||
#include <move_base2/navigation_runtime.h>
|
||||
|
||||
@@ -246,6 +247,29 @@ public:
|
||||
robot_nav_2d_msgs::Twist2DStamped getTwist() override;
|
||||
|
||||
robot::move_base_core::NavFeedback* getFeedback() override;
|
||||
|
||||
/**
|
||||
* @brief Bảng trạng thái thành phần của runtime (null khi chưa initialize / cổng bơm từ ngoài).
|
||||
*
|
||||
* Cho host (C API `navigation_get_component_status`) và watchdog. Chỉ atomic load — gọi được từ
|
||||
* thread bất kỳ.
|
||||
*/
|
||||
const ComponentStatusBoard* componentStatus() const;
|
||||
|
||||
const Watchdog& watchdog() const
|
||||
{
|
||||
return watchdog_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Khởi động watchdog trên bảng @p board với @p config — test seam (initialize() tự làm
|
||||
* việc này với bảng của runtime).
|
||||
*/
|
||||
bool startWatchdog(const ComponentStatusBoard* board, const WatchdogConfig& config,
|
||||
std::string& error)
|
||||
{
|
||||
return watchdog_.start(board, config, error);
|
||||
}
|
||||
robot::move_base_core::PlannerDataOutput getGlobalData() override;
|
||||
robot::move_base_core::PlannerDataOutput getLocalData() override;
|
||||
|
||||
@@ -367,6 +391,12 @@ private:
|
||||
std::atomic<bool> control_thread_running_{ false };
|
||||
robot::TFListenerPtr tf_;
|
||||
|
||||
/**
|
||||
* Watchdog (2026-09-11): thread riêng đọc bảng trạng thái thành phần của runtime, kết luận treo.
|
||||
* Khai SAU runtime_ (huỷ trước nó) — nó chỉ giữ con trỏ non-owning vào bảng của runtime.
|
||||
*/
|
||||
Watchdog watchdog_;
|
||||
|
||||
/// Bảo vệ dữ liệu sensor và footprint: host ghi từ thread của nó, control loop đọc.
|
||||
mutable std::mutex data_mutex_;
|
||||
|
||||
|
||||
@@ -40,7 +40,8 @@ struct ActionTick
|
||||
{
|
||||
kRunning,
|
||||
kSucceeded,
|
||||
kFailed
|
||||
kFailed,
|
||||
kCancelled ///< Handler đã dừng thiết bị an toàn sau cancel() (pha 3). Terminal.
|
||||
};
|
||||
|
||||
Status status = Status::kRunning;
|
||||
@@ -83,8 +84,25 @@ public:
|
||||
/// @brief Một control cycle. CHỈ được gọi sau khi @ref start trả true.
|
||||
virtual ActionTick update() = 0;
|
||||
|
||||
/// @brief Yêu cầu dừng an toàn action đang chạy (huỷ mission, emergency).
|
||||
/**
|
||||
* @brief Yêu cầu dừng an toàn action đang chạy (huỷ mission, emergency).
|
||||
*
|
||||
* Từ 2026-09-11 có hai kiểu: handler dừng xong ngay trong cancel() (port quên nó, hàng kAction về
|
||||
* idle) hoặc handler cần tick tới khi trả `kCancelled` (port giữ nó, hàng kAction = stopping,
|
||||
* state machine tiếp tục @ref update trong CANCELLING). Bên gọi phân biệt qua bảng trạng thái
|
||||
* thành phần, không qua giá trị trả về.
|
||||
*/
|
||||
virtual void cancel() = 0;
|
||||
|
||||
/// @brief Tạm dừng action đang chạy: handler dịch đồng hồ timeout, KHÔNG dừng thiết bị. Default no-op.
|
||||
virtual void pause()
|
||||
{
|
||||
}
|
||||
|
||||
/// @brief Tiếp tục sau @ref pause. Default no-op.
|
||||
virtual void resume()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace move_base2
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
#include <action_core/action_registry.h>
|
||||
|
||||
#include <move_base2/core/component_status.h>
|
||||
#include <move_base2/ports/action_port.h>
|
||||
#include <move_base2/ports/clock_port.h>
|
||||
|
||||
@@ -86,6 +87,8 @@ public:
|
||||
bool start(const robot_protocol_msgs::Action& action) override;
|
||||
ActionTick update() override;
|
||||
void cancel() override;
|
||||
void pause() override;
|
||||
void resume() override;
|
||||
|
||||
/// @brief Số handler đã nạp.
|
||||
std::size_t handlerCount() const
|
||||
@@ -105,6 +108,18 @@ public:
|
||||
return registry_.find(action_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gắn bảng trạng thái thành phần (non-owning, null = không khai).
|
||||
*
|
||||
* Runner là CHỦ hàng `kAction`: kBusy từ @ref start thành công, `progress()` mỗi @ref update,
|
||||
* kIdle khi tick trả terminal hoặc khi @ref cancel (cổng hiện quên handler ngay trong cancel —
|
||||
* pha 3 đổi thành kStopping tới khi handler trả kCancelled).
|
||||
*/
|
||||
void attachStatusBoard(ComponentStatusBoard* board)
|
||||
{
|
||||
status_board_ = board;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Dịch kết quả của framework sang kiểu của port.
|
||||
static ActionTick toTick(const action_core::ActionTick& tick);
|
||||
@@ -117,6 +132,7 @@ private:
|
||||
action_core::ActionRegistry registry_;
|
||||
|
||||
action_core::ActionHandler* active_ = nullptr; ///< non-owning, thuộc registry_
|
||||
ComponentStatusBoard* status_board_ = nullptr; ///< non-owning, null = không khai
|
||||
std::string active_action_id_;
|
||||
};
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
#include <robot_nav_core/base_global_planner.h>
|
||||
#include <robot_nav_core/preemptable.h>
|
||||
|
||||
#include <move_base2/core/component_status.h>
|
||||
#include <move_base2/io/runtime_stats.h>
|
||||
#include <move_base2/ports/planner_port.h>
|
||||
|
||||
@@ -120,6 +122,15 @@ public:
|
||||
*/
|
||||
void attachStats(RuntimeStats* stats);
|
||||
|
||||
/**
|
||||
* @brief Gắn bảng trạng thái thành phần (non-owning, null = không khai). Gọi trước @ref configure.
|
||||
*
|
||||
* Runner là CHỦ hàng `kGlobalPlanner`: kBusy khi thread bắt đầu makePlan, kStopping khi
|
||||
* @ref cancelPlan tới mà lượt còn chạy, kIdle khi makePlan trả về. Mọi lời ghi đều dưới `mutex_`
|
||||
* nên dù từ hai thread vẫn tuần tự.
|
||||
*/
|
||||
void attachStatusBoard(ComponentStatusBoard* board);
|
||||
|
||||
/**
|
||||
* @brief Tắt việc khoá costmap mutex quanh makePlan — CHỈ dành cho unit test.
|
||||
*
|
||||
@@ -174,6 +185,9 @@ private:
|
||||
/// @brief Thân thread: ngủ tới khi có yêu cầu, chạy plugin, đặt kết quả vào hộp thư.
|
||||
void threadBody();
|
||||
|
||||
/// @brief Gọi `preempt()` trên instance đang ở trong makePlan (nếu nó Preemptable). Gọi dưới mutex_.
|
||||
void preemptRunningLocked();
|
||||
|
||||
// --- Chỉ control thread chạm ------------------------------------------------------------------
|
||||
robot::NodeHandle nh_;
|
||||
robot_costmap_2d::Costmap2DROBOT* costmap_ = nullptr;
|
||||
@@ -184,6 +198,7 @@ private:
|
||||
|
||||
/// Telemetry non-owning, null = tắt đo. Chỉ đọc sau khi @ref attachStats.
|
||||
RuntimeStats* stats_ = nullptr;
|
||||
ComponentStatusBoard* status_board_ = nullptr; ///< Non-owning, null = không khai.
|
||||
RuntimeStats::SectionId section_make_plan_ = RuntimeStats::kInvalidSection;
|
||||
|
||||
std::map<std::string, Loaded> planners_;
|
||||
@@ -213,6 +228,14 @@ private:
|
||||
bool has_result_ = false;
|
||||
|
||||
robot_nav_core::BaseGlobalPlanner* active_ = nullptr; ///< Non-owning, trỏ vào @ref planners_.
|
||||
/**
|
||||
* Instance mà thread lập plan ĐANG ở trong `makePlan` (non-owning, chỉ khác null khi `running_`).
|
||||
*
|
||||
* Khác `active_`: swapPlanner() đổi `active_` ngay cả khi lượt cũ chưa về, nên preempt qua
|
||||
* `active_` sau một lần đổi planner sẽ cắt nhầm instance mới (rảnh) và bỏ sót instance cũ (đang
|
||||
* chạy). Mọi đường cắt — cancelPlan, swapPlanner, destructor — đều đi qua con trỏ này.
|
||||
*/
|
||||
robot_nav_core::BaseGlobalPlanner* running_planner_ = nullptr;
|
||||
robot_geometry_msgs::PoseStamped request_start_;
|
||||
robot_geometry_msgs::PoseStamped request_goal_;
|
||||
std::shared_ptr<robot_protocol_msgs::Order> request_order_;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <recovery_core/adapters/costmap_collision_checker.h>
|
||||
#include <recovery_core/recovery_registry.h>
|
||||
|
||||
#include <move_base2/core/component_status.h>
|
||||
#include <move_base2/ports/clock_port.h>
|
||||
#include <move_base2/ports/pose_port.h>
|
||||
#include <move_base2/ports/recovery_port.h>
|
||||
@@ -125,6 +126,19 @@ public:
|
||||
void cancel() override;
|
||||
std::string behaviorName(std::size_t index) const override;
|
||||
|
||||
/**
|
||||
* @brief Gắn bảng trạng thái thành phần (non-owning, null = không khai).
|
||||
*
|
||||
* Runner là CHỦ hàng `kRecovery`: kBusy từ @ref start thành công, `progress()` mỗi @ref update,
|
||||
* kIdle khi tick trả terminal hoặc khi @ref cancel — behavior họ velocity/one-shot dừng ngay
|
||||
* trong cùng cycle (arbiter phát 0), behavior có việc chạy dài phải xong việc đó ngay trong
|
||||
* `cancel()` của nó (contract `onCancelRequested()`, pha 2) nên sau cancel() hàng về idle là đúng.
|
||||
*/
|
||||
void attachStatusBoard(ComponentStatusBoard* board)
|
||||
{
|
||||
status_board_ = board;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Chuyển `PosePort` của lõi thành cổng pose của recovery_core.
|
||||
class PoseBridge final : public recovery_core::PoseProvider
|
||||
@@ -186,6 +200,7 @@ private:
|
||||
|
||||
bool configured_ = false;
|
||||
recovery_core::RecoveryBehavior* active_ = nullptr; ///< non-owning, thuộc registry_
|
||||
ComponentStatusBoard* status_board_ = nullptr; ///< non-owning, null = không khai
|
||||
|
||||
/// Tên + trigger của lượt đang chạy, giữ để log được sườn KẾT THÚC (active_ bị xoá ở đó).
|
||||
std::string active_name_;
|
||||
|
||||
Reference in New Issue
Block a user