terminal/straight_length is one global value, so every docking marker gets the same straight run into the dock. A marker profile can now set dock_straight_length (move_base2 copies it into the DockPlanner namespace and writes YAML null when the next marker lacks it). The terminal leg is max(cusp floor, dock_straight_length or terminal/straight_length). - Valid for every dock_anchor (center, face, mating_edge), unlike dock_face_offset and dock_pivot_safety_margin; finite and in [0, 2.0] m. - The init summary and the plan log print the effective length and whether it came from the marker or the default. - Test: missing and null keep the default, 0.30 lengthens the terminal leg, all three anchors load, out-of-range and NaN values are rejected.
449 lines
16 KiB
C++
449 lines
16 KiB
C++
#include "dock_planner/dock_planner.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <optional>
|
|
#include <utility>
|
|
|
|
#include <boost/dll/alias.hpp>
|
|
#include <boost/thread/locks.hpp>
|
|
#include <data_convert/data_convert.h>
|
|
#include <robot/robot.h>
|
|
#include <robot_costmap_2d/cost_values.h>
|
|
|
|
namespace dock_planner
|
|
{
|
|
namespace
|
|
{
|
|
|
|
enum class FootprintLoadStatus
|
|
{
|
|
kMissing,
|
|
kValid,
|
|
kInvalid,
|
|
};
|
|
|
|
FootprintLoadStatus loadDockFootprint(
|
|
const robot::NodeHandle& nh, const std::string& planner_name,
|
|
std::vector<robot_geometry_msgs::Point>& footprint)
|
|
{
|
|
footprint.clear();
|
|
YAML::Node node;
|
|
if (!nh.getParam("footprint_dock", node) || !node.IsDefined())
|
|
{
|
|
return FootprintLoadStatus::kMissing;
|
|
}
|
|
if (!node.IsSequence() || node.size() < 3)
|
|
{
|
|
robot::log_error("[dock_planner] '%s/footprint_dock' must contain at least three [x, y] points.\n",
|
|
planner_name.c_str());
|
|
return FootprintLoadStatus::kInvalid;
|
|
}
|
|
|
|
footprint.reserve(node.size());
|
|
for (std::size_t index = 0; index < node.size(); ++index)
|
|
{
|
|
const YAML::Node& point_node = node[index];
|
|
if (!point_node.IsSequence() || point_node.size() != 2)
|
|
{
|
|
robot::log_error("[dock_planner] '%s/footprint_dock[%zu]' must be exactly [x, y].\n",
|
|
planner_name.c_str(), index);
|
|
footprint.clear();
|
|
return FootprintLoadStatus::kInvalid;
|
|
}
|
|
|
|
robot_geometry_msgs::Point point;
|
|
try
|
|
{
|
|
point.x = point_node[0].as<double>();
|
|
point.y = point_node[1].as<double>();
|
|
}
|
|
catch (const YAML::Exception& exception)
|
|
{
|
|
robot::log_error("[dock_planner] '%s/footprint_dock[%zu]' is not numeric: %s\n",
|
|
planner_name.c_str(), index, exception.what());
|
|
footprint.clear();
|
|
return FootprintLoadStatus::kInvalid;
|
|
}
|
|
|
|
if (!std::isfinite(point.x) || !std::isfinite(point.y))
|
|
{
|
|
robot::log_error("[dock_planner] '%s/footprint_dock[%zu]' contains NaN/Inf.\n",
|
|
planner_name.c_str(), index);
|
|
footprint.clear();
|
|
return FootprintLoadStatus::kInvalid;
|
|
}
|
|
point.z = 0.0;
|
|
footprint.push_back(point);
|
|
}
|
|
|
|
const auto minmax_x = std::minmax_element(
|
|
footprint.begin(), footprint.end(),
|
|
[](const auto& first, const auto& second) { return first.x < second.x; });
|
|
const auto minmax_y = std::minmax_element(
|
|
footprint.begin(), footprint.end(),
|
|
[](const auto& first, const auto& second) { return first.y < second.y; });
|
|
if (minmax_x.second->x - minmax_x.first->x <= 1e-9 ||
|
|
minmax_y.second->y - minmax_y.first->y <= 1e-9)
|
|
{
|
|
robot::log_error("[dock_planner] '%s/footprint_dock' has zero X or Y extent.\n",
|
|
planner_name.c_str());
|
|
footprint.clear();
|
|
return FootprintLoadStatus::kInvalid;
|
|
}
|
|
return FootprintLoadStatus::kValid;
|
|
}
|
|
|
|
std::string optionalDistanceText(const std::optional<double>& value)
|
|
{
|
|
return value.has_value() ? std::to_string(*value) + " m" : "unset";
|
|
}
|
|
|
|
} // namespace
|
|
|
|
DockPlanner::DockPlanner() = default;
|
|
|
|
bool DockPlanner::rotationClear(const robot_geometry_msgs::Pose& pose,
|
|
double radius) const
|
|
{
|
|
// getCostmap() may return a replacement map after a resize. Keep this
|
|
// pointer local to the callback and protect every read with that map's mutex.
|
|
robot_costmap_2d::Costmap2D* costmap =
|
|
costmap_robot_ == nullptr ? nullptr : costmap_robot_->getCostmap();
|
|
if (costmap == nullptr || !std::isfinite(pose.position.x) ||
|
|
!std::isfinite(pose.position.y) || !std::isfinite(radius) || radius < 0.0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(
|
|
*costmap->getMutex());
|
|
const double resolution = costmap->getResolution();
|
|
const unsigned int size_x = costmap->getSizeInCellsX();
|
|
const unsigned int size_y = costmap->getSizeInCellsY();
|
|
if (!std::isfinite(resolution) || resolution <= 0.0 || size_x == 0 || size_y == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const double map_min_x = costmap->getOriginX();
|
|
const double map_min_y = costmap->getOriginY();
|
|
const double map_max_x = map_min_x + resolution * size_x;
|
|
const double map_max_y = map_min_y + resolution * size_y;
|
|
if (pose.position.x - radius < map_min_x ||
|
|
pose.position.y - radius < map_min_y ||
|
|
pose.position.x + radius >= map_max_x ||
|
|
pose.position.y + radius >= map_max_y)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int min_mx = 0;
|
|
int min_my = 0;
|
|
int max_mx = 0;
|
|
int max_my = 0;
|
|
costmap->worldToMapNoBounds(pose.position.x - radius,
|
|
pose.position.y - radius, min_mx, min_my);
|
|
costmap->worldToMapNoBounds(pose.position.x + radius,
|
|
pose.position.y + radius, max_mx, max_my);
|
|
min_mx = std::max(0, min_mx);
|
|
min_my = std::max(0, min_my);
|
|
max_mx = std::min(static_cast<int>(size_x) - 1, max_mx);
|
|
max_my = std::min(static_cast<int>(size_y) - 1, max_my);
|
|
|
|
const double half_cell = resolution * 0.5;
|
|
const double radius_squared = radius * radius;
|
|
for (int my = min_my; my <= max_my; ++my)
|
|
{
|
|
for (int mx = min_mx; mx <= max_mx; ++mx)
|
|
{
|
|
double cell_x = 0.0;
|
|
double cell_y = 0.0;
|
|
costmap->mapToWorld(static_cast<unsigned int>(mx),
|
|
static_cast<unsigned int>(my), cell_x, cell_y);
|
|
const double dx = std::max(
|
|
std::fabs(cell_x - pose.position.x) - half_cell, 0.0);
|
|
const double dy = std::max(
|
|
std::fabs(cell_y - pose.position.y) - half_cell, 0.0);
|
|
if (dx * dx + dy * dy <= radius_squared &&
|
|
costmap->getCost(static_cast<unsigned int>(mx),
|
|
static_cast<unsigned int>(my)) ==
|
|
robot_costmap_2d::LETHAL_OBSTACLE)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool DockPlanner::footprintClear(const robot_geometry_msgs::Pose& pose)
|
|
{
|
|
// As above, never retain getCostmap() across calls: the master can be
|
|
// replaced while a planner is running.
|
|
robot_costmap_2d::Costmap2D* costmap =
|
|
costmap_robot_ == nullptr ? nullptr : costmap_robot_->getCostmap();
|
|
if (costmap == nullptr || !std::isfinite(pose.position.x) ||
|
|
!std::isfinite(pose.position.y))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const auto& footprint = costmap_robot_->getRobotFootprint();
|
|
if (footprint.size() < 3)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(
|
|
*costmap->getMutex());
|
|
footprint_map_polygon_.clear();
|
|
footprint_map_cells_.clear();
|
|
footprint_map_polygon_.reserve(footprint.size());
|
|
const double yaw = data_convert::getYaw(pose.orientation);
|
|
if (!std::isfinite(yaw))
|
|
{
|
|
return false;
|
|
}
|
|
const double cos_yaw = std::cos(yaw);
|
|
const double sin_yaw = std::sin(yaw);
|
|
for (const auto& point : footprint)
|
|
{
|
|
const double world_x = pose.position.x +
|
|
cos_yaw * point.x - sin_yaw * point.y;
|
|
const double world_y = pose.position.y +
|
|
sin_yaw * point.x + cos_yaw * point.y;
|
|
robot_costmap_2d::MapLocation map_point;
|
|
if (!costmap->worldToMap(world_x, world_y, map_point.x, map_point.y))
|
|
{
|
|
return false;
|
|
}
|
|
footprint_map_polygon_.push_back(map_point);
|
|
}
|
|
|
|
costmap->convexFillCells(footprint_map_polygon_, footprint_map_cells_);
|
|
for (const auto& cell : footprint_map_cells_)
|
|
{
|
|
if (costmap->getCost(cell.x, cell.y) == robot_costmap_2d::LETHAL_OBSTACLE)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return !footprint_map_cells_.empty();
|
|
}
|
|
|
|
bool DockPlanner::initialize(std::string name,
|
|
robot_costmap_2d::Costmap2DROBOT* costmap_robot)
|
|
{
|
|
if (initialized_)
|
|
{
|
|
return true;
|
|
}
|
|
if (name.empty())
|
|
{
|
|
robot::log_error("[dock_planner] Cannot initialize with an empty plugin name.\n");
|
|
return false;
|
|
}
|
|
if (costmap_robot == nullptr || costmap_robot->getCostmap() == nullptr)
|
|
{
|
|
robot::log_error("[dock_planner] Cannot initialize without a valid costmap.\n");
|
|
return false;
|
|
}
|
|
if (costmap_robot->getGlobalFrameID().empty())
|
|
{
|
|
robot::log_error("[dock_planner] Costmap global frame is empty.\n");
|
|
return false;
|
|
}
|
|
|
|
robot::NodeHandle planner_nh(name);
|
|
if (planner_nh.getNamespace() == "/")
|
|
{
|
|
robot::log_error("[dock_planner] Parameter namespace '%s' does not exist.\n", name.c_str());
|
|
return false;
|
|
}
|
|
|
|
DockPlannerConfig loaded_config;
|
|
std::string config_error;
|
|
if (!DockPlannerConfig::load(planner_nh, loaded_config, config_error))
|
|
{
|
|
robot::log_error("[dock_planner] Invalid configuration for '%s': %s.\n",
|
|
name.c_str(), config_error.c_str());
|
|
return false;
|
|
}
|
|
|
|
if (loaded_config.requiresAxleTf())
|
|
{
|
|
try
|
|
{
|
|
// Match StanleyControl's axle-TF contract: lookup the front axle in
|
|
// the rear-axle frame, then use the planar translation norm.
|
|
const tf3::TransformStampedMsg axle_transform =
|
|
costmap_robot->getTfBuffer().lookupTransform(
|
|
loaded_config.rear_axle_frame,
|
|
loaded_config.front_axle_frame,
|
|
tf3::Time());
|
|
loaded_config.wheel_base = std::hypot(
|
|
axle_transform.transform.translation.x,
|
|
axle_transform.transform.translation.y);
|
|
}
|
|
catch (const tf3::TransformException& exception)
|
|
{
|
|
robot::log_error(
|
|
"[dock_planner] Cannot resolve wheel base from TF %s -> %s: %s.\n",
|
|
loaded_config.front_axle_frame.c_str(),
|
|
loaded_config.rear_axle_frame.c_str(), exception.what());
|
|
return false;
|
|
}
|
|
|
|
if (!std::isfinite(loaded_config.wheel_base) || loaded_config.wheel_base <= 0.0)
|
|
{
|
|
robot::log_error(
|
|
"[dock_planner] Wheel base resolved to %.6f m from TF %s -> %s.\n",
|
|
loaded_config.wheel_base, loaded_config.front_axle_frame.c_str(),
|
|
loaded_config.rear_axle_frame.c_str());
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// A differential base publishes no steered-axle frame. Looking one up
|
|
// would throw and take the whole plugin down, so the turning radius
|
|
// comes from configuration instead and wheel_base stays meaningless.
|
|
loaded_config.wheel_base = 0.0;
|
|
}
|
|
|
|
const double minimum_turning_radius = loaded_config.minimumTurningRadius();
|
|
// 0 is valid for a differential base: it means the robot may turn on the
|
|
// spot. Only a negative or non-finite radius is a configuration error.
|
|
if (!std::isfinite(minimum_turning_radius) || minimum_turning_radius < 0.0)
|
|
{
|
|
robot::log_error(
|
|
"[dock_planner] Cannot derive minimum turning radius (model=%s, "
|
|
"wheel_base=%.6f m, max_steering_angle=%.6f rad, "
|
|
"minimum_turning_radius=%.6f m).\n",
|
|
loaded_config.requiresAxleTf() ? "ackermann" : "differential",
|
|
loaded_config.wheel_base, loaded_config.max_steering_angle,
|
|
loaded_config.minimum_turning_radius);
|
|
return false;
|
|
}
|
|
|
|
std::vector<robot_geometry_msgs::Point> loaded_footprint;
|
|
if (loaded_config.dock_anchor == DockAnchor::kFace ||
|
|
loaded_config.dock_anchor == DockAnchor::kMatingEdge)
|
|
{
|
|
// Goal sits on the approach face (face) or the face touches the robot at
|
|
// the goal (mating_edge): dock size never enters the approach formula,
|
|
// so a declared footprint_dock is deliberately ignored.
|
|
if (planner_nh.hasParam("footprint_dock"))
|
|
{
|
|
robot::log_info("[dock_planner] '%s' uses dock_anchor=%s; footprint_dock is ignored.\n",
|
|
name.c_str(),
|
|
loaded_config.dock_anchor == DockAnchor::kFace ? "face"
|
|
: "mating_edge");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
const FootprintLoadStatus footprint_status =
|
|
loadDockFootprint(planner_nh, name, loaded_footprint);
|
|
if (footprint_status == FootprintLoadStatus::kInvalid)
|
|
{
|
|
return false;
|
|
}
|
|
if (footprint_status == FootprintLoadStatus::kMissing)
|
|
{
|
|
robot::log_warning("[dock_planner] '%s/footprint_dock' is not set; dock size will not contribute to approach geometry.\n",
|
|
name.c_str());
|
|
}
|
|
}
|
|
|
|
costmap_robot_ = costmap_robot;
|
|
global_frame_ = costmap_robot->getGlobalFrameID();
|
|
footprint_dock_ = std::move(loaded_footprint);
|
|
dock_calc_.setConfig(loaded_config);
|
|
dock_calc_.setCostmapCheckCallbacks(
|
|
[this](const robot_geometry_msgs::Pose& pose, double radius)
|
|
{
|
|
return rotationClear(pose, radius);
|
|
},
|
|
[this](const robot_geometry_msgs::Pose& pose)
|
|
{
|
|
return footprintClear(pose);
|
|
});
|
|
initialized_ = true;
|
|
|
|
const std::string dock_face_offset_text =
|
|
optionalDistanceText(loaded_config.dock_face_offset);
|
|
const std::string dock_pivot_safety_margin_text =
|
|
optionalDistanceText(loaded_config.dock_pivot_safety_margin);
|
|
const std::string dock_straight_length_text =
|
|
optionalDistanceText(loaded_config.dock_straight_length);
|
|
robot::log_info(
|
|
"[dock_planner] Initialized dock maneuver (docking edge is derived per plan "
|
|
"from the side the robot stands on): model=%s, TF %s -> %s, wheel_base=%.4f m, "
|
|
"max_steering_angle=%.4f rad, minimum_turning_radius=%.4f m, "
|
|
"pivot_fallback_extension=%.3f m, pivot_safety_margin=%.3f m, "
|
|
"dock_face_offset=%s, dock_pivot_safety_margin=%s, dock_straight_length=%s, "
|
|
"pivot_backoff_max_distance=%.3f m, pivot_check_costmap=%s.\n",
|
|
loaded_config.requiresAxleTf() ? "ackermann" : "differential",
|
|
loaded_config.requiresAxleTf() ? loaded_config.front_axle_frame.c_str() : "-",
|
|
loaded_config.requiresAxleTf() ? loaded_config.rear_axle_frame.c_str() : "-",
|
|
loaded_config.wheel_base, loaded_config.max_steering_angle,
|
|
minimum_turning_radius, loaded_config.pivot_fallback_extension,
|
|
loaded_config.pivot_safety_margin,
|
|
dock_face_offset_text.c_str(), dock_pivot_safety_margin_text.c_str(),
|
|
dock_straight_length_text.c_str(),
|
|
loaded_config.pivot_backoff_max_distance,
|
|
loaded_config.pivot_check_costmap ? "true" : "false");
|
|
return true;
|
|
}
|
|
|
|
bool DockPlanner::makePlan(
|
|
const robot_geometry_msgs::PoseStamped& start,
|
|
const robot_geometry_msgs::PoseStamped& goal,
|
|
std::vector<robot_geometry_msgs::PoseStamped>& plan)
|
|
{
|
|
plan.clear();
|
|
if (!initialized_ || costmap_robot_ == nullptr)
|
|
{
|
|
robot::log_error("[dock_planner] makePlan() called before successful initialize().\n");
|
|
return false;
|
|
}
|
|
if (start.header.frame_id.empty() || goal.header.frame_id.empty() ||
|
|
start.header.frame_id != global_frame_ || goal.header.frame_id != global_frame_)
|
|
{
|
|
robot::log_error("[dock_planner] Start and goal must both be in global frame '%s' (got '%s' and '%s').\n",
|
|
global_frame_.c_str(), start.header.frame_id.c_str(),
|
|
goal.header.frame_id.c_str());
|
|
return false;
|
|
}
|
|
|
|
std::vector<robot_geometry_msgs::Pose> path;
|
|
if (!dock_calc_.makePlanMoveToDock(start, goal, costmap_robot_->getRobotFootprint(),
|
|
footprint_dock_, path) || path.empty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const robot::Time plan_time = robot::Time::now();
|
|
plan.reserve(path.size());
|
|
for (const auto& path_pose : path)
|
|
{
|
|
robot_geometry_msgs::PoseStamped stamped_pose;
|
|
stamped_pose.header.stamp = plan_time;
|
|
stamped_pose.header.frame_id = global_frame_;
|
|
stamped_pose.pose = path_pose;
|
|
plan.push_back(stamped_pose);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
robot_nav_core::BaseGlobalPlanner::Ptr DockPlanner::create()
|
|
{
|
|
return std::make_shared<DockPlanner>();
|
|
}
|
|
|
|
} // namespace dock_planner
|
|
|
|
BOOST_DLL_ALIAS(dock_planner::DockPlanner::create, DockPlanner)
|