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:
@@ -48,6 +48,9 @@
|
||||
#include <tf3/convert.h>
|
||||
#include <tf3/utils.h>
|
||||
#include <exception>
|
||||
#include <initializer_list>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/dll/import.hpp>
|
||||
|
||||
@@ -63,10 +66,26 @@ void move_parameter(robot::NodeHandle& old_h, robot::NodeHandle& new_h, std::str
|
||||
if (!old_h.hasParam(name))
|
||||
return;
|
||||
|
||||
old_h.getParam(name, value);
|
||||
// A failed getParam() writes its default (0, false, "") into value; copying that would replace
|
||||
// the layer's own default with a silent zero. A key of the wrong type is a configuration error.
|
||||
if (!old_h.getParam(name, value, value))
|
||||
throw std::invalid_argument("'" + old_h.getNamespace() + "/" + name +
|
||||
"' does not convert to the type the layer reads");
|
||||
new_h.setParam(name, value);
|
||||
}
|
||||
|
||||
// Moves each key of names that exists under old_h, read as T.
|
||||
template <class T>
|
||||
void move_parameters(robot::NodeHandle& old_h, robot::NodeHandle& new_h,
|
||||
std::initializer_list<const char*> names)
|
||||
{
|
||||
for (const char* name : names)
|
||||
{
|
||||
T value{};
|
||||
move_parameter(old_h, new_h, name, value);
|
||||
}
|
||||
}
|
||||
|
||||
Costmap2DROBOT::Costmap2DROBOT(const std::string& name, tf3::BufferCore& tf) :
|
||||
layered_costmap_(NULL),
|
||||
name_(name),
|
||||
@@ -109,6 +128,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 +254,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 +301,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 +361,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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,132 +374,81 @@ void Costmap2DROBOT::copyParentParameters(const std::string& costmap_name,
|
||||
const std::string& plugin_type,
|
||||
robot::NodeHandle& nh)
|
||||
{
|
||||
// Step 2 of the layer config flow. A layer reads its default file, then overrides each key found
|
||||
// in "<costmap>/<plugin>". This copies the keys of the shared "<plugin>:" block there first, so the
|
||||
// lists below must hold every key the layer reads with hasParam(): a key missing here is ignored
|
||||
// without a word when it is set in the shared block. A copied key overwrites the same key in
|
||||
// "<costmap>/<plugin>". Types follow what the layer reads.
|
||||
robot::NodeHandle costmap_nh(nh, costmap_name);
|
||||
robot::NodeHandle costmap_plugin_nh(costmap_nh, plugin_name);
|
||||
robot::NodeHandle plugin_nh(nh, plugin_name);
|
||||
if(plugin_type == "StaticLayer")
|
||||
{
|
||||
std::string map_topic;
|
||||
int unknown_cost_value;
|
||||
int lethal_cost_threshold;
|
||||
bool track_unknown_space;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "map_topic", map_topic);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "unknown_cost_value", unknown_cost_value);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "lethal_cost_threshold", lethal_cost_threshold);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "track_unknown_space", track_unknown_space);
|
||||
}
|
||||
else if(plugin_type == "VoxelLayer")
|
||||
{
|
||||
double origin_z;
|
||||
double z_resolution;
|
||||
int z_voxels;
|
||||
int mark_threshold;
|
||||
int unknown_threshold;
|
||||
bool publish_voxel_map;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "origin_z", origin_z);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "z_resolution", z_resolution);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "z_voxels", z_voxels);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "mark_threshold", mark_threshold);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "unknown_threshold", unknown_threshold);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "publish_voxel_map", publish_voxel_map);
|
||||
if(plugin_nh.hasParam("observation_sources"))
|
||||
|
||||
// observation_sources, then the keys of each source under "<plugin>/<source>/".
|
||||
const auto move_sources = [&](const auto& move_source_keys) {
|
||||
std::string topics_string;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "observation_sources", topics_string);
|
||||
std::stringstream ss(topics_string);
|
||||
std::string source;
|
||||
while (ss >> source)
|
||||
{
|
||||
std::string topics_string;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "observation_sources", topics_string);
|
||||
robot::log_error("topics_string: %s", topics_string.c_str());
|
||||
std::stringstream ss(topics_string);
|
||||
std::string source;
|
||||
while (ss >> source)
|
||||
{
|
||||
robot::NodeHandle plugin_nh_element(plugin_nh, source);
|
||||
robot::NodeHandle costmap_plugin_nh_element(costmap_plugin_nh, source);
|
||||
std::string topic;
|
||||
std::string data_type;
|
||||
bool clearing;
|
||||
bool marking;
|
||||
bool inf_is_valid;
|
||||
std::string sensor_frame;
|
||||
double observation_persistence;
|
||||
double expected_update_rate;
|
||||
double min_obstacle_height;
|
||||
double max_obstacle_height;
|
||||
double obstacle_range;
|
||||
double raytrace_range;
|
||||
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "topic", topic);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "sensor_frame", sensor_frame);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "observation_persistence", observation_persistence);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "expected_update_rate", expected_update_rate);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "data_type", data_type);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "clearing", clearing);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "marking", marking);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "inf_is_valid", inf_is_valid);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "min_obstacle_height", min_obstacle_height);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "max_obstacle_height", max_obstacle_height);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "obstacle_range", obstacle_range);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "raytrace_range", raytrace_range);
|
||||
robot::log_info("topic: %s data_type: %s clearing: %d marking: %d inf_is_valid: %d min_obstacle_height: %f max_obstacle_height: %f", topic.c_str(), data_type.c_str(), clearing, marking, inf_is_valid, min_obstacle_height, max_obstacle_height);
|
||||
}
|
||||
robot::NodeHandle plugin_nh_element(plugin_nh, source);
|
||||
robot::NodeHandle costmap_plugin_nh_element(costmap_plugin_nh, source);
|
||||
move_source_keys(plugin_nh_element, costmap_plugin_nh_element);
|
||||
}
|
||||
}
|
||||
else if(plugin_type == "ObstacleLayer")
|
||||
};
|
||||
|
||||
if (plugin_type == "StaticLayer")
|
||||
{
|
||||
double max_obstacle_height;
|
||||
double raytrace_range;
|
||||
double obstacle_range;
|
||||
bool track_unknown_space;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "max_obstacle_height", max_obstacle_height);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "raytrace_range", raytrace_range);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "obstacle_range", obstacle_range);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "track_unknown_space", track_unknown_space);
|
||||
if(plugin_nh.hasParam("observation_sources"))
|
||||
move_parameters<std::string>(plugin_nh, costmap_plugin_nh, {"map_topic", "base_frame_id"});
|
||||
move_parameters<int>(plugin_nh, costmap_plugin_nh, {"unknown_cost_value", "lethal_cost_threshold"});
|
||||
move_parameters<bool>(plugin_nh, costmap_plugin_nh,
|
||||
{"enabled", "track_unknown_space", "first_map_only", "subscribe_to_updates",
|
||||
"use_maximum", "trinary_costmap"});
|
||||
}
|
||||
else if (plugin_type == "ObstacleLayer" || plugin_type == "VoxelLayer")
|
||||
{
|
||||
// VoxelLayer runs ObstacleLayer::getParams() first, so it reads these keys too.
|
||||
move_parameters<double>(plugin_nh, costmap_plugin_nh, {"max_obstacle_height", "transform_tolerance"});
|
||||
move_parameters<int>(plugin_nh, costmap_plugin_nh, {"combination_method"});
|
||||
move_parameters<bool>(plugin_nh, costmap_plugin_nh, {"track_unknown_space", "footprint_clearing_enabled"});
|
||||
if (plugin_type == "VoxelLayer")
|
||||
{
|
||||
std::string topics_string;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "observation_sources", topics_string);
|
||||
robot::log_error("topics_string: %s", topics_string.c_str());
|
||||
std::stringstream ss(topics_string);
|
||||
std::string source;
|
||||
while (ss >> source)
|
||||
{
|
||||
robot::NodeHandle plugin_nh_element(plugin_nh, source);
|
||||
robot::NodeHandle costmap_plugin_nh_element(costmap_plugin_nh, source);
|
||||
std::string topic;
|
||||
std::string data_type;
|
||||
bool clearing;
|
||||
bool marking;
|
||||
bool inf_is_valid;
|
||||
std::string sensor_frame;
|
||||
double observation_persistence;
|
||||
double expected_update_rate;
|
||||
double min_obstacle_height;
|
||||
double max_obstacle_height;
|
||||
double obstacle_range;
|
||||
double raytrace_range;
|
||||
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "topic", topic);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "sensor_frame", sensor_frame);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "observation_persistence", observation_persistence);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "expected_update_rate", expected_update_rate);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "data_type", data_type);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "clearing", clearing);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "marking", marking);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "inf_is_valid", inf_is_valid);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "min_obstacle_height", min_obstacle_height);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "max_obstacle_height", max_obstacle_height);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "obstacle_range", obstacle_range);
|
||||
move_parameter(plugin_nh_element, costmap_plugin_nh_element, "raytrace_range", raytrace_range);
|
||||
robot::log_info("topic: %s data_type: %s clearing: %d marking: %d inf_is_valid: %d min_obstacle_height: %f max_obstacle_height: %f", topic.c_str(), data_type.c_str(), clearing, marking, inf_is_valid, min_obstacle_height, max_obstacle_height);
|
||||
}
|
||||
move_parameters<double>(plugin_nh, costmap_plugin_nh, {"origin_z", "z_resolution", "unknown_threshold"});
|
||||
move_parameters<int>(plugin_nh, costmap_plugin_nh, {"z_voxels", "mark_threshold"});
|
||||
move_parameters<bool>(plugin_nh, costmap_plugin_nh, {"enabled"});
|
||||
}
|
||||
move_sources([](robot::NodeHandle& from, robot::NodeHandle& to) {
|
||||
move_parameters<std::string>(from, to, {"topic", "sensor_frame", "data_type"});
|
||||
move_parameters<double>(from, to, {"observation_persistence", "expected_update_rate", "min_obstacle_height",
|
||||
"max_obstacle_height", "obstacle_range", "raytrace_range"});
|
||||
move_parameters<bool>(from, to, {"clearing", "marking", "inf_is_valid"});
|
||||
});
|
||||
}
|
||||
else if(plugin_type == "InflationLayer")
|
||||
else if (plugin_type == "InflationLayer")
|
||||
{
|
||||
double cost_scaling_factor;
|
||||
double inflation_radius;
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "cost_scaling_factor", cost_scaling_factor);
|
||||
move_parameter(plugin_nh, costmap_plugin_nh, "inflation_radius", inflation_radius);
|
||||
move_parameters<double>(plugin_nh, costmap_plugin_nh, {"cost_scaling_factor", "inflation_radius"});
|
||||
move_parameters<bool>(plugin_nh, costmap_plugin_nh, {"enabled", "inflate_unknown"});
|
||||
}
|
||||
else if (plugin_type == "DepthCameraObstacleLayer")
|
||||
{
|
||||
move_parameters<bool>(plugin_nh, costmap_plugin_nh,
|
||||
{"track_unknown_space", "enabled", "footprint_clearing_enabled",
|
||||
"enable_near_blocked_protection", "use_voxelized_observation", "use_global_frame_to_mark",
|
||||
"clear_touches_bounds", "debug/stats"});
|
||||
move_parameters<int>(plugin_nh, costmap_plugin_nh,
|
||||
{"combination_method", "number_points_considered_as_blocked", "ec_cluster_min_size",
|
||||
"size_of_cluster_rejection", "number_clearing_threshold"});
|
||||
move_parameters<double>(plugin_nh, costmap_plugin_nh,
|
||||
{"forced_clearing_distance", "ec_seg_distance", "voxel_resolution", "check_radius",
|
||||
"transform_tolerance", "debug/stats_period"});
|
||||
move_sources([](robot::NodeHandle& from, robot::NodeHandle& to) {
|
||||
move_parameters<std::string>(from, to, {"topic", "sensor_frame"});
|
||||
move_parameters<bool>(from, to, {"sensor_frame_is_optical", "marking", "inf_is_valid"});
|
||||
move_parameters<double>(from, to, {"observation_persistence", "expected_update_rate", "min_obstacle_height",
|
||||
"max_obstacle_height", "FOV_V", "FOV_W", "min_detect_distance",
|
||||
"max_detect_distance"});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Costmap2DROBOT::setUnpaddedRobotFootprintPolygon(const robot_geometry_msgs::Polygon& footprint)
|
||||
|
||||
Reference in New Issue
Block a user