#include #include #include #include #include #include #include #include #include using namespace robot_costmap_2d; namespace { constexpr double kFovW = 1.23; // D410 at 424x240, fx = 300 [rad] constexpr double kFovV = 0.76; constexpr char kTopic[] = "/camera/depth/points_proc"; // Optical frame "camera" 0.19 m above odom, looking along +x of odom (rotated by yaw). 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; } // Points given in odom for a camera at yaw 0; stored in the optical frame of the cloud. robot_sensor_msgs::PointCloud2 cloud(robot::Time stamp, const std::vector& odom_points) { 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(odom_points.size()); robot_sensor_msgs::PointCloud2Iterator x(c, "x"), y(c, "y"), z(c, "z"); for (const auto& p : odom_points) { *x = -p.y(); *y = .19 - p.z(); *z = p.x(); ++x; ++y; ++z; } return c; } // 3 x 3 points, 6 cm apart (one per 5 cm voxel), one Euclidean cluster. Kept away from cell // boundaries: the source stores (int)(x / 0.05), so y = .07 .13 .19 are columns 1 2 3. std::vector object(double x = 1.02, double y = .13, double z = .30) { std::vector pts; for (double dy : {-.06, 0., .06}) for (double dz : {-.06, 0., .06}) pts.emplace_back(x, y + dy, z + dz); return pts; } // 11 points in a row 0.6 m behind the object: keeps the cloud above 5 points, own cluster. std::vector wall() { std::vector pts; for (int i = -5; i <= 5; ++i) pts.emplace_back(1.62, i * .06 + .13, .30); return pts; } std::vector join(std::vector a, const std::vector& b) { a.insert(a.end(), b.begin(), b.end()); return a; } YAML::Node baseConfig() { YAML::Node cfg; cfg["enabled"] = true; cfg["footprint_clearing_enabled"] = false; cfg["use_global_frame_to_mark"] = true; cfg["enable_near_blocked_protection"] = false; cfg["observation_sources"] = "depth_cam"; cfg["debug"]["stats"] = false; cfg["depth_cam"]["topic"] = kTopic; cfg["depth_cam"]["sensor_frame_is_optical"] = true; cfg["depth_cam"]["FOV_W"] = kFovW; cfg["depth_cam"]["FOV_V"] = kFovV; cfg["depth_cam"]["min_detect_distance"] = .15; cfg["depth_cam"]["max_detect_distance"] = 2.5; cfg["depth_cam"]["min_obstacle_height"] = .05; cfg["depth_cam"]["max_obstacle_height"] = 1.0; return cfg; } } // namespace // --------------------------------------------------------------------------------------------- // Frustum geometry // --------------------------------------------------------------------------------------------- TEST(DepthCameraFrustum, InsideFollowsTheConfiguredPyramid) { DepthCameraFrustum f; f.build(Eigen::Isometry3d::Identity(), kFovW, kFovV, .15, 2.5, false); // X forward const float half_w = std::tan(kFovW / 2), half_v = std::tan(kFovV / 2); EXPECT_TRUE(f.inside(Eigen::Vector3f(1, 0, 0))); EXPECT_FALSE(f.inside(Eigen::Vector3f(.1f, 0, 0))); // before the near plane EXPECT_FALSE(f.inside(Eigen::Vector3f(2.6f, 0, 0))); // beyond the far plane EXPECT_FALSE(f.inside(Eigen::Vector3f(-1, 0, 0))); EXPECT_TRUE(f.inside(Eigen::Vector3f(1, .98f * half_w, 0))); EXPECT_FALSE(f.inside(Eigen::Vector3f(1, 1.02f * half_w, 0))); EXPECT_FALSE(f.inside(Eigen::Vector3f(1, -1.02f * half_w, 0))); EXPECT_TRUE(f.inside(Eigen::Vector3f(1, 0, -.98f * half_v))); EXPECT_FALSE(f.inside(Eigen::Vector3f(1, 0, 1.02f * half_v))); } TEST(DepthCameraFrustum, AttachBandIsTwelveCentimetresFromEveryFace) { DepthCameraFrustum f; f.build(Eigen::Isometry3d::Identity(), kFovW, kFovV, .15, 2.5, false); EXPECT_FALSE(f.nearFace(Eigen::Vector3f(1, 0, 0))); EXPECT_TRUE(f.nearFace(Eigen::Vector3f(.25f, 0, 0))); // 10 cm past the near plane EXPECT_TRUE(f.nearFace(Eigen::Vector3f(2.4f, 0, 0))); // 10 cm before the far plane const float half_w = std::tan(kFovW / 2); EXPECT_TRUE(f.nearFace(Eigen::Vector3f(1, half_w - .05f, 0))); EXPECT_FALSE(f.nearFace(Eigen::Vector3f(1, half_w - .3f, 0))); } TEST(DepthCameraFrustum, OpticalFrameGivesTheSamePyramid) { DepthCameraFrustum link, optical; link.build(Eigen::Isometry3d::Identity(), kFovW, kFovV, .15, 2.5, false); Eigen::Isometry3d T = Eigen::Isometry3d::Identity(); T.linear() << 0, 0, 1, -1, 0, 0, 0, -1, 0; // optical axes expressed in camera_link optical.build(T, kFovW, kFovV, .15, 2.5, true); for (int i = 0; i < 8; ++i) EXPECT_LT((link.vertices[i] - optical.vertices[i]).norm(), 1e-5f) << "vertex " << i; EXPECT_TRUE(optical.inside(Eigen::Vector3f(1, 0, 0))); } TEST(DepthCameraFrustum, AttachedUnlessWellInsideAnotherFrustum) { DepthCameraFrustum a, b; a.build(Eigen::Isometry3d::Identity(), kFovW, kFovV, .15, 2.5, false); Eigen::Isometry3d T = Eigen::Isometry3d::Identity(); T.translation() << 0, .3, 0; // second camera 30 cm to the left b.build(T, kFovW, kFovV, .15, 2.5, false); const Eigen::Vector3f near_left_face_of_a(1, std::tan(kFovW / 2) - .05f, 0); EXPECT_TRUE(attachedToFrustums({a}, near_left_face_of_a)); EXPECT_FALSE(attachedToFrustums({a, b}, near_left_face_of_a)); EXPECT_FALSE(attachedToFrustums({a}, Eigen::Vector3f(1, 0, 0))); } // --------------------------------------------------------------------------------------------- // Layer // --------------------------------------------------------------------------------------------- class DepthCameraLayerTest : public ::testing::Test { protected: tf3::BufferCore tf; LayeredCostmap parent{"odom", false, false}; DepthCameraObstacleLayer layer; uint32_t tick = 0; double min_x = 0, min_y = 0, max_x = 0, max_y = 0; void SetUp() override { robot::Time::setNow(robot::Time(100, 0)); YAML::Node cfg = baseConfig(); configure(cfg); robot::NodeHandle nh("~"); nh.setParam("dcam_test", cfg); parent.getCostmap()->resizeMap(80, 80, .05, -2, -2); layer.initialize(&parent, "dcam_test", &tf); ASSERT_TRUE(layer.isEnabled()); } virtual void configure(YAML::Node&) {} robot::Time next() { return robot::Time(100, ++tick * 10000000); } void update() { min_x = min_y = 1e9; max_x = max_y = -1e9; layer.updateBounds(0, 0, 0, &min_x, &min_y, &max_x, &max_y); } void frame(const std::vector& pts, double yaw = 0, bool has_tf = true) { const robot::Time stamp = next(); robot::Time::setNow(stamp); if (has_tf) ASSERT_TRUE(tf.setTransform(pose(stamp, yaw), "test")); layer.dataCallBack(cloud(stamp, pts), kTopic); update(); } unsigned char master(double x = 1.01, double y = .11) { Costmap2D& m = *parent.getCostmap(); m.resetMap(0, 0, m.getSizeInCellsX(), m.getSizeInCellsY()); layer.updateCosts(m, 0, 0, m.getSizeInCellsX(), m.getSizeInCellsY()); unsigned int a, b; EXPECT_TRUE(m.worldToMap(x, y, a, b)); return m.getCost(a, b); } }; TEST_F(DepthCameraLayerTest, MarksAClusterInsideTheFrustum) { frame(object()); EXPECT_EQ(layer.storedPoints(), 9u); EXPECT_EQ(master(), LETHAL_OBSTACLE); EXPECT_TRUE(layer.isCurrent()); } TEST_F(DepthCameraLayerTest, SmallClusterAndOutOfHeightPointsAreNotMarked) { std::vector five = object(); five.resize(5); // <= size_of_cluster_rejection frame(join(five, wall())); EXPECT_NE(master(), LETHAL_OBSTACLE); frame(join(object(1.02, .13, 1.5), wall())); // above max_obstacle_height EXPECT_NE(master(), LETHAL_OBSTACLE); } TEST_F(DepthCameraLayerTest, KeptWhileSeenAgainAndClearedWhenGone) { frame(join(object(), wall())); ASSERT_EQ(master(), LETHAL_OBSTACLE); for (int i = 0; i < 5; ++i) frame(join(object(), wall())); EXPECT_EQ(master(), LETHAL_OBSTACLE); frame(wall()); // no cloud point within check_radius of the stored points EXPECT_NE(master(), LETHAL_OBSTACLE); EXPECT_EQ(master(1.61, .11), LETHAL_OBSTACLE); } TEST_F(DepthCameraLayerTest, OutsideTheFrustumIsKept) { frame(object()); ASSERT_EQ(master(), LETHAL_OBSTACLE); for (int i = 0; i < 5; ++i) frame({}, 1.57); // camera turned away, sees nothing EXPECT_EQ(master(), LETHAL_OBSTACLE); } TEST_F(DepthCameraLayerTest, NearlyEmptyCloudClearsEverythingInViewWithoutProtection) { frame(object()); ASSERT_EQ(master(), LETHAL_OBSTACLE); frame({}); EXPECT_NE(master(), LETHAL_OBSTACLE); } class DepthCameraBlockedTest : public DepthCameraLayerTest { protected: void configure(YAML::Node& cfg) override { cfg["enable_near_blocked_protection"] = true; } }; TEST_F(DepthCameraBlockedTest, NearlyEmptyCloudSkipsClearingWithProtection) { frame(object()); ASSERT_EQ(master(), LETHAL_OBSTACLE); for (int i = 0; i < 5; ++i) frame({}); EXPECT_EQ(master(), LETHAL_OBSTACLE); frame(wall()); EXPECT_NE(master(), LETHAL_OBSTACLE); } TEST_F(DepthCameraLayerTest, AttachBandIsNotMarked) { // 10 cm past the near plane (0.15 m) of a camera at x = 0: inside, but attached. frame(wall()); const std::size_t wall_points = layer.storedPoints(); ASSERT_GT(wall_points, 0u); frame(join(object(.25, .10, .19), wall())); EXPECT_EQ(layer.storedPoints(), wall_points); } TEST_F(DepthCameraLayerTest, WaitsForTheMeasurementTfThenDropsAfterTheTolerance) { frame(object(), 0, false); // no TF at this stamp yet EXPECT_EQ(layer.storedPoints(), 0u); const robot::Time stamp(100, tick * 10000000); ASSERT_TRUE(tf.setTransform(pose(stamp), "late_tf")); update(); EXPECT_EQ(layer.storedPoints(), 9u); frame(object(1.32, -.47, .3), 0, false); const robot::Time late(100, tick * 10000000); robot::Time::setNow(robot::Time(101, 0)); // older than transform_tolerance (0.2 s) update(); ASSERT_TRUE(tf.setTransform(pose(late), "too_late_tf")); update(); EXPECT_NE(master(1.31, -.44), LETHAL_OBSTACLE); // the waiting cloud was dropped EXPECT_EQ(layer.storedPoints(), 9u); } TEST_F(DepthCameraLayerTest, OtherTopicsAndDepthImagesAreIgnored) { const robot::Time stamp = next(); robot::Time::setNow(stamp); ASSERT_TRUE(tf.setTransform(pose(stamp), "test")); layer.dataCallBack(cloud(stamp, object()), "/other/points"); robot_sensor_msgs::DepthCameraData::ConstPtr depth = boost::make_shared(); layer.dataCallBack(depth, kTopic); update(); EXPECT_EQ(layer.storedPoints(), 0u); } TEST_F(DepthCameraLayerTest, MarkingsThatLeaveTheWindowAreDropped) { frame(object()); ASSERT_EQ(layer.storedPoints(), 9u); layer.updateOrigin(10, 10); // the stored cells are outside the layer now update(); EXPECT_EQ(layer.storedPoints(), 0u); } TEST_F(DepthCameraLayerTest, ClearedCellLeavesTheMasterInsideTheBounds) { frame(join(object(), wall())); ASSERT_EQ(master(), LETHAL_OBSTACLE); // master now holds the object cell frame(wall()); 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); } 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["footprint_clearing_enabled"] = true; cfg["enable_near_blocked_protection"] = true; // an empty cycle must not clear by itself } }; TEST_F(DepthCameraFootprintTest, FootprintClearingErasesThePointsUnderTheRobot) { std::vector 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_F(DepthCameraLayerTest, HasItsOwnLayerType) { EXPECT_EQ(layer.getType(), LayerType::DEPTH_CAMERA_LAYER); EXPECT_NE(layer.getType(), LayerType::VOXEL_LAYER); } TEST_F(DepthCameraLayerTest, MatchSizeKeepsAndReMarksTheStoredPoints) { frame(object()); ASSERT_EQ(master(), LETHAL_OBSTACLE); layer.matchSize(); // rebuilds the own grid from scratch EXPECT_EQ(layer.storedPoints(), 9u); EXPECT_EQ(master(), LETHAL_OBSTACLE); // re-marked without waiting for an update } TEST(DepthCameraLayerConfig, RejectsUnportedModeAndOutOfRangeValues) { tf3::BufferCore tf; LayeredCostmap parent{"odom", false, false}; parent.getCostmap()->resizeMap(80, 80, .05, -2, -2); robot::NodeHandle nh("~"); const auto expectThrow = [&](const YAML::Node& cfg) { nh.setParam("dcam_bad", cfg); DepthCameraObstacleLayer layer; EXPECT_THROW(layer.initialize(&parent, "dcam_bad", &tf), std::invalid_argument); }; YAML::Node cfg = baseConfig(); cfg["use_global_frame_to_mark"] = false; expectThrow(cfg); cfg = baseConfig(); cfg["depth_cam"]["FOV_W"] = 3.5; expectThrow(cfg); cfg = baseConfig(); cfg["depth_cam"]["min_detect_distance"] = 3.0; expectThrow(cfg); cfg = baseConfig(); cfg["voxel_resolution"] = 0.0; expectThrow(cfg); }