feat(sbpl): plan only along allowed directions of the direction field
DirectionalEnvironmentNAVXYTHETALAT overrides GetActionCost: after the base obstacle cost, it rejects an action when a cell the primitive's reference point crosses has a direction code whose mask forbids that segment's displacement (dot(v, d) >= 0 allowed), or when the cell is outside the grid. Per-action (dx, dy, mask) tables are precomputed with the shared DDA once per environment init; near-integer grid coordinates are snapped to avoid sliver cells from binary rounding. makePlan reads the LayeredCostmap direction snapshot once and keeps it for the whole call: PENDING or a geometry mismatch refuses to plan, a state/generation change forces planning from scratch, and the path is discarded if the field changed during the search. Environment init failure now returns false instead of exit(1). Tests: direction_environment_test (4) passes, including forward and backward ARA* detours and the no-solution case.
This commit is contained in:
@@ -292,7 +292,7 @@ bool SBPLLatticePlanner::initialize(std::string name, robot_costmap_2d::Costmap2
|
||||
|
||||
if ("XYThetaLattice" == environment_type_){
|
||||
robot::log_debug("Using a 3D costmap for theta lattice\n");
|
||||
env_ = new EnvironmentNAVXYTHETALAT();
|
||||
env_ = new DirectionalEnvironmentNAVXYTHETALAT();
|
||||
}
|
||||
else{
|
||||
robot::log_error("XYThetaLattice is currently the only supported environment!\n");
|
||||
@@ -356,7 +356,8 @@ bool SBPLLatticePlanner::initialize(std::string name, robot_costmap_2d::Costmap2
|
||||
perimeterptsV, init_grid->getResolution(), nominalvel_mpersecs,
|
||||
timetoturn45degsinplace_secs, obst_cost_thresh,
|
||||
primitive_filename_.c_str());
|
||||
ret = true;
|
||||
if (ret) ret = env_->buildDirectionTable();
|
||||
current_env_resolution_ = init_grid->getResolution();
|
||||
current_env_width_ = init_grid->getSizeInCellsX();
|
||||
current_env_height_ = init_grid->getSizeInCellsY();
|
||||
// robot::log_warning("DEBUG 3000");
|
||||
@@ -366,8 +367,10 @@ bool SBPLLatticePlanner::initialize(std::string name, robot_costmap_2d::Costmap2
|
||||
ret = false;
|
||||
}
|
||||
if(!ret){
|
||||
robot::log_error("SBPL initialization failed!");
|
||||
exit(1);
|
||||
robot::log_error("SBPL initialization or direction table construction failed!");
|
||||
delete env_;
|
||||
env_ = NULL;
|
||||
return false;
|
||||
}
|
||||
for (ssize_t ix(0); ix < init_grid->getSizeInCellsX(); ++ix)
|
||||
for (ssize_t iy(0); iy < init_grid->getSizeInCellsY(); ++iy)
|
||||
@@ -485,8 +488,37 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
// 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();
|
||||
|
||||
const auto direction_snapshot = costmap_robot_->getLayeredCostmap()->getDirectionFieldSnapshot();
|
||||
const robot_costmap_2d::DirectionGeometry initial_geometry{grid->getSizeInCellsX(), grid->getSizeInCellsY(),
|
||||
grid->getResolution(), grid->getOriginX(), grid->getOriginY(), costmap_robot_->getGlobalFrameID()};
|
||||
const auto initial_decision = decideDirection(direction_snapshot, have_previous_direction_,
|
||||
previous_direction_state_, previous_direction_generation_, initial_geometry);
|
||||
if (initial_decision.result == DirectionDecision::Result::kPending) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (direction_pending_since_ == std::chrono::steady_clock::time_point{}) direction_pending_since_ = now;
|
||||
if (last_direction_pending_log_ == std::chrono::steady_clock::time_point{} ||
|
||||
now - last_direction_pending_log_ >= std::chrono::seconds(5)) {
|
||||
const double waited = std::chrono::duration<double>(now - direction_pending_since_).count();
|
||||
robot::log_error("[sbpl_lattice_planner] direction field is not ready (%.1f s); refusing to plan", waited);
|
||||
last_direction_pending_log_ = now;
|
||||
}
|
||||
direction_scratch_pending_ = direction_scratch_pending_ || initial_decision.force_scratch;
|
||||
have_previous_direction_ = true;
|
||||
previous_direction_state_ = direction_snapshot.state;
|
||||
previous_direction_generation_ = direction_snapshot.generation;
|
||||
if (debug_plan_) robot::log_info("[sbpl_lattice_planner] direction state=PENDING generation=%llu rejected=0",
|
||||
static_cast<unsigned long long>(direction_snapshot.generation));
|
||||
return false;
|
||||
}
|
||||
direction_pending_since_ = {};
|
||||
last_direction_pending_log_ = {};
|
||||
|
||||
bool do_init = false;
|
||||
if (current_env_width_ != grid->getSizeInCellsX() ||
|
||||
if (current_env_resolution_ != grid->getResolution()) {
|
||||
robot::log_info("Costmap resolution has changed, reinitializing sbpl_lattice_planner.");
|
||||
do_init = true;
|
||||
}
|
||||
else 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_,
|
||||
@@ -526,7 +558,49 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
grid = activeGrid();
|
||||
}
|
||||
// Re-init đọc cell từ activeGrid(): bản chụp nếu có, lưới sống nếu không (xem initialize()).
|
||||
initialize(name_, costmap_robot_);
|
||||
if (!initialize(name_, costmap_robot_)) return false;
|
||||
}
|
||||
|
||||
// The snapshot owns the field through search, conversion and the optional realignment retry.
|
||||
struct DirectionGuard {
|
||||
DirectionalEnvironmentNAVXYTHETALAT* env;
|
||||
const robot_costmap_2d::DirectionFieldSnapshot& snapshot;
|
||||
bool debug;
|
||||
~DirectionGuard() {
|
||||
if (debug) {
|
||||
const char* state = snapshot.state == robot_costmap_2d::DirectionFieldState::kReady ? "READY" :
|
||||
snapshot.state == robot_costmap_2d::DirectionFieldState::kPending ? "PENDING" : "DISABLED";
|
||||
robot::log_info("[sbpl_lattice_planner] direction state=%s generation=%llu rejected=%llu", state,
|
||||
static_cast<unsigned long long>(snapshot.generation),
|
||||
static_cast<unsigned long long>(env->directionRejections()));
|
||||
}
|
||||
env->setDirectionField(nullptr);
|
||||
}
|
||||
} guard{env_, direction_snapshot, debug_plan_};
|
||||
env_->resetDirectionRejections();
|
||||
const robot_costmap_2d::DirectionGeometry geometry{grid->getSizeInCellsX(), grid->getSizeInCellsY(),
|
||||
grid->getResolution(), grid->getOriginX(), grid->getOriginY(), costmap_robot_->getGlobalFrameID()};
|
||||
const auto decision = decideDirection(direction_snapshot, have_previous_direction_, previous_direction_state_,
|
||||
previous_direction_generation_, geometry);
|
||||
using DirectionResult = DirectionDecision::Result;
|
||||
direction_scratch_pending_ = direction_scratch_pending_ || decision.force_scratch;
|
||||
have_previous_direction_ = true;
|
||||
previous_direction_state_ = direction_snapshot.state;
|
||||
previous_direction_generation_ = direction_snapshot.generation;
|
||||
if (decision.result == DirectionResult::kGeometryMismatch) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (last_direction_geometry_log_ == std::chrono::steady_clock::time_point{} ||
|
||||
now - last_direction_geometry_log_ >= std::chrono::seconds(5)) {
|
||||
robot::log_error("[sbpl_lattice_planner] direction field geometry does not match the active grid");
|
||||
last_direction_geometry_log_ = now;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
last_direction_geometry_log_ = {};
|
||||
env_->setDirectionField(decision.result == DirectionResult::kReady ? direction_snapshot.field.get() : nullptr);
|
||||
if (direction_scratch_pending_) {
|
||||
planner_->force_planning_from_scratch();
|
||||
direction_scratch_pending_ = false;
|
||||
}
|
||||
|
||||
plan.clear();
|
||||
@@ -980,6 +1054,20 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
// plan_pub_.publish(gui_path);
|
||||
// publishStats(solution_cost, sbpl_path.size(), start, goal);
|
||||
|
||||
// The search and realignment use one immutable snapshot. Refuse to hand off a path
|
||||
// for an older map or field when the provider changed while planning.
|
||||
const auto handoff_snapshot = costmap_robot_->getLayeredCostmap()->getDirectionFieldSnapshot();
|
||||
const bool both_disabled = direction_snapshot.state == robot_costmap_2d::DirectionFieldState::kDisabled &&
|
||||
handoff_snapshot.state == robot_costmap_2d::DirectionFieldState::kDisabled;
|
||||
if (!both_disabled && (direction_snapshot.state != handoff_snapshot.state ||
|
||||
direction_snapshot.generation != handoff_snapshot.generation)) {
|
||||
plan.clear();
|
||||
robot::log_warning("[sbpl_lattice_planner] direction field changed during planning "
|
||||
"(generation %llu -> %llu); discarding path",
|
||||
static_cast<unsigned long long>(direction_snapshot.generation),
|
||||
static_cast<unsigned long long>(handoff_snapshot.generation));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user