535 lines
20 KiB
C++
535 lines
20 KiB
C++
// Cau noi toi MCU: IMU qua UART.
|
|
//
|
|
// (Cach thu ba -- WT61PC noi THANG UART, khong ESP32 -- nam o wt61pc_bridge.cc;
|
|
// CreateMcuBridge() o cuoi file chon theo mcu.protocol.)
|
|
//
|
|
// ================== HAI NHANH, CHON BANG mcu.expect_timestamps ==================
|
|
//
|
|
// `true` -- firmware trong firmware/imu_uart/ cua repo nay. Phat theo dung
|
|
// mcu_protocol.h:
|
|
// I,<micros>,<gx>,<gy>,<gz>,<ax>,<ay>,<az> rad/s, m/s^2
|
|
// `micros` sinh TREN MCU ngay khi khung gyro qua checksum, va ClockSync anh xa
|
|
// dong ho MCU sang dong ho host. Jitter cua USB va cua scheduler bi phep fit
|
|
// loai bo thay vi doi thang vao stamp.
|
|
//
|
|
// `false` -- firmware cu (base_kit/src/hardware/IMU_uart). Phat text ba dong:
|
|
// Acc 0.05 -0.10 9.82 <- m/s^2
|
|
// Gyro 0.00 0.00 0.00 <- DO/s, khong phai rad/s
|
|
// Angle -0.55 -0.34 -176.61 <- do
|
|
// Khong co dau thoi gian nen stamp lay luc HOST doc duoc dong, mang theo moi
|
|
// do tre cua USB-CDC va scheduler. Do duoc tren robot nay: 80 ms.
|
|
//
|
|
// Giu ca hai de con lui lai duoc khi firmware chua kip flash.
|
|
//
|
|
// ================== CAI GIA CON LAI, CA HAI NHANH ==================
|
|
//
|
|
// 1. DO TRE HANG SO ~80 ms, nam TRUOC diem MCU dong dau.
|
|
// WT61PC o che do 10 Hz loc/trung binh noi bo (~50 ms), roi day 2 khung
|
|
// Acc+Gyro qua day 9600 baud (~23 ms). Dong dau som cach may cung khong
|
|
// cuu duoc phan do. No la HANG SO nen `mcu.imu_lag_sec` tru duoc -- do
|
|
// bang log `wheel_odom/lag`, xem sensors.yaml.
|
|
//
|
|
// 2. GYRO LAM TRON.
|
|
// Nhanh cu: firmware in "%.2f" do/s = 0.01 do/s ~ 0.17 mrad/s.
|
|
// Nhanh moi: "%.5f" rad/s, min hon nhieu, nhung cam bien van tu ve 0 khi
|
|
// nghi -- doc duoc gyro = 0.00000 o moi mau luc dung yen.
|
|
//
|
|
// 3. KHONG CO BARO / SIEU AM / BUMPER.
|
|
// Dac ta co ban ghi B, U, K. Ca hai firmware deu khong phat cai nao. Cac
|
|
// callback tuong ung KHONG BAO GIO duoc goi -- xem ghi chu tai cho dang ky.
|
|
// Dac biet nguy hiem voi bumper: lop safety cho rang mat bumper la mot
|
|
// duong doc lap, o day no khong ton tai.
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
|
|
#include <atomic>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "vslam/clock.h"
|
|
#include "vslam/clock_sync.h"
|
|
#include "vslam/driver_config.h"
|
|
#include "vslam/driver_interfaces.h"
|
|
#include "vslam/geometry.h"
|
|
#include "vslam/log.h"
|
|
#include "vslam/mcu_protocol.h"
|
|
|
|
namespace vslam {
|
|
namespace {
|
|
|
|
constexpr double kDeg2Rad = M_PI / 180.0;
|
|
|
|
speed_t BaudConstant(uint32_t 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;
|
|
case 460800: return B460800;
|
|
case 921600: return B921600;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
class McuBridge : public McuInterface {
|
|
public:
|
|
McuBridge(const McuConfig& cfg, ClockPtr clock)
|
|
: cfg_(cfg), clock_(std::move(clock)) {}
|
|
|
|
~McuBridge() override { Close(); }
|
|
|
|
void SetImuCallback(ImuCallback cb) override { imu_cb_ = std::move(cb); }
|
|
|
|
// Ba callback duoi day KHONG BAO GIO duoc goi voi firmware hien tai --
|
|
// khong phai quen noi day, ma la firmware khong phat B/U/K. Van nhan va
|
|
// giu de khi firmware dung dac ta thi chi phai sua mot cho.
|
|
void SetBaroCallback(BaroCallback cb) override { baro_cb_ = std::move(cb); }
|
|
void SetUltrasonicCallback(UltrasonicCallback cb) override {
|
|
ultra_cb_ = std::move(cb);
|
|
}
|
|
void SetBumperCallback(BumperCallback cb) override {
|
|
bumper_cb_ = std::move(cb);
|
|
}
|
|
|
|
StatusResponse Open() override {
|
|
if (fd_ >= 0) {
|
|
return StatusResponse::Error(StatusCode::kFailedPrecondition,
|
|
"mcu: da mo roi");
|
|
}
|
|
if (!clock_) {
|
|
return StatusResponse::Error(StatusCode::kInvalidArgument,
|
|
"mcu: thieu clock");
|
|
}
|
|
|
|
const speed_t speed = BaudConstant(cfg_.baud);
|
|
if (speed == 0) {
|
|
return StatusResponse::Error(
|
|
StatusCode::kInvalidArgument,
|
|
"mcu: baud " + std::to_string(cfg_.baud) + " khong ho tro");
|
|
}
|
|
|
|
fd_ = ::open(cfg_.port.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
|
|
if (fd_ < 0) {
|
|
return StatusResponse::Error(
|
|
StatusCode::kUnavailable,
|
|
"mcu: mo " + cfg_.port + " that bai: " + std::strerror(errno) +
|
|
(errno == EACCES ? " -- thieu quyen? them user vao nhom dialout"
|
|
: ""));
|
|
}
|
|
|
|
termios tty{};
|
|
if (::tcgetattr(fd_, &tty) != 0) {
|
|
const std::string err = std::strerror(errno);
|
|
CloseFd();
|
|
return StatusResponse::Error(StatusCode::kInternal,
|
|
"mcu: tcgetattr that bai: " + err);
|
|
}
|
|
|
|
::cfsetospeed(&tty, speed);
|
|
::cfsetispeed(&tty, speed);
|
|
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8 bit
|
|
tty.c_cflag |= (CLOCAL | CREAD);
|
|
tty.c_cflag &= ~(PARENB | PARODD); // khong parity
|
|
tty.c_cflag &= ~CSTOPB; // 1 stop bit
|
|
tty.c_cflag &= ~CRTSCTS; // khong flow control
|
|
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
|
|
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
|
|
tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
|
tty.c_oflag &= ~OPOST;
|
|
// VMIN=0 + VTIME: read() tra ve sau nhieu nhat VTIME*100 ms du khong co
|
|
// byte nao. Can the de vong doc con thoat duoc khi Close().
|
|
tty.c_cc[VMIN] = 0;
|
|
tty.c_cc[VTIME] = static_cast<cc_t>(
|
|
cfg_.read_timeout_sec > 0.0 ? cfg_.read_timeout_sec * 10.0 : 10);
|
|
|
|
if (::tcsetattr(fd_, TCSANOW, &tty) != 0) {
|
|
const std::string err = std::strerror(errno);
|
|
CloseFd();
|
|
return StatusResponse::Error(StatusCode::kInternal,
|
|
"mcu: tcsetattr that bai: " + err);
|
|
}
|
|
|
|
VSLAM_INFO("mcu: mo " << cfg_.port << " @" << cfg_.baud);
|
|
if (cfg_.expect_timestamps) {
|
|
VSLAM_INFO("mcu: doc ban ghi co dau thoi gian theo mcu_protocol.h. "
|
|
"ClockSync se anh xa dong ho MCU sang dong ho host; "
|
|
"imu_lag_sec = " << cfg_.imu_lag_sec * 1000.0 << " ms bu phan "
|
|
"hang so con lai (thoi gian truyen khung tren day cam bien).");
|
|
} else if (cfg_.imu_lag_sec > 0.0) {
|
|
VSLAM_INFO("mcu: firmware khong phat timestamp -- stamp lay tu dong ho "
|
|
"HOST, da tru bu imu_lag_sec = "
|
|
<< cfg_.imu_lag_sec * 1000.0
|
|
<< " ms. Kiem lai bang log `wheel_odom/lag`: phai bao ~0 ms.");
|
|
} else {
|
|
VSLAM_WARN(
|
|
"mcu: firmware khong phat timestamp -- stamp lay tu dong ho HOST luc "
|
|
"doc duoc dong, va imu_lag_sec = 0 nen KHONG bu gi. Moi phep dong bo "
|
|
"IMU voi LiDAR/camera mang mot do tre chua do. Chay lai co vao/ra cua "
|
|
"roi doc log `wheel_odom/lag`. Xem mcu_bridge.cc, muc 1.");
|
|
}
|
|
|
|
running_ = true;
|
|
thread_ = std::thread([this] { ReadLoop(); });
|
|
return StatusResponse::Ok();
|
|
}
|
|
|
|
void Close() override {
|
|
running_ = false;
|
|
CloseFd();
|
|
if (thread_.joinable()) thread_.join();
|
|
}
|
|
|
|
DriverStats Stats() const override {
|
|
std::lock_guard<std::mutex> lk(stats_mu_);
|
|
return stats_;
|
|
}
|
|
|
|
private:
|
|
void CloseFd() {
|
|
if (fd_ >= 0) {
|
|
::close(fd_);
|
|
fd_ = -1;
|
|
}
|
|
}
|
|
|
|
void ReadLoop() {
|
|
char buf[512];
|
|
std::string line;
|
|
while (running_) {
|
|
const ssize_t n = ::read(fd_, buf, sizeof(buf));
|
|
if (n < 0) {
|
|
if (!running_) break;
|
|
if (errno == EAGAIN || errno == EINTR) continue;
|
|
Fail(std::string("read() that bai: ") + std::strerror(errno));
|
|
continue;
|
|
}
|
|
if (n == 0) continue; // timeout, khong phai loi
|
|
|
|
for (ssize_t i = 0; i < n; ++i) {
|
|
if (buf[i] == '\n') {
|
|
ProcessLine(line);
|
|
line.clear();
|
|
} else if (buf[i] != '\r') {
|
|
// Chan dong rac dai vo han neu thiet bi phat nhieu: mot dong hop le
|
|
// dai chung 30 ky tu.
|
|
if (line.size() < 256) line += buf[i];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ProcessLine(const std::string& line) {
|
|
if (line.empty()) return;
|
|
|
|
// ⚠️ Bo dong DAU TIEN sau khi mo cong.
|
|
//
|
|
// Thiet bi dang phat lien tuc, nen `open()` gan nhu luon roi vao GIUA mot
|
|
// dong. Ky tu '\n' dau tien ta gap la ket thuc cua mot dong CUT.
|
|
//
|
|
// Dong cut van parse duoc, va do moi la van de: "I,117592345,..." bi cat
|
|
// dau thanh "I,5678,..." thi `micros` be di hang tram giay ma moi truong
|
|
// deu hop le. Da do tren robot: mau dau tien cho buoc 117.587 s -- dung
|
|
// bang tuoi cua ESP32 luc do.
|
|
if (!first_line_dropped_) {
|
|
first_line_dropped_ = true;
|
|
++unparsed_;
|
|
return;
|
|
}
|
|
|
|
if (cfg_.expect_timestamps) {
|
|
ProcessRecordLine(line);
|
|
return;
|
|
}
|
|
ProcessLegacyLine(line);
|
|
}
|
|
|
|
// Nhanh MOI: firmware phat theo mcu_protocol.h, co `micros()` cua chinh MCU.
|
|
//
|
|
// Khac biet that so voi nhanh cu KHONG phai o dinh dang ma o DONG HO. Nhanh
|
|
// cu dong dau bang dong ho host luc doc duoc dong, nen moi do tre cua UART,
|
|
// USB va scheduler deu chui vao stamp ma khong ai do duoc -- da do tren robot
|
|
// nay: 80 ms. O day `micros` sinh ra TREN MCU ngay khi nhan xong khung gyro,
|
|
// va ClockSync anh xa dong ho MCU sang dong ho host bang fit tuyen tinh, nen
|
|
// jitter cua duong truyen bi fit loai bo thay vi doi thang vao stamp.
|
|
void ProcessRecordLine(const std::string& line) {
|
|
const double t_recv = clock_->Now().toSec();
|
|
|
|
McuRecord rec;
|
|
if (!ParseMcuLine(line, &rec)) {
|
|
// Dong hong KHONG duoc coi la du lieu 0 -- dac ta noi ro. Firmware con
|
|
// in banner luc khoi dong nen vai dong dau khong doc duoc la binh thuong.
|
|
++unparsed_;
|
|
return;
|
|
}
|
|
|
|
// PHAI goi theo dung thu tu ban tin den: `micros` la uint32, tran moi 71.58
|
|
// phut, va McuTimeUnwrapper cong phan tran dua tren mau truoc do.
|
|
const double mcu_s = unwrap_.UnwrapSeconds(rec.micros);
|
|
|
|
// Chua co ping/pong nen khong do duoc RTT: dua t_send = t_recv. Hau qua la
|
|
// `offset` lech dung do tre MOT CHIEU cua USB Serial/JTAG (co ms), con
|
|
// `skew` va viec khu jitter thi khong anh huong -- chung den tu fit tuyen
|
|
// tinh tren nhieu mau. Phan lech hang so do gop chung vao `imu_lag_sec`,
|
|
// vay la mot nut duy nhat, do duoc, thay vi hai nut chong nhau.
|
|
// ⚠️ Loc mau truoc khi nap vao phep fit.
|
|
//
|
|
// Dong text KHONG co checksum, nen mot byte hong lam `micros` thanh mot so
|
|
// bat ky ma van parse duoc. Va mot mau rac duy nhat du de pha ca phep fit:
|
|
// khi `skew` bi tu choi thi `offset = trung_binh_host - trung_binh_mcu`,
|
|
// nen mot mau lech X giay keo offset di X/16. Do tren robot: ClockSync cho
|
|
// thoi diem lech 77 giay -- ung voi mot mau sai ~1200 s.
|
|
//
|
|
// O 10 Hz thi buoc that la ~0.1 s. Cho toi 2 s de con chiu duoc cu vap cua
|
|
// USB hay scheduler, nhung khong hon: ngoai khoang do thi khong phan biet
|
|
// duoc "gap that" voi "byte hong", ma doan sai o day thi hong ca dong ho.
|
|
bool mcu_step_ok = true;
|
|
if (have_prev_mcu_) {
|
|
const double step = mcu_s - prev_mcu_s_;
|
|
mcu_step_ok = (step > 0.0 && step < 2.0);
|
|
if (!mcu_step_ok) {
|
|
++mcu_step_rejected_;
|
|
VSLAM_WARN_THROTTLE(10.0,
|
|
"mcu: buoc thoi gian MCU = " << step
|
|
<< " s, ngoai khoang [0, 2] -- dong co the bi hong byte (text "
|
|
"khong co checksum). Bo mau nay khoi ClockSync va dung dong ho "
|
|
"host cho no. Lan thu " << mcu_step_rejected_ << ".");
|
|
}
|
|
}
|
|
if (mcu_step_ok) {
|
|
sync_.Add(mcu_s, t_recv, t_recv);
|
|
}
|
|
// ⚠️ LUON cap nhat moc, ke ca khi bo mau.
|
|
//
|
|
// Ban truoc giu nguyen moc voi ly le "dung lay mau rac lam moc". Nghe hop
|
|
// ly, hau qua thi nguoc han: mot mau rac lam moc KET vinh vien, va moi mau
|
|
// that sau do deu co buoc lon nen deu bi bo. Do duoc tren robot: bo dem
|
|
// nhay 1 -> 101 -> 201 moi 10 giay, tuc 100% mau bi loai, va buoc bao cao
|
|
// lon dan dung bang thoi gian thuc (117 -> 127 -> 137 s). ClockSync khong
|
|
// nhan duoc mau nao suot ca phien.
|
|
//
|
|
// Luon tien moc thi mot cu vap ton dung HAI mau: cu vap, roi cu nhay nguoc
|
|
// ve nhip that. Sau do tu khop lai.
|
|
prev_mcu_s_ = mcu_s;
|
|
have_prev_mcu_ = true;
|
|
|
|
double host_s = 0.0;
|
|
UnixTime stamp;
|
|
// ⚠️ KIEM tinh hop ly truoc khi tin. `ready()` cua ClockSync bat ngay khi
|
|
// co 2 mau, va fit tu 2 diem cach nhau mot chu ky IMU thi ra bat cu gi.
|
|
//
|
|
// Do duoc tren robot: lan chuyen tu dong ho host sang dong ho MCU lam dau
|
|
// thoi gian nhay LUI 29.1 GIAY. Do khong phai sai so, do la vo nghia -- mot
|
|
// mau vua den thi thoi diem do cua no phai cach `t_recv` dung do tre mot
|
|
// chieu cua duong truyen, tuc vai mili giay. Lech 29 s thi phep fit dang
|
|
// noi ve mot vu tru khac.
|
|
//
|
|
// 1 giay la nguong RAT rong so voi vai ms that su, co y: no chi de bat cai
|
|
// vo nghia, khong de tinh chinh.
|
|
const bool sane = mcu_step_ok && sync_.ToHost(mcu_s, &host_s) &&
|
|
std::fabs(host_s - t_recv) < 1.0;
|
|
if (!sane && sync_.ready()) {
|
|
++sync_rejected_;
|
|
VSLAM_WARN_THROTTLE(10.0,
|
|
"mcu: ClockSync cho thoi diem lech " << (host_s - t_recv)
|
|
<< " s so voi luc nhan duoc dong -- bo qua, dung dong ho host. "
|
|
"Lan thu " << sync_rejected_ << ".");
|
|
}
|
|
if (sane) {
|
|
stamp = UnixTime::fromSec(host_s);
|
|
// In DINH KY chu khong in mot lan.
|
|
//
|
|
// `ready()` bat ngay khi co 2 mau -- o 10 Hz la 68 ms sau khi mo cong,
|
|
// va fit tu 2 diem cach nhau 100 ms thi vo nghia. In mot lan tai thoi
|
|
// diem do la in dung con so te nhat roi im luon. Da mac loi do: log bao
|
|
// "skew 0 ppm" trong khi ClockSync dang tu choi moi ket qua fit va giu
|
|
// skew = 1.0 -- hai trang thai do nhin y het nhau tu ben ngoai.
|
|
VSLAM_INFO_THROTTLE(30.0, "mcu: ClockSync skew " << sync_.skew_ppm()
|
|
<< " ppm, stamp lay tu dong ho MCU.");
|
|
} else {
|
|
// Chua du mau de fit. Dung dong ho host nhu nhanh cu -- kem hon nhung
|
|
// van chay duoc, va chi keo dai vai giay dau.
|
|
stamp = clock_->Now();
|
|
VSLAM_INFO_THROTTLE(5.0, "mcu: ClockSync chua du mau, tam dung dong ho "
|
|
"host de dong dau.");
|
|
}
|
|
|
|
if (rec.type != McuRecordType::kImu) {
|
|
// B / E / U / K chua co nguoi dung o day. Dem de thay khi firmware bat
|
|
// dau phat chung, chu khong im lang bo di.
|
|
++unhandled_records_;
|
|
return;
|
|
}
|
|
|
|
// ⚠️ KHONG doi don vi o day. mcu_protocol.h dac ta ban ghi I la rad/s va
|
|
// m/s^2 SAN ROI; nhanh cu doi do/s -> rad/s vi firmware cu in do/s. Doi
|
|
// hai lan thi gyro nho di 57 lan va odometry van "chay binh thuong".
|
|
gx_ = rec.gx; gy_ = rec.gy; gz_ = rec.gz;
|
|
ax_ = rec.ax; ay_ = rec.ay; az_ = rec.az;
|
|
// Ban ghi I khong mang orientation. Khong ai doc imu.orientation trong
|
|
// libs/ va app/ (da kiem), nen de quaternion don vi.
|
|
roll_ = pitch_ = yaw_ = 0.0;
|
|
|
|
Publish(stamp - Duration::fromSec(cfg_.imu_lag_sec));
|
|
}
|
|
|
|
void ProcessLegacyLine(const std::string& line) {
|
|
std::istringstream iss(line);
|
|
std::string type;
|
|
double x = 0.0, y = 0.0, z = 0.0;
|
|
if (!(iss >> type >> x >> y >> z)) {
|
|
// Dong khong doc duoc KHONG tinh la loi: firmware in ca banner luc
|
|
// khoi dong. Chi dem de thay khi ty le rac tang bat thuong.
|
|
++unparsed_;
|
|
return;
|
|
}
|
|
|
|
if (type == "Acc") {
|
|
ax_ = x; ay_ = y; az_ = z;
|
|
have_acc_ = true;
|
|
} else if (type == "Gyro") {
|
|
// DO/s -> rad/s. Firmware in do/s; Imu.angular_velocity la rad/s.
|
|
gx_ = x * kDeg2Rad; gy_ = y * kDeg2Rad; gz_ = z * kDeg2Rad;
|
|
have_gyro_ = true;
|
|
} else if (type == "Angle") {
|
|
roll_ = x * kDeg2Rad; pitch_ = y * kDeg2Rad; yaw_ = z * kDeg2Rad;
|
|
have_angle_ = true;
|
|
} else {
|
|
++unparsed_;
|
|
return;
|
|
}
|
|
|
|
if (have_acc_ && have_gyro_ && have_angle_) {
|
|
// ⚠️ Dong ho HOST, khong phai dong ho thiet bi -- xem dau file, muc 1.
|
|
// Tru do tre da do duoc: mau nay do robot SOM hon luc doc duoc no.
|
|
Publish(clock_->Now() - Duration::fromSec(cfg_.imu_lag_sec));
|
|
have_acc_ = have_gyro_ = have_angle_ = false;
|
|
}
|
|
}
|
|
|
|
void Publish(UnixTime stamp) {
|
|
auto imu = std::make_shared<Imu>();
|
|
imu->header.seq = seq_++;
|
|
// Nguoi goi da tru `imu_lag_sec`. Hai nhanh dua stamp den tu hai nguon
|
|
// khac han nhau -- dong ho host, hay dong ho MCU qua ClockSync -- nen viec
|
|
// chon nguon nam o cho goi, khong nam o day.
|
|
imu->header.stamp = stamp;
|
|
imu->header.frame_id = cfg_.imu_frame_id;
|
|
|
|
imu->angular_velocity.x = gx_;
|
|
imu->angular_velocity.y = gy_;
|
|
imu->angular_velocity.z = gz_;
|
|
imu->linear_acceleration.x = ax_;
|
|
imu->linear_acceleration.y = ay_;
|
|
imu->linear_acceleration.z = az_;
|
|
imu->orientation = QuaternionFromRPY(roll_, pitch_, yaw_);
|
|
|
|
// Covariance tu datasheet ICM-42688-P, deu la DOAN cho toi khi chay
|
|
// allan_variance. Dat tren duong cheo, khong de 0: 0 nghia la "chinh xac
|
|
// tuyet doi" voi moi bo loc doc no.
|
|
for (int i = 0; i < 3; ++i) {
|
|
imu->angular_velocity_covariance[i * 4] = cfg_.gyro_variance;
|
|
imu->linear_acceleration_covariance[i * 4] = cfg_.accel_variance;
|
|
}
|
|
// WT61PC co fusion rieng cho ra Angle, nhung do tin cay cua no khong do
|
|
// duoc o day. Khong dat [0] < 0 (tuc "khong co orientation") vi yaw van
|
|
// dung duoc lam goi y; de covariance lon de bo loc tu ha trong so.
|
|
for (int i = 0; i < 3; ++i) imu->orientation_covariance[i * 4] = 0.05;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(stats_mu_);
|
|
++stats_.frames;
|
|
stats_.connected = true;
|
|
const UnixTime now = imu->header.stamp;
|
|
if (!last_pub_.isZero()) {
|
|
const double dt = (now - last_pub_).toSec();
|
|
if (dt > 0.0) stats_.hz = 1.0 / dt;
|
|
}
|
|
last_pub_ = now;
|
|
if (cfg_.expect_timestamps && sync_.ready()) {
|
|
stats_.clock_skew_ppm = sync_.skew_ppm();
|
|
stats_.stamp_skew_ms = (clock_->Now() - now).toSec() * 1e3;
|
|
} else {
|
|
// KHONG co gi de so -> de 0. Xem dau file, muc 1.
|
|
stats_.stamp_skew_ms = 0.0;
|
|
stats_.clock_skew_ppm = 0.0;
|
|
}
|
|
}
|
|
|
|
if (imu_cb_) imu_cb_(imu);
|
|
}
|
|
|
|
void Fail(const std::string& msg) {
|
|
{
|
|
std::lock_guard<std::mutex> lk(stats_mu_);
|
|
++stats_.errors;
|
|
stats_.connected = false;
|
|
stats_.last_error = msg;
|
|
}
|
|
VSLAM_WARN_THROTTLE(5.0, "mcu: " << msg);
|
|
}
|
|
|
|
McuConfig cfg_;
|
|
ClockPtr clock_;
|
|
|
|
ImuCallback imu_cb_;
|
|
BaroCallback baro_cb_;
|
|
UltrasonicCallback ultra_cb_;
|
|
BumperCallback bumper_cb_;
|
|
|
|
int fd_ = -1;
|
|
std::atomic<bool> running_{false};
|
|
std::thread thread_;
|
|
|
|
double ax_ = 0.0, ay_ = 0.0, az_ = 0.0;
|
|
double gx_ = 0.0, gy_ = 0.0, gz_ = 0.0;
|
|
double roll_ = 0.0, pitch_ = 0.0, yaw_ = 0.0;
|
|
bool have_acc_ = false, have_gyro_ = false, have_angle_ = false;
|
|
uint64_t unparsed_ = 0;
|
|
|
|
// Chi dung o nhanh expect_timestamps. Ca hai deu chi cham tu luong doc, nen
|
|
// khong can khoa.
|
|
McuTimeUnwrapper unwrap_;
|
|
ClockSync sync_;
|
|
uint64_t sync_rejected_ = 0; // lan ClockSync cho ket qua vo nghia
|
|
// Loc mau truoc khi nap vao ClockSync. Dong text khong co checksum nen mot
|
|
// byte hong lam `micros` thanh so bat ky ma van parse duoc.
|
|
double prev_mcu_s_ = 0.0;
|
|
bool have_prev_mcu_ = false;
|
|
uint64_t mcu_step_rejected_ = 0;
|
|
bool first_line_dropped_ = false; // dong dau sau open() luon la dong cut
|
|
uint64_t unhandled_records_ = 0; // B / E / U / K: chua co nguoi dung
|
|
|
|
uint32_t seq_ = 0;
|
|
UnixTime last_pub_;
|
|
|
|
mutable std::mutex stats_mu_;
|
|
DriverStats stats_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
// Dinh nghia o wt61pc_bridge.cc.
|
|
std::unique_ptr<McuInterface> CreateWt61pcBridge(const McuConfig& cfg,
|
|
ClockPtr clock);
|
|
|
|
std::unique_ptr<McuInterface> CreateMcuBridge(const McuConfig& cfg,
|
|
ClockPtr clock) {
|
|
if (cfg.protocol == McuProtocol::kWt61pc) {
|
|
return CreateWt61pcBridge(cfg, std::move(clock));
|
|
}
|
|
return std::make_unique<McuBridge>(cfg, std::move(clock));
|
|
}
|
|
|
|
} // namespace vslam
|