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>
1330 lines
32 KiB
C++
1330 lines
32 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* move_base2 — hiện thực giả của các port, kịch bản hoá bằng chuỗi kết quả định sẵn.
|
|
*
|
|
* Đặt trong test/ của chính move_base2 chứ không đặt trong nav_test_harness: các fake này hiện thực
|
|
* port CỦA move_base2, nếu để trong harness thì harness phải phụ thuộc ngược vào move_base2 và
|
|
* chiều phụ thuộc một chiều bị phá vỡ. Phần fake thực sự dùng chung (đồng hồ, costmap, pose,
|
|
* kiểm va chạm, kịch bản) nằm ở nav_test_harness.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#ifndef MOVE_BASE2_TEST_FAKE_PORTS_H_
|
|
#define MOVE_BASE2_TEST_FAKE_PORTS_H_
|
|
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <limits>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#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>
|
|
#include <move_base2/ports/costmap_status_port.h>
|
|
#include <move_base2/ports/environment_port.h>
|
|
#include <move_base2/ports/mission_port.h>
|
|
#include <move_base2/ports/planner_port.h>
|
|
#include <move_base2/ports/pose_port.h>
|
|
#include <move_base2/ports/recovery_port.h>
|
|
|
|
namespace move_base2
|
|
{
|
|
namespace testing
|
|
{
|
|
|
|
/// @brief Kết quả một lần lập plan trong kịch bản.
|
|
enum class PlannerScript
|
|
{
|
|
kOk, ///< Trả plan hợp lệ.
|
|
kFail, ///< makePlan trả false.
|
|
kEmpty ///< makePlan trả true nhưng plan rỗng — bẫy front()/back() trên vector rỗng.
|
|
};
|
|
|
|
/// @brief Kết quả một lần gọi controller trong kịch bản.
|
|
enum class ControllerScript
|
|
{
|
|
kOk, ///< Sinh lệnh hợp lệ.
|
|
kFail, ///< Không sinh được lệnh.
|
|
kGoalReached, ///< Báo đã tới đích.
|
|
kNaN, ///< Sinh lệnh chứa NaN — phải bị bộ trọng tài chặn.
|
|
kTooFast ///< Sinh lệnh vượt trần vận tốc — phải bị clamp.
|
|
};
|
|
|
|
/// @brief Kết quả một tick recovery trong kịch bản.
|
|
enum class RecoveryScript
|
|
{
|
|
kRunning,
|
|
kSucceeded,
|
|
kFailed
|
|
};
|
|
|
|
/// @brief Kết quả một tick action trong kịch bản (D8).
|
|
enum class ActionScript
|
|
{
|
|
kRunning,
|
|
kSucceeded,
|
|
kFailed
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
/// @brief Đồng hồ do test điều khiển, chuyển tiếp một FakeClock của harness qua ClockPort.
|
|
class FakeClockPort final : public ClockPort
|
|
{
|
|
public:
|
|
explicit FakeClockPort(double start_sec = 1000.0) : now_(start_sec)
|
|
{
|
|
}
|
|
|
|
robot::Time now() const override
|
|
{
|
|
return now_;
|
|
}
|
|
|
|
/// @param seconds [s] Lượng thời gian trôi. Giá trị âm bị bỏ qua.
|
|
void advance(double seconds)
|
|
{
|
|
if (seconds > 0.0)
|
|
{
|
|
now_ = robot::Time(now_.toSec() + seconds);
|
|
}
|
|
}
|
|
|
|
void setTime(double seconds)
|
|
{
|
|
now_ = robot::Time(seconds);
|
|
}
|
|
|
|
private:
|
|
robot::Time now_;
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
class FakePosePort final : public PosePort
|
|
{
|
|
public:
|
|
bool getRobotPose(robot_geometry_msgs::PoseStamped& pose) const override
|
|
{
|
|
++call_count_;
|
|
if (!available_)
|
|
{
|
|
return false;
|
|
}
|
|
pose = pose_;
|
|
return true;
|
|
}
|
|
|
|
/// @param x,y [m]
|
|
void setPosition(double x, double y)
|
|
{
|
|
pose_.header.frame_id = "map";
|
|
pose_.pose.position.x = x;
|
|
pose_.pose.position.y = y;
|
|
pose_.pose.orientation.w = 1.0;
|
|
}
|
|
|
|
/// @param yaw [rad] hướng robot; cần cho mọi chặng quy từ pose tương đối.
|
|
void setYaw(double yaw)
|
|
{
|
|
pose_.header.frame_id = "map";
|
|
pose_.pose.orientation.x = 0.0;
|
|
pose_.pose.orientation.y = 0.0;
|
|
pose_.pose.orientation.z = std::sin(0.5 * yaw);
|
|
pose_.pose.orientation.w = std::cos(0.5 * yaw);
|
|
}
|
|
|
|
/// @brief false = mô phỏng TF thiếu/stale.
|
|
void setAvailable(bool available)
|
|
{
|
|
available_ = available;
|
|
}
|
|
|
|
/**
|
|
* @brief Đăng một frame mà @ref lookupPose tra được.
|
|
* @param x,y [m] ; @param yaw [rad] hướng của CHÍNH frame đó, không phải của robot.
|
|
*/
|
|
void setFramePose(const std::string& frame, double x, double y, double yaw)
|
|
{
|
|
robot_geometry_msgs::PoseStamped pose;
|
|
pose.header.frame_id = "map";
|
|
pose.pose.position.x = x;
|
|
pose.pose.position.y = y;
|
|
pose.pose.orientation.z = std::sin(0.5 * yaw);
|
|
pose.pose.orientation.w = std::cos(0.5 * yaw);
|
|
frames_[frame] = pose;
|
|
}
|
|
|
|
bool lookupPose(const std::string& frame,
|
|
robot_geometry_msgs::PoseStamped& pose) const override
|
|
{
|
|
const auto it = frames_.find(frame);
|
|
if (it == frames_.end())
|
|
{
|
|
return false;
|
|
}
|
|
pose = it->second;
|
|
return true;
|
|
}
|
|
|
|
std::size_t callCount() const
|
|
{
|
|
return call_count_;
|
|
}
|
|
|
|
private:
|
|
robot_geometry_msgs::PoseStamped pose_;
|
|
std::map<std::string, robot_geometry_msgs::PoseStamped> frames_;
|
|
bool available_ = true;
|
|
mutable std::size_t call_count_ = 0;
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
class FakePlannerPort final : public PlannerPort
|
|
{
|
|
public:
|
|
bool swapPlanner(const std::string& planner_name) override
|
|
{
|
|
if (!swap_succeeds_)
|
|
{
|
|
return false;
|
|
}
|
|
active_ = planner_name;
|
|
++swap_count_;
|
|
return true;
|
|
}
|
|
|
|
bool startPlan(const robot_geometry_msgs::PoseStamped& /*start*/,
|
|
const robot_geometry_msgs::PoseStamped& goal,
|
|
const robot_protocol_msgs::Order* order, std::uint64_t tag) override
|
|
{
|
|
if (in_flight_)
|
|
{
|
|
return false; // Đúng như PlannerRunner: một lượt tại một thời điểm.
|
|
}
|
|
|
|
++make_plan_count_;
|
|
saw_order_ = saw_order_ || order != nullptr;
|
|
order_history_.push_back(order != nullptr);
|
|
|
|
in_flight_ = true;
|
|
pending_tag_ = tag;
|
|
pending_goal_ = goal;
|
|
cycles_left_ = latency_cycles_;
|
|
pending_action_ = nextAction();
|
|
report(ComponentPhase::kBusy);
|
|
return true;
|
|
}
|
|
|
|
bool isPlanning() const override
|
|
{
|
|
return in_flight_;
|
|
}
|
|
|
|
/// @brief Goal mà lõi đã trao cho lượt lập plan gần nhất — đích SAU khi đã quy từ dạng tương đối.
|
|
const robot_geometry_msgs::PoseStamped& pendingGoal() const
|
|
{
|
|
return pending_goal_;
|
|
}
|
|
|
|
bool pollPlan(PlanResult& result) override
|
|
{
|
|
if (!in_flight_)
|
|
{
|
|
return false;
|
|
}
|
|
if (cycles_left_ > 0)
|
|
{
|
|
--cycles_left_;
|
|
return false; // Còn "đang tính" — bên gọi phải thấy kBusy.
|
|
}
|
|
|
|
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();
|
|
|
|
switch (pending_action_)
|
|
{
|
|
case PlannerScript::kFail:
|
|
result.succeeded = false;
|
|
return true;
|
|
case PlannerScript::kEmpty:
|
|
// Contract nói thành công phải kèm plan không rỗng; fake cố ý vi phạm để kiểm guard của
|
|
// bên gọi.
|
|
result.succeeded = true;
|
|
return true;
|
|
case PlannerScript::kOk:
|
|
break;
|
|
}
|
|
|
|
result.succeeded = true;
|
|
result.plan.push_back(pending_goal_);
|
|
return true;
|
|
}
|
|
|
|
void cancelPlan() override
|
|
{
|
|
++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
|
|
{
|
|
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ả.
|
|
*
|
|
* 0 (mặc định) = kết quả có ngay ở lần poll kế tiếp, tức đúng nhịp của bản lập plan đồng bộ cũ:
|
|
* kick ở cuối cycle N, state machine thấy plan ở cycle N+1. Nhờ vậy mọi test viết cho bản đồng bộ
|
|
* giữ nguyên ý nghĩa.
|
|
*/
|
|
void setLatencyCycles(std::size_t cycles)
|
|
{
|
|
latency_cycles_ = cycles;
|
|
}
|
|
|
|
std::size_t cancelCount() const
|
|
{
|
|
return cancel_count_;
|
|
}
|
|
|
|
void setScript(std::vector<PlannerScript> script)
|
|
{
|
|
script_ = std::move(script);
|
|
index_ = 0;
|
|
}
|
|
|
|
void setSwapSucceeds(bool succeeds)
|
|
{
|
|
swap_succeeds_ = succeeds;
|
|
}
|
|
|
|
std::size_t makePlanCount() const
|
|
{
|
|
return make_plan_count_;
|
|
}
|
|
|
|
std::size_t swapCount() const
|
|
{
|
|
return swap_count_;
|
|
}
|
|
|
|
bool sawOrder() const
|
|
{
|
|
return saw_order_;
|
|
}
|
|
|
|
const std::vector<bool>& orderHistory() const
|
|
{
|
|
return order_history_;
|
|
}
|
|
|
|
private:
|
|
/// Hết kịch bản thì giữ kết quả cuối; kịch bản rỗng thì luôn thành công.
|
|
PlannerScript nextAction()
|
|
{
|
|
if (script_.empty())
|
|
{
|
|
return PlannerScript::kOk;
|
|
}
|
|
if (index_ >= script_.size())
|
|
{
|
|
return script_.back();
|
|
}
|
|
return script_[index_++];
|
|
}
|
|
|
|
std::vector<PlannerScript> script_;
|
|
std::size_t index_ = 0;
|
|
std::string active_;
|
|
bool swap_succeeds_ = true;
|
|
|
|
bool in_flight_ = false;
|
|
std::uint64_t pending_tag_ = 0;
|
|
robot_geometry_msgs::PoseStamped pending_goal_;
|
|
PlannerScript pending_action_ = PlannerScript::kOk;
|
|
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;
|
|
bool saw_order_ = false;
|
|
std::vector<bool> order_history_;
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
class FakeControllerPort final : public ControllerPort
|
|
{
|
|
public:
|
|
bool swapPlanner(const std::string& planner_name) override
|
|
{
|
|
if (!swap_succeeds_)
|
|
{
|
|
return false;
|
|
}
|
|
active_ = planner_name;
|
|
return true;
|
|
}
|
|
|
|
bool setDockingMarker(const std::string& marker) override
|
|
{
|
|
last_docking_marker_ = marker;
|
|
return docking_marker_succeeds_;
|
|
}
|
|
|
|
void setDockingMarkerSucceeds(bool succeeds)
|
|
{
|
|
docking_marker_succeeds_ = succeeds;
|
|
}
|
|
|
|
/// @brief Marker của lời gọi setDockingMarker gần nhất; rỗng nếu chưa từng gọi.
|
|
const std::string& lastDockingMarker() const
|
|
{
|
|
return last_docking_marker_;
|
|
}
|
|
|
|
bool setPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan) override
|
|
{
|
|
++set_plan_count_;
|
|
last_plan_size_ = plan.size();
|
|
return set_plan_succeeds_ && !plan.empty();
|
|
}
|
|
|
|
bool computeVelocityCommands(robot_geometry_msgs::Twist& cmd) override
|
|
{
|
|
++compute_count_;
|
|
switch (current_action_)
|
|
{
|
|
case ControllerScript::kFail:
|
|
return false;
|
|
case ControllerScript::kNaN:
|
|
cmd.linear.x = std::numeric_limits<double>::quiet_NaN();
|
|
cmd.angular.z = 0.0;
|
|
return true;
|
|
case ControllerScript::kTooFast:
|
|
cmd.linear.x = 99.0;
|
|
cmd.angular.z = 99.0;
|
|
return true;
|
|
case ControllerScript::kGoalReached:
|
|
case ControllerScript::kOk:
|
|
break;
|
|
}
|
|
cmd.linear.x = nominal_speed_;
|
|
cmd.angular.z = nominal_turn_rate_;
|
|
return true;
|
|
}
|
|
|
|
bool isGoalReached() override
|
|
{
|
|
// Lấy hành động cho cycle này ở đây vì đây là lời gọi ĐẦU TIÊN của một cycle controller, đúng
|
|
// thứ tự mà control loop dùng.
|
|
current_action_ = nextAction();
|
|
++goal_check_count_;
|
|
return current_action_ == ControllerScript::kGoalReached;
|
|
}
|
|
|
|
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
|
|
{
|
|
plan = local_plan_;
|
|
}
|
|
|
|
/// @brief Đặt quỹ đạo cục bộ mà fake sẽ trả về.
|
|
void setLocalPlan(const robot_nav_2d_msgs::Path2D& plan)
|
|
{
|
|
local_plan_ = plan;
|
|
}
|
|
|
|
bool setTwistLinear(const robot_geometry_msgs::Vector3& linear) override
|
|
{
|
|
if (linear.x < 0.0)
|
|
{
|
|
limit_backward_ = linear.x; // [m/s], âm
|
|
}
|
|
else
|
|
{
|
|
limit_forward_ = linear.x; // [m/s]
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool setTwistAngular(const robot_geometry_msgs::Vector3& angular) override
|
|
{
|
|
limit_angular_ = angular.z; // [rad/s]
|
|
return true;
|
|
}
|
|
|
|
const robot_geometry_msgs::Twist& measuredVelocity() const
|
|
{
|
|
return measured_velocity_;
|
|
}
|
|
|
|
double limitForward() const
|
|
{
|
|
return limit_forward_;
|
|
}
|
|
|
|
double limitBackward() const
|
|
{
|
|
return limit_backward_;
|
|
}
|
|
|
|
double limitAngular() const
|
|
{
|
|
return limit_angular_;
|
|
}
|
|
|
|
std::string activeController() const override
|
|
{
|
|
return active_;
|
|
}
|
|
|
|
void setScript(std::vector<ControllerScript> script)
|
|
{
|
|
script_ = std::move(script);
|
|
index_ = 0;
|
|
}
|
|
|
|
void setSwapSucceeds(bool succeeds)
|
|
{
|
|
swap_succeeds_ = succeeds;
|
|
}
|
|
|
|
void setSetPlanSucceeds(bool succeeds)
|
|
{
|
|
set_plan_succeeds_ = succeeds;
|
|
}
|
|
|
|
/// @param speed [m/s] Tốc độ dài của lệnh khi kịch bản là kOk.
|
|
void setNominalSpeed(double speed)
|
|
{
|
|
nominal_speed_ = speed;
|
|
}
|
|
|
|
/// @param omega [rad/s] Tốc độ góc của lệnh khi kịch bản là kOk (0 = đi thẳng).
|
|
void setNominalTurnRate(double omega)
|
|
{
|
|
nominal_turn_rate_ = omega;
|
|
}
|
|
|
|
std::size_t setPlanCount() const
|
|
{
|
|
return set_plan_count_;
|
|
}
|
|
|
|
std::size_t computeCount() const
|
|
{
|
|
return compute_count_;
|
|
}
|
|
|
|
std::size_t goalCheckCount() const
|
|
{
|
|
return goal_check_count_;
|
|
}
|
|
|
|
std::size_t lastPlanSize() const
|
|
{
|
|
return last_plan_size_;
|
|
}
|
|
|
|
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]
|
|
|
|
ControllerScript nextAction()
|
|
{
|
|
if (script_.empty())
|
|
{
|
|
return ControllerScript::kOk;
|
|
}
|
|
if (index_ >= script_.size())
|
|
{
|
|
return script_.back();
|
|
}
|
|
return script_[index_++];
|
|
}
|
|
|
|
std::vector<ControllerScript> script_;
|
|
std::size_t index_ = 0;
|
|
ControllerScript current_action_ = ControllerScript::kOk;
|
|
|
|
std::string active_;
|
|
bool swap_succeeds_ = true;
|
|
bool set_plan_succeeds_ = true;
|
|
bool docking_marker_succeeds_ = true;
|
|
std::string last_docking_marker_;
|
|
double nominal_speed_ = 0.3; ///< [m/s]
|
|
double nominal_turn_rate_ = 0.0; ///< [rad/s]
|
|
|
|
std::size_t set_plan_count_ = 0;
|
|
std::size_t compute_count_ = 0;
|
|
std::size_t goal_check_count_ = 0;
|
|
std::size_t last_plan_size_ = 0;
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
class FakeRecoveryPort final : public RecoveryPort
|
|
{
|
|
public:
|
|
explicit FakeRecoveryPort(std::size_t behavior_count = 2) : behavior_count_(behavior_count)
|
|
{
|
|
}
|
|
|
|
bool configure(robot::NodeHandle& /*nh*/) override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
std::size_t behaviorCount() const override
|
|
{
|
|
return behavior_count_;
|
|
}
|
|
|
|
RecoveryOutputKind outputKind(std::size_t index) const override
|
|
{
|
|
if (index >= behavior_count_)
|
|
{
|
|
return RecoveryOutputKind::kNone;
|
|
}
|
|
const auto it = output_kinds_.find(index);
|
|
return it == output_kinds_.end() ? default_output_kind_ : it->second;
|
|
}
|
|
|
|
/// @brief Đặt họ output cho behavior thứ @p index (mặc định mọi behavior đều lái robot).
|
|
void setOutputKind(std::size_t index, RecoveryOutputKind kind)
|
|
{
|
|
output_kinds_[index] = kind;
|
|
}
|
|
|
|
void setDefaultOutputKind(RecoveryOutputKind kind)
|
|
{
|
|
default_output_kind_ = kind;
|
|
}
|
|
|
|
bool start(std::size_t index, RecoveryTrigger trigger) override
|
|
{
|
|
++start_count_;
|
|
last_start_index_ = index;
|
|
last_trigger_ = trigger;
|
|
started_indices_.push_back(index);
|
|
|
|
if (index >= behavior_count_ || !start_succeeds_)
|
|
{
|
|
return false;
|
|
}
|
|
active_ = true;
|
|
report(ComponentPhase::kBusy);
|
|
return true;
|
|
}
|
|
|
|
RecoveryTick update() override
|
|
{
|
|
++update_count_;
|
|
|
|
RecoveryTick tick;
|
|
switch (nextAction())
|
|
{
|
|
case RecoveryScript::kRunning:
|
|
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;
|
|
if (succeed_with_path_)
|
|
{
|
|
tick.has_path = true;
|
|
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)
|
|
{
|
|
succeed_with_path_ = true;
|
|
success_path_ = std::move(path);
|
|
}
|
|
|
|
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
|
|
{
|
|
return index < behavior_count_ ? "fake_behavior_" + std::to_string(index) : std::string();
|
|
}
|
|
|
|
void setScript(std::vector<RecoveryScript> script)
|
|
{
|
|
script_ = std::move(script);
|
|
index_ = 0;
|
|
}
|
|
|
|
void setStartSucceeds(bool succeeds)
|
|
{
|
|
start_succeeds_ = succeeds;
|
|
}
|
|
|
|
/// @param speed [m/s] Vận tốc behavior phát khi đang chạy. Dấu âm nghĩa là lùi.
|
|
void setRecoveryVelocity(bool emits, double speed)
|
|
{
|
|
emits_velocity_ = emits;
|
|
recovery_speed_ = speed;
|
|
}
|
|
|
|
std::size_t startCount() const
|
|
{
|
|
return start_count_;
|
|
}
|
|
|
|
std::size_t updateCount() const
|
|
{
|
|
return update_count_;
|
|
}
|
|
|
|
std::size_t cancelCount() const
|
|
{
|
|
return cancel_count_;
|
|
}
|
|
|
|
std::size_t lastStartIndex() const
|
|
{
|
|
return last_start_index_;
|
|
}
|
|
|
|
RecoveryTrigger lastTrigger() const
|
|
{
|
|
return last_trigger_;
|
|
}
|
|
|
|
const std::vector<std::size_t>& startedIndices() const
|
|
{
|
|
return started_indices_;
|
|
}
|
|
|
|
bool active() const
|
|
{
|
|
return active_;
|
|
}
|
|
|
|
private:
|
|
RecoveryScript nextAction()
|
|
{
|
|
if (script_.empty())
|
|
{
|
|
return RecoveryScript::kSucceeded;
|
|
}
|
|
if (index_ >= script_.size())
|
|
{
|
|
return script_.back();
|
|
}
|
|
return script_[index_++];
|
|
}
|
|
|
|
std::size_t behavior_count_;
|
|
std::vector<RecoveryScript> script_;
|
|
std::size_t index_ = 0;
|
|
|
|
bool start_succeeds_ = true;
|
|
bool succeed_with_path_ = false;
|
|
std::vector<robot_geometry_msgs::PoseStamped> success_path_;
|
|
bool active_ = false;
|
|
bool emits_velocity_ = false;
|
|
double recovery_speed_ = -0.1; ///< [m/s], âm = lùi
|
|
|
|
/// Mặc định coi mọi behavior đều lái robot — giữ nguyên hành vi của các test viết trước khi
|
|
/// RecoveryPort có outputKind().
|
|
RecoveryOutputKind default_output_kind_ = RecoveryOutputKind::kVelocity;
|
|
std::map<std::size_t, RecoveryOutputKind> output_kinds_;
|
|
|
|
std::size_t start_count_ = 0;
|
|
std::size_t update_count_ = 0;
|
|
std::size_t cancel_count_ = 0;
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
class FakeActionPort final : public ActionPort
|
|
{
|
|
public:
|
|
bool configure(robot::NodeHandle& /*nh*/) override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bool start(const robot_protocol_msgs::Action& action) override
|
|
{
|
|
++start_count_;
|
|
started_action_types_.push_back(action.actionType);
|
|
if (!start_succeeds_)
|
|
{
|
|
return false;
|
|
}
|
|
active_ = true;
|
|
report(ComponentPhase::kBusy);
|
|
return true;
|
|
}
|
|
|
|
ActionTick update() override
|
|
{
|
|
++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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
script_ = std::move(script);
|
|
index_ = 0;
|
|
}
|
|
|
|
void setStartSucceeds(bool succeeds)
|
|
{
|
|
start_succeeds_ = succeeds;
|
|
}
|
|
|
|
std::size_t startCount() const
|
|
{
|
|
return start_count_;
|
|
}
|
|
|
|
std::size_t updateCount() const
|
|
{
|
|
return update_count_;
|
|
}
|
|
|
|
std::size_t cancelCount() const
|
|
{
|
|
return cancel_count_;
|
|
}
|
|
|
|
/// @brief actionType của từng lần start, theo thứ tự — kiểm "actions đi nguyên vẹn, đúng thứ tự".
|
|
const std::vector<std::string>& startedActionTypes() const
|
|
{
|
|
return started_action_types_;
|
|
}
|
|
|
|
bool active() const
|
|
{
|
|
return active_;
|
|
}
|
|
|
|
private:
|
|
ActionScript nextAction()
|
|
{
|
|
if (script_.empty())
|
|
{
|
|
return ActionScript::kSucceeded;
|
|
}
|
|
if (index_ >= script_.size())
|
|
{
|
|
return script_.back();
|
|
}
|
|
return script_[index_++];
|
|
}
|
|
|
|
std::vector<ActionScript> script_;
|
|
std::size_t index_ = 0;
|
|
|
|
bool start_succeeds_ = true;
|
|
bool active_ = false;
|
|
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @class FakeCostmapStatusPort
|
|
* @brief Costmap "còn hạn / hết hạn" bật tắt được, cho guard không-đi-mù.
|
|
*
|
|
* Mặc định **còn hạn**: một cổng giả im lặng chặn robot sẽ làm mọi test khác fail vì lý do không
|
|
* liên quan tới thứ chúng đang kiểm.
|
|
*/
|
|
class FakeCostmapStatusPort final : public CostmapStatusPort
|
|
{
|
|
public:
|
|
bool isCurrent() const override
|
|
{
|
|
++query_count_;
|
|
return current_;
|
|
}
|
|
|
|
void setCurrent(bool current)
|
|
{
|
|
current_ = current;
|
|
}
|
|
|
|
std::size_t queryCount() const
|
|
{
|
|
return query_count_;
|
|
}
|
|
|
|
private:
|
|
bool current_ = true;
|
|
mutable std::size_t query_count_ = 0;
|
|
};
|
|
|
|
/**
|
|
* @class FakeEnvironmentPort
|
|
* @brief Cổng môi trường giả cho EnvMonitor: kịch bản quyết định cost_to_go / tiếp xúc, không cần costmap.
|
|
*
|
|
* Mặc định "không kết luận được" cho hình học và quỹ đạo (như cổng gốc thiếu costmap/TF) nhưng CÓ thân
|
|
* (footprint) — để các test cũ chạy với EnvMonitor bật mà không sinh verdict nào.
|
|
*/
|
|
class FakeEnvironmentPort final : public EnvironmentPort
|
|
{
|
|
public:
|
|
bool sampleGeometry(const EnvGeometryRequest& request, EnvGeometry& out) override
|
|
{
|
|
(void)request;
|
|
++geometry_query_count_;
|
|
if (!geometry_available_)
|
|
{
|
|
return false;
|
|
}
|
|
out = EnvGeometry();
|
|
out.valid = true;
|
|
out.free_radius = body_side_;
|
|
out.cost.conclusive = true;
|
|
out.cost.reachable = reachable_;
|
|
out.cost.cost_to_go = reachable_ ? cost_to_go_ : std::numeric_limits<double>::infinity();
|
|
out.edt_body_min = edt_body_min_;
|
|
return true;
|
|
}
|
|
|
|
bool sweepTrajectory(const EnvTrajectoryRequest& request, SweepResult& out, bool& used_local_plan) override
|
|
{
|
|
(void)request;
|
|
++sweep_query_count_;
|
|
used_local_plan = false;
|
|
if (!sweep_available_)
|
|
{
|
|
return false;
|
|
}
|
|
out = SweepResult();
|
|
out.conclusive = true;
|
|
out.scanned = 1;
|
|
out.blocked = contact_blocked_;
|
|
out.distance = contact_blocked_ ? contact_distance_ : 0.0;
|
|
return true;
|
|
}
|
|
|
|
bool robotBodyExtents(double& front, double& rear, double& side) const override
|
|
{
|
|
if (!body_available_)
|
|
{
|
|
return false;
|
|
}
|
|
front = body_front_;
|
|
rear = body_rear_;
|
|
side = body_side_;
|
|
return true;
|
|
}
|
|
|
|
/// @param distance [m] quãng dọc quỹ đạo tới điểm chạm.
|
|
void setContact(bool blocked, double distance = 0.1)
|
|
{
|
|
sweep_available_ = true;
|
|
contact_blocked_ = blocked;
|
|
contact_distance_ = distance;
|
|
}
|
|
|
|
void setSweepAvailable(bool available)
|
|
{
|
|
sweep_available_ = available;
|
|
}
|
|
|
|
/// @param cost_to_go [m] chi phí còn lại; @p edt_body_min [m] khoảng trống thân.
|
|
void setGeometry(bool reachable, double cost_to_go, double edt_body_min = 1.0)
|
|
{
|
|
geometry_available_ = true;
|
|
reachable_ = reachable;
|
|
cost_to_go_ = cost_to_go;
|
|
edt_body_min_ = edt_body_min;
|
|
}
|
|
|
|
void setGeometryAvailable(bool available)
|
|
{
|
|
geometry_available_ = available;
|
|
}
|
|
|
|
void setBodyExtents(double front, double rear, double side)
|
|
{
|
|
body_available_ = true;
|
|
body_front_ = front;
|
|
body_rear_ = rear;
|
|
body_side_ = side;
|
|
}
|
|
|
|
void setBodyAvailable(bool available)
|
|
{
|
|
body_available_ = available;
|
|
}
|
|
|
|
std::size_t sweepQueryCount() const
|
|
{
|
|
return sweep_query_count_;
|
|
}
|
|
|
|
std::size_t geometryQueryCount() const
|
|
{
|
|
return geometry_query_count_;
|
|
}
|
|
|
|
private:
|
|
bool geometry_available_ = false;
|
|
bool reachable_ = true;
|
|
double cost_to_go_ = 10.0; // [m]
|
|
double edt_body_min_ = 1.0; // [m]
|
|
bool sweep_available_ = false;
|
|
bool contact_blocked_ = false;
|
|
double contact_distance_ = 0.1; // [m]
|
|
bool body_available_ = true;
|
|
double body_front_ = 0.58; // [m]
|
|
double body_rear_ = 0.58; // [m]
|
|
double body_side_ = 0.48; // [m]
|
|
std::size_t geometry_query_count_ = 0;
|
|
std::size_t sweep_query_count_ = 0;
|
|
};
|
|
|
|
class FakeMissionPort final : public MissionPort
|
|
{
|
|
public:
|
|
void setRequestCallback(RequestCallback callback) override
|
|
{
|
|
callback_ = std::move(callback);
|
|
}
|
|
|
|
void reportOutcome(std::uint64_t mission_sequence_id, NavigationOutcome outcome) override
|
|
{
|
|
reports_.emplace_back(mission_sequence_id, outcome);
|
|
}
|
|
|
|
bool hasActiveMission() const override
|
|
{
|
|
return active_;
|
|
}
|
|
|
|
void start() override
|
|
{
|
|
active_ = true;
|
|
}
|
|
|
|
void stop() override
|
|
{
|
|
active_ = false;
|
|
}
|
|
|
|
/// @brief Giả lập mission layer đẩy một chặng xuống.
|
|
void emit(const NavigationRequest& request)
|
|
{
|
|
if (callback_)
|
|
{
|
|
callback_(request);
|
|
}
|
|
}
|
|
|
|
const std::vector<std::pair<std::uint64_t, NavigationOutcome>>& reports() const
|
|
{
|
|
return reports_;
|
|
}
|
|
|
|
/// @brief Số lần đã báo kết quả cho một sequence id — bất biến là phải bằng 1.
|
|
std::size_t reportCountFor(std::uint64_t mission_sequence_id) const
|
|
{
|
|
std::size_t count = 0;
|
|
for (const auto& report : reports_)
|
|
{
|
|
if (report.first == mission_sequence_id)
|
|
{
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
private:
|
|
RequestCallback callback_;
|
|
std::vector<std::pair<std::uint64_t, NavigationOutcome>> reports_;
|
|
bool active_ = false;
|
|
};
|
|
|
|
} // namespace testing
|
|
} // namespace move_base2
|
|
|
|
#endif // MOVE_BASE2_TEST_FAKE_PORTS_H_
|