Lenh di qua FSM an toan nhu moi lenh khac; dung han khi lech duong > 0.4 m, scan mat khop 1.5 s, pose nhay > 0.3 m, nghieng > 20 do. Do lech dinh vi 2 lan/giay (khop scan voi likelihood field, doc lap RTAB-Map) ra logs/slope_test_<gio>.csv + bang tong. Sim 08/10 (slope_two_ramps, 10 luot): lech that trung vi 0.9 cm, max 3.0 cm, sai z trung vi 0.3 cm, khong troi tu luot 1 den 10.
261 lines
8.6 KiB
C++
261 lines
8.6 KiB
C++
// Test doc tu dong (08/10): lap duong tu ban do + may trang thai lai.
|
|
// Robot gia lap: mo hinh xe hai banh, tich phan lenh (v, w) cua chinh SlopeTest.
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vslam/geometry.h"
|
|
#include "vslam/slope_test.h"
|
|
|
|
using namespace vslam;
|
|
|
|
namespace {
|
|
|
|
// Doc gia doc truc x: tang tren z 1.2 (x < 2), doc xuong toi z 0 o x = 8, san
|
|
// duoi phang. Nha |y| < 3, x trong (-1, 11); tinh lan 0.3 m cho than robot.
|
|
bool Floor(double x, double y, double* z) {
|
|
if (x < -1.0 || x > 11.0 || std::fabs(y) > 3.0) return false;
|
|
if (x < 2.0) *z = 1.2;
|
|
else if (x < 8.0) *z = 1.2 * (8.0 - x) / 6.0;
|
|
else *z = 0.0;
|
|
return true;
|
|
}
|
|
bool Free(double x, double y) { return x > -0.7 && x < 10.7 && std::fabs(y) < 2.7; }
|
|
|
|
struct Sim {
|
|
SlopeTest t;
|
|
Pose2D p;
|
|
double now = 1.0;
|
|
std::vector<std::string> events;
|
|
double vmax_leg = 0.0;
|
|
explicit Sim(SlopeTestConfig c) : t(std::move(c)) {}
|
|
|
|
// Mot buoc 50 ms. Tra output de test kiem tra.
|
|
SlopeTestOutput Step(bool start = false, bool human = false, bool localized = true) {
|
|
SlopeTestInput in;
|
|
in.t = now;
|
|
in.pose = p;
|
|
in.localized = localized;
|
|
in.start = start;
|
|
in.human = human;
|
|
const SlopeTestOutput o = t.Step(in);
|
|
if (!o.event.empty()) events.push_back(o.event);
|
|
if (o.command) {
|
|
const double dt = 0.05;
|
|
p.theta = NormalizeAngle(p.theta + o.w * dt);
|
|
p.x += o.v * std::cos(p.theta) * dt;
|
|
p.y += o.v * std::sin(p.theta) * dt;
|
|
vmax_leg = std::max(vmax_leg, o.v);
|
|
}
|
|
now += 0.05;
|
|
return o;
|
|
}
|
|
// Chay toi khi gap pha `ph` (hoac het so buoc).
|
|
bool RunUntil(SlopeTestPhase ph, int max_steps = 20000) {
|
|
for (int i = 0; i < max_steps; ++i) {
|
|
if (t.phase() == ph) return true;
|
|
Step();
|
|
}
|
|
return t.phase() == ph;
|
|
}
|
|
};
|
|
|
|
SlopeTestConfig Cfg() {
|
|
SlopeTestConfig c;
|
|
c.loops = 3;
|
|
c.speeds = {0.10, 0.30, 0.45};
|
|
c.dwell_s = 1.0;
|
|
return c;
|
|
}
|
|
|
|
bool Has(const std::vector<std::string>& ev, const std::string& s) {
|
|
for (const auto& e : ev) {
|
|
if (e.find(s) != std::string::npos) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(PlanSlopePath, TimDungTrucDocVaChanDoc) {
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why)) << why;
|
|
EXPECT_NEAR(p.a.theta, 0.0, 3.0 * M_PI / 180.0); // xuong doc theo +x
|
|
EXPECT_NEAR(p.drop, 1.2, 0.05);
|
|
// Chan doc: mat san PHANG (do doc < 3 do, sai phan +-0.25 m) tu x = 8.25, + 0.5 m.
|
|
EXPECT_NEAR(p.bx, 8.75, 0.2);
|
|
EXPECT_NEAR(p.by, 0.2, 0.2);
|
|
// Robot da o dinh phang: A = cho robot.
|
|
EXPECT_NEAR(p.a.x, 0.5, 1e-9);
|
|
}
|
|
|
|
TEST(PlanSlopePath, GiuaDocThiADatODinhDoc) {
|
|
// Dinh vi lai tu chay ra GIUA doc (sim 08/10: 3.5 m). A phai la DINH phang,
|
|
// KHONG phai cho robot dung.
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(4.0, 0.2, Floor, Free, SlopePathConfig{}, &p, &why)) << why;
|
|
EXPECT_NEAR(p.a.theta, 0.0, 3.0 * M_PI / 180.0);
|
|
EXPECT_NEAR(p.a.x, 1.25, 0.15); // phang tu x = 1.75 (+-0.25) lui 1 m, + 0.5 m
|
|
EXPECT_NEAR(p.bx, 8.75, 0.2);
|
|
EXPECT_NEAR(p.drop, 1.2, 0.05);
|
|
}
|
|
|
|
TEST(PlanSlopePath, KhongChonDuongCheoNgangDoc) {
|
|
// Doc rong (|y| < 3) dung SAT tuong y = 2.5: huong cheo cat qua doc ngan hon
|
|
// nhung khong xuong het -- phai chon truc doc (xuong 1.2 m).
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(3.0, 2.3, Floor, Free, SlopePathConfig{}, &p, &why)) << why;
|
|
EXPECT_NEAR(p.drop, 1.2, 0.05);
|
|
EXPECT_NEAR(p.a.theta, 0.0, 6.0 * M_PI / 180.0);
|
|
}
|
|
|
|
TEST(PlanSlopePath, KhongCoDocThiBaoLoi) {
|
|
SlopePath p;
|
|
std::string why;
|
|
// San phang toan bo: khong huong nao xuong.
|
|
auto flat = [](double x, double y, double* z) {
|
|
*z = 0.0;
|
|
return std::fabs(x) < 5.0 && std::fabs(y) < 5.0;
|
|
};
|
|
auto free = [](double x, double y) { return std::fabs(x) < 4.7 && std::fabs(y) < 4.7; };
|
|
EXPECT_FALSE(PlanSlopePath(0.0, 0.0, flat, free, SlopePathConfig{}, &p, &why));
|
|
EXPECT_NE(why.find("khong thay doc"), std::string::npos);
|
|
}
|
|
|
|
TEST(SlopeTest, QuayMatXuongDocRoiChoNut) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{0.5, 0.2, 2.0}; // dang nhin xien sau reloc
|
|
s.t.SetPath(p, false);
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kWaitStart, 2000));
|
|
EXPECT_NEAR(NormalizeAngle(s.p.theta - p.a.theta), 0.0, 4.0 * M_PI / 180.0);
|
|
// Cho nut: KHONG ra lenh, robot dung yen.
|
|
const Pose2D before = s.p;
|
|
for (int i = 0; i < 100; ++i) EXPECT_FALSE(s.Step().command);
|
|
EXPECT_DOUBLE_EQ(s.p.x, before.x);
|
|
}
|
|
|
|
TEST(SlopeTest, GiuaDocThiVeDinhTruoc) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(4.0, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{4.0, 0.2, 2.0};
|
|
s.t.SetPath(p, false);
|
|
s.Step();
|
|
EXPECT_TRUE(s.t.approaching());
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kWaitStart, 4000));
|
|
EXPECT_LT(std::hypot(s.p.x - p.a.x, s.p.y - p.a.y), 0.15);
|
|
EXPECT_NEAR(NormalizeAngle(s.p.theta - p.a.theta), 0.0, 4.0 * M_PI / 180.0);
|
|
EXPECT_TRUE(Has(s.events, "chay CHAM ve A"));
|
|
EXPECT_LE(s.vmax_leg, 0.15 + 1e-9); // ve A cham
|
|
}
|
|
|
|
TEST(SlopeTest, DuLuotDungThuTuVaVanToc) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{0.5, 0.2, 0.0};
|
|
s.t.SetPath(p, false);
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kWaitStart, 2000));
|
|
s.Step(true); // bam Y
|
|
const double speeds[] = {0.10, 0.30, 0.45};
|
|
for (int k = 0; k < 3; ++k) {
|
|
for (bool down : {true, false}) {
|
|
s.vmax_leg = 0.0;
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDwell)) << "luot " << k + 1 << (down ? " xuong" : " len");
|
|
EXPECT_EQ(s.t.loop(), k + 1);
|
|
EXPECT_EQ(s.t.down(), down);
|
|
// Van toc luot nay: dat toi, khong vuot.
|
|
EXPECT_NEAR(s.vmax_leg, speeds[k], 1e-9);
|
|
// Toi dung dau doc.
|
|
const double tx = down ? p.bx : p.a.x, ty = down ? p.by : p.a.y;
|
|
EXPECT_LT(std::hypot(s.p.x - tx, s.p.y - ty), 0.15);
|
|
// Roi pha dung do.
|
|
for (int i = 0; i < 40 && s.t.phase() == SlopeTestPhase::kDwell; ++i) s.Step();
|
|
}
|
|
}
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDone, 200));
|
|
EXPECT_TRUE(Has(s.events, "XONG 3 luot"));
|
|
EXPECT_TRUE(Has(s.events, "luot 2/3 XUONG 0.3 m/s"));
|
|
EXPECT_TRUE(Has(s.events, "luot 3/3 LEN 0.45 m/s"));
|
|
// Xong thi im.
|
|
EXPECT_FALSE(s.Step().command);
|
|
}
|
|
|
|
TEST(SlopeTest, NguoiLaiThiTamDungRoiChayTiep) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{0.5, 0.2, 0.0};
|
|
s.t.SetPath(p, true); // tu chay
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDrive, 2000));
|
|
for (int i = 0; i < 40; ++i) s.Step();
|
|
// Nguoi gat can: tam dung, IM ngay.
|
|
EXPECT_FALSE(s.Step(false, true).command);
|
|
EXPECT_EQ(s.t.phase(), SlopeTestPhase::kPaused);
|
|
for (int i = 0; i < 50; ++i) EXPECT_FALSE(s.Step().command);
|
|
// Bam Y: chay tiep toi chan doc.
|
|
s.Step(true);
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDwell));
|
|
EXPECT_LT(std::hypot(s.p.x - p.bx, s.p.y - p.by), 0.15);
|
|
}
|
|
|
|
TEST(SlopeTest, LechDuongThiDungHan) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{0.5, 0.2, 0.0};
|
|
s.t.SetPath(p, true);
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDrive, 2000));
|
|
for (int i = 0; i < 20; ++i) s.Step();
|
|
s.p.y += 0.25; // truot ngang (khong nhay qua jump_max)
|
|
s.Step();
|
|
s.p.y += 0.25;
|
|
const SlopeTestOutput o = s.Step();
|
|
EXPECT_EQ(s.t.phase(), SlopeTestPhase::kHalted);
|
|
EXPECT_TRUE(o.command); // MOT lenh dung
|
|
EXPECT_DOUBLE_EQ(o.v, 0.0);
|
|
EXPECT_NE(s.t.halt_reason().find("lech khoi duong"), std::string::npos);
|
|
EXPECT_FALSE(s.Step().command); // roi im
|
|
}
|
|
|
|
TEST(SlopeTest, MatDinhViThiDungHan) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{0.5, 0.2, 0.0};
|
|
s.t.SetPath(p, true);
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDrive, 2000));
|
|
s.Step(false, false, false);
|
|
EXPECT_EQ(s.t.phase(), SlopeTestPhase::kHalted);
|
|
EXPECT_NE(s.t.halt_reason().find("mat dinh vi"), std::string::npos);
|
|
}
|
|
|
|
TEST(SlopeTest, PoseNhayThiDungHan) {
|
|
Sim s(Cfg());
|
|
SlopePath p;
|
|
std::string why;
|
|
ASSERT_TRUE(PlanSlopePath(0.5, 0.2, Floor, Free, SlopePathConfig{}, &p, &why));
|
|
s.p = Pose2D{0.5, 0.2, 0.0};
|
|
s.t.SetPath(p, true);
|
|
ASSERT_TRUE(s.RunUntil(SlopeTestPhase::kDrive, 2000));
|
|
s.Step();
|
|
s.p.x += 0.5; // dinh vi nhay 0.5 m
|
|
s.Step();
|
|
EXPECT_EQ(s.t.phase(), SlopeTestPhase::kHalted);
|
|
EXPECT_NE(s.t.halt_reason().find("pose nhay"), std::string::npos);
|
|
}
|