refactor(depth): DepthCameraObstacleLayer follows the CostmapLayer contract

The port wrote LETHAL straight into the master from its stored 3D points and
left its own grid empty, as costmap_depth_camera does. Everything CostmapLayer
offers on top of the own grid therefore missed the layer: clearArea() (the
ClearCostmapRecovery path), reset() (Costmap2DROBOT::resetLayers()), footprint
clearing and combination_method all acted on an empty grid, and the next cycle
wrote the ghosts back. Cleared cells also stayed on the master until another
layer's bounds covered them (the source's B7).

The stored points stay the source of truth; the clearing and marking
algorithm is unchanged. The own grid now mirrors them cell by cell, the way
VoxelLayer keeps a voxel grid next to its 2D grid:

- marking sets the column's cell LETHAL, the last point of a column leaving
  sets it FREE_SPACE, and both touch the bounds, so a cleared cell leaves the
  master in the same cycle (clear_touches_bounds is gone, always on);
- updateCosts() only merges the own grid with combination_method, like
  ObstacleLayer; combination_method other than 0/1 now means "no effect";
- reset() drops the stored points and buffered observations;
- clearArea() follows CostmapLayer::clearArea(): invert_area false clears
  outside the box and keeps the inside, true clears the inside;
- footprint clearing erases the stored columns under the robot;
- markings that leave the window are dropped in updateBounds() (B6), and a
  column whose cell went missing is re-marked;
- the layer mutex guards the point map, since ClearCostmapRecovery and
  resetLayers() reach it from other threads (recursive mutex, held by the
  recovery around clearArea()).

Tests: the two clear_touches_bounds cases become one bounded-update case, and
clearArea (both directions), reset and footprint clearing get their own.
This commit is contained in:
2026-10-20 13:33:51 +07:00
parent b23ff27a3c
commit a5f548f0d7
4 changed files with 258 additions and 74 deletions

View File

@@ -331,32 +331,79 @@ TEST_F(DepthCameraLayerTest, MarkingsThatLeaveTheWindowAreDropped)
frame(object());
ASSERT_EQ(layer.storedPoints(), 9u);
layer.updateOrigin(10, 10); // the stored cells are outside the layer now
Costmap2D& m = *parent.getCostmap();
layer.updateCosts(m, 0, 0, m.getSizeInCellsX(), m.getSizeInCellsY());
update();
EXPECT_EQ(layer.storedPoints(), 0u);
}
TEST_F(DepthCameraLayerTest, ClearedPointsDoNotTouchTheBoundsByDefault)
TEST_F(DepthCameraLayerTest, ClearedCellLeavesTheMasterInsideTheBounds)
{
frame(join(object(), wall()));
ASSERT_EQ(master(), LETHAL_OBSTACLE); // master now holds the object cell
frame(wall());
EXPECT_GT(min_x, 1.5); // only the wall marking touched the bounds
EXPECT_LT(min_x, 1.05); // the cleared object cell is part of this cycle's bounds
// What LayeredCostmap does: reset the master inside the bounds only, then merge the layer there.
Costmap2D& m = *parent.getCostmap();
int i0, j0, i1, j1;
m.worldToMapEnforceBounds(min_x, min_y, i0, j0);
m.worldToMapEnforceBounds(max_x, max_y, i1, j1);
m.resetMap(i0, j0, i1 + 1, j1 + 1);
layer.updateCosts(m, i0, j0, i1 + 1, j1 + 1);
unsigned int a, b;
ASSERT_TRUE(m.worldToMap(1.01, .11, a, b));
EXPECT_NE(m.getCost(a, b), LETHAL_OBSTACLE);
ASSERT_TRUE(m.worldToMap(1.61, .11, a, b));
EXPECT_EQ(m.getCost(a, b), LETHAL_OBSTACLE);
}
class DepthCameraTouchTest : public DepthCameraLayerTest
TEST_F(DepthCameraLayerTest, ClearAreaKeepsTheInsideByDefaultAndClearsItWhenInverted)
{
frame(join(object(), wall()));
ASSERT_EQ(master(), LETHAL_OBSTACLE);
ASSERT_EQ(master(1.61, .11), LETHAL_OBSTACLE);
// Cells strictly inside (55, 35)-(65, 50) hold the object (cell x 60); the wall is at cell x 72.
layer.clearArea(55, 35, 65, 50, false); // CostmapLayer contract: clears OUTSIDE the box
EXPECT_EQ(layer.storedPoints(), 9u);
EXPECT_EQ(master(), LETHAL_OBSTACLE);
EXPECT_NE(master(1.61, .11), LETHAL_OBSTACLE);
layer.clearArea(55, 35, 65, 50, true); // inverted: clears the inside
EXPECT_EQ(layer.storedPoints(), 0u);
EXPECT_NE(master(), LETHAL_OBSTACLE);
}
TEST_F(DepthCameraLayerTest, ResetDropsTheStoredPointsAndObservations)
{
frame(join(object(), wall()));
ASSERT_GT(layer.storedPoints(), 0u);
layer.reset();
EXPECT_EQ(layer.storedPoints(), 0u);
EXPECT_NE(master(), LETHAL_OBSTACLE);
update(); // no new cloud: the buffered observation is gone too, nothing comes back
EXPECT_EQ(layer.storedPoints(), 0u);
}
class DepthCameraFootprintTest : public DepthCameraLayerTest
{
protected:
void configure(YAML::Node& cfg) override
{
cfg["clear_touches_bounds"] = true;
cfg["footprint_clearing_enabled"] = true;
cfg["enable_near_blocked_protection"] = true; // an empty cycle must not clear by itself
}
};
TEST_F(DepthCameraTouchTest, ClearedPointsTouchTheBoundsWhenEnabled)
TEST_F(DepthCameraFootprintTest, FootprintClearingErasesThePointsUnderTheRobot)
{
frame(join(object(), wall()));
frame(wall());
EXPECT_LT(min_x, 1.05); // the cleared object cell is reset in the master too
std::vector<robot_geometry_msgs::Point> square(4);
square[0].x = -.3; square[0].y = -.3;
square[1].x = .3; square[1].y = -.3;
square[2].x = .3; square[2].y = .3;
square[3].x = -.3; square[3].y = .3;
parent.setFootprint(square);
frame(object());
ASSERT_EQ(layer.storedPoints(), 9u);
layer.updateBounds(1.02, .13, 0, &min_x, &min_y, &max_x, &max_y); // robot drives onto it
EXPECT_EQ(layer.storedPoints(), 0u);
EXPECT_NE(master(), LETHAL_OBSTACLE);
}
TEST(DepthCameraLayerConfig, RejectsUnportedModeAndOutOfRangeValues)