feat(lane_route): add lane_route_core and lane_route_planner (R0 + R1)
lane_route_core plans on a precomputed coarse graph (Voronoi ridges and direction-zone lanes) and, when enabled, falls back to a weighted A* search over an SE(2) lattice that enforces forward motion, the minimum turn radius, end-only spins, the footprint gap and direction zones. - R0: route_planner.cpp split into graph search, path building and the graph tier; regress7 output identical to the previous build. - R1: lattice tier (lazy packed C-space tiles, direction-aware heuristic field, analytic Dubins and point connectors, end-gap floors counting unknown cells). Off by default (lattice_enabled = false). - lane_route_eval takes an optional tier mode and LANE_ROUTE_EVAL_SET overrides for offline tuning. Local snapshot for history only; not pushed.
This commit is contained in:
286
src/lane_route_planner.cpp
Normal file
286
src/lane_route_planner.cpp
Normal file
@@ -0,0 +1,286 @@
|
||||
#include <lane_route_planner/lane_route_planner.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <exception>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/dll/alias.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
|
||||
#include <robot/robot.h>
|
||||
#include <robot_costmap_2d/footprint.h>
|
||||
#include <robot_costmap_2d/layered_costmap.h>
|
||||
|
||||
#include <data_convert/data_convert.h>
|
||||
|
||||
namespace lane_route_planner
|
||||
{
|
||||
robot_nav_core::BaseGlobalPlanner::Ptr LaneRoutePlanner::create()
|
||||
{
|
||||
return std::make_shared<LaneRoutePlanner>();
|
||||
}
|
||||
|
||||
void setExternalGridOn(robot_nav_core::BaseGlobalPlanner* planner, const robot_costmap_2d::Costmap2D* grid)
|
||||
{
|
||||
auto* self = dynamic_cast<LaneRoutePlanner*>(planner);
|
||||
if (self)
|
||||
self->setExternalGrid(grid);
|
||||
else
|
||||
robot::log_error("[lane_route_planner] ExternalGrid: wrong planner instance");
|
||||
}
|
||||
|
||||
bool LaneRoutePlanner::initialize(std::string name, robot_costmap_2d::Costmap2DROBOT* costmap)
|
||||
{
|
||||
if (initialized_)
|
||||
return true;
|
||||
if (!costmap || !costmap->getLayeredCostmap() || !costmap->getCostmap())
|
||||
{
|
||||
robot::log_error("[lane_route_planner] initialize: missing costmap");
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
robot::NodeHandle root;
|
||||
robot::NodeHandle nh(root, name);
|
||||
const auto load = [&nh](const char* key, auto& value, const auto& fallback)
|
||||
{
|
||||
if (nh.hasParam(key))
|
||||
{
|
||||
if (!nh.getParam(key, value, fallback))
|
||||
throw std::invalid_argument(std::string("invalid parameter: ") + key);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = fallback;
|
||||
}
|
||||
};
|
||||
SessionConfig defaults;
|
||||
auto& p = config_.planner;
|
||||
const auto& d = defaults.planner;
|
||||
load("min_turn_radius_m", p.min_turn_radius_m, d.min_turn_radius_m);
|
||||
load("corner_radii_m", p.corner_radii_m, d.corner_radii_m);
|
||||
load("clearance_min_margin_m", config_.clearance_min_margin_m, defaults.clearance_min_margin_m);
|
||||
load("clearance_preferred_margin_m", config_.clearance_preferred_margin_m, defaults.clearance_preferred_margin_m);
|
||||
load("collision_check_margin_m", config_.collision_check_margin_m, defaults.collision_check_margin_m);
|
||||
load("footprint_min_gap_m", p.footprint_min_gap_m, d.footprint_min_gap_m);
|
||||
load("footprint_gap_shortcut_raise_m", p.footprint_gap_shortcut_raise_m, d.footprint_gap_shortcut_raise_m);
|
||||
load("deviation_ladder_deg", p.deviation_ladder_deg, d.deviation_ladder_deg);
|
||||
load("route_too_long_ratio", p.route_too_long_ratio, d.route_too_long_ratio);
|
||||
load("route_too_long_margin_m", p.route_too_long_margin_m, d.route_too_long_margin_m);
|
||||
load("suspicious_ratio", p.suspicious_ratio, d.suspicious_ratio);
|
||||
load("suspicious_margin_m", p.suspicious_margin_m, d.suspicious_margin_m);
|
||||
load("max_route_attempts", p.max_route_attempts, d.max_route_attempts);
|
||||
load("time_limit_s", p.time_limit_s, d.time_limit_s);
|
||||
load("output_step_m", p.output_step_m, d.output_step_m);
|
||||
load("live_ridge_pad_m", p.live_ridge_pad_m, d.live_ridge_pad_m);
|
||||
int max_cells = static_cast<int>(d.live_ridge_max_cells);
|
||||
load("live_ridge_max_cells", max_cells, max_cells);
|
||||
p.live_ridge_max_cells = max_cells;
|
||||
load("debug_plan", config_.debug_plan, defaults.debug_plan);
|
||||
load("debug_state", config_.debug_state, defaults.debug_state);
|
||||
const std::string error = config_.validate();
|
||||
if (!error.empty())
|
||||
{
|
||||
robot::log_error("[lane_route_planner] invalid configuration: %s", error.c_str());
|
||||
return false;
|
||||
}
|
||||
costmap_ = costmap;
|
||||
session_.setConfig(config_);
|
||||
initialized_ = true;
|
||||
robot::log_info("[lane_route_planner] %s: time_limit=%.2fs min_radius=%.3fm clearance_margin=%.3fm gap=%.3fm debug_plan=%d debug_state=%d",
|
||||
name.c_str(), p.time_limit_s, p.min_turn_radius_m, config_.clearance_min_margin_m,
|
||||
p.footprint_min_gap_m, config_.debug_plan, config_.debug_state);
|
||||
if (costmap_->getLayeredCostmap()->getReferenceGraphSnapshot().state == robot_costmap_2d::ReferenceGraphState::kDisabled)
|
||||
robot::log_error("[lane_route_planner] reference graph is disabled; plans will fail until enabled");
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[lane_route_planner] initialize: %s", ex.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool LaneRoutePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start, const robot_geometry_msgs::PoseStamped& goal,
|
||||
std::vector<robot_geometry_msgs::PoseStamped>& plan)
|
||||
{
|
||||
plan.clear();
|
||||
if (!initialized_)
|
||||
{
|
||||
robot::log_error("[lane_route_planner] not_initialized");
|
||||
return false;
|
||||
}
|
||||
preempt_.store(false, std::memory_order_relaxed);
|
||||
try
|
||||
{
|
||||
auto* layered = costmap_->getLayeredCostmap();
|
||||
auto* master = costmap_->getCostmap();
|
||||
if (!layered || !master)
|
||||
{
|
||||
robot::log_error("[lane_route_planner] missing_costmap");
|
||||
return false;
|
||||
}
|
||||
SessionRequest request;
|
||||
request.initialized = initialized_;
|
||||
request.start = start;
|
||||
request.goal = goal;
|
||||
request.frame = costmap_->getGlobalFrameID();
|
||||
request.cancel = &preempt_;
|
||||
request.graph = layered->getReferenceGraphSnapshot();
|
||||
request.direction = layered->getDirectionFieldSnapshot();
|
||||
if (!have_state_ || graph_state_ != request.graph.state || direction_state_ != request.direction.state ||
|
||||
graph_generation_ != request.graph.generation || direction_generation_ != request.direction.generation)
|
||||
{
|
||||
if (config_.debug_state)
|
||||
robot::log_info("[lane_route_planner] [debug] graph_state=%d generation=%llu direction_state=%d generation=%llu",
|
||||
static_cast<int>(request.graph.state), static_cast<unsigned long long>(request.graph.generation),
|
||||
static_cast<int>(request.direction.state), static_cast<unsigned long long>(request.direction.generation));
|
||||
have_state_ = true;
|
||||
graph_state_ = request.graph.state;
|
||||
graph_generation_ = request.graph.generation;
|
||||
direction_state_ = request.direction.state;
|
||||
direction_generation_ = request.direction.generation;
|
||||
}
|
||||
// Check state and pose guards before copying the large grid. Geometry is filled below.
|
||||
const std::string early = validateState(request);
|
||||
if (!early.empty())
|
||||
{
|
||||
robot::log_warning("[lane_route_planner] plan failed: %s", early.c_str());
|
||||
return false;
|
||||
}
|
||||
auto& bytes = session_.liveCostBuffer();
|
||||
{
|
||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*master->getMutex());
|
||||
if (master->getSizeInCellsX() > static_cast<unsigned>(std::numeric_limits<int32_t>::max()) ||
|
||||
master->getSizeInCellsY() > static_cast<unsigned>(std::numeric_limits<int32_t>::max()))
|
||||
{
|
||||
robot::log_error("[lane_route_planner] invalid_costmap_size");
|
||||
return false;
|
||||
}
|
||||
request.live.width = static_cast<int32_t>(master->getSizeInCellsX());
|
||||
request.live.height = static_cast<int32_t>(master->getSizeInCellsY());
|
||||
request.live.resolution = master->getResolution();
|
||||
request.live.origin_x = master->getOriginX();
|
||||
request.live.origin_y = master->getOriginY();
|
||||
request.footprint = layered->getFootprint();
|
||||
}
|
||||
const std::size_t count = static_cast<std::size_t>(request.live.width) * request.live.height;
|
||||
if (count == 0 || count > static_cast<std::size_t>(std::numeric_limits<int32_t>::max()))
|
||||
{
|
||||
robot::log_error("[lane_route_planner] invalid_costmap_size");
|
||||
return false;
|
||||
}
|
||||
bytes.resize(count);
|
||||
const auto matches = [&](const robot_costmap_2d::Costmap2D* grid)
|
||||
{
|
||||
return grid && grid->getSizeInCellsX() == static_cast<unsigned>(request.live.width) &&
|
||||
grid->getSizeInCellsY() == static_cast<unsigned>(request.live.height) &&
|
||||
grid->getResolution() == request.live.resolution && grid->getOriginX() == request.live.origin_x &&
|
||||
grid->getOriginY() == request.live.origin_y && grid->getCharMap();
|
||||
};
|
||||
if (external_grid_)
|
||||
{
|
||||
if (!matches(external_grid_))
|
||||
{
|
||||
robot::log_error("[lane_route_planner] external_grid_geometry_mismatch");
|
||||
return false;
|
||||
}
|
||||
std::copy_n(external_grid_->getCharMap(), count, bytes.begin());
|
||||
}
|
||||
else
|
||||
{
|
||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*master->getMutex());
|
||||
if (!matches(master))
|
||||
{
|
||||
robot::log_error("[lane_route_planner] costmap_geometry_changed");
|
||||
return false;
|
||||
}
|
||||
std::copy_n(master->getCharMap(), count, bytes.begin());
|
||||
}
|
||||
// The radius setter runs outside the costmap lock; use the same algorithm on the locked footprint copy.
|
||||
if (request.footprint.size() >= 3)
|
||||
{
|
||||
double circumscribed = 0.0;
|
||||
robot_costmap_2d::calculateMinAndMaxDistances(request.footprint, request.inscribed_radius_m, circumscribed);
|
||||
}
|
||||
request.live.cost = bytes.data();
|
||||
if (request.footprint != previous_footprint_)
|
||||
{
|
||||
previous_footprint_ = request.footprint;
|
||||
double x, y;
|
||||
bool rectangular;
|
||||
if (footprintRectangle(request.footprint, &x, &y, &rectangular) && !rectangular)
|
||||
robot::log_warning("[lane_route_planner] footprint is approximated by a centred bounding rectangle");
|
||||
}
|
||||
auto result = session_.plan(request);
|
||||
if (config_.debug_plan)
|
||||
robot::log_info("[lane_route_planner] [debug] ok=%d reason=%s rung=%.3f retries=%d gap_failures=%d soft_ignored=%d live=%s query=%.1fms compile=%.1fms search=%.1fms build=%.1fms verify=%.1fms live_ridge=%.1fms gap=%.3fm raise=%.3fm exempt=%d swing_windows=%d swing_checks=%ld swing_builds=%d swing_selected=%d",
|
||||
result.ok, result.info.reason.c_str(), result.info.rung_used_deg, result.info.retries,
|
||||
result.info.gap_failures, result.info.soft_cost_ignored, result.info.live_ridge.c_str(),
|
||||
result.info.query_ms, result.info.compile_ms, result.info.search_ms, result.info.build_ms, result.info.verify_ms,
|
||||
result.info.live_ridge_ms, result.info.min_gap_m, result.info.gap_shortcut_raise_m, result.info.gap_exemption_used,
|
||||
result.info.swing_windows, result.info.swing_line_checks, result.info.swing_builds, result.info.swing_selected);
|
||||
if (!result.ok || result.path.empty() || !result.check.valid)
|
||||
{
|
||||
const char* reason = result.ok ? "invalid_or_empty_verified_path" : result.info.reason.c_str();
|
||||
robot::log_warning("[lane_route_planner] plan failed: %s", reason);
|
||||
return false;
|
||||
}
|
||||
const auto final_graph = layered->getReferenceGraphSnapshot();
|
||||
const auto final_direction = layered->getDirectionFieldSnapshot();
|
||||
if (preempt_.load(std::memory_order_relaxed) || final_graph.state != request.graph.state ||
|
||||
final_graph.generation != request.graph.generation || final_direction.state != request.direction.state ||
|
||||
final_direction.generation != request.direction.generation)
|
||||
{
|
||||
robot::log_warning("[lane_route_planner] plan failed: cancelled_or_snapshot_changed");
|
||||
return false;
|
||||
}
|
||||
{
|
||||
auto* latest = costmap_->getCostmap();
|
||||
if (!latest)
|
||||
{
|
||||
robot::log_warning("[lane_route_planner] plan failed: missing_costmap");
|
||||
return false;
|
||||
}
|
||||
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> lock(*latest->getMutex());
|
||||
if (!matches(latest) || layered->getFootprint() != request.footprint)
|
||||
{
|
||||
robot::log_warning("[lane_route_planner] plan failed: costmap_or_footprint_changed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const auto stamp = robot::Time::now();
|
||||
plan.reserve(result.path.size());
|
||||
for (const auto& point : result.path)
|
||||
{
|
||||
robot_geometry_msgs::PoseStamped pose;
|
||||
pose.header.frame_id = request.frame;
|
||||
pose.header.stamp = stamp;
|
||||
pose.pose.position.x = point.x;
|
||||
pose.pose.position.y = point.y;
|
||||
pose.pose.position.z = start.pose.position.z;
|
||||
pose.pose.orientation = data_convert::getQuaternion(point.yaw);
|
||||
plan.push_back(pose);
|
||||
}
|
||||
plan.back().pose = goal.pose;
|
||||
if (preempt_.load(std::memory_order_relaxed))
|
||||
{
|
||||
plan.clear();
|
||||
robot::log_warning("[lane_route_planner] plan failed: cancelled");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
plan.clear();
|
||||
robot::log_error("[lane_route_planner] plan exception: %s", ex.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} // namespace lane_route_planner
|
||||
|
||||
BOOST_DLL_ALIAS(lane_route_planner::LaneRoutePlanner::create, LaneRoutePlanner)
|
||||
BOOST_DLL_ALIAS(lane_route_planner::setExternalGridOn, LaneRoutePlannerExternalGrid)
|
||||
323
src/lane_route_session.cpp
Normal file
323
src/lane_route_session.cpp
Normal file
@@ -0,0 +1,323 @@
|
||||
#include <lane_route_planner/lane_route_session.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#include <lane_route_core/costmap_backend.h>
|
||||
#include <lane_route_core/grid_checks.h>
|
||||
|
||||
namespace lane_route_planner
|
||||
{
|
||||
namespace
|
||||
{
|
||||
bool finitePose(const robot_geometry_msgs::PoseStamped& p)
|
||||
{
|
||||
const auto& q = p.pose.orientation;
|
||||
const auto& x = p.pose.position;
|
||||
return std::isfinite(x.x) && std::isfinite(x.y) && std::isfinite(x.z) && std::isfinite(q.x) &&
|
||||
std::isfinite(q.y) && std::isfinite(q.z) && std::isfinite(q.w);
|
||||
}
|
||||
|
||||
bool sameGeometry(const robot_costmap_2d::DirectionGeometry& g, const lane_route_core::GridRef& grid,
|
||||
const std::string& frame)
|
||||
{
|
||||
return g.width == static_cast<uint32_t>(grid.width) && g.height == static_cast<uint32_t>(grid.height) &&
|
||||
std::abs(g.resolution - grid.resolution) < 1e-8 && std::abs(g.origin_x - grid.origin_x) < 1e-8 &&
|
||||
std::abs(g.origin_y - grid.origin_y) < 1e-8 && g.frame_id == frame;
|
||||
}
|
||||
|
||||
lane_route_core::Pose2 poseOf(const robot_geometry_msgs::PoseStamped& p, bool has_yaw)
|
||||
{
|
||||
const auto& q = p.pose.orientation;
|
||||
return { p.pose.position.x, p.pose.position.y,
|
||||
std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z)), has_yaw };
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string SessionConfig::validate() const
|
||||
{
|
||||
const auto nonnegative = [](double x)
|
||||
{
|
||||
return std::isfinite(x) && x >= 0.0;
|
||||
};
|
||||
std::ostringstream error;
|
||||
if (!nonnegative(clearance_min_margin_m))
|
||||
error << "clearance_min_margin_m must be finite and >= 0, got " << clearance_min_margin_m;
|
||||
else if (!nonnegative(clearance_preferred_margin_m))
|
||||
error << "clearance_preferred_margin_m must be finite and >= 0, got " << clearance_preferred_margin_m;
|
||||
else if (clearance_preferred_margin_m < clearance_min_margin_m)
|
||||
error << "clearance_preferred_margin_m=" << clearance_preferred_margin_m
|
||||
<< " must be >= clearance_min_margin_m=" << clearance_min_margin_m;
|
||||
else if (!nonnegative(collision_check_margin_m))
|
||||
error << "collision_check_margin_m must be finite and >= 0, got " << collision_check_margin_m;
|
||||
if (!error.str().empty())
|
||||
return error.str();
|
||||
return planner.validate();
|
||||
}
|
||||
|
||||
bool footprintRectangle(const std::vector<robot_geometry_msgs::Point>& footprint, double* half_x, double* half_y,
|
||||
bool* centered_rectangle)
|
||||
{
|
||||
if (!half_x || !half_y || !centered_rectangle || footprint.size() < 3)
|
||||
return false;
|
||||
*half_x = 0.0;
|
||||
*half_y = 0.0;
|
||||
for (const auto& p : footprint)
|
||||
{
|
||||
if (!std::isfinite(p.x) || !std::isfinite(p.y))
|
||||
return false;
|
||||
*half_x = std::max(*half_x, std::abs(p.x));
|
||||
*half_y = std::max(*half_y, std::abs(p.y));
|
||||
}
|
||||
if (*half_x <= 0 || *half_y <= 0)
|
||||
return false;
|
||||
unsigned corners = 0;
|
||||
for (const auto& p : footprint)
|
||||
{
|
||||
if (std::abs(std::abs(p.x) - *half_x) > 1e-9 || std::abs(std::abs(p.y) - *half_y) > 1e-9)
|
||||
continue;
|
||||
corners |= 1u << ((p.x > 0 ? 1 : 0) + (p.y > 0 ? 2 : 0));
|
||||
}
|
||||
*centered_rectangle = footprint.size() == 4 && corners == 15;
|
||||
if (*centered_rectangle)
|
||||
{
|
||||
for (std::size_t i = 0; i < 4; ++i)
|
||||
{
|
||||
const auto& a = footprint[i];
|
||||
const auto& b = footprint[(i + 1) % 4];
|
||||
if ((std::abs(a.x - b.x) < 1e-9) == (std::abs(a.y - b.y) < 1e-9))
|
||||
*centered_rectangle = false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string validateState(const SessionRequest& r)
|
||||
{
|
||||
if (!r.initialized)
|
||||
return "not_initialized";
|
||||
if (!finitePose(r.start))
|
||||
return "invalid_start_pose";
|
||||
if (!finitePose(r.goal))
|
||||
return "invalid_goal_pose";
|
||||
if (r.start.header.frame_id != r.frame || r.goal.header.frame_id != r.frame)
|
||||
return "frame_mismatch";
|
||||
if (r.graph.state == robot_costmap_2d::ReferenceGraphState::kDisabled)
|
||||
return "graph_disabled";
|
||||
if (r.graph.state == robot_costmap_2d::ReferenceGraphState::kPending || !r.graph.graph)
|
||||
return "graph_pending";
|
||||
if (r.direction.state == robot_costmap_2d::DirectionFieldState::kPending ||
|
||||
(r.direction.state == robot_costmap_2d::DirectionFieldState::kReady && !r.direction.field))
|
||||
return "direction_pending";
|
||||
if (r.graph.graph->direction_generation != r.direction.generation)
|
||||
return "direction_generation_mismatch";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string validateGrid(const SessionRequest& r)
|
||||
{
|
||||
if (r.live.width <= 0 || r.live.height <= 0 || !std::isfinite(r.live.resolution) || r.live.resolution <= 0 ||
|
||||
!std::isfinite(r.live.origin_x) || !std::isfinite(r.live.origin_y) || !r.live.cost ||
|
||||
!sameGeometry(r.graph.graph->geometry, r.live, r.frame) ||
|
||||
(r.direction.field && !sameGeometry(r.direction.field->geometry(), r.live, r.frame)))
|
||||
return "graph_geometry_mismatch";
|
||||
const std::size_t cells = static_cast<std::size_t>(r.live.width) * static_cast<std::size_t>(r.live.height);
|
||||
if (cells > static_cast<std::size_t>(std::numeric_limits<int32_t>::max()))
|
||||
return "graph_geometry_mismatch";
|
||||
if (r.graph.graph->static_cost.size() != cells || r.graph.graph->clearance_mm.size() != cells)
|
||||
return "graph_data_invalid";
|
||||
double x, y;
|
||||
bool rect;
|
||||
if (!footprintRectangle(r.footprint, &x, &y, &rect))
|
||||
return "invalid_footprint";
|
||||
if (!std::isfinite(r.inscribed_radius_m) || r.inscribed_radius_m < 0)
|
||||
return "invalid_inscribed_radius";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string validateRequest(const SessionRequest& r)
|
||||
{
|
||||
const std::string state_error = validateState(r);
|
||||
if (!state_error.empty())
|
||||
return state_error;
|
||||
return validateGrid(r);
|
||||
}
|
||||
|
||||
void LaneRouteSession::clearCache()
|
||||
{
|
||||
graphs_.clear();
|
||||
coarse_ = {};
|
||||
preferred_mask_.clear();
|
||||
preferred_mask_empty_ = true;
|
||||
directions_.clear();
|
||||
graph_identity_ = nullptr;
|
||||
direction_identity_ = nullptr;
|
||||
half_x_ = -1;
|
||||
half_y_ = -1;
|
||||
inscribed_ = -1;
|
||||
}
|
||||
|
||||
lane_route_core::PlanResult LaneRouteSession::plan(const SessionRequest& request)
|
||||
{
|
||||
using namespace lane_route_core;
|
||||
PlanResult failed;
|
||||
failed.info.reason = validateRequest(request);
|
||||
if (!failed.info.reason.empty())
|
||||
return failed;
|
||||
const std::string error = config_.validate();
|
||||
if (!error.empty())
|
||||
{
|
||||
failed.info.reason = "invalid_config: " + error;
|
||||
return failed;
|
||||
}
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
|
||||
{
|
||||
failed.info.reason = "cancelled";
|
||||
return failed;
|
||||
}
|
||||
const auto& reference = *request.graph.graph;
|
||||
const std::size_t cells = static_cast<std::size_t>(request.live.width) * request.live.height;
|
||||
double half_x, half_y;
|
||||
bool rectangle;
|
||||
footprintRectangle(request.footprint, &half_x, &half_y, &rectangle);
|
||||
if (graph_identity_ != request.graph.graph.get() || graph_generation_ != request.graph.generation ||
|
||||
half_x_ != half_x || half_y_ != half_y || inscribed_ != request.inscribed_radius_m)
|
||||
{
|
||||
graphs_.clear();
|
||||
preferred_mask_.clear();
|
||||
coarse_ = toCoarseGraph(reference, request.cancel);
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
|
||||
{
|
||||
clearCache();
|
||||
failed.info.reason = "cancelled";
|
||||
return failed;
|
||||
}
|
||||
graph_identity_ = request.graph.graph.get();
|
||||
graph_generation_ = request.graph.generation;
|
||||
half_x_ = half_x;
|
||||
half_y_ = half_y;
|
||||
inscribed_ = request.inscribed_radius_m;
|
||||
}
|
||||
const auto* field = request.direction.field.get();
|
||||
if (direction_identity_ != field || direction_generation_ != request.direction.generation)
|
||||
{
|
||||
graphs_.clear();
|
||||
directions_.clear();
|
||||
direction_identity_ = field;
|
||||
direction_generation_ = request.direction.generation;
|
||||
if (field && field->hasConstraints())
|
||||
{
|
||||
directions_.resize(cells);
|
||||
for (int32_t y = 0; y < request.live.height; ++y)
|
||||
for (int32_t x = 0; x < request.live.width; ++x)
|
||||
directions_[static_cast<std::size_t>(y) * request.live.width + x] = field->code(x, y);
|
||||
}
|
||||
}
|
||||
const bool live_has_preferred = std::memchr(request.live.cost, kPreferredSpace, cells) != nullptr;
|
||||
bool preferred_changed = preferred_mask_.size() != cells;
|
||||
if (!preferred_changed && (live_has_preferred || !preferred_mask_empty_))
|
||||
{
|
||||
for (std::size_t i = 0; i < cells; ++i)
|
||||
{
|
||||
if (preferred_mask_[i] != (request.live.cost[i] == kPreferredSpace))
|
||||
{
|
||||
preferred_changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (preferred_changed)
|
||||
{
|
||||
graphs_.clear();
|
||||
preferred_mask_.resize(cells);
|
||||
for (std::size_t i = 0; i < cells; ++i)
|
||||
preferred_mask_[i] = request.live.cost[i] == kPreferredSpace;
|
||||
preferred_mask_empty_ = !live_has_preferred;
|
||||
}
|
||||
PlannerConfig config = config_.planner;
|
||||
config.half_x_m = half_x;
|
||||
config.half_y_m = half_y;
|
||||
config.clearance_min_m = request.inscribed_radius_m + config_.clearance_min_margin_m;
|
||||
config.clearance_hard_m = config.clearance_min_m;
|
||||
config.clearance_preferred_m = request.inscribed_radius_m + config_.clearance_preferred_margin_m;
|
||||
config.collision_check_distance_m = std::hypot(half_x, half_y) + config_.collision_check_margin_m;
|
||||
if (field)
|
||||
config.max_deviation_deg = field->maxDeviationRad() * 180.0 / kPi;
|
||||
GridRef compile_grid = request.live;
|
||||
compile_grid.cost = reference.static_cost.data();
|
||||
compile_grid.clearance_mm = reference.clearance_mm.data();
|
||||
compile_grid.direction = directions_.empty() ? nullptr : directions_.data();
|
||||
GridRef live = request.live;
|
||||
live.direction = compile_grid.direction;
|
||||
live.clearance_mm = reference.clearance_mm.data();
|
||||
RouteRequest route;
|
||||
route.start = poseOf(request.start, request.start_has_yaw);
|
||||
route.goal = poseOf(request.goal, request.goal_has_yaw);
|
||||
route.cancel = request.cancel;
|
||||
route.changed_boxes = changedBoxes(reference.static_cost.data(), live, request.cancel, &changed_cells_, &changed_stack_);
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
|
||||
{
|
||||
failed.info.reason = "cancelled";
|
||||
return failed;
|
||||
}
|
||||
if (!route.changed_boxes.empty())
|
||||
{
|
||||
const double radius = std::max({ 1.5, config.clearance_preferred_m, config.collision_check_distance_m,
|
||||
config.clearance_hard_m, config.clearance_min_m });
|
||||
patchClearance(reference.static_cost.data(), reference.clearance_mm.data(), live, radius, &live_clearance_, request.cancel);
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
|
||||
{
|
||||
failed.info.reason = "cancelled";
|
||||
return failed;
|
||||
}
|
||||
live.clearance_mm = live_clearance_.data();
|
||||
}
|
||||
const RidgeBackend backend = makeRidgeBackend(config, reference.params);
|
||||
const GraphProvider provider = [&](double deviation) -> std::shared_ptr<const CompiledGraph>
|
||||
{
|
||||
auto it = graphs_.find(deviation);
|
||||
if (it != graphs_.end())
|
||||
return it->second;
|
||||
auto graph = std::make_shared<const CompiledGraph>(
|
||||
compileGraph(coarse_, compile_grid, live.cost, deviation, config, backend, request.cancel));
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
|
||||
return {};
|
||||
graphs_.emplace(deviation, graph);
|
||||
++compile_count_;
|
||||
return graph;
|
||||
};
|
||||
PlanResult result = planRoute(provider, route, live, config, backend);
|
||||
if (result.ok && !result.path.empty())
|
||||
{
|
||||
// The adapter outputs the exact goal pose. Verify that final adjustment as well, while
|
||||
// keeping the original core path available to callers comparing against the eval tool.
|
||||
const auto verify_begin = std::chrono::steady_clock::now();
|
||||
Path exact = result.path;
|
||||
exact.back().x = route.goal.x;
|
||||
exact.back().y = route.goal.y;
|
||||
if (route.goal.has_yaw)
|
||||
exact.back().yaw = route.goal.yaw;
|
||||
const PathCheck check = verifyPath(exact, live, result.info.rung_used_deg, config, request.cancel);
|
||||
const bool valid = check.valid && endpointOk(exact, route.start, route.goal) &&
|
||||
freeClearanceOk(exact, live, clearanceThresholdMm(config.clearance_min_m)) &&
|
||||
!blockedEndpointTurn(exact, live, config.half_x_m, config.half_y_m) &&
|
||||
checkFootprintGap(exact, live, config.footprint_min_gap_m, config.half_x_m,
|
||||
config.half_y_m, request.cancel).valid;
|
||||
const double verify_ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - verify_begin).count();
|
||||
result.info.verify_ms += verify_ms;
|
||||
result.info.query_ms += verify_ms;
|
||||
if (!valid || (request.cancel && request.cancel->load(std::memory_order_relaxed)))
|
||||
{
|
||||
result.ok = false;
|
||||
result.path.clear();
|
||||
result.info.reason = request.cancel && request.cancel->load(std::memory_order_relaxed) ? "cancelled" : "exact_goal_invalid";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace lane_route_planner
|
||||
Reference in New Issue
Block a user