feat(costmap_2d): background reference graph, shared keys under root:
- ReferenceGraph / ReferenceGraphWorker: Voronoi ridges and one-way lanes are computed on a low-priority thread when the map changes (reference_graph_* keys, off with rolling_window), with unit tests. - Shared costmap keys can live in a root: map or flat at the top of the YAML; costmap_common_root_test covers both layouts. - Static and directional layers report map changes to the layered costmap.
This commit is contained in:
670
test/reference_graph_worker_test.cpp
Normal file
670
test/reference_graph_worker_test.cpp
Normal file
@@ -0,0 +1,670 @@
|
||||
#include <robot_costmap_2d/reference_graph_worker.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/make_shared.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <robot_costmap_2d/cost_values.h>
|
||||
#include <robot_costmap_2d/costmap_layer.h>
|
||||
#include <robot_costmap_2d/layered_costmap.h>
|
||||
|
||||
using namespace robot_costmap_2d;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
constexpr double kResolution = 0.05;
|
||||
|
||||
bool waitUntil(const std::function<bool()>& condition, double seconds = 60.0)
|
||||
{
|
||||
const Clock::time_point end = Clock::now() + std::chrono::duration_cast<Clock::duration>(std::chrono::duration<double>(seconds));
|
||||
while (Clock::now() < end)
|
||||
{
|
||||
if (condition())
|
||||
return true;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
return condition();
|
||||
}
|
||||
|
||||
DirectionGeometry geometryOf(uint32_t width, uint32_t height) { return { width, height, kResolution, 0.0, 0.0, "map" }; }
|
||||
|
||||
/** Free map with a lethal border, so its ridge runs along the middle. */
|
||||
std::vector<uint8_t> corridor(uint32_t width, uint32_t height)
|
||||
{
|
||||
std::vector<uint8_t> cost(static_cast<std::size_t>(width) * height, FREE_SPACE);
|
||||
for (uint32_t x = 0; x < width; ++x)
|
||||
cost[x] = cost[static_cast<std::size_t>(height - 1) * width + x] = LETHAL_OBSTACLE;
|
||||
for (uint32_t y = 0; y < height; ++y)
|
||||
cost[static_cast<std::size_t>(y) * width] = cost[static_cast<std::size_t>(y) * width + width - 1] = LETHAL_OBSTACLE;
|
||||
return cost;
|
||||
}
|
||||
|
||||
std::vector<robot_geometry_msgs::Point> rectangle(double half_x, double half_y)
|
||||
{
|
||||
std::vector<robot_geometry_msgs::Point> footprint(4);
|
||||
const double xs[4] = { half_x, -half_x, -half_x, half_x };
|
||||
const double ys[4] = { half_y, half_y, -half_y, -half_y };
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
footprint[static_cast<std::size_t>(i)].x = xs[i];
|
||||
footprint[static_cast<std::size_t>(i)].y = ys[i];
|
||||
}
|
||||
return footprint;
|
||||
}
|
||||
|
||||
/** Zone with direction +X over a band in the middle of the map. */
|
||||
DirectionFieldSnapshot bandZone(uint32_t width, uint32_t height, uint64_t generation)
|
||||
{
|
||||
robot_nav_msgs::OccupancyGrid grid;
|
||||
grid.header.frame_id = "map";
|
||||
grid.info.width = width;
|
||||
grid.info.height = height;
|
||||
grid.info.resolution = kResolution;
|
||||
grid.data.assign(static_cast<std::size_t>(width) * height, 0);
|
||||
for (uint32_t y = height / 3; y < 2 * height / 3; ++y)
|
||||
for (uint32_t x = width / 4; x < 3 * width / 4; ++x)
|
||||
grid.data[static_cast<std::size_t>(y) * width + x] = 1;
|
||||
DirectionFieldSnapshot snapshot;
|
||||
snapshot.state = DirectionFieldState::kReady;
|
||||
snapshot.field = DirectionField::create(grid, geometryOf(width, height), 1.5);
|
||||
snapshot.generation = generation;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
ReferenceGraphParams quietParams()
|
||||
{
|
||||
ReferenceGraphParams params;
|
||||
params.debug_log = false;
|
||||
return params;
|
||||
}
|
||||
|
||||
/** A costmap stand-in the test controls from its own thread. */
|
||||
class FakeCostmap
|
||||
{
|
||||
public:
|
||||
FakeCostmap(uint32_t width, uint32_t height) : geometry_(geometryOf(width, height)), grid_(corridor(width, height)), footprint_(rectangle(0.3, 0.3)) {}
|
||||
|
||||
ReferenceGraphWorker::Source source()
|
||||
{
|
||||
ReferenceGraphWorker::Source out;
|
||||
out.capture = [this](bool with_grid, ReferenceGraphInput* input) {
|
||||
++captures;
|
||||
if (on_capture)
|
||||
on_capture();
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (status_ != ReferenceCaptureStatus::kReady)
|
||||
return status_;
|
||||
input->geometry = geometry_;
|
||||
input->footprint = footprint_;
|
||||
if (with_grid)
|
||||
{
|
||||
++grid_captures;
|
||||
input->static_layers.push_back(grid_);
|
||||
}
|
||||
return ReferenceCaptureStatus::kReady;
|
||||
};
|
||||
out.direction = [this]() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return direction_;
|
||||
};
|
||||
return out;
|
||||
}
|
||||
|
||||
void setGrid(std::vector<uint8_t> grid)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
grid_ = std::move(grid);
|
||||
}
|
||||
void setStatus(ReferenceCaptureStatus status)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
status_ = status;
|
||||
}
|
||||
void setDirection(DirectionFieldSnapshot direction)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
direction_ = std::move(direction);
|
||||
}
|
||||
|
||||
std::atomic<int> captures{ 0 };
|
||||
std::atomic<int> grid_captures{ 0 };
|
||||
std::function<void()> on_capture; ///< Set before the worker starts.
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
DirectionGeometry geometry_;
|
||||
std::vector<uint8_t> grid_;
|
||||
std::vector<robot_geometry_msgs::Point> footprint_;
|
||||
ReferenceCaptureStatus status_ = ReferenceCaptureStatus::kReady;
|
||||
DirectionFieldSnapshot direction_;
|
||||
};
|
||||
|
||||
bool ready(const ReferenceGraphWorker& worker) { return worker.snapshot().state == ReferenceGraphState::kReady; }
|
||||
|
||||
/** Holds the worker inside a capture until the test lets it go. */
|
||||
class Gate
|
||||
{
|
||||
public:
|
||||
void hold()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
reached_ = true;
|
||||
changed_.notify_all();
|
||||
changed_.wait(lock, [this]() { return open_; });
|
||||
}
|
||||
void waitReached()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
changed_.wait(lock, [this]() { return reached_; });
|
||||
}
|
||||
void open()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
open_ = true;
|
||||
changed_.notify_all();
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable changed_;
|
||||
bool reached_ = false;
|
||||
bool open_ = false;
|
||||
};
|
||||
|
||||
class FakeLayer : public CostmapLayer
|
||||
{
|
||||
public:
|
||||
explicit FakeLayer(LayerType type, bool current = true) : type_(type)
|
||||
{
|
||||
enabled_ = true;
|
||||
current_ = current;
|
||||
}
|
||||
LayerType getType() const override { return type_; }
|
||||
void setCurrent(bool current) { current_ = current; }
|
||||
void write(const std::vector<uint8_t>& cost) { std::copy(cost.begin(), cost.end(), getCharMap()); }
|
||||
|
||||
protected:
|
||||
void handleImpl(const void*, const std::type_info&, const std::string&) override {}
|
||||
|
||||
private:
|
||||
LayerType type_;
|
||||
};
|
||||
|
||||
class FakeDirectionLayer : public Layer, public DirectionFieldProvider
|
||||
{
|
||||
public:
|
||||
FakeDirectionLayer() { enabled_ = true; current_ = true; }
|
||||
LayerType getType() const override { return LayerType::DIRECTIONAL_LAYER; }
|
||||
DirectionFieldSnapshot directionSnapshot() const override
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return snapshot_;
|
||||
}
|
||||
void publish(DirectionFieldSnapshot snapshot)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
snapshot_ = std::move(snapshot);
|
||||
layered_costmap_->notifyDirectionFieldChanged();
|
||||
}
|
||||
|
||||
protected:
|
||||
void handleImpl(const void*, const std::type_info&, const std::string&) override {}
|
||||
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
DirectionFieldSnapshot snapshot_;
|
||||
};
|
||||
|
||||
/** A LayeredCostmap with one static layer that already holds a map. */
|
||||
struct Costmap
|
||||
{
|
||||
Costmap(uint32_t width, uint32_t height) : layered("map", false, true), layer(boost::make_shared<FakeLayer>(LayerType::STATIC_LAYER))
|
||||
{
|
||||
layer->initialize(&layered, "static", nullptr);
|
||||
layered.addPlugin(layer);
|
||||
layered.resizeMap(width, height, kResolution, 0.0, 0.0);
|
||||
layer->write(corridor(width, height));
|
||||
layered.setFootprint(rectangle(0.3, 0.3));
|
||||
}
|
||||
bool ready() const { return layered.getReferenceGraphSnapshot().state == ReferenceGraphState::kReady; }
|
||||
|
||||
LayeredCostmap layered;
|
||||
boost::shared_ptr<FakeLayer> layer;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(MergeStaticLayers, LethalWinsAndUnknownNeedsEveryLayer)
|
||||
{
|
||||
const std::vector<uint8_t> merged = mergeStaticLayers({ { FREE_SPACE, LETHAL_OBSTACLE, NO_INFORMATION, NO_INFORMATION, INSCRIBED_INFLATED_OBSTACLE },
|
||||
{ LETHAL_OBSTACLE, FREE_SPACE, FREE_SPACE, NO_INFORMATION, NO_INFORMATION } });
|
||||
EXPECT_EQ(merged, (std::vector<uint8_t>{ LETHAL_OBSTACLE, LETHAL_OBSTACLE, FREE_SPACE, NO_INFORMATION, FREE_SPACE }));
|
||||
EXPECT_TRUE(mergeStaticLayers({}).empty());
|
||||
EXPECT_THROW(mergeStaticLayers({ { FREE_SPACE }, { FREE_SPACE, FREE_SPACE } }), std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, BadInputIsRefused)
|
||||
{
|
||||
FakeCostmap costmap(40, 40);
|
||||
ReferenceGraphParams bad = quietParams();
|
||||
bad.spur_min_length_m = -1.0;
|
||||
EXPECT_THROW(ReferenceGraphWorker(costmap.source(), bad), std::invalid_argument);
|
||||
EXPECT_THROW(ReferenceGraphWorker(ReferenceGraphWorker::Source{}, quietParams()), std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, GoesFromPendingToReadyAndSleeps)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
Gate gate;
|
||||
costmap.on_capture = [&gate]() { gate.hold(); };
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
gate.waitReached();
|
||||
const ReferenceGraphSnapshot pending = worker.snapshot();
|
||||
EXPECT_EQ(pending.state, ReferenceGraphState::kPending);
|
||||
EXPECT_EQ(pending.graph, nullptr);
|
||||
gate.open();
|
||||
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
const ReferenceGraphSnapshot done = worker.snapshot();
|
||||
ASSERT_NE(done.graph, nullptr);
|
||||
EXPECT_NE(done.generation, pending.generation);
|
||||
EXPECT_FALSE(done.graph->edges.empty());
|
||||
EXPECT_EQ(done.graph->static_cost, mergeStaticLayers({ corridor(120, 60) }));
|
||||
EXPECT_EQ(done.graph->clearance_mm.size(), 120U * 60U);
|
||||
EXPECT_EQ(done.graph->geometry.frame_id, "map");
|
||||
|
||||
// A ready worker does no work at all, so it costs nothing on a 2-core robot.
|
||||
const int captures = costmap.captures;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(450));
|
||||
EXPECT_EQ(costmap.captures, captures);
|
||||
EXPECT_EQ(worker.snapshot().generation, done.generation);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, RetainedSnapshotSurvivesTheNextChange)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
const ReferenceGraphSnapshot held = worker.snapshot();
|
||||
costmap.setGrid(corridor(120, 60));
|
||||
worker.notifyStaticChanged();
|
||||
EXPECT_EQ(worker.snapshot().graph == held.graph, false);
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
EXPECT_FALSE(held.graph->static_cost.empty());
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, WaitsForTheStaticLayer)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
costmap.setStatus(ReferenceCaptureStatus::kWait);
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
EXPECT_EQ(worker.stats().step1_runs, 0U);
|
||||
costmap.setStatus(ReferenceCaptureStatus::kReady);
|
||||
EXPECT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, NoStaticLayerStaysPending)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
costmap.setStatus(ReferenceCaptureStatus::kNoStaticLayer);
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, WaitsForTheDirectionGridThenRunsOnlyStep2)
|
||||
{
|
||||
FakeCostmap costmap(200, 120);
|
||||
DirectionFieldSnapshot pending;
|
||||
pending.state = DirectionFieldState::kPending;
|
||||
costmap.setDirection(pending);
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return worker.stats().step1_runs == 1; }));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
EXPECT_EQ(worker.stats().step2_runs, 0U);
|
||||
|
||||
costmap.setDirection(bandZone(200, 120, 7));
|
||||
worker.notifyDirectionChanged();
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
const ReferenceGraphSnapshot done = worker.snapshot();
|
||||
EXPECT_EQ(done.graph->direction_generation, 7U);
|
||||
EXPECT_FALSE(done.graph->lanes.empty());
|
||||
EXPECT_EQ(worker.stats().step1_runs, 1U);
|
||||
EXPECT_EQ(costmap.grid_captures, 1);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, GridOfTheOldMapIsNotUsed)
|
||||
{
|
||||
FakeCostmap costmap(200, 120);
|
||||
costmap.setDirection(bandZone(100, 60, 3));
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
costmap.setDirection(bandZone(200, 120, 4));
|
||||
worker.notifyDirectionChanged();
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
EXPECT_EQ(worker.snapshot().graph->direction_generation, 4U);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, DirectionAndFootprintChangesRunOnlyStep2)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
ASSERT_EQ(worker.stats().step1_runs, 1U);
|
||||
|
||||
worker.notifyDirectionChanged();
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
EXPECT_EQ(worker.snapshot().graph, nullptr);
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
|
||||
worker.notifyFootprintChanged();
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
|
||||
const ReferenceGraphWorkerStats stats = worker.stats();
|
||||
EXPECT_EQ(stats.step1_runs, 1U);
|
||||
EXPECT_EQ(stats.step2_runs, 3U);
|
||||
EXPECT_EQ(stats.published, 3U);
|
||||
EXPECT_EQ(costmap.grid_captures, 1);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, StaticChangeRunsBothSteps)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
worker.notifyStaticChanged();
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
EXPECT_EQ(worker.stats().step1_runs, 2U);
|
||||
EXPECT_EQ(worker.stats().step2_runs, 2U);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, ThreeMapsInARowPublishOnlyTheLast)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
Gate gate;
|
||||
costmap.on_capture = [&gate]() { gate.hold(); };
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
gate.waitReached();
|
||||
std::vector<uint8_t> last = corridor(120, 60);
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
last[static_cast<std::size_t>(30 * 120 + 10 + i)] = LETHAL_OBSTACLE;
|
||||
costmap.setGrid(last);
|
||||
worker.notifyStaticChanged();
|
||||
}
|
||||
gate.open();
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
EXPECT_EQ(worker.snapshot().graph->static_cost, mergeStaticLayers({ last }));
|
||||
EXPECT_EQ(worker.stats().published, 1U);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, MapDuringAComputationRestartsIt)
|
||||
{
|
||||
const uint32_t size = 1500;
|
||||
FakeCostmap costmap(size, size);
|
||||
std::atomic<bool> first{ true };
|
||||
ReferenceGraphWorker* worker_ptr = nullptr;
|
||||
std::vector<uint8_t> changed = corridor(size, size);
|
||||
changed[static_cast<std::size_t>(size / 2) * size + size / 2] = LETHAL_OBSTACLE;
|
||||
std::mutex started_mutex;
|
||||
std::condition_variable started;
|
||||
bool started_flag = false;
|
||||
costmap.on_capture = [&]() {
|
||||
if (first.exchange(false))
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(started_mutex);
|
||||
started_flag = true;
|
||||
started.notify_all();
|
||||
}
|
||||
};
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
worker_ptr = &worker;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(started_mutex);
|
||||
started.wait(lock, [&]() { return started_flag; });
|
||||
}
|
||||
costmap.setGrid(changed);
|
||||
worker_ptr->notifyStaticChanged();
|
||||
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
EXPECT_EQ(worker.snapshot().graph->static_cost, mergeStaticLayers({ changed }));
|
||||
EXPECT_EQ(worker.stats().published, 1U);
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, ExceptionStaysPendingUntilTheNextChange)
|
||||
{
|
||||
FakeCostmap costmap(120, 60);
|
||||
std::atomic<bool> fail{ true };
|
||||
costmap.on_capture = [&fail]() {
|
||||
if (fail)
|
||||
throw std::runtime_error("injected");
|
||||
};
|
||||
ReferenceGraphWorker worker(costmap.source(), quietParams());
|
||||
ASSERT_TRUE(waitUntil([&]() { return worker.stats().errors == 1; }));
|
||||
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
|
||||
const int captures = costmap.captures;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(450));
|
||||
EXPECT_EQ(costmap.captures, captures);
|
||||
|
||||
fail = false;
|
||||
worker.notifyStaticChanged();
|
||||
EXPECT_TRUE(waitUntil([&]() { return ready(worker); }));
|
||||
}
|
||||
|
||||
TEST(ReferenceGraphWorker, StopsQuicklyDuringAComputation)
|
||||
{
|
||||
const uint32_t size = 3000;
|
||||
FakeCostmap costmap(size, size);
|
||||
std::unique_ptr<ReferenceGraphWorker> worker(new ReferenceGraphWorker(costmap.source(), quietParams()));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.grid_captures >= 1; }));
|
||||
// Let it get well into step 1, where the map is largest.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
const Clock::time_point began = Clock::now();
|
||||
worker.reset();
|
||||
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - began).count();
|
||||
std::printf("stopped in %.1f ms\n", ms);
|
||||
EXPECT_LT(ms, 100.0);
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, DisabledHasNoWork)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kDisabled);
|
||||
costmap.layered.notifyStaticMapChanged(LayerType::STATIC_LAYER);
|
||||
costmap.layered.notifyDirectionFieldChanged();
|
||||
costmap.layered.setFootprint(rectangle(0.4, 0.4));
|
||||
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kDisabled);
|
||||
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::STATIC_LAYER), 1U);
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, RollingCostmapIsRefused)
|
||||
{
|
||||
LayeredCostmap rolling("map", true, true);
|
||||
EXPECT_FALSE(rolling.enableReferenceGraph(quietParams()));
|
||||
EXPECT_EQ(rolling.getReferenceGraphSnapshot().state, ReferenceGraphState::kDisabled);
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, BadParametersAndASecondCallAreRefused)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
ReferenceGraphParams bad = quietParams();
|
||||
bad.min_clearance_m = -1.0;
|
||||
EXPECT_FALSE(costmap.layered.enableReferenceGraph(bad));
|
||||
EXPECT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
EXPECT_FALSE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, MapThatArrivedBeforeEnablingCounts)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
const auto graph = costmap.layered.getReferenceGraphSnapshot().graph;
|
||||
EXPECT_EQ(graph->geometry.width, 120U);
|
||||
EXPECT_EQ(graph->direction_generation, 0U);
|
||||
EXPECT_TRUE(graph->lanes.empty());
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, WaitsUntilTheLayerWroteItsMap)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
costmap.layer->setCurrent(false);
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
|
||||
{
|
||||
boost::unique_lock<Costmap2D::mutex_t> lock(*costmap.layered.getCostmap()->getMutex());
|
||||
costmap.layer->setCurrent(true);
|
||||
}
|
||||
EXPECT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, NoStaticLayerStaysPending)
|
||||
{
|
||||
LayeredCostmap layered("map", false, true);
|
||||
layered.resizeMap(120, 60, kResolution, 0.0, 0.0);
|
||||
ASSERT_TRUE(layered.enableReferenceGraph(quietParams()));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
EXPECT_EQ(layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, OtherLayerTypesOnlyCountARevision)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
const ReferenceGraphSnapshot before = costmap.layered.getReferenceGraphSnapshot();
|
||||
costmap.layered.notifyStaticMapChanged(LayerType::PREFERRED_LAYER);
|
||||
costmap.layered.notifyStaticMapChanged(LayerType::UNPREFERRED_LAYER);
|
||||
costmap.layered.notifyStaticMapChanged(LayerType::CRITICAL_LAYER);
|
||||
const ReferenceGraphSnapshot after = costmap.layered.getReferenceGraphSnapshot();
|
||||
EXPECT_EQ(after.state, ReferenceGraphState::kReady);
|
||||
EXPECT_EQ(after.generation, before.generation);
|
||||
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::PREFERRED_LAYER), 1U);
|
||||
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::STATIC_LAYER), 0U);
|
||||
|
||||
costmap.layered.notifyStaticMapChanged(LayerType::STATIC_LAYER);
|
||||
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::STATIC_LAYER), 1U);
|
||||
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
|
||||
EXPECT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, MergesStaticLayersAndIgnoresSoftOnes)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
auto walls = boost::make_shared<FakeLayer>(LayerType::STATIC_LAYER);
|
||||
auto preferred = boost::make_shared<FakeLayer>(LayerType::PREFERRED_LAYER);
|
||||
for (const auto& layer : { walls, preferred })
|
||||
{
|
||||
layer->initialize(&costmap.layered, "extra", nullptr);
|
||||
costmap.layered.addPlugin(layer);
|
||||
layer->matchSize();
|
||||
}
|
||||
std::vector<uint8_t> wall_cost(120 * 60, NO_INFORMATION);
|
||||
wall_cost[30 * 120 + 60] = LETHAL_OBSTACLE;
|
||||
walls->write(wall_cost);
|
||||
std::vector<uint8_t> soft_cost(120 * 60, NO_INFORMATION);
|
||||
soft_cost[20 * 120 + 40] = LETHAL_OBSTACLE;
|
||||
preferred->write(soft_cost);
|
||||
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
const auto graph = costmap.layered.getReferenceGraphSnapshot().graph;
|
||||
EXPECT_EQ(graph->static_cost[30 * 120 + 60], LETHAL_OBSTACLE);
|
||||
EXPECT_EQ(graph->static_cost[20 * 120 + 40], FREE_SPACE);
|
||||
EXPECT_EQ(graph->static_cost[0], LETHAL_OBSTACLE);
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, OnlyARealFootprintChangeRecomputes)
|
||||
{
|
||||
Costmap costmap(120, 60);
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
const uint64_t generation = costmap.layered.getReferenceGraphSnapshot().generation;
|
||||
costmap.layered.setFootprint(rectangle(0.3, 0.3));
|
||||
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().generation, generation);
|
||||
costmap.layered.setFootprint(rectangle(0.35, 0.3));
|
||||
EXPECT_NE(costmap.layered.getReferenceGraphSnapshot().generation, generation);
|
||||
EXPECT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, ReadsTheDirectionProviderAndFollowsIt)
|
||||
{
|
||||
Costmap costmap(200, 120);
|
||||
auto direction = boost::make_shared<FakeDirectionLayer>();
|
||||
direction->initialize(&costmap.layered, "direction", nullptr);
|
||||
costmap.layered.addPlugin(direction);
|
||||
DirectionFieldSnapshot pending;
|
||||
pending.state = DirectionFieldState::kPending;
|
||||
direction->publish(pending);
|
||||
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
|
||||
|
||||
direction->publish(bandZone(200, 120, 11));
|
||||
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
|
||||
const auto graph = costmap.layered.getReferenceGraphSnapshot().graph;
|
||||
EXPECT_EQ(graph->direction_generation, 11U);
|
||||
EXPECT_FALSE(graph->lanes.empty());
|
||||
|
||||
direction->publish(bandZone(200, 120, 12));
|
||||
ASSERT_TRUE(waitUntil([&]() {
|
||||
const auto now = costmap.layered.getReferenceGraphSnapshot();
|
||||
return now.state == ReferenceGraphState::kReady && now.graph->direction_generation == 12U;
|
||||
}));
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, MasterMutexStaysFreeWhileComputing)
|
||||
{
|
||||
// SEHC-sized map. The costmap update must not feel the graph, so the master mutex may be held only for the copy.
|
||||
const uint32_t size = 3060;
|
||||
Costmap costmap(size, size);
|
||||
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
|
||||
double worst_ms = 0.0;
|
||||
const Clock::time_point end = Clock::now() + std::chrono::seconds(120);
|
||||
while (!costmap.ready() && Clock::now() < end)
|
||||
{
|
||||
const Clock::time_point began = Clock::now();
|
||||
{
|
||||
boost::unique_lock<Costmap2D::mutex_t> lock(*costmap.layered.getCostmap()->getMutex());
|
||||
}
|
||||
worst_ms = std::max(worst_ms, std::chrono::duration<double, std::milli>(Clock::now() - began).count());
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(200));
|
||||
}
|
||||
ASSERT_TRUE(costmap.ready());
|
||||
std::printf("longest wait for the master mutex %.2f ms\n", worst_ms);
|
||||
EXPECT_LT(worst_ms, 10.0);
|
||||
}
|
||||
|
||||
TEST(LayeredCostmapReferenceGraph, DestroyedDuringAComputation)
|
||||
{
|
||||
const uint32_t size = 2000;
|
||||
std::unique_ptr<Costmap> costmap(new Costmap(size, size));
|
||||
ASSERT_TRUE(costmap->layered.enableReferenceGraph(quietParams()));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
const Clock::time_point began = Clock::now();
|
||||
costmap.reset();
|
||||
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - began).count();
|
||||
EXPECT_LT(ms, 100.0);
|
||||
}
|
||||
Reference in New Issue
Block a user