feat(depth): observed_volume depth clearing as deployed 2026-10-14
Baseline commit of the working-tree refactor built on 2026-10-14 17:24 and running on the KIT robot, recorded in one commit before further changes. - VoxelLayer clearing looks up TF at the depth image stamp. - ObservationBuffer: per-source cloud preparation at the measurement stamp, height/range gate before the PCL VoxelGrid. - DepthFramePairer: pairs the marking cloud with the clearing image by source and stamp, bounded queue and age. - DepthVisibilityChecker and the observed_volume clearing mode in VoxelLayer; the legacy frustum mode stays the default. - Tests: depth_observation_buffer_test, depth_visibility_test.
This commit is contained in:
210
test/depth_visibility_test.cpp
Normal file
210
test/depth_visibility_test.cpp
Normal file
@@ -0,0 +1,210 @@
|
||||
#include <robot_costmap_2d/depth_visibility.h>
|
||||
#include <robot_costmap_2d/depth_frame_pairer.h>
|
||||
#include <robot_costmap_2d/voxel_layer.h>
|
||||
#include <robot_costmap_2d/inflation_layer.h>
|
||||
#include <robot_sensor_msgs/point_cloud2_iterator.h>
|
||||
#include <boost/make_shared.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <filesystem>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
using namespace robot_costmap_2d;
|
||||
namespace {
|
||||
robot_sensor_msgs::DepthCameraData::Ptr depth(double meters, robot::Time stamp = robot::Time(100,0)) {
|
||||
auto d = boost::make_shared<robot_sensor_msgs::DepthCameraData>();
|
||||
d->header.stamp=stamp; d->header.frame_id="camera";
|
||||
d->depth.header=d->header; d->camera_info.header=d->header;
|
||||
d->depth.width=d->camera_info.width=200; d->depth.height=d->camera_info.height=160;
|
||||
d->depth.step=400; d->depth.encoding="16UC1"; d->depth.data.resize(64000);
|
||||
uint16_t value=static_cast<uint16_t>(meters*1000);
|
||||
for(size_t i=0;i<d->depth.data.size();i+=2) std::memcpy(&d->depth.data[i], &value, 2);
|
||||
d->camera_info.K={100,0,100,0,100,80,0,0,1};
|
||||
d->camera_info.P={100,0,100,0,0,100,80,0,0,0,1,0};
|
||||
return d;
|
||||
}
|
||||
robot_sensor_msgs::PointCloud2 cloud(robot::Time stamp, bool point=true) {
|
||||
robot_sensor_msgs::PointCloud2 c; c.header.stamp=stamp; c.header.frame_id="camera"; c.height=1;
|
||||
robot_sensor_msgs::PointCloud2Modifier m(c); m.setPointCloud2FieldsByString(1,"xyz"); m.resize(point?1:0);
|
||||
if(point) { robot_sensor_msgs::PointCloud2Iterator<float> x(c,"x"), y(c,"y"), z(c,"z");
|
||||
*x=-.01;*y=.11;*z=.62; }
|
||||
return c;
|
||||
}
|
||||
tf3::TransformStampedMsg pose(robot::Time stamp, double yaw=0) {
|
||||
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
|
||||
p.header.stamp=tf3::Time(stamp.sec,stamp.nsec);
|
||||
Eigen::Matrix3d optical; optical << 0,0,1, -1,0,0, 0,-1,0;
|
||||
Eigen::Quaterniond q(Eigen::AngleAxisd(yaw,Eigen::Vector3d::UnitZ())*optical);
|
||||
p.transform.rotation.x=q.x();p.transform.rotation.y=q.y();
|
||||
p.transform.rotation.z=q.z();p.transform.rotation.w=q.w();p.transform.translation.z=.19;
|
||||
return p;
|
||||
}
|
||||
TEST(DepthVisibility, FullRegionFreeInvalidOccludedAndOutsideView) {
|
||||
DepthVisibilityChecker c; DepthFrustumConfig cfg;
|
||||
auto d=depth(2);
|
||||
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
|
||||
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
|
||||
Eigen::Vector3d lo(-.02,-.02,1),hi(.02,.02,1.05);
|
||||
ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
|
||||
EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Free);
|
||||
uint16_t zero=0;std::memcpy(&d->depth.data[80*400+100*2],&zero,2);
|
||||
ASSERT_TRUE(c.prepare(d,p,0,3,cfg));EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Unknown);
|
||||
d=depth(.8);ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
|
||||
EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Surface);
|
||||
d=depth(2);ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
|
||||
EXPECT_EQ(c.classify(Eigen::Vector3d(1,-.02,1),Eigen::Vector3d(1.05,.02,1.05)).state,DepthVisibility::Unknown);
|
||||
EXPECT_EQ(c.classify(Eigen::Vector3d(-.02,-.02,.05),hi).state,DepthVisibility::Unknown);
|
||||
EXPECT_EQ(c.classify(Eigen::Vector3d(1e30,-.02,1),Eigen::Vector3d(2e30,.02,1.05)).state,DepthVisibility::Unknown);
|
||||
}
|
||||
TEST(DepthVisibility, InvalidPixelAtProjectedEdgeAlsoBlocksClear) {
|
||||
auto d=depth(2); DepthVisibilityChecker c;DepthFrustumConfig cfg;
|
||||
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
|
||||
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
|
||||
uint16_t zero=0;std::memcpy(&d->depth.data[80*400+97*2],&zero,2);
|
||||
ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
|
||||
EXPECT_EQ(c.classify(Eigen::Vector3d(-.02,-.02,1),Eigen::Vector3d(.02,.02,1.05)).state,DepthVisibility::Unknown);
|
||||
}
|
||||
TEST(DepthVisibility, RejectsWrongStampModelAndMalformedImage) {
|
||||
auto d=depth(2);DepthVisibilityChecker c;DepthFrustumConfig cfg;
|
||||
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
|
||||
p.header.stamp=tf3::Time(101,0);p.transform.rotation.w=1;
|
||||
EXPECT_FALSE(c.prepare(d,p,0,3,cfg));p.header.stamp=tf3::Time(100,0);
|
||||
d->camera_info.P[0]=90;EXPECT_FALSE(c.prepare(d,p,0,3,cfg));
|
||||
d=depth(2);d->depth.data.pop_back();EXPECT_FALSE(c.prepare(d,p,0,3,cfg));
|
||||
d=depth(2);d->depth.is_bigendian=true;EXPECT_FALSE(c.prepare(d,p,0,3,cfg));
|
||||
}
|
||||
TEST(DepthPairer, MissingHalfExactPairAndDeduplication) {
|
||||
robot::Time::setNow(robot::Time(100,0));DepthFrustumConfig cfg;DepthFramePairer p(cfg,.5);
|
||||
auto c=cloud(robot::Time(100,0));p.addCloud(c);DepthFramePairer::Candidate next;
|
||||
EXPECT_FALSE(p.next(next));p.addDepth(depth(2));ASSERT_TRUE(p.next(next));
|
||||
EXPECT_EQ(next.stamp,c.header.stamp.toNSec());p.applied(next.stamp);
|
||||
p.addCloud(c);p.addDepth(depth(2));EXPECT_FALSE(p.next(next));
|
||||
EXPECT_EQ(p.counts().applied,1u);EXPECT_EQ(p.counts().duplicate,2u);
|
||||
}
|
||||
TEST(DepthPairer, NewerAppliedFrameCannotBeResurrectedByDelayedOlderCloud) {
|
||||
robot::Time::setNow(robot::Time(100,0));DepthFrustumConfig cfg;DepthFramePairer p(cfg,.5);
|
||||
p.addCloud(cloud(robot::Time(100,0)));robot::Time::setNow(robot::Time(100,100000000));
|
||||
p.addCloud(cloud(robot::Time(100,100000000)));p.addDepth(depth(2,robot::Time(100,100000000)));
|
||||
DepthFramePairer::Candidate n;ASSERT_TRUE(p.next(n));p.applied(n.stamp);
|
||||
p.addDepth(depth(2));EXPECT_FALSE(p.next(n));p.reset();
|
||||
p.addCloud(cloud(robot::Time(100,0)));p.addDepth(depth(2));EXPECT_FALSE(p.next(n));
|
||||
}
|
||||
TEST(DepthPairer, BoundTimeoutAgeAndClockReset) {
|
||||
robot::Time::setNow(robot::Time(100,0));DepthFrustumConfig cfg;cfg.pair_queue_size=2;cfg.pair_max_wait_s=.005;
|
||||
DepthFramePairer p(cfg,.5);
|
||||
for(int i=0;i<3;++i) {robot::Time::setNow(robot::Time(100,i*10000000));p.addCloud(cloud(robot::Time(100,i*10000000)));}
|
||||
EXPECT_EQ(p.counts().overflow,1u);std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
DepthFramePairer::Candidate n;EXPECT_FALSE(p.next(n));EXPECT_EQ(p.counts().expired,2u);
|
||||
p.addCloud(cloud(robot::Time(99,0)));EXPECT_EQ(p.counts().stale,1u);
|
||||
p.applied(robot::Time(100,20000000).toNSec());
|
||||
robot::Time::setNow(robot::Time(10,0));p.addCloud(cloud(robot::Time(10,0)));p.addDepth(depth(2,robot::Time(10,0)));
|
||||
EXPECT_TRUE(p.next(n));
|
||||
}
|
||||
class DepthLayerTest:public ::testing::Test {
|
||||
protected:
|
||||
tf3::BufferCore tf;
|
||||
LayeredCostmap parent{"odom",false,false};
|
||||
VoxelLayer layer;
|
||||
uint32_t tick=0;
|
||||
void SetUp() override {
|
||||
robot::Time::setNow(robot::Time(100,0));
|
||||
auto runtime=std::filesystem::path(ROBOT_COSTMAP_2D_DIR).parent_path().parent_path().parent_path()/"config/runtime";
|
||||
setenv("PNKX_NAV_CORE_CONFIG_DIR",runtime.c_str(),1);
|
||||
YAML::Node cfg=YAML::Clone(YAML::LoadFile((runtime/"costmap_common_params.yaml").string())["obstacles"]);
|
||||
cfg["observation_sources"]="pc_marking pc_clearing";cfg["footprint_clearing_enabled"]=false;cfg["enabled"]=true;
|
||||
robot::NodeHandle nh("~");nh.setParam("depth_test",cfg);
|
||||
parent.getCostmap()->resizeMap(80,80,.05,-2,-2);layer.initialize(&parent,"depth_test",&tf);
|
||||
ASSERT_TRUE(layer.isEnabled());
|
||||
}
|
||||
robot::Time now() {return robot::Time(100,++tick*10000000);}
|
||||
void update() {double a=1e9,b=1e9,c=-1e9,d=-1e9;layer.updateBounds(0,0,0,&a,&b,&c,&d);}
|
||||
void frame(double measurement,bool point=false,double yaw=0,bool hasTf=true,bool hasCloud=true,bool hasDepth=true) {
|
||||
auto stamp=now();robot::Time::setNow(stamp);
|
||||
if(hasTf) ASSERT_TRUE(tf.setTransform(pose(stamp,yaw),"test"));
|
||||
if(hasCloud) layer.dataCallBack(cloud(stamp,point),"/camera/depth/points_proc");
|
||||
if(hasDepth) {robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(measurement,stamp);
|
||||
layer.dataCallBack(d,"/camera/depth/data");}
|
||||
update();
|
||||
}
|
||||
unsigned char at(double x=.62,double y=.01) {unsigned a,b;EXPECT_TRUE(layer.worldToMap(x,y,a,b));return layer.getCost(a,b);}
|
||||
};
|
||||
TEST_F(DepthLayerTest, RetainsOutsideInvalidOccludedAndClearsOnFirstValidFrame) {
|
||||
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2,false,1.57);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(0);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(.4);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2);EXPECT_EQ(at(),FREE_SPACE); // one valid frame; no free-frame delay
|
||||
}
|
||||
TEST_F(DepthLayerTest, MissingCloudDepthOrTfCannotClearAndNoTtl) {
|
||||
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2,false,0,true,false,true);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2,false,0,true,true,false);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2,false,0,false,true,true);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
robot::Time::setNow(robot::Time(110,0));update();EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
EXPECT_FALSE(layer.isCurrent());
|
||||
}
|
||||
TEST_F(DepthLayerTest, ClearingFrameDoesNotRepeatOldMarkingCloud) {
|
||||
frame(.62,true);frame(2);ASSERT_EQ(at(),FREE_SPACE);
|
||||
layer.dataCallBack(cloud(robot::Time(100,10000000)),"/camera/depth/points_proc");
|
||||
update();EXPECT_EQ(at(),FREE_SPACE);
|
||||
}
|
||||
TEST_F(DepthLayerTest, RollingOriginPreservesOverlapAndDropsExitedCells) {
|
||||
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
|
||||
layer.updateOrigin(-1.5,-2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
layer.updateOrigin(1,-2);layer.updateOrigin(-2,-2);EXPECT_NE(at(),LETHAL_OBSTACLE);
|
||||
}
|
||||
TEST_F(DepthLayerTest, WaitingPairAppliesWhenItsExactTfArrives) {
|
||||
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2,false,0,false);EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
auto stamp=robot::Time(100,tick*10000000);
|
||||
ASSERT_TRUE(tf.setTransform(pose(stamp),"late_tf"));
|
||||
update();EXPECT_EQ(at(),FREE_SPACE);
|
||||
}
|
||||
TEST_F(DepthLayerTest, PendingFrameSurvivesRollingStorageShift) {
|
||||
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
|
||||
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
|
||||
layer.dataCallBack(cloud(stamp,false),"/camera/depth/points_proc");
|
||||
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(2,stamp);layer.dataCallBack(d,"/camera/depth/data");
|
||||
layer.updateOrigin(-1.5,-2);update();EXPECT_EQ(at(),FREE_SPACE);
|
||||
}
|
||||
TEST_F(DepthLayerTest, MasterAndInflationRemoveTheClearedObstacle) {
|
||||
parent.addPlugin(boost::shared_ptr<Layer>(&layer,[](Layer*){}));
|
||||
auto inflation=boost::make_shared<InflationLayer>();
|
||||
robot::NodeHandle nh("~");nh.setParam("depth_inflation/inflation_radius",.3);
|
||||
nh.setParam("depth_inflation/cost_scaling_factor",10.);
|
||||
parent.addPlugin(inflation);inflation->initialize(&parent,"depth_inflation",&tf);
|
||||
std::vector<robot_geometry_msgs::Point> footprint(4);
|
||||
footprint[0].x=footprint[1].x=-.1;footprint[2].x=footprint[3].x=.1;
|
||||
footprint[0].y=footprint[3].y=-.1;footprint[1].y=footprint[2].y=.1;
|
||||
parent.setFootprint(footprint);
|
||||
for(bool occupied : {true,false}) {
|
||||
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
|
||||
layer.dataCallBack(cloud(stamp,occupied),"/camera/depth/points_proc");
|
||||
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(occupied?.62:2,stamp);
|
||||
layer.dataCallBack(d,"/camera/depth/data");parent.updateMap(0,0,0);
|
||||
unsigned x,y;ASSERT_TRUE(parent.getCostmap()->worldToMap(.62,.01,x,y));
|
||||
EXPECT_EQ(parent.getCostmap()->getCost(x,y),occupied?LETHAL_OBSTACLE:FREE_SPACE);
|
||||
ASSERT_TRUE(parent.getCostmap()->worldToMap(.72,.01,x,y));
|
||||
if(occupied) EXPECT_GT(parent.getCostmap()->getCost(x,y),FREE_SPACE);
|
||||
else EXPECT_EQ(parent.getCostmap()->getCost(x,y),FREE_SPACE);
|
||||
}
|
||||
}
|
||||
TEST_F(DepthLayerTest, AStillOccludedVoxelKeepsItsColumnLethal) {
|
||||
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
|
||||
auto c=cloud(stamp);robot_sensor_msgs::PointCloud2Modifier m(c);m.resize(2);
|
||||
robot_sensor_msgs::PointCloud2Iterator<float> x(c,"x"),y(c,"y"),z(c,"z");
|
||||
*x=-.01;*y=.11;*z=.62;++x;++y;++z;*x=-.01;*y=-.29;*z=.62;
|
||||
layer.dataCallBack(c,"/camera/depth/points_proc");
|
||||
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(.62,stamp);layer.dataCallBack(d,"/camera/depth/data");update();
|
||||
ASSERT_EQ(at(),LETHAL_OBSTACLE);
|
||||
stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
|
||||
auto partial=depth(2,stamp);uint16_t blocked=400;
|
||||
for(unsigned v=0;v<65;++v) for(unsigned u=0;u<200;++u)
|
||||
std::memcpy(&partial->depth.data[v*400+u*2],&blocked,2);
|
||||
layer.dataCallBack(cloud(stamp,false),"/camera/depth/points_proc");
|
||||
d=partial;layer.dataCallBack(d,"/camera/depth/data");update();EXPECT_EQ(at(),LETHAL_OBSTACLE);
|
||||
frame(2);EXPECT_EQ(at(),FREE_SPACE);
|
||||
}
|
||||
}
|
||||
int main(int argc,char**argv) {robot::Time::init();testing::InitGoogleTest(&argc,argv);return RUN_ALL_TESTS();}
|
||||
|
||||
Reference in New Issue
Block a user