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.
216 lines
9.9 KiB
C++
216 lines
9.9 KiB
C++
#include <sbpl_lattice_planner/directional_environment.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <sbpl/planners/araplanner.h>
|
|
#include <sbpl/utils/key.h>
|
|
|
|
namespace
|
|
{
|
|
using sbpl_lattice_planner::DirectionalEnvironmentNAVXYTHETALAT;
|
|
using robot_costmap_2d::DirectionField;
|
|
using robot_costmap_2d::DirectionFieldSnapshot;
|
|
using robot_costmap_2d::DirectionFieldState;
|
|
using robot_costmap_2d::DirectionGeometry;
|
|
constexpr double kResolution = 0.125;
|
|
constexpr int kWidth = 50, kHeight = 35;
|
|
using Masks = std::map<std::pair<int64_t, int64_t>, uint8_t>;
|
|
|
|
DirectionGeometry geometry() { return {kWidth, kHeight, kResolution, 0, 0, "map"}; }
|
|
robot_nav_msgs::OccupancyGrid grid()
|
|
{
|
|
robot_nav_msgs::OccupancyGrid g;
|
|
g.header.frame_id = "map";
|
|
g.info.width = kWidth; g.info.height = kHeight; g.info.resolution = kResolution;
|
|
g.data.assign(kWidth * kHeight, 0);
|
|
return g;
|
|
}
|
|
std::shared_ptr<const DirectionField> makeField(const robot_nav_msgs::OccupancyGrid& g)
|
|
{ return DirectionField::create(g, geometry(), M_PI / 2.0); }
|
|
|
|
class DirectionEnvironmentTest : public ::testing::Test
|
|
{
|
|
protected:
|
|
DirectionalEnvironmentNAVXYTHETALAT env;
|
|
void SetUp() override
|
|
{
|
|
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(M_PI / 2.0));
|
|
}
|
|
EnvNAVXYTHETALATAction_t* action(int theta, int dx, int dy, bool rotation = false)
|
|
{
|
|
for (int i = 0; i < env.actionWidth(); ++i) {
|
|
auto& a = const_cast<EnvNAVXYTHETALATAction_t&>(env.action(theta, i));
|
|
if (a.dX == dx && a.dY == dy && (!rotation || a.endtheta != a.starttheta)) return &a;
|
|
}
|
|
return nullptr;
|
|
}
|
|
};
|
|
|
|
// Coordinates in this rescaled copy are exact multiples of 1/4096 cell, so translating
|
|
// to large source indices does not introduce new binary rounding at grid boundaries.
|
|
TEST_F(DirectionEnvironmentTest, TableMatchesDirectTraversalAtMultipleSources)
|
|
{
|
|
for (int theta = 0; theta < env.thetaCount(); ++theta) {
|
|
for (int a = 0; a < env.actionWidth(); ++a) {
|
|
const auto& p = env.action(theta, a).intermptV;
|
|
for (const auto& source : {std::make_pair(0, 0), std::make_pair(37, 19), std::make_pair(1000000, 800000)}) {
|
|
Masks reference, actual;
|
|
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, 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) {
|
|
auto entry = reference.emplace(std::make_pair(x-source.first, y-source.second), mask);
|
|
if (!entry.second) entry.first->second &= mask;
|
|
return true;
|
|
}));
|
|
}
|
|
const size_t index = theta * env.actionWidth() + a;
|
|
for (size_t i = env.offsets()[index]; i < env.offsets()[index+1]; ++i) {
|
|
const auto& c = env.cells()[i]; actual[{c.dx, c.dy}] = c.mask;
|
|
}
|
|
EXPECT_EQ(reference, actual) << theta << "," << a << " source " << source.first;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_F(DirectionEnvironmentTest, ActionCostsRespectDisplacementAndObstacles)
|
|
{
|
|
auto* forward = action(0, 1, 0); auto* reverse = action(0, -1, 0); auto* turn = action(0, 0, 0, true);
|
|
auto* long_forward = action(0, 8, 0);
|
|
ASSERT_TRUE(forward && reverse && turn && long_forward);
|
|
const int base = env.GetActionCost(20, 15, 0, forward);
|
|
ASSERT_LT(base, INFINITECOST);
|
|
auto g = grid();
|
|
std::fill(g.data.begin(), g.data.end(), 1); // east
|
|
auto east = makeField(g); ASSERT_TRUE(east); env.setDirectionField(east.get());
|
|
EXPECT_EQ(base, env.GetActionCost(20, 15, 0, forward));
|
|
EXPECT_GE(env.GetActionCost(20, 15, 0, reverse), INFINITECOST);
|
|
EXPECT_LT(env.GetActionCost(20, 15, 0, turn), INFINITECOST);
|
|
std::fill(g.data.begin(), g.data.end(), 5); // west
|
|
auto west = makeField(g); ASSERT_TRUE(west); env.setDirectionField(west.get());
|
|
EXPECT_GE(env.GetActionCost(20, 15, 0, forward), INFINITECOST);
|
|
EXPECT_LT(env.GetActionCost(20, 15, 0, reverse), INFINITECOST);
|
|
std::fill(g.data.begin(), g.data.end(), 0);
|
|
g.data[15*kWidth + 24] = 5; // Neither endpoint is in this clipped cell.
|
|
auto clip = makeField(g); ASSERT_TRUE(clip); env.setDirectionField(clip.get());
|
|
EXPECT_GE(env.GetActionCost(20, 15, 0, long_forward), INFINITECOST);
|
|
env.UpdateCost(21, 15, 254);
|
|
EXPECT_GE(env.GetActionCost(20, 15, 0, forward), INFINITECOST);
|
|
env.UpdateCost(21, 15, 0);
|
|
env.setDirectionField(nullptr);
|
|
EXPECT_EQ(base, env.GetActionCost(20, 15, 0, forward));
|
|
// 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, 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;
|
|
using R = sbpl_lattice_planner::DirectionDecision::Result;
|
|
DirectionFieldSnapshot s;
|
|
EXPECT_EQ(decideDirection(s, true, s.state, 0, geometry()).result, R::kDisabled);
|
|
s.state = DirectionFieldState::kPending;
|
|
EXPECT_EQ(decideDirection(s, true, DirectionFieldState::kDisabled, 0, geometry()).result, R::kPending);
|
|
s.state = DirectionFieldState::kReady; s.field = makeField(grid()); s.generation = 2;
|
|
ASSERT_TRUE(s.field);
|
|
auto different = geometry(); different.origin_x = 0.01;
|
|
EXPECT_EQ(decideDirection(s, true, s.state, 1, different).result, R::kGeometryMismatch);
|
|
for (int axis = 0; axis < 4; ++axis) {
|
|
different = geometry();
|
|
if (axis == 0) ++different.width;
|
|
if (axis == 1) ++different.height;
|
|
if (axis == 2) different.resolution += 0.01;
|
|
if (axis == 3) different.origin_y += 0.01;
|
|
EXPECT_EQ(decideDirection(s, true, s.state, 1, different).result, R::kGeometryMismatch);
|
|
}
|
|
EXPECT_TRUE(decideDirection(s, true, DirectionFieldState::kPending, 2, geometry()).force_scratch);
|
|
EXPECT_TRUE(decideDirection(s, true, s.state, 1, geometry()).force_scratch);
|
|
EXPECT_FALSE(decideDirection(s, true, s.state, 2, geometry()).force_scratch);
|
|
EXPECT_EQ(decideDirection(s, true, s.state, 2, geometry()).result, R::kReady);
|
|
}
|
|
|
|
TEST_F(DirectionEnvironmentTest, SearchDetoursAndReplansForBothSearchDirections)
|
|
{
|
|
// The near passage permits eastward travel only; the far passage remains unconstrained.
|
|
for (int x = 22; x <= 26; ++x)
|
|
for (int y = 0; y < kHeight; ++y)
|
|
if (!((y >= 6 && y <= 10) || (y >= 24 && y <= 28))) env.UpdateCost(x, y, 254);
|
|
auto g = grid();
|
|
for (int x = 20; x <= 28; ++x)
|
|
for (int y = 4; y <= 12; ++y) g.data[y*kWidth+x] = 1;
|
|
auto east = makeField(g); ASSERT_TRUE(east);
|
|
for (bool forward_search : {true, false}) {
|
|
// SBPL planners store their state index in the environment and clear it only in
|
|
// force_planning_from_scratch(), not in the destructor. A second planner on the same
|
|
// environment would read the first planner's indices into its own empty state array.
|
|
for (int* row : env.StateID2IndexMapping)
|
|
std::fill(row, row + NUMOFINDICES_STATEID2IND, -1);
|
|
ARAPlanner planner(&env, forward_search);
|
|
planner.set_initialsolution_eps(1.0);
|
|
planner.set_search_mode(true);
|
|
const int start = env.SetStart(38.5*kResolution, 8.5*kResolution, 0);
|
|
const int goal = env.SetGoal(10.5*kResolution, 8.5*kResolution, 0);
|
|
ASSERT_GE(start, 0); ASSERT_GE(goal, 0);
|
|
ASSERT_EQ(planner.set_start(start), 1); ASSERT_EQ(planner.set_goal(goal), 1);
|
|
auto solve = [&]() {
|
|
std::vector<int> states; std::vector<EnvNAVXYTHETALAT3Dpt_t> path;
|
|
const int ok = planner.replan(10.0, &states);
|
|
if (ok) env.ConvertStateIDPathintoXYThetaPath(&states, &path);
|
|
return path;
|
|
};
|
|
env.setDirectionField(nullptr);
|
|
const auto direct = solve(); ASSERT_FALSE(direct.empty());
|
|
EXPECT_LT(std::max_element(direct.begin(), direct.end(), [](const auto& a, const auto& b) {
|
|
return a.y < b.y; })->y, 20*kResolution);
|
|
env.setDirectionField(east.get()); planner.force_planning_from_scratch();
|
|
const auto detour = solve(); ASSERT_FALSE(detour.empty());
|
|
EXPECT_GT(std::max_element(detour.begin(), detour.end(), [](const auto& a, const auto& b) {
|
|
return a.y < b.y; })->y, 20*kResolution);
|
|
for (int x = 22; x <= 26; ++x) for (int y = 24; y <= 28; ++y) env.UpdateCost(x, y, 254);
|
|
planner.force_planning_from_scratch();
|
|
EXPECT_TRUE(solve().empty());
|
|
for (int x = 22; x <= 26; ++x) for (int y = 24; y <= 28; ++y) env.UpdateCost(x, y, 0);
|
|
env.setDirectionField(nullptr);
|
|
}
|
|
}
|
|
} // namespace
|