feat(move_base2): wait for a ready global costmap and replay maps in host order

- Replay static maps in arrival order instead of topic order, so a
  direction grid can no longer be replayed before its static map (F3).
- PlannerRunner checks every enabled global layer under the same lock as
  the snapshot; while not current it keeps the goal, reports
  waiting_for_costmap, logs the stale layers (throttled) and does not
  spend planner_patience or watchdog budget. After costmap_ready_timeout
  [s] (default 10, 0 = legacy behaviour) the attempt fails as before.
- Test fixture destroys the server before its fake ports; the control
  thread could call a destroyed port (race existed before this change).
This commit is contained in:
2026-11-04 11:14:15 +07:00
parent 7bfb3c362d
commit 6decd8db32
18 changed files with 446 additions and 31 deletions

View File

@@ -13,6 +13,7 @@
#include <chrono>
#include <cmath>
#include <cstddef>
#include <future>
#include <limits>
#include <string>
#include <thread>
@@ -195,7 +196,6 @@ public:
server_.attachCostmaps(&global_, &local_);
}
NavigationServer server_;
FakeClockPort clock_;
FakePosePort pose_;
FakePlannerPort planner_;
@@ -208,6 +208,10 @@ public:
robot_costmap_2d::LayeredCostmap global_;
robot_costmap_2d::LayeredCostmap local_;
// The server joins its control thread in its destructor. Declare it last so all borrowed
// ports, status data and costmaps outlive that join.
NavigationServer server_;
};
} // namespace
@@ -611,6 +615,74 @@ TEST(NavigationServerSensors, StaticMapReceivedBeforeAttachIsReplayed)
EXPECT_EQ(static_layer->records()[0].topic, "/map");
}
TEST(CostmapReadyDecision, ReadyWaitTimeoutAndLegacy)
{
using move_base2::CostmapReadyDecision;
using move_base2::costmapReadyDecision;
EXPECT_EQ(costmapReadyDecision(false, 0.0, 10.0), CostmapReadyDecision::kWait);
EXPECT_EQ(costmapReadyDecision(false, 9.99, 10.0), CostmapReadyDecision::kWait);
EXPECT_EQ(costmapReadyDecision(false, 10.0, 10.0), CostmapReadyDecision::kTimeout);
EXPECT_EQ(costmapReadyDecision(true, 3.0, 10.0), CostmapReadyDecision::kPlan);
EXPECT_EQ(costmapReadyDecision(false, 0.0, 0.0), CostmapReadyDecision::kPlan);
}
TEST(NavigationServerSensors, ReplayUsesLatestArrivalOrder)
{
Fixture fixture;
fixture.configure();
const robot_nav_msgs::OccupancyGrid grid;
fixture.server_.addStaticMap("/map", grid);
fixture.server_.addStaticMap("/direction_map", grid);
SpyPtr layer = attachSpy(fixture.global_, LayerType::STATIC_LAYER, "replay_spy");
fixture.attachCostmaps();
ASSERT_EQ(layer->count(), 2U);
EXPECT_EQ(layer->records()[0].topic, "/map");
EXPECT_EQ(layer->records()[1].topic, "/direction_map");
// The live pushes are followed by a second replay after both latest arrivals.
fixture.server_.addStaticMap("/map", grid);
fixture.server_.addStaticMap("/direction_map", grid);
fixture.attachCostmaps();
ASSERT_EQ(layer->count(), 6U);
EXPECT_EQ(layer->records()[4].topic, "/map");
EXPECT_EQ(layer->records()[5].topic, "/direction_map");
}
TEST(NavigationServerSensors, ReplayForgetsErasedAndClearedSequences)
{
Fixture fixture;
fixture.configure();
const robot_nav_msgs::OccupancyGrid grid;
fixture.server_.addStaticMap("/map", grid);
fixture.server_.addStaticMap("/direction_map", grid);
ASSERT_TRUE(fixture.server_.removeStaticMap("/map"));
fixture.server_.addStaticMap("/map", grid);
SpyPtr layer = attachSpy(fixture.global_, LayerType::STATIC_LAYER, "replay_spy");
fixture.attachCostmaps();
ASSERT_EQ(layer->count(), 2U);
EXPECT_EQ(layer->records()[0].topic, "/direction_map");
EXPECT_EQ(layer->records()[1].topic, "/map");
ASSERT_TRUE(fixture.server_.removeAllStaticMaps());
fixture.server_.addStaticMap("/direction_map", grid);
const std::size_t before_clear_replay = layer->count();
fixture.attachCostmaps();
ASSERT_EQ(layer->count(), before_clear_replay + 1U);
EXPECT_EQ(layer->records()[before_clear_replay].topic, "/direction_map");
ASSERT_TRUE(fixture.server_.removeAllData());
fixture.server_.addStaticMap("/map", grid);
fixture.server_.addStaticMap("/direction_map", grid);
fixture.server_.map_name_save_ = "/legacy";
const std::size_t before_replay = layer->count();
fixture.attachCostmaps();
ASSERT_EQ(layer->count(), before_replay + 3U);
EXPECT_EQ(layer->records()[before_replay].topic, "/legacy");
EXPECT_EQ(layer->records()[before_replay + 1].topic, "/map");
EXPECT_EQ(layer->records()[before_replay + 2].topic, "/direction_map");
}
TEST(NavigationServerSensors, LegacyMapSavePublicMemberIsAlsoReplayed)
{
// `map_save_`/`map_name_save_` là biến PUBLIC của BaseNavigation mà host tự gán
@@ -1203,12 +1275,33 @@ TEST(NavigationServerControlThread, SecondStartIsRefusedAndStopIsIdempotent)
TEST(NavigationServerControlThread, DestructorStopsTheThread)
{
// Thread chạm loop_, runtime_ và sensors_ mỗi cycle; huỷ chúng khi thread còn sống là hỏng ở chỗ
// không truy được.
Fixture fixture;
fixture.configure();
ASSERT_TRUE(fixture.server_.startControlThread(100.0));
SUCCEED(); // destructor của fixture phải join, không treo
// Wait until a real cycle has begun; fixture destruction must join before its borrowed ports die.
class SignallingClock final : public move_base2::ClockPort
{
public:
explicit SignallingClock(std::promise<void>& started) : started_(started) {}
robot::Time now() const override
{
if (!signalled_.exchange(true))
started_.set_value();
return robot::Time(kClockStart);
}
private:
std::promise<void>& started_;
mutable std::atomic<bool> signalled_{false};
};
std::promise<void> started;
auto first_cycle = started.get_future();
SignallingClock clock(started);
{
Fixture fixture;
fixture.deps_.clock = &clock;
fixture.configure();
ASSERT_TRUE(fixture.server_.startControlThread(100.0));
ASSERT_EQ(first_cycle.wait_for(std::chrono::seconds(2)), std::future_status::ready);
}
}
// ================================================================================================