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:
@@ -25,6 +25,7 @@
|
||||
|
||||
#include <robot/time.h>
|
||||
|
||||
#include <move_base2/core/component_status.h>
|
||||
#include <move_base2/ports/action_port.h>
|
||||
#include <move_base2/ports/clock_port.h>
|
||||
#include <move_base2/ports/controller_port.h>
|
||||
@@ -221,6 +222,7 @@ public:
|
||||
pending_goal_ = goal;
|
||||
cycles_left_ = latency_cycles_;
|
||||
pending_action_ = nextAction();
|
||||
report(ComponentPhase::kBusy);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -248,6 +250,14 @@ public:
|
||||
}
|
||||
|
||||
in_flight_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
if (discarded_)
|
||||
{
|
||||
// Lượt đã bị huỷ giữa chừng nhưng plugin không cắt được (SBPL thật): kết quả về thì vứt lặng
|
||||
// lẽ, đúng như PlannerRunner với discard_. Bên gọi chỉ thấy isPlanning() chuyển về false.
|
||||
discarded_ = false;
|
||||
return false;
|
||||
}
|
||||
result.tag = pending_tag_;
|
||||
result.plan.clear();
|
||||
|
||||
@@ -272,8 +282,23 @@ public:
|
||||
|
||||
void cancelPlan() override
|
||||
{
|
||||
in_flight_ = false;
|
||||
++cancel_count_;
|
||||
if (cancel_keeps_busy_ && in_flight_)
|
||||
{
|
||||
// Mô phỏng plugin KHÔNG preempt được: thread vẫn bận cho tới hết latency đã đặt, kết quả
|
||||
// về bị vứt. Đây là hành vi của PlannerRunner + SBPL trước pha 2.
|
||||
discarded_ = true;
|
||||
report(ComponentPhase::kStopping);
|
||||
return;
|
||||
}
|
||||
in_flight_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
}
|
||||
|
||||
/// @brief Ghi hàng `kGlobalPlanner` như PlannerRunner thật. Null = không khai.
|
||||
void attachStatusBoard(ComponentStatusBoard* board)
|
||||
{
|
||||
status_board_ = board;
|
||||
}
|
||||
|
||||
std::string activePlanner() const override
|
||||
@@ -281,6 +306,18 @@ public:
|
||||
return active_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sau cancelPlan(), lượt đang chạy vẫn chiếm thread cho tới hết latency (mặc định false:
|
||||
* cancel cắt ngay, như plugin có preempt).
|
||||
*
|
||||
* Bật để tái hiện log 2026-09-11: cancel lúc SBPL đang chạy, goal kế bị `startPlan()` từ chối
|
||||
* suốt mấy giây.
|
||||
*/
|
||||
void setCancelKeepsBusy(bool keeps_busy)
|
||||
{
|
||||
cancel_keeps_busy_ = keeps_busy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Số cycle mà một lượt lập plan "mất" trước khi có kết quả.
|
||||
*
|
||||
@@ -356,6 +393,17 @@ private:
|
||||
std::size_t latency_cycles_ = 0;
|
||||
std::size_t cycles_left_ = 0;
|
||||
std::size_t cancel_count_ = 0;
|
||||
bool cancel_keeps_busy_ = false;
|
||||
bool discarded_ = false;
|
||||
ComponentStatusBoard* status_board_ = nullptr;
|
||||
|
||||
void report(ComponentPhase phase)
|
||||
{
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->enter(Component::kGlobalPlanner, phase);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t make_plan_count_ = 0;
|
||||
std::size_t swap_count_ = 0;
|
||||
@@ -438,6 +486,28 @@ public:
|
||||
void setMeasuredVelocity(const robot_geometry_msgs::Twist& velocity) override
|
||||
{
|
||||
measured_velocity_ = velocity;
|
||||
has_measured_velocity_ = true;
|
||||
}
|
||||
|
||||
/// Cổng thật (ControllerRunner) trả false cho tới khi host đẩy odom vào; fake giữ đúng nghĩa đó
|
||||
/// để mọi test không đẩy odom vẫn đi đường "đo bằng lệnh đã phát" như trước.
|
||||
bool measuredVelocity(robot_geometry_msgs::Twist& out) const override
|
||||
{
|
||||
if (!has_measured_velocity_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
out = measured_velocity_;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Giả lập odom: robot đang lăn với @p linear_x [m/s], @p angular_z [rad/s].
|
||||
void setOdometry(double linear_x, double angular_z = 0.0)
|
||||
{
|
||||
robot_geometry_msgs::Twist twist;
|
||||
twist.linear.x = linear_x;
|
||||
twist.angular.z = angular_z;
|
||||
setMeasuredVelocity(twist);
|
||||
}
|
||||
|
||||
void getLocalPlan(robot_nav_2d_msgs::Path2D& plan) override
|
||||
@@ -546,6 +616,7 @@ public:
|
||||
private:
|
||||
robot_nav_2d_msgs::Path2D local_plan_;
|
||||
robot_geometry_msgs::Twist measured_velocity_;
|
||||
bool has_measured_velocity_ = false;
|
||||
double limit_forward_ = 0.0; ///< [m/s]
|
||||
double limit_backward_ = 0.0; ///< [m/s], âm
|
||||
double limit_angular_ = 0.0; ///< [rad/s]
|
||||
@@ -633,6 +704,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
active_ = true;
|
||||
report(ComponentPhase::kBusy);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -647,6 +719,10 @@ public:
|
||||
tick.status = RecoveryTick::Status::kRunning;
|
||||
tick.has_velocity = emits_velocity_;
|
||||
tick.cmd.linear.x = recovery_speed_;
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->progress(Component::kRecovery);
|
||||
}
|
||||
break;
|
||||
case RecoveryScript::kSucceeded:
|
||||
tick.status = RecoveryTick::Status::kSucceeded;
|
||||
@@ -656,15 +732,44 @@ public:
|
||||
tick.path = success_path_;
|
||||
}
|
||||
active_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
break;
|
||||
case RecoveryScript::kFailed:
|
||||
tick.status = RecoveryTick::Status::kFailed;
|
||||
active_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
break;
|
||||
}
|
||||
return tick;
|
||||
}
|
||||
|
||||
/// @brief Ghi hàng `kRecovery` như RecoveryRunner thật. Null = không khai.
|
||||
void attachStatusBoard(ComponentStatusBoard* board)
|
||||
{
|
||||
status_board_ = board;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sau cancel(), behavior vẫn "bận" thêm @p cycles tick — mô phỏng detour worker chưa thoát.
|
||||
* Hàng kRecovery giữ kStopping; test gọi @ref settleCancel để nhả.
|
||||
*/
|
||||
void setCancelKeepsBusy(bool keeps_busy)
|
||||
{
|
||||
cancel_keeps_busy_ = keeps_busy;
|
||||
}
|
||||
|
||||
/// @brief Worker giả đã thoát: hàng về idle.
|
||||
void settleCancel()
|
||||
{
|
||||
stopping_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
}
|
||||
|
||||
bool stopping() const
|
||||
{
|
||||
return stopping_;
|
||||
}
|
||||
|
||||
/// @brief Thành công thì kèm một detour path (như 'detour_path' thật) — cần cho stand-down của kContact.
|
||||
void setSucceedWithPath(std::vector<robot_geometry_msgs::PoseStamped> path)
|
||||
{
|
||||
@@ -675,7 +780,17 @@ public:
|
||||
void cancel() override
|
||||
{
|
||||
++cancel_count_;
|
||||
const bool was_active = active_;
|
||||
active_ = false;
|
||||
if (was_active && cancel_keeps_busy_)
|
||||
{
|
||||
stopping_ = true;
|
||||
report(ComponentPhase::kStopping);
|
||||
}
|
||||
else
|
||||
{
|
||||
report(ComponentPhase::kIdle);
|
||||
}
|
||||
}
|
||||
|
||||
std::string behaviorName(std::size_t index) const override
|
||||
@@ -772,6 +887,18 @@ private:
|
||||
std::size_t last_start_index_ = 0;
|
||||
RecoveryTrigger last_trigger_ = RecoveryTrigger::kPlanningFailed;
|
||||
std::vector<std::size_t> started_indices_;
|
||||
|
||||
bool cancel_keeps_busy_ = false;
|
||||
bool stopping_ = false;
|
||||
ComponentStatusBoard* status_board_ = nullptr;
|
||||
|
||||
void report(ComponentPhase phase)
|
||||
{
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->enter(Component::kRecovery, phase);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -793,6 +920,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
active_ = true;
|
||||
report(ComponentPhase::kBusy);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -801,19 +929,45 @@ public:
|
||||
++update_count_;
|
||||
|
||||
ActionTick tick;
|
||||
if (stopping_)
|
||||
{
|
||||
if (cancel_ticks_left_ > 0)
|
||||
{
|
||||
--cancel_ticks_left_;
|
||||
tick.status = ActionTick::Status::kRunning;
|
||||
tick.message = "moving the device to a safe position";
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->progress(Component::kAction);
|
||||
}
|
||||
return tick;
|
||||
}
|
||||
stopping_ = false;
|
||||
active_ = false;
|
||||
tick.status = ActionTick::Status::kCancelled;
|
||||
tick.message = "device safe";
|
||||
report(ComponentPhase::kIdle);
|
||||
return tick;
|
||||
}
|
||||
switch (nextAction())
|
||||
{
|
||||
case ActionScript::kRunning:
|
||||
tick.status = ActionTick::Status::kRunning;
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->progress(Component::kAction);
|
||||
}
|
||||
break;
|
||||
case ActionScript::kSucceeded:
|
||||
tick.status = ActionTick::Status::kSucceeded;
|
||||
active_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
break;
|
||||
case ActionScript::kFailed:
|
||||
tick.status = ActionTick::Status::kFailed;
|
||||
tick.message = "fake action failed";
|
||||
active_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
break;
|
||||
}
|
||||
return tick;
|
||||
@@ -822,7 +976,56 @@ public:
|
||||
void cancel() override
|
||||
{
|
||||
++cancel_count_;
|
||||
if (active_ && cancel_ticks_needed_ > 0)
|
||||
{
|
||||
// Handler thiết bị (pha 3): giữ "đang dừng" thêm N tick rồi trả kCancelled.
|
||||
cancel_ticks_left_ = cancel_ticks_needed_;
|
||||
stopping_ = true;
|
||||
report(ComponentPhase::kStopping);
|
||||
return;
|
||||
}
|
||||
active_ = false;
|
||||
report(ComponentPhase::kIdle);
|
||||
}
|
||||
|
||||
void pause() override
|
||||
{
|
||||
++pause_count_;
|
||||
if (active_)
|
||||
{
|
||||
report(ComponentPhase::kPaused);
|
||||
}
|
||||
}
|
||||
|
||||
void resume() override
|
||||
{
|
||||
++resume_count_;
|
||||
if (active_)
|
||||
{
|
||||
report(ComponentPhase::kBusy);
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Sau cancel() handler cần @p ticks lượt update() nữa mới trả kCancelled (0 = dừng ngay).
|
||||
void setCancelNeedsTicks(std::size_t ticks)
|
||||
{
|
||||
cancel_ticks_needed_ = ticks;
|
||||
}
|
||||
|
||||
std::size_t pauseCount() const
|
||||
{
|
||||
return pause_count_;
|
||||
}
|
||||
|
||||
std::size_t resumeCount() const
|
||||
{
|
||||
return resume_count_;
|
||||
}
|
||||
|
||||
/// @brief Ghi hàng `kAction` như ActionRunner thật. Null = không khai.
|
||||
void attachStatusBoard(ComponentStatusBoard* board)
|
||||
{
|
||||
status_board_ = board;
|
||||
}
|
||||
|
||||
void setScript(std::vector<ActionScript> script)
|
||||
@@ -885,7 +1088,21 @@ private:
|
||||
std::size_t start_count_ = 0;
|
||||
std::size_t update_count_ = 0;
|
||||
std::size_t cancel_count_ = 0;
|
||||
std::size_t pause_count_ = 0;
|
||||
std::size_t resume_count_ = 0;
|
||||
std::size_t cancel_ticks_needed_ = 0;
|
||||
std::size_t cancel_ticks_left_ = 0;
|
||||
bool stopping_ = false;
|
||||
std::vector<std::string> started_action_types_;
|
||||
ComponentStatusBoard* status_board_ = nullptr;
|
||||
|
||||
void report(ComponentPhase phase)
|
||||
{
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->enter(Component::kAction, phase);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user