feat(move_base2): direction zone delivery and costmap readiness wait
- SensorGateway::pushStaticMap also dispatches to DIRECTIONAL_LAYER on the global costmap, before STATIC_LAYER: with a same-geometry map the directional layer goes PENDING before StaticLayer can report current, so a plan cannot pair the new map with the old zones. - Static maps are replayed in arrival order instead of topic order, so a direction grid is never replayed before its static map. - PlannerRunner checks every enabled global layer under the same lock as the snapshot. While a layer is not current it keeps the goal, reports waiting_for_costmap, logs the stale layers (throttled) and spends no planner_patience or watchdog budget. After costmap_ready_timeout [s] (default 10, 0 = legacy behaviour) the attempt fails as before. The key is added to config/runtime/move_base_common_params.yaml. - Test fixture destroys the server before its fake ports (the control thread could call a destroyed port). Full history: branch feature/direction-zone.
This commit is contained in:
@@ -8,7 +8,9 @@
|
||||
*********************************************************************/
|
||||
#include <move_base2/runners/planner_runner.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <exception>
|
||||
#include <utility>
|
||||
|
||||
@@ -19,12 +21,51 @@
|
||||
#include <robot/plugin_loader_helper.h>
|
||||
#include <robot/robot.h>
|
||||
#include <robot_costmap_2d/costmap_2d_robot.h>
|
||||
#include <robot_costmap_2d/direction_field.h>
|
||||
#include <robot_costmap_2d/layer.h>
|
||||
#include <robot_costmap_2d/layered_costmap.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
namespace
|
||||
{
|
||||
|
||||
// Called with the master mutex held. Direction providers own a separate mutex, so their
|
||||
// snapshot is the race-free equivalent of Layer::isCurrent() for those layers.
|
||||
bool costmapReady(robot_costmap_2d::LayeredCostmap& costmap, std::string* stale_names)
|
||||
{
|
||||
const auto& layers = *costmap.getPlugins();
|
||||
std::size_t providers = 0;
|
||||
for (const auto& layer : layers)
|
||||
if (layer && layer->isEnabled() &&
|
||||
dynamic_cast<const robot_costmap_2d::DirectionFieldProvider*>(layer.get()))
|
||||
++providers;
|
||||
|
||||
bool ready = true;
|
||||
if (stale_names)
|
||||
stale_names->clear();
|
||||
for (const auto& layer : layers)
|
||||
{
|
||||
if (!layer || !layer->isEnabled())
|
||||
continue;
|
||||
const auto* provider = dynamic_cast<const robot_costmap_2d::DirectionFieldProvider*>(layer.get());
|
||||
const bool current = provider
|
||||
? providers == 1 && provider->directionSnapshot().state != robot_costmap_2d::DirectionFieldState::kPending
|
||||
: layer->getType() != robot_costmap_2d::LayerType::DIRECTIONAL_LAYER && layer->isCurrent();
|
||||
if (!current)
|
||||
{
|
||||
ready = false;
|
||||
if (stale_names)
|
||||
{
|
||||
if (!stale_names->empty())
|
||||
stale_names->append(", ");
|
||||
stale_names->append(layer->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ready;
|
||||
}
|
||||
|
||||
/// @brief Pose có dùng được để lập plan không — NaN/Inf lọt vào SBPL là hỏng ở tầng khó truy nhất.
|
||||
bool isFinitePose(const robot_geometry_msgs::PoseStamped& pose)
|
||||
{
|
||||
@@ -430,6 +471,9 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
// Thread chưa nhận: rút yêu cầu ra, kẻo nó chạy yêu cầu của planner cũ trên instance mới rồi
|
||||
// vứt — tốn một lượt makePlan vô ích.
|
||||
pending_ = false;
|
||||
waiting_for_costmap_ = false;
|
||||
if (status_board_ != nullptr)
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kIdle);
|
||||
request_order_.reset();
|
||||
}
|
||||
else if (running_)
|
||||
@@ -445,6 +489,7 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
}
|
||||
active_ = planner;
|
||||
active_external_grid_ = external_grid ? &external_grid : nullptr;
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
active_name_ = planner_name;
|
||||
@@ -510,6 +555,9 @@ bool PlannerRunner::startPlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
pending_ = true;
|
||||
discard_ = false;
|
||||
has_result_ = false;
|
||||
waiting_for_costmap_ = costmap_lock_enabled_ && costmap_ready_timeout_ > 0.0;
|
||||
if (waiting_for_costmap_ && status_board_ != nullptr)
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kWaitingForCostmap);
|
||||
|
||||
cv_.notify_one();
|
||||
return true;
|
||||
@@ -521,6 +569,20 @@ bool PlannerRunner::isPlanning() const
|
||||
return pending_ || running_;
|
||||
}
|
||||
|
||||
void PlannerRunner::setCostmapReadyTimeout(double seconds)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
costmap_ready_timeout_ = seconds;
|
||||
}
|
||||
|
||||
bool PlannerRunner::costmapWaitState(bool& finished)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
finished = costmap_wait_finished_;
|
||||
costmap_wait_finished_ = false;
|
||||
return waiting_for_costmap_;
|
||||
}
|
||||
|
||||
bool PlannerRunner::pollPlan(PlanResult& result)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
@@ -567,6 +629,9 @@ void PlannerRunner::cancelPlan()
|
||||
// sẽ nhận job này NGAY SAU cancel, chạy trọn allocated_time cho một plan không ai cần (bắt được
|
||||
// bằng test CancelPreemptsAPreemptablePlannerWithinMilliseconds, 2026-09-11).
|
||||
pending_ = false;
|
||||
waiting_for_costmap_ = false;
|
||||
if (status_board_ != nullptr)
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kIdle);
|
||||
request_order_.reset();
|
||||
}
|
||||
else if (pending_ || running_)
|
||||
@@ -588,6 +653,7 @@ void PlannerRunner::cancelPlan()
|
||||
// Kết quả đã nằm sẵn trong hộp thư cũng bỏ luôn: bên gọi vừa nói nó không còn cần plan này.
|
||||
has_result_ = false;
|
||||
handoff_.clear();
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
@@ -617,15 +683,16 @@ void PlannerRunner::threadBody()
|
||||
// control thread không bao giờ phải chờ một lượt lập plan.
|
||||
robot_nav_core::BaseGlobalPlanner* planner = active_;
|
||||
const ExternalGridSetter* external_grid = active_external_grid_;
|
||||
running_planner_ = planner;
|
||||
running_planner_ = nullptr;
|
||||
const robot_geometry_msgs::PoseStamped start = request_start_;
|
||||
const robot_geometry_msgs::PoseStamped goal = request_goal_;
|
||||
const std::shared_ptr<robot_protocol_msgs::Order> order = request_order_;
|
||||
const std::uint64_t tag = request_tag_;
|
||||
const double ready_timeout = costmap_ready_timeout_;
|
||||
|
||||
pending_ = false;
|
||||
running_ = true;
|
||||
if (status_board_ != nullptr)
|
||||
if (status_board_ != nullptr && !waiting_for_costmap_)
|
||||
{
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kBusy);
|
||||
}
|
||||
@@ -634,10 +701,10 @@ void PlannerRunner::threadBody()
|
||||
|
||||
planning_.clear();
|
||||
bool ok = false;
|
||||
bool waited = false;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedSection timer(stats_, section_make_plan_);
|
||||
// Planner phải thấy một lưới ĐÔNG CỨNG suốt makePlan: LayeredCostmap::updateMap ghi lưới, và
|
||||
// resizeMap REALLOC cả mảng, dưới Costmap2D::getMutex(). Hai cách đạt được điều đó:
|
||||
//
|
||||
@@ -650,32 +717,118 @@ void PlannerRunner::threadBody()
|
||||
// đứng chờ hết lượt search; với SBPL cạn search trên map lớn là 6-14 s (đo 2026-09-17).
|
||||
// Đây là đường rơi về khi tắt snapshot hoặc plugin không nhận lưới ngoài.
|
||||
//
|
||||
// Kiểm "costmap đã sẵn sàng" (P4, Test/docs/DIRECTION_ZONE_PLAN.md mục 7.3) chạy dưới CÙNG lần
|
||||
// khoá đó, nên lưới planner đọc chính là lưới vừa được xác nhận current. Chưa current thì nhả
|
||||
// khoá, chờ có hạn `costmap_ready_timeout` rồi kiểm lại; không gọi makePlan trên map dở dang.
|
||||
//
|
||||
// Lấy getCostmap() mỗi lượt, không cache — con trỏ đó có thể bị thay (bài học double-free của
|
||||
// workspace). Với capability có mà snapshot TẮT, vẫn gọi setter với nullptr: xoá lưới ngoài
|
||||
// còn sót từ cấu hình trước, để planner đọc lưới sống dưới khoá dài đúng như (b).
|
||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> costmap_lock;
|
||||
if (costmap_lock_enabled_)
|
||||
using Clock = std::chrono::steady_clock;
|
||||
Clock::time_point wait_started;
|
||||
std::string& stale_names = costmap_stale_names_;
|
||||
while (true)
|
||||
{
|
||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> costmap_lock;
|
||||
const bool has_setter = external_grid != nullptr && static_cast<bool>(*external_grid);
|
||||
if (has_setter && costmap_->isSnapshotEnabled())
|
||||
const bool use_snapshot = costmap_lock_enabled_ && has_setter && costmap_->isSnapshotEnabled();
|
||||
if (costmap_lock_enabled_)
|
||||
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
||||
*costmap_->getCostmap()->getMutex());
|
||||
|
||||
// updateMap and StaticLayer callbacks use this same master mutex. For a snapshot, the
|
||||
// recursive mutex remains held across both readiness and refreshSnapshot's copy. For a
|
||||
// live grid it stays held through makePlan. Direction providers use their own snapshot lock.
|
||||
if (costmap_lock_enabled_ && ready_timeout > 0.0 && !costmap_stale_names_reserved_)
|
||||
{
|
||||
std::size_t capacity = 0;
|
||||
for (const auto& layer : *costmap_->getLayeredCostmap()->getPlugins())
|
||||
if (layer) capacity += layer->getName().size() + 2;
|
||||
stale_names.reserve(capacity);
|
||||
costmap_stale_names_reserved_ = true;
|
||||
}
|
||||
const bool ready = !costmap_lock_enabled_ || ready_timeout == 0.0 ||
|
||||
costmapReady(*costmap_->getLayeredCostmap(), &stale_names);
|
||||
const auto now = Clock::now();
|
||||
if (!ready && !waited)
|
||||
{
|
||||
waited = true;
|
||||
wait_started = now;
|
||||
if (!costmap_wait_problem_active_)
|
||||
{
|
||||
costmap_wait_problem_active_ = true;
|
||||
last_costmap_wait_log_ = now;
|
||||
robot::log_warning("[move_base2] PlannerRunner: waiting for global costmap; not current: %s\n",
|
||||
stale_names.c_str());
|
||||
}
|
||||
}
|
||||
const double elapsed = waited ? std::chrono::duration<double>(now - wait_started).count() : 0.0;
|
||||
const CostmapReadyDecision decision = costmapReadyDecision(ready, elapsed, ready_timeout);
|
||||
if (decision == CostmapReadyDecision::kTimeout)
|
||||
{
|
||||
if (last_costmap_timeout_log_ == Clock::time_point{} ||
|
||||
std::chrono::duration<double>(now - last_costmap_timeout_log_).count() >= 5.0)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: global costmap readiness timed out after %.2f s; not current: %s\n",
|
||||
elapsed, stale_names.c_str());
|
||||
last_costmap_timeout_log_ = now;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (decision == CostmapReadyDecision::kWait)
|
||||
{
|
||||
if (std::chrono::duration<double>(now - last_costmap_wait_log_).count() >= 5.0)
|
||||
{
|
||||
robot::log_warning("[move_base2] PlannerRunner: still waiting for global costmap (%.2f s); not current: %s\n",
|
||||
elapsed, stale_names.c_str());
|
||||
last_costmap_wait_log_ = now;
|
||||
}
|
||||
costmap_lock.unlock();
|
||||
lock.lock();
|
||||
const double poll_seconds = std::min(0.05, ready_timeout - elapsed);
|
||||
cv_.wait_for(lock, std::chrono::duration<double>(poll_seconds),
|
||||
[this] { return shutdown_ || discard_; });
|
||||
const bool cancelled = shutdown_ || discard_;
|
||||
lock.unlock();
|
||||
if (cancelled)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (use_snapshot)
|
||||
{
|
||||
costmap_->refreshSnapshot();
|
||||
(*external_grid)(planner, costmap_->getCostmapForRead());
|
||||
}
|
||||
else
|
||||
else if (costmap_lock_enabled_ && has_setter)
|
||||
(*external_grid)(planner, nullptr);
|
||||
|
||||
lock.lock();
|
||||
const bool cancelled = shutdown_ || discard_;
|
||||
waiting_for_costmap_ = false;
|
||||
costmap_wait_finished_ = costmap_wait_finished_ || waited;
|
||||
if (!cancelled)
|
||||
{
|
||||
if (has_setter)
|
||||
{
|
||||
(*external_grid)(planner, nullptr);
|
||||
}
|
||||
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
|
||||
*(costmap_->getCostmap()->getMutex()));
|
||||
running_planner_ = planner;
|
||||
if (status_board_ != nullptr)
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kBusy);
|
||||
}
|
||||
lock.unlock();
|
||||
if (cancelled)
|
||||
break;
|
||||
if (use_snapshot)
|
||||
costmap_lock.unlock();
|
||||
if (costmap_wait_problem_active_)
|
||||
{
|
||||
robot::log_info("[move_base2] PlannerRunner: global costmap is ready; planning resumes.\n");
|
||||
costmap_wait_problem_active_ = false;
|
||||
last_costmap_timeout_log_ = Clock::time_point{};
|
||||
}
|
||||
ScopedSection timer(stats_, section_make_plan_);
|
||||
ok = (order != nullptr) ? planner->makePlan(*order, start, goal, planning_)
|
||||
: planner->makePlan(start, goal, planning_);
|
||||
break;
|
||||
}
|
||||
// Hai overload của interface gốc gộp lại: "có Order hay không" là một nhánh, không phải hai
|
||||
// contract. Plugin nào không hiểu Order thì overload mặc định của nó tự lo.
|
||||
ok = (order != nullptr) ? planner->makePlan(*order, start, goal, planning_)
|
||||
: planner->makePlan(start, goal, planning_);
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
@@ -701,6 +854,9 @@ void PlannerRunner::threadBody()
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
if (waiting_for_costmap_ && waited)
|
||||
costmap_wait_finished_ = true;
|
||||
waiting_for_costmap_ = false;
|
||||
running_ = false;
|
||||
running_planner_ = nullptr;
|
||||
if (status_board_ != nullptr)
|
||||
|
||||
Reference in New Issue
Block a user