From 471d13410cca9ac441ebc31c22d28b54fa80ca18 Mon Sep 17 00:00:00 2001 From: duongtd Date: Wed, 4 Nov 2026 08:52:00 +0700 Subject: [PATCH] feat(sbpl): rebuild the direction table when the field deviation changes The per-action mask table records the deviation it was built with. When a ready field carries another value, rebuild before attaching it and plan from scratch; on failure keep the old table, detach the field and refuse to plan. --- .../directional_environment.h | 11 +++++-- src/directional_environment.cpp | 20 ++++++++----- src/sbpl_lattice_planner.cpp | 23 ++++++++++++++- test/direction_environment_test.cpp | 29 ++++++++++++++++--- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/include/sbpl_lattice_planner/directional_environment.h b/include/sbpl_lattice_planner/directional_environment.h index 2eb2e66..0fc8d48 100644 --- a/include/sbpl_lattice_planner/directional_environment.h +++ b/include/sbpl_lattice_planner/directional_environment.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -34,8 +35,13 @@ 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 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. */ @@ -61,6 +67,7 @@ private: uint64_t rejections_ = 0; std::vector cells_; std::vector offsets_; + double table_max_deviation_rad_ = std::numeric_limits::quiet_NaN(); }; } // namespace sbpl_lattice_planner #endif diff --git a/src/directional_environment.cpp b/src/directional_environment.cpp index b990b5c..2c99773 100644 --- a/src/directional_environment.cpp +++ b/src/directional_environment.cpp @@ -32,12 +32,13 @@ DirectionDecision decideDirection(const robot_costmap_2d::DirectionFieldSnapshot return {Result::kReady, changed}; } -bool DirectionalEnvironmentNAVXYTHETALAT::buildDirectionTable() +bool DirectionalEnvironmentNAVXYTHETALAT::buildDirectionTable(double max_deviation_rad) { - cells_.clear(); - offsets_.clear(); + std::vector cells; + std::vector offsets; const double resolution = EnvNAVXYTHETALATCfg.cellsize_m; - if (!std::isfinite(resolution) || resolution <= 0 || !EnvNAVXYTHETALATCfg.ActionsV) + if (!std::isfinite(max_deviation_rad) || max_deviation_rad <= 0 || max_deviation_rad >= M_PI || + !std::isfinite(resolution) || resolution <= 0 || !EnvNAVXYTHETALATCfg.ActionsV) return false; // Snap near boundaries before DDA: binary rounding must not create a spurious sliver cell. const auto snap = [](double q) @@ -49,7 +50,7 @@ bool DirectionalEnvironmentNAVXYTHETALAT::buildDirectionTable() { for (int a = 0; a < actionWidth(); ++a) { - offsets_.push_back(cells_.size()); + offsets.push_back(cells.size()); const auto& primitive = action(theta, a); std::map, uint8_t> masks; auto segment = [&](double x0, double y0, double x1, double y1) @@ -58,7 +59,7 @@ bool DirectionalEnvironmentNAVXYTHETALAT::buildDirectionTable() const double dy = y1 - y0; if (dx == 0 && dy == 0) return true; - const uint8_t mask = robot_costmap_2d::allowedDirectionMask(dx, dy); + const uint8_t mask = robot_costmap_2d::allowedDirectionMask(dx, dy, max_deviation_rad); return robot_costmap_2d::traverseGridSegment(snap(0.5 + x0 / resolution), snap(0.5 + y0 / resolution), snap(0.5 + x1 / resolution), snap(0.5 + y1 / resolution), [&](int64_t x, int64_t y) { @@ -84,10 +85,13 @@ bool DirectionalEnvironmentNAVXYTHETALAT::buildDirectionTable() return false; } for (const auto& entry : masks) - cells_.push_back({entry.first.first, entry.first.second, entry.second}); + cells.push_back({entry.first.first, entry.first.second, entry.second}); } } - offsets_.push_back(cells_.size()); + offsets.push_back(cells.size()); + cells_.swap(cells); + offsets_.swap(offsets); + table_max_deviation_rad_ = max_deviation_rad; return true; } diff --git a/src/sbpl_lattice_planner.cpp b/src/sbpl_lattice_planner.cpp index 84b8528..69ae790 100644 --- a/src/sbpl_lattice_planner.cpp +++ b/src/sbpl_lattice_planner.cpp @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -356,7 +357,7 @@ 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()); - if (ret) ret = env_->buildDirectionTable(); + if (ret) ret = env_->buildDirectionTable(M_PI / 2.0); current_env_resolution_ = init_grid->getResolution(); current_env_width_ = init_grid->getSizeInCellsX(); current_env_height_ = init_grid->getSizeInCellsY(); @@ -597,6 +598,26 @@ bool SBPLLatticePlanner::makePlan(const robot_geometry_msgs::PoseStamped& start, return false; } last_direction_geometry_log_ = {}; + if (decision.result == DirectionResult::kReady && + direction_snapshot.field->maxDeviationRad() != env_->tableMaxDeviationRad()) { + const double old_deviation = env_->tableMaxDeviationRad(); + const double new_deviation = direction_snapshot.field->maxDeviationRad(); + bool rebuilt = false; + try { + rebuilt = env_->buildDirectionTable(new_deviation); + } catch (const std::exception& e) { + robot::log_error("[sbpl_lattice_planner] direction table rebuild exception: %s", e.what()); + } + if (!rebuilt) { + robot::log_error("[sbpl_lattice_planner] direction table rebuild failed; refusing to plan"); + env_->setDirectionField(nullptr); + direction_scratch_pending_ = true; + return false; + } + robot::log_info("[sbpl_lattice_planner] direction table max deviation changed %.3f -> %.3f deg", + old_deviation * 180.0 / M_PI, new_deviation * 180.0 / M_PI); + direction_scratch_pending_ = true; + } env_->setDirectionField(decision.result == DirectionResult::kReady ? direction_snapshot.field.get() : nullptr); if (direction_scratch_pending_) { planner_->force_planning_from_scratch(); diff --git a/test/direction_environment_test.cpp b/test/direction_environment_test.cpp index a99946d..cc91ee4 100644 --- a/test/direction_environment_test.cpp +++ b/test/direction_environment_test.cpp @@ -32,7 +32,7 @@ robot_nav_msgs::OccupancyGrid grid() return g; } std::shared_ptr makeField(const robot_nav_msgs::OccupancyGrid& g) -{ return DirectionField::create(g, geometry()); } +{ return DirectionField::create(g, geometry(), M_PI / 2.0); } class DirectionEnvironmentTest : public ::testing::Test { @@ -43,7 +43,7 @@ protected: std::vector footprint; ASSERT_TRUE(env.InitializeEnv(kWidth, kHeight, nullptr, 0, 0, 0, 0, 0, 0, 0, 0, 0, footprint, kResolution, 1.0, 1.0, 254, DIRECTION_TEST_MPRIM)); - ASSERT_TRUE(env.buildDirectionTable()); + ASSERT_TRUE(env.buildDirectionTable(M_PI / 2.0)); } EnvNAVXYTHETALATAction_t* action(int theta, int dx, int dy, bool rotation = false) { @@ -67,7 +67,7 @@ TEST_F(DirectionEnvironmentTest, TableMatchesDirectTraversalAtMultipleSources) for (size_t i = 1; i < p.size(); ++i) { const double dx = p[i].x - p[i-1].x, dy = p[i].y - p[i-1].y; if (dx == 0 && dy == 0) continue; - const uint8_t mask = robot_costmap_2d::allowedDirectionMask(dx, dy); + const uint8_t mask = robot_costmap_2d::allowedDirectionMask(dx, dy, M_PI / 2.0); ASSERT_TRUE(robot_costmap_2d::traverseGridSegment(source.first + 0.5 + p[i-1].x/kResolution, source.second + 0.5 + p[i-1].y/kResolution, source.first + 0.5 + p[i].x/kResolution, source.second + 0.5 + p[i].y/kResolution, [&](int64_t x, int64_t y) { @@ -115,12 +115,33 @@ TEST_F(DirectionEnvironmentTest, ActionCostsRespectDisplacementAndObstacles) // A narrower field makes a base-valid action leave the direction grid. auto small_g = grid(); small_g.info.width = 21; small_g.data.assign(21*kHeight, 0); auto small_geo = geometry(); small_geo.width = 21; - auto small = DirectionField::create(small_g, small_geo); ASSERT_TRUE(small); + auto small = DirectionField::create(small_g, small_geo, M_PI / 2.0); ASSERT_TRUE(small); env.setDirectionField(small.get()); EXPECT_GE(env.GetActionCost(20, 15, 0, forward), INFINITECOST); env.setDirectionField(nullptr); } +TEST_F(DirectionEnvironmentTest, RebuildChangesPerpendicularActionCost) +{ + auto* forward = action(0, 1, 0); + ASSERT_TRUE(forward); + auto g = grid(); + std::fill(g.data.begin(), g.data.end(), 3); // +Y zone. + const auto wide = makeField(g); + const auto narrow = DirectionField::create(g, geometry(), M_PI / 4.0); + ASSERT_TRUE(wide && narrow); + env.setDirectionField(wide.get()); + EXPECT_LT(env.GetActionCost(20, 15, 0, forward), INFINITECOST); + ASSERT_TRUE(env.buildDirectionTable(narrow->maxDeviationRad())); + EXPECT_DOUBLE_EQ(env.tableMaxDeviationRad(), M_PI / 4.0); + env.setDirectionField(narrow.get()); + EXPECT_GE(env.GetActionCost(20, 15, 0, forward), INFINITECOST); + EXPECT_FALSE(env.buildDirectionTable(0)); + EXPECT_DOUBLE_EQ(env.tableMaxDeviationRad(), M_PI / 4.0); + EXPECT_GE(env.GetActionCost(20, 15, 0, forward), INFINITECOST); + env.setDirectionField(nullptr); +} + TEST(DirectionDecisionTest, StatesGeometryAndGeneration) { using sbpl_lattice_planner::decideDirection;