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:
66
include/sbpl_lattice_planner/directional_environment.h
Normal file
66
include/sbpl_lattice_planner/directional_environment.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef SBPL_LATTICE_PLANNER_DIRECTIONAL_ENVIRONMENT_H_
|
||||
#define SBPL_LATTICE_PLANNER_DIRECTIONAL_ENVIRONMENT_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#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 each successful InitializeEnv; false means invalid primitive geometry. */
|
||||
bool buildDirectionTable();
|
||||
/** @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_;
|
||||
};
|
||||
} // namespace sbpl_lattice_planner
|
||||
#endif
|
||||
@@ -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 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user