2 Commits

Author SHA1 Message Date
f8705e1c49 fix(costmap): put every layer on one parameter flow
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. 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 bypassed the flow: it had no default file and
read the shared block itself, with the opposite precedence.

- copyParentParameters(): copy every key each layer reads.
  - StaticLayer: add enabled, first_map_only, subscribe_to_updates,
    use_maximum, trinary_costmap and base_frame_id.
  - Obstacle/VoxelLayer: add transform_tolerance,
    footprint_clearing_enabled and combination_method; VoxelLayer also
    gets enabled, max_obstacle_height and track_unknown_space.
  - InflationLayer: add enabled and inflate_unknown.
  - Add a DepthCameraObstacleLayer branch with its layer and per-source
    keys.
  - Drop copies that no layer reads (publish_voxel_map, layer-level
    obstacle_range and raytrace_range).
  - Keys are copied with the type the layer reads. A key that does not
    convert now throws: getParam() used to write 0, false or "" into the
    value, and that was copied as if configured.
- DepthCameraObstacleLayer::loadParameters(): read
  config/depth_camera_obstacle_layer_params.yaml, then "<costmap>/<plugin>",
  like ObstacleLayer. A missing file or block, or a value of the wrong
  type, throws. ParamReader is gone. With the shared block copied in,
  "<costmap>/<plugin>/<source>" exists, and the node_handle.cpp:265
  warning at start-up goes away.
- config/depth_camera_obstacle_layer_params.yaml: new, equal to the C++
  defaults. track_unknown_space is left out, so the layer still follows
  the costmap.
- depth_camera_obstacle_layer_test: point PNKX_NAV_CORE_CONFIG_DIR at the
  package. Add tests for a value of the wrong type and for a missing
  default file.
2026-10-22 10:25:53 +07:00
ae7fce9e77 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.
2026-10-22 09:46:11 +07:00
2 changed files with 70 additions and 115 deletions

View File

@@ -83,6 +83,27 @@ Eigen::Isometry3d toIsometry(const tf3::TransformStampedMsg& t)
return iso;
}
// 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)
{
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;
}
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)
{
if (!ok)
@@ -237,80 +258,36 @@ void DepthCameraObstacleLayer::loadParameters()
throw std::runtime_error("DepthCameraObstacleLayer " + name_ + ": " + config_file_name +
" has no depth_camera_obstacle_layer: block");
// ===== 1. Defaults from the layer's file =====
// track_unknown_space is not in the file on purpose: unset, the layer follows the costmap.
bool track_unknown_space = loadParam(layer, "track_unknown_space", layered_costmap_->isTrackingUnknown());
enabled_ = loadParam(layer, "enabled", true);
footprint_clearing_enabled_ = loadParam(layer, "footprint_clearing_enabled", true);
combination_method_ = loadParam(layer, "combination_method", 1);
enable_near_blocked_protection_ = loadParam(layer, "enable_near_blocked_protection", true);
number_points_considered_as_blocked_ = loadParam(layer, "number_points_considered_as_blocked", 5);
forced_clearing_distance_ = loadParam(layer, "forced_clearing_distance", 0.1);
ec_seg_distance_ = loadParam(layer, "ec_seg_distance", 0.2);
ec_cluster_min_size_ = loadParam(layer, "ec_cluster_min_size", 5);
size_of_cluster_rejection_ = loadParam(layer, "size_of_cluster_rejection", 5);
voxel_resolution_ = loadParam(layer, "voxel_resolution", 0.01);
check_radius_ = loadParam(layer, "check_radius", 0.1);
number_clearing_threshold_ = loadParam(layer, "number_clearing_threshold", 2);
use_voxelized_observation_ = loadParam(layer, "use_voxelized_observation", true);
transform_tolerance_ = loadParam(layer, "transform_tolerance", 0.2);
bool use_global_frame_to_mark = loadParam(layer, "use_global_frame_to_mark", true);
std::string topics_string = loadParam(layer, "observation_sources", std::string(""));
debug_stats_ = loadParam(layer["debug"], "stats", true);
debug_stats_period_ = loadParam(layer["debug"], "stats_period", 5.0);
// ===== 2. Override from "<costmap>/<plugin>", which copyParentParameters() filled from the
// shared "<plugin>:" block (only when the key exists) =====
// getParam() gets the current value as its default: a value of the wrong type then keeps the
// file default instead of becoming 0/false/"". A wrong type in the shared block already stopped
// copyParentParameters().
// Step 3: "<costmap>/<plugin>", filled by copyParentParameters() from the shared block.
robot::NodeHandle nh("~");
robot::NodeHandle priv_nh(nh, name_);
if (priv_nh.hasParam("track_unknown_space"))
priv_nh.getParam("track_unknown_space", track_unknown_space, track_unknown_space);
if (priv_nh.hasParam("enabled"))
priv_nh.getParam("enabled", enabled_, enabled_);
if (priv_nh.hasParam("footprint_clearing_enabled"))
priv_nh.getParam("footprint_clearing_enabled", footprint_clearing_enabled_, footprint_clearing_enabled_);
if (priv_nh.hasParam("combination_method"))
priv_nh.getParam("combination_method", combination_method_, combination_method_);
if (priv_nh.hasParam("enable_near_blocked_protection"))
priv_nh.getParam("enable_near_blocked_protection", enable_near_blocked_protection_,
enable_near_blocked_protection_);
if (priv_nh.hasParam("number_points_considered_as_blocked"))
priv_nh.getParam("number_points_considered_as_blocked", number_points_considered_as_blocked_,
number_points_considered_as_blocked_);
if (priv_nh.hasParam("forced_clearing_distance"))
priv_nh.getParam("forced_clearing_distance", forced_clearing_distance_, forced_clearing_distance_);
if (priv_nh.hasParam("ec_seg_distance"))
priv_nh.getParam("ec_seg_distance", ec_seg_distance_, ec_seg_distance_);
if (priv_nh.hasParam("ec_cluster_min_size"))
priv_nh.getParam("ec_cluster_min_size", ec_cluster_min_size_, ec_cluster_min_size_);
if (priv_nh.hasParam("size_of_cluster_rejection"))
priv_nh.getParam("size_of_cluster_rejection", size_of_cluster_rejection_, size_of_cluster_rejection_);
if (priv_nh.hasParam("voxel_resolution"))
priv_nh.getParam("voxel_resolution", voxel_resolution_, voxel_resolution_);
if (priv_nh.hasParam("check_radius"))
priv_nh.getParam("check_radius", check_radius_, check_radius_);
if (priv_nh.hasParam("number_clearing_threshold"))
priv_nh.getParam("number_clearing_threshold", number_clearing_threshold_, number_clearing_threshold_);
if (priv_nh.hasParam("use_voxelized_observation"))
priv_nh.getParam("use_voxelized_observation", use_voxelized_observation_, use_voxelized_observation_);
if (priv_nh.hasParam("transform_tolerance"))
priv_nh.getParam("transform_tolerance", transform_tolerance_, transform_tolerance_);
if (priv_nh.hasParam("use_global_frame_to_mark"))
priv_nh.getParam("use_global_frame_to_mark", use_global_frame_to_mark, use_global_frame_to_mark);
if (priv_nh.hasParam("observation_sources"))
priv_nh.getParam("observation_sources", topics_string, topics_string);
if (priv_nh.hasParam("debug/stats"))
priv_nh.getParam("debug/stats", debug_stats_, debug_stats_);
if (priv_nh.hasParam("debug/stats_period"))
priv_nh.getParam("debug/stats_period", debug_stats_period_, debug_stats_period_);
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_ = 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());
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.
@@ -329,58 +306,29 @@ void DepthCameraObstacleLayer::loadParameters()
marking_height_under_ground_ = 100.0;
marking_height_above_ground_ = -100.0;
std::stringstream ss(topics_string);
std::stringstream ss(readParam(layer, priv_nh, "observation_sources", std::string("")));
std::string name;
while (ss >> 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;
// ===== 1. Defaults. Like obstacle_layer_params.yaml, the file has no block per source, so
// these are the C++ defaults unless the file gains one =====
src->topic = loadParam(layer[name], "topic", name);
src->sensor_frame = loadParam(layer[name], "sensor_frame", std::string(""));
src->sensor_frame_is_optical = loadParam(layer[name], "sensor_frame_is_optical", false);
src->observation_keep_time = loadParam(layer[name], "observation_persistence", 0.0);
src->expected_update_rate = loadParam(layer[name], "expected_update_rate", 0.0);
src->min_obstacle_height = loadParam(layer[name], "min_obstacle_height", 0.0);
src->max_obstacle_height = loadParam(layer[name], "max_obstacle_height", 2.0);
src->marking = loadParam(layer[name], "marking", true);
src->fov_v = loadParam(layer[name], "FOV_V", 1.0);
src->fov_w = loadParam(layer[name], "FOV_W", 1.5);
src->min_detect_distance = loadParam(layer[name], "min_detect_distance", 0.15);
src->max_detect_distance = loadParam(layer[name], "max_detect_distance", 2.5);
bool inf_is_valid = loadParam(layer[name], "inf_is_valid", false);
// ===== 2. Override from "<costmap>/<plugin>/<source>" =====
robot::NodeHandle source_nh(priv_nh, name);
if (source_nh.hasParam("topic"))
source_nh.getParam("topic", src->topic, src->topic);
if (source_nh.hasParam("sensor_frame"))
source_nh.getParam("sensor_frame", src->sensor_frame, src->sensor_frame);
if (source_nh.hasParam("sensor_frame_is_optical"))
source_nh.getParam("sensor_frame_is_optical", src->sensor_frame_is_optical, src->sensor_frame_is_optical);
if (source_nh.hasParam("observation_persistence"))
source_nh.getParam("observation_persistence", src->observation_keep_time, src->observation_keep_time);
if (source_nh.hasParam("expected_update_rate"))
source_nh.getParam("expected_update_rate", src->expected_update_rate, src->expected_update_rate);
if (source_nh.hasParam("min_obstacle_height"))
source_nh.getParam("min_obstacle_height", src->min_obstacle_height, src->min_obstacle_height);
if (source_nh.hasParam("max_obstacle_height"))
source_nh.getParam("max_obstacle_height", src->max_obstacle_height, src->max_obstacle_height);
if (source_nh.hasParam("marking"))
source_nh.getParam("marking", src->marking, src->marking);
if (source_nh.hasParam("FOV_V"))
source_nh.getParam("FOV_V", src->fov_v, src->fov_v);
if (source_nh.hasParam("FOV_W"))
source_nh.getParam("FOV_W", src->fov_w, src->fov_w);
if (source_nh.hasParam("min_detect_distance"))
source_nh.getParam("min_detect_distance", src->min_detect_distance, src->min_detect_distance);
if (source_nh.hasParam("max_detect_distance"))
source_nh.getParam("max_detect_distance", src->max_detect_distance, src->max_detect_distance);
if (source_nh.hasParam("inf_is_valid"))
source_nh.getParam("inf_is_valid", inf_is_valid, inf_is_valid);
if (inf_is_valid)
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

@@ -459,6 +459,13 @@ TEST(DepthCameraLayerConfig, RejectsUnportedModeAndOutOfRangeValues)
cfg = baseConfig();
cfg["voxel_resolution"] = 0.0;
expectThrow(cfg);
// A value of the wrong type stops the layer instead of becoming 0.
cfg = baseConfig();
cfg["number_clearing_threshold"] = "many";
expectThrow(cfg);
cfg = baseConfig();
cfg["depth_cam"]["FOV_W"] = "wide";
expectThrow(cfg);
}
TEST(DepthCameraLayerConfig, RefusesToStartWithoutItsDefaultFile)