fix(costmap): one parameter flow for every layer; fail on a bad layer

Each layer reads its default file under $PNKX_NAV_CORE_CONFIG_DIR/config,
then overrides a key from "<costmap>/<plugin>".
Costmap2DROBOT::copyParentParameters() copies the shared "<plugin>:"
block there first. This was broken in three ways:
- the copy lists covered only part of what the layers read, so a key set
  in the shared block was dropped without a word;
- DepthCameraObstacleLayer had no default file and read the shared block
  itself;
- a layer that failed to initialize left a half-built costmap that
  reported READY.

Failing loudly:
- costmap_2d_robot.cpp: a failing layer, a malformed plugins: list or a
  missing costmap_params.yaml throws std::runtime_error with the costmap
  and layer name, after releasing layered_costmap_.
  NavigationRuntime::buildCostmaps() turns this into an initialization
  error, so the host never reports READY.
- Static/Obstacle/Voxel/Inflation and DepthCameraObstacleLayer throw when
  their default file cannot be opened.
- A key whose value does not convert to the type the layer reads throws.
  getParam() used to write 0, false or "" into the value, and that was
  copied as if configured.

One flow:
- copyParentParameters() copies every key each layer reads, with the
  layer's type, including a new DepthCameraObstacleLayer branch. Copies no
  layer reads are dropped.
- DepthCameraObstacleLayer::loadParameters() reads
  config/depth_camera_obstacle_layer_params.yaml (new, equal to the C++
  defaults), then "<costmap>/<plugin>". ParamReader is gone, and so is the
  node_handle.cpp:265 warning at start-up.
- VoxelLayer: z_resolution can be overridden; unknown_threshold is
  computed after the final z_voxels; the column keys are range-checked;
  the effective column is logged once at start-up.
- config/obstacle_layer_params.yaml: drop the per-source keys the layer
  never read there.
- depth_camera_obstacle_layer_test: set PNKX_NAV_CORE_CONFIG_DIR; add
  tests for a wrong type and a missing default file.

Squash of fix/config-flow (ae7fce9, f8705e1). Checked on the KIT robot on
22/10/2026:
- the depth layer summary at start-up is unchanged;
- a shared-block key now reaches VoxelLayer;
- FOV_W 3.5, z_voxels 17, a wrong-typed key and a missing default file
  each give INITIALIZATION_FAILED.
This commit is contained in:
2026-10-22 10:49:31 +07:00
parent 3b89add5ee
commit e40e9c0104
9 changed files with 340 additions and 262 deletions

View File

@@ -50,9 +50,13 @@
#include <pcl/search/kdtree.h>
#include <pcl/segmentation/extract_clusters.h>
#include <robot_costmap_2d/utils.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <filesystem>
#include <limits>
#include <sstream>
#include <stdexcept>
@@ -79,42 +83,26 @@ Eigen::Isometry3d toIsometry(const tf3::TransformStampedMsg& t)
return iso;
}
// The source keeps parameters in the plugin namespace. Costmap2DROBOT only copies the keys of
// known layer types from the shared namespace into "<costmap>/<plugin>", so read the scoped
// value first and fall back to the shared block with the plugin's own name.
class ParamReader
// Steps 1 and 3 of the costmap layer config flow for one key: the value in the layer's default
// file, overridden by "<costmap>/<plugin>/<key>", which Costmap2DROBOT::copyParentParameters() has
// filled from the shared "<plugin>:" block. A value that does not convert to T is a configuration
// error: getParam() would otherwise replace it with 0, false or "" without a word.
template <typename T>
T readParam(const YAML::Node& file_node, const std::string& file_key, const robot::NodeHandle& nh,
const std::string& key, const T& fallback)
{
public:
ParamReader(const robot::NodeHandle& scoped, const robot::NodeHandle& shared)
: scoped_(scoped), shared_(shared)
{
}
T value = loadParam(file_node, file_key, fallback);
if (nh.hasParam(key) && !nh.getParam(key, value, value))
throw std::invalid_argument("DepthCameraObstacleLayer: '" + nh.getNamespace() + "/" + key +
"' does not convert to the type the layer reads");
return value;
}
ParamReader child(const std::string& ns) const
{
return ParamReader(robot::NodeHandle(scoped_, ns), robot::NodeHandle(shared_, ns));
}
bool has(const std::string& key) const
{
return scoped_.hasParam(key) || shared_.hasParam(key);
}
template <typename T>
T get(const std::string& key, const T& default_value) const
{
T value = default_value;
if (scoped_.hasParam(key))
scoped_.getParam(key, value, default_value);
else if (shared_.hasParam(key))
shared_.getParam(key, value, default_value);
return value;
}
private:
robot::NodeHandle scoped_;
robot::NodeHandle shared_;
};
template <typename T>
T readParam(const YAML::Node& file_node, const robot::NodeHandle& nh, const std::string& key, const T& fallback)
{
return readParam(file_node, key, nh, key, fallback);
}
void requireRange(bool ok, const std::string& what)
{
@@ -250,33 +238,56 @@ void DepthCameraObstacleLayer::onInitialize()
void DepthCameraObstacleLayer::loadParameters()
{
robot::NodeHandle nh("~");
const std::string short_name = name_.substr(name_.find_last_of('/') + 1);
const ParamReader p(robot::NodeHandle(nh, name_), robot::NodeHandle(nh, short_name));
// Step 1 of the config flow: the default file, like the other layers. A missing file or block
// stops the costmap from being built instead of running on C++ defaults.
const std::string config_file_name = "depth_camera_obstacle_layer_params.yaml";
const char* env_config = std::getenv("PNKX_NAV_CORE_CONFIG_DIR");
const std::string folder = (env_config && std::filesystem::exists(env_config)) ? env_config : "";
YAML::Node config;
try
{
config = YAML::LoadFile(getSourceFile(folder, config_file_name));
}
catch (const YAML::BadFile&)
{
throw std::runtime_error("DepthCameraObstacleLayer " + name_ + ": cannot open the default file " +
config_file_name);
}
YAML::Node layer = config["depth_camera_obstacle_layer"];
if (!layer.IsMap())
throw std::runtime_error("DepthCameraObstacleLayer " + name_ + ": " + config_file_name +
" has no depth_camera_obstacle_layer: block");
const bool track_unknown_space = p.get("track_unknown_space", layered_costmap_->isTrackingUnknown());
// Step 3: "<costmap>/<plugin>", filled by copyParentParameters() from the shared block.
robot::NodeHandle nh("~");
const robot::NodeHandle priv_nh(nh, name_);
// Not in the default file on purpose: unset, the layer follows the costmap's track_unknown_space.
const bool track_unknown_space =
readParam(layer, priv_nh, "track_unknown_space", layered_costmap_->isTrackingUnknown());
default_value_ = track_unknown_space ? NO_INFORMATION : FREE_SPACE;
enabled_ = p.get("enabled", true);
footprint_clearing_enabled_ = p.get("footprint_clearing_enabled", true);
combination_method_ = p.get("combination_method", 1);
enable_near_blocked_protection_ = p.get("enable_near_blocked_protection", true);
number_points_considered_as_blocked_ = p.get("number_points_considered_as_blocked", 5);
forced_clearing_distance_ = p.get("forced_clearing_distance", 0.1);
ec_seg_distance_ = p.get("ec_seg_distance", 0.2);
ec_cluster_min_size_ = p.get("ec_cluster_min_size", 5);
size_of_cluster_rejection_ = p.get("size_of_cluster_rejection", 5);
voxel_resolution_ = p.get("voxel_resolution", 0.01);
check_radius_ = p.get("check_radius", 0.1);
number_clearing_threshold_ = p.get("number_clearing_threshold", 2);
use_voxelized_observation_ = p.get("use_voxelized_observation", true);
transform_tolerance_ = p.get("transform_tolerance", 0.2);
const bool use_global_frame_to_mark = p.get("use_global_frame_to_mark", true);
if (p.has("clear_touches_bounds"))
enabled_ = readParam(layer, priv_nh, "enabled", true);
footprint_clearing_enabled_ = readParam(layer, priv_nh, "footprint_clearing_enabled", true);
combination_method_ = readParam(layer, priv_nh, "combination_method", 1);
enable_near_blocked_protection_ = readParam(layer, priv_nh, "enable_near_blocked_protection", true);
number_points_considered_as_blocked_ = readParam(layer, priv_nh, "number_points_considered_as_blocked", 5);
forced_clearing_distance_ = readParam(layer, priv_nh, "forced_clearing_distance", 0.1);
ec_seg_distance_ = readParam(layer, priv_nh, "ec_seg_distance", 0.2);
ec_cluster_min_size_ = readParam(layer, priv_nh, "ec_cluster_min_size", 5);
size_of_cluster_rejection_ = readParam(layer, priv_nh, "size_of_cluster_rejection", 5);
voxel_resolution_ = readParam(layer, priv_nh, "voxel_resolution", 0.01);
check_radius_ = readParam(layer, priv_nh, "check_radius", 0.1);
number_clearing_threshold_ = readParam(layer, priv_nh, "number_clearing_threshold", 2);
use_voxelized_observation_ = readParam(layer, priv_nh, "use_voxelized_observation", true);
transform_tolerance_ = readParam(layer, priv_nh, "transform_tolerance", 0.2);
const bool use_global_frame_to_mark = readParam(layer, priv_nh, "use_global_frame_to_mark", true);
if (layer["clear_touches_bounds"] || priv_nh.hasParam("clear_touches_bounds"))
robot::log_warning("DepthCameraObstacleLayer %s: clear_touches_bounds is obsolete and ignored: a cleared "
"cell now always resets its master cell\n", name_.c_str());
debug_stats_ = p.get("debug/stats", true);
debug_stats_period_ = p.get("debug/stats_period", 5.0);
YAML::Node debug = layer["debug"];
debug_stats_ = readParam(debug, "stats", priv_nh, "debug/stats", true);
debug_stats_period_ = readParam(debug, "stats_period", priv_nh, "debug/stats_period", 5.0);
// The source's local mode keys points by costmap index, so markings move with a rolling
// window (its README shows it). Only the global mode is ported.
@@ -295,26 +306,29 @@ void DepthCameraObstacleLayer::loadParameters()
marking_height_under_ground_ = 100.0;
marking_height_above_ground_ = -100.0;
std::stringstream ss(p.get("observation_sources", std::string("")));
std::stringstream ss(readParam(layer, priv_nh, "observation_sources", std::string("")));
std::string name;
while (ss >> name)
{
const ParamReader s = p.child(name);
// Like obstacle_layer_params.yaml, the default file has no block per source, so a source key
// comes from "<costmap>/<plugin>/<source>/" or falls back to the C++ default below.
YAML::Node source_defaults = layer[name];
const robot::NodeHandle s(priv_nh, name);
std::unique_ptr<Source> src(new Source());
src->name = name;
src->topic = s.get("topic", name);
src->sensor_frame = s.get("sensor_frame", std::string(""));
src->sensor_frame_is_optical = s.get("sensor_frame_is_optical", false);
src->observation_keep_time = s.get("observation_persistence", 0.0);
src->expected_update_rate = s.get("expected_update_rate", 0.0);
src->min_obstacle_height = s.get("min_obstacle_height", 0.0);
src->max_obstacle_height = s.get("max_obstacle_height", 2.0);
src->marking = s.get("marking", true);
src->fov_v = s.get("FOV_V", 1.0);
src->fov_w = s.get("FOV_W", 1.5);
src->min_detect_distance = s.get("min_detect_distance", 0.15);
src->max_detect_distance = s.get("max_detect_distance", 2.5);
if (s.get("inf_is_valid", false))
src->topic = readParam(source_defaults, s, "topic", name);
src->sensor_frame = readParam(source_defaults, s, "sensor_frame", std::string(""));
src->sensor_frame_is_optical = readParam(source_defaults, s, "sensor_frame_is_optical", false);
src->observation_keep_time = readParam(source_defaults, s, "observation_persistence", 0.0);
src->expected_update_rate = readParam(source_defaults, s, "expected_update_rate", 0.0);
src->min_obstacle_height = readParam(source_defaults, s, "min_obstacle_height", 0.0);
src->max_obstacle_height = readParam(source_defaults, s, "max_obstacle_height", 2.0);
src->marking = readParam(source_defaults, s, "marking", true);
src->fov_v = readParam(source_defaults, s, "FOV_V", 1.0);
src->fov_w = readParam(source_defaults, s, "FOV_W", 1.5);
src->min_detect_distance = readParam(source_defaults, s, "min_detect_distance", 0.15);
src->max_detect_distance = readParam(source_defaults, s, "max_detect_distance", 2.5);
if (readParam(source_defaults, s, "inf_is_valid", false))
robot::log_warning("DepthCameraObstacleLayer: inf_is_valid is not applicable to PointCloud observations\n");
const std::string what = "source " + name + ": ";

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) {