Files
dock_planner/src/dock_planner_config.cpp
duongtd 539fc5da16 Refactor DockPlanner: typed config, dock anchor, validated dock footprint
Split configuration out of the planner into DockPlannerConfig with explicit
load/validate, so a missing or malformed key fails at initialize() with a
message instead of silently defaulting to zero.

- initialize() now scopes its NodeHandle to the plugin name. The previous
  "~/<name>" form never resolved ("~" is not a key in the merged tree) and
  fell back to ROOT, so every parameter was read from the wrong place and
  cost_threshold silently dropped from 200 to 0.
- Replace robot_nav_2d_utils::footprintFromParams(), whose declaration and
  definition disagreed on the argument type. The symbol was never defined,
  and because the plugin is dlopen'd lazily the failure only surfaced at the
  call site on the robot, long after a clean build.
- Read footprint_dock, the dock object's polygon, with range and NaN checks.
  move_base2 copies it in per marker before the instance is built.
- dock_anchor selects whether the marker goal sits at the dock centre or on
  its approach face; the face case ignores footprint_dock entirely.
- makePlan() rejects start/goal outside the costmap global frame instead of
  planning across mismatched frames.
- Add dock_planner_test covering config validation, footprint parsing and
  path generation.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-12 21:52:52 +07:00

179 lines
5.4 KiB
C++

#include "dock_planner/dock_planner_config.h"
#include <algorithm>
#include <cmath>
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