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.
This commit is contained in:
2026-11-04 08:52:00 +07:00
parent 47dd76f39e
commit 471d13410c
4 changed files with 68 additions and 15 deletions

View File

@@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdint>
#include <limits>
#include <vector>
#include <robot_costmap_2d/direction_field.h>
@@ -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<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

@@ -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<CellMask> cells;
std::vector<size_t> 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<std::pair<int64_t, int64_t>, 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;
}

View File

@@ -39,6 +39,7 @@
#include <algorithm>
#include <cmath>
#include <exception>
#include <string>
#include <cstdlib>
#include <dlfcn.h>
@@ -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();

View File

@@ -32,7 +32,7 @@ robot_nav_msgs::OccupancyGrid grid()
return g;
}
std::shared_ptr<const DirectionField> 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<sbpl_2Dpt_t> 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;