Costmap2DROBOT copies a fixed list of observed_volume keys from obstacles/<source> into <costmap>/obstacles/<source>. The three phase-1 keys were missing, so the live layer always ran with their C++ defaults (evidence off, marking border 0) whatever the runtime YAML said. Unit tests set the layer params directly and did not catch it. The factory test now checks that the runtime YAML values reach the layer.
275 lines
11 KiB
C++
275 lines
11 KiB
C++
#include <robot_costmap_2d/observation_buffer.h>
|
|
#include <robot_costmap_2d/voxel_layer.h>
|
|
#include <robot_costmap_2d/costmap_2d_robot.h>
|
|
#include <robot_sensor_msgs/point_cloud2_iterator.h>
|
|
#include <gtest/gtest.h>
|
|
#include <array>
|
|
#include <limits>
|
|
#include <filesystem>
|
|
#include <cstdlib>
|
|
|
|
namespace
|
|
{
|
|
using robot_costmap_2d::CloudProcessingConfig;
|
|
using robot_costmap_2d::Observation;
|
|
using robot_costmap_2d::ObservationBuffer;
|
|
using Points = std::vector<std::array<float, 3>>;
|
|
robot_sensor_msgs::PointCloud2 cloud(const Points &points)
|
|
{
|
|
robot_sensor_msgs::PointCloud2 out;
|
|
out.height = 1;
|
|
out.header.frame_id = "camera";
|
|
out.header.stamp = robot::Time(10, 123456789);
|
|
out.header.seq = 9;
|
|
robot_sensor_msgs::PointCloud2Modifier m(out);
|
|
m.setPointCloud2FieldsByString(1, "xyz");
|
|
m.resize(points.size());
|
|
robot_sensor_msgs::PointCloud2Iterator<float> x(out, "x"), y(out, "y"), z(out, "z");
|
|
for (const auto &p : points)
|
|
{
|
|
*x = p[0]; *y = p[1]; *z = p[2];
|
|
++x; ++y; ++z;
|
|
}
|
|
return out;
|
|
}
|
|
void transform(tf3::BufferCore &tf, const std::string &child, double x, double z,
|
|
uint32_t sec = 10, bool fixed = true)
|
|
{
|
|
tf3::TransformStampedMsg t;
|
|
t.header.frame_id = "odom";
|
|
t.header.stamp = tf3::Time(sec, 123456789);
|
|
t.child_frame_id = child;
|
|
t.transform.translation.x = x;
|
|
t.transform.translation.z = z;
|
|
ASSERT_TRUE(tf.setTransform(t, "test", fixed));
|
|
}
|
|
CloudProcessingConfig config(bool voxel = true)
|
|
{
|
|
CloudProcessingConfig c;
|
|
c.transform_at_stamp = true;
|
|
c.voxel_filter_enabled = voxel;
|
|
return c;
|
|
}
|
|
Points output(ObservationBuffer &buffer, Observation *observation = nullptr)
|
|
{
|
|
std::vector<Observation> observations;
|
|
buffer.getObservations(observations);
|
|
if (observations.empty()) return {};
|
|
if (observation) *observation = observations.front();
|
|
const auto &c = *observations.front().cloud_;
|
|
Points points;
|
|
robot_sensor_msgs::PointCloud2ConstIterator<float> x(c, "x"), y(c, "y"), z(c, "z");
|
|
for (size_t i = 0; i < c.width * c.height; ++i, ++x, ++y, ++z)
|
|
points.push_back({*x, *y, *z});
|
|
return points;
|
|
}
|
|
TEST(DepthBuffer, HeightGatePrecedesCentroidAndKeepsExactHeader)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 0, 0.04);
|
|
ObservationBuffer b("depth", 0, 0, 0.05, 1, 2.5, 3, tf, "odom", "", 0.2);
|
|
b.setCloudProcessingConfig(config());
|
|
// Two floor points + a 6 cm obstacle share a camera-frame leaf before TF.
|
|
// Their premature centroid would be below the 5 cm height threshold.
|
|
ASSERT_TRUE(b.tryBufferCloud(cloud({{0.011f, 0.011f, 0.001f},
|
|
{0.013f, 0.011f, 0.001f},
|
|
{0.019f, 0.011f, 0.020f}})));
|
|
Observation obs;
|
|
const auto pts = output(b, &obs);
|
|
ASSERT_EQ(pts.size(), 1u);
|
|
EXPECT_NEAR(pts[0][2], 0.06, 1e-6);
|
|
EXPECT_EQ(obs.cloud_->header.frame_id, "odom");
|
|
EXPECT_EQ(obs.cloud_->header.stamp.nsec, 123456789u);
|
|
EXPECT_EQ(obs.cloud_->header.seq, 9u);
|
|
EXPECT_DOUBLE_EQ(obs.origin_.z, 0.04);
|
|
}
|
|
TEST(DepthBuffer, VoxelCentroidsArePerSourceOptIn)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 0, 0);
|
|
const auto c = cloud({{.011f, .011f, .11f}, {.019f, .011f, .11f}});
|
|
ObservationBuffer filtered("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
|
|
filtered.setCloudProcessingConfig(config());
|
|
ASSERT_TRUE(filtered.tryBufferCloud(c));
|
|
ASSERT_EQ(output(filtered).size(), 1u);
|
|
EXPECT_NEAR(output(filtered)[0][0], .015, 1e-6);
|
|
ObservationBuffer legacy("other", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
|
|
ASSERT_TRUE(legacy.tryBufferCloud(c));
|
|
EXPECT_EQ(output(legacy).size(), 2u);
|
|
}
|
|
TEST(DepthBuffer, UsesMeasurementTransformForCloudAndDifferentOriginFrame)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 1, 0, 10, false);
|
|
transform(tf, "camera", 4, 0, 11, false);
|
|
transform(tf, "sensor_origin", .5, 0, 10, false);
|
|
transform(tf, "sensor_origin", 3.5, 0, 11, false);
|
|
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "sensor_origin", .2);
|
|
b.setCloudProcessingConfig(config(false));
|
|
ASSERT_TRUE(b.tryBufferCloud(cloud({{0, 0, .1f}})));
|
|
Observation obs;
|
|
ASSERT_EQ(output(b, &obs).size(), 1u);
|
|
EXPECT_NEAR(output(b)[0][0], 1, 1e-6);
|
|
EXPECT_DOUBLE_EQ(obs.origin_.x, .5);
|
|
}
|
|
TEST(DepthBuffer, EmptyCloudIsValidAndMissingTfDoesNotReplaceIt)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 0, 0, 10, false);
|
|
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
|
|
b.setCloudProcessingConfig(config());
|
|
ASSERT_TRUE(b.tryBufferCloud(cloud({})));
|
|
auto late = cloud({{0, 0, .1f}});
|
|
late.header.stamp.sec = 20;
|
|
EXPECT_FALSE(b.tryBufferCloud(late));
|
|
std::vector<Observation> obs;
|
|
b.getObservations(obs);
|
|
ASSERT_EQ(obs.size(), 1u);
|
|
EXPECT_EQ(obs[0].cloud_->header.stamp.sec, 10u);
|
|
EXPECT_EQ(obs[0].cloud_->width, 0u);
|
|
EXPECT_FALSE(obs[0].cloud_->fields.empty());
|
|
}
|
|
TEST(DepthBuffer, FiniteHeightAndRangeGatesPrecedePcl)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 0, 0);
|
|
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
|
|
b.setCloudProcessingConfig(config(false));
|
|
const float nan = std::numeric_limits<float>::quiet_NaN();
|
|
ASSERT_TRUE(b.tryBufferCloud(cloud({{nan, 0, .1f}, {0, 0, nan},
|
|
{3, 0, .1f}, {0, 0, .049f},
|
|
{.1f, 0, .05f}, {.2f, 0, 1.f}, {.3f, 0, 1.01f}})));
|
|
EXPECT_EQ(output(b).size(), 2u);
|
|
}
|
|
TEST(DepthBuffer, RejectsZeroStampAndInvalidFilterConfig)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 0, 0);
|
|
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
|
|
b.setCloudProcessingConfig(config());
|
|
auto c = cloud({}); c.header.stamp = robot::Time();
|
|
EXPECT_FALSE(b.tryBufferCloud(c));
|
|
auto bad = config();
|
|
for (double leaf : {0., -1., std::numeric_limits<double>::infinity()})
|
|
{
|
|
bad.voxel_leaf_size = leaf;
|
|
EXPECT_THROW(b.setCloudProcessingConfig(bad), std::invalid_argument);
|
|
}
|
|
bad = config(); bad.transform_at_stamp = false;
|
|
EXPECT_THROW(b.setCloudProcessingConfig(bad), std::invalid_argument);
|
|
}
|
|
TEST(DepthBuffer, RejectsMalformedXyzFieldsBeforeReadingOrTransforming)
|
|
{
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 0, 0);
|
|
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
|
|
b.setCloudProcessingConfig(config());
|
|
auto c = cloud({{0, 0, .1f}});
|
|
c.fields[0].offset = c.point_step;
|
|
EXPECT_FALSE(b.tryBufferCloud(c));
|
|
c = cloud({}); c.fields.pop_back();
|
|
EXPECT_FALSE(b.tryBufferCloud(c));
|
|
c = cloud({}); c.is_bigendian = true;
|
|
EXPECT_FALSE(b.tryBufferCloud(c));
|
|
}
|
|
class InspectVoxelLayer : public robot_costmap_2d::VoxelLayer
|
|
{
|
|
public:
|
|
using robot_costmap_2d::ObstacleLayer::getMarkingObservations;
|
|
size_t pairedSources() const { return paired_depth_sources_.size(); }
|
|
};
|
|
TEST(DepthBuffer, RuntimeYamlActuallyEnablesTimestampAndPclOnDepthSource)
|
|
{
|
|
const auto runtime = std::filesystem::path(ROBOT_COSTMAP_2D_DIR)
|
|
.parent_path().parent_path().parent_path() / "config/runtime";
|
|
ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", runtime.c_str(), 1), 0);
|
|
tf3::BufferCore tf;
|
|
transform(tf, "camera", 1, 0, 10, false);
|
|
transform(tf, "camera", 4, 0, 11, false);
|
|
transform(tf, "base_link", 0, 0);
|
|
tf3::TransformStampedMsg map_odom;
|
|
map_odom.header.frame_id = "map";
|
|
map_odom.child_frame_id = "odom";
|
|
ASSERT_TRUE(tf.setTransform(map_odom, "test", true));
|
|
// Run the real costmap factory so copyParentParameters(), not test-only
|
|
// manual parameter injection, forwards common source options to this layer.
|
|
// Select the camera observation profile in memory; the default runtime may
|
|
// intentionally use f_scan. No config file or ROS parameter server is written.
|
|
robot::NodeHandle root("~");
|
|
root.setParam("obstacles/observation_sources", std::string("pc_marking"));
|
|
root.setParam("local_costmap/library_path", std::string(DEPTH_MARKING_TEST_PLUGIN_PATH));
|
|
robot_costmap_2d::Costmap2DROBOT configured("local_costmap", tf);
|
|
configured.pause();
|
|
robot::NodeHandle settings("local_costmap/obstacles/pc_marking");
|
|
bool stamp_tf = false, pcl_enabled = false;
|
|
ASSERT_TRUE(settings.getParam("cloud_transform_at_stamp", stamp_tf));
|
|
ASSERT_TRUE(settings.getParam("marking_voxel_filter/enabled", pcl_enabled));
|
|
EXPECT_TRUE(stamp_tf);
|
|
EXPECT_TRUE(pcl_enabled);
|
|
robot_costmap_2d::LayeredCostmap parent("odom", true, false);
|
|
parent.getCostmap()->resizeMap(40, 40, .05, -1, -1);
|
|
InspectVoxelLayer layer;
|
|
ASSERT_NO_THROW(layer.initialize(&parent, "local_costmap/obstacles", &tf));
|
|
ASSERT_TRUE(layer.isEnabled());
|
|
layer.dataCallBack(cloud({{.011f, .011f, .11f}, {.019f, .011f, .11f}}),
|
|
"/camera/depth/points_proc");
|
|
std::vector<Observation> observations;
|
|
EXPECT_TRUE(layer.getMarkingObservations(observations));
|
|
ASSERT_EQ(observations.size(), 1u);
|
|
const auto &c = *observations.front().cloud_;
|
|
ASSERT_EQ(c.width, 1u);
|
|
robot_sensor_msgs::PointCloud2ConstIterator<float> x(c, "x");
|
|
EXPECT_NEAR(*x, 1.015, 1e-6);
|
|
EXPECT_EQ(c.header.stamp.nsec, 123456789u);
|
|
}
|
|
TEST(DepthBuffer, RuntimeFactoryForwardsObservedVolumeModeAndPairsSources)
|
|
{
|
|
const auto runtime = std::filesystem::path(ROBOT_COSTMAP_2D_DIR)
|
|
.parent_path().parent_path().parent_path() / "config/runtime";
|
|
ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", runtime.c_str(), 1), 0);
|
|
tf3::BufferCore tf;
|
|
transform(tf, "base_link", 0, 0);
|
|
tf3::TransformStampedMsg map_odom;
|
|
map_odom.header.frame_id = "map"; map_odom.child_frame_id = "odom";
|
|
ASSERT_TRUE(tf.setTransform(map_odom, "test", true));
|
|
robot::NodeHandle root("~");
|
|
root.setParam("obstacles/observation_sources", std::string("pc_marking pc_clearing"));
|
|
root.setParam("local_costmap/library_path", std::string(DEPTH_MARKING_TEST_PLUGIN_PATH));
|
|
robot_costmap_2d::Costmap2DROBOT configured("local_costmap", tf);
|
|
configured.pause();
|
|
robot::NodeHandle settings("local_costmap/obstacles/pc_clearing");
|
|
std::string mode;
|
|
ASSERT_TRUE(settings.getParam("depth_clearing_mode", mode));
|
|
EXPECT_EQ(mode,"observed_volume");
|
|
// Every observed_volume key of the runtime YAML must reach the layer namespace; a key
|
|
// missing from the factory forward list silently falls back to its C++ default.
|
|
const YAML::Node runtime_clearing =
|
|
YAML::LoadFile((runtime / "costmap_common_params.yaml").string())["obstacles"]["pc_clearing"];
|
|
for (const char* key : {"depth_evidence_points_enabled", "depth_evidence_points_per_voxel",
|
|
"depth_marking_border_px", "depth_projection_margin_px", "depth_valid_max_z_m"})
|
|
{
|
|
ASSERT_TRUE(runtime_clearing[key].IsDefined()) << key;
|
|
ASSERT_TRUE(settings.hasParam(key)) << key;
|
|
EXPECT_EQ(YAML::Dump(settings.getParamValue(key)), YAML::Dump(runtime_clearing[key])) << key;
|
|
}
|
|
// Instantiate with the factory's scoped parameters, then verify the paired
|
|
// owner exists and a marking-only half does not enter legacy observations.
|
|
robot_costmap_2d::LayeredCostmap parent("odom", true, false);
|
|
parent.getCostmap()->resizeMap(40,40,.05,-1,-1);
|
|
InspectVoxelLayer layer;
|
|
ASSERT_NO_THROW(layer.initialize(&parent,"local_costmap/obstacles",&tf));
|
|
ASSERT_EQ(layer.pairedSources(),1u);
|
|
layer.dataCallBack(cloud({{.1f,0,.1f}}),"/camera/depth/points_proc");
|
|
std::vector<Observation> observations;
|
|
EXPECT_TRUE(layer.getMarkingObservations(observations));
|
|
EXPECT_TRUE(observations.empty());
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
robot::Time::init();
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|