- DirectionalLayer rewritten as a DirectionFieldProvider. It no longer derives from StaticLayer and never writes the master grid. It takes an OccupancyGrid of direction codes (0 = no constraint, 1..8 = direction_index + 1) on direction_topic, validates it against the master geometry and global frame, and publishes an immutable DirectionField snapshot with a DISABLED / PENDING / READY state and a generation counter. Every static map on map_topic returns the layer to PENDING until a grid for that map arrives. - direction_field.h is the core API for planners: grid validation, bounds-checked codeAt(), a half-open-cell DDA (traverseGridSegment), allowedDirectionMask() and checkSegment(). LayeredCostmap::getDirectionFieldSnapshot() exposes the snapshot without adding data members. - max_deviation_deg (default 90, valid 0 < x < 180). The field carries the value so every planner uses the same rule; 90 keeps the previous masks bit for bit. - StaticLayer ignores an OccupancyGrid addressed to another layer's topic instead of logging two unthrottled errors per push. - StaticLayer and its subclasses report current only after updateCosts has written the newest map (markMapWritten, also used by CriticalLayer). Readiness flags are written under the master mutex; invalid partial updates are rejected with a throttled error. Full history: branch feature/direction-zone.
368 lines
12 KiB
C++
368 lines
12 KiB
C++
#include <robot_costmap_2d/directional_layer.h>
|
|
#include <robot_costmap_2d/critical_layer.h>
|
|
#include <robot_costmap_2d/static_layer.h>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <condition_variable>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <mutex>
|
|
#include <stdexcept>
|
|
#include <thread>
|
|
|
|
#include <boost/make_shared.hpp>
|
|
#include <boost/thread/locks.hpp>
|
|
#include <gtest/gtest.h>
|
|
#include <tf3/buffer_core.h>
|
|
|
|
using namespace robot_costmap_2d;
|
|
namespace
|
|
{
|
|
class LayerTest : public ::testing::Test
|
|
{
|
|
protected:
|
|
std::string directory_;
|
|
std::string old_env_;
|
|
bool had_env_ = false;
|
|
tf3::BufferCore tf_{tf3::Duration(10.0)};
|
|
|
|
void SetUp() override
|
|
{
|
|
const char* old = std::getenv("PNKX_NAV_CORE_CONFIG_DIR");
|
|
if (old)
|
|
{
|
|
had_env_ = true;
|
|
old_env_ = old;
|
|
}
|
|
char pattern[] = "/tmp/direction-layer-test-XXXXXX";
|
|
char* path = mkdtemp(pattern);
|
|
ASSERT_NE(path, nullptr);
|
|
directory_ = path;
|
|
std::filesystem::create_directory(directory_ + "/config");
|
|
ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", directory_.c_str(), 1), 0);
|
|
config(true);
|
|
std::ofstream static_config(directory_ + "/config/static_layer_params.yaml");
|
|
static_config << "static_layer:\n enabled: true\n map_topic: map\n"
|
|
" first_map_only: false\n subscribe_to_updates: true\n";
|
|
}
|
|
|
|
void TearDown() override
|
|
{
|
|
if (had_env_)
|
|
setenv("PNKX_NAV_CORE_CONFIG_DIR", old_env_.c_str(), 1);
|
|
else
|
|
unsetenv("PNKX_NAV_CORE_CONFIG_DIR");
|
|
std::filesystem::remove_all(directory_);
|
|
}
|
|
|
|
void config(bool enabled, double max_deviation_deg = 90.0)
|
|
{
|
|
std::ofstream out(directory_ + "/config/directional_layer_params.yaml");
|
|
out << "directional_layer:\n enabled: " << (enabled ? "true" : "false")
|
|
<< "\n map_topic: map\n direction_topic: direction_map\n max_deviation_deg: " << max_deviation_deg
|
|
<< "\n debug_state: false\n";
|
|
}
|
|
|
|
robot_nav_msgs::OccupancyGrid grid()
|
|
{
|
|
robot_nav_msgs::OccupancyGrid g;
|
|
g.header.frame_id = "map";
|
|
g.info.width = 4;
|
|
g.info.height = 4;
|
|
g.info.resolution = 1;
|
|
g.data.assign(16, 0);
|
|
return g;
|
|
}
|
|
};
|
|
|
|
TEST_F(LayerTest, StaticAndCriticalReadinessTracksWrites)
|
|
{
|
|
for (bool critical : {false, true})
|
|
{
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
boost::shared_ptr<Layer> layer = critical
|
|
? boost::static_pointer_cast<Layer>(boost::make_shared<CriticalLayer>())
|
|
: boost::static_pointer_cast<Layer>(boost::make_shared<StaticLayer>());
|
|
layer->initialize(&master, critical ? "critical" : "static", &tf_);
|
|
master.addPlugin(layer);
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
EXPECT_FALSE(master.isCurrent());
|
|
|
|
robot_map_msgs::OccupancyGridUpdate early_update;
|
|
early_update.x = early_update.y = 0;
|
|
early_update.width = early_update.height = 1;
|
|
early_update.data.assign(1, 100);
|
|
layer->dataCallBack(early_update, "map_updates");
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
|
|
auto map = grid();
|
|
map.data[0] = 100;
|
|
layer->dataCallBack(map, "map");
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
layer->updateCosts(*master.getCostmap(), 0, 0, 0, 0);
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
master.updateMap(0, 0, 0);
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
|
|
layer->reset();
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
master.updateMap(0, 0, 0);
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
EXPECT_TRUE(master.isCurrent());
|
|
EXPECT_NE(master.getCostmap()->getCost(0, 0), FREE_SPACE);
|
|
|
|
map.data[1] = 100;
|
|
map.data[15] = 100;
|
|
layer->dataCallBack(map, "map");
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
|
|
robot_map_msgs::OccupancyGridUpdate update;
|
|
update.x = 0;
|
|
update.y = 0;
|
|
update.width = 1;
|
|
update.height = 1;
|
|
update.data.assign(1, 100);
|
|
layer->dataCallBack(update, "map_updates");
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
master.updateMap(0, 0, 0);
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
EXPECT_EQ(master.getCostmap()->getCost(3, 3), critical ? CRITICAL_SPACE : LETHAL_OBSTACLE);
|
|
|
|
update.x = 0;
|
|
update.y = 3;
|
|
layer->dataCallBack(update, "map_updates");
|
|
update.x = 3;
|
|
update.y = 0;
|
|
layer->dataCallBack(update, "map_updates");
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
master.updateMap(0, 0, 0);
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
EXPECT_EQ(master.getCostmap()->getCost(0, 3), critical ? CRITICAL_SPACE : LETHAL_OBSTACLE);
|
|
EXPECT_EQ(master.getCostmap()->getCost(3, 0), critical ? CRITICAL_SPACE : LETHAL_OBSTACLE);
|
|
|
|
update.x = -1;
|
|
layer->dataCallBack(update, "map_updates");
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
layer->dataCallBack(map, "map");
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
master.updateMap(0, 0, 0);
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
}
|
|
}
|
|
|
|
TEST_F(LayerTest, InvalidMaxDeviationThrows)
|
|
{
|
|
for (double degrees : {0.0, 180.0, -5.0, 200.0})
|
|
{
|
|
config(true, degrees);
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
DirectionalLayer layer;
|
|
EXPECT_THROW(layer.initialize(&master, "directional", &tf_), std::runtime_error) << degrees;
|
|
}
|
|
}
|
|
|
|
TEST_F(LayerTest, ReadyFieldCarriesConfiguredDeviation)
|
|
{
|
|
config(true, 45.0);
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
DirectionalLayer layer;
|
|
ASSERT_NO_THROW(layer.initialize(&master, "directional", &tf_));
|
|
auto g = grid();
|
|
layer.dataCallBack(g, "map");
|
|
layer.dataCallBack(g, "direction_map");
|
|
const auto snapshot = layer.directionSnapshot();
|
|
ASSERT_EQ(snapshot.state, DirectionFieldState::kReady);
|
|
ASSERT_TRUE(snapshot.field);
|
|
EXPECT_DOUBLE_EQ(snapshot.field->maxDeviationRad(), M_PI / 4.0);
|
|
}
|
|
|
|
TEST_F(LayerTest, LifecycleAndProvider)
|
|
{
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
EXPECT_EQ(master.getDirectionFieldSnapshot().state, DirectionFieldState::kDisabled);
|
|
auto layer = boost::make_shared<DirectionalLayer>();
|
|
layer->initialize(&master, "directional", &tf_);
|
|
master.addPlugin(layer);
|
|
auto g = grid();
|
|
auto s = master.getDirectionFieldSnapshot();
|
|
EXPECT_EQ(s.state, DirectionFieldState::kPending);
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
layer->dataCallBack(g, "direction_map");
|
|
EXPECT_EQ(layer->directionSnapshot().generation, s.generation);
|
|
double min_x = 1;
|
|
double min_y = 2;
|
|
double max_x = 3;
|
|
double max_y = 4;
|
|
layer->updateBounds(0, 0, 0, &min_x, &min_y, &max_x, &max_y);
|
|
EXPECT_DOUBLE_EQ(min_x, 1);
|
|
EXPECT_DOUBLE_EQ(min_y, 2);
|
|
EXPECT_DOUBLE_EQ(max_x, 3);
|
|
EXPECT_DOUBLE_EQ(max_y, 4);
|
|
auto& costs = *master.getCostmap();
|
|
costs.setCost(0, 0, 42);
|
|
layer->updateCosts(costs, 0, 0, 4, 4);
|
|
EXPECT_EQ(costs.getCost(0, 0), 42);
|
|
layer->dataCallBack(7, "map");
|
|
layer->dataCallBack(g, "other_topic");
|
|
EXPECT_EQ(layer->directionSnapshot().generation, s.generation);
|
|
layer->dataCallBack(g, "/map");
|
|
s = layer->directionSnapshot();
|
|
EXPECT_EQ(s.state, DirectionFieldState::kPending);
|
|
g.data[0] = 1;
|
|
layer->dataCallBack(g, "/direction_map");
|
|
auto ready = master.getDirectionFieldSnapshot();
|
|
EXPECT_EQ(ready.state, DirectionFieldState::kReady);
|
|
ASSERT_TRUE(ready.field);
|
|
EXPECT_TRUE(layer->isCurrent());
|
|
EXPECT_GT(ready.generation, s.generation);
|
|
g.data[0] = 100;
|
|
layer->dataCallBack(g, "direction_map");
|
|
s = layer->directionSnapshot();
|
|
EXPECT_EQ(s.generation, ready.generation);
|
|
EXPECT_EQ(s.field, ready.field);
|
|
layer->reset();
|
|
EXPECT_EQ(layer->directionSnapshot().field, ready.field);
|
|
g.data.clear();
|
|
layer->dataCallBack(g, "direction_map");
|
|
s = layer->directionSnapshot();
|
|
ASSERT_TRUE(s.field);
|
|
EXPECT_FALSE(s.field->hasConstraints());
|
|
EXPECT_GT(s.generation, ready.generation);
|
|
layer->dataCallBack(g, "map");
|
|
s = layer->directionSnapshot();
|
|
EXPECT_EQ(s.state, DirectionFieldState::kPending);
|
|
EXPECT_FALSE(s.field);
|
|
EXPECT_FALSE(layer->isCurrent());
|
|
layer->dataCallBack(g, "direction_map");
|
|
layer->matchSize();
|
|
EXPECT_EQ(layer->directionSnapshot().state, DirectionFieldState::kPending);
|
|
layer->dataCallBack(g, "direction_map");
|
|
master.getCostmap()->resizeMap(3, 3, 1, 0, 0);
|
|
layer->reset();
|
|
EXPECT_EQ(layer->directionSnapshot().state, DirectionFieldState::kPending);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
layer->dataCallBack(g, "direction_map");
|
|
master.resizeMap(3, 3, 1, 0, 0);
|
|
EXPECT_EQ(layer->directionSnapshot().state, DirectionFieldState::kPending);
|
|
}
|
|
|
|
TEST_F(LayerTest, DisabledAndMultipleProviders)
|
|
{
|
|
config(false);
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
auto disabled = boost::make_shared<DirectionalLayer>();
|
|
disabled->initialize(&master, "directional", &tf_);
|
|
master.addPlugin(disabled);
|
|
EXPECT_TRUE(disabled->isCurrent());
|
|
EXPECT_EQ(master.getDirectionFieldSnapshot().state, DirectionFieldState::kDisabled);
|
|
config(true);
|
|
auto a = boost::make_shared<DirectionalLayer>();
|
|
a->initialize(&master, "first", &tf_);
|
|
master.addPlugin(a);
|
|
auto b = boost::make_shared<DirectionalLayer>();
|
|
b->initialize(&master, "second", &tf_);
|
|
master.addPlugin(b);
|
|
EXPECT_EQ(master.getDirectionFieldSnapshot().state, DirectionFieldState::kPending);
|
|
}
|
|
|
|
TEST_F(LayerTest, RollingCostmapStaysPending)
|
|
{
|
|
LayeredCostmap master("map", true, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
DirectionalLayer layer;
|
|
layer.initialize(&master, "directional", &tf_);
|
|
auto g = grid();
|
|
layer.dataCallBack(g, "map");
|
|
layer.dataCallBack(g, "direction_map");
|
|
EXPECT_EQ(layer.directionSnapshot().state, DirectionFieldState::kPending);
|
|
EXPECT_FALSE(layer.isCurrent());
|
|
}
|
|
|
|
TEST_F(LayerTest, StaticMapCallbackDoesNotWaitForMasterMutex)
|
|
{
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
DirectionalLayer layer;
|
|
layer.initialize(&master, "directional", &tf_);
|
|
auto g = grid();
|
|
std::mutex completion_mutex;
|
|
std::condition_variable completion;
|
|
bool done = false;
|
|
bool completed_while_locked = false;
|
|
std::thread callback;
|
|
{
|
|
boost::unique_lock<Costmap2D::mutex_t> master_lock(*master.getCostmap()->getMutex());
|
|
callback = std::thread([&]
|
|
{
|
|
layer.dataCallBack(g, "map");
|
|
{
|
|
std::lock_guard<std::mutex> lock(completion_mutex);
|
|
done = true;
|
|
}
|
|
completion.notify_one();
|
|
});
|
|
std::unique_lock<std::mutex> lock(completion_mutex);
|
|
completed_while_locked = completion.wait_for(lock, std::chrono::seconds(2), [&] { return done; });
|
|
}
|
|
callback.join();
|
|
EXPECT_TRUE(completed_while_locked);
|
|
EXPECT_EQ(layer.directionSnapshot().state, DirectionFieldState::kPending);
|
|
}
|
|
|
|
TEST_F(LayerTest, OldSnapshotSurvivesConcurrentReplacement)
|
|
{
|
|
LayeredCostmap master("map", false, false);
|
|
master.resizeMap(4, 4, 1, 0, 0);
|
|
auto layer = boost::make_shared<DirectionalLayer>();
|
|
layer->initialize(&master, "directional", &tf_);
|
|
master.addPlugin(layer);
|
|
auto g = grid();
|
|
g.data[0] = 1;
|
|
layer->dataCallBack(g, "map");
|
|
layer->dataCallBack(g, "direction_map");
|
|
auto old = master.getDirectionFieldSnapshot();
|
|
ASSERT_TRUE(old.field);
|
|
std::atomic<bool> start{false};
|
|
std::atomic<bool> ok{true};
|
|
std::thread reader([&]
|
|
{
|
|
while (!start.load())
|
|
{
|
|
}
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
auto s = master.getDirectionFieldSnapshot();
|
|
if (!s.field || old.field->code(0, 0) != 1)
|
|
ok = false;
|
|
}
|
|
});
|
|
std::thread updater([&]
|
|
{
|
|
while (!start.load())
|
|
{
|
|
}
|
|
for (int i = 0; i < 1000; ++i)
|
|
layer->updateBounds(0, 0, 0, nullptr, nullptr, nullptr, nullptr);
|
|
});
|
|
start = true;
|
|
g.data[0] = 3;
|
|
layer->dataCallBack(g, "direction_map");
|
|
reader.join();
|
|
updater.join();
|
|
EXPECT_TRUE(ok.load());
|
|
EXPECT_EQ(old.field->code(0, 0), 1);
|
|
EXPECT_EQ(master.getDirectionFieldSnapshot().field->code(0, 0), 3);
|
|
}
|
|
} // namespace
|