Files
vslam_cpp/test/test_robot_model.cc
trung09122002 3896886349 upload driver
2026-09-19 18:25:55 +07:00

214 lines
7.0 KiB
C++

// Test cho RobotModel -- lop THAY CHO URDF.
//
// Cai duoc khoa o day khong phai phep toan (TransformTree da co test rieng),
// ma la cac CONG CHAN CONFIG SAI. Moi loi trong robot.yaml deu la loi im
// lang: mot phep bien doi lech 24 cm van cho ra ban do "trong nhu that", chi
// la sai cho.
#include <gtest/gtest.h>
#include <cmath>
#include "vslam/geometry.h"
#include "vslam/node_constants.h"
#include "vslam/robot_model.h"
using namespace vslam;
namespace {
// robot.yaml toi thieu ma hop le.
const char* kGood = R"(
geometry:
wheel_radius: 0.075
wheel_separation: 0.36
frames:
- parent: base_link
child: laser_link
xyz: [0.24, 0.0, 0.125]
rpy: [0.0, 0.0, 0.0]
- parent: base_link
child: camera_link
xyz: [0.19, 0.0, 0.725]
rpy: [0.0, 0.2618, 0.0]
- parent: camera_link
child: camera_depth_optical_frame
xyz: [0.0, 0.0, 0.0]
rpy: [-1.5707963267948966, 0.0, -1.5707963267948966]
)";
StatusResponse LoadYaml(RobotModel* m, const std::string& yaml) {
return m->Load(ConfigNode::LoadString(yaml, "<test>"));
}
} // namespace
TEST(RobotModel, NapDuocFileHopLe) {
RobotModel m;
const StatusResponse st = LoadYaml(&m, kGood);
ASSERT_TRUE(st.ok()) << st.message;
EXPECT_NEAR(m.geometry().wheel_radius, 0.075, 1e-12);
EXPECT_NEAR(m.geometry().wheel_separation, 0.36, 1e-12);
}
TEST(RobotModel, ChieuCaoCameraLaHEQUACuaHaiSoKhac) {
// `cliff_detect.nominal_tz` KHONG duoc go tay: no la
// camera_link.z + wheel_radius. Go tay lan thu hai la cho mot trong hai
// duoc phep lech -- va chung DA lech that: 0.803 trong cliff_detect.h so
// voi 0.80 trong sim.
RobotModel m;
ASSERT_TRUE(LoadYaml(&m, kGood).ok());
EXPECT_NEAR(m.camera_height_above_floor(), 0.725 + 0.075, 1e-12);
EXPECT_NEAR(m.camera_pitch(), 0.2618, 1e-12);
}
TEST(RobotModel, FrameQuangHocDungChieu) {
// Day la phep dung tung bi LAP LAI o bon file. Neu no sai o day thi no sai
// o dung mot cho, va test nay bat duoc.
//
// Quy uoc: +Z_quang = +X_than, +X_quang = -Y_than, +Y_quang = -Z_than.
RobotModel m;
ASSERT_TRUE(LoadYaml(&m, kGood).ok());
Transform cam_to_base;
ASSERT_TRUE(m.StaticTransform(kBaseLinkFrame, kCameraDepthOpticalFrame,
&cam_to_base));
// Mot diem cach camera 2 m theo TRUC QUANG (+Z_quang) phai nam phia TRUOC
// robot, va thap hon camera vi camera chuc xuong 15 do.
const Point p = TransformPoint(cam_to_base, Point{0.0, 0.0, 2.0});
EXPECT_GT(p.x, 1.8) << "truc quang khong tro ve phia truoc";
EXPECT_NEAR(p.y, 0.0, 1e-9) << "lech ngang -- sai phep quay quanh Z";
EXPECT_LT(p.z, 0.725) << "camera chuc xuong ma diem lai cao hon";
// Mot diem lech sang PHAI trong anh (+X_quang) phai nam ben PHAI robot,
// tuc -Y.
const Point q = TransformPoint(cam_to_base, Point{1.0, 0.0, 2.0});
EXPECT_LT(q.y, -0.5) << "+X_quang phai la -Y_than";
}
TEST(RobotModel, FrameLaserDungChoDaKhai) {
RobotModel m;
ASSERT_TRUE(LoadYaml(&m, kGood).ok());
Transform t;
ASSERT_TRUE(m.StaticTransform(kBaseLinkFrame, kLaserFrame, &t));
EXPECT_NEAR(t.translation.x, 0.24, 1e-12);
EXPECT_NEAR(t.translation.z, 0.125, 1e-12);
}
// ==================== Cac cong chan config sai ====================
TEST(RobotModel, FrameKhaiHaiLanBiTuChoi) {
// Hai dong cung `child`: dong sau am tham thang, va nua so nguoi doc file
// se tin dong dau. Day la lop bug ma ca file robot.yaml sinh ra de xoa,
// nen no phai la LOI chu khong phai canh bao.
RobotModel m;
const std::string y = std::string(kGood) + R"(
- parent: base_link
child: laser_link
xyz: [9.9, 0.0, 0.0]
rpy: [0.0, 0.0, 0.0]
)";
const StatusResponse st = LoadYaml(&m, y);
EXPECT_FALSE(st.ok());
EXPECT_NE(st.message.find("laser_link"), std::string::npos) << st.message;
}
TEST(RobotModel, GoSaiTenKhoaLaLoiChuKhongPhaiVeMacDinh) {
// `wheel_base` thay vi `wheel_separation` nghia la MOI phep quay sai ti le,
// va bang mac dinh im lang thi khong co gi bao.
RobotModel m;
const char* y = R"(
geometry:
wheel_radius: 0.075
wheel_base: 0.36
frames:
- parent: base_link
child: laser_link
xyz: [0.24, 0.0, 0.125]
rpy: [0.0, 0.0, 0.0]
)";
const StatusResponse st = LoadYaml(&m, y);
EXPECT_FALSE(st.ok());
EXPECT_NE(st.message.find("wheel_base"), std::string::npos) << st.message;
}
TEST(RobotModel, ThieuKhoiFramesLaLoi) {
RobotModel m;
const StatusResponse st = LoadYaml(&m, "geometry:\n wheel_radius: 0.075\n");
EXPECT_FALSE(st.ok());
EXPECT_NE(st.message.find("frames"), std::string::npos) << st.message;
}
TEST(RobotModel, XyzSaiSoPhanTuLaLoi) {
// `xyz: [0.24, 0.0]` doc bang mat trong nhu that. Thieu mot phan tu nghia
// la z = 0 -- laser tut xuong ngang san.
RobotModel m;
const char* y = R"(
frames:
- parent: base_link
child: laser_link
xyz: [0.24, 0.0]
rpy: [0.0, 0.0, 0.0]
)";
const StatusResponse st = LoadYaml(&m, y);
EXPECT_FALSE(st.ok());
EXPECT_NE(st.message.find("xyz"), std::string::npos) << st.message;
}
TEST(RobotModel, ParentTrungChildLaLoi) {
RobotModel m;
const char* y = R"(
frames:
- parent: base_link
child: base_link
xyz: [0.0, 0.0, 0.0]
rpy: [0.0, 0.0, 0.0]
)";
EXPECT_FALSE(LoadYaml(&m, y).ok());
}
TEST(RobotModel, FrameKhongTonTaiTraFalseChuKhongTaoMoi) {
// `Find` chu khong `Resolve`: tra cuu mot frame go sai ten phai TRA FALSE.
// Neu no am tham tao ra frame moi thi mot cai ten go sai se thanh mot nhanh
// roi, va loi chi hien ra khi ai do thac mac vi sao cloud rong.
RobotModel m;
ASSERT_TRUE(LoadYaml(&m, kGood).ok());
Transform t;
EXPECT_FALSE(m.StaticTransform(kBaseLinkFrame, "camera_dept_optical_frame", &t));
EXPECT_FALSE(m.StaticTransform("khong_ton_tai", kLaserFrame, &t));
}
TEST(RobotModel, FileKhongTonTaiTraLoiChuKhongNem) {
// Mot duong dan config go sai khong duoc phep lam sap tien trinh.
RobotModel m;
StatusResponse st = StatusResponse::Ok();
EXPECT_NO_THROW({ st = m.LoadFile("/khong/co/robot.yaml"); });
EXPECT_FALSE(st.ok());
}
TEST(RobotModel, FileThatTrongConfigurationFilesNapDuoc) {
// File that duoc cai len /etc/vslam phai luon nap duoc. Neu test nay do,
// ban vua lam hong cau hinh se giao cho robot.
const char* dir = std::getenv("VSLAM_CONFIG_SRC_DIR");
if (dir == nullptr) GTEST_SKIP() << "khong co VSLAM_CONFIG_SRC_DIR";
RobotModel m;
const StatusResponse st = m.LoadFile(std::string(dir) + "/robot.yaml");
ASSERT_TRUE(st.ok()) << st.message;
// Va no phai khop voi cac gia tri ma phan con lai cua he dang gia dinh.
// Ba so nay do tren robot that (banh Ø173 mm; camera cao hon LiDAR 17.7 cm
// va truoc 9.5 cm; LiDAR cao 13 cm so voi tam truc, x = 0).
EXPECT_NEAR(m.camera_height_above_floor(), 0.3935, 1e-9)
<< "chieu cao camera doi -> cliff_detect.min_dist va nominal_tz phai "
"doi theo. Xem pipeline.yaml muc cliff_detect.";
EXPECT_NEAR(m.geometry().wheel_radius, 0.0865, 1e-9);
EXPECT_NEAR(m.geometry().wheel_separation, 0.400, 1e-9);
Transform t;
ASSERT_TRUE(m.StaticTransform(kBaseLinkFrame, kLaserFrame, &t));
// LiDAR ngay tren tam truc, khong phai gan mep truoc.
EXPECT_NEAR(t.translation.x, 0.0, 1e-9);
EXPECT_NEAR(t.translation.z, 0.13, 1e-9);
}