feat: run detour planning on a worker thread so cancel doesn't block the control loop
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>
This commit is contained in:
@@ -13,6 +13,8 @@ recovery:
|
||||
- {name: detour_map, type: DetourPathRecovery}
|
||||
- {name: detour_narrow, type: DetourPathRecovery}
|
||||
- {name: detour_alias, type: DetourPathRecovery}
|
||||
- {name: detour_async, type: DetourPathRecovery}
|
||||
- {name: detour_slow, type: DetourPathRecovery}
|
||||
|
||||
detour:
|
||||
planner_name: TestLinePlanner
|
||||
@@ -21,6 +23,7 @@ recovery:
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
async_planning: false # planner giả tức thì — giữ đường đồng bộ để test đếm tick như cũ
|
||||
|
||||
detour_flaky:
|
||||
planner_name: TestFlakyPlanner # hai ứng viên đầu bị "chắn", ứng viên ba mới thông
|
||||
@@ -29,6 +32,7 @@ recovery:
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
async_planning: false # planner giả tức thì — giữ đường đồng bộ để test đếm tick như cũ
|
||||
|
||||
detour_dead:
|
||||
planner_name: TestDeadPlanner # không ứng viên nào thông -> behavior phải kFailed
|
||||
@@ -37,6 +41,7 @@ recovery:
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
async_planning: false # planner giả tức thì — giữ đường đồng bộ để test đếm tick như cũ
|
||||
|
||||
detour_map:
|
||||
planner_name: TestLinePlanner # plan ở frame map, costmap ở odom -> ép đường quy đổi TF
|
||||
@@ -45,6 +50,7 @@ recovery:
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
async_planning: false # planner giả tức thì — giữ đường đồng bộ để test đếm tick như cũ
|
||||
|
||||
# Tên instance KHÁC symbol: đọc param từ `DetourOnlyTuning`, nạp .so theo alias `TestLinePlanner`.
|
||||
# Đây là đường mà cấu hình runtime dùng để cho detour một instance SBPL riêng (allocated_time
|
||||
@@ -57,6 +63,25 @@ recovery:
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
async_planning: false # planner giả tức thì — giữ đường đồng bộ để test đếm tick như cũ
|
||||
# Đường bất đồng bộ (mặc định production): planner giả ngủ 0.3 s, có Preemptable.
|
||||
detour_async:
|
||||
planner_name: TestSlowPlanner
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
cancel_join_timeout: 0.5 # [s]
|
||||
# Cùng planner; instance riêng để test cancel giữa chừng không dính bộ đếm của instance trên.
|
||||
detour_slow:
|
||||
planner_name: TestSlowPlanner
|
||||
planning_distance: 1.0
|
||||
sample_step: 0.5
|
||||
attempts_per_run: 3
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
cancel_join_timeout: 0.5 # [s]
|
||||
|
||||
detour_narrow:
|
||||
planner_name: TestOffsetPlanner # planner giả luôn vẽ đường vòng lệch 1.0 m khỏi tuyến
|
||||
@@ -66,6 +91,7 @@ recovery:
|
||||
max_deviation: 0.2 # [m] chặt hơn độ lệch của planner -> mọi ứng viên bị loại
|
||||
lock_costmap: false
|
||||
costmap_frame_override: odom
|
||||
async_planning: false # planner giả tức thì — giữ đường đồng bộ để test đếm tick như cũ
|
||||
|
||||
# Bảng symbol -> thư viện cho Boost.DLL. Ba symbol planner cùng một .so; param của planner đọc theo
|
||||
# namespace mang tên symbol.
|
||||
@@ -80,6 +106,10 @@ TestDeadPlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
fail_first_attempts: 1000
|
||||
|
||||
TestSlowPlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
plan_delay: 0.3 # [s] một lượt "search" giả
|
||||
|
||||
# Luôn vẽ đường vòng lệch 1.0 m khỏi đoạn thẳng start->goal: dùng để kiểm guard max_deviation.
|
||||
TestOffsetPlanner:
|
||||
library_path: libsbpl_recovery_test_line_planner
|
||||
|
||||
@@ -12,10 +12,12 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
@@ -226,7 +228,7 @@ protected:
|
||||
|
||||
robot::NodeHandle nh;
|
||||
ASSERT_TRUE(registry_.loadFromConfig(nh, "recovery", ctx_));
|
||||
ASSERT_EQ(registry_.size(), 6u);
|
||||
ASSERT_EQ(registry_.size(), 8u); // +detour_async, detour_slow (2026-09-11)
|
||||
}
|
||||
|
||||
FakePoseProvider pose_;
|
||||
@@ -573,6 +575,107 @@ TEST(MergeGrids, OverlayTransformsLocalFrameIntoGridFrame)
|
||||
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Đường bất đồng bộ (2026-09-11): makePlan trên worker thread, control thread chỉ poll; cancel()
|
||||
// cắt được lượt đang chạy qua robot_nav_core::Preemptable.
|
||||
// ================================================================================================
|
||||
|
||||
namespace
|
||||
{
|
||||
/// Poll update() tới khi status khác kRunning-"đang chạy", tối đa @p max_seconds.
|
||||
RecoveryResult pumpUntilSettled(RecoveryBehavior* detour, const robot::Time& t0, double max_seconds,
|
||||
int* ticks_out = nullptr)
|
||||
{
|
||||
const auto deadline = std::chrono::steady_clock::now() +
|
||||
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
||||
std::chrono::duration<double>(max_seconds));
|
||||
int ticks = 0;
|
||||
RecoveryResult result;
|
||||
while (std::chrono::steady_clock::now() < deadline)
|
||||
{
|
||||
result = detour->update(t0);
|
||||
++ticks;
|
||||
if (result.status != RecoveryStatus::kRunning)
|
||||
{
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
if (ticks_out != nullptr)
|
||||
{
|
||||
*ticks_out = ticks;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, AsyncUpdateNeverBlocksAndDeliversThePathLater)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_async");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
|
||||
// Tick đầu khởi động worker và trả về NGAY — planner giả ngủ 300 ms, tick phải < 50 ms.
|
||||
const auto before = std::chrono::steady_clock::now();
|
||||
const RecoveryResult first = detour->update(t0_);
|
||||
const double first_ms =
|
||||
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - before).count();
|
||||
EXPECT_EQ(first.status, RecoveryStatus::kRunning);
|
||||
EXPECT_LT(first_ms, 50.0) << "update() blocked inside makePlan";
|
||||
|
||||
int ticks = 0;
|
||||
const RecoveryResult result = pumpUntilSettled(detour, t0_, 3.0, &ticks);
|
||||
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
EXPECT_GE(ticks, 2) << "the path must arrive on a later tick, not the first";
|
||||
const robot_nav_msgs::Path* path = result.pathOut();
|
||||
ASSERT_NE(path, nullptr);
|
||||
ASSERT_EQ(path->poses.size(), 5u);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, CancelMidSearchReturnsQuicklyAndJoinsTheWorker)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_slow");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
ASSERT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // worker đang ở giữa 300 ms "search"
|
||||
|
||||
const auto before = std::chrono::steady_clock::now();
|
||||
detour->cancel(); // → onCancelRequested(): preempt + join
|
||||
const double cancel_ms =
|
||||
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - before).count();
|
||||
EXPECT_LT(cancel_ms, 100.0) << "cancel must not wait for the fake 300 ms search to finish";
|
||||
|
||||
// Sau cancel behavior phải khởi động lại được ngay (worker cũ đã join), và lượt mới chạy bình thường.
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_)) << "stale worker blocked a new run";
|
||||
const RecoveryResult result = pumpUntilSettled(detour, t0_, 3.0);
|
||||
EXPECT_EQ(result.status, RecoveryStatus::kSucceeded);
|
||||
}
|
||||
|
||||
TEST_F(DetourPathRecoveryTest, CancelledSearchResultIsDiscarded)
|
||||
{
|
||||
RecoveryBehavior* detour = findBehavior(registry_, "detour_slow");
|
||||
ASSERT_NE(detour, nullptr);
|
||||
|
||||
pose_.pose = makePose("odom", 0.0, 0.0);
|
||||
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
||||
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
||||
ASSERT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
||||
detour->cancel();
|
||||
|
||||
// Tick sau cancel: base trả kCancelled (đường onCancel), không được là kSucceeded với path của
|
||||
// lượt đã huỷ.
|
||||
const RecoveryResult after = detour->update(t0_);
|
||||
EXPECT_NE(after.status, RecoveryStatus::kSucceeded);
|
||||
EXPECT_EQ(after.pathOut(), nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
#ifdef SBPL_RECOVERY_TEST_CONFIG_DIR
|
||||
|
||||
@@ -14,8 +14,11 @@
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -23,11 +26,13 @@
|
||||
|
||||
#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
|
||||
class TestLinePlanner final : public robot_nav_core::BaseGlobalPlanner,
|
||||
public robot_nav_core::Preemptable
|
||||
{
|
||||
public:
|
||||
static robot_nav_core::BaseGlobalPlanner::Ptr create()
|
||||
@@ -43,14 +48,38 @@ public:
|
||||
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;
|
||||
@@ -83,7 +112,10 @@ 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
|
||||
@@ -92,3 +124,4 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user