update
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
|
||||
#include "vslam/clock.h"
|
||||
#include "vslam/geometry.h"
|
||||
@@ -1000,6 +1001,14 @@ void Runtime::WebLoop() {
|
||||
// trong vong lap khi khung dau tien toi.
|
||||
}
|
||||
|
||||
// Lich su pose de bu do tre cua cloud. 5 s phu moi do tre thuc te.
|
||||
constexpr double kPoseHistorySec = 5.0;
|
||||
// Khong khop duoc trong 2 s thi khong con la do tre nua ma la stamp sai
|
||||
// GOC THOI GIAN -- luc do bu se te hon khong bu.
|
||||
constexpr double kPoseMatchMaxSec = 2.0;
|
||||
struct PoseAt { UnixTime stamp; Pose2D pose; };
|
||||
std::deque<PoseAt> pose_hist;
|
||||
|
||||
while (!stop_.load()) {
|
||||
WebSnapshot snap;
|
||||
snap.robot = shape;
|
||||
@@ -1103,6 +1112,46 @@ void Runtime::WebLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Bu do tre cua cloud ----
|
||||
// app.js ve cloud VA scan bang pose moi nhat ("scan va cloud o frame
|
||||
// robot -> xoay theo pose truoc khi ve"). Cloud cu hon scan nhieu: no qua
|
||||
// depth_filter (10 Hz), qua gravity_align, roi doi luot gui. Ve mot dam
|
||||
// may 0.5 s tuoi tai pose hien tai la dat no lech dung `v * 0.5 s` --
|
||||
// 25 cm o 0.5 m/s. Do la thu nhin thay tren robot: cloud va scan tach
|
||||
// nhau khi chay, khop lai 1-2 s sau khi dung.
|
||||
//
|
||||
// Bu bang phep bien doi TUONG DOI trong he robot, khong phai bang pose
|
||||
// tuyet doi: nhu vay khung "the gioi" (ve tai pose ground truth) dung
|
||||
// duoc cung mot con so, va app.js khong phai doi gi.
|
||||
//
|
||||
// ⚠️ Lich su giu `ekf_pose` (he ODOM), KHONG phai `pose_map`. pose_map
|
||||
// NHAY moi lan SLAM dong vong -- cu nhay do se chui vao `delta` va day
|
||||
// ca dam may di mot doan khong lien quan gi toi chuyen dong cua robot.
|
||||
// Odom khong nhay, va thu can o day dung la chuyen dong THAN robot giua
|
||||
// luc chup va bay gio.
|
||||
pose_hist.push_back(PoseAt{snap.stamp, ekf_pose});
|
||||
while (pose_hist.size() > 1 &&
|
||||
SecondsBetween(snap.stamp, pose_hist.front().stamp) > kPoseHistorySec) {
|
||||
pose_hist.pop_front();
|
||||
}
|
||||
|
||||
if (snap.cloud) {
|
||||
const UnixTime cs = snap.cloud->header.stamp;
|
||||
const PoseAt* best = nullptr;
|
||||
double best_dt = 0.0;
|
||||
for (const PoseAt& e : pose_hist) {
|
||||
const double d = std::fabs(SecondsBetween(cs, e.stamp));
|
||||
if (best == nullptr || d < best_dt) { best = &e; best_dt = d; }
|
||||
}
|
||||
// Stamp camera la TIME_OF_ARRIVAL (D410 FW khong ho tro global time,
|
||||
// xem sensors.yaml) nen cung goc thoi gian voi `clock_->Now()`. Lech
|
||||
// hon ca cua so lich su nghia la gia thiet do khong con dung -> khong bu.
|
||||
if (best != nullptr && best_dt <= kPoseMatchMaxSec) {
|
||||
snap.cloud_delta = Pose2DCompose(Pose2DInverse(ekf_pose), best->pose);
|
||||
snap.have_cloud_delta = true;
|
||||
}
|
||||
}
|
||||
|
||||
// ⚠️ Quy dao phai lay SAU khi `pose_map` da duoc tinh.
|
||||
//
|
||||
// Khoi nay truoc day nam TRUOC phep tinh pose_map, nen no doc mot
|
||||
|
||||
@@ -58,8 +58,12 @@ void EncodeScan(const LaserScan& scan, UnixTime stamp, std::string* out);
|
||||
|
||||
// Giam mau xuong `max_points` bang cach lay buoc deu, roi luong tu hoa theo
|
||||
// `quant_m`. Bo diem NaN.
|
||||
//
|
||||
// `delta` quy diem ve he base_footprint HIEN TAI truoc khi ma hoa. Ap o day
|
||||
// chu khong tren ca dam may vi o day da qua buoc deu: quy 40 k diem roi vut
|
||||
// di 36 k la lang phi thuan tuy. Dong nhat = khong bu.
|
||||
void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points,
|
||||
float quant_m, std::string* out);
|
||||
float quant_m, const Pose2D& delta, std::string* out);
|
||||
|
||||
// RLE hang-chinh. Xem rle.h.
|
||||
// `type` cho phep dung chung bo ma hoa cho ban do SLAM (kMapFull) va ban do
|
||||
|
||||
@@ -145,6 +145,12 @@ struct WebSnapshot {
|
||||
|
||||
LaserScanPtr scan;
|
||||
PointCloudXYZPtr cloud;
|
||||
// Phep bien doi quy cloud tu he base_footprint LUC CHUP ve he
|
||||
// base_footprint HIEN TAI. Trinh duyet ve cloud bang pose moi nhat, nen
|
||||
// mot dam may 0.5 s tuoi se bi dat lech dung `v * 0.5 s` neu khong bu.
|
||||
// Dong nhat khi khong biet pose luc chup -- xem Runtime::WebLoop.
|
||||
Pose2D cloud_delta;
|
||||
bool have_cloud_delta = false;
|
||||
OccupancyGridPtr map; // ban do SLAM dung
|
||||
// Ban do THE GIOI cua bo mo phong (khung nhin "Gazebo"). Null khi chay
|
||||
// that -- luc do khong co su that nao de doi chieu.
|
||||
|
||||
@@ -101,7 +101,7 @@ void EncodeScan(const LaserScan& scan, UnixTime stamp, std::string* out) {
|
||||
}
|
||||
|
||||
void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points,
|
||||
float quant_m, std::string* out) {
|
||||
float quant_m, const Pose2D& delta, std::string* out) {
|
||||
if (quant_m <= 0.0f) quant_m = 0.01f;
|
||||
|
||||
// Lay buoc deu thay vi cat duoi: cat duoi lam mat ca mot vung khong gian,
|
||||
@@ -118,22 +118,27 @@ void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points
|
||||
}
|
||||
|
||||
PutHeader(out, BinaryType::kCloud, stamp);
|
||||
// Goc = (0,0,0): cloud da o frame base_footprint sau gravity_align.
|
||||
// Goc = (0,0,0): sau khi ap `delta` o vong duoi, diem nam trong he
|
||||
// base_footprint HIEN TAI -- dung he ma trinh duyet ve.
|
||||
PutF32(out, 0.0f);
|
||||
PutF32(out, 0.0f);
|
||||
PutF32(out, 0.0f);
|
||||
PutF32(out, quant_m);
|
||||
PutU32(out, n_out);
|
||||
|
||||
const double dc = std::cos(delta.theta), ds = std::sin(delta.theta);
|
||||
const float inv = 1.0f / quant_m;
|
||||
constexpr float kLim = 32767.0f;
|
||||
for (size_t i = 0; i < n_in; i += stride) {
|
||||
const PointXYZ& p = cloud.points[i];
|
||||
if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) continue;
|
||||
// z khong doi: `delta` la SE(2), robot khong di chuyen theo phuong dung.
|
||||
const double bx = delta.x + dc * p.x - ds * p.y;
|
||||
const double by = delta.y + ds * p.x + dc * p.y;
|
||||
// Kep vao dai i16. Voi quant 0.01 m thi dai la +-327 m -- khong bao gio
|
||||
// cham toi trong nha, nhung kep van re hon la de tran am tham.
|
||||
const float qx = std::max(-kLim, std::min(kLim, p.x * inv));
|
||||
const float qy = std::max(-kLim, std::min(kLim, p.y * inv));
|
||||
const float qx = std::max(-kLim, std::min(kLim, static_cast<float>(bx) * inv));
|
||||
const float qy = std::max(-kLim, std::min(kLim, static_cast<float>(by) * inv));
|
||||
const float qz = std::max(-kLim, std::min(kLim, p.z * inv));
|
||||
PutI16(out, static_cast<int16_t>(std::lround(qx)));
|
||||
PutI16(out, static_cast<int16_t>(std::lround(qy)));
|
||||
|
||||
@@ -118,6 +118,39 @@ class WebServerImpl {
|
||||
mutable std::mutex world_mu;
|
||||
std::string world_blob;
|
||||
|
||||
// --- Nhip gui rieng cho tung kenh -------------------------------------
|
||||
// `status_rate_hz`, `scan_rate_hz`, `cloud_rate_hz`, `path_rate_hz` da nam
|
||||
// trong system.yaml tu dau va da duoc parse vao WebConfig, nhung KHONG cho
|
||||
// nao ap dung chung -- chi `tools/vslam_web_demo.cc`. Publish() gui ca ba
|
||||
// kenh o MOI lan goi, tuc o status_rate_hz = 10 Hz.
|
||||
//
|
||||
// Do duoc: cloud 4000 diem = 24 kB, gui 10 Hz la 240 kB/s -- gap DOI ngan
|
||||
// sach 120 kB/s cua ca UI. Hang doi 4 khung day lien tuc, drop khung cu
|
||||
// nhat, va man hinh tut lai 1-2 giay so voi robot. Chinh cai bay ma
|
||||
// system.yaml da ghi ("109 kB/s chi vi PATH gui o 9.8 Hz thay vi 1 Hz"),
|
||||
// quay lai o ca ba kenh.
|
||||
//
|
||||
// CHI luong goi Publish() cham vao -- khong khoa.
|
||||
UnixTime last_scan_at, last_cloud_at, last_path_at;
|
||||
bool have_scan_at = false, have_cloud_at = false, have_path_at = false;
|
||||
|
||||
// Den luot kenh nay chua? `rate <= 0` = khong gioi han.
|
||||
//
|
||||
// Gate theo STAMP CUA SNAPSHOT, khong theo dong ho tuong: phat lai ban ghi
|
||||
// nhanh hon thoi gian that phai ra cung ket qua nhu chay live. Cung ly do
|
||||
// da ghi trong DepthFilter::Apply().
|
||||
static bool Due(double rate, UnixTime now, UnixTime* last, bool* have) {
|
||||
if (rate <= 0.0) return true;
|
||||
if (*have) {
|
||||
const double dt = SecondsBetween(now, *last);
|
||||
// dt < 0 = dong ho lui (ban ghi phat lai tu dau) -> gui va dat lai moc.
|
||||
if (dt >= 0.0 && dt < 1.0 / rate) return false;
|
||||
}
|
||||
*last = now;
|
||||
*have = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
WebServerImpl(WebConfig c, WebServer::CommandHandler cmd,
|
||||
WebServer::TeleopHandler tel)
|
||||
: cfg(std::move(c)), on_command(std::move(cmd)), on_teleop(std::move(tel)) {}
|
||||
@@ -712,20 +745,26 @@ void WebServer::Publish(const WebSnapshot& snap) {
|
||||
WriteStateJson(snap, state.get());
|
||||
for (const auto& s : targets) s->Send(state, /*binary=*/false, /*is_map=*/false);
|
||||
|
||||
if (snap.scan) {
|
||||
if (snap.scan && impl_->Due(impl_->cfg.scan_hz, snap.stamp,
|
||||
&impl_->last_scan_at, &impl_->have_scan_at)) {
|
||||
auto buf = std::make_shared<std::string>();
|
||||
EncodeScan(*snap.scan, snap.stamp, buf.get());
|
||||
for (const auto& s : targets) s->Send(buf, true, false);
|
||||
}
|
||||
|
||||
if (snap.cloud) {
|
||||
if (snap.cloud && impl_->Due(impl_->cfg.cloud_hz, snap.stamp,
|
||||
&impl_->last_cloud_at, &impl_->have_cloud_at)) {
|
||||
auto buf = std::make_shared<std::string>();
|
||||
EncodeCloud(*snap.cloud, snap.stamp, impl_->cfg.cloud_max_points,
|
||||
impl_->cfg.cloud_quant_m, buf.get());
|
||||
impl_->cfg.cloud_quant_m,
|
||||
snap.have_cloud_delta ? snap.cloud_delta : Pose2D{},
|
||||
buf.get());
|
||||
for (const auto& s : targets) s->Send(buf, true, false);
|
||||
}
|
||||
|
||||
if (!snap.trajectory.empty()) {
|
||||
if (!snap.trajectory.empty() &&
|
||||
impl_->Due(impl_->cfg.path_hz, snap.stamp, &impl_->last_path_at,
|
||||
&impl_->have_path_at)) {
|
||||
auto buf = std::make_shared<std::string>();
|
||||
EncodePath(snap.trajectory, snap.stamp, impl_->cfg.path_min_step_m, buf.get());
|
||||
for (const auto& s : targets) s->Send(buf, true, false);
|
||||
|
||||
@@ -184,7 +184,7 @@ TEST(BinaryCodec, CloudGiamMauVaBoNaN) {
|
||||
c.points[5] = PointXYZ{std::numeric_limits<float>::quiet_NaN(), 0.0f, 0.0f};
|
||||
|
||||
std::string buf;
|
||||
EncodeCloud(c, UnixTime::fromSec(1.0), /*max_points=*/1000, 0.01f, &buf);
|
||||
EncodeCloud(c, UnixTime::fromSec(1.0), /*max_points=*/1000, 0.01f, Pose2D{}, &buf);
|
||||
|
||||
// 16 header + 20 (origin3 + scale + n) + n*6
|
||||
ASSERT_GE(buf.size(), 36u);
|
||||
@@ -196,13 +196,56 @@ TEST(BinaryCodec, CloudGiamMauVaBoNaN) {
|
||||
TEST(BinaryCodec, CloudRongVanHopLe) {
|
||||
PointCloudXYZ c;
|
||||
std::string buf;
|
||||
EncodeCloud(c, UnixTime::fromSec(1.0), 1000, 0.01f, &buf);
|
||||
EncodeCloud(c, UnixTime::fromSec(1.0), 1000, 0.01f, Pose2D{}, &buf);
|
||||
EXPECT_EQ(buf.size(), 36u);
|
||||
BinaryHeader h;
|
||||
ASSERT_TRUE(DecodeHeader(buf, 0, &h));
|
||||
EXPECT_EQ(h.type, BinaryType::kCloud);
|
||||
}
|
||||
|
||||
// Diem phai DI THEO delta, neu khong thi phep bu do tre khong ton tai that.
|
||||
//
|
||||
// `delta` la `pose_now^-1 * pose_capture` (xem Runtime::WebLoop), nen robot
|
||||
// TIEN 0.5 m ke tu luc chup cho ra delta.x = -0.5: diem tung o 1.0 m truoc
|
||||
// mui gio chi con cach 0.5 m.
|
||||
TEST(BinaryCodec, CloudApDeltaTinhTien) {
|
||||
PointCloudXYZ c;
|
||||
c.points.push_back(PointXYZ{1.0f, 0.0f, 0.0f});
|
||||
|
||||
std::string buf;
|
||||
EncodeCloud(c, UnixTime::fromSec(1.0), 100, 0.01f, Pose2D{-0.5, 0.0, 0.0}, &buf);
|
||||
|
||||
ASSERT_EQ(buf.size(), 42u); // 16 header + 20 + 1*6
|
||||
auto i16 = [&buf](size_t off) {
|
||||
return static_cast<int16_t>(static_cast<uint8_t>(buf[off]) |
|
||||
(static_cast<uint8_t>(buf[off + 1]) << 8));
|
||||
};
|
||||
EXPECT_NEAR(i16(36) * 0.01, 0.5, 1e-6);
|
||||
EXPECT_NEAR(i16(38) * 0.01, 0.0, 1e-6);
|
||||
}
|
||||
|
||||
// Xoay, khong chi tinh tien: quay tai cho la truong hop lam cloud va scan
|
||||
// tach nhau ro nhat, va mot delta chi co tinh tien se khong bat duoc no.
|
||||
TEST(BinaryCodec, CloudApDeltaXoay) {
|
||||
PointCloudXYZ c;
|
||||
c.points.push_back(PointXYZ{1.0f, 0.0f, 0.5f});
|
||||
|
||||
std::string buf;
|
||||
// Robot da quay PHAI 90 do ke tu luc chup -> diem truoc mui gio o ben TRAI.
|
||||
EncodeCloud(c, UnixTime::fromSec(1.0), 100, 0.01f,
|
||||
Pose2D{0.0, 0.0, M_PI / 2.0}, &buf);
|
||||
|
||||
ASSERT_EQ(buf.size(), 42u);
|
||||
auto i16 = [&buf](size_t off) {
|
||||
return static_cast<int16_t>(static_cast<uint8_t>(buf[off]) |
|
||||
(static_cast<uint8_t>(buf[off + 1]) << 8));
|
||||
};
|
||||
EXPECT_NEAR(i16(36) * 0.01, 0.0, 1e-6);
|
||||
EXPECT_NEAR(i16(38) * 0.01, 1.0, 1e-6);
|
||||
// z khong bi delta cham vao.
|
||||
EXPECT_NEAR(i16(40) * 0.01, 0.5, 1e-6);
|
||||
}
|
||||
|
||||
TEST(BinaryCodec, PathThuaHoa) {
|
||||
std::vector<Pose2D> path;
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
|
||||
Reference in New Issue
Block a user