feat: snapshot API cho reader — planner solve trên bản copy, hết chặn map-update
Costmap2D::copyFrom: copy metadata + grid, chỉ realloc khi size đổi (operator= delete/new mỗi lần — không hợp copy định kỳ 30 Hz). Caller giữ mutex nguồn. Costmap2DROBOT: key config snapshot_for_readers (mặc định false = nguyên trạng); refreshSnapshot() khoá master mutex đúng bằng thời gian memcpy; getCostmapForRead() trả snapshot khi bật / lưới live khi tắt. Object snapshot ổn định địa chỉ, refresh tại chỗ — không tái tạo con trỏ (bài học DOUBLE_FREE_BUG_FIX_REPORT). Hợp đồng single-consumer: refresh + đọc cùng thread control loop. Kèm: load debug_level (commit trước) + fix priv_nh(priv_nh, name) tự khởi tạo từ chính nó — đổi thành (nh, name), cùng kết quả namespace, hết UB-adjacent. Verify: test_costmap 10/10 (3 test copyFrom mới), sim mission — lock_wait local 53 ms max -> 13 us, p99 cycle 21.4 -> 3.4 ms, local costmap chạy được 10 Hz với 1.2% một core. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -165,6 +165,28 @@ Costmap2D::Costmap2D(const Costmap2D& map) :
|
||||
*this = map;
|
||||
}
|
||||
|
||||
void Costmap2D::copyFrom(const Costmap2D& map)
|
||||
{
|
||||
if (this == &map)
|
||||
return;
|
||||
|
||||
boost::unique_lock<mutex_t> lock(*access_);
|
||||
if (costmap_ == NULL || size_x_ != map.size_x_ || size_y_ != map.size_y_)
|
||||
{
|
||||
delete[] costmap_;
|
||||
costmap_ = new unsigned char[static_cast<std::size_t>(map.size_x_) * map.size_y_];
|
||||
size_x_ = map.size_x_;
|
||||
size_y_ = map.size_y_;
|
||||
}
|
||||
|
||||
resolution_ = map.resolution_;
|
||||
origin_x_ = map.origin_x_;
|
||||
origin_y_ = map.origin_y_;
|
||||
default_value_ = map.default_value_;
|
||||
memcpy(costmap_, map.costmap_,
|
||||
static_cast<std::size_t>(size_x_) * size_y_ * sizeof(unsigned char));
|
||||
}
|
||||
|
||||
// just initialize everything to NULL by default
|
||||
Costmap2D::Costmap2D() :
|
||||
size_x_(0), size_y_(0), resolution_(0.0), origin_x_(0.0), origin_y_(0.0), costmap_(NULL)
|
||||
|
||||
@@ -105,7 +105,7 @@ void Costmap2DROBOT::getParams(const std::string& config_file_name,const std::st
|
||||
YAML::Node config = YAML::LoadFile(path_source);
|
||||
YAML::Node layer = config["robot_costmap_2d"];
|
||||
|
||||
robot::NodeHandle priv_nh(priv_nh, name);
|
||||
robot::NodeHandle priv_nh(nh, name);
|
||||
|
||||
std::string global_frame =
|
||||
loadParam(layer, "global_frame", std::string("map"));
|
||||
@@ -164,11 +164,27 @@ void Costmap2DROBOT::getParams(const std::string& config_file_name,const std::st
|
||||
if (priv_nh.hasParam("performance_metrics_period"))
|
||||
priv_nh.getParam("performance_metrics_period", performance_metrics_period);
|
||||
|
||||
int debug_level = loadParam(layer, "debug_level", 0);
|
||||
if (priv_nh.hasParam("debug_level"))
|
||||
priv_nh.getParam("debug_level", debug_level);
|
||||
|
||||
bool snapshot_for_readers = loadParam(layer, "snapshot_for_readers", false);
|
||||
if (priv_nh.hasParam("snapshot_for_readers"))
|
||||
priv_nh.getParam("snapshot_for_readers", snapshot_for_readers);
|
||||
snapshot_enabled_ = snapshot_for_readers;
|
||||
if (debug_level < 0 || debug_level > 2)
|
||||
{
|
||||
robot::log_warning("costmap '%s': debug_level %d out of [0, 2], clamped\n",
|
||||
name_.c_str(), debug_level);
|
||||
debug_level = std::max(0, std::min(debug_level, 2));
|
||||
}
|
||||
|
||||
if (priv_nh.hasParam("library_path"))
|
||||
path_plugins = loader.findLibraryPath(name_);
|
||||
layered_costmap_ = new LayeredCostmap(global_frame_, rolling_window, track_unknown_space);
|
||||
layered_costmap_->setPerformanceMetrics(
|
||||
performance_metrics_enabled, performance_metrics_period);
|
||||
layered_costmap_->setDebugLevel(debug_level);
|
||||
|
||||
// find size parameters
|
||||
double map_width_meters = loadParam(layer, "width", 0.0);
|
||||
@@ -524,10 +540,34 @@ void Costmap2DROBOT::copyParentParameters(const std::string& costmap_name,
|
||||
}
|
||||
|
||||
void Costmap2DROBOT::setUnpaddedRobotFootprintPolygon(const robot_geometry_msgs::Polygon& footprint)
|
||||
{
|
||||
{
|
||||
setUnpaddedRobotFootprint(toPointVector(footprint));
|
||||
}
|
||||
|
||||
void Costmap2DROBOT::refreshSnapshot()
|
||||
{
|
||||
if (!snapshot_enabled_ || layered_costmap_ == NULL)
|
||||
return;
|
||||
|
||||
Costmap2D* master = layered_costmap_->getCostmap();
|
||||
// Master mutex held only for the copy (~us for a local-costmap grid), not
|
||||
// for the reader's whole computation — that is the entire point.
|
||||
boost::unique_lock<Costmap2D::mutex_t> lock(*(master->getMutex()));
|
||||
costmap_snapshot_.copyFrom(*master);
|
||||
}
|
||||
|
||||
Costmap2D* Costmap2DROBOT::getCostmapForRead()
|
||||
{
|
||||
if (!snapshot_enabled_)
|
||||
return getCostmap();
|
||||
|
||||
// First read before any refresh (e.g. planner initialize probing the grid):
|
||||
// fill the snapshot once so the reader never sees an empty map.
|
||||
if (costmap_snapshot_.getCharMap() == NULL)
|
||||
refreshSnapshot();
|
||||
return &costmap_snapshot_;
|
||||
}
|
||||
|
||||
Costmap2DROBOT::~Costmap2DROBOT()
|
||||
{
|
||||
map_update_thread_shutdown_ = true;
|
||||
|
||||
Reference in New Issue
Block a user