// HeightEstimator: z tren doc tu ds*sin(pitch), kep mat bang, neo ban do. #include #include #include "vslam/height_estimator.h" using namespace vslam; namespace { constexpr double kDeg = M_PI / 180.0; // Lai `dist` m doc mat doc voi pitch co dinh, buoc 1 cm. void Drive(HeightEstimator* h, double dist, double pitch) { const double step = dist >= 0 ? 0.01 : -0.01; const int n = static_cast(std::round(std::fabs(dist) / 0.01)); for (int i = 0; i < n; ++i) h->OnWheelStep(step, pitch); } } // namespace TEST(HeightEstimator, LenDocDungDoCao) { // Doc 15 do, cao 1.5 m: duong doc dai 1.5 / sin(15) = 5.796 m. Len doc thi // mui NGOC len: pitch AM (quy uoc ROS, duong = chuc xuong). HeightEstimator h; Drive(&h, 3.0, 0.0); Drive(&h, 1.5 / std::sin(15 * kDeg), -15 * kDeg); Drive(&h, 3.0, 0.0); EXPECT_NEAR(h.z(), 1.5, 0.002); } TEST(HeightEstimator, LuiXuongDocCungDung) { // Lui xuong doc: ds am, mui van ngoc (pitch am) -> z giam. HeightEstimator h; h.Reset(1.5, 0.0); Drive(&h, -1.5 / std::sin(10 * kDeg), -10 * kDeg); EXPECT_NEAR(h.z(), 0.0, 0.002); } TEST(HeightEstimator, KepMatBangTrietTieuBiasPitch) { // Bias pitch 0.5 do tren san phang 100 m: khong kep la 87 cm, kep la 0. HeightEstimator h; Drive(&h, 100.0, 0.5 * kDeg); EXPECT_DOUBLE_EQ(h.z(), 0.0); EXPECT_EQ(h.stats().clamped, h.stats().steps); EXPECT_FALSE(h.on_slope()); } TEST(HeightEstimator, DocNhoHonNguongBiCoiLaPhang) { // Gioi han NOI RO: doc 1 do < nguong 1.5 do -> bi bo qua. HeightEstimator h; Drive(&h, 30.0, -1.0 * kDeg); EXPECT_DOUBLE_EQ(h.z(), 0.0); } TEST(HeightEstimator, NeoKeoVeZBanDo) { // Sau mot doc, z lech 10 cm (vd truot banh): neo keo ve gan ban do. HeightEstimator h; Drive(&h, 5.0 * 1.1, -15 * kDeg); // truot 10%: dem nhieu hon that const double z_true = 5.0 * std::sin(15 * kDeg); ASSERT_GT(h.z() - z_true, 0.1); EXPECT_TRUE(h.Anchor(z_true)); EXPECT_LT(std::fabs(h.z() - z_true), 0.03); // Neo lan nua thi sat hon, P giam. const double s1 = h.sigma(); EXPECT_TRUE(h.Anchor(z_true)); EXPECT_LT(h.sigma(), s1); } TEST(HeightEstimator, NeoQuaXaBiTuChoi) { // z ban do lech 1 m so voi uoc luong vua di 3 m phang: nhieu kha nang la // node sai tang / sai cho -- khong duoc keo. HeightEstimator h; Drive(&h, 3.0, 0.0); EXPECT_FALSE(h.Anchor(1.0)); EXPECT_DOUBLE_EQ(h.z(), 0.0); EXPECT_EQ(h.stats().anchors_rejected, 1u); } TEST(HeightEstimator, TrenPhangNeoKeoNhe) { // Di 50 m phang (P nho): neo lech 5 cm chi keo mot phan -- mot node ban do // khong duoc dat lai z o noi robot da biet chac. HeightEstimator h; h.Reset(0.0, 0.0); Drive(&h, 1.0, 0.0); h.Anchor(0.05); EXPECT_GT(h.z(), 0.0); EXPECT_LT(h.z(), 0.03); }