From 92508203704dfc1ea7ec3b0da2af4274d6256b1e Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Sat, 19 Sep 2026 18:53:12 +0700 Subject: [PATCH] update --- .gitignore | 1 + libs/vslam_drivers/src/zlac_base.cc | 164 ++++++++++++++++++++++------ 2 files changed, 129 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 9d5fe0d..4559dfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ build/ build-*/ install/ +base_kit/ .cache/ compile_commands.json Testing/ diff --git a/libs/vslam_drivers/src/zlac_base.cc b/libs/vslam_drivers/src/zlac_base.cc index ffc4d38..46453fa 100644 --- a/libs/vslam_drivers/src/zlac_base.cc +++ b/libs/vslam_drivers/src/zlac_base.cc @@ -16,9 +16,14 @@ // 0x6040:0 u16 Controlword 6 -> 7 -> F -> 7F (chuoi DS402) // 0x60FF:3 u32 Target velocity GOP: 16 bit THAP = trai, CAO = phai // (ZLAC8015D manual p.19-20) -// 0x606C:1/2 i32 Velocity actual, don vi 0.1 RPM -// 0x6064:1/2 i32 Position actual, don vi increment <-- doc o day -// 0x608F:1 u32 Encoder increments / vong +// 0x606C:1/2 i32 Velocity actual, don vi r/min +// 0x6064:1/2 i32 Position actual, don vi "count" <-- doc o day +// 0x200E:1/2 u16 Encoder wire number (vung Factory Custom), mac dinh 1024 +// +// ⚠️ KHONG dung 0x608F (Position encoder resolution cua DS402). ZLAC8015D +// KHONG co object do -- Appendix A liet ke day du dai CiA402 va no dung o +// 6087h roi nhay thang toi 60FFh. So vong day encoder nam o 0x200E, trong +// vung rieng cua nha san xuat. // // ================== VI SAO DOC 0x6064 CHU KHONG TICH PHAN RPM ================== // `EncoderReading` yeu cau GOC QUAY TICH LUY, khong phai van toc. Ly do nam o @@ -31,12 +36,13 @@ // differential_drive_kinematics.cpp). Doc 0x6064 thay the: bo dem tuyet doi // trong driver motor, lo mot mau thi mau sau van dung vi no la TICH LUY. // -// ⚠️ CHUA XAC MINH TREN THIET BI: khong cho nao trong base_kit doc 0x6064, nen -// chua ai chung minh ZLAC8015D co expose object nay. DS402 bat buoc no cho -// che do position, nhung thiet bi chay Profile Velocity co the khong map. -// Open() THU DOC va tra loi RO neu khong co, thay vi im lang tra ve 0 -- -// encoder dung yen o 0 trong khi robot dang chay la trieu chung rat kho lan -// ra, no trong het nhu "odometry chet". +// DA XAC MINH TRONG TAI LIEU: can_help.pdf (ZLAC8015DD CANopen Communication +// Routine V1.0) Appendix A ghi ro: +// 6064h :01 Position_actual_value (left) I32 RO "unit: count" +// :02 Position_actual_value (right) I32 RO +// Open() van doc thu mot lan: neu vi ly do gi no khong tra loi thi phai biet +// NGAY, chu khong phai sau nua gio thay pose dung yen o goc 0 -- trieu chung +// do trong het nhu "odometry chet" va rat kho lan ra. // // ================== BUILD ================== // Can liblely-coapp. Tat bang -DVSLAM_WITH_CANOPEN=OFF thi file nay khong @@ -53,6 +59,7 @@ #include #include +#include #include #include #include @@ -101,14 +108,29 @@ std::optional SdoRead(lely::canopen::AsyncMaster& m, uint8_t node, class ZlacBase; +// Tin hieu "slave da boot xong", chia se giua bridge va driver. +struct BootGate { + std::mutex mu; + std::condition_variable cv; + bool booted = false; +}; + class LelyBridge : public lely::canopen::BasicDriver { public: - LelyBridge(lely::canopen::AsyncMaster& m, uint8_t id, std::atomic* lost) - : BasicDriver(m, id), lost_(lost) {} + LelyBridge(lely::canopen::AsyncMaster& m, uint8_t id, std::atomic* lost, + BootGate* gate) + : BasicDriver(m, id), lost_(lost), gate_(gate) {} protected: void OnBoot(lely::canopen::NmtState, char, const std::string&) noexcept override { master.Command(lely::canopen::NmtCommand::START, id()); + if (gate_) { + { + std::lock_guard lk(gate_->mu); + gate_->booted = true; + } + gate_->cv.notify_all(); + } } void OnHeartbeat(bool occurred) noexcept override { if (lost_) lost_->store(occurred); @@ -119,6 +141,7 @@ class LelyBridge : public lely::canopen::BasicDriver { private: std::atomic* lost_ = nullptr; + BootGate* gate_ = nullptr; }; class ZlacBase : public BaseInterface { @@ -158,7 +181,8 @@ class ZlacBase : public BaseInterface { chan_->open(*ctrl_); master_ = std::make_unique( *timer_, *chan_, cfg_.dcf_path, "", 127); - bridge_ = std::make_unique(*master_, cfg_.can_node_id, &lost_); + bridge_ = std::make_unique(*master_, cfg_.can_node_id, &lost_, + &boot_); master_->Reset(); lely_thread_ = std::thread([this] { try { loop_->run(); } catch (...) {} @@ -170,6 +194,7 @@ class ZlacBase : public BaseInterface { e.what() + " -- `ip link show " + cfg_.can_channel + "` co UP khong?"); } + if (StatusResponse st = WaitForBoot(); !st.ok()) return st; if (StatusResponse st = ProbeIdentity(); !st.ok()) return st; if (StatusResponse st = EnableDs402(); !st.ok()) return st; if (StatusResponse st = ResolveEncoderScale(); !st.ok()) return st; @@ -206,8 +231,16 @@ class ZlacBase : public BaseInterface { const double v_r = linear + angular * half; // m/s -> rad/s tai banh -> RPM - const double rpm_l = (v_l / cfg_.wheel_radius) * 60.0 / kTwoPi; - const double rpm_r = (v_r / cfg_.wheel_radius) * 60.0 / kTwoPi; + double rpm_l = (v_l / cfg_.wheel_radius) * 60.0 / kTwoPi; + double rpm_r = (v_r / cfg_.wheel_radius) * 60.0 / kTwoPi; + + // 0x60FF nhan -1000..1000 r/min (can_help.pdf Appendix A). Gioi han thuc + // te thap hon nhieu -- max_linear 0.35 m/s voi r=0.0865 chi ra ~39 RPM -- + // nhung kep o day de mot config sai khong bao gio gui duoc gia tri ma + // driver se tu dien giai lai. + constexpr double kMaxRpm = 1000.0; + rpm_l = Clamp(rpm_l, kMaxRpm); + rpm_r = Clamp(rpm_r, kMaxRpm); { std::lock_guard lk(cmd_mu_); @@ -231,6 +264,35 @@ class ZlacBase : public BaseInterface { return (v > lim) ? lim : ((v < -lim) ? -lim : v); } + // Cho slave BOOT XONG truoc khi gui SDO dau tien. + // + // ⚠️ Day la loi that, do duoc tren robot: ban dau Open() goi thang + // ProbeIdentity() ngay sau master_->Reset(). SDO dau tien di ra sau 116 ms + // va KHONG AI TRA LOI -- slave chua kip boot. Doc log thi trong y het + // "thiet bi khong ho tro object do", va da dan chan doan di sai huong hai + // lan. + // + // base_kit khong gap loi nay nhung cung khong xu ly no: no co mot ROS timer + // 500 ms goi lai enableHardware() cho toi khi duoc, nen boot xong luc nao + // thi tinh co vua dung luc do. Doi day dung tin hieu that -- callback + // OnBoot cua lely -- thay vi ngu mot khoang co dinh doan mo. + StatusResponse WaitForBoot() { + constexpr auto kTimeout = std::chrono::seconds(5); + std::unique_lock lk(boot_.mu); + if (boot_.cv.wait_for(lk, kTimeout, [this] { return boot_.booted; })) { + return StatusResponse::Ok(); + } + return StatusResponse::Error( + StatusCode::kTimeout, + "base: node " + std::to_string(cfg_.can_node_id) + + " khong boot trong 5 s (khong co callback OnBoot cua lely).\n" + " - master.dcf co khai bao dung node nay khong? lely chi boot " + "nhung slave co trong DCF.\n" + " - driver motor da co dien chua?\n" + " - `candump can0` co thay khung 0x70" + + std::to_string(cfg_.can_node_id) + " (heartbeat) khong?"); + } + // Hoi 0x1018:01 (Vendor-ID) TRUOC moi thu khac. // // Object nay BAT BUOC voi moi thiet bi CANopen -- no nam trong Identity @@ -245,14 +307,13 @@ class ZlacBase : public BaseInterface { StatusResponse ProbeIdentity() { // Thu NHIEU object, khong chi 0x1018. // - // ⚠️ Ban dau cho nay chi doc 0x1018:01 voi lap luan "DS301 bat buoc no, - // nen no im tuc la SDO hong". Lap luan do SAI voi thiet bi nay: base_kit - // chay duoc tren cung con driver ma KHONG BAO GIO doc 0x1018 -- no chi - // dung 0x6060, 0x6040, 0x606C. Driver gia re thuong bo trong Identity - // Object. + // can_help.pdf Appendix A xac nhan 0x1018:01 CO ton tai (Vendor Id = + // 0x0100), nen lan hong tren robot khong phai vi thieu object ma vi hoi + // qua som -- WaitForBoot() o tren la cho sua that su. // - // Nen: SDO duoc coi la thong neu BAT KY object nao tra loi. 0x606C la - // object base_kit doc thanh cong moi 20 ms, tuc no chac chan co. + // Van giu ca ba lan thu: chung re, va neu mot firmware ZLAC khac bo trong + // Identity Object thi 0x606C/0x6060 van cho di tiep. 0x606C la object + // base_kit doc thanh cong moi 20 ms nen no chac chan co. if (SdoRead(*master_, cfg_.can_node_id, 0x1018, 1)) { return StatusResponse::Ok(); } @@ -328,29 +389,59 @@ class ZlacBase : public BaseInterface { StatusResponse ResolveEncoderScale() { counts_per_rev_ = cfg_.encoder_counts_per_rev; if (counts_per_rev_ == 0) { - if (auto inc = SdoRead(*master_, cfg_.can_node_id, 0x608F, 1)) { - counts_per_rev_ = *inc; + // ⚠️ KHONG phai 0x608F. Object do la chuan DS402 (Position encoder + // resolution) nhung ZLAC8015D KHONG co no -- Appendix A cua + // can_help.pdf liet ke day du dai CiA402 va no dung o 6087h roi nhay + // thang toi 60FFh. Doc 0x608F se abort mai mai. + // + // So vong day cua encoder nam trong vung Factory Custom: 0x200E:01 + // (trai) / :02 (phai), "Encoder wire number setting", 0-4096, mac dinh + // 1024. + const auto lines = + SdoRead(*master_, cfg_.can_node_id, 0x200E, 1); + if (!lines || *lines == 0) { + return StatusResponse::Error( + StatusCode::kFailedPrecondition, + "base: khong doc duoc 0x200E:01 (Encoder wire number) va " + "encoder_counts_per_rev trong config dang la 0. Mac dinh cua " + "ZLAC8015D la 1024 -- dat tuong minh roi chay lai."); } - } - if (counts_per_rev_ == 0) { - return StatusResponse::Error( - StatusCode::kFailedPrecondition, - "base: khong biet so increment/vong. Thiet bi khong tra loi " - "0x608F:1, va encoder_counts_per_rev trong config dang la 0. Dat " - "tuong minh tu datasheet ZLAC8015D roi chay lai."); + + // ⚠️ GIA DINH CHUA KIEM CHUNG: counts = lines x 4 (giai ma cau phuong). + // + // Tai lieu goi 0x200E la "wire number" (so vong day) va goi don vi cua + // 0x6064 la "count". Hai ten khac nhau, va khong cho nao trong 37 trang + // noi he so giua chung. Suy doan x4 la quy uoc pho bien nhat, nhung no + // VAN LA SUY DOAN. + // + // Sai he so nay lam odometry sai dung 4 lan -- de thay, nhung van nen + // do cho chac. Cach do (2 phut): + // 1. Ke banh len khoi san + // 2. Doc 0x6064:01, quay banh dung MOT vong bang tay, doc lai + // 3. Hieu so chinh la counts/vong that + // 4. Dat vao sensors.yaml: base.encoder_counts_per_rev + counts_per_rev_ = static_cast(*lines) * 4u; + VSLAM_WARN("base: 0x200E:01 = " << *lines << " wire, SUY RA " + << counts_per_rev_ << " counts/vong (gia dinh x4 cau phuong, " + "CHUA kiem chung). Sai he so nay lam odometry sai 4 lan -- do " + "bang cach quay banh mot vong roi so hieu 0x6064, va dat vao " + "base.encoder_counts_per_rev."); } - // Doc thu 0x6064 -- neu thiet bi khong map object nay thi bao NGAY o day. + // 0x6064 la tru cot cua ca thiet ke nay, va can_help.pdf Appendix A xac + // nhan no CO: + // 6064h :01 Position_actual_value (left) I32 RO "unit: count" + // :02 Position_actual_value (right) I32 RO + // Van kiem luc Open(): neu vi ly do gi no khong tra loi thi phai biet + // NGAY, chu khong phai sau nua gio thay robot dung yen o goc 0. const auto l = SdoRead(*master_, cfg_.can_node_id, 0x6064, 1); const auto r = SdoRead(*master_, cfg_.can_node_id, 0x6064, 2); if (!l || !r) { return StatusResponse::Error( StatusCode::kFailedPrecondition, - "base: ZLAC8015D khong tra loi 0x6064 (Position Actual Value). " - "EncoderReading can goc quay TICH LUY, ma 0x606C chi cho van toc " - "tuc thoi -- tich phan van toc lam mat vinh vien moi doan duong bi " - "lo mot mau. Kiem lai PDO/SDO mapping cua driver, hoac doi thiet ke " - "sang chap nhan troi tich luy."); + "base: ZLAC8015D khong tra loi 0x6064 (Position Actual Value), du " + "can_help.pdf Appendix A liet ke no la I32 RO. Neu 0x606C doc duoc " + "ma 0x6064 thi khong, kiem lai firmware driver motor."); } origin_l_ = *l; origin_r_ = *r; @@ -462,6 +553,7 @@ class ZlacBase : public BaseInterface { std::unique_ptr bridge_; std::thread lely_thread_; + BootGate boot_; std::atomic running_{false}; std::atomic lost_{false}; bool ready_ = false;