- Implements Preemptable::preempt() (atomic store), checked between the up
to 3 internal ARA* runs a single makePlan() can do (free_start_heading
retry, realign) — the runs now share one time budget instead of each
getting the full allocated_time, and a preempt between runs stops the
next one from starting (kMinReplanBudget floor so a preempt right at the
edge still gets a minimal attempt).
- Probes for the vendored sbpl's abort flag at runtime via
dlsym(RTLD_DEFAULT, "sbpl_abort_flag_abi_version") before touching
set_abort_flag() — logs an error if the header advertises the flag but
the loaded .so doesn't (header/lib version mismatch), a warning if built
against an sbpl without the flag at all (falls back to the old
run-to-allocated_time behaviour, no crash).
- primitive_filename now resolves relative to PNKX_NAV_CORE_CONFIG_DIR
instead of only working from one dev machine's absolute path.
- CMakeLists.txt: target_include_directories(... BEFORE PRIVATE
${SBPL_INCLUDE_DIRS}) so this package's own sbpl header wins over a
same-named one pulled in transitively (apt ros-noetic-sbpl in
/opt/ros/noetic/include has no abort flag and was winning silently);
configure-time check for SBPL_HAS_ABORT_FLAG in the resolved header with
a loud warning if missing.
Depends on Test/sbpl commit bd89924 (the abort flag itself) and
pnkx_nav_core commit 2b0993b (robot_nav_core::Preemptable header).
Measured on the dev machine (2026-09-12, apt ros-noetic-sbpl removed,
rebuilt): "preempted after N ms" with N in the tens of ms instead of
running to allocated_time (was up to 9 s). Full numbers:
Test/move_base2/docs/PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md section 6.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
812 lines
36 KiB
C++
812 lines
36 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* Copyright (c) 2008, Willow Garage, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer in the documentation and/or other materials provided
|
|
* with the distribution.
|
|
* * Neither the name of the Willow Garage nor the names of its
|
|
* contributors may be used to endorse or promote products derived
|
|
* from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
* Author: Mike Phillips
|
|
*********************************************************************/
|
|
|
|
#include <sbpl_lattice_planner/sbpl_lattice_planner.h>
|
|
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <dlfcn.h>
|
|
#include <unistd.h>
|
|
|
|
#include <robot_nav_msgs/Path.h>
|
|
|
|
#include <robot_costmap_2d/inflation_layer.h>
|
|
#include <tf3/LinearMath/Quaternion.h>
|
|
#include <boost/dll/alias.hpp>
|
|
|
|
using namespace std;
|
|
|
|
namespace robot_geometry_msgs {
|
|
bool operator== (const Point &p1, const Point &p2)
|
|
{
|
|
return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z;
|
|
}
|
|
}
|
|
|
|
namespace sbpl_lattice_planner{
|
|
class LatticeSCQ : public StateChangeQuery{
|
|
public:
|
|
LatticeSCQ(EnvironmentNAVXYTHETALAT* env, std::vector<nav2dcell_t> const & changedcellsV)
|
|
: env_(env), changedcellsV_(changedcellsV) {
|
|
}
|
|
|
|
// lazy init, because we do not always end up calling this method
|
|
virtual std::vector<int> const * getPredecessors() const{
|
|
if(predsOfChangedCells_.empty() && !changedcellsV_.empty())
|
|
env_->GetPredsofChangedEdges(&changedcellsV_, &predsOfChangedCells_);
|
|
return &predsOfChangedCells_;
|
|
}
|
|
|
|
// lazy init, because we do not always end up calling this method
|
|
virtual std::vector<int> const * getSuccessors() const{
|
|
if(succsOfChangedCells_.empty() && !changedcellsV_.empty())
|
|
env_->GetSuccsofChangedEdges(&changedcellsV_, &succsOfChangedCells_);
|
|
return &succsOfChangedCells_;
|
|
}
|
|
|
|
EnvironmentNAVXYTHETALAT * env_;
|
|
std::vector<nav2dcell_t> const & changedcellsV_;
|
|
mutable std::vector<int> predsOfChangedCells_;
|
|
mutable std::vector<int> succsOfChangedCells_;
|
|
};
|
|
|
|
SBPLLatticePlanner::SBPLLatticePlanner()
|
|
: initialized_(false), costmap_robot_(NULL), external_grid_(NULL){
|
|
}
|
|
|
|
SBPLLatticePlanner::SBPLLatticePlanner(std::string name, robot_costmap_2d::Costmap2DROBOT* costmap_robot)
|
|
: initialized_(false), costmap_robot_(NULL), external_grid_(NULL)
|
|
{
|
|
initialize(name, costmap_robot);
|
|
}
|
|
|
|
void SBPLLatticePlanner::setExternalGrid(const robot_costmap_2d::Costmap2D* grid){
|
|
external_grid_ = grid;
|
|
}
|
|
|
|
const robot_costmap_2d::Costmap2D* SBPLLatticePlanner::activeGrid() const{
|
|
return external_grid_ != NULL ? external_grid_ : costmap_robot_->getCostmap();
|
|
}
|
|
|
|
bool SBPLLatticePlanner::initialize(std::string name, robot_costmap_2d::Costmap2DROBOT* costmap_robot){
|
|
if(!initialized_){
|
|
robot::NodeHandle nh;
|
|
robot::NodeHandle private_nh = robot::NodeHandle(nh, name);
|
|
|
|
robot::log_info("Name is %s", name.c_str());
|
|
|
|
private_nh.param("planner_type", planner_type_, string("ARAPlanner"));
|
|
private_nh.param("allocated_time", allocated_time_, 10.0);
|
|
private_nh.param("initial_epsilon",initial_epsilon_,3.0);
|
|
private_nh.param("environment_type", environment_type_, string("XYThetaLattice"));
|
|
private_nh.param("forward_search", forward_search_, bool(false));
|
|
private_nh.param("primitive_filename",primitive_filename_,string(""));
|
|
private_nh.param("force_scratch_limit",force_scratch_limit_,500);
|
|
|
|
// Đường dẫn TƯƠNG ĐỐI của file mprim được giải theo cây config đang chạy (2026-09-11), cùng thứ
|
|
// tự NodeHandle dùng: PNKX_NAV_CORE_CONFIG_DIR rồi PNKX_NAV_CORE_DIR/config. Trước đây YAML phải
|
|
// ghi đường dẫn tuyệt đối — máy dev chép sang robot là gãy (`/home/duongtd/rl_ws/...`).
|
|
if (!primitive_filename_.empty() && primitive_filename_[0] != '/' &&
|
|
access(primitive_filename_.c_str(), R_OK) != 0)
|
|
{
|
|
std::vector<std::string> candidates;
|
|
if (const char* cfg = std::getenv("PNKX_NAV_CORE_CONFIG_DIR"))
|
|
candidates.push_back(std::string(cfg) + "/" + primitive_filename_);
|
|
if (const char* root = std::getenv("PNKX_NAV_CORE_DIR"))
|
|
candidates.push_back(std::string(root) + "/config/" + primitive_filename_);
|
|
bool resolved = false;
|
|
for (const std::string& c : candidates)
|
|
{
|
|
if (access(c.c_str(), R_OK) == 0)
|
|
{
|
|
robot::log_info("[sbpl_lattice_planner] primitive_filename '%s' resolved to '%s'",
|
|
primitive_filename_.c_str(), c.c_str());
|
|
primitive_filename_ = c;
|
|
resolved = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!resolved)
|
|
{
|
|
robot::log_error("[sbpl_lattice_planner] primitive_filename '%s' is relative and was not "
|
|
"found under PNKX_NAV_CORE_CONFIG_DIR or PNKX_NAV_CORE_DIR/config",
|
|
primitive_filename_.c_str());
|
|
}
|
|
}
|
|
|
|
double nominalvel_mpersecs, timetoturn45degsinplace_secs;
|
|
private_nh.param("nominalvel_mpersecs", nominalvel_mpersecs, 0.4);
|
|
private_nh.param("timetoturn45degsinplace_secs", timetoturn45degsinplace_secs, 0.6);
|
|
robot::log_error("nominalvel_mpersecs: %f, timetoturn45degsinplace_secs: %f, primitive_filename_: %s",nominalvel_mpersecs,timetoturn45degsinplace_secs,primitive_filename_.c_str());
|
|
|
|
int lethal_obstacle;
|
|
private_nh.param("lethal_obstacle",lethal_obstacle,20);
|
|
lethal_obstacle_ = (unsigned char) lethal_obstacle;
|
|
inscribed_inflated_obstacle_ = lethal_obstacle_-1;
|
|
sbpl_cost_multiplier_ = (unsigned char) (robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE/inscribed_inflated_obstacle_ + 1);
|
|
robot::log_debug("SBPL: lethal: %uz, inscribed inflated: %uz, multiplier: %uz",lethal_obstacle,inscribed_inflated_obstacle_,sbpl_cost_multiplier_);
|
|
|
|
private_nh.param("publish_footprint_path", publish_footprint_path_, bool(true));
|
|
private_nh.param<int>("visualizer_skip_poses", visualizer_skip_poses_, 5);
|
|
|
|
private_nh.param("allow_unknown", allow_unknown_, bool(true));
|
|
// chỉ bật khi local planner có bước quay tại chỗ đầu path (turn_around_priority)
|
|
private_nh.param("free_start_heading", free_start_heading_, bool(false));
|
|
|
|
name_ = name;
|
|
costmap_robot_ = costmap_robot;
|
|
|
|
robot::Time start_time = robot::Time::now();
|
|
robot::Rate rate(1.0);
|
|
while(costmap_robot_->getCostmap()->getSizeInCellsX() == 0 || costmap_robot_->getCostmap()->getSizeInCellsY() == 0){
|
|
robot::log_warning("Waiting for costmap to be initialized...");
|
|
rate.sleep();
|
|
if((robot::Time::now() - start_time).toSec() > 2.0){
|
|
robot::log_error("Costmap not initialized after 10 seconds, exiting...");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
footprint_ = costmap_robot_->getRobotFootprint();
|
|
footprint_prev_ = footprint_;
|
|
|
|
if ("XYThetaLattice" == environment_type_){
|
|
robot::log_debug("Using a 3D costmap for theta lattice\n");
|
|
env_ = new EnvironmentNAVXYTHETALAT();
|
|
}
|
|
else{
|
|
robot::log_error("XYThetaLattice is currently the only supported environment!\n");
|
|
exit(1);
|
|
}
|
|
|
|
circumscribed_cost_ = computeCircumscribedCost();
|
|
|
|
if (circumscribed_cost_ == 0) {
|
|
// Unfortunately, the inflation_radius is not taken into account by
|
|
// inflation_layer->computeCost(). If inflation_radius is smaller than
|
|
// the circumscribed radius, SBPL will ignore some obstacles, but we
|
|
// cannot detect this problem. If the cost_scaling_factor is too large,
|
|
// SBPL won't run into obstacles, but will always perform an expensive
|
|
// footprint check, no matter how far the nearest obstacle is.
|
|
robot::log_warning("The costmap value at the robot's circumscribed radius (%f m) is 0.", costmap_robot_->getLayeredCostmap()->getCircumscribedRadius());
|
|
robot::log_warning("SBPL performance will suffer.");
|
|
robot::log_warning("Please decrease the costmap's cost_scaling_factor.");
|
|
}
|
|
if(!env_->SetEnvParameter("cost_inscribed_thresh",costMapCostToSBPLCost(robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE))){
|
|
robot::log_error("Failed to set cost_inscribed_thresh parameter");
|
|
exit(1);
|
|
}
|
|
if(!env_->SetEnvParameter("cost_possibly_circumscribed_thresh", circumscribed_cost_)){
|
|
robot::log_error("Failed to set cost_possibly_circumscribed_thresh parameter");
|
|
exit(1);
|
|
}
|
|
int obst_cost_thresh = costMapCostToSBPLCost(robot_costmap_2d::LETHAL_OBSTACLE);
|
|
vector<sbpl_2Dpt_t> perimeterptsV;
|
|
perimeterptsV.reserve(footprint_.size());
|
|
for (size_t ii(0); ii < footprint_.size(); ++ii) {
|
|
sbpl_2Dpt_t pt;
|
|
pt.x = footprint_[ii].x;
|
|
pt.y = footprint_[ii].y;
|
|
perimeterptsV.push_back(pt);
|
|
}
|
|
|
|
robot::log_info("footprint size = %zu", perimeterptsV.size());
|
|
robot::log_info("width=%d",
|
|
costmap_robot_->getCostmap()->getSizeInCellsX());
|
|
|
|
robot::log_info("height=%d",
|
|
costmap_robot_->getCostmap()->getSizeInCellsY());
|
|
|
|
robot::log_info("resolution=%f",
|
|
costmap_robot_->getCostmap()->getResolution());
|
|
|
|
bool ret;
|
|
try{
|
|
// robot::log_warning("DEBUG 2000");
|
|
ret = env_->InitializeEnv(costmap_robot_->getCostmap()->getSizeInCellsX(), // width
|
|
costmap_robot_->getCostmap()->getSizeInCellsY(), // height
|
|
0, // mapdata
|
|
0, 0, 0, // start (x, y, theta, t)
|
|
0, 0, 0, // goal (x, y, theta)
|
|
0, 0, 0, //goal tolerance
|
|
perimeterptsV, costmap_robot_->getCostmap()->getResolution(), nominalvel_mpersecs,
|
|
timetoturn45degsinplace_secs, obst_cost_thresh,
|
|
primitive_filename_.c_str());
|
|
ret = true;
|
|
current_env_width_ = costmap_robot_->getCostmap()->getSizeInCellsX();
|
|
current_env_height_ = costmap_robot_->getCostmap()->getSizeInCellsY();
|
|
// robot::log_warning("DEBUG 3000");
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
robot::log_error("SBPL encountered a fatal exception: %s", e->what());
|
|
ret = false;
|
|
}
|
|
if(!ret){
|
|
robot::log_error("SBPL initialization failed!");
|
|
exit(1);
|
|
}
|
|
for (ssize_t ix(0); ix < costmap_robot_->getCostmap()->getSizeInCellsX(); ++ix)
|
|
for (ssize_t iy(0); iy < costmap_robot_->getCostmap()->getSizeInCellsY(); ++iy)
|
|
env_->UpdateCost(ix, iy, costMapCostToSBPLCost(costmap_robot_->getCostmap()->getCost(ix,iy)));
|
|
|
|
if ("ARAPlanner" == planner_type_){
|
|
robot::log_info("Planning with ARA*");
|
|
planner_ = new ARAPlanner(env_, forward_search_);
|
|
}
|
|
else if ("ADPlanner" == planner_type_){
|
|
robot::log_info("Planning with AD*");
|
|
planner_ = new ADPlanner(env_, forward_search_);
|
|
}
|
|
else{
|
|
robot::log_error("ARAPlanner and ADPlanner are currently the only supported planners!\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Preempt (2026-09-11) cần libsbpl có abort flag. Header nói có (SBPL_HAS_ABORT_FLAG) chưa đủ:
|
|
// .so nạp lúc chạy có thể là bản cũ (ros-noetic-sbpl 2020 ở /opt/ros) — layout SBPLPlanner khác,
|
|
// chạm vào set_abort_flag() là ghi đè member đầu của ARAPlanner. Probe symbol trước, không có thì
|
|
// TUYỆT ĐỐI không đụng cờ và nói to: cancel sẽ không cắt được lượt đang chạy.
|
|
sbpl_abort_supported_ = false;
|
|
#ifdef SBPL_HAS_ABORT_FLAG
|
|
{
|
|
using ProbeFn = int (*)();
|
|
void* sym = dlsym(RTLD_DEFAULT, "sbpl_abort_flag_abi_version");
|
|
if (sym != nullptr && reinterpret_cast<ProbeFn>(sym)() == SBPL_ABORT_FLAG_ABI_VERSION)
|
|
{
|
|
sbpl_abort_supported_ = true;
|
|
planner_->set_abort_flag(&preempt_);
|
|
robot::log_info("[sbpl_lattice_planner] libsbpl abort flag: supported — cancel/pause can "
|
|
"interrupt a running search");
|
|
}
|
|
else
|
|
{
|
|
robot::log_error("[sbpl_lattice_planner] compiled against an sbpl header with abort flag "
|
|
"but the loaded libsbpl.so has no sbpl_abort_flag_abi_version (old library "
|
|
"on the load path?) — preempt DISABLED, a cancelled search runs to "
|
|
"allocated_time. Fix the install so one libsbpl is used.");
|
|
}
|
|
}
|
|
#else
|
|
robot::log_warning("[sbpl_lattice_planner] built against an sbpl without abort flag — "
|
|
"cancel/pause cannot interrupt a running search (runs to allocated_time)");
|
|
#endif
|
|
|
|
robot::log_info("[sbpl_lattice_planner] Initialized successfully");
|
|
// plan_pub_ = private_nh.advertise<nav_msgs::Path>("plan", 1);
|
|
// stats_publisher_ = private_nh.advertise<sbpl_lattice_planner::SBPLLatticePlannerStats>("sbpl_lattice_planner_stats", 1);
|
|
// sbpl_plan_footprint_pub_ = private_nh.advertise<robot_visualization_msgs::Marker>("footprint_markers", 1);
|
|
|
|
initialized_ = true;
|
|
}
|
|
return initialized_;
|
|
}
|
|
|
|
//Taken from Sachin's sbpl_cart_planner
|
|
//This rescales the costmap according to a robotparam which sets the obstacle cost
|
|
unsigned char SBPLLatticePlanner::costMapCostToSBPLCost(unsigned char newcost){
|
|
if(newcost == robot_costmap_2d::LETHAL_OBSTACLE || (!allow_unknown_ && newcost == robot_costmap_2d::NO_INFORMATION))
|
|
return lethal_obstacle_;
|
|
else if(newcost == robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
|
|
return inscribed_inflated_obstacle_;
|
|
else if(newcost == 0 || newcost == robot_costmap_2d::NO_INFORMATION)
|
|
return 0;
|
|
else {
|
|
unsigned char sbpl_cost = newcost / sbpl_cost_multiplier_;
|
|
if (sbpl_cost == 0)
|
|
sbpl_cost = 1;
|
|
return sbpl_cost;
|
|
}
|
|
}
|
|
|
|
unsigned char SBPLLatticePlanner::computeCircumscribedCost() {
|
|
unsigned char result = 0;
|
|
|
|
if (!costmap_robot_) {
|
|
robot::log_error("Costmap is not initialized");
|
|
return 0;
|
|
}
|
|
|
|
// // check if the costmap has an inflation layer
|
|
for (const auto &layer : *costmap_robot_->getLayeredCostmap()->getPlugins())
|
|
{
|
|
// Kiểm tra layer có đúng type
|
|
if (layer->getType() == robot_costmap_2d::LayerType::INFLATION_LAYER)
|
|
{
|
|
result = costMapCostToSBPLCost(layer->computeCost(
|
|
costmap_robot_->getLayeredCostmap()->getCircumscribedRadius() /
|
|
costmap_robot_->getCostmap()->getResolution()));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
|
|
const robot_geometry_msgs::PoseStamped& goal,
|
|
std::vector<robot_geometry_msgs::PoseStamped>& plan){
|
|
if(!initialized_){
|
|
robot::log_error("Global planner is not initialized");
|
|
return false;
|
|
}
|
|
|
|
// Mỗi lượt bắt đầu với cờ preempt SẠCH: một preempt nhắm vào lượt trước không được giết lượt này
|
|
// (contract robot_nav_core::Preemptable). Cờ đã gắn vào planner_ ở initialize() nếu libsbpl hỗ trợ.
|
|
preempt_.store(false, std::memory_order_relaxed);
|
|
|
|
// Lưới cell đang có hiệu lực: bản chụp ngoài (detour trên lưới gộp) hoặc lưới sống. Footprint,
|
|
// inflation và frame vẫn thuộc costmap sống — lưới ngoài bắt buộc cùng kích thước với nó.
|
|
const robot_costmap_2d::Costmap2D* grid = activeGrid();
|
|
|
|
bool do_init = false;
|
|
if (current_env_width_ != grid->getSizeInCellsX() ||
|
|
current_env_height_ != grid->getSizeInCellsY()) {
|
|
robot::log_info("Costmap dimensions have changed from (%d x %d) to (%d x %d), reinitializing sbpl_lattice_planner.",
|
|
current_env_width_, current_env_height_,
|
|
grid->getSizeInCellsX(), grid->getSizeInCellsY());
|
|
do_init = true;
|
|
}
|
|
else if (footprint_ != costmap_robot_->getRobotFootprint()) {
|
|
robot::log_info("Robot footprint has changed, reinitializing sbpl_lattice_planner.");
|
|
do_init = true;
|
|
}
|
|
else if (circumscribed_cost_ != computeCircumscribedCost()) {
|
|
robot::log_info("Cost at circumscribed radius has changed, reinitializing sbpl_lattice_planner.");
|
|
do_init = true;
|
|
}
|
|
|
|
if (do_init) {
|
|
initialized_ = false;
|
|
delete planner_;
|
|
planner_ = NULL;
|
|
delete env_;
|
|
env_ = NULL;
|
|
// initialize() dựng env theo LƯỚI SỐNG — lưới ngoài lệch kích thước với nó sẽ churn re-init
|
|
// mỗi lượt. Contract của setExternalGrid cấm lệch, nhưng phát hiện được thì nói thẳng.
|
|
if (external_grid_ != NULL &&
|
|
(external_grid_->getSizeInCellsX() != grid->getSizeInCellsX() ||
|
|
external_grid_->getSizeInCellsY() != grid->getSizeInCellsY())) {
|
|
robot::log_error("[sbpl_lattice_planner] external grid (%d x %d) does not match the live "
|
|
"costmap (%d x %d); dropping the external grid.",
|
|
external_grid_->getSizeInCellsX(), external_grid_->getSizeInCellsY(),
|
|
grid->getSizeInCellsX(),
|
|
grid->getSizeInCellsY());
|
|
external_grid_ = NULL;
|
|
grid = activeGrid();
|
|
}
|
|
initialize(name_, costmap_robot_);
|
|
}
|
|
|
|
plan.clear();
|
|
|
|
robot::log_info("[sbpl_lattice_planner] getting start point (%g,%g) goal point (%g,%g)",
|
|
start.pose.position.x, start.pose.position.y,goal.pose.position.x, goal.pose.position.y);
|
|
double theta_start = 2 * atan2(start.pose.orientation.z, start.pose.orientation.w);
|
|
double theta_goal = 2 * atan2(goal.pose.orientation.z, goal.pose.orientation.w);
|
|
|
|
// Heading thật của robot — giữ lại để retry nếu heading giả định không ra nghiệm.
|
|
const double theta_start_actual = theta_start;
|
|
bool start_heading_overridden = false;
|
|
if (free_start_heading_) {
|
|
// Local planner tự quay tại chỗ về hướng path trước khi bám (turn_around_priority),
|
|
// nên heading xuất phát không phải ràng buộc thật. Giữ nguyên nó thì khi goal nằm
|
|
// phía sau, SBPL sẽ vẽ cung quay đầu (U-turn) ở đầu path — chuỗi prim quay tại chỗ
|
|
// đắt hơn cung tiến nhiều lần theo costmult nên không bao giờ được chọn. Gán heading
|
|
// giả định hướng thẳng tới goal để path bắt đầu thẳng, phần xoay đầu do local planner lo.
|
|
const double dx = goal.pose.position.x - start.pose.position.x;
|
|
const double dy = goal.pose.position.y - start.pose.position.y;
|
|
// Goal quá gần (< ~2 cell): hướng tới goal hết ý nghĩa, dùng luôn heading của goal.
|
|
theta_start = (std::hypot(dx, dy) > 2.0 * grid->getResolution())
|
|
? atan2(dy, dx)
|
|
: theta_goal;
|
|
start_heading_overridden = true;
|
|
}
|
|
|
|
try{
|
|
int ret = env_->SetStart(start.pose.position.x - grid->getOriginX(), start.pose.position.y - grid->getOriginY(), theta_start);
|
|
if(ret < 0 || planner_->set_start(ret) == 0){
|
|
robot::log_error("ERROR: failed to set start state\n");
|
|
return false;
|
|
}
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
robot::log_error("SBPL encountered a fatal exception while setting the start state");
|
|
return false;
|
|
}
|
|
|
|
try{
|
|
int ret = env_->SetGoal(goal.pose.position.x - grid->getOriginX(), goal.pose.position.y - grid->getOriginY(), theta_goal);
|
|
if(ret < 0 || planner_->set_goal(ret) == 0){
|
|
robot::log_error("ERROR: failed to set goal state\n");
|
|
return false;
|
|
}
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
robot::log_error("SBPL encountered a fatal exception while setting the goal state");
|
|
return false;
|
|
}
|
|
|
|
int offOnCount = 0;
|
|
int onOffCount = 0;
|
|
int allCount = 0;
|
|
vector<nav2dcell_t> changedcellsV;
|
|
|
|
for(unsigned int ix = 0; ix < grid->getSizeInCellsX(); ix++) {
|
|
for(unsigned int iy = 0; iy < grid->getSizeInCellsY(); iy++) {
|
|
|
|
unsigned char oldCost = env_->GetMapCost(ix,iy);
|
|
unsigned char newCost = costMapCostToSBPLCost(grid->getCost(ix,iy));
|
|
|
|
if(oldCost == newCost) continue;
|
|
|
|
allCount++;
|
|
|
|
//first case - off cell goes on
|
|
|
|
if((oldCost != costMapCostToSBPLCost(robot_costmap_2d::LETHAL_OBSTACLE) && oldCost != costMapCostToSBPLCost(robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)) &&
|
|
(newCost == costMapCostToSBPLCost(robot_costmap_2d::LETHAL_OBSTACLE) || newCost == costMapCostToSBPLCost(robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE))) {
|
|
offOnCount++;
|
|
}
|
|
|
|
if((oldCost == costMapCostToSBPLCost(robot_costmap_2d::LETHAL_OBSTACLE) || oldCost == costMapCostToSBPLCost(robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)) &&
|
|
(newCost != costMapCostToSBPLCost(robot_costmap_2d::LETHAL_OBSTACLE) && newCost != costMapCostToSBPLCost(robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE))) {
|
|
onOffCount++;
|
|
}
|
|
env_->UpdateCost(ix, iy, costMapCostToSBPLCost(grid->getCost(ix,iy)));
|
|
|
|
nav2dcell_t nav2dcell;
|
|
nav2dcell.x = ix;
|
|
nav2dcell.y = iy;
|
|
changedcellsV.push_back(nav2dcell);
|
|
}
|
|
}
|
|
|
|
try{
|
|
if(!changedcellsV.empty()){
|
|
StateChangeQuery* scq = new LatticeSCQ(env_, changedcellsV);
|
|
planner_->costs_changed(*scq);
|
|
delete scq;
|
|
}
|
|
|
|
if(allCount > force_scratch_limit_)
|
|
planner_->force_planning_from_scratch();
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
robot::log_error("SBPL failed to update the costmap");
|
|
return false;
|
|
}
|
|
|
|
//setting planner parameters
|
|
robot::log_debug("allocated:%f, init eps:%f\n",allocated_time_,initial_epsilon_);
|
|
planner_->set_initialsolution_eps(initial_epsilon_);
|
|
planner_->set_search_mode(false);
|
|
|
|
robot::log_debug("[sbpl_lattice_planner] run planner");
|
|
const robot::Time plan_started = robot::Time::now();
|
|
// NGÂN SÁCH CHUNG (2026-09-11): `allocated_time` là trần của CẢ lượt makePlan, không phải của
|
|
// từng lượt replan nội bộ. Trước đây ba lượt (free heading → heading thật → căn lại heading) mỗi
|
|
// lượt được cấp lại nguyên allocated_time, trần thật là 3x — 30 s với config 10 s, và
|
|
// PlannerRunner giữ costmap mutex suốt chừng ấy. Lượt sau chỉ chạy khi còn >= kMinReplanBudget.
|
|
constexpr double kMinReplanBudget = 0.5; // [s] dưới mức này một lượt ARA* không kịp ra gì
|
|
const auto budget_left = [&]() {
|
|
return allocated_time_ - (robot::Time::now() - plan_started).toSec();
|
|
};
|
|
vector<int> solution_stateIDs;
|
|
int solution_cost;
|
|
try{
|
|
int ret = planner_->replan(allocated_time_, &solution_stateIDs, &solution_cost);
|
|
if(preempt_.load(std::memory_order_relaxed)){
|
|
robot::log_info("[sbpl_lattice_planner] preempted after %.0f ms (first search)",
|
|
(robot::Time::now() - plan_started).toSec() * 1000.0);
|
|
return false;
|
|
}
|
|
if(!ret && start_heading_overridden && budget_left() >= kMinReplanBudget){
|
|
// Heading giả định có thể không khả thi (vd hướng tới goal đâm thẳng vào tường
|
|
// ngay tại cell xuất phát) — thử lại MỘT lần với heading thật trước khi bỏ cuộc.
|
|
robot::log_warning("[sbpl_lattice_planner] no solution with free start heading, "
|
|
"retrying with actual robot heading (%.2f s left)", budget_left());
|
|
int sid = env_->SetStart(start.pose.position.x - grid->getOriginX(),
|
|
start.pose.position.y - grid->getOriginY(),
|
|
theta_start_actual);
|
|
if(sid >= 0 && planner_->set_start(sid) != 0){
|
|
ret = planner_->replan(budget_left(), &solution_stateIDs, &solution_cost);
|
|
}
|
|
if(preempt_.load(std::memory_order_relaxed)){
|
|
robot::log_info("[sbpl_lattice_planner] preempted after %.0f ms (heading retry)",
|
|
(robot::Time::now() - plan_started).toSec() * 1000.0);
|
|
return false;
|
|
}
|
|
}
|
|
if(ret)
|
|
{
|
|
// eps THẬT đạt được, không phải eps đặt trong config. Cần thiết vì toàn bộ log nội bộ của
|
|
// SBPL bị compile bỏ (`Test/sbpl/.../config.h:53 DEBUG 0` làm `SBPL_PRINTF` rỗng), nên đây
|
|
// là đường DUY NHẤT biết ARA* dừng ở nấc nào. Không có nó thì mọi kết luận về
|
|
// `allocated_time` chỉ là suy đoán từ thời gian đo được.
|
|
//
|
|
// Đọc thế nào: eps == initial_epsilon nghĩa là hết giờ ngay ở nấc đầu — nới `allocated_time`
|
|
// hoặc hạ `initial_epsilon`. eps == 1.0 nghĩa là đã tới nghiệm tối ưu.
|
|
robot::log_info("[sbpl_lattice_planner] solved: eps=%.3f (init %.2f), cost=%d, %zu state(s), "
|
|
"%.0f ms of %.2f s allowed",
|
|
planner_->get_final_epsilon(), initial_epsilon_, solution_cost,
|
|
solution_stateIDs.size(),
|
|
(robot::Time::now() - plan_started).toSec() * 1000.0, allocated_time_);
|
|
}
|
|
else{
|
|
robot::log_info("Solution not found\n");
|
|
// publishStats(solution_cost, 0, start, goal);
|
|
return false;
|
|
}
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
robot::log_error("SBPL encountered a fatal exception while planning");
|
|
return false;
|
|
}
|
|
|
|
robot::log_debug("size of solution=%d", (int)solution_stateIDs.size());
|
|
|
|
vector<EnvNAVXYTHETALAT3Dpt_t> sbpl_path;
|
|
try{
|
|
env_->ConvertStateIDPathintoXYThetaPath(&solution_stateIDs, &sbpl_path);
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
robot::log_error("SBPL encountered a fatal exception while reconstructing the path");
|
|
return false;
|
|
}
|
|
if (start_heading_overridden && sbpl_path.size() > 1) {
|
|
// free_start_heading nhắm heading giả định thẳng tới goal; nếu hướng đó bị chặn,
|
|
// path vẫn mở đầu bằng cung rẽ từ heading giả định sang hướng đi thật. Yêu cầu vận
|
|
// hành là đoạn đầu path phải THẲNG (robot quay tại chỗ xong là bám thẳng), nên nếu
|
|
// phát hiện đoạn đầu cong thì căn lại heading xuất phát theo hướng đi thật của path
|
|
// rồi plan lại đúng MỘT lần (không lặp — lần hai đã xuất phát đúng hướng đi thật
|
|
// nên primitive thẳng là lựa chọn rẻ nhất).
|
|
constexpr double kStraightLeadCheckM = 0.5; // [m] cửa sổ kiểm tra độ thẳng đoạn đầu
|
|
constexpr double kMaxLeadHeadingDevRad = 0.2; // [rad] ~ nửa bước góc lưới 16 hướng
|
|
double lead_len = 0.0;
|
|
double max_dev = 0.0;
|
|
size_t iend = 1;
|
|
for (; iend < sbpl_path.size() && lead_len < kStraightLeadCheckM; ++iend) {
|
|
lead_len += std::hypot(sbpl_path[iend].x - sbpl_path[iend - 1].x,
|
|
sbpl_path[iend].y - sbpl_path[iend - 1].y);
|
|
max_dev = std::max(max_dev, std::fabs(std::remainder(
|
|
sbpl_path[iend].theta - sbpl_path[0].theta, 2.0 * M_PI)));
|
|
}
|
|
if (max_dev > kMaxLeadHeadingDevRad) {
|
|
// Tìm HƯỚNG ĐI THẬT của route: đoạn thẳng đầu tiên trên path (heading giữ nguyên
|
|
// liên tục >= kStraightRunM). Với U-turn dài hơn cửa sổ 0.5m, mốc cố định sẽ rơi
|
|
// vào giữa cung — phải quét qua hết phần cong. Đoạn thẳng mở đầu đi theo hướng
|
|
// route tìm được, KHÔNG ép theo hướng tới goal (goal chỉ là seed cho lần plan đầu).
|
|
constexpr double kStraightRunM = 0.3; // [m] độ dài tối thiểu để tính là đoạn thẳng
|
|
constexpr double kScanLimitM = 3.0; // [m] giới hạn quét phần cong mở đầu
|
|
double theta_realign = sbpl_path[iend - 1].theta; // fallback: heading sau cửa sổ đầu
|
|
double scanned = 0.0;
|
|
for (size_t i = 0; i + 1 < sbpl_path.size() && scanned < kScanLimitM; ++i) {
|
|
double run = 0.0;
|
|
bool straight = true;
|
|
for (size_t j = i + 1; j < sbpl_path.size() && run < kStraightRunM; ++j) {
|
|
run += std::hypot(sbpl_path[j].x - sbpl_path[j - 1].x,
|
|
sbpl_path[j].y - sbpl_path[j - 1].y);
|
|
if (std::fabs(std::remainder(sbpl_path[j].theta - sbpl_path[i].theta,
|
|
2.0 * M_PI)) > 1e-3) {
|
|
straight = false;
|
|
break;
|
|
}
|
|
}
|
|
if (straight && run >= kStraightRunM) {
|
|
theta_realign = sbpl_path[i].theta;
|
|
break;
|
|
}
|
|
scanned += std::hypot(sbpl_path[i + 1].x - sbpl_path[i].x,
|
|
sbpl_path[i + 1].y - sbpl_path[i].y);
|
|
}
|
|
if(preempt_.load(std::memory_order_relaxed)){
|
|
robot::log_info("[sbpl_lattice_planner] preempted after %.0f ms (before realign)",
|
|
(robot::Time::now() - plan_started).toSec() * 1000.0);
|
|
return false;
|
|
}
|
|
if(budget_left() < kMinReplanBudget){
|
|
robot::log_debug("[sbpl_lattice_planner] initial segment curved (dev %.2f rad) but only "
|
|
"%.2f s of the budget left — keeping the first path", max_dev, budget_left());
|
|
}
|
|
else{
|
|
robot::log_debug("[sbpl_lattice_planner] initial segment curved (dev %.2f rad), "
|
|
"replanning with start heading realigned to %.2f (%.2f s left)",
|
|
max_dev, theta_realign, budget_left());
|
|
try{
|
|
int sid = env_->SetStart(start.pose.position.x - grid->getOriginX(),
|
|
start.pose.position.y - grid->getOriginY(),
|
|
theta_realign);
|
|
vector<int> realigned_ids;
|
|
int realigned_cost;
|
|
if(sid >= 0 && planner_->set_start(sid) != 0 &&
|
|
planner_->replan(budget_left(), &realigned_ids, &realigned_cost)){
|
|
vector<EnvNAVXYTHETALAT3Dpt_t> realigned_path;
|
|
env_->ConvertStateIDPathintoXYThetaPath(&realigned_ids, &realigned_path);
|
|
if(!realigned_path.empty()){
|
|
sbpl_path = realigned_path;
|
|
}
|
|
}
|
|
if(preempt_.load(std::memory_order_relaxed)){
|
|
robot::log_info("[sbpl_lattice_planner] preempted after %.0f ms (realign)",
|
|
(robot::Time::now() - plan_started).toSec() * 1000.0);
|
|
return false;
|
|
}
|
|
}
|
|
catch(SBPL_Exception *e){
|
|
// giữ path đầu tiên — cong nhưng vẫn hợp lệ, không được trả thất bại ở đây
|
|
robot::log_warning("[sbpl_lattice_planner] realigned replan failed, keeping first path");
|
|
}
|
|
} // budget_left() >= kMinReplanBudget
|
|
}
|
|
}
|
|
|
|
// if the plan has zero points, add a single point to make move_base happy
|
|
if( sbpl_path.size() == 0 ) {
|
|
EnvNAVXYTHETALAT3Dpt_t s(
|
|
start.pose.position.x - grid->getOriginX(),
|
|
start.pose.position.y - grid->getOriginY(),
|
|
theta_start);
|
|
// sbpl_path.push_back(s);
|
|
}
|
|
|
|
robot::log_debug("Plan has %d points.\n", (int)sbpl_path.size());
|
|
robot::Time plan_time = robot::Time::now();
|
|
|
|
// if (publish_footprint_path_)
|
|
// {
|
|
// robot_visualization_msgs::Marker sbpl_plan_footprint;
|
|
// getFootprintList(sbpl_path, costmap_robot_->getGlobalFrameID(), sbpl_plan_footprint);
|
|
// sbpl_plan_footprint_pub_.publish(sbpl_plan_footprint);
|
|
// }
|
|
|
|
//create a message for the plan
|
|
// nav_msgs::Path gui_path;
|
|
// gui_path.poses.resize(sbpl_path.size());
|
|
// gui_path.header.frame_id = costmap_robot_->getGlobalFrameID();
|
|
// gui_path.header.stamp = plan_time;
|
|
for(unsigned int i=0; i<sbpl_path.size(); i++){
|
|
robot_geometry_msgs::PoseStamped pose;
|
|
pose.header.stamp = plan_time;
|
|
pose.header.frame_id = costmap_robot_->getGlobalFrameID();
|
|
|
|
pose.pose.position.x = sbpl_path[i].x + grid->getOriginX();
|
|
pose.pose.position.y = sbpl_path[i].y + grid->getOriginY();
|
|
pose.pose.position.z = start.pose.position.z;
|
|
|
|
// tf2::Quaternion temp;
|
|
// temp.setRPY(0,0,sbpl_path[i].theta);
|
|
// pose.pose.orientation.x = temp.getX();
|
|
// pose.pose.orientation.y = temp.getY();
|
|
// pose.pose.orientation.z = temp.getZ();
|
|
// pose.pose.orientation.w = temp.getW();
|
|
pose.pose.orientation = data_convert::getQuaternion(sbpl_path[i].theta);
|
|
|
|
plan.push_back(pose);
|
|
|
|
// gui_path.poses[i] = plan[i];
|
|
}
|
|
// plan_pub_.publish(gui_path);
|
|
// publishStats(solution_cost, sbpl_path.size(), start, goal);
|
|
|
|
return true;
|
|
}
|
|
|
|
void SBPLLatticePlanner::getFootprintList(const std::vector<EnvNAVXYTHETALAT3Dpt_t>& sbpl_path,
|
|
const std::string& path_frame_id, robot_visualization_msgs::Marker& ma)
|
|
{
|
|
ma.header.frame_id = path_frame_id;
|
|
ma.header.stamp = robot::Time();
|
|
ma.ns = "sbpl_robot_footprint";
|
|
ma.id = 0;
|
|
ma.type = robot_visualization_msgs::Marker::LINE_LIST;
|
|
ma.action = robot_visualization_msgs::Marker::ADD;
|
|
ma.scale.x = 0.05;
|
|
ma.color.a = 1.0;
|
|
ma.color.r = 0.0;
|
|
ma.color.g = 0.0;
|
|
ma.color.b = 1.0;
|
|
ma.pose.orientation.w = 1.0;
|
|
|
|
for (unsigned int i = 0; i < sbpl_path.size(); i = i + visualizer_skip_poses_)
|
|
{
|
|
std::vector<robot_geometry_msgs::Point> transformed_rfp;
|
|
robot_geometry_msgs::Pose robot_pose;
|
|
robot_pose.position.x = sbpl_path[i].x + costmap_robot_->getCostmap()->getOriginX();
|
|
robot_pose.position.y = sbpl_path[i].y + costmap_robot_->getCostmap()->getOriginY();
|
|
robot_pose.position.z = 0.0;
|
|
|
|
robot_pose.orientation = data_convert::getQuaternion(sbpl_path[i].theta);
|
|
transformFootprintToEdges(robot_pose, footprint_, transformed_rfp);
|
|
|
|
for (auto & point : transformed_rfp)
|
|
ma.points.push_back(point);
|
|
}
|
|
}
|
|
|
|
void SBPLLatticePlanner::transformFootprintToEdges(const robot_geometry_msgs::Pose& robot_pose,
|
|
const std::vector<robot_geometry_msgs::Point>& footprint,
|
|
std::vector<robot_geometry_msgs::Point>& out_footprint)
|
|
{
|
|
out_footprint.resize(2 * footprint.size());
|
|
double yaw = data_convert::getYaw(robot_pose.orientation);
|
|
for (unsigned int i = 0; i < footprint.size(); i++)
|
|
{
|
|
out_footprint[2 * i].x = robot_pose.position.x + cos(yaw) * footprint[i].x - sin(yaw) * footprint[i].y;
|
|
out_footprint[2 * i].y = robot_pose.position.y + sin(yaw) * footprint[i].x + cos(yaw) * footprint[i].y;
|
|
if (i == 0)
|
|
{
|
|
out_footprint.back().x = out_footprint[i].x;
|
|
out_footprint.back().y = out_footprint[i].y;
|
|
}
|
|
else
|
|
{
|
|
out_footprint[2 * i - 1].x = out_footprint[2 * i].x;
|
|
out_footprint[2 * i - 1].y = out_footprint[2 * i].y;
|
|
}
|
|
}
|
|
}
|
|
// Export factory function
|
|
robot_nav_core::BaseGlobalPlanner::Ptr SBPLLatticePlanner::create() {
|
|
return std::make_shared<sbpl_lattice_planner::SBPLLatticePlanner>();
|
|
}
|
|
|
|
// Cầu capability cho bên NGOÀI .so: caller chỉ giữ BaseGlobalPlanner* (nạp qua alias factory),
|
|
// còn dynamic_cast xuống lớp cụ thể chạy Ở TRONG .so này — typeinfo nhất quán, không phụ thuộc
|
|
// RTLD_LOCAL/GLOBAL của boost::dll. Planner không phải instance của lớp này thì bỏ qua kèm log.
|
|
void setExternalGridOn(robot_nav_core::BaseGlobalPlanner* planner,
|
|
const robot_costmap_2d::Costmap2D* grid) {
|
|
SBPLLatticePlanner* self = dynamic_cast<SBPLLatticePlanner*>(planner);
|
|
if (self != NULL) {
|
|
self->setExternalGrid(grid);
|
|
} else {
|
|
robot::log_error("[sbpl_lattice_planner] setExternalGridOn: planner is not an "
|
|
"SBPLLatticePlanner instance; external grid ignored.");
|
|
}
|
|
}
|
|
|
|
void SBPLLatticePlanner::preempt()
|
|
{
|
|
// Từ thread khác: một store, không lock. ARA* đọc cờ này qua set_abort_flag (nếu libsbpl hỗ trợ);
|
|
// makePlan đọc nó giữa các lượt replan nội bộ. Không log ở đây — chạy trên control thread.
|
|
preempt_.store(true, std::memory_order_relaxed);
|
|
}
|
|
|
|
};
|
|
|
|
// Alias cho Boost.DLL (nếu muốn dùng boost::dll::import_alias)
|
|
BOOST_DLL_ALIAS(sbpl_lattice_planner::SBPLLatticePlanner::create, SBPLLatticePlanner)
|
|
// Capability lưới ngoài: tra theo tên "<planner_symbol>ExternalGrid" (xem sbpl_recovery).
|
|
BOOST_DLL_ALIAS(sbpl_lattice_planner::setExternalGridOn, SBPLLatticePlannerExternalGrid)
|