diff --git a/plugins/depth_camera_obstacle_layer.cpp b/plugins/depth_camera_obstacle_layer.cpp index 604ed23..e692411 100644 --- a/plugins/depth_camera_obstacle_layer.cpp +++ b/plugins/depth_camera_obstacle_layer.cpp @@ -83,27 +83,6 @@ 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 "//", which Costmap2DROBOT::copyParentParameters() has -// filled from the shared ":" 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 -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 -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) @@ -258,36 +237,80 @@ void DepthCameraObstacleLayer::loadParameters() throw std::runtime_error("DepthCameraObstacleLayer " + name_ + ": " + config_file_name + " has no depth_camera_obstacle_layer: block"); - // Step 3: "/", filled by copyParentParameters() from the shared 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 "/", which copyParentParameters() filled from the + // shared ":" 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(). robot::NodeHandle nh("~"); - const robot::NodeHandle priv_nh(nh, name_); + 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_); - // 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. @@ -306,29 +329,58 @@ void DepthCameraObstacleLayer::loadParameters() marking_height_under_ground_ = 100.0; marking_height_above_ground_ = -100.0; - std::stringstream ss(readParam(layer, priv_nh, "observation_sources", std::string(""))); + std::stringstream ss(topics_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 "///" or falls back to the C++ default below. - YAML::Node source_defaults = layer[name]; - const robot::NodeHandle s(priv_nh, name); std::unique_ptr src(new Source()); src->name = name; - 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)) + + // ===== 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 "//" ===== + 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) robot::log_warning("DepthCameraObstacleLayer: inf_is_valid is not applicable to PointCloud observations\n"); const std::string what = "source " + name + ": "; diff --git a/test/depth_camera_obstacle_layer_test.cpp b/test/depth_camera_obstacle_layer_test.cpp index a144df4..f3ea4f4 100644 --- a/test/depth_camera_obstacle_layer_test.cpp +++ b/test/depth_camera_obstacle_layer_test.cpp @@ -459,13 +459,6 @@ 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)