Files
sbpl_lattice_planner/include/sbpl_lattice_planner/sbpl_lattice_planner.h
duongtd 47dd76f39e 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.
2026-11-03 16:55:23 +07:00

203 lines
9.2 KiB
C++

#ifndef SBPL_LATTICE_PLANNER_H
#define SBPL_LATTICE_PLANNER_H
#include <atomic>
#include <chrono>
#include <iostream>
#include <vector>
// sbpl headers
#include <sbpl/headers.h>
// ROBOT
#include <robot/robot.h>
#include <robot_geometry_msgs/PoseStamped.h>
#include <robot_visualization_msgs/Marker.h>
// Costmap used for the map representation
#include <robot_costmap_2d/costmap_2d_robot.h>
// global representation
#include <robot_nav_core/base_global_planner.h>
#include <robot_nav_core/preemptable.h>
#include <sbpl_lattice_planner/directional_environment.h>
using namespace std;
namespace sbpl_lattice_planner{
class SBPLLatticePlanner : public robot_nav_core::BaseGlobalPlanner,
public robot_nav_core::Preemptable {
public:
/**
* @brief Default constructor for the NavFnROBOT object
*/
SBPLLatticePlanner();
/**
* @brief Constructor for the SBPLLatticePlanner object
* @param name The name of this planner
* @param costmap_robot A pointer to the ROBOT wrapper of the costmap to use
*/
SBPLLatticePlanner(std::string name, robot_costmap_2d::Costmap2DROBOT* costmap_robot);
/**
* @brief Initialization function for the SBPLLatticePlanner object
* @param name The name of this planner
* @param costmap_robot A pointer to the ROBOT wrapper of the costmap to use
*/
virtual bool initialize(std::string name,
robot_costmap_2d::Costmap2DROBOT* costmap_robot);
/**
* @brief Given a goal pose in the world, compute a plan
* @param start The start pose
* @param goal The goal pose
* @param plan The plan... filled by the planner
* @return True if a valid plan was found, false otherwise
*/
virtual bool makePlan(const robot_geometry_msgs::PoseStamped& start,
const robot_geometry_msgs::PoseStamped& goal,
std::vector<robot_geometry_msgs::PoseStamped>& plan);
/**
* @brief Cắt lượt makePlan đang chạy (robot_nav_core::Preemptable, 2026-09-11).
*
* Gọi từ thread KHÁC thread đang ở trong makePlan: một atomic store, không lock. ARA* thấy cờ ở
* vòng expand kế và trả "không có nghiệm"; các lượt replan nội bộ (retry heading thật, căn lại
* heading) không được khởi động nữa. makePlan trả false. Cờ được xoá ở đầu mỗi makePlan.
*
* Chỉ có hiệu lực khi libsbpl đang nạp có abort flag (SBPL_HAS_ABORT_FLAG + probe
* sbpl_abort_flag_abi_version lúc initialize); không có thì preempt() chỉ đặt cờ cho phần kiểm
* giữa các lượt replan, còn một lượt ARA* đang chạy vẫn đi tới hết allocated_time.
*/
void preempt() override;
/**
* @brief Nạp lưới NGOÀI thay cho lưới sống của costmap trong các lần makePlan kế tiếp.
*
* Dùng cho detour trên lưới gộp (global nền + local đè): bên gọi snapshot hai costmap dưới mutex
* rồi đưa bản chụp vào đây — makePlan đọc bản chụp, KHÔNG giữ mutex costmap nào trong lúc search.
*
* Ràng buộc: lưới ngoài phải CÙNG kích thước/resolution với costmap đã initialize() (bản chụp
* gộp lấy global làm nền nên tự thoả). Footprint/inflation/frame vẫn đọc từ costmap sống.
* Con trỏ non-owning, bên gọi giữ buffer sống qua lời gọi makePlan. `nullptr` = quay lại lưới sống.
*
* Không thuộc interface `BaseGlobalPlanner` — bên ngoài .so gọi qua alias Boost.DLL
* `SBPLLatticePlannerExternalGrid` (xem cuối sbpl_lattice_planner.cpp).
*/
void setExternalGrid(const robot_costmap_2d::Costmap2D* grid);
virtual ~SBPLLatticePlanner(){};
static robot_nav_core::BaseGlobalPlanner::Ptr create();
private:
unsigned char costMapCostToSBPLCost(unsigned char newcost);
unsigned char computeCircumscribedCost();
static void transformFootprintToEdges(const robot_geometry_msgs::Pose& robot_pose,
const std::vector<robot_geometry_msgs::Point>& footprint,
std::vector<robot_geometry_msgs::Point>& out_footprint);
void getFootprintList(const std::vector<EnvNAVXYTHETALAT3Dpt_t>& sbpl_path, const std::string& path_frame_id,
robot_visualization_msgs::Marker& ma);
bool initialized_;
SBPLPlanner* planner_;
std::atomic<bool> preempt_{false}; ///< Cờ cắt lượt makePlan đang chạy (Preemptable).
bool sbpl_abort_supported_ = false; ///< libsbpl đang nạp hiểu set_abort_flag (probe lúc initialize).
DirectionalEnvironmentNAVXYTHETALAT* env_;
bool have_previous_direction_ = false;
bool direction_scratch_pending_ = false;
robot_costmap_2d::DirectionFieldState previous_direction_state_ = robot_costmap_2d::DirectionFieldState::kDisabled;
uint64_t previous_direction_generation_ = 0;
std::chrono::steady_clock::time_point last_direction_pending_log_{};
std::chrono::steady_clock::time_point direction_pending_since_{};
std::chrono::steady_clock::time_point last_direction_geometry_log_{};
double current_env_resolution_ = 0.0;
std::string planner_type_; /**< sbpl method to use for planning. choices are ARAPlanner and ADPlanner */
double allocated_time_; /**< amount of time allowed for search */
double initial_epsilon_; /**< initial epsilon for beginning the anytime search */
std::string environment_type_; /** what type of environment in which to plan. choices are 2D and XYThetaLattice. */
std::string cost_map_topic_; /** what topic is being used for the costmap topic */
bool forward_search_; /** whether to use forward or backward search */
std::string primitive_filename_; /** where to find the motion primitives for the current robot */
int force_scratch_limit_; /** the number of cells that have to be changed in the costmap to force the planner to plan from scratch even if its an incremental planner */
unsigned char lethal_obstacle_;
unsigned char inscribed_inflated_obstacle_;
unsigned char circumscribed_cost_;
unsigned char sbpl_cost_multiplier_;
bool publish_footprint_path_;
int visualizer_skip_poses_;
bool allow_unknown_;
/** Bỏ ràng buộc heading tại điểm xuất phát, đảm bảo path MỞ ĐẦU BẰNG ĐOẠN THẲNG
* theo hướng đi thật của route (không nhất thiết hướng tới goal). Dùng khi local
* planner tự quay tại chỗ về hướng path trước khi bám (turn_around_priority) — khi
* đó heading xuất phát không phải ràng buộc thật, giữ nó chỉ ép SBPL vẽ cung quay
* đầu lúc goal nằm phía sau robot. Cơ chế: plan lần 1 với heading seed hướng tới
* goal; nếu đoạn đầu vẫn cong thì căn heading theo đoạn thẳng đầu tiên của path và
* plan lại một lần. Không ra nghiệm thì retry với heading thật. */
bool free_start_heading_;
// ---- Chẩn đoán lập plan (2026-09-18) ----------------------------------------------------------
// Một lượt makePlan fail phải tự nói được: goal/start đứng trên ô gì, có đường 2D không, search
// dừng vì cạn hay hết giờ. Dòng LỖI (goal bị từ chối, NO SOLUTION) không nằm sau công tắc nào.
/// Nhóm log `debug_plan`: audit goal/start khi hợp lệ, h 2D khi hữu hạn, thống kê `solved:`.
bool debug_plan_;
/// [ô] Bán kính cửa sổ lưới in ASCII quanh goal và start khi search fail. 0 = tắt.
int debug_grid_window_;
/// @brief Kết quả soi một pose trên lưới đang có hiệu lực.
struct PoseAudit
{
bool in_map = false; ///< Ô tâm nằm trong lưới.
unsigned int mx = 0, my = 0; ///< Chỉ số ô tâm (chỉ có nghĩa khi in_map).
unsigned char cost = 0; ///< Cost costmap tại ô tâm (0..255).
bool footprint_ok = false; ///< Footprint tại (x, y, theta) không đè lethal / mép lưới.
};
/// @return tên lớp cost để in log: FREE / INFLATED / INSCRIBED / LETHAL / UNKNOWN.
static const char* costClassName(unsigned char cost);
/// @brief Soi pose: ô tâm, cost, footprint. Không đổi trạng thái env.
PoseAudit auditPose(const robot_costmap_2d::Costmap2D* grid, double wx, double wy,
double theta) const;
/// @brief In cửa sổ ASCII (2r+1)^2 ô quanh (cx, cy); đánh dấu S (start) và G (goal).
void logGridWindow(const robot_costmap_2d::Costmap2D* grid, int radius, unsigned int cx,
unsigned int cy, unsigned int sx, unsigned int sy, unsigned int gx,
unsigned int gy, const char* label) const;
/// @return lưới đang có hiệu lực: lưới ngoài nếu được nạp, không thì lưới sống của costmap.
const robot_costmap_2d::Costmap2D* activeGrid() const;
std::string name_;
robot_costmap_2d::Costmap2DROBOT* costmap_robot_; /**< manages the cost map for us */
const robot_costmap_2d::Costmap2D* external_grid_; /**< non-owning; NULL = dùng lưới sống */
std::vector<robot_geometry_msgs::Point> footprint_;
std::vector<robot_geometry_msgs::Point> footprint_prev_;
unsigned int current_env_width_;
unsigned int current_env_height_;
};
};
#endif