267 lines
9.1 KiB
C++
267 lines
9.1 KiB
C++
#include "dock_planner/dock_planner.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <utility>
|
|
|
|
#include <boost/dll/alias.hpp>
|
|
#include <robot/robot.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;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
DockPlanner::DockPlanner() = default;
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
const double minimum_turning_radius = loaded_config.minimumTurningRadius();
|
|
if (!std::isfinite(minimum_turning_radius) || minimum_turning_radius <= 0.0)
|
|
{
|
|
robot::log_error(
|
|
"[dock_planner] Cannot derive minimum turning radius from wheel_base=%.6f m "
|
|
"and max_steering_angle=%.6f rad.\n",
|
|
loaded_config.wheel_base, loaded_config.max_steering_angle);
|
|
return false;
|
|
}
|
|
|
|
std::vector<robot_geometry_msgs::Point> loaded_footprint;
|
|
if (loaded_config.dock_anchor == DockAnchor::kFace)
|
|
{
|
|
// Goal sits on the approach face: 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=face; footprint_dock is ignored.\n",
|
|
name.c_str());
|
|
}
|
|
}
|
|
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);
|
|
initialized_ = true;
|
|
|
|
robot::log_info(
|
|
"[dock_planner] Initialized single-cusp dock maneuver (docking edge is "
|
|
"derived per plan from the side the robot stands on): TF %s -> %s, "
|
|
"wheel_base=%.4f m, max_steering_angle=%.4f rad, minimum_turning_radius=%.4f m.\n",
|
|
loaded_config.front_axle_frame.c_str(), loaded_config.rear_axle_frame.c_str(),
|
|
loaded_config.wheel_base, loaded_config.max_steering_angle,
|
|
minimum_turning_radius);
|
|
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)
|