fix(costmap_2d): draw StaticLayer-family layers correctly in rolling costmaps

In a rolling (local) costmap the StaticLayer extent is in map_frame_, so the
bounds it reported did not cover the moving master window and zone layers
(forbidden, preferred, unpreferred, critical) were not redrawn as the robot
moved.

- StaticLayer::updateBounds: when the costmap is rolling, expand the bounds to
  the whole master window every cycle.
- StaticLayer::updateCosts: skip when disabled; the transformed path uses the
  master grid passed in and the same merge policy as the aligned path.
- CostmapLayer::mergeCost: one merge rule for aligned and transformed grids.
  NO_INFORMATION in the layer keeps the master cell; CRITICAL_SPACE always
  wins; PREFERRED/UNPREFERRED only below INSCRIBED_INFLATED_OBSTACLE; otherwise
  max. updateWithMax and updateWithOverwrite keep their previous results.
- CriticalLayer::updateCosts delegates to StaticLayer::updateCosts, so it is
  rolling-aware too.
- Test: rolling_static_layer_test, 7 cases.

Tests on dev: rolling_static_layer_test 7/7, test_costmap 9/9,
costmap_common_root_test 4/4 (rooted and flat). directional_layer_test
InvalidMaxDeviationThrows still fails: it checks directional_layer.cpp
initialization, which this change does not touch.
This commit is contained in:
2026-11-16 14:01:01 +07:00
parent 5034029c6e
commit 74bc39b1f6
6 changed files with 285 additions and 40 deletions

View File

@@ -321,6 +321,9 @@ if(BUILD_COSTMAP_TESTS)
add_executable(directional_layer_test test/directional_layer_test.cpp)
target_link_libraries(directional_layer_test PRIVATE plugins robot_costmap_2d GTest::GTest GTest::Main Boost::thread ${TF3_LIBRARY})
add_executable(rolling_static_layer_test test/rolling_static_layer_test.cpp)
target_link_libraries(rolling_static_layer_test PRIVATE plugins robot_costmap_2d GTest::GTest GTest::Main Boost::thread ${TF3_LIBRARY})
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/array_parser_test.cpp)
add_executable(test_array_parser test/array_parser_test.cpp)
target_link_libraries(test_array_parser PRIVATE

View File

@@ -72,6 +72,9 @@ public:
void addExtraBounds(double mx0, double my0, double mx1, double my1);
protected:
/// Merge one known layer cell with the same policy in aligned and transformed grids.
static unsigned char mergeCost(unsigned char master_cost, unsigned char layer_cost, bool use_maximum);
/*
* Updates the master_grid within the specified
* bounding box using this layer's values.

View File

@@ -22,16 +22,7 @@ unsigned char CriticalLayer::interpretValue(unsigned char value)
void CriticalLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
{
if (!map_received_)
return;
// if not rolling, the layered costmap (master_grid) has same coordinates as this layer
if (!use_maximum_)
StaticLayer::CostmapLayer::updateWithOverwrite(master_grid, min_i, min_j, max_i, max_j);
else
updateWithMax(master_grid, min_i, min_j, max_i, max_j);
markMapWritten(min_i, min_j, max_i, max_j);
StaticLayer::updateCosts(master_grid, min_i, min_j, max_i, max_j);
}
// Export factory function

View File

@@ -425,6 +425,18 @@ void StaticLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
useExtraBounds(min_x, min_y, max_x, max_y);
if (layered_costmap_->isRolling())
{
// The layer extent is in map_frame_; redraw the moving window in the master frame.
const Costmap2D& master = *layered_costmap_->getCostmap();
*min_x = std::min(*min_x, master.getOriginX());
*min_y = std::min(*min_y, master.getOriginY());
*max_x = std::max(*max_x, master.getOriginX() + master.getSizeInCellsX() * master.getResolution());
*max_y = std::max(*max_y, master.getOriginY() + master.getSizeInCellsY() * master.getResolution());
has_updated_data_ = false;
return;
}
double wx, wy;
mapToWorld(x_, y_, wx, wy);
@@ -440,7 +452,7 @@ void StaticLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
void StaticLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
{
if (!map_received_)
if (!map_received_ || !enabled_)
return;
if (!layered_costmap_->isRolling())
@@ -478,17 +490,14 @@ void StaticLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int min_
for (unsigned int j = min_j; j < max_j; ++j)
{
// Convert master_grid coordinates (i,j) into global_frame_(wx,wy) coordinates
layered_costmap_->getCostmap()->mapToWorld(i, j, wx, wy);
master_grid.mapToWorld(i, j, wx, wy);
// Transform from global_frame_ to map_frame_
tf3::Vector3 p(wx, wy, 0);
p = tf3_transform*p;
// Set master_grid with cell from map
if (worldToMap(p.x(), p.y(), mx, my))
{
if (!use_maximum_)
master_grid.setCost(i, j, getCost(mx, my));
else
master_grid.setCost(i, j, std::max(getCost(mx, my), master_grid.getCost(i, j)));
master_grid.setCost(i, j, mergeCost(master_grid.getCost(i, j), getCost(mx, my), use_maximum_));
}
}
}

View File

@@ -60,6 +60,18 @@ void CostmapLayer::useExtraBounds(double* min_x, double* min_y, double* max_x, d
has_extra_bounds_ = false;
}
unsigned char CostmapLayer::mergeCost(unsigned char master_cost, unsigned char layer_cost, bool use_maximum)
{
if (layer_cost == NO_INFORMATION)
return master_cost;
if (!use_maximum || layer_cost == CRITICAL_SPACE ||
((layer_cost == PREFERRED_SPACE || layer_cost == UNPREFERRED_SPACE) &&
master_cost < INSCRIBED_INFLATED_OBSTACLE) ||
master_cost == NO_INFORMATION || master_cost < layer_cost)
return layer_cost;
return master_cost;
}
void CostmapLayer::updateWithMax(robot_costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
{
if (!enabled_)
@@ -73,28 +85,7 @@ void CostmapLayer::updateWithMax(robot_costmap_2d::Costmap2D& master_grid, int m
unsigned int it = j * span + min_i;
for (int i = min_i; i < max_i; i++)
{
if (costmap_[it] == NO_INFORMATION){
it++;
continue;
}
if (costmap_[it] == CRITICAL_SPACE){
master_array[it] = costmap_[it];
it++;
continue;
}
if (costmap_[it] == PREFERRED_SPACE && master_array[it] < INSCRIBED_INFLATED_OBSTACLE ){
master_array[it] = costmap_[it];
it++;
continue;
}
if (costmap_[it] == UNPREFERRED_SPACE && master_array[it] < INSCRIBED_INFLATED_OBSTACLE ){
master_array[it] = costmap_[it];
it++;
continue;
}
unsigned char old_cost = master_array[it];
if (old_cost == NO_INFORMATION || old_cost < costmap_[it])
master_array[it] = costmap_[it];
master_array[it] = mergeCost(master_array[it], costmap_[it], true);
it++;
}
}
@@ -131,8 +122,7 @@ void CostmapLayer::updateWithOverwrite(robot_costmap_2d::Costmap2D& master_grid,
unsigned int it = span*j+min_i;
for (int i = min_i; i < max_i; i++)
{
if (costmap_[it] != NO_INFORMATION)
master[it] = costmap_[it];
master[it] = mergeCost(master[it], costmap_[it], false);
it++;
}
}

View File

@@ -0,0 +1,249 @@
#include <robot_costmap_2d/cost_values.h>
#include <robot_costmap_2d/critical_layer.h>
#include <robot_costmap_2d/preferred_layer.h>
#include <robot_costmap_2d/static_layer.h>
#include <robot_costmap_2d/unpreferred_layer.h>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <limits>
#include <string>
#include <boost/make_shared.hpp>
#include <gtest/gtest.h>
#include <tf3/buffer_core.h>
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 <class Zone>
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<ObstacleMarks>();
obstacles->initialize(&master, "obstacles", &tf_);
master.addPlugin(obstacles);
auto zone = boost::make_shared<Zone>();
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<Layer>(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<StaticLayer>(true, -1, LETHAL_OBSTACLE, true);
}
TEST_F(RollingStaticLayerTest, ForbiddenOverwriteSkipsUnknown)
{
runZone<StaticLayer>(false, -1, LETHAL_OBSTACLE, true);
}
TEST_F(RollingStaticLayerTest, PreferredAndUnpreferredRespectObstaclesInMaxMode)
{
runZone<PreferredLayer>(true, 0, PREFERRED_SPACE, true);
runZone<UnPreferredLayer>(true, 0, UNPREFERRED_SPACE, true);
}
TEST_F(RollingStaticLayerTest, CriticalIsTransformedAndOverridesObstaclesInBothModes)
{
runZone<CriticalLayer>(true, 0, CRITICAL_SPACE, false);
runZone<CriticalLayer>(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<StaticLayer>();
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<ObstacleMarks>();
obstacles->initialize(&master, "obstacles", &tf_);
master.addPlugin(obstacles);
auto zone = boost::make_shared<PreferredLayer>();
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