Files
dock_planner/src/dock_planner_config.cpp
duongtd 9d31c0e5db feat: add dock_anchor mating_edge for goals already offset to the docked pose
The charger detector offsets its target to the docked base_link pose (offset_x 0.40 from
the V apex), so the charger face is at the robot's rear edge at the goal. dock_anchor=face
places the face at the goal itself, so the pivot vertex came out 0.624 m in front of the
goal, about 0.40 m more than needed. Robots standing 0.38-0.58 m out had to back off or
failed to plan.

dock_anchor=mating_edge keeps every face behaviour, except that the on-the-spot pivot
clearance is measured from the real face, mating_extent behind the goal:
vertex = max(straight_length, circumscribed - mating_extent + pivot_safety_margin).
For the KIT robot (0.65 x 0.69) the vertex drops from 0.624 m to 0.4 m. footprint_dock is
ignored, as with face. center, face, direct entry and single cusp are unchanged.

Test: the 29/10 starts 0.579/0.292 and 0.427/0.155 now pivot directly at 0.4 m;
0.375/-0.149 backs off only to 0.45 m (26 poses against 36 with face).
2026-10-29 15:59:47 +07:00

325 lines
9.9 KiB
C++

#include "dock_planner/dock_planner_config.h"
#include <cmath>
#include <limits>
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 readOptionalBool(const robot::NodeHandle& nh, const std::string& key,
bool& value, std::string& error)
{
if (!nh.hasParam(key))
{
return true;
}
bool parsed = value;
if (!nh.getParam(key, parsed, value))
{
error = "parameter '" + key + "' must be a boolean";
return false;
}
value = parsed;
return true;
}
bool readOptionalString(const robot::NodeHandle& nh, const std::string& key,
std::string& value, std::string& error)
{
if (!nh.hasParam(key))
{
return true;
}
std::string parsed;
if (!nh.getParam(key, parsed, std::string()))
{
error = "parameter '" + key + "' must be a string";
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;
}
if (parsed == "mating_edge")
{
anchor = DockAnchor::kMatingEdge;
return true;
}
error = "parameter 'dock_anchor' must be 'center', 'face' or 'mating_edge' (got '" +
parsed + "')";
return false;
}
bool readKinematicModel(const robot::NodeHandle& nh, KinematicModel& model,
std::string& error)
{
if (!nh.hasParam("kinematics/model"))
{
return true;
}
std::string parsed;
if (!nh.getParam("kinematics/model", parsed, std::string()))
{
error = "parameter 'kinematics/model' must be a string";
return false;
}
if (parsed == "ackermann")
{
model = KinematicModel::kAckermann;
return true;
}
if (parsed == "differential")
{
model = KinematicModel::kDifferential;
return true;
}
error = "parameter 'kinematics/model' must be 'ackermann' or 'differential' (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 (!readKinematicModel(nh, loaded.kinematic_model, error) ||
!readOptionalDouble(nh, "kinematics/minimum_turning_radius",
loaded.minimum_turning_radius, error) ||
!readOptionalDouble(nh, "maneuver/pivot_fallback_extension",
loaded.pivot_fallback_extension, error) ||
!readOptionalDouble(nh, "maneuver/pivot_safety_margin",
loaded.pivot_safety_margin, error) ||
!readOptionalDouble(nh, "maneuver/pivot_backoff_max_distance",
loaded.pivot_backoff_max_distance, error) ||
!readOptionalBool(nh, "maneuver/pivot_check_costmap",
loaded.pivot_check_costmap, error) ||
!readOptionalString(nh, "kinematics/front_axle_frame",
loaded.front_axle_frame, error) ||
!readOptionalString(nh, "kinematics/rear_axle_frame",
loaded.rear_axle_frame, error) ||
!readOptionalDouble(nh, "kinematics/max_steering_angle",
loaded.max_steering_angle, error) ||
!readOptionalDouble(nh, "terminal/reverse_distance_padding",
loaded.reverse_distance_padding, error) ||
!readOptionalDouble(nh, "maneuver/minimum_forward_distance",
loaded.minimum_forward_distance, error) ||
!readOptionalDouble(nh, "maneuver/maximum_forward_extension",
loaded.maximum_forward_extension, error) ||
!readOptionalDouble(nh, "terminal/straight_length",
loaded.straight_length, error) ||
!readOptionalDouble(nh, "maneuver/direct_entry_min_curve_room",
loaded.direct_entry_min_curve_room, 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[] = {
{"kinematics/max_steering_angle", max_steering_angle},
{"kinematics/wheel_base", wheel_base},
{"kinematics/minimum_turning_radius", minimum_turning_radius},
{"maneuver/pivot_fallback_extension", pivot_fallback_extension},
{"maneuver/pivot_safety_margin", pivot_safety_margin},
{"maneuver/pivot_backoff_max_distance", pivot_backoff_max_distance},
{"terminal/reverse_distance_padding", reverse_distance_padding},
{"terminal/straight_length", straight_length},
{"maneuver/minimum_forward_distance", minimum_forward_distance},
{"maneuver/maximum_forward_extension", maximum_forward_extension},
{"maneuver/direct_entry_min_curve_room", direct_entry_min_curve_room},
{"sampling/path_point_spacing", path_point_spacing},
};
for (const auto& entry : values)
{
if (!requireFinite(entry.name, entry.value, error))
{
return false;
}
}
// A differential base has no steered axle: validating axle frames or a
// steering limit there would reject a perfectly valid configuration and
// stop the plugin from loading at all.
if (kinematic_model == KinematicModel::kAckermann)
{
if (front_axle_frame.empty() || rear_axle_frame.empty())
{
error = "kinematics axle frame names must not be empty";
return false;
}
if (front_axle_frame == rear_axle_frame)
{
error = "kinematics front_axle_frame and rear_axle_frame must be different";
return false;
}
if (max_steering_angle <= 0.0 || max_steering_angle >= kPi / 2.0)
{
error = "parameter 'kinematics/max_steering_angle' must be in (0, pi/2) rad";
return false;
}
if (wheel_base < 0.0)
{
error = "resolved kinematics wheel_base must be >= 0 m";
return false;
}
}
else if (minimum_turning_radius < 0.0)
{
error = "parameter 'kinematics/minimum_turning_radius' must be >= 0 m "
"(0 allows turning on the spot)";
return false;
}
if (reverse_distance_padding < 0.0)
{
error = "parameter 'terminal/reverse_distance_padding' must be >= 0 m";
return false;
}
if (straight_length < 0.0)
{
error = "parameter 'terminal/straight_length' must be >= 0 m";
return false;
}
if (direct_entry_min_curve_room < 0.0)
{
error = "parameter 'maneuver/direct_entry_min_curve_room' must be >= 0 m "
"(0 disables the direct entry)";
return false;
}
if (minimum_forward_distance <= 0.0)
{
error = "parameter 'maneuver/minimum_forward_distance' must be > 0 m";
return false;
}
if (maximum_forward_extension < minimum_forward_distance)
{
error = "maneuver maximum_forward_extension must be >= minimum_forward_distance";
return false;
}
if (pivot_fallback_extension < 0.0)
{
error = "parameter 'maneuver/pivot_fallback_extension' must be >= 0 m "
"(0 disables the straight-pivot-straight shape)";
return false;
}
if (pivot_safety_margin < 0.0)
{
error = "parameter 'maneuver/pivot_safety_margin' must be >= 0 m";
return false;
}
if (pivot_backoff_max_distance < 0.0 || pivot_backoff_max_distance > 3.0)
{
error = "parameter 'maneuver/pivot_backoff_max_distance' must be in [0, 3] 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::minimumTurningRadius() const
{
if (kinematic_model == KinematicModel::kDifferential)
{
// 0 is a legal answer here, not a failure: it is what lets the planner
// place an explicit on-the-spot rotation in the path.
if (!std::isfinite(minimum_turning_radius) || minimum_turning_radius < 0.0)
{
return std::numeric_limits<double>::quiet_NaN();
}
return minimum_turning_radius;
}
if (!(wheel_base > 0.0) || !(max_steering_angle > 0.0) ||
!(max_steering_angle < kPi / 2.0))
{
return std::numeric_limits<double>::quiet_NaN();
}
return wheel_base / std::tan(max_steering_angle);
}
} // namespace dock_planner