This commit is contained in:
2026-08-09 19:21:50 +07:00
parent 84d4becbdf
commit 6fbc29f6d8
3 changed files with 193 additions and 197 deletions

View File

@@ -82,14 +82,22 @@ class LatticeSCQ : public StateChangeQuery{
};
SBPLLatticePlanner::SBPLLatticePlanner()
: initialized_(false), costmap_robot_(NULL){
: initialized_(false), costmap_robot_(NULL), external_grid_(NULL){
}
SBPLLatticePlanner::SBPLLatticePlanner(std::string name, robot_costmap_2d::Costmap2DROBOT* costmap_robot)
: initialized_(false), costmap_robot_(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_){
@@ -194,7 +202,7 @@ bool SBPLLatticePlanner::initialize(std::string name, robot_costmap_2d::Costmap2
bool ret;
try{
robot::log_warning("DEBUG 2000");
// robot::log_warning("DEBUG 2000");
ret = env_->InitializeEnv(costmap_robot_->getCostmap()->getSizeInCellsX(), // width
costmap_robot_->getCostmap()->getSizeInCellsY(), // height
0, // mapdata
@@ -207,7 +215,7 @@ bool SBPLLatticePlanner::initialize(std::string name, robot_costmap_2d::Costmap2
ret = true;
current_env_width_ = costmap_robot_->getCostmap()->getSizeInCellsX();
current_env_height_ = costmap_robot_->getCostmap()->getSizeInCellsY();
robot::log_warning("DEBUG 3000");
// robot::log_warning("DEBUG 3000");
}
catch(SBPL_Exception *e){
robot::log_error("SBPL encountered a fatal exception: %s", e->what());
@@ -291,12 +299,16 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
return false;
}
// 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_ != costmap_robot_->getCostmap()->getSizeInCellsX() ||
current_env_height_ != costmap_robot_->getCostmap()->getSizeInCellsY()) {
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_,
costmap_robot_->getCostmap()->getSizeInCellsX(), costmap_robot_->getCostmap()->getSizeInCellsY());
grid->getSizeInCellsX(), grid->getSizeInCellsY());
do_init = true;
}
else if (footprint_ != costmap_robot_->getRobotFootprint()) {
@@ -314,6 +326,19 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
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_);
}
@@ -336,14 +361,14 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
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 * costmap_robot_->getCostmap()->getResolution())
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 - costmap_robot_->getCostmap()->getOriginX(), start.pose.position.y - costmap_robot_->getCostmap()->getOriginY(), theta_start);
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;
@@ -355,7 +380,7 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
}
try{
int ret = env_->SetGoal(goal.pose.position.x - costmap_robot_->getCostmap()->getOriginX(), goal.pose.position.y - costmap_robot_->getCostmap()->getOriginY(), theta_goal);
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;
@@ -371,11 +396,11 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
int allCount = 0;
vector<nav2dcell_t> changedcellsV;
for(unsigned int ix = 0; ix < costmap_robot_->getCostmap()->getSizeInCellsX(); ix++) {
for(unsigned int iy = 0; iy < costmap_robot_->getCostmap()->getSizeInCellsY(); iy++) {
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(costmap_robot_->getCostmap()->getCost(ix,iy));
unsigned char newCost = costMapCostToSBPLCost(grid->getCost(ix,iy));
if(oldCost == newCost) continue;
@@ -392,7 +417,7 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
(newCost != costMapCostToSBPLCost(robot_costmap_2d::LETHAL_OBSTACLE) && newCost != costMapCostToSBPLCost(robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE))) {
onOffCount++;
}
env_->UpdateCost(ix, iy, costMapCostToSBPLCost(costmap_robot_->getCostmap()->getCost(ix,iy)));
env_->UpdateCost(ix, iy, costMapCostToSBPLCost(grid->getCost(ix,iy)));
nav2dcell_t nav2dcell;
nav2dcell.x = ix;
@@ -431,8 +456,8 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
// 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");
int sid = env_->SetStart(start.pose.position.x - costmap_robot_->getCostmap()->getOriginX(),
start.pose.position.y - costmap_robot_->getCostmap()->getOriginY(),
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(allocated_time_, &solution_stateIDs, &solution_cost);
@@ -510,8 +535,8 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
robot::log_debug("[sbpl_lattice_planner] initial segment curved (dev %.2f rad), "
"replanning with start heading realigned to %.2f", max_dev, theta_realign);
try{
int sid = env_->SetStart(start.pose.position.x - costmap_robot_->getCostmap()->getOriginX(),
start.pose.position.y - costmap_robot_->getCostmap()->getOriginY(),
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;
@@ -534,8 +559,8 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
// 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 - costmap_robot_->getCostmap()->getOriginX(),
start.pose.position.y - costmap_robot_->getCostmap()->getOriginY(),
start.pose.position.x - grid->getOriginX(),
start.pose.position.y - grid->getOriginY(),
theta_start);
// sbpl_path.push_back(s);
}
@@ -560,8 +585,8 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
pose.header.stamp = plan_time;
pose.header.frame_id = costmap_robot_->getGlobalFrameID();
pose.pose.position.x = sbpl_path[i].x + costmap_robot_->getCostmap()->getOriginX();
pose.pose.position.y = sbpl_path[i].y + costmap_robot_->getCostmap()->getOriginY();
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;
@@ -641,8 +666,23 @@ 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.");
}
}
};
// Alias cho Boost.DLL (nếu muốn dùng boost::dll::import_alias)
BOOST_DLL_ALIAS(sbpl_lattice_planner::SBPLLatticePlanner::create, SBPLLatticePlanner)
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)