#include "dock_planner/dock_planner_config.h" #include #include namespace dock_planner { namespace { bool readOptionalDouble(const robot::NodeHandle& nh, const std::string& key, double& value, std::string& error) { if (!nh.hasParam(key)) { return true; } double parsed = value; if (!nh.getParam(key, parsed, value)) { error = "parameter '" + key + "' must be numeric"; return false; } value = parsed; return true; } bool readDockAnchor(const robot::NodeHandle& nh, DockAnchor& anchor, std::string& error) { if (!nh.hasParam("dock_anchor")) { return true; } std::string parsed; if (!nh.getParam("dock_anchor", parsed, std::string())) { error = "parameter 'dock_anchor' must be a string"; return false; } if (parsed == "center") { anchor = DockAnchor::kCenter; return true; } if (parsed == "face") { anchor = DockAnchor::kFace; return true; } error = "parameter 'dock_anchor' must be 'center' or 'face' (got '" + parsed + "')"; return false; } bool requireFinite(const char* name, double value, std::string& error) { if (std::isfinite(value)) { return true; } error = std::string("parameter '") + name + "' must be finite"; return false; } } // namespace bool DockPlannerConfig::load(const robot::NodeHandle& nh, DockPlannerConfig& config, std::string& error) { DockPlannerConfig loaded; if (!readOptionalDouble(nh, "approach/alignment_distance_padding", loaded.alignment_distance_padding, error) || !readOptionalDouble(nh, "approach/lateral_distance_near", loaded.lateral_distance_near, error) || !readOptionalDouble(nh, "approach/lateral_distance_far", loaded.lateral_distance_far, error) || !readOptionalDouble(nh, "approach/angle_threshold_near", loaded.angle_threshold_near, error) || !readOptionalDouble(nh, "approach/angle_threshold_far", loaded.angle_threshold_far, error) || !readOptionalDouble(nh, "approach/direction_angle_hysteresis", loaded.direction_angle_hysteresis, error) || !readOptionalDouble(nh, "approach/direction_distance_tolerance", loaded.direction_distance_tolerance, error) || !readOptionalDouble(nh, "sampling/path_point_spacing", loaded.path_point_spacing, error)) { return false; } if (!readDockAnchor(nh, loaded.dock_anchor, error)) { return false; } if (!loaded.validate(error)) { return false; } config = loaded; error.clear(); return true; } bool DockPlannerConfig::validate(std::string& error) const { const struct { const char* name; double value; } values[] = { {"approach/alignment_distance_padding", alignment_distance_padding}, {"approach/lateral_distance_near", lateral_distance_near}, {"approach/lateral_distance_far", lateral_distance_far}, {"approach/angle_threshold_near", angle_threshold_near}, {"approach/angle_threshold_far", angle_threshold_far}, {"approach/direction_angle_hysteresis", direction_angle_hysteresis}, {"approach/direction_distance_tolerance", direction_distance_tolerance}, {"sampling/path_point_spacing", path_point_spacing}, }; for (const auto& entry : values) { if (!requireFinite(entry.name, entry.value, error)) { return false; } } if (alignment_distance_padding < 0.0) { error = "parameter 'approach/alignment_distance_padding' must be >= 0 m"; return false; } if (lateral_distance_near < 0.0 || lateral_distance_far <= lateral_distance_near) { error = "approach lateral distances must satisfy 0 <= near < far"; return false; } if (angle_threshold_far <= 0.0 || angle_threshold_near >= kPi || angle_threshold_near <= angle_threshold_far) { error = "approach angle thresholds must satisfy 0 < far < near < pi"; return false; } if (direction_angle_hysteresis < 0.0 || direction_angle_hysteresis >= angle_threshold_far) { error = "parameter 'approach/direction_angle_hysteresis' must be >= 0 and smaller than angle_threshold_far"; return false; } if (direction_distance_tolerance < 0.0) { error = "parameter 'approach/direction_distance_tolerance' must be >= 0 m"; return false; } if (path_point_spacing <= 0.0 || path_point_spacing > 1.0) { error = "parameter 'sampling/path_point_spacing' must be in (0, 1] m"; return false; } error.clear(); return true; } double DockPlannerConfig::angleThreshold(double lateral_distance) const { const double clamped = std::clamp(lateral_distance, lateral_distance_near, lateral_distance_far); const double ratio = (clamped - lateral_distance_near) / (lateral_distance_far - lateral_distance_near); return angle_threshold_near + ratio * (angle_threshold_far - angle_threshold_near); } } // namespace dock_planner