248 lines
7.5 KiB
C++
248 lines
7.5 KiB
C++
#ifndef VSLAM_CHANNEL_H
|
|
#define VSLAM_CHANNEL_H
|
|
|
|
// Thay pub/sub cua ROS.
|
|
//
|
|
// QUY TAC RANH GIOI -- doc truoc khi dung:
|
|
//
|
|
// Channel<T> CHI dung khi du lieu di QUA BIEN LUONG.
|
|
// Trong cung mot luong: goi ham truc tiep.
|
|
//
|
|
// Day la cach giu nguyen loi the "3 nodelet chung 1 manager" cua ban ROS.
|
|
// Chuoi depth_to_cloud -> depth_filter -> gravity_align -> cliff_detect chay
|
|
// inline tren chinh luong camera trong mot loi goi PerceptionPipeline::Process().
|
|
// Tot hon nodelet manager: khong intrusive_ptr, khong dispatch theo topic,
|
|
// khong nodelet loader.
|
|
//
|
|
// Don vi du lieu duy nhat la std::shared_ptr<const T>. Publish() copy mot
|
|
// con tro (mot atomic increment), khong bao gio copy cloud/anh/scan.
|
|
//
|
|
// KHAC BIET QUAN TRONG SO VOI QUEUE CUA ROS: so ban tin bi bo duoc DEM va bao
|
|
// cao ra /api/status. Queue cua ROS drop im lang, va do la ly do repo cu phai
|
|
// doan mo ve viec costmap co nhan du cloud hay khong.
|
|
//
|
|
// AN TOAN LUONG:
|
|
// * Publish() khong bao gio chan. Day la bat buoc: luong lidar_rx hay
|
|
// camera_rx khong duoc phep dung cho luong slam (Rtabmap::process() chan
|
|
// 100-600 ms).
|
|
// * Latest() la duong doc cua luong safety: mot lan copy shared_ptr duoi
|
|
// khoa, khong bao gio giu khoa khi goi code nguoi dung.
|
|
// * Tap Reader co dinh truoc khi chay. MakeReader() phai goi xong trong
|
|
// giai doan wiring, khong duoc goi khi he dang chay.
|
|
|
|
#include <atomic>
|
|
#include <cassert>
|
|
#include <condition_variable>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vslam/stats.h"
|
|
#include "vslam/unix_time.h"
|
|
|
|
namespace vslam {
|
|
|
|
template <typename T>
|
|
class Channel {
|
|
public:
|
|
using Ptr = std::shared_ptr<const T>;
|
|
|
|
struct Item {
|
|
Ptr msg;
|
|
UnixTime stamp;
|
|
uint64_t seq = 0;
|
|
};
|
|
|
|
Channel(size_t capacity, std::string name)
|
|
: capacity_(capacity), name_(std::move(name)), ring_(capacity) {
|
|
assert(capacity > 0 && "Channel capacity phai > 0");
|
|
}
|
|
|
|
Channel(const Channel&) = delete;
|
|
Channel& operator=(const Channel&) = delete;
|
|
|
|
// Khong bao gio chan. Tra ve false neu phai ghi de mot phan tu ma con reader
|
|
// chua doc toi -- goi co the dung gia tri nay de dem hoac de canh bao.
|
|
bool Publish(Ptr msg, UnixTime stamp) {
|
|
bool overwrote = false;
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
const uint64_t seq = head_++;
|
|
// Ghi de o cu nhat neu vong da day.
|
|
if (seq >= capacity_) {
|
|
const uint64_t evicted = seq - capacity_;
|
|
for (const auto& r : readers_) {
|
|
if (r->cursor.load(std::memory_order_relaxed) <= evicted) {
|
|
overwrote = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
Item& slot = ring_[seq % capacity_];
|
|
slot.msg = std::move(msg);
|
|
slot.stamp = stamp;
|
|
slot.seq = seq;
|
|
|
|
latest_ = slot;
|
|
published_++;
|
|
if (overwrote) overwritten_++;
|
|
}
|
|
rate_.Tick();
|
|
cv_.notify_all();
|
|
return !overwrote;
|
|
}
|
|
|
|
// Con tro doc rieng cua tung consumer. Khong sao chep duoc -- moi consumer
|
|
// giu mot Reader va chi minh no dung.
|
|
class Reader {
|
|
public:
|
|
// Chan toi khi co ban tin moi hoac het `timeout`. Tra false neu het gio
|
|
// hoac channel da dong.
|
|
bool Read(Ptr* out, UnixTime* stamp, std::chrono::milliseconds timeout) {
|
|
std::unique_lock<std::mutex> lk(ch_->mu_);
|
|
const bool got = ch_->cv_.wait_for(lk, timeout, [this] {
|
|
return ch_->closed_ || state_->cursor.load(std::memory_order_relaxed) < ch_->head_;
|
|
});
|
|
if (!got || ch_->closed_) return false;
|
|
return PopLocked(out, stamp);
|
|
}
|
|
|
|
// Khong chan.
|
|
bool TryRead(Ptr* out, UnixTime* stamp) {
|
|
std::lock_guard<std::mutex> lk(ch_->mu_);
|
|
if (state_->cursor.load(std::memory_order_relaxed) >= ch_->head_) return false;
|
|
return PopLocked(out, stamp);
|
|
}
|
|
|
|
// So ban tin reader nay KHONG BAO GIO nhin thay vi bi ghi de.
|
|
uint64_t dropped() const { return state_->dropped.load(std::memory_order_relaxed); }
|
|
|
|
// So ban tin dang cho doc.
|
|
uint64_t lag() const {
|
|
std::lock_guard<std::mutex> lk(ch_->mu_);
|
|
const uint64_t c = state_->cursor.load(std::memory_order_relaxed);
|
|
return ch_->head_ > c ? ch_->head_ - c : 0;
|
|
}
|
|
|
|
const std::string& name() const { return state_->name; }
|
|
|
|
private:
|
|
friend class Channel<T>;
|
|
struct State {
|
|
std::atomic<uint64_t> cursor{0};
|
|
std::atomic<uint64_t> dropped{0};
|
|
std::string name;
|
|
};
|
|
|
|
Reader(Channel<T>* ch, State* st) : ch_(ch), state_(st) {}
|
|
|
|
// Goi khi da giu mu_.
|
|
bool PopLocked(Ptr* out, UnixTime* stamp) {
|
|
uint64_t c = state_->cursor.load(std::memory_order_relaxed);
|
|
const uint64_t oldest = ch_->head_ > ch_->capacity_ ? ch_->head_ - ch_->capacity_ : 0;
|
|
if (c < oldest) {
|
|
// Reader da bi nhay qua. Dem phan mat roi tua toi o cu nhat con song.
|
|
state_->dropped.fetch_add(oldest - c, std::memory_order_relaxed);
|
|
c = oldest;
|
|
}
|
|
const Item& slot = ch_->ring_[c % ch_->capacity_];
|
|
*out = slot.msg;
|
|
if (stamp) *stamp = slot.stamp;
|
|
state_->cursor.store(c + 1, std::memory_order_relaxed);
|
|
return true;
|
|
}
|
|
|
|
Channel<T>* ch_ = nullptr;
|
|
State* state_ = nullptr;
|
|
};
|
|
|
|
// Phai goi xong trong giai doan wiring, truoc khi bat ky luong nao chay.
|
|
// `name` chi de bao cao so lieu.
|
|
Reader MakeReader(std::string name) {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
assert(!started_ && "MakeReader() sau khi he da chay");
|
|
auto st = std::make_unique<typename Reader::State>();
|
|
st->name = std::move(name);
|
|
st->cursor.store(head_, std::memory_order_relaxed); // bat dau tu hien tai
|
|
auto* raw = st.get();
|
|
readers_.push_back(std::move(st));
|
|
return Reader(this, raw);
|
|
}
|
|
|
|
// Danh dau ket thuc wiring. Sau loi goi nay MakeReader() se assert.
|
|
void Freeze() {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
started_ = true;
|
|
}
|
|
|
|
// O(1), khong dung cursor. Day la duong doc cua luong safety va web_tx:
|
|
// chung khong quan tam lich su, chi can ban moi nhat.
|
|
bool Latest(Ptr* out, UnixTime* stamp = nullptr) const {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
if (!latest_.msg) return false;
|
|
*out = latest_.msg;
|
|
if (stamp) *stamp = latest_.stamp;
|
|
return true;
|
|
}
|
|
|
|
// Danh thuc moi reader dang cho.
|
|
void Close() {
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
closed_ = true;
|
|
}
|
|
cv_.notify_all();
|
|
}
|
|
|
|
bool closed() const {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
return closed_;
|
|
}
|
|
|
|
ChannelStats stats() const {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
ChannelStats s;
|
|
s.name = name_;
|
|
s.capacity = capacity_;
|
|
s.published = published_;
|
|
s.overwritten = overwritten_;
|
|
s.readers = readers_.size();
|
|
s.hz = rate_.hz();
|
|
for (const auto& r : readers_) {
|
|
const uint64_t c = r->cursor.load(std::memory_order_relaxed);
|
|
const uint64_t lag = head_ > c ? head_ - c : 0;
|
|
if (lag > s.max_reader_lag) s.max_reader_lag = lag;
|
|
s.total_reader_drops += r->dropped.load(std::memory_order_relaxed);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
const std::string& name() const { return name_; }
|
|
size_t capacity() const { return capacity_; }
|
|
|
|
private:
|
|
mutable std::mutex mu_;
|
|
std::condition_variable cv_;
|
|
|
|
const size_t capacity_;
|
|
const std::string name_;
|
|
|
|
std::vector<Item> ring_;
|
|
uint64_t head_ = 0; // seq se cap cho lan Publish ke tiep
|
|
Item latest_;
|
|
bool closed_ = false;
|
|
bool started_ = false;
|
|
|
|
uint64_t published_ = 0;
|
|
uint64_t overwritten_ = 0;
|
|
mutable RateMonitor rate_{2.0};
|
|
|
|
std::vector<std::unique_ptr<typename Reader::State>> readers_;
|
|
};
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_CHANNEL_H
|