This commit is contained in:
trung09122002
2026-09-18 18:14:42 +07:00
commit 48bd5377dc
224 changed files with 37685 additions and 0 deletions

164
test/test_mcu_protocol.cc Normal file
View File

@@ -0,0 +1,164 @@
#include "vslam/mcu_protocol.h"
#include <gtest/gtest.h>
#include <string>
using namespace vslam;
// --- Ban ghi hop le --------------------------------------------------------
TEST(McuProtocol, ParseImu) {
McuRecord r;
ASSERT_TRUE(ParseMcuLine("I,123456,0.01,-0.02,0.03,0.1,-0.2,9.81", &r));
EXPECT_EQ(r.type, McuRecordType::kImu);
EXPECT_EQ(r.micros, 123456u);
EXPECT_DOUBLE_EQ(r.gx, 0.01);
EXPECT_DOUBLE_EQ(r.gy, -0.02);
EXPECT_DOUBLE_EQ(r.gz, 0.03);
EXPECT_DOUBLE_EQ(r.ax, 0.1);
EXPECT_DOUBLE_EQ(r.ay, -0.2);
EXPECT_DOUBLE_EQ(r.az, 9.81);
}
TEST(McuProtocol, ParseBaro) {
McuRecord r;
ASSERT_TRUE(ParseMcuLine("B,500,101325.5,24.3", &r));
EXPECT_EQ(r.type, McuRecordType::kBaro);
EXPECT_DOUBLE_EQ(r.pressure_pa, 101325.5);
EXPECT_DOUBLE_EQ(r.temp_c, 24.3);
}
TEST(McuProtocol, ParseEncoder) {
// Ban ghi MOI. Giao thuc cu chi co I/B/P, khong co encoder, du phan cung
// co encoder -- ban ROS gia dinh mot firmware chua ton tai.
McuRecord r;
ASSERT_TRUE(ParseMcuLine("E,700,12345,-6789", &r));
EXPECT_EQ(r.type, McuRecordType::kEncoder);
EXPECT_EQ(r.micros, 700u);
EXPECT_EQ(r.ticks_l, 12345);
EXPECT_EQ(r.ticks_r, -6789);
}
TEST(McuProtocol, ParsePong) {
McuRecord r;
ASSERT_TRUE(ParseMcuLine("P,900,42", &r));
EXPECT_EQ(r.type, McuRecordType::kPong);
EXPECT_EQ(r.seq, 42u);
}
TEST(McuProtocol, BuildPingCoNewline) {
EXPECT_EQ(BuildPing(7), "p,7\n");
EXPECT_EQ(BuildPing(0), "p,0\n");
}
// --- Dong hong phai bi TU CHOI, khong duoc coi la du lieu 0 ---------------
TEST(McuProtocol, TuChoiDongHong) {
McuRecord r;
// Mot dong hong bi coi la du lieu 0 nghia la mot mau IMU gia voi gia toc 0
// lot vao bo loc -- va bo loc thi khong co cach nao biet.
EXPECT_FALSE(ParseMcuLine("", &r));
EXPECT_FALSE(ParseMcuLine("I", &r));
EXPECT_FALSE(ParseMcuLine("I,", &r));
EXPECT_FALSE(ParseMcuLine("X,1,2,3", &r)) << "ky tu loai la";
EXPECT_FALSE(ParseMcuLine("II,1,2,3", &r)) << "ky tu loai 2 ky tu";
EXPECT_FALSE(ParseMcuLine("I,abc,1,2,3,4,5,6", &r))<< "micros khong phai so";
EXPECT_FALSE(ParseMcuLine("I,1,2,3", &r)) << "IMU thieu truong";
EXPECT_FALSE(ParseMcuLine("I,1,2,3,4,5,6,7,8", &r))<< "IMU thua truong";
EXPECT_FALSE(ParseMcuLine("B,1,2", &r)) << "baro thieu truong";
EXPECT_FALSE(ParseMcuLine("E,1,2", &r)) << "encoder thieu truong";
EXPECT_FALSE(ParseMcuLine("E,1,2,3,4", &r)) << "encoder thua truong";
EXPECT_FALSE(ParseMcuLine("P,1", &r)) << "pong thieu seq";
EXPECT_FALSE(ParseMcuLine("E,1,1.5,2", &r)) << "tick phai la so nguyen";
EXPECT_FALSE(ParseMcuLine("I,1,x,0,0,0,0,0", &r)) << "gyro khong phai so";
}
TEST(McuProtocol, TuChoiSoVuotDai) {
McuRecord r;
EXPECT_FALSE(ParseMcuLine("E,1,99999999999,0", &r));
EXPECT_FALSE(ParseMcuLine("I,99999999999,0,0,0,0,0,0", &r));
}
TEST(McuProtocol, ChapNhanSoAmVaKhoaHoc) {
McuRecord r;
ASSERT_TRUE(ParseMcuLine("I,1,-1.5e-3,0,0,0,0,-9.81", &r));
EXPECT_DOUBLE_EQ(r.gx, -1.5e-3);
EXPECT_DOUBLE_EQ(r.az, -9.81);
}
// --- TickUnwrapper ---------------------------------------------------------
TEST(TickUnwrapper, DemTangBinhThuong) {
TickUnwrapper u;
EXPECT_EQ(u.Unwrap(0), 0);
EXPECT_EQ(u.Unwrap(1000), 1000);
EXPECT_EQ(u.Unwrap(2000), 2000);
EXPECT_EQ(u.wrap_count(), 0u);
}
TEST(TickUnwrapper, DemGiamKhiRobotLui) {
TickUnwrapper u;
u.Unwrap(5000);
EXPECT_EQ(u.Unwrap(4000), 4000);
EXPECT_EQ(u.Unwrap(-1000), -1000);
EXPECT_EQ(u.wrap_count(), 0u) << "lui binh thuong bi hieu nham la tran";
}
TEST(TickUnwrapper, TranLen) {
TickUnwrapper u;
constexpr int32_t kMax = 2147483647;
const int64_t a = u.Unwrap(kMax - 100);
const int64_t b = u.Unwrap(kMax);
const int64_t c = u.Unwrap(-2147483648 + 100); // vua tran
EXPECT_EQ(u.wrap_count(), 1u);
EXPECT_GT(b, a);
EXPECT_GT(c, b) << "sau khi tran, so tick tich luy phai TIEP TUC tang";
EXPECT_EQ(c - b, 101);
}
TEST(TickUnwrapper, TranXuongKhiLuiLau) {
TickUnwrapper u;
constexpr int32_t kMin = -2147483647 - 1;
const int64_t a = u.Unwrap(kMin + 100);
const int64_t b = u.Unwrap(2147483647 - 100); // tran xuong
EXPECT_EQ(u.wrap_count(), 1u);
EXPECT_LT(b, a) << "lui qua moc tran phai tiep tuc GIAM";
EXPECT_EQ(a - b, 201);
}
TEST(TickUnwrapper, QuangDuongDaiKhongMat) {
// Ban ghi E gui tick TICH LUY chu khong gui delta. Ly do: mat mot dong
// serial thi delta mat vinh vien mot doan quang duong va khong the phat
// hien; con tich luy thi dong ke tiep tu no sua lai.
//
// Bai nay chay qua 3 lan tran.
TickUnwrapper u;
int64_t expected = 0;
int64_t raw = 0;
int64_t last = 0;
for (int i = 0; i < 300000; ++i) {
raw += 50000;
expected += 50000;
last = u.Unwrap(static_cast<int32_t>(raw & 0xFFFFFFFF));
}
EXPECT_EQ(last, expected);
EXPECT_GT(u.wrap_count(), 2u);
}
TEST(TickUnwrapper, MatDongVanTuSuaDuoc) {
// Bo qua vai ban ghi (mat dong serial) roi tiep tuc: gia tri tich luy van
// dung, khong tich luy sai so.
TickUnwrapper a, b;
int64_t last_a = 0, last_b = 0;
for (int i = 1; i <= 100; ++i) {
const int32_t raw = static_cast<int32_t>(i * 1000);
last_a = a.Unwrap(raw);
if (i % 3 == 0) last_b = b.Unwrap(raw); // chi nhan 1/3 so dong
}
EXPECT_EQ(last_a, 100000);
EXPECT_EQ(last_b, 99000); // dong cuoi nhan duoc la i=99
}