upload
This commit is contained in:
345
libs/vslam_common/include/vslam/approx_sync.h
Normal file
345
libs/vslam_common/include/vslam/approx_sync.h
Normal file
@@ -0,0 +1,345 @@
|
||||
#ifndef VSLAM_APPROX_SYNC_H
|
||||
#define VSLAM_APPROX_SYNC_H
|
||||
|
||||
// Thay message_filters::Synchronizer<ApproximateTime<...>>.
|
||||
//
|
||||
// Cau hinh hien tai can khop lai: rtabmap.launch:235-237 dat approx_sync=true,
|
||||
// sync_queue_size=30, dong bo scan + odom + depth + rgb.
|
||||
// cliff_detect_nodelet.cpp:101-102 dong bo depth + camera_info.
|
||||
//
|
||||
// Thuat toan o day DON GIAN VA DE DOAN HON ApproximateTime cua ROS. ROS dung
|
||||
// mot thuat toan toi uu cua so co the giu ban tin lai kha lau va rat kho suy
|
||||
// luan khi mot nguon tut. O day:
|
||||
//
|
||||
// 1. Moi ban tin moi o stream PIVOT (scan, 25 Hz -- cham nhat) tao mot moc.
|
||||
// 2. Voi moi stream khac, chon ban tin co |stamp - pivot| nho nhat, yeu cau
|
||||
// <= max_slop_sec.
|
||||
// 3. Phat khi:
|
||||
// (a) du moi stream REQUIRED, va moi stream TUY CHON hoac da co hoac ban
|
||||
// tin moi nhat cua no DA VUOT pivot (khong the khop hon duoc nua); hoac
|
||||
// (b) now - pivot > wait_timeout_sec -- phat voi stream tuy chon de trong.
|
||||
// 4. Pivot cu hon queue_size bi bo, dem vao dropped_queue.
|
||||
//
|
||||
// Nhanh (b) khong lam mat tin hieu: RTAB-Map chay duoc voi scan + odom ma
|
||||
// khong can depth (subscribe_depth la tuy chon), chi mat visual word.
|
||||
//
|
||||
// SyncStats dem RIENG ba loai drop -- slop, timeout, queue -- vi ba nguyen
|
||||
// nhan nay can ba cach sua khac han nhau:
|
||||
// slop -> lech dong ho giua hai nguon, hoac max_slop_sec qua chat
|
||||
// timeout -> mot nguon cham hon du kien hoac da chet
|
||||
// queue -> consumer khong theo kip producer
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/stats.h"
|
||||
#include "vslam/unix_time.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
template <typename... Ts>
|
||||
class ApproxTimeSync {
|
||||
public:
|
||||
static constexpr size_t kN = sizeof...(Ts);
|
||||
using Tuple = std::tuple<std::shared_ptr<const Ts>...>;
|
||||
using Callback = std::function<void(UnixTime pivot_stamp, const Tuple&)>;
|
||||
|
||||
struct Options {
|
||||
size_t queue_size = 30; // khop sync_queue_size=30 hien tai
|
||||
double max_slop_sec = 0.05; // ~1.5 khung camera o 30 Hz
|
||||
double wait_timeout_sec = 0.20; // het han cho input tuy chon
|
||||
size_t pivot_index = 0; // stream cham nhat lam moc (scan)
|
||||
std::array<bool, kN> required{}; // scan+odom = true; depth+infra = false
|
||||
};
|
||||
|
||||
ApproxTimeSync(Options opt, Callback cb)
|
||||
: opt_(std::move(opt)), cb_(std::move(cb)) {
|
||||
if (opt_.queue_size == 0) opt_.queue_size = 1;
|
||||
if (opt_.pivot_index >= kN) opt_.pivot_index = 0;
|
||||
}
|
||||
|
||||
// Them ban tin vao stream thu I.
|
||||
template <size_t I>
|
||||
void Add(UnixTime stamp,
|
||||
std::shared_ptr<const std::tuple_element_t<I, std::tuple<Ts...>>> msg) {
|
||||
static_assert(I < kN, "chi so stream vuot so stream");
|
||||
std::vector<std::pair<UnixTime, Tuple>> emit;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
auto& q = queues_[I];
|
||||
if (!q.empty() && stamp < q.back().stamp) {
|
||||
// Stamp lui: ban ghi bi xao tron hoac hai nguon ghi cung stream.
|
||||
stats_.dropped_queue++;
|
||||
return;
|
||||
}
|
||||
q.push_back(Slot{stamp, std::static_pointer_cast<const void>(std::move(msg))});
|
||||
while (q.size() > opt_.queue_size) {
|
||||
q.pop_front();
|
||||
if (I == opt_.pivot_index) stats_.dropped_queue++;
|
||||
}
|
||||
CollectLocked(kTimeZero, false, &emit);
|
||||
}
|
||||
Fire(emit);
|
||||
}
|
||||
|
||||
// Lai nhanh wait_timeout. Goi tu vong lap cua luong so huu sync.
|
||||
void Tick(UnixTime now) {
|
||||
std::vector<std::pair<UnixTime, Tuple>> emit;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
CollectLocked(now, true, &emit);
|
||||
}
|
||||
Fire(emit);
|
||||
}
|
||||
|
||||
SyncStats stats() const {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
return stats_;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
for (auto& q : queues_) q.clear();
|
||||
stats_ = SyncStats{};
|
||||
}
|
||||
|
||||
private:
|
||||
struct Slot {
|
||||
UnixTime stamp;
|
||||
std::shared_ptr<const void> msg;
|
||||
};
|
||||
|
||||
// Goi khi da giu mu_. Khong goi callback o day -- chi gom vao `emit`.
|
||||
void CollectLocked(UnixTime now, bool allow_timeout,
|
||||
std::vector<std::pair<UnixTime, Tuple>>* emit) {
|
||||
auto& pivot_q = queues_[opt_.pivot_index];
|
||||
|
||||
while (!pivot_q.empty()) {
|
||||
const UnixTime pivot = pivot_q.front().stamp;
|
||||
|
||||
std::array<size_t, kN> pick{};
|
||||
std::array<bool, kN> have{};
|
||||
bool all_required = true;
|
||||
bool all_settled = true;
|
||||
double worst_slop = 0.0;
|
||||
|
||||
for (size_t s = 0; s < kN; ++s) {
|
||||
if (s == opt_.pivot_index) {
|
||||
pick[s] = 0;
|
||||
have[s] = true;
|
||||
continue;
|
||||
}
|
||||
const auto& q = queues_[s];
|
||||
if (q.empty()) {
|
||||
all_settled = false;
|
||||
if (opt_.required[s]) all_required = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t best = 0;
|
||||
double best_d = std::abs(SecondsBetween(q[0].stamp, pivot));
|
||||
for (size_t i = 1; i < q.size(); ++i) {
|
||||
const double d = std::abs(SecondsBetween(q[i].stamp, pivot));
|
||||
if (d < best_d) { best_d = d; best = i; }
|
||||
}
|
||||
|
||||
if (best_d <= opt_.max_slop_sec) {
|
||||
pick[s] = best;
|
||||
have[s] = true;
|
||||
worst_slop = std::max(worst_slop, best_d);
|
||||
} else {
|
||||
if (opt_.required[s]) all_required = false;
|
||||
// Ban tin moi nhat cua stream nay DA VUOT pivot: khong the khop hon
|
||||
// duoc nua, nen stream coi nhu da chot (du la chot rong).
|
||||
if (q.back().stamp <= pivot) all_settled = false;
|
||||
}
|
||||
}
|
||||
|
||||
const bool timed_out =
|
||||
allow_timeout && !now.isZero() &&
|
||||
SecondsBetween(now, pivot) > opt_.wait_timeout_sec;
|
||||
|
||||
if (!all_required) {
|
||||
if (!timed_out) break; // con co the cho them
|
||||
// Het gio ma van thieu stream bat buoc -> bo pivot nay.
|
||||
stats_.dropped_timeout++;
|
||||
pivot_q.pop_front();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!all_settled && !timed_out) break; // cho stream tuy chon
|
||||
|
||||
Tuple t;
|
||||
FillTuple(t, pick, have, std::index_sequence_for<Ts...>{});
|
||||
emit->emplace_back(pivot, std::move(t));
|
||||
|
||||
stats_.matched++;
|
||||
stats_.last_slop_sec = worst_slop;
|
||||
if (timed_out && !all_settled) stats_.dropped_timeout++;
|
||||
|
||||
// Bo moi ban tin da dung va moi ban tin cu hon no.
|
||||
for (size_t s = 0; s < kN; ++s) {
|
||||
if (s == opt_.pivot_index || !have[s]) continue;
|
||||
auto& q = queues_[s];
|
||||
q.erase(q.begin(), q.begin() + static_cast<long>(pick[s]) + 1);
|
||||
}
|
||||
pivot_q.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t... Is>
|
||||
void FillTuple(Tuple& t, const std::array<size_t, kN>& pick,
|
||||
const std::array<bool, kN>& have, std::index_sequence<Is...>) {
|
||||
(void)std::initializer_list<int>{
|
||||
(std::get<Is>(t) =
|
||||
have[Is] ? std::static_pointer_cast<
|
||||
const std::tuple_element_t<Is, std::tuple<Ts...>>>(
|
||||
queues_[Is][pick[Is]].msg)
|
||||
: nullptr,
|
||||
0)...};
|
||||
}
|
||||
|
||||
// Goi callback NGOAI khoa. Callback cua SLAM chan 100-600 ms; giu khoa
|
||||
// trong do se chan luong lidar_rx.
|
||||
void Fire(const std::vector<std::pair<UnixTime, Tuple>>& emit) {
|
||||
for (const auto& e : emit) cb_(e.first, e.second);
|
||||
}
|
||||
|
||||
Options opt_;
|
||||
Callback cb_;
|
||||
mutable std::mutex mu_;
|
||||
std::array<std::deque<Slot>, kN> queues_;
|
||||
SyncStats stats_;
|
||||
};
|
||||
|
||||
// --- Bien the hai luong ----------------------------------------------------
|
||||
// Truong hop hai nguon pho bien den muc dang co ban rieng: de doc hon, compile
|
||||
// nhanh hon, va khong phai dien std::array<bool,2> cho moi lan dung.
|
||||
// Dung boi cliff_detect (depth + camera_info).
|
||||
//
|
||||
// HEADER-ONLY, khong pimpl. Ban dau viet pimpl + khoi tao tuong minh cho
|
||||
// PairSync<Image, CameraInfo>, nhung nhu the thi khong ai dung duoc no voi
|
||||
// kieu khac -- ke ca unit test voi struct gia. Voi mot lop nho nhu nay thi
|
||||
// cai gia cua pimpl (mot ban khoi tao tuong minh cho moi cap kieu) lon hon
|
||||
// cai loi.
|
||||
template <typename A, typename B>
|
||||
class PairSync {
|
||||
public:
|
||||
using Callback = std::function<void(UnixTime, std::shared_ptr<const A>,
|
||||
std::shared_ptr<const B>)>;
|
||||
|
||||
PairSync(size_t queue_size, double max_slop_sec, Callback cb)
|
||||
: queue_size_(queue_size == 0 ? 1 : queue_size),
|
||||
max_slop_sec_(max_slop_sec),
|
||||
cb_(std::move(cb)) {}
|
||||
|
||||
void AddFirst(UnixTime stamp, std::shared_ptr<const A> msg) {
|
||||
std::vector<Emit> emit;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
qa_.push_back(SlotA{stamp, std::move(msg)});
|
||||
TrimLocked();
|
||||
MatchLocked(&emit);
|
||||
}
|
||||
Fire(emit);
|
||||
}
|
||||
|
||||
void AddSecond(UnixTime stamp, std::shared_ptr<const B> msg) {
|
||||
std::vector<Emit> emit;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
qb_.push_back(SlotB{stamp, std::move(msg)});
|
||||
TrimLocked();
|
||||
MatchLocked(&emit);
|
||||
}
|
||||
Fire(emit);
|
||||
}
|
||||
|
||||
SyncStats stats() const {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
return stats_;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
qa_.clear();
|
||||
qb_.clear();
|
||||
stats_ = SyncStats{};
|
||||
}
|
||||
|
||||
private:
|
||||
struct SlotA { UnixTime stamp; std::shared_ptr<const A> msg; };
|
||||
struct SlotB { UnixTime stamp; std::shared_ptr<const B> msg; };
|
||||
struct Emit {
|
||||
UnixTime stamp;
|
||||
std::shared_ptr<const A> a;
|
||||
std::shared_ptr<const B> b;
|
||||
};
|
||||
|
||||
// Goi khi da giu mu_. Chi gom vao `emit`, khong goi callback o day.
|
||||
void MatchLocked(std::vector<Emit>* emit) {
|
||||
while (!qa_.empty() && !qb_.empty()) {
|
||||
const SlotA& a = qa_.front();
|
||||
|
||||
size_t best = 0;
|
||||
double best_d = std::abs(SecondsBetween(qb_[0].stamp, a.stamp));
|
||||
for (size_t i = 1; i < qb_.size(); ++i) {
|
||||
const double d = std::abs(SecondsBetween(qb_[i].stamp, a.stamp));
|
||||
if (d < best_d) { best_d = d; best = i; }
|
||||
}
|
||||
|
||||
if (best_d <= max_slop_sec_) {
|
||||
emit->push_back(Emit{a.stamp, a.msg, qb_[best].msg});
|
||||
stats_.matched++;
|
||||
stats_.last_slop_sec = best_d;
|
||||
// Bo moi phan tu b cu hon phan tu vua khop: chung khong con co hoi
|
||||
// khop voi bat ky a nao ve sau (a chi tien len).
|
||||
qb_.erase(qb_.begin(), qb_.begin() + static_cast<long>(best) + 1);
|
||||
qa_.pop_front();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Chua khop duoc. Neu a da cu hon MOI phan tu b thi no khong bao gio
|
||||
// khop nua -> bo. Nguoc lai thi cho them b.
|
||||
if (a.stamp < qb_.back().stamp) {
|
||||
stats_.dropped_slop++;
|
||||
qa_.pop_front();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TrimLocked() {
|
||||
while (qa_.size() > queue_size_) { qa_.pop_front(); stats_.dropped_queue++; }
|
||||
while (qb_.size() > queue_size_) { qb_.pop_front(); stats_.dropped_queue++; }
|
||||
}
|
||||
|
||||
// Goi callback NGOAI khoa. Callback cua cliff_detect lam viec nang
|
||||
// (ray-cast toan anh depth); giu khoa trong do se chan luong camera.
|
||||
void Fire(const std::vector<Emit>& emit) {
|
||||
for (const auto& e : emit) cb_(e.stamp, e.a, e.b);
|
||||
}
|
||||
|
||||
size_t queue_size_;
|
||||
double max_slop_sec_;
|
||||
Callback cb_;
|
||||
mutable std::mutex mu_;
|
||||
std::deque<SlotA> qa_;
|
||||
std::deque<SlotB> qb_;
|
||||
SyncStats stats_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_APPROX_SYNC_H
|
||||
Reference in New Issue
Block a user