This commit is contained in:
trung09122002
2026-09-21 09:02:29 +07:00
parent b4b9f7ec7c
commit 3090cf4124
3 changed files with 51 additions and 6 deletions

View File

@@ -207,9 +207,10 @@ class ZlacWrapper : public BaseInterface {
if (!impl_) return; if (!impl_) return;
int32_t l = 0, r = 0; int32_t l = 0, r = 0;
double skew = 0.0; double skew = 0.0, age = 0.0;
uint64_t seq = 0;
EncoderReading out; EncoderReading out;
if (!impl_->getPositionCounts(l, r, &skew)) { if (!impl_->getPositionCounts(l, r, &skew, &seq, &age)) {
out.stamp = clock_->Now(); out.stamp = clock_->Now();
out.valid = false; out.valid = false;
{ {
@@ -221,7 +222,21 @@ class ZlacWrapper : public BaseInterface {
if (cb_) cb_(out); if (cb_) cb_(out);
return; return;
} }
out.stamp = clock_->Now(); // ⚠️ BO QUA mau KHONG DOI.
//
// telemetryLoop cua driver cap nhat cache o 50 Hz, va vong poll nay cung
// 50 Hz. Hai vong doc lap cung tan so bi NHIEU PHA: co luc hai lan poll
// thay cung mot gia tri -> van toc ra 0; co luc mot lan poll nuot hai lan
// cap nhat -> van toc gap doi. Da do tren robot: cach mot lai ra
// "encoder 0.25 / gyro 0" roi "encoder 0 / gyro 0.7", xen ke -- va
// wheel_odometry doc ca hai thanh TRUOT BANH.
//
// Chi phat khi `seq` doi, tuc khi thuc su co mau moi.
if (seq == last_seq_) return;
last_seq_ = seq;
// Dong dau theo luc LAY MAU, khong phai luc doc cache: tru tuoi cua mau.
out.stamp = clock_->Now() - Duration::fromSec(age);
if (!have_origin_) { if (!have_origin_) {
// Tru moc dau tien de pose bat dau tu 0 thay vi tu mot so tuy y con lai // Tru moc dau tien de pose bat dau tu 0 thay vi tu mot so tuy y con lai
@@ -298,6 +313,8 @@ class ZlacWrapper : public BaseInterface {
double prev_left_counts_ = 0.0; double prev_left_counts_ = 0.0;
UnixTime prev_left_stamp_; UnixTime prev_left_stamp_;
bool have_prev_left_ = false; bool have_prev_left_ = false;
// Mau gan nhat da phat. Chi phat khi driver co mau MOI -- xem ReadEncoders.
uint64_t last_seq_ = 0;
mutable std::mutex cmd_mu_; mutable std::mutex cmd_mu_;
double cmd_linear_ = 0.0, cmd_angular_ = 0.0; double cmd_linear_ = 0.0, cmd_angular_ = 0.0;

View File

@@ -11,6 +11,7 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <mutex>
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
@@ -112,7 +113,9 @@ public:
// phan van toc thi lo mot mau telemetry la mat vinh vien mot doan quang // phan van toc thi lo mot mau telemetry la mat vinh vien mot doan quang
// duong ma khong phat hien duoc. Xem thirdparty/PROVENANCE.md. // duong ma khong phat hien duoc. Xem thirdparty/PROVENANCE.md.
bool getPositionCounts(int32_t& out_left, int32_t& out_right, bool getPositionCounts(int32_t& out_left, int32_t& out_right,
double* out_skew_sec = nullptr); double* out_skew_sec = nullptr,
uint64_t* out_seq = nullptr,
double* out_age_sec = nullptr);
bool isReady() const { return hardware_ready_.load(); } bool isReady() const { return hardware_ready_.load(); }
bool hasConnectionLost() const { return connection_lost_.load(); } bool hasConnectionLost() const { return connection_lost_.load(); }
@@ -157,6 +160,16 @@ private:
std::atomic<bool> cached_pos_valid_{false}; std::atomic<bool> cached_pos_valid_{false};
// [vslam] Banh phai lay mau muon hon banh trai bay nhieu giay. // [vslam] Banh phai lay mau muon hon banh trai bay nhieu giay.
std::atomic<double> cached_pos_skew_sec_{0.0}; std::atomic<double> cached_pos_skew_sec_{0.0};
// [vslam] Dem so lan cache vi tri duoc CAP NHAT, va thoi diem cap nhat.
//
// telemetryLoop chay 50 Hz, ma ben goi cung poll 50 Hz -- hai vong doc lap
// cung tan so thi bi nhieu pha: co luc hai lan poll thay CUNG mot gia tri
// (van toc ra 0), co luc mot lan poll nuot HAI lan cap nhat (van toc gap
// doi). Ben goi dung `seq` de bo qua mau khong doi, va `age` de dong dau
// theo luc LAY MAU chu khong phai luc doc cache.
std::atomic<uint64_t> cached_pos_seq_{0};
std::chrono::steady_clock::time_point cached_pos_time_{};
std::mutex cached_pos_time_mu_;
void telemetryLoop(); void telemetryLoop();
// Target caching // Target caching

View File

@@ -316,7 +316,14 @@ void ZlacMotorDriver::telemetryLoop() {
pos_ok = true; pos_ok = true;
} }
} }
if (pos_ok) cached_pos_valid_ = true; if (pos_ok) {
{
std::lock_guard<std::mutex> lk(cached_pos_time_mu_);
cached_pos_time_ = std::chrono::steady_clock::now();
}
cached_pos_seq_.fetch_add(1);
cached_pos_valid_ = true;
}
} }
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // 50 Hz telemetry poll std::this_thread::sleep_for(std::chrono::milliseconds(20)); // 50 Hz telemetry poll
} }
@@ -334,12 +341,20 @@ bool ZlacMotorDriver::getActualRPM(double& out_left_rpm, double& out_right_rpm)
// [vslam] Xem PROVENANCE.md. 0x6064 la bo dem TUYET DOI trong driver motor: // [vslam] Xem PROVENANCE.md. 0x6064 la bo dem TUYET DOI trong driver motor:
// lo mot mau thi mau sau van dung, khac han tich phan van toc. // lo mot mau thi mau sau van dung, khac han tich phan van toc.
bool ZlacMotorDriver::getPositionCounts(int32_t& out_left, int32_t& out_right, bool ZlacMotorDriver::getPositionCounts(int32_t& out_left, int32_t& out_right,
double* out_skew_sec) { double* out_skew_sec,
uint64_t* out_seq,
double* out_age_sec) {
if (!cached_pos_valid_.load()) return false; if (!cached_pos_valid_.load()) return false;
out_left = cached_left_pos_raw_.load(); out_left = cached_left_pos_raw_.load();
out_right = cached_right_pos_raw_.load(); out_right = cached_right_pos_raw_.load();
// Banh PHAI duoc lay mau muon hon banh TRAI bay nhieu giay. 0 khi di qua PDO. // Banh PHAI duoc lay mau muon hon banh TRAI bay nhieu giay. 0 khi di qua PDO.
if (out_skew_sec) *out_skew_sec = cached_pos_skew_sec_.load(); if (out_skew_sec) *out_skew_sec = cached_pos_skew_sec_.load();
if (out_seq) *out_seq = cached_pos_seq_.load();
if (out_age_sec) {
std::lock_guard<std::mutex> lk(cached_pos_time_mu_);
*out_age_sec = std::chrono::duration<double>(
std::chrono::steady_clock::now() - cached_pos_time_).count();
}
return true; return true;
} }