DetourPathRecovery was the one recovery behavior that mattered for the onCancelRequested() fix in recovery_core (commit 254c7fc): it calls SBPLDetourPlanner::makePlan() synchronously from update(), which runs on the control thread — cancel arriving mid-search had nothing to preempt it with, and the control loop itself would stall for the remainder of allocated_time. - Moves the search to a per-attempt worker thread (startWorker/workerBody/ planInto/finishAttempt/waitWorker/joinWorker/requestWorkerStop); update() only polls. - onCancelRequested() (new override): preempts via robot_nav_core::Preemptable if the inner planner supports it (see Test/sbpl_lattice_planner), then waits up to cancel_join_timeout (0.2 s) and joins — cancel now actually blocks until the worker is gone, not just until a flag is set. - onStart() joins a stale worker from a previous attempt before starting a new one; destructor joins too. - async_planning param (default true) so tests can force the old synchronous path when they need deterministic single-thread timing. - Uses RECOVERY_CORE_EXPORT_BEHAVIOR for the ABI version symbol. - test/plugins/test_line_planner.cpp: added plan_delay (simulate a slow search) and made it Preemptable, exported as TestSlowPlanner, to drive the new worker-thread cancel tests. Depends on recovery_core commit 254c7fc (onCancelRequested hook + ABI macro) and Test/sbpl_lattice_planner commit 7456e99 (Preemptable pattern this follows). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
128 lines
4.8 KiB
C++
128 lines
4.8 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* sbpl_recovery — global planner GIẢ cho unit test: nối thẳng start->goal.
|
|
*
|
|
* Không đụng costmap (test truyền placeholder không dereference được). Kịch bản lỗi điều khiển
|
|
* bằng param `fail_first_attempts`: N lời gọi makePlan đầu tiên trả false — mô phỏng "ứng viên
|
|
* điểm nối gần bị chắn, ứng viên xa hơn mới thông".
|
|
*
|
|
* Export hai alias từ cùng một factory: `TestLinePlanner` (luôn thành công) và `TestFlakyPlanner`
|
|
* (đọc param theo tên riêng của nó) — mỗi symbol đọc param từ namespace mang tên chính nó.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <memory>
|
|
#include <thread>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <boost/dll/alias.hpp>
|
|
|
|
#include <robot/node_handle.h>
|
|
#include <robot_nav_core/base_global_planner.h>
|
|
#include <robot_nav_core/preemptable.h>
|
|
|
|
namespace sbpl_recovery_test
|
|
{
|
|
|
|
class TestLinePlanner final : public robot_nav_core::BaseGlobalPlanner,
|
|
public robot_nav_core::Preemptable
|
|
{
|
|
public:
|
|
static robot_nav_core::BaseGlobalPlanner::Ptr create()
|
|
{
|
|
return std::make_shared<TestLinePlanner>();
|
|
}
|
|
|
|
bool initialize(std::string name, robot_costmap_2d::Costmap2DROBOT* /*costmap*/) override
|
|
{
|
|
// Costmap là placeholder trong test — TUYỆT ĐỐI không dereference.
|
|
robot::NodeHandle root;
|
|
robot::NodeHandle nh(root, name);
|
|
nh.param("fail_first_attempts", fail_first_attempts_, 0);
|
|
nh.param("points", points_, 5);
|
|
nh.param("lateral_offset", lateral_offset_, 0.0);
|
|
// [s] Giả lập một lượt search dài: ngủ từng lát 5 ms và kiểm cờ preempt — mô hình đúng của
|
|
// ARA* với abort flag (poll mỗi expand). 0 = tức thì.
|
|
nh.param("plan_delay", plan_delay_, 0.0);
|
|
return true;
|
|
}
|
|
|
|
void preempt() override
|
|
{
|
|
preempt_.store(true, std::memory_order_relaxed);
|
|
}
|
|
|
|
bool makePlan(const robot_geometry_msgs::PoseStamped& start,
|
|
const robot_geometry_msgs::PoseStamped& goal,
|
|
std::vector<robot_geometry_msgs::PoseStamped>& plan) override
|
|
{
|
|
plan.clear();
|
|
preempt_.store(false, std::memory_order_relaxed);
|
|
if (plan_delay_ > 0.0)
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() +
|
|
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
|
std::chrono::duration<double>(plan_delay_));
|
|
while (std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
if (preempt_.load(std::memory_order_relaxed))
|
|
{
|
|
++preempted_calls_;
|
|
return false;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
|
}
|
|
}
|
|
if (calls_made_++ < fail_first_attempts_)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// `lateral_offset` đẩy các điểm GIỮA sang bên theo pháp tuyến của đoạn start->goal, giữ nguyên
|
|
// hai đầu. Đây là hình dạng của một đường vòng thật, và là thứ guard `max_deviation` phải đo.
|
|
const double dx = goal.pose.position.x - start.pose.position.x;
|
|
const double dy = goal.pose.position.y - start.pose.position.y;
|
|
const double length = std::hypot(dx, dy);
|
|
const double nx = length > 1e-9 ? -dy / length : 0.0;
|
|
const double ny = length > 1e-9 ? dx / length : 0.0;
|
|
|
|
for (int i = 0; i < points_; ++i)
|
|
{
|
|
const double ratio = points_ > 1 ? static_cast<double>(i) / (points_ - 1) : 1.0;
|
|
// Hình chuông: 0 ở hai đầu, lớn nhất ở giữa.
|
|
const double bulge = lateral_offset_ * std::sin(ratio * M_PI);
|
|
|
|
robot_geometry_msgs::PoseStamped pose = start;
|
|
pose.pose.position.x = start.pose.position.x + ratio * dx + bulge * nx;
|
|
pose.pose.position.y = start.pose.position.y + ratio * dy + bulge * ny;
|
|
pose.pose.orientation = goal.pose.orientation;
|
|
plan.push_back(pose);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
int fail_first_attempts_ = 0;
|
|
int points_ = 5;
|
|
double lateral_offset_ = 0.0; ///< [m] độ phình ngang lớn nhất so với đoạn start->goal.
|
|
double plan_delay_ = 0.0; ///< [s] thời gian giả lập một lượt search.
|
|
int calls_made_ = 0;
|
|
int preempted_calls_ = 0;
|
|
std::atomic<bool> preempt_{false};
|
|
};
|
|
|
|
} // namespace sbpl_recovery_test
|
|
|
|
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestLinePlanner)
|
|
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestFlakyPlanner)
|
|
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestDeadPlanner)
|
|
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestOffsetPlanner)
|
|
BOOST_DLL_ALIAS(sbpl_recovery_test::TestLinePlanner::create, TestSlowPlanner)
|