upload
This commit is contained in:
301
test/test_teleop_model.cc
Normal file
301
test/test_teleop_model.cc
Normal file
@@ -0,0 +1,301 @@
|
||||
#include "vslam/teleop_model.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace vslam;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Vector {
|
||||
std::string tag;
|
||||
double lin, ang, joy_h, joy_v, lin_speed, ang_speed;
|
||||
};
|
||||
|
||||
std::vector<Vector> LoadVectors() {
|
||||
// Duong dan do CMake truyen vao, khong hardcode.
|
||||
const char* dir = std::getenv("VSLAM_TEST_DATA_DIR");
|
||||
const std::string path =
|
||||
std::string(dir != nullptr ? dir : "data") + "/teleop_vectors.txt";
|
||||
|
||||
std::vector<Vector> out;
|
||||
std::ifstream f(path);
|
||||
if (!f) return out;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(f, line)) {
|
||||
if (line.empty() || line[0] == '#') continue;
|
||||
std::istringstream ss(line);
|
||||
Vector v;
|
||||
ss >> v.tag >> v.lin >> v.ang >> v.joy_h >> v.joy_v >> v.lin_speed >> v.ang_speed;
|
||||
out.push_back(v);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Chay CUNG mot chuoi thao tac nhu gen_teleop_vectors.py.
|
||||
// Hai ham nay phai giu dong bo voi nhau -- do la toan bo y nghia cua bai test.
|
||||
std::vector<Vector> RunCppSequence() {
|
||||
std::vector<Vector> out;
|
||||
TeleopModel m;
|
||||
|
||||
auto emit = [&](const char* tag) {
|
||||
const auto c = m.Command();
|
||||
const auto j = m.JoystickPosition();
|
||||
out.push_back(Vector{tag, c.first, c.second, j.first, j.second,
|
||||
m.linear_speed(), m.angular_speed()});
|
||||
};
|
||||
|
||||
emit("init");
|
||||
|
||||
const TeleopKey keys[] = {TeleopKey::kUp, TeleopKey::kDown, TeleopKey::kLeft,
|
||||
TeleopKey::kRight};
|
||||
const char* down_tags[] = {"key_down_Up", "key_down_Down", "key_down_Left",
|
||||
"key_down_Right"};
|
||||
const char* up_tags[] = {"key_up_Up", "key_up_Down", "key_up_Left", "key_up_Right"};
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
m.SetKey(keys[i], true);
|
||||
emit(down_tags[i]);
|
||||
m.SetKey(keys[i], false);
|
||||
emit(up_tags[i]);
|
||||
}
|
||||
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
m.SetKey(TeleopKey::kLeft, true);
|
||||
emit("up_left");
|
||||
m.SetKey(TeleopKey::kDown, true);
|
||||
emit("up_left_down");
|
||||
m.ClearKeys();
|
||||
emit("clear_keys");
|
||||
|
||||
const double hv[][2] = {{0.0, -1.0}, {0.0, 1.0}, {-1.0, 0.0}, {1.0, 0.0},
|
||||
{0.5, -0.5}, {-0.3, 0.7}, {2.0, -2.0}, {-2.0, 2.0}};
|
||||
const char* joy_tags[] = {"joy_0.0_-1.0", "joy_0.0_1.0", "joy_-1.0_0.0",
|
||||
"joy_1.0_0.0", "joy_0.5_-0.5", "joy_-0.3_0.7",
|
||||
"joy_2.0_-2.0", "joy_-2.0_2.0"};
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
m.SetJoystick(hv[i][0], hv[i][1]);
|
||||
emit(joy_tags[i]);
|
||||
}
|
||||
m.ReleaseJoystick();
|
||||
emit("joy_release");
|
||||
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
emit("key_then");
|
||||
m.SetJoystick(0.0, -1.0);
|
||||
emit("joy_over_key");
|
||||
m.ReleaseJoystick();
|
||||
emit("joy_released_key_remains");
|
||||
m.ClearKeys();
|
||||
|
||||
for (int i = 0; i < 5; ++i) { m.AdjustLinearSpeed(+1); emit("lin_up"); }
|
||||
for (int i = 0; i < 12; ++i) { m.AdjustLinearSpeed(-1); emit("lin_down"); }
|
||||
for (int i = 0; i < 3; ++i) { m.AdjustAngularSpeed(+1); emit("ang_up"); }
|
||||
for (int i = 0; i < 20; ++i) { m.AdjustAngularSpeed(-1); emit("ang_down"); }
|
||||
|
||||
m.SetLinearSpeed(1.5); emit("set_lin_1.5");
|
||||
m.SetLinearSpeed(99.0); emit("set_lin_99");
|
||||
m.SetAngularSpeed(-5.0); emit("set_ang_-5");
|
||||
|
||||
{
|
||||
const double lin = 0.25;
|
||||
m.SetManualCommand(&lin, nullptr);
|
||||
emit("manual_lin");
|
||||
}
|
||||
{
|
||||
const double ang = -0.4;
|
||||
m.SetManualCommand(nullptr, &ang);
|
||||
emit("manual_ang");
|
||||
}
|
||||
m.SetJoystick(0.0, -1.0);
|
||||
emit("joy_clears_manual");
|
||||
{
|
||||
const double lin = 0.1, ang = 0.1;
|
||||
m.SetManualCommand(&lin, &ang);
|
||||
emit("manual_over_joy");
|
||||
}
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
emit("key_clears_manual");
|
||||
m.ClearMotion();
|
||||
emit("clear_motion");
|
||||
|
||||
m.SetMaxLinearSpeed(0.5); emit("max_lin_0.5");
|
||||
m.SetMaxAngularSpeed(0.3); emit("max_ang_0.3");
|
||||
m.SetMinLinearCommand(-0.2); emit("min_lin_cmd");
|
||||
m.SetMinAngularCommand(-0.1);emit("min_ang_cmd");
|
||||
m.SetJoystick(1.0, 1.0); emit("joy_at_limits");
|
||||
{
|
||||
const double lin = -99.0, ang = 99.0;
|
||||
m.SetManualCommand(&lin, &ang);
|
||||
emit("manual_clamped");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ===========================================================================
|
||||
// DAY LA BAI KIEM QUAN TRONG NHAT CUA FILE.
|
||||
//
|
||||
// teleop_model.py ghi o dau file: "Logic giu NGUYEN VEN, khong sua mot dong
|
||||
// nao." Doc hai file roi gat dau thi khong phai kiem chung. Bai nay chay CUNG
|
||||
// mot chuoi thao tac tren ca hai ban va so tung so.
|
||||
//
|
||||
// Vector duoc sinh boi test/gen_teleop_vectors.py tu chinh ban Python goc.
|
||||
// Doi mo hinh -> chay lai script -> diff se chi ra dung cho doi.
|
||||
// ===========================================================================
|
||||
TEST(TeleopModel, KhopTungSoVoiBanPythonGoc) {
|
||||
const std::vector<Vector> want = LoadVectors();
|
||||
ASSERT_FALSE(want.empty())
|
||||
<< "khong nap duoc teleop_vectors.txt -- chay test/gen_teleop_vectors.py";
|
||||
|
||||
const std::vector<Vector> got = RunCppSequence();
|
||||
ASSERT_EQ(got.size(), want.size()) << "chuoi thao tac hai ben da lech nhau";
|
||||
|
||||
for (size_t i = 0; i < want.size(); ++i) {
|
||||
EXPECT_EQ(got[i].tag, want[i].tag) << "buoc " << i;
|
||||
EXPECT_NEAR(got[i].lin, want[i].lin, 1e-9) << "buoc " << i << " (" << want[i].tag << ") linear";
|
||||
EXPECT_NEAR(got[i].ang, want[i].ang, 1e-9) << "buoc " << i << " (" << want[i].tag << ") angular";
|
||||
EXPECT_NEAR(got[i].joy_h, want[i].joy_h, 1e-9) << "buoc " << i << " joy_h";
|
||||
EXPECT_NEAR(got[i].joy_v, want[i].joy_v, 1e-9) << "buoc " << i << " joy_v";
|
||||
EXPECT_NEAR(got[i].lin_speed, want[i].lin_speed, 1e-9) << "buoc " << i << " linear_speed";
|
||||
EXPECT_NEAR(got[i].ang_speed, want[i].ang_speed, 1e-9) << "buoc " << i << " angular_speed";
|
||||
}
|
||||
}
|
||||
|
||||
// --- Hanh vi rieng le ------------------------------------------------------
|
||||
|
||||
TEST(TeleopModel, ThuTuUuTienManualJoystickPhim) {
|
||||
TeleopModel m;
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
EXPECT_NEAR(m.Command().first, 0.30, 1e-9);
|
||||
|
||||
// Joystick de len phim.
|
||||
m.SetJoystick(0.0, -1.0);
|
||||
EXPECT_TRUE(m.joystick_active());
|
||||
EXPECT_NEAR(m.Command().first, 0.30, 1e-9);
|
||||
|
||||
// Manual de len ca hai, VA xoa chung.
|
||||
const double lin = 0.1;
|
||||
m.SetManualCommand(&lin, nullptr);
|
||||
EXPECT_TRUE(m.manual_control_active());
|
||||
EXPECT_FALSE(m.joystick_active());
|
||||
EXPECT_FALSE(m.key_pressed(TeleopKey::kUp));
|
||||
EXPECT_NEAR(m.Command().first, 0.1, 1e-9);
|
||||
|
||||
// Bam phim xoa manual.
|
||||
m.SetKey(TeleopKey::kDown, true);
|
||||
EXPECT_FALSE(m.manual_control_active());
|
||||
EXPECT_NEAR(m.Command().first, -0.30, 1e-9);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, UpVaDownTrietTieuNhau) {
|
||||
TeleopModel m;
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
m.SetKey(TeleopKey::kDown, true);
|
||||
EXPECT_NEAR(m.Command().first, 0.0, 1e-12);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, JoystickKepVaoDaiDonVi) {
|
||||
TeleopModel m;
|
||||
m.SetJoystick(5.0, -5.0);
|
||||
const auto p = m.JoystickPosition();
|
||||
EXPECT_NEAR(p.first, 1.0, 1e-12);
|
||||
EXPECT_NEAR(p.second, -1.0, 1e-12);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, ChinhTocDoKepGiuaMinVaMax) {
|
||||
TeleopModel m;
|
||||
for (int i = 0; i < 100; ++i) m.AdjustLinearSpeed(+1);
|
||||
EXPECT_NEAR(m.linear_speed(), m.max_linear_speed(), 1e-12);
|
||||
for (int i = 0; i < 200; ++i) m.AdjustLinearSpeed(-1);
|
||||
EXPECT_NEAR(m.linear_speed(), 0.05, 1e-12); // min_linear_speed
|
||||
}
|
||||
|
||||
TEST(TeleopModel, GiamMaxThiKeoTocDoHienTaiXuongTheo) {
|
||||
TeleopModel m;
|
||||
m.SetLinearSpeed(1.5);
|
||||
m.SetMaxLinearSpeed(0.4);
|
||||
EXPECT_NEAR(m.linear_speed(), 0.4, 1e-12);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, GioiHanLuiKhacGioiHanTien) {
|
||||
// min_linear_command tach rieng de gioi han lui chat hon tien.
|
||||
TeleopModel m;
|
||||
m.SetMaxLinearSpeed(1.0);
|
||||
m.SetMinLinearCommand(-0.2);
|
||||
m.SetLinearSpeed(1.0);
|
||||
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
EXPECT_NEAR(m.Command().first, 1.0, 1e-9);
|
||||
m.SetKey(TeleopKey::kUp, false);
|
||||
m.SetKey(TeleopKey::kDown, true);
|
||||
EXPECT_NEAR(m.Command().first, -0.2, 1e-9) << "lui phai bi kep chat hon";
|
||||
}
|
||||
|
||||
TEST(TeleopModel, ClearMotionDuaVeKhong) {
|
||||
TeleopModel m;
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
m.SetJoystick(1.0, -1.0);
|
||||
m.ClearMotion();
|
||||
EXPECT_NEAR(m.Command().first, 0.0, 1e-12);
|
||||
EXPECT_NEAR(m.Command().second, 0.0, 1e-12);
|
||||
EXPECT_FALSE(m.joystick_active());
|
||||
EXPECT_FALSE(m.manual_control_active());
|
||||
}
|
||||
|
||||
TEST(TeleopModel, PhimMuiTenCungLamKnobDich) {
|
||||
// De nguoi lai thay duoc minh dang bam gi.
|
||||
TeleopModel m;
|
||||
m.SetKey(TeleopKey::kUp, true);
|
||||
EXPECT_NEAR(m.JoystickPosition().second, -1.0, 1e-12);
|
||||
m.SetKey(TeleopKey::kUp, false);
|
||||
m.SetKey(TeleopKey::kRight, true);
|
||||
EXPECT_NEAR(m.JoystickPosition().first, 1.0, 1e-12);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, TuChoiCauHinhVoLy) {
|
||||
// Mot gioi han van toc sai la mot robot chay nhanh hon nguoi lai nghi.
|
||||
TeleopConfig c;
|
||||
c.linear_step = 0.0;
|
||||
EXPECT_THROW(TeleopModel{c}, std::invalid_argument);
|
||||
|
||||
c = TeleopConfig();
|
||||
c.min_linear_speed = -1.0;
|
||||
EXPECT_THROW(TeleopModel{c}, std::invalid_argument);
|
||||
|
||||
c = TeleopConfig();
|
||||
c.max_linear_speed = 0.01; // < min
|
||||
EXPECT_THROW(TeleopModel{c}, std::invalid_argument);
|
||||
|
||||
c = TeleopConfig();
|
||||
c.has_min_linear_command = true;
|
||||
c.min_linear_command = 0.5; // phai <= 0
|
||||
EXPECT_THROW(TeleopModel{c}, std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, TuChoiGiaTriKhongHuuHan) {
|
||||
TeleopModel m;
|
||||
EXPECT_THROW(m.SetJoystick(std::nan(""), 0.0), std::invalid_argument);
|
||||
EXPECT_THROW(m.SetLinearSpeed(std::nan("")), std::invalid_argument);
|
||||
const double bad = std::nan("");
|
||||
EXPECT_THROW(m.SetManualCommand(&bad, nullptr), std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(TeleopModel, ParseTenPhim) {
|
||||
TeleopKey k;
|
||||
EXPECT_TRUE(ParseTeleopKey("Up", &k));
|
||||
EXPECT_EQ(k, TeleopKey::kUp);
|
||||
EXPECT_TRUE(ParseTeleopKey("Right", &k));
|
||||
EXPECT_EQ(k, TeleopKey::kRight);
|
||||
EXPECT_FALSE(ParseTeleopKey("Space", &k));
|
||||
EXPECT_FALSE(ParseTeleopKey("w", &k));
|
||||
EXPECT_STREQ(TeleopKeyName(TeleopKey::kDown), "Down");
|
||||
}
|
||||
Reference in New Issue
Block a user