update
This commit is contained in:
@@ -171,66 +171,89 @@ void EncodeMapFull(const OccupancyGrid& map, UnixTime stamp, std::string* out,
|
||||
}
|
||||
}
|
||||
|
||||
bool EncodeMapPatch(const OccupancyGrid& map, const OccupancyGrid& prev,
|
||||
UnixTime stamp, std::string* out) {
|
||||
// Kich thuoc doi -> khong vá duoc, phai gui full.
|
||||
if (map.width != prev.width || map.height != prev.height ||
|
||||
map.data.size() != prev.data.size() ||
|
||||
std::abs(map.resolution - prev.resolution) > 1e-6f ||
|
||||
std::abs(map.origin.position.x - prev.origin.position.x) > 1e-6 ||
|
||||
std::abs(map.origin.position.y - prev.origin.position.y) > 1e-6) {
|
||||
return false;
|
||||
// --- Ban do voxel 3D -------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr size_t kVoxelFullHeaderBytes = 32; // sau header chung 16 byte
|
||||
constexpr size_t kVoxelCellBytes = 6; // i16 * 3
|
||||
|
||||
void PutCells(std::string* out, const VoxelIndex* cells, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
PutI16(out, cells[i].x);
|
||||
PutI16(out, cells[i].y);
|
||||
PutI16(out, cells[i].z);
|
||||
}
|
||||
}
|
||||
|
||||
// Vung bao cac o doi.
|
||||
int32_t x0 = static_cast<int32_t>(map.width);
|
||||
int32_t y0 = static_cast<int32_t>(map.height);
|
||||
int32_t x1 = -1, y1 = -1;
|
||||
|
||||
for (uint32_t y = 0; y < map.height; ++y) {
|
||||
const size_t row = static_cast<size_t>(y) * map.width;
|
||||
for (uint32_t x = 0; x < map.width; ++x) {
|
||||
if (map.data[row + x] == prev.data[row + x]) continue;
|
||||
const int32_t xi = static_cast<int32_t>(x);
|
||||
const int32_t yi = static_cast<int32_t>(y);
|
||||
if (xi < x0) x0 = xi;
|
||||
if (yi < y0) y0 = yi;
|
||||
if (xi > x1) x1 = xi;
|
||||
if (yi > y1) y1 = yi;
|
||||
}
|
||||
}
|
||||
|
||||
if (x1 < 0) return false; // khong o nao doi
|
||||
|
||||
const int32_t w = x1 - x0 + 1;
|
||||
const int32_t h = y1 - y0 + 1;
|
||||
|
||||
std::vector<int8_t> patch;
|
||||
patch.reserve(static_cast<size_t>(w) * static_cast<size_t>(h));
|
||||
for (int32_t y = y0; y <= y1; ++y) {
|
||||
const size_t row = static_cast<size_t>(y) * map.width;
|
||||
for (int32_t x = x0; x <= x1; ++x) {
|
||||
patch.push_back(map.data[row + static_cast<size_t>(x)]);
|
||||
}
|
||||
}
|
||||
|
||||
PutHeader(out, BinaryType::kMapPatch, stamp);
|
||||
PutI32(out, x0);
|
||||
PutI32(out, y0);
|
||||
PutI32(out, w);
|
||||
PutI32(out, h);
|
||||
|
||||
const size_t run_count_off = out->size();
|
||||
PutU32(out, 0);
|
||||
const size_t runs = RleEncode(patch, out);
|
||||
const uint32_t r32 = static_cast<uint32_t>(runs);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
(*out)[run_count_off + static_cast<size_t>(i)] =
|
||||
static_cast<char>((r32 >> (8 * i)) & 0xFF);
|
||||
bool GetCells(const std::string& b, size_t off, size_t n,
|
||||
std::vector<VoxelIndex>* out) {
|
||||
if (b.size() < off + n * kVoxelCellBytes) return false;
|
||||
out->reserve(out->size() + n);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const size_t o = off + i * kVoxelCellBytes;
|
||||
VoxelIndex k;
|
||||
// Qua uint16 roi ep ve int16: ep thang tu char la noi dau am di lac.
|
||||
k.x = static_cast<int16_t>(GetU16(b, o));
|
||||
k.y = static_cast<int16_t>(GetU16(b, o + 2));
|
||||
k.z = static_cast<int16_t>(GetU16(b, o + 4));
|
||||
out->push_back(k);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
size_t VoxelCellsPerChunk(uint32_t chunk_bytes) {
|
||||
const size_t usable = chunk_bytes > (kBinaryHeaderSize + kVoxelFullHeaderBytes)
|
||||
? chunk_bytes - kBinaryHeaderSize - kVoxelFullHeaderBytes
|
||||
: 0;
|
||||
const size_t n = usable / kVoxelCellBytes;
|
||||
return n > 0 ? n : 1; // khong bao gio 0: se lap vo han khi chia manh
|
||||
}
|
||||
|
||||
uint16_t VoxelChunkCount(const VoxelGrid& grid, uint32_t chunk_bytes) {
|
||||
const size_t per = VoxelCellsPerChunk(chunk_bytes);
|
||||
// Ban do RONG van phai co MOT manh: do la cach client biet "gio khong con
|
||||
// gi" thay vi giu mai tap cu.
|
||||
if (grid.cells.empty()) return 1;
|
||||
const size_t n = (grid.cells.size() + per - 1) / per;
|
||||
return static_cast<uint16_t>(std::min<size_t>(n, 65535));
|
||||
}
|
||||
|
||||
void EncodeVoxelFull(const VoxelGrid& grid, UnixTime stamp, size_t first,
|
||||
size_t count, uint16_t chunk_index, uint16_t chunk_count,
|
||||
std::string* out) {
|
||||
const size_t total = grid.cells.size();
|
||||
const size_t begin = std::min(first, total);
|
||||
const size_t n = std::min(count, total - begin);
|
||||
|
||||
PutHeader(out, BinaryType::kVoxelFull, stamp);
|
||||
PutF32(out, grid.resolution);
|
||||
PutF32(out, static_cast<float>(grid.origin_x));
|
||||
PutF32(out, static_cast<float>(grid.origin_y));
|
||||
PutF32(out, static_cast<float>(grid.origin_z));
|
||||
PutU32(out, grid.epoch);
|
||||
PutU32(out, static_cast<uint32_t>(total));
|
||||
PutU16(out, chunk_index);
|
||||
PutU16(out, chunk_count);
|
||||
PutU32(out, static_cast<uint32_t>(n));
|
||||
PutCells(out, grid.cells.data() + begin, n);
|
||||
}
|
||||
|
||||
void EncodeVoxelDelta(uint32_t epoch, uint32_t seq,
|
||||
const std::vector<VoxelIndex>& added,
|
||||
const std::vector<VoxelIndex>& removed, UnixTime stamp,
|
||||
std::string* out) {
|
||||
PutHeader(out, BinaryType::kVoxelDelta, stamp);
|
||||
PutU32(out, epoch);
|
||||
PutU32(out, seq);
|
||||
PutU32(out, static_cast<uint32_t>(added.size()));
|
||||
PutU32(out, static_cast<uint32_t>(removed.size()));
|
||||
PutCells(out, added.data(), added.size());
|
||||
PutCells(out, removed.data(), removed.size());
|
||||
}
|
||||
|
||||
void EncodePath(const std::vector<Pose2D>& path, UnixTime stamp, double min_step_m,
|
||||
std::string* out) {
|
||||
std::vector<const Pose2D*> kept;
|
||||
@@ -332,4 +355,38 @@ bool DecodeMapFull(const std::string& buf, OccupancyGrid* out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DecodeVoxelFull(const std::string& buf, VoxelChunkInfo* info,
|
||||
std::vector<VoxelIndex>* out) {
|
||||
BinaryHeader h;
|
||||
if (!DecodeHeader(buf, 0, &h) || h.type != BinaryType::kVoxelFull) return false;
|
||||
if (buf.size() < kBinaryHeaderSize + 32) return false;
|
||||
const size_t o = kBinaryHeaderSize;
|
||||
|
||||
info->epoch = GetU32(buf, o + 16);
|
||||
info->total = GetU32(buf, o + 20);
|
||||
info->chunk_index = GetU16(buf, o + 24);
|
||||
info->chunk_count = GetU16(buf, o + 26);
|
||||
const uint32_t n = GetU32(buf, o + 28);
|
||||
return GetCells(buf, o + 32, n, out);
|
||||
}
|
||||
|
||||
bool DecodeVoxelDelta(const std::string& buf, uint32_t* epoch, uint32_t* seq,
|
||||
std::vector<VoxelIndex>* added,
|
||||
std::vector<VoxelIndex>* removed) {
|
||||
BinaryHeader h;
|
||||
if (!DecodeHeader(buf, 0, &h) || h.type != BinaryType::kVoxelDelta) return false;
|
||||
if (buf.size() < kBinaryHeaderSize + 16) return false;
|
||||
const size_t o = kBinaryHeaderSize;
|
||||
|
||||
*epoch = GetU32(buf, o);
|
||||
*seq = GetU32(buf, o + 4);
|
||||
const uint32_t n_add = GetU32(buf, o + 8);
|
||||
const uint32_t n_del = GetU32(buf, o + 12);
|
||||
|
||||
added->clear();
|
||||
removed->clear();
|
||||
if (!GetCells(buf, o + 16, n_add, added)) return false;
|
||||
return GetCells(buf, o + 16 + static_cast<size_t>(n_add) * 6, n_del, removed);
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
@@ -188,7 +188,12 @@ void WriteStateJson(const WebSnapshot& s, std::string* out) {
|
||||
Int("prox_id", s.slam.proximity_id, out); Comma(out);
|
||||
Int("prox_total", s.slam.proximity_total, out); Comma(out);
|
||||
Int("map_version", s.slam.map_version, out); Comma(out);
|
||||
Num("process_ms", s.slam.process_ms, 1, out);
|
||||
Num("process_ms", s.slam.process_ms, 1, out); Comma(out);
|
||||
// Ban do 3D: nguon va so o. Mot ban do voxel RONG phai doc duoc tu day, chu
|
||||
// khong phai bang cach mo khung 3D len roi doan.
|
||||
Str("voxels_source", s.slam.voxels_source, out); Comma(out);
|
||||
Int("voxel_cells", static_cast<int>(s.slam.voxel_cells), out); Comma(out);
|
||||
Int("voxel_epoch", static_cast<int>(s.slam.voxel_epoch), out);
|
||||
out->push_back('}'); Comma(out);
|
||||
|
||||
Key("teleop", out);
|
||||
|
||||
@@ -265,6 +265,20 @@ class WsSession : public std::enable_shared_from_this<WsSession>,
|
||||
void mark_world_map_sent() { world_map_sent_.store(true); }
|
||||
void set_acked_map_version(uint32_t v) { acked_map_version_.store(v); }
|
||||
|
||||
// --- Ban do voxel 3D ---
|
||||
// `voxel_epoch_` = epoch client DA co du. 0 = chua co gi.
|
||||
// `voxel_sync_from_` = da bom toi o thu may cua epoch dang dong bo.
|
||||
uint32_t voxel_epoch() const { return voxel_epoch_.load(); }
|
||||
void set_voxel_epoch(uint32_t v) { voxel_epoch_.store(v); }
|
||||
bool voxel_resync() const { return voxel_resync_.load(); }
|
||||
void set_voxel_resync(bool v) { voxel_resync_.store(v); }
|
||||
uint32_t voxel_sync_epoch() const { return voxel_sync_epoch_.load(); }
|
||||
size_t voxel_sync_from() const { return voxel_sync_from_.load(); }
|
||||
void set_voxel_sync(uint32_t epoch, size_t from) {
|
||||
voxel_sync_epoch_.store(epoch);
|
||||
voxel_sync_from_.store(from);
|
||||
}
|
||||
|
||||
void Close() override {
|
||||
auto self = shared_from_this();
|
||||
asio::post(ws_.get_executor(), [self] {
|
||||
@@ -342,6 +356,23 @@ class WsSession : public std::enable_shared_from_this<WsSession>,
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Client da rap du moi manh cua mot epoch voxel.
|
||||
if (type == "ack_voxel") {
|
||||
double v = 0.0;
|
||||
if (JsonGetNumber(msg, "epoch", &v) && v >= 0.0) {
|
||||
set_voxel_epoch(static_cast<uint32_t>(v));
|
||||
set_voxel_resync(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Client phat hien hut so thu tu delta hoac lech epoch -> xin ca tap.
|
||||
if (type == "voxel_resync") {
|
||||
set_voxel_resync(true);
|
||||
set_voxel_epoch(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Enqueue(std::shared_ptr<const std::string> payload, bool is_binary, bool is_map) {
|
||||
@@ -401,6 +432,10 @@ class WsSession : public std::enable_shared_from_this<WsSession>,
|
||||
// Ban do the gioi la TINH trong mo phong -- gui mot lan la du, khong can
|
||||
// co che version/ACK nhu ban do SLAM.
|
||||
std::atomic<bool> world_map_sent_{false};
|
||||
std::atomic<uint32_t> voxel_epoch_{0};
|
||||
std::atomic<bool> voxel_resync_{false};
|
||||
std::atomic<uint32_t> voxel_sync_epoch_{0};
|
||||
std::atomic<size_t> voxel_sync_from_{0};
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -801,6 +836,81 @@ void WebServer::Publish(const WebSnapshot& snap) {
|
||||
s->set_acked_map_version(snap.slam.map_version);
|
||||
}
|
||||
}
|
||||
|
||||
// --- BAN DO VOXEL 3D ---
|
||||
//
|
||||
// Hai duong, va chung KHONG doi xung:
|
||||
//
|
||||
// full-sync : client chua co epoch nay -> bom `voxel_sync_chunks_per_tick`
|
||||
// manh moi lan Publish, cho toi het. KHONG danh dau is_map:
|
||||
// `Enqueue` khong bao gio drop khung map, nen 23 manh voxel
|
||||
// danh dau is_map se lam hang doi phinh vo han. Chung la
|
||||
// khung THUONG, drop duoc; mat manh nao thi ACK khong bao gio
|
||||
// toi va lan dong bo sau bat dau lai.
|
||||
//
|
||||
// delta : client dang giu dung epoch -> gui phan them/bot ngay khi co.
|
||||
// Payload da duoc ma hoa san o luong slam va chi song MOT lan
|
||||
// (WebLoop lay xong la xoa), nen khong co cong tan so nao o day.
|
||||
if (snap.voxels) {
|
||||
const uint32_t epoch = snap.voxels->epoch;
|
||||
const size_t per = VoxelCellsPerChunk(impl_->cfg.voxel_chunk_bytes);
|
||||
const uint16_t chunks =
|
||||
VoxelChunkCount(*snap.voxels, impl_->cfg.voxel_chunk_bytes);
|
||||
// KHONG gate theo tan so: delta chi sinh ra khi luong slam xuat ban do
|
||||
// (<= map_rebuild_rate_hz, va da bi duty 25% ghim lai), nen nhip do CHINH
|
||||
// la nhip tu nhien. Mot cong tan so o day chi co the LAM MAT khung.
|
||||
|
||||
for (const auto& s : targets) {
|
||||
const bool synced = !s->voxel_resync() && s->voxel_epoch() == epoch;
|
||||
if (synced) {
|
||||
// ⚠️ is_map=TRUE cho delta. Khong phai vi no la ban do 2D, ma vi
|
||||
// `Enqueue` chi drop khung khong-map: mat mot delta la client giu mot
|
||||
// ban do SAI vinh vien, y het mat mot khung MAP. Delta nho (vai tram
|
||||
// byte) va toi da ~4 khung/giay nen giu chung lai la re.
|
||||
if (snap.voxel_delta) s->Send(snap.voxel_delta, true, /*is_map=*/true);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Bat dau lai tu manh 0 khi epoch doi giua chung -- tron hai epoch la
|
||||
// cach chac chan nhat de ra mot ban do sai ma khong ai biet.
|
||||
size_t from = s->voxel_sync_from();
|
||||
if (s->voxel_sync_epoch() != epoch) from = 0;
|
||||
|
||||
for (uint32_t k = 0;
|
||||
k < impl_->cfg.voxel_sync_chunks_per_tick &&
|
||||
from < snap.voxels->cells.size();
|
||||
++k) {
|
||||
const uint16_t index = static_cast<uint16_t>(from / per);
|
||||
auto buf = std::make_shared<std::string>();
|
||||
EncodeVoxelFull(*snap.voxels, snap.stamp, from, per, index, chunks, buf.get());
|
||||
s->Send(buf, true, /*is_map=*/false);
|
||||
from += per;
|
||||
}
|
||||
|
||||
// Ban do RONG: van gui MOT manh rong, de client biet "gio khong con gi"
|
||||
// thay vi giu mai tap cu.
|
||||
if (snap.voxels->cells.empty()) {
|
||||
auto buf = std::make_shared<std::string>();
|
||||
EncodeVoxelFull(*snap.voxels, snap.stamp, 0, 0, 0, 1, buf.get());
|
||||
s->Send(buf, true, /*is_map=*/false);
|
||||
}
|
||||
|
||||
s->set_voxel_sync(epoch, from);
|
||||
|
||||
// Da bom het -> danh dau DA DONG BO ngay, khong doi `ack_voxel`.
|
||||
//
|
||||
// ⚠️ Cho ACK o day la mot cho treo that: ACK mat thi server coi
|
||||
// client van dang dong bo, ma `from` da toi cuoi nen vong tren khong gui
|
||||
// gi nua, va delta cung khong duoc gui vi chua "synced" -- ban do 3D
|
||||
// dung yen vinh vien ma khong mot loi nao. Danh dau lac quan o day va de
|
||||
// client TU xin resync khi no phat hien hut so thu tu delta; do la cung
|
||||
// khuon mau `set_acked_map_version` ngay sau khi xep hang o khoi MAP.
|
||||
if (from >= snap.voxels->cells.size()) {
|
||||
s->set_voxel_epoch(epoch);
|
||||
s->set_voxel_resync(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t WebServer::client_count() const {
|
||||
|
||||
Reference in New Issue
Block a user