drivers,tools: IMU WT61PC 115200 baud / 200 Hz -- mcu.baud 115200, imu_lag_sec 0.01 (uoc luong, can do lai bang wheel_odom/lag), FTDI low-latency 1 ms + log latency_timer, luat udev configuration_files/udev/99-imu.rules; tool vslam_imu_config (do / doi baud + tan so WT61PC bang lenh WitMotion); vslam_vo_bench --pitch-source sensor

This commit is contained in:
trung09122002
2026-10-06 10:17:07 +07:00
parent 6d552e231e
commit b1ab04c51e
7 changed files with 435 additions and 13 deletions

View File

@@ -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

310
tools/vslam_imu_config.cc Normal file
View File

@@ -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 <dia chi> <byte thap> <byte cao>
// 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 <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <cerrno>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <thread>
#include <vector>
#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<double>(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<ssize_t>(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<int> 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<unsigned long long>(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<int>(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<uint8_t>(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<uint8_t>(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;
}

View File

@@ -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 <algorithm>
#include <array>
#include <deque>
#include <chrono>
#include <random>
#include <cmath>
@@ -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<uint32_t>(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<std::array<double, 3>> s_pending; // {t_den_ns, roll, pitch}
std::normal_distribution<double> 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<double> 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<double>(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<int64_t>(1e9 / sensor_hz);
}
while (!s_pending.empty() && s_pending.front()[0] <= static_cast<double>(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<float>(rx), static_cast<float>(ry),
static_cast<float>(rz), static_cast<float>(ga.roll()),
static_cast<float>(ga.pitch()), static_cast<float>(wyaw));
static_cast<float>(rz), static_cast<float>(ref_roll()),
static_cast<float>(ref_pitch()), static_cast<float>(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<float>(rx), static_cast<float>(ry),
static_cast<float>(rz), static_cast<float>(ga.roll()),
static_cast<float>(ga.pitch()), static_cast<float>(wyaw));
static_cast<float>(rz), static_cast<float>(ref_roll()),
static_cast<float>(ref_pitch()), static_cast<float>(wyaw));
WriteTum(fr, t * 1e-9, ref);
}
if (pose.isNull()) {