#include #include #include #include #include #include #include #include #include #include #include #include #include namespace robot_costmap_2d { namespace { // This deterministic obstacle producer leaves bounds to the zone layer, so the tests also // exercise whether the rolling zone requests a redraw of the master window. class ObstacleMarks : public Layer { public: void onInitialize() override { enabled_ = true; } void updateCosts(Costmap2D& master, int, int, int, int) override { master.setCost(0, 0, LETHAL_OBSTACLE); master.setCost(2, 1, LETHAL_OBSTACLE); } protected: void handleImpl(const void*, const std::type_info&, const std::string&) override {} }; class MergeLayer : public CostmapLayer { public: MergeLayer() { enabled_ = true; } using CostmapLayer::updateWithMax; using CostmapLayer::updateWithOverwrite; protected: void handleImpl(const void*, const std::type_info&, const std::string&) override {} }; class RollingStaticLayerTest : public ::testing::Test { protected: tf3::BufferCore tf_{tf3::Duration(10.0)}; std::string directory_; std::string old_env_; bool had_env_ = false; void SetUp() override { if (const char* old = std::getenv("PNKX_NAV_CORE_CONFIG_DIR")) { had_env_ = true; old_env_ = old; } char pattern[] = "/tmp/rolling-static-layer-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); tf3::TransformStampedMsg transform; transform.header.frame_id = "map"; transform.child_frame_id = "odom"; transform.transform.translation.x = 14.0; transform.transform.translation.y = 24.0; transform.transform.rotation.z = 1.0; transform.transform.rotation.w = 0.0; ASSERT_TRUE(tf_.setTransform(transform, "test", true)); } void TearDown() override { if (had_env_) setenv("PNKX_NAV_CORE_CONFIG_DIR", old_env_.c_str(), 1); else unsetenv("PNKX_NAV_CORE_CONFIG_DIR"); if (!directory_.empty()) std::filesystem::remove_all(directory_); } void configure(bool maximum) { std::ofstream out(directory_ + "/config/static_layer_params.yaml"); out << "static_layer:\n enabled: true\n map_topic: map\n" " track_unknown_space: true\n lethal_cost_threshold: 100\n use_maximum: " << (maximum ? "true\n" : "false\n"); } robot_nav_msgs::OccupancyGrid grid(int outside) { robot_nav_msgs::OccupancyGrid map; map.header.frame_id = "map"; map.info.width = map.info.height = 4; map.info.resolution = 1.0; map.info.origin.position.x = 10.0; map.info.origin.position.y = 20.0; map.info.origin.orientation.w = 1.0; map.data.assign(16, outside); // map (2,2) and (1,2) correspond to odom (1,1) and (2,1). map.data[2 * 4 + 2] = 100; map.data[2 * 4 + 1] = 100; return map; } template void runZone(bool maximum, int outside, unsigned char zone_cost, bool obstacle_survives) { configure(maximum); LayeredCostmap master("odom", true, false); master.resizeMap(4, 4, 1.0, 0.0, 0.0); auto obstacles = boost::make_shared(); obstacles->initialize(&master, "obstacles", &tf_); master.addPlugin(obstacles); auto zone = boost::make_shared(); zone->initialize(&master, "zone", &tf_); master.addPlugin(zone); zone->dataCallBack(grid(outside), "map"); Costmap2D& costs = *master.getCostmap(); const auto check_costs = [&] { EXPECT_EQ(costs.getCost(1, 1), zone_cost); EXPECT_EQ(costs.getCost(2, 1), obstacle_survives ? LETHAL_OBSTACLE : zone_cost); EXPECT_EQ(costs.getCost(0, 0), LETHAL_OBSTACLE); EXPECT_EQ(costs.getCost(3, 3), FREE_SPACE); for (unsigned int y = 0; y < 4; ++y) for (unsigned int x = 0; x < 4; ++x) EXPECT_NE(costs.getCost(x, y), NO_INFORMATION) << x << "," << y; }; // Check the transformed merge independently of the bounds calculation. obstacles->updateCosts(costs, 0, 0, 4, 4); boost::static_pointer_cast(zone)->updateCosts(costs, 0, 0, 4, 4); check_costs(); master.updateMap(1.75, 1.75, 0.0); EXPECT_DOUBLE_EQ(costs.getOriginX(), 0.0); EXPECT_DOUBLE_EQ(costs.getOriginY(), 0.0); check_costs(); unsigned int x0, xn, y0, yn; master.getBounds(&x0, &xn, &y0, &yn); EXPECT_EQ(x0, 0u); EXPECT_EQ(y0, 0u); EXPECT_EQ(xn, 4u); EXPECT_EQ(yn, 4u); } }; TEST_F(RollingStaticLayerTest, ForbiddenMaxPreservesObstaclesAndFreeCells) { runZone(true, -1, LETHAL_OBSTACLE, true); } TEST_F(RollingStaticLayerTest, ForbiddenOverwriteSkipsUnknown) { runZone(false, -1, LETHAL_OBSTACLE, true); } TEST_F(RollingStaticLayerTest, PreferredAndUnpreferredRespectObstaclesInMaxMode) { runZone(true, 0, PREFERRED_SPACE, true); runZone(true, 0, UNPREFERRED_SPACE, true); } TEST_F(RollingStaticLayerTest, CriticalIsTransformedAndOverridesObstaclesInBothModes) { runZone(true, 0, CRITICAL_SPACE, false); runZone(false, 0, CRITICAL_SPACE, false); } TEST_F(RollingStaticLayerTest, BoundsCoverWindowWhenMapExtentIsElsewhere) { configure(true); LayeredCostmap master("odom", true, false); master.resizeMap(4, 4, 1.0, 0.0, 0.0); auto zone = boost::make_shared(); zone->initialize(&master, "zone", &tf_); master.addPlugin(zone); zone->dataCallBack(grid(-1), "map"); master.updateMap(1.75, 1.75, 0.0); EXPECT_EQ(master.getCostmap()->getCost(1, 1), LETHAL_OBSTACLE); EXPECT_EQ(master.getCostmap()->getCost(3, 3), FREE_SPACE); } TEST_F(RollingStaticLayerTest, NonRollingPreferredRetainsMaxAndOverwriteSemantics) { for (bool maximum : {false, true}) { configure(maximum); LayeredCostmap master("map", false, false); master.resizeMap(4, 4, 1.0, 0.0, 0.0); auto obstacles = boost::make_shared(); obstacles->initialize(&master, "obstacles", &tf_); master.addPlugin(obstacles); auto zone = boost::make_shared(); zone->initialize(&master, "zone", &tf_); master.addPlugin(zone); auto map = grid(0); map.info.origin.position.x = map.info.origin.position.y = 0.0; map.data.assign(16, 0); map.data[1 * 4 + 1] = 100; map.data[1 * 4 + 2] = 100; zone->dataCallBack(map, "map"); master.updateMap(0, 0, 0); EXPECT_EQ(master.getCostmap()->getCost(1, 1), PREFERRED_SPACE); EXPECT_EQ(master.getCostmap()->getCost(2, 1), maximum ? LETHAL_OBSTACLE : PREFERRED_SPACE); EXPECT_EQ(master.getCostmap()->getCost(0, 0), LETHAL_OBSTACLE); } } // Enumerate every possible byte pair against the original aligned-grid policies. TEST(StaticLayerMergeRegression, NonRollingOverwriteAndMaxRetainAllByteResults) { MergeLayer layer; layer.resizeMap(256, 256, 1.0, 0.0, 0.0); Costmap2D master(256, 256, 1.0, 0.0, 0.0, FREE_SPACE); for (bool maximum : {false, true}) { for (unsigned int old = 0; old < 256; ++old) for (unsigned int value = 0; value < 256; ++value) { master.setCost(value, old, old); layer.setCost(value, old, value); } if (maximum) layer.updateWithMax(master, 0, 0, 256, 256); else layer.updateWithOverwrite(master, 0, 0, 256, 256); for (unsigned int old = 0; old < 256; ++old) for (unsigned int value = 0; value < 256; ++value) { unsigned int expected = old; if (value != NO_INFORMATION && (!maximum || value == CRITICAL_SPACE || ((value == PREFERRED_SPACE || value == UNPREFERRED_SPACE) && old < INSCRIBED_INFLATED_OBSTACLE) || old == NO_INFORMATION || old < value)) expected = value; ASSERT_EQ(master.getCost(value, old), expected) << "old=" << old << " layer=" << value << " maximum=" << maximum; } } } } // namespace } // namespace robot_costmap_2d