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 crossed by the
  primitive's reference point has a direction code whose mask forbids
  that segment's displacement, or when the cell is outside the grid.
  Per-action (dx, dy, mask) tables are precomputed with the shared DDA.
- makePlan reads the direction snapshot once per call: PENDING or a
  geometry mismatch refuses to plan, a state or generation change forces
  planning from scratch, and the path is discarded if the field changed
  during the search. Environment init failure returns false instead of
  exit(1).
- The mask table records the deviation it was built with and is rebuilt
  when a ready field carries another max_deviation_deg; on failure the
  field is detached and planning is refused.

Full history: branch feature/direction-zone.
This commit is contained in:
2026-11-04 13:24:16 +07:00
parent c98a5acf44
commit f021a9ea59
8 changed files with 2245 additions and 11 deletions

View File

@@ -0,0 +1,73 @@
#ifndef SBPL_LATTICE_PLANNER_DIRECTIONAL_ENVIRONMENT_H_
#define SBPL_LATTICE_PLANNER_DIRECTIONAL_ENVIRONMENT_H_
#include <cstddef>
#include <cstdint>
#include <limits>
#include <vector>
#include <robot_costmap_2d/direction_field.h>
#include <sbpl/discrete_space_information/environment_navxythetalat.h>
namespace sbpl_lattice_planner
{
/** @brief Search-time decision for a single immutable direction snapshot. */
struct DirectionDecision
{
enum class Result { kDisabled, kPending, kGeometryMismatch, kReady };
Result result;
bool force_scratch;
};
/** @brief Decide whether a snapshot can be used with the active grid. */
DirectionDecision decideDirection(const robot_costmap_2d::DirectionFieldSnapshot& snapshot,
bool have_previous, robot_costmap_2d::DirectionFieldState previous_state,
uint64_t previous_generation,
const robot_costmap_2d::DirectionGeometry& grid);
/** @brief Lattice environment that removes actions violating the direction field.
* The attached field is non-owning and must outlive all search and path conversion calls.
*/
class DirectionalEnvironmentNAVXYTHETALAT : public EnvironmentNAVXYTHETALAT
{
public:
/** @brief A direction mask for one cell relative to the action source. */
struct CellMask { int64_t dx, dy; uint8_t mask; };
/** @brief Rebuild after InitializeEnv or a deviation change.
* @param max_deviation_rad Maximum inclusive deviation [rad], in (0, pi).
* @return False for invalid deviation or primitive geometry; the previous table is retained.
*/
bool buildDirectionTable(double max_deviation_rad);
/** @brief Deviation [rad] used for the current table, or NaN before a successful build. */
double tableMaxDeviationRad() const { return table_max_deviation_rad_; }
/** @brief Attach a snapshot owned by the caller, or detach with nullptr. */
void setDirectionField(const robot_costmap_2d::DirectionField* field) { field_ = field; }
/** @brief Reset per-plan rejection statistics. */
void resetDirectionRejections() { rejections_ = 0; }
/** @brief Number of actions rejected by direction or field bounds. */
uint64_t directionRejections() const { return rejections_; }
/** @brief Inspect precomputed masks for a given action. */
const std::vector<CellMask>& cells() const { return cells_; }
/** @brief Offsets into cells(), indexed by theta * actionWidth() + action. */
const std::vector<size_t>& offsets() const { return offsets_; }
/** @brief Number of actions per source orientation. */
int actionWidth() const { return EnvNAVXYTHETALATCfg.actionwidth; }
/** @brief Number of discretized source orientations. */
int thetaCount() const { return EnvNAVXYTHETALATCfg.NumThetaDirs; }
/** @brief Inspect an SBPL action using valid theta and action indices. */
const EnvNAVXYTHETALATAction_t& action(int theta, int index) const
{ return EnvNAVXYTHETALATCfg.ActionsV[theta][index]; }
int GetActionCost(int source_x, int source_y, int source_theta,
EnvNAVXYTHETALATAction_t* action) override;
private:
const robot_costmap_2d::DirectionField* field_ = nullptr;
uint64_t rejections_ = 0;
std::vector<CellMask> cells_;
std::vector<size_t> offsets_;
double table_max_deviation_rad_ = std::numeric_limits<double>::quiet_NaN();
};
} // namespace sbpl_lattice_planner
#endif

View File

@@ -2,10 +2,12 @@
#define SBPL_LATTICE_PLANNER_H
#include <atomic>
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;
// sbpl headers
#include <sbpl/headers.h>
// ROBOT
#include <robot/robot.h>
@@ -15,13 +17,14 @@ using namespace std;
// Costmap used for the map representation
#include <robot_costmap_2d/costmap_2d_robot.h>
// sbpl headers
#include <sbpl/headers.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,
@@ -111,7 +114,15 @@ private:
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).
EnvironmentNAVXYTHETALAT* env_;
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 */