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:
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_
|
||||
Reference in New Issue
Block a user