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)
|
||||
Reference in New Issue
Block a user