diff --git a/PROJECT_CONTEXT.md b/PROJECT_CONTEXT.md index 78e5020..9adb387 100644 --- a/PROJECT_CONTEXT.md +++ b/PROJECT_CONTEXT.md @@ -60,7 +60,7 @@ Use this file for verified target-repository facts only. Keep unknown values as - Cameras: RealSense D410 (depth + IR, **no RGB** — left IR doubles as the "rgb" channel for SLAM). 640x480 @ 30 fps, `decimate: 2` with CameraInfo still carrying full-res K (consumers must compensate). Driver `realsense_driver.cc`, built only with `-DVSLAM_WITH_REALSENSE=ON`; sensors.yaml states it is **not yet verified against real hardware**. - LiDAR: LakiBeam 1L over UDP, bind 0.0.0.0:2368, expected 25 Hz, 270° FOV, range 0.05–40.0 m. Angle convention `theta = (flip ? -alpha : alpha) + angle_offset_deg` with `angle_offset_deg: 178.75`, `angle_flip: true` (sensors.yaml:52-53; the 1.25 deg off 180 is a MEASURED mount error, and the driver logs a suggestion line every 10 s that reads as a no-op when the value is already right). -- IMU: WT61PC wired straight to a UART (USB-FTDI adapter, `/dev/imu`, `mcu.protocol: wt61pc`), 9600 baud, 10 Hz by default. Stamped from the host clock minus `imu_lag_sec` (no `ClockSync`). The older ESP32-S3 path (`mcu.protocol: esp`, `/dev/ttyACM0`, `ClockSync`, `McuTimeUnwrapper` for uint32 `micros()` wrap every 71.58 min) is still supported. +- IMU: WT61PC wired straight to a UART (USB-FTDI adapter, `/dev/imu`, `mcu.protocol: wt61pc`), 115200 baud / 200 Hz (stored in the sensor 2026-10-06; factory default 9600 baud / 10 Hz), FTDI latency_timer 1 ms via `configuration_files/udev/99-imu.rules`. Stamped from the host clock minus `imu_lag_sec` (no `ClockSync`). The older ESP32-S3 path (`mcu.protocol: esp`, `/dev/ttyACM0`, `ClockSync`, `McuTimeUnwrapper` for uint32 `micros()` wrap every 71.58 min) is still supported. - Compute target: Jetson AGX (arm64); jetson preset uses `CMAKE_PREFIX_PATH=/opt/vslam` and `CMAKE_INSTALL_PREFIX=/opt/vslam`, deps built from source (tools/build_deps.sh referenced by CMakePresets.json:40 — **script not present in repo**). - Accelerators: none used. No CUDA/TensorRT/OpenCL in the build; all processing is CPU. - Actuators or robot base: ZLAC8015D dual motor driver over **CANopen on `can0`, node id 1** (not serial), via `liblely-coapp`, gated by `-DVSLAM_WITH_CANOPEN=ON`. DCF at `configuration_files/master.dcf`, path resolved relative to the config dir. `encoder_counts_per_rev: 0` = fall back to 16384 (4096 "wire" x4); the x4 factor is **confirmed** by field data (2026-09-23, see Verification Notes). diff --git a/configuration_files/sensors.yaml b/configuration_files/sensors.yaml index c2be52f..665d1c1 100644 --- a/configuration_files/sensors.yaml +++ b/configuration_files/sensors.yaml @@ -178,13 +178,17 @@ mcu: # duoi CHI co nghia voi `esp`. protocol: wt61pc - # wt61pc: /dev/imu do luat udev tao (cap FTDI 0403:6001 -> SYMLINK+="imu"); - # 9600 la baud mac dinh cua cam bien. Cong duoc KHOA DOC QUYEN: chuong trinh - # khac (imu_ros_node cua motor_control) mo cong sau se loi EBUSY. + # wt61pc: /dev/imu do luat udev tao (configuration_files/udev/99-imu.rules: cap + # FTDI 0403:6001 -> SYMLINK+="imu", latency_timer 1 ms). Cam bien DA DOI + # sang 115200 baud / 200 Hz (2026-10-06, luu trong cam bien; xuat xuong + # 9600 / 10 Hz). Day la baud HOST nghe, phai trung baud da luu trong cam + # bien: sai thi IMU im, log "checksum sai". Cong duoc KHOA DOC QUYEN: + # chuong trinh khac (imu_ros_node cua motor_control) mo cong sau se loi + # EBUSY. # esp: /dev/ttyACM0 @ 115200 theo firmware that. Gia tri cu la 921600 -- # doc sai baud cho ra RAC chu khong cho ra loi, nen no hong im lang. port: /dev/imu - baud: 9600 + baud: 115200 # ClockSync khop micros() cua MCU voi dong ho host bang fit tuyen tinh. # @@ -259,7 +263,14 @@ mcu: # ⚠️ 0.07 do duoc tren duong ESP32 + USB-CDC. Voi protocol: wt61pc (UART # thang, khong ESP32) duong truyen khac, nen DO LAI bang quy trinh o tren # truoc khi tin con so nay. - imu_lag_sec: 0.07 + # + # --- 2026-10-06: 115200 baud / 200 Hz -> 0.01 la UOC LUONG, CHUA DO --- + # ~2 ms day 2 khung Acc+Gyro qua day 115200 baud + # ~3-10 ms loc / trung binh noi bo o che do 200 Hz (o 10 Hz la ~50 ms) + # ~1 ms USB (latency_timer 1 ms, xem luat udev) + # Giu 0.07 o 200 Hz la lech gyro ~60 ms. Do lai theo quy trinh tren: may do + # co bin 10 ms, nen lan chay sau bao ~0 ms la dat. + imu_lag_sec: 0.01 # Ban ghi encoder `E,,,` la MO RONG moi -- giao # thuc cu chi co I/B/P. diff --git a/configuration_files/udev/99-imu.rules b/configuration_files/udev/99-imu.rules new file mode 100644 index 0000000..ea00843 --- /dev/null +++ b/configuration_files/udev/99-imu.rules @@ -0,0 +1,15 @@ +# /dev/imu cho IMU WT61PC qua cap USB-FTDI (0403:6001) -- sensors.yaml `mcu.port`. +# +# latency_timer=1: cap FTDI day byte len host moi 1 ms thay vi 16 ms (mac dinh). +# O 200 Hz (5 ms/mau) 16 ms la ~3 mau den cung mot luc, stamp IMU giat. +# Wt61pcBridge log `imu: latency_timer cap FTDI = N ms` luc mo cong. +# +# Nguon: goi imu_uart cua namnc (ROS 2), 2026-10-06. Cai tren Jetson, mot lan: +# sudo cp configuration_files/udev/99-imu.rules /etc/udev/rules.d/ +# sudo udevadm control --reload-rules && sudo udevadm trigger +# ls -l /dev/imu # -> ttyUSBn +# cat /sys/bus/usb-serial/devices/ttyUSB*/latency_timer # phai ra 1 +# +# Luat khop MOI cap FTDI 0403:6001: cam them mot cap cung loai thi ca hai deu +# tranh ten /dev/imu. +KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0666", GROUP="robotics", SYMLINK+="imu", ATTR{latency_timer}="1", RUN+="/bin/chmod 777 /dev/%k" diff --git a/libs/vslam_drivers/src/wt61pc_bridge.cc b/libs/vslam_drivers/src/wt61pc_bridge.cc index 4e11c07..89e5512 100644 --- a/libs/vslam_drivers/src/wt61pc_bridge.cc +++ b/libs/vslam_drivers/src/wt61pc_bridge.cc @@ -10,7 +10,10 @@ // ho host. O day khong co MCU, nen stamp lay tu dong ho HOST luc nhan xong // khung gyro. De jitter khong chui thang vao stamp: cap USB-FTDI gom byte // theo latency_timer (mac dinh 16 ms), nen mot lan read() co the mang nhieu -// khung. Thoi diem cua mot khung duoc suy NGUOC tu luc read() tra ve, tru +// khung. Driver bat ASYNC_LOW_LATENCY (ftdi_sio -> 1 ms) va luat udev +// configuration_files/udev/99-imu.rules dat san 1 ms: o 200 Hz (5 ms/mau) +// 16 ms la ~3 mau mot lan doc. Log luc mo cong bao gia tri that. +// Thoi diem cua mot khung duoc suy NGUOC tu luc read() tra ve, tru // di thoi gian truyen cua cac byte toi sau no trong cung lan doc. // Phan tre con lai (loc noi bo cua cam bien + thoi gian truyen khung) la // HANG SO, tru bang `mcu.imu_lag_sec`. Con so 0.07 do duoc tren duong ESP32; @@ -39,13 +42,17 @@ #include #include +#include #include #include #include #include #include +#include +#include #include +#include #include #include #include @@ -74,6 +81,28 @@ speed_t BaudConstant(uint32_t baud) { } } +// Cap FTDI gom byte latency_timer ms roi moi day len host (mac dinh 16 ms; o +// 200 Hz la ~3 mau mot lan). ASYNC_LOW_LATENCY -> ftdi_sio dat 1 ms. Cong khong +// phai FTDI (pty khi test) thi ioctl loi -> bo qua. +void SetLowLatency(int fd) { + serial_struct ss{}; + if (::ioctl(fd, TIOCGSERIAL, &ss) != 0) return; + ss.flags |= ASYNC_LOW_LATENCY; + ::ioctl(fd, TIOCSSERIAL, &ss); +} + +// latency_timer (ms) cua cap FTDI sau `port` (/dev/imu -> ttyUSB0); -1 = khong +// doc duoc (khong phai FTDI, hoac khong co sysfs). +int FtdiLatencyMs(const std::string& port) { + char real[PATH_MAX]; + if (::realpath(port.c_str(), real) == nullptr) return -1; + const char* name = std::strrchr(real, '/'); + std::ifstream f(std::string("/sys/class/tty/") + (name != nullptr ? name + 1 : real) + + "/device/latency_timer"); + int ms = -1; + return (f >> ms) ? ms : -1; +} + class Wt61pcBridge : public McuInterface { public: Wt61pcBridge(const McuConfig& cfg, ClockPtr clock) @@ -112,6 +141,13 @@ class Wt61pcBridge : public McuInterface { << ", doc quyen cong. Stamp lay tu dong ho HOST luc nhan xong " "khung gyro, tru imu_lag_sec = " << cfg_.imu_lag_sec * 1000.0 << " ms -- do lai bang log `wheel_odom/lag` sau khi doi nguon IMU."); + const int lat = FtdiLatencyMs(cfg_.port); + if (lat > 2) { + VSLAM_WARN("imu: latency_timer cap FTDI = " << lat << " ms -- moi lan doc gom nhieu " + "mau, stamp IMU giat. Cai configuration_files/udev/99-imu.rules (1 ms)."); + } else if (lat >= 0) { + VSLAM_INFO("imu: latency_timer cap FTDI = " << lat << " ms"); + } if (cfg_.baud <= 9600) { VSLAM_INFO("imu: 9600 baud chi du ~29 Hz (cam bien mac dinh 10 Hz). " "wheel_odom bo gyro khi IMU cham hon encoder -- xem muc mcu " @@ -195,6 +231,7 @@ class Wt61pcBridge : public McuInterface { << std::strerror(errno) << ") -- chuong trinh khac mo cong " "sau se chia nhau luong byte."); } + SetLowLatency(fd); ::tcflush(fd, TCIOFLUSH); // bo byte cu, tranh bat dau giua mot khung fd_ = fd; return StatusResponse::Ok(); diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 05e7f54..afc1beb 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -124,6 +124,13 @@ add_executable(vslam_lidar_probe vslam_lidar_probe.cc) # duoc tren may chi co tay cam cam vao, truoc khi build ca cay. add_executable(vslam_joy_probe vslam_joy_probe.cc) +# vslam_imu_config: doc / doi baud va tan so phat cua IMU WT61PC qua CHINH cong +# serial (lenh WitMotion: mo khoa -> ghi -> luu), khong can phan mem cua hang. +if(VSLAM_BUILD_DRIVERS) + add_executable(vslam_imu_config vslam_imu_config.cc) + target_link_libraries(vslam_imu_config PRIVATE vslam_drivers vslam_common) +endif() + # vslam_world_export: `.world` cua Gazebo -> luoi chiem dung .pgm + .yaml. # # Cho phep dung the gioi mo phong TU CHINH file world, thay vi tu mot .db da diff --git a/tools/vslam_imu_config.cc b/tools/vslam_imu_config.cc new file mode 100644 index 0000000..bc3a3e6 --- /dev/null +++ b/tools/vslam_imu_config.cc @@ -0,0 +1,310 @@ +// vslam_imu_config -- doc / doi baud va tan so phat cua IMU WT61PC (WitMotion, +// ban DFRobot SEN0386) qua CHINH cong serial, khong can phan mem cua hang. +// +// vslam_imu_config [--port /dev/imu] chi DO: baud, tan so +// vslam_imu_config [--port P] --baud 115200 --rate-hz 100 [--yes] +// +// Khong co --yes thi chi do va in cac lenh SE gui. Can cong RANH: dung +// vslam_node va motor_control (imu_ros_node) truoc -- driver khoa doc quyen. +// +// ================== GIAO THUC (WIT Standard Protocol) ================== +// mo khoa FF AA 69 88 B5 hieu luc 10 s +// ghi FF AA +// 0x03 RRATE: 0x06 10 Hz, 0x07 20, 0x08 50, 0x09 100, 0x0B 200 +// 0x04 BAUD : 0x02 9600, 0x03 19200, 0x04 38400, 0x05 57600, +// 0x06 115200, 0x07 230400 +// luu FF AA 00 00 00 +// Thu vien Arduino cua DFRobot chi gui `FF AA 03 xx 00` (khong mo khoa, khong +// luu) va dien dan DFRobot co nhieu bao "doi tan so khong an" -- nen o day +// LUON mo khoa truoc va luu sau. +// +// ================== TRINH TU ================== +// 1. Do baud hien tai (thu lan luot, tim khung 0x55 dung checksum). +// 2. Mo khoa -> doi baud -> luu, o baud CU. +// 3. Mo lai o baud MOI, phai thay khung; mo khoa -> luu lan nua (mot so +// firmware doi baud NGAY khi nhan lenh, lenh luu o baud cu thanh rac). +// 4. Mo khoa -> doi tan so -> luu. +// 5. Do lai tan so that. +// Lo mat baud (vd mat dien giua chung): chay lai khong tham so -- buoc 1 do +// qua moi baud ho tro. + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vslam/wt61pc_protocol.h" + +namespace { + +speed_t BaudConstant(int baud) { + switch (baud) { + case 9600: return B9600; + case 19200: return B19200; + case 38400: return B38400; + case 57600: return B57600; + case 115200: return B115200; + case 230400: return B230400; + default: return 0; + } +} + +int BaudCode(int baud) { + switch (baud) { + case 9600: return 0x02; + case 19200: return 0x03; + case 38400: return 0x04; + case 57600: return 0x05; + case 115200: return 0x06; + case 230400: return 0x07; + default: return -1; + } +} + +int RateCode(int hz) { + switch (hz) { + case 10: return 0x06; + case 20: return 0x07; + case 50: return 0x08; + case 100: return 0x09; + case 200: return 0x0B; + default: return -1; + } +} + +// Go khoa doc quyen TRUOC khi dong: neu con ai giu tty (vd cong gia khi test) +// thi co TIOCEXCL ton tai sau close va lan mo ke tiep bao EBUSY. +void ClosePort(int fd) { + ::ioctl(fd, TIOCNXCL); + ::close(fd); +} + +int OpenPort(const std::string& port, int baud) { + const int fd = ::open(port.c_str(), O_RDWR | O_NOCTTY); + if (fd < 0) { + std::fprintf(stderr, "khong mo duoc %s: %s%s\n", port.c_str(), std::strerror(errno), + errno == EBUSY ? " -- cong dang bi giu: dung vslam_node / motor_control truoc" + : ""); + return -1; + } + if (::ioctl(fd, TIOCEXCL) != 0) { + std::fprintf(stderr, "%s: khong khoa doc quyen duoc (%s)\n", port.c_str(), std::strerror(errno)); + } + termios tio{}; + if (::tcgetattr(fd, &tio) != 0) { + ClosePort(fd); + return -1; + } + ::cfmakeraw(&tio); + tio.c_cflag |= CLOCAL | CREAD; + tio.c_cc[VMIN] = 0; + tio.c_cc[VTIME] = 1; // read() cho toi 0.1 s + ::cfsetispeed(&tio, BaudConstant(baud)); + ::cfsetospeed(&tio, BaudConstant(baud)); + if (::tcsetattr(fd, TCSANOW, &tio) != 0) { + ClosePort(fd); + return -1; + } + ::tcflush(fd, TCIOFLUSH); + return fd; +} + +struct Probe { + int frames = 0; + int gyro = 0; + uint64_t bad = 0; + double seconds = 0.0; + double roll = 0.0, pitch = 0.0; + bool have_angle = false; + double hz() const { return seconds > 0.0 ? gyro / seconds : 0.0; } +}; + +Probe Listen(int fd, double seconds) { + Probe p; + vslam::Wt61pcParser parser; + vslam::Wt61pcFrame f; + const auto t0 = std::chrono::steady_clock::now(); + uint8_t buf[256]; + while (true) { + const double el = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); + if (el >= seconds) { + p.seconds = el; + break; + } + const ssize_t n = ::read(fd, buf, sizeof(buf)); + for (ssize_t i = 0; i < n; ++i) { + if (!parser.Push(buf[i], &f)) continue; + p.frames++; + if (f.type == vslam::Wt61pcType::kGyro) p.gyro++; + if (f.type == vslam::Wt61pcType::kAngle) { + p.roll = f.x; + p.pitch = f.y; + p.have_angle = true; + } + } + } + p.bad = parser.bad_checksum(); + return p; +} + +bool Send(int fd, uint8_t addr, uint8_t lo, uint8_t hi, const char* what) { + const uint8_t cmd[5] = {0xFF, 0xAA, addr, lo, hi}; + std::printf(" gui %-22s FF AA %02X %02X %02X\n", what, addr, lo, hi); + if (::write(fd, cmd, sizeof(cmd)) != static_cast(sizeof(cmd))) { + std::fprintf(stderr, " ghi loi: %s\n", std::strerror(errno)); + return false; + } + ::tcdrain(fd); + std::this_thread::sleep_for(std::chrono::milliseconds(150)); + return true; +} + +bool Unlock(int fd) { return Send(fd, 0x69, 0x88, 0xB5, "mo khoa"); } +bool Save(int fd) { return Send(fd, 0x00, 0x00, 0x00, "luu"); } + +// Thu lan luot cac baud; tra baud thay khung hop le, 0 neu khong thay. +int Detect(const std::string& port, int first, Probe* out) { + std::vector order{first, 9600, 115200, 57600, 38400, 19200, 230400}; + for (size_t i = 0; i < order.size(); ++i) { + const int b = order[i]; + if (b <= 0 || BaudConstant(b) == 0) continue; + bool dup = false; + for (size_t j = 0; j < i; ++j) dup = dup || order[j] == b; + if (dup) continue; + const int fd = OpenPort(port, b); + if (fd < 0) return -1; + const Probe p = Listen(fd, 1.5); + ClosePort(fd); + std::printf(" thu %6d baud: %3d khung hop le, checksum sai %llu\n", b, p.frames, + static_cast(p.bad)); + if (p.frames >= 5) { + *out = p; + return b; + } + } + return 0; +} + +void Report(int baud, const Probe& p) { + std::printf("=> %d baud, gyro %.1f Hz", baud, p.hz()); + if (p.have_angle) { + std::printf(", goc cam bien roll %.2f / pitch %.2f do", p.roll * 180.0 / M_PI, + p.pitch * 180.0 / M_PI); + } + std::printf("\n"); +} + +} // namespace + +int main(int argc, char** argv) { + std::string port = "/dev/imu"; + int want_baud = 0, want_hz = 0, guess = 9600; + bool yes = false; + for (int i = 1; i < argc; ++i) { + const std::string a = argv[i]; + auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : ""; }; + if (a == "--port") port = next(); + else if (a == "--baud") want_baud = std::atoi(next().c_str()); + else if (a == "--rate-hz") want_hz = std::atoi(next().c_str()); + else if (a == "--guess") guess = std::atoi(next().c_str()); + else if (a == "--yes") yes = true; + else { + std::fprintf(stderr, "tham so la: %s (xem dau file)\n", a.c_str()); + return 2; + } + } + if ((want_baud != 0 && BaudCode(want_baud) < 0) || (want_hz != 0 && RateCode(want_hz) < 0)) { + std::fprintf(stderr, "ho tro --baud 9600/19200/38400/57600/115200/230400, " + "--rate-hz 10/20/50/100/200\n"); + return 2; + } + + std::printf("[1] do IMU o %s\n", port.c_str()); + Probe p; + const int cur = Detect(port, guess, &p); + if (cur < 0) return 1; + if (cur == 0) { + std::fprintf(stderr, "khong thay khung WT61PC o baud nao -- kiem day (TX cam bien -> RX cap), " + "nguon, va dung cong.\n"); + return 1; + } + Report(cur, p); + if (want_baud == 0 && want_hz == 0) return 0; + + const int new_baud = want_baud != 0 ? want_baud : cur; + const int new_hz = want_hz != 0 ? want_hz : static_cast(std::lround(p.hz())); + // Moi chu ky 33 byte (gia toc + gyro + goc), 10 bit moi byte. + const double max_hz = new_baud / 10.0 / 33.0; + if (new_hz > 0.8 * max_hz) { + std::fprintf(stderr, "%d Hz qua tai %d baud (toi da ~%.0f Hz) -- tang --baud.\n", new_hz, + new_baud, 0.8 * max_hz); + return 2; + } + if (!yes) { + std::printf("[chua gui gi] se doi: %d baud -> %d baud, tan so -> %d Hz. Them --yes de lam.\n", + cur, new_baud, new_hz); + return 0; + } + + if (new_baud != cur) { + std::printf("[2] doi baud %d -> %d\n", cur, new_baud); + int fd = OpenPort(port, cur); + if (fd < 0) return 1; + bool ok = Unlock(fd) && Send(fd, 0x04, static_cast(BaudCode(new_baud)), 0x00, "baud") && + Save(fd); + ClosePort(fd); + if (!ok) return 1; + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + std::printf("[3] mo lai o %d baud\n", new_baud); + fd = OpenPort(port, new_baud); + if (fd < 0) return 1; + const Probe q = Listen(fd, 1.5); + if (q.frames < 5) { + ClosePort(fd); + std::fprintf(stderr, "khong thay khung o %d baud -- cam bien chua doi baud. Chay lai khong " + "tham so de do baud hien tai.\n", new_baud); + return 1; + } + std::printf(" thay %d khung -- da o %d baud. Luu lan nua o baud moi.\n", q.frames, new_baud); + ok = Unlock(fd) && Save(fd); + ClosePort(fd); + if (!ok) return 1; + } + + std::printf("[4] doi tan so -> %d Hz\n", new_hz); + int fd = OpenPort(port, new_baud); + if (fd < 0) return 1; + const bool ok = Unlock(fd) && Send(fd, 0x03, static_cast(RateCode(new_hz)), 0x00, "tan so") && + Save(fd); + if (!ok) { + ClosePort(fd); + return 1; + } + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + ::tcflush(fd, TCIFLUSH); + + std::printf("[5] do lai\n"); + const Probe r = Listen(fd, 3.0); + ClosePort(fd); + Report(new_baud, r); + if (std::fabs(r.hz() - new_hz) > 0.15 * new_hz) { + std::fprintf(stderr, "TAN SO CHUA DUNG (%.1f Hz, muon %d Hz). Thu tat/bat nguon cam bien roi " + "chay lai khong tham so de xem gia tri da luu.\n", r.hz(), new_hz); + return 1; + } + std::printf("XONG. Dat `mcu.baud: %d` trong sensors.yaml; bao nguoi dung imu_ros_node doi " + "baudrate sang %d. Tat/bat nguon roi chay lai khong tham so de chac da LUU.\n", + new_baud, new_baud); + return 0; +} diff --git a/tools/vslam_vo_bench.cc b/tools/vslam_vo_bench.cc index 30548fa..285ffa4 100644 --- a/tools/vslam_vo_bench.cc +++ b/tools/vslam_vo_bench.cc @@ -6,7 +6,8 @@ // [--reset-after N] [--ref-out REF.txt] [--imu-orient] // [--imu-hz N] [--accel-bias x,y,z] [--gyro-bias x,y,z] // [--enc-scale K] [--ir-noise SIGMA] [--seed N] [--no-vo] -// [--ga-alpha A] +// [--ga-alpha A] [--pitch-source ga|sensor] [--sensor-hz N] +// [--sensor-bias-deg B] [--sensor-noise-deg S] [--sensor-lag-ms L] // // Ra: TRAJ.txt = "giay x y z qx qy qz qw" cua base_footprint trong frame // odom (goc = tu the robot o khung dau), chi khung odometry TINH DUOC. @@ -30,6 +31,13 @@ // sai ban kinh) -- ca doan F2M lan tham chieu // --ir-noise SIGMA nhieu Gauss tren anh IR truoc khi vao VO // --no-vo chi tinh tham chieu (nhanh) +// --pitch-source sensor pitch cua hang tham chieu lay tu goc DA LOC cua +// chinh cam bien (khung Angle 0x53 cua WT61PC) thay vi +// tu loc gyro/accel tho. Gia lap: pitch THAT (stream +// ground truth) lay mau --sensor-hz, cong bias + nhieu, +// tre --sensor-lag-ms, giu gia tri giua hai mau. LAC +// QUAN: bo loc trong cam bien that co sai rieng (vd khi +// xe tang toc) -- phai do lai tren robot. // --ga-alpha A alpha bo loc bu cua GravityAlign (mac dinh 0.98 cua // pipeline.yaml, tinh MOI MAU: 10 Hz -> tau 5 s, // 200 Hz -> tau 0.25 s) @@ -52,6 +60,8 @@ // vslam_node se lam khi VO hong. Moi lan reset la mot lan dem. #include +#include +#include #include #include #include @@ -111,6 +121,8 @@ int main(int argc, char** argv) { int reset_after = 5; bool imu_orient = false, no_vo = false; double imu_hz = 0.0, enc_scale = 1.0, ir_noise = 0.0, ga_alpha = -1.0; + std::string pitch_source = "ga"; + double sensor_hz = 10.0, sensor_bias_deg = 0.0, sensor_noise_deg = 0.0, sensor_lag_ms = 0.0; double accel_bias[3] = {0, 0, 0}, gyro_bias[3] = {0, 0, 0}; uint32_t seed = 1; rtabmap::ParametersMap params; @@ -136,6 +148,11 @@ int main(int argc, char** argv) { else if (a == "--imu-hz") imu_hz = std::atof(next().c_str()); else if (a == "--enc-scale") enc_scale = std::atof(next().c_str()); else if (a == "--ga-alpha") ga_alpha = std::atof(next().c_str()); + else if (a == "--pitch-source") pitch_source = next(); + else if (a == "--sensor-hz") sensor_hz = std::atof(next().c_str()); + else if (a == "--sensor-bias-deg") sensor_bias_deg = std::atof(next().c_str()); + else if (a == "--sensor-noise-deg") sensor_noise_deg = std::atof(next().c_str()); + else if (a == "--sensor-lag-ms") sensor_lag_ms = std::atof(next().c_str()); else if (a == "--ir-noise") ir_noise = std::atof(next().c_str()); else if (a == "--seed") seed = static_cast(std::atoi(next().c_str())); else if (a == "--accel-bias" || a == "--gyro-bias") { @@ -220,6 +237,14 @@ int main(int argc, char** argv) { vslam::GravityAlign ga(gcfg); double rx = 0, ry = 0, rz = 0; // tham chieu: xy ngang + z tich luy int64_t enc_t = -1, imu_kept_t = -1; + // --pitch-source sensor: goc cam bien gia lap tu ground truth (xem dau file). + const bool use_sensor = pitch_source == "sensor"; + double s_roll = 0.0, s_pitch = 0.0; + int64_t s_next = -1; + std::deque> s_pending; // {t_den_ns, roll, pitch} + std::normal_distribution n_ang(0.0, 1.0); + auto ref_pitch = [&]() { return use_sensor ? s_pitch : ga.pitch(); }; + auto ref_roll = [&]() { return use_sensor ? s_roll : ga.roll(); }; std::mt19937 rng(seed); std::normal_distribution n01(0.0, 1.0); @@ -255,7 +280,7 @@ int main(int argc, char** argv) { wx += ds * std::cos(wyaw + 0.5 * dth); wy += ds * std::sin(wyaw + 0.5 * dth); // Tham chieu: pitch duong = mui chuc xuong, nen len doc pitch am. - const double p = ga.pitch(); + const double p = ref_pitch(); rx += ds * std::cos(p) * std::cos(wyaw + 0.5 * dth); ry += ds * std::cos(p) * std::sin(wyaw + 0.5 * dth); rz += -ds * std::sin(p); @@ -269,6 +294,23 @@ int main(int argc, char** argv) { enc_init = true; continue; } + if (use_sensor && h.stream == vslam::VsrStream::kGroundTruth) { + double g[6]; + if (!vslam::DecodeGroundTruthPayload(payload, g)) continue; + const double d2r = M_PI / 180.0; + if (s_next < 0 || t >= s_next) { + s_pending.push_back({static_cast(t) + sensor_lag_ms * 1e6, + g[3] + sensor_noise_deg * d2r * n_ang(rng), + g[4] + sensor_bias_deg * d2r + sensor_noise_deg * d2r * n_ang(rng)}); + s_next = t + static_cast(1e9 / sensor_hz); + } + while (!s_pending.empty() && s_pending.front()[0] <= static_cast(t)) { + s_roll = s_pending.front()[1]; + s_pitch = s_pending.front()[2]; + s_pending.pop_front(); + } + continue; + } if (h.stream == vslam::VsrStream::kImu) { vslam::Imu m; if (!vslam::DecodeImuPayload(payload, &m)) continue; @@ -309,8 +351,8 @@ int main(int argc, char** argv) { depth.t = ir.t = -1; if (fr != nullptr) { const rtabmap::Transform ref(static_cast(rx), static_cast(ry), - static_cast(rz), static_cast(ga.roll()), - static_cast(ga.pitch()), static_cast(wyaw)); + static_cast(rz), static_cast(ref_roll()), + static_cast(ref_pitch()), static_cast(wyaw)); WriteTum(fr, t * 1e-9, ref); } continue; @@ -347,8 +389,8 @@ int main(int argc, char** argv) { depth.t = ir.t = -1; if (fr != nullptr) { const rtabmap::Transform ref(static_cast(rx), static_cast(ry), - static_cast(rz), static_cast(ga.roll()), - static_cast(ga.pitch()), static_cast(wyaw)); + static_cast(rz), static_cast(ref_roll()), + static_cast(ref_pitch()), static_cast(wyaw)); WriteTum(fr, t * 1e-9, ref); } if (pose.isNull()) {