// 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; }