fix(costmap): refuse to build a costmap with a failed layer

Costmap2DROBOT::getParams() logged a layer that threw from initialize()
and returned. The constructor then finished normally with a half-built
costmap: no failed layer, no layers after it, no footprint and no update
thread. move_base2 reported READY, and the only trace was a message
blaming plugin registration.

- costmap_2d_robot.cpp: a failing layer, a malformed plugins: list and a
  missing costmap_params.yaml now throw std::runtime_error with the
  costmap and layer name, after releasing layered_costmap_ (the
  destructor does not run when the constructor throws).
  NavigationRuntime::buildCostmaps() already turns this into an
  initialization error, so the host never reports READY.
- Static/Obstacle/Voxel/Inflation layers throw when their default file
  cannot be opened, instead of running with no parameters.
- VoxelLayer: z_resolution can be overridden from the costmap config
  like the other column keys; unknown_threshold is computed after the
  final z_voxels, so overriding z_voxels alone keeps it right; z_voxels,
  z_resolution, origin_z and the thresholds are range-checked; the
  effective column is logged once at start-up.
- config/obstacle_layer_params.yaml: drop the per-source keys. The layer
  reads them only under <plugin>/<source>/, so these were never used.
This commit is contained in:
2026-10-22 09:46:11 +07:00
parent 3b89add5ee
commit ae7fce9e77
6 changed files with 87 additions and 75 deletions

View File

@@ -1,18 +1,13 @@
# Giá trị mặc định của ObstacleLayer (cũng là phần ObstacleLayer bên trong VoxelLayer).
# Plugin đọc file này TRƯỚC, rồi khoá cùng tên trong khối <costmap>/<plugin> của config costmap đè lên.
# Đây là bản đóng gói kèm gói robot_costmap_2d; robot chạy bản trong
# pnkx_nav_core/config/runtime/config/obstacle_layer_params.yaml.
# Khoá theo từng nguồn (topic, sensor_frame, data_type, clearing, marking, obstacle_range,
# raytrace_range...) KHÔNG đặt ở đây: plugin chỉ đọc chúng trong khối <plugin>/<tên source>/.
obstacle_layer:
track_unknown_space: true
transform_tolerance: 0.2
topic: "map"
sensor_frame: laser_frame
observation_persistence: 0.0
expected_update_rate: 0.0
data_type: PointCloud
min_obstacle_height: 0.0
max_obstacle_height: 2.0
inf_is_valid: false
clearing: false
marking: true
obstacle_range: 2.5
raytrace_range: 3.0
footprint_clearing_enabled: true
combination_method: 1
track_unknown_space: true # [bool] true = ô chưa quan sát giữ NO_INFORMATION; false = coi là free.
transform_tolerance: 0.2 # [s] độ trễ TF tối đa khi đổi điểm sensor sang frame costmap. Nhỏ quá
# thì điểm bị bỏ khi TF trễ; lớn quá thì điểm được đặt sai chỗ khi robot quay.
max_obstacle_height: 2.0 # [m] điểm cao hơn mức này không được đánh dấu.
footprint_clearing_enabled: true # [bool] xoá vật cản nằm trong footprint robot (chính thân robot).
combination_method: 1 # [-] 0 = ghi đè lên master costmap; 1 = lấy max với master.

View File

@@ -41,6 +41,7 @@
#include <robot_costmap_2d/footprint.h>
#include <boost/thread.hpp>
#include <boost/dll/alias.hpp>
#include <stdexcept>
using robot_costmap_2d::LETHAL_OBSTACLE;
@@ -80,7 +81,8 @@ void InflationLayer::onInitialize()
need_reinflation_ = false;
std::string config_file_name = "inflation_layer_params.yaml";
// std::cout << "InflationLayer: " << config_file_name << std::endl;
getParams(config_file_name, priv_nh);
if (!getParams(config_file_name, priv_nh))
throw std::runtime_error("InflationLayer " + name_ + ": cannot open the default file " + config_file_name);
matchSize();
}

View File

@@ -42,6 +42,7 @@
#include <tf3/exceptions.h>
#include <boost/dll/alias.hpp>
#include <stdexcept>
using robot_costmap_2d::NO_INFORMATION;
@@ -66,7 +67,8 @@ void ObstacleLayer::onInitialize()
stop_receiving_data_ = false;
global_frame_ = layered_costmap_->getGlobalFrameID();
std::string config_file_name = "obstacle_layer_params.yaml";
getParams(config_file_name, priv_nh);
if (!getParams(config_file_name, priv_nh))
throw std::runtime_error("ObstacleLayer " + name_ + ": cannot open the default file " + config_file_name);
}
ObstacleLayer::~ObstacleLayer()

View File

@@ -43,6 +43,7 @@
#include <robot_tf3_geometry_msgs/tf3_geometry_msgs.h>
#include <boost/dll/alias.hpp>
#include <stdexcept>
#include <fstream>
#include <cxxabi.h>
@@ -71,7 +72,8 @@ void StaticLayer::onInitialize()
global_frame_ = layered_costmap_->getGlobalFrameID();
std::string config_file_name = "static_layer_params.yaml";
getParams(config_file_name, priv_nh);
if (!getParams(config_file_name, priv_nh))
throw std::runtime_error("StaticLayer " + name_ + ": cannot open the default file " + config_file_name);
robot::log_warning("Initializing static layer with map topic \"%s\" in frame \"%s\"", map_topic_.c_str(), global_frame_.c_str());
}

View File

@@ -45,6 +45,8 @@
#include <cmath>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <string>
#define VOXEL_BITS 16
@@ -65,7 +67,8 @@ void VoxelLayer::onInitialize()
ObstacleLayer::onInitialize();
std::string config_file_name = "voxel_layer_params.yaml";
getParams(config_file_name, priv_nh);
if (!getParams(config_file_name, priv_nh))
throw std::runtime_error("VoxelLayer " + name_ + ": cannot open the default file " + config_file_name);
}
VoxelLayer::~VoxelLayer()
@@ -92,14 +95,14 @@ bool VoxelLayer::getParams(const std::string& config_file_name, robot::NodeHandl
enabled_ = loadParam(layer, "enabled", true);
footprint_clearing_enabled_ = loadParam(layer, "footprint_clearing_enabled", true);
max_obstacle_height_ = loadParam(layer, "max_obstacle_height", 2.0);
size_z_ = loadParam(layer, "z_voxels", 10);
int size_z = loadParam(layer, "z_voxels", 10);
origin_z_ = loadParam(layer, "origin_z", 0.0);
z_resolution_ = loadParam(layer, "z_resolution", 0.2);
unknown_threshold_ = loadParam(layer, "unknown_threshold", 15.0) + (VOXEL_BITS - size_z_);
mark_threshold_ = loadParam(layer, "mark_threshold", 0);
// Read as double: the default file writes 15.0, which yaml-cpp refuses to convert to int.
double unknown_threshold = loadParam(layer, "unknown_threshold", 15.0);
int mark_threshold = loadParam(layer, "mark_threshold", 0);
combination_method_ = loadParam(layer, "combination_method", 0.0);
int size_z, unknown_threshold, mark_threshold;
if (nh.hasParam("enabled"))
nh.getParam("enabled", enabled_);
if (nh.hasParam("footprint_clearing_enabled"))
@@ -107,25 +110,40 @@ bool VoxelLayer::getParams(const std::string& config_file_name, robot::NodeHandl
if (nh.hasParam("max_obstacle_height"))
nh.getParam("max_obstacle_height", max_obstacle_height_);
if (nh.hasParam("z_voxels"))
{
nh.getParam("z_voxels", size_z);
size_z_ = size_z;
}
if (nh.hasParam("origin_z"))
nh.getParam("origin_z", origin_z_);
if (nh.hasParam("z_resolution"))
nh.getParam("z_resolution", z_resolution_);
if (nh.hasParam("unknown_threshold"))
{
nh.getParam("unknown_threshold", unknown_threshold);
unknown_threshold_ = unknown_threshold + (VOXEL_BITS - size_z_);
}
if (nh.hasParam("mark_threshold"))
{
nh.getParam("mark_threshold", mark_threshold);
mark_threshold_ = mark_threshold;
}
if (nh.hasParam("combination_method"))
nh.getParam("combination_method", combination_method_);
// A column stores VOXEL_BITS levels at most, and worldToMap3D divides by z_resolution.
if (size_z < 1 || size_z > VOXEL_BITS)
throw std::invalid_argument("VoxelLayer " + name_ + ": z_voxels must be in [1, 16]");
if (!(z_resolution_ > 0.0) || !std::isfinite(z_resolution_))
throw std::invalid_argument("VoxelLayer " + name_ + ": z_resolution must be > 0 [m]");
if (!std::isfinite(origin_z_))
throw std::invalid_argument("VoxelLayer " + name_ + ": origin_z must be finite [m]");
if (!(unknown_threshold >= 0.0) || mark_threshold < 0)
throw std::invalid_argument("VoxelLayer " + name_ + ": unknown_threshold and mark_threshold must be >= 0");
size_z_ = size_z;
// Levels above z_voxels are never observed and stay unknown; adding them keeps the threshold
// counting only the levels in use. Computed after size_z_ is final, so overriding z_voxels
// alone no longer leaves the threshold sized for the default file's column.
unknown_threshold_ = static_cast<unsigned int>(unknown_threshold) + (VOXEL_BITS - size_z_);
mark_threshold_ = static_cast<unsigned int>(mark_threshold);
robot::log_info("VoxelLayer %s: column origin_z=%.2f m z_resolution=%.3f m z_voxels=%u (top %.2f m) "
"unknown_threshold=%u mark_threshold=%u max_obstacle_height=%.2f m\n",
name_.c_str(), origin_z_, z_resolution_, size_z_, origin_z_ + size_z_ * z_resolution_,
unknown_threshold_, mark_threshold_, max_obstacle_height_);
this->matchSize();
}
catch (const YAML::BadFile& e) {

View File

@@ -48,6 +48,7 @@
#include <tf3/convert.h>
#include <tf3/utils.h>
#include <exception>
#include <stdexcept>
#include <boost/dll/import.hpp>
@@ -109,6 +110,17 @@ void Costmap2DROBOT::getParams(const std::string& config_file_name,const std::st
robot::NodeHandle priv_nh(nh, name);
// A costmap missing a layer, the layers after it or its update thread still looks alive to the
// planner, which then drives on a map that never marks obstacles. Refuse to build it instead:
// NavigationRuntime::buildCostmaps() turns the exception into an initialization error and the
// host never reports READY. The destructor does not run when the constructor throws, so the
// layered costmap (and the layers already added to it) is released here.
const auto fail = [this](const std::string& what) {
delete layered_costmap_;
layered_costmap_ = NULL;
throw std::runtime_error("costmap '" + name_ + "': " + what);
};
std::string global_frame =
loadParam(layer, "global_frame", std::string("map"));
std::string robot_base_frame =
@@ -224,44 +236,22 @@ void Costmap2DROBOT::getParams(const std::string& config_file_name,const std::st
{
my_list.clear();
YAML::Node my_plugins = priv_nh.getParamValue("plugins");
if (my_plugins && my_plugins.IsSequence())
if (!my_plugins || !my_plugins.IsSequence())
fail("plugins: must be a list of {name, type} maps");
for (size_t i = 0; i < my_plugins.size(); ++i)
{
for (size_t i = 0; i < my_plugins.size(); ++i)
{
YAML::Node plugin_i = my_plugins[i].as<YAML::Node>();
YAML::Node plugin_i = my_plugins[i].as<YAML::Node>();
// Checked before reading the fields: indexing a scalar entry throws a bare YAML error.
if (!plugin_i.IsMap() || !plugin_i["name"] || !plugin_i["type"])
fail("plugins: entry " + std::to_string(i) + " must be a map with a name and a type");
PluginConfig p;
p.type = plugin_i["type"].as<std::string>();
p.name = plugin_i["name"].as<std::string>();
my_list.push_back(p);
if (!plugin_i.IsMap())
{
std::cerr << "Plugins must be specified as maps. We'll use the default recovery behaviors instead." << std::endl;
return;
}
if (!plugin_i["name"] || !plugin_i["type"])
{
std::cerr << "Plugins must have a name and a type. Using the default recovery behaviors instead." << std::endl;
return;
}
// check for recovery behaviors with the same name
std::string name_i = plugin_i["name"].as<std::string>();
for (size_t j = i + 1; j < my_plugins.size(); ++j)
{
YAML::Node plugin_j = my_plugins[j].as<YAML::Node>();
if (plugin_j.IsMap() && plugin_j["name"])
{
std::string name_j = plugin_j["name"].as<std::string>();
if (name_i == name_j)
{
std::cerr << "A plugin with the name " << name_i << " already exists, this is not allowed. Using the default recovery behaviors instead." << std::endl;
return;
}
}
}
}
PluginConfig p;
p.type = plugin_i["type"].as<std::string>();
p.name = plugin_i["name"].as<std::string>();
for (const auto& known : my_list)
if (known.name == p.name)
fail("plugins: the name '" + p.name + "' is used twice; each layer needs its own name");
my_list.push_back(p);
}
}
else
@@ -293,10 +283,10 @@ void Costmap2DROBOT::getParams(const std::string& config_file_name,const std::st
plugin->initialize(layered_costmap_,name_ + "/" + info.name, &tf_);
layered_costmap_->addPlugin(plugin);
}
catch (std::exception &ex)
catch (const std::exception& ex)
{
robot::log_error("Failed to create the %s, are you sure it is properly registered and that the containing library is built? Exception: %s\n", info.name.c_str(), ex.what());
return;
// Covers a missing library or symbol as well as a layer rejecting its parameters.
fail("layer '" + info.name + "' (" + info.type + "): " + ex.what());
}
}
@@ -353,8 +343,11 @@ void Costmap2DROBOT::getParams(const std::string& config_file_name,const std::st
}
catch (const YAML::BadFile& e)
{
std::cerr << "Cannot open YAML file: " << e.what() << std::endl;
return;
// costmap_params.yaml is read before layered_costmap_ exists; delete is a no-op on NULL.
delete layered_costmap_;
layered_costmap_ = NULL;
throw std::runtime_error("costmap '" + name_ + "': cannot open the default file " + config_file_name +
" under $PNKX_NAV_CORE_CONFIG_DIR/config: " + e.what());
}
}