284 lines
8.6 KiB
C++
284 lines
8.6 KiB
C++
// Cau noi WT61PC qua mot cap pty: phia slave la "cong serial" ma driver mo, phia
|
|
// master la "cam bien" ghi khung vao. Chay duoc ma khong can phan cung, va di
|
|
// qua dung termios/read cua driver that.
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <sys/ioctl.h>
|
|
#include <unistd.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <cmath>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "vslam/clock.h"
|
|
#include "vslam/driver_config.h"
|
|
#include "vslam/driver_interfaces.h"
|
|
|
|
using namespace vslam;
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace {
|
|
|
|
constexpr double kG = 9.80665;
|
|
constexpr double kDeg = M_PI / 180.0;
|
|
|
|
std::vector<uint8_t> MakeFrame(uint8_t type, int16_t x, int16_t y, int16_t z) {
|
|
auto lo = [](int16_t v) {
|
|
return static_cast<uint8_t>(static_cast<uint16_t>(v) & 0xFF);
|
|
};
|
|
auto hi = [](int16_t v) {
|
|
return static_cast<uint8_t>((static_cast<uint16_t>(v) >> 8) & 0xFF);
|
|
};
|
|
std::vector<uint8_t> f = {0x55, type, lo(x), hi(x), lo(y), hi(y),
|
|
lo(z), hi(z), 0x00, 0x00, 0x00};
|
|
uint8_t sum = 0;
|
|
for (size_t i = 0; i < 10; ++i) sum = static_cast<uint8_t>(sum + f[i]);
|
|
f[10] = sum;
|
|
return f;
|
|
}
|
|
|
|
std::vector<uint8_t> Cat(std::initializer_list<std::vector<uint8_t>> parts) {
|
|
std::vector<uint8_t> out;
|
|
for (const auto& p : parts) out.insert(out.end(), p.begin(), p.end());
|
|
return out;
|
|
}
|
|
|
|
class FakePort {
|
|
public:
|
|
FakePort() {
|
|
master_ = ::posix_openpt(O_RDWR | O_NOCTTY);
|
|
if (master_ < 0) return;
|
|
char name[128] = {};
|
|
if (::grantpt(master_) != 0 || ::unlockpt(master_) != 0 ||
|
|
::ptsname_r(master_, name, sizeof(name)) != 0) {
|
|
::close(master_);
|
|
master_ = -1;
|
|
return;
|
|
}
|
|
slave_ = name;
|
|
}
|
|
~FakePort() {
|
|
if (master_ >= 0) ::close(master_);
|
|
}
|
|
|
|
bool ok() const { return master_ >= 0; }
|
|
const std::string& slave() const { return slave_; }
|
|
|
|
void Write(const std::vector<uint8_t>& bytes) {
|
|
size_t off = 0;
|
|
while (off < bytes.size()) {
|
|
const ssize_t n = ::write(master_, bytes.data() + off, bytes.size() - off);
|
|
if (n <= 0) break;
|
|
off += static_cast<size_t>(n);
|
|
}
|
|
}
|
|
|
|
private:
|
|
int master_ = -1;
|
|
std::string slave_;
|
|
};
|
|
|
|
class ImuCollector {
|
|
public:
|
|
void Add(const Imu& imu) {
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
list_.push_back(imu);
|
|
}
|
|
cv_.notify_all();
|
|
}
|
|
bool WaitFor(size_t n, std::chrono::milliseconds timeout) {
|
|
std::unique_lock<std::mutex> lk(mu_);
|
|
return cv_.wait_for(lk, timeout, [&] { return list_.size() >= n; });
|
|
}
|
|
std::vector<Imu> Get() const {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
return list_;
|
|
}
|
|
|
|
private:
|
|
mutable std::mutex mu_;
|
|
std::condition_variable cv_;
|
|
std::vector<Imu> list_;
|
|
};
|
|
|
|
McuConfig MakeConfig(const std::string& port) {
|
|
McuConfig c;
|
|
c.protocol = McuProtocol::kWt61pc;
|
|
c.port = port;
|
|
c.baud = 9600;
|
|
c.imu_lag_sec = 0.0;
|
|
return c;
|
|
}
|
|
|
|
// Kernel/pty khong chac ap TIOCEXCL (root thi khong bao gio bi chan). Tu kiem
|
|
// truoc de test khong bao dong gia.
|
|
bool PtyHonoursExcl(const std::string& slave) {
|
|
const int a = ::open(slave.c_str(), O_RDWR | O_NOCTTY);
|
|
if (a < 0) return false;
|
|
bool honoured = false;
|
|
if (::ioctl(a, TIOCEXCL) == 0) {
|
|
errno = 0;
|
|
const int b = ::open(slave.c_str(), O_RDWR | O_NOCTTY);
|
|
honoured = (b < 0 && errno == EBUSY);
|
|
if (b >= 0) ::close(b);
|
|
::ioctl(a, TIOCNXCL);
|
|
}
|
|
::close(a);
|
|
return honoured;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(Wt61pcBridge, PhatNgayKhiCoKhungGyroKhongDoiAngle) {
|
|
FakePort port;
|
|
if (!port.ok()) GTEST_SKIP() << "khong tao duoc pty (container khong co /dev/ptmx?)";
|
|
auto bridge = CreateMcuBridge(MakeConfig(port.slave()), MakeSystemClock());
|
|
ImuCollector got;
|
|
bridge->SetImuCallback([&](ImuPtr imu) { got.Add(*imu); });
|
|
ASSERT_TRUE(bridge->Open().ok());
|
|
|
|
// Chi Acc + Gyro, khong gui Angle: phai ra dung mot mau.
|
|
port.Write(Cat({MakeFrame(0x51, 0, 0, 2048), MakeFrame(0x52, 0, 0, 16384)}));
|
|
ASSERT_TRUE(got.WaitFor(1, 2000ms));
|
|
|
|
const auto v = got.Get();
|
|
ASSERT_EQ(v.size(), 1u);
|
|
EXPECT_NEAR(v[0].linear_acceleration.z, kG, 1e-9);
|
|
EXPECT_NEAR(v[0].angular_velocity.z, 1000.0 * kDeg, 1e-9);
|
|
// Khong mang orientation.
|
|
EXPECT_LT(v[0].orientation_covariance[0], 0.0);
|
|
EXPECT_TRUE(bridge->Stats().connected);
|
|
bridge->Close();
|
|
}
|
|
|
|
TEST(Wt61pcBridge, GyroKhongCoGiaTocDiTruocThiBoMau) {
|
|
FakePort port;
|
|
if (!port.ok()) GTEST_SKIP() << "khong tao duoc pty (container khong co /dev/ptmx?)";
|
|
auto bridge = CreateMcuBridge(MakeConfig(port.slave()), MakeSystemClock());
|
|
ImuCollector got;
|
|
bridge->SetImuCallback([&](ImuPtr imu) { got.Add(*imu); });
|
|
ASSERT_TRUE(bridge->Open().ok());
|
|
|
|
// Gyro lac loai, hoac Angle don doc: khong duoc ghep voi gia toc cu.
|
|
port.Write(Cat({MakeFrame(0x52, 0, 0, 16384), MakeFrame(0x53, 1, 2, 3)}));
|
|
EXPECT_FALSE(got.WaitFor(1, 400ms));
|
|
|
|
// Chu ky day du sau do thi ra mau.
|
|
port.Write(Cat({MakeFrame(0x51, 0, 0, 2048), MakeFrame(0x52, 0, 0, 16384)}));
|
|
EXPECT_TRUE(got.WaitFor(1, 2000ms));
|
|
bridge->Close();
|
|
}
|
|
|
|
TEST(Wt61pcBridge, DauThoiGianKhongLuiVaTruImuLag) {
|
|
FakePort port;
|
|
if (!port.ok()) GTEST_SKIP() << "khong tao duoc pty (container khong co /dev/ptmx?)";
|
|
McuConfig cfg = MakeConfig(port.slave());
|
|
cfg.imu_lag_sec = 0.2;
|
|
auto clock = MakeSystemClock();
|
|
auto bridge = CreateMcuBridge(cfg, clock);
|
|
|
|
std::mutex mu;
|
|
std::vector<double> stamps;
|
|
std::vector<double> seen_at;
|
|
ImuCollector got;
|
|
bridge->SetImuCallback([&](ImuPtr imu) {
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu);
|
|
stamps.push_back(imu->header.stamp.toSec());
|
|
seen_at.push_back(clock->Now().toSec());
|
|
}
|
|
got.Add(*imu);
|
|
});
|
|
ASSERT_TRUE(bridge->Open().ok());
|
|
|
|
// Nam chu ky lien nhau trong mot lan ghi: co the vao cung mot lan read().
|
|
std::vector<uint8_t> burst;
|
|
for (int i = 0; i < 5; ++i) {
|
|
const auto c = Cat({MakeFrame(0x51, 0, 0, 2048), MakeFrame(0x52, 0, 0, 16384),
|
|
MakeFrame(0x53, 0, 0, 0)});
|
|
burst.insert(burst.end(), c.begin(), c.end());
|
|
}
|
|
port.Write(burst);
|
|
ASSERT_TRUE(got.WaitFor(5, 3000ms));
|
|
bridge->Close();
|
|
|
|
std::lock_guard<std::mutex> lk(mu);
|
|
ASSERT_GE(stamps.size(), 5u);
|
|
for (size_t i = 1; i < stamps.size(); ++i) {
|
|
EXPECT_GE(stamps[i], stamps[i - 1]) << "stamp lui o mau " << i;
|
|
}
|
|
// Moi stamp phai cu hon luc nhan it nhat imu_lag_sec (0.2 s), va khong cu hon
|
|
// qua mot khoang vo ly.
|
|
for (size_t i = 0; i < stamps.size(); ++i) {
|
|
EXPECT_LE(stamps[i], seen_at[i] - 0.2 + 1e-3) << "mau " << i;
|
|
EXPECT_GE(stamps[i], seen_at[i] - 0.2 - 1.0) << "mau " << i;
|
|
}
|
|
}
|
|
|
|
TEST(Wt61pcBridge, ImLangThiBaoLoiVaHetLoiKhiCoDuLieu) {
|
|
FakePort port;
|
|
if (!port.ok()) GTEST_SKIP() << "khong tao duoc pty (container khong co /dev/ptmx?)";
|
|
McuConfig cfg = MakeConfig(port.slave());
|
|
cfg.no_data_warn_sec = 0.3;
|
|
auto bridge = CreateMcuBridge(cfg, MakeSystemClock());
|
|
ImuCollector got;
|
|
bridge->SetImuCallback([&](ImuPtr imu) { got.Add(*imu); });
|
|
ASSERT_TRUE(bridge->Open().ok());
|
|
|
|
std::this_thread::sleep_for(800ms);
|
|
EXPECT_FALSE(bridge->Stats().connected);
|
|
EXPECT_NE(bridge->Stats().last_error.find("khong co khung"), std::string::npos)
|
|
<< bridge->Stats().last_error;
|
|
|
|
port.Write(Cat({MakeFrame(0x51, 0, 0, 2048), MakeFrame(0x52, 0, 0, 16384)}));
|
|
ASSERT_TRUE(got.WaitFor(1, 2000ms));
|
|
EXPECT_TRUE(bridge->Stats().connected);
|
|
bridge->Close();
|
|
}
|
|
|
|
TEST(Wt61pcBridge, CongKhongTonTaiLaLoiUnavailable) {
|
|
McuConfig cfg = MakeConfig("/dev/khong_co_imu_nay");
|
|
auto bridge = CreateMcuBridge(cfg, MakeSystemClock());
|
|
const StatusResponse st = bridge->Open();
|
|
ASSERT_FALSE(st.ok());
|
|
EXPECT_EQ(st.code, StatusCode::kUnavailable);
|
|
EXPECT_NE(st.message.find("/dev/khong_co_imu_nay"), std::string::npos) << st.message;
|
|
}
|
|
|
|
TEST(Wt61pcBridge, BaudKhongHoTroBiTuChoi) {
|
|
FakePort port;
|
|
if (!port.ok()) GTEST_SKIP() << "khong tao duoc pty (container khong co /dev/ptmx?)";
|
|
McuConfig cfg = MakeConfig(port.slave());
|
|
cfg.baud = 12345;
|
|
auto bridge = CreateMcuBridge(cfg, MakeSystemClock());
|
|
const StatusResponse st = bridge->Open();
|
|
ASSERT_FALSE(st.ok());
|
|
EXPECT_EQ(st.code, StatusCode::kInvalidArgument);
|
|
}
|
|
|
|
TEST(Wt61pcBridge, DocQuyenCongChuongTrinhThuHaiBiEbusy) {
|
|
FakePort port;
|
|
if (!port.ok()) GTEST_SKIP() << "khong tao duoc pty (container khong co /dev/ptmx?)";
|
|
if (!PtyHonoursExcl(port.slave())) {
|
|
GTEST_SKIP() << "pty/kernel/quyen root nay khong ap TIOCEXCL";
|
|
}
|
|
auto bridge = CreateMcuBridge(MakeConfig(port.slave()), MakeSystemClock());
|
|
ASSERT_TRUE(bridge->Open().ok());
|
|
|
|
errno = 0;
|
|
const int fd = ::open(port.slave().c_str(), O_RDWR | O_NOCTTY);
|
|
EXPECT_LT(fd, 0);
|
|
EXPECT_EQ(errno, EBUSY);
|
|
if (fd >= 0) ::close(fd);
|
|
bridge->Close();
|
|
}
|