upload
This commit is contained in:
95
libs/vslam_web/include/vslam/binary_codec.h
Normal file
95
libs/vslam_web/include/vslam/binary_codec.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#ifndef VSLAM_BINARY_CODEC_H
|
||||
#define VSLAM_BINARY_CODEC_H
|
||||
|
||||
// Ma hoa nhi phan cho khung WebSocket. Khung text (JSON) lo o json_writer.h.
|
||||
//
|
||||
// Header 16 byte, little-endian:
|
||||
// off size truong
|
||||
// 0 4 magic 'V','S','L','1'
|
||||
// 4 1 type
|
||||
// 5 1 version = 1
|
||||
// 6 2 flags
|
||||
// 8 8 stamp_ns (uint64, unix ns)
|
||||
// 16 ... payload
|
||||
//
|
||||
// Vi sao nhi phan chu khong JSON cho phan nay: mot sweep 1081 tia dang JSON
|
||||
// la ~15 kB va phai parse 1081 so; dang nhi phan la 2198 byte va doc thang
|
||||
// vao Uint16Array. O 10 Hz thi khac biet la that.
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
enum class BinaryType : uint8_t {
|
||||
kScan = 1,
|
||||
kCloud = 2,
|
||||
kMapFull = 3,
|
||||
kMapPatch = 4,
|
||||
kPath = 5,
|
||||
// Ban do THE GIOI THAT cua bo mo phong -- tuong duong khung nhin Gazebo.
|
||||
//
|
||||
// Tach han khoi kMapFull (ban do SLAM DUNG): hai thu do tra loi hai cau hoi
|
||||
// khac nhau. Ban do the gioi de LAI cho chuan; ban do SLAM de biet phien
|
||||
// quet dang ra sao. Gop lam mot la mat kha nang doi chieu -- ma doi chieu
|
||||
// chinh la ly do nguoi ta mo ca Gazebo lan RViz.
|
||||
//
|
||||
// TINH: trong mo phong no khong bao gio doi, nen chi gui MOT lan cho moi
|
||||
// client.
|
||||
kWorldMap = 6,
|
||||
};
|
||||
|
||||
constexpr size_t kBinaryHeaderSize = 16;
|
||||
constexpr uint8_t kBinaryVersion = 1;
|
||||
|
||||
// Gia tri canh bien cho tia laser, khop ba ngu nghia o vslam_common.h.
|
||||
// Doc ky: ba thu nay KHAC NHAU va lan lon chung lam robot dung im hoac dam
|
||||
// vao tuong.
|
||||
constexpr uint16_t kRangeNaN = 0xFFFF; // khong do duoc (duoi range_min)
|
||||
constexpr uint16_t kRangeInf = 0xFFFE; // khong co phan xa (vuot range_max)
|
||||
constexpr uint16_t kRangeBlanked = 0xFFFD; // bi sector mask cat (than robot)
|
||||
constexpr uint16_t kRangeMaxValid = 0xFFFC; // 65.532 m -- qua du cho 40 m
|
||||
|
||||
// Tat ca deu NOI THEM vao `out`, khong xoa truoc. Cho phep gom nhieu khung.
|
||||
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.
|
||||
void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points,
|
||||
float quant_m, 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
|
||||
// THE GIOI cua bo mo phong (kWorldMap): khung giong het nhau, chi khac y
|
||||
// nghia. Mot bo ma hoa thu hai se la hai cho de lech dinh dang.
|
||||
void EncodeMapFull(const OccupancyGrid& map, UnixTime stamp, std::string* out,
|
||||
BinaryType type = BinaryType::kMapFull);
|
||||
|
||||
// Chi vung bao (bbox) cac o doi so voi `prev`. Tra false neu kich thuoc hai
|
||||
// map khac nhau (khi do phai gui MAP_FULL) hoac khong co o nao doi.
|
||||
bool EncodeMapPatch(const OccupancyGrid& map, const OccupancyGrid& prev,
|
||||
UnixTime stamp, std::string* out);
|
||||
|
||||
// Thua hoa: chi giu diem cach diem truoc > min_step_m.
|
||||
void EncodePath(const std::vector<Pose2D>& path, UnixTime stamp, double min_step_m,
|
||||
std::string* out);
|
||||
|
||||
// --- Giai ma, chi dung cho test round-trip --------------------------------
|
||||
|
||||
struct BinaryHeader {
|
||||
BinaryType type = BinaryType::kScan;
|
||||
uint8_t version = 0;
|
||||
uint16_t flags = 0;
|
||||
uint64_t stamp_ns = 0;
|
||||
};
|
||||
|
||||
bool DecodeHeader(const std::string& buf, size_t offset, BinaryHeader* out);
|
||||
bool DecodeScan(const std::string& buf, LaserScan* out);
|
||||
bool DecodeMapFull(const std::string& buf, OccupancyGrid* out);
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_BINARY_CODEC_H
|
||||
38
libs/vslam_web/include/vslam/rle.h
Normal file
38
libs/vslam_web/include/vslam/rle.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef VSLAM_RLE_H
|
||||
#define VSLAM_RLE_H
|
||||
|
||||
// Ma hoa do dai loat cho occupancy grid.
|
||||
//
|
||||
// Moi run la {u16 count, i8 value} = 3 byte, hang-chinh.
|
||||
// `count` la u16 nen run > 65535 phai TACH -- de quen cho nay la bug im lang
|
||||
// tren ban do lon.
|
||||
//
|
||||
// Do tren du lieu that (tools/probe_rtabmap):
|
||||
// map_house.db 323x356 -> 5705 run, 17 kB (6.7x)
|
||||
// map_hospital.db 526x1162 -> 21242 run, 62 kB (9.6x)
|
||||
//
|
||||
// Luu y: ban do trong nha o day chi 16-42% la o "chua biet", KHONG phai >90%
|
||||
// nhu uoc luong ban dau. Ti le nen thuc te vi vay thap hon du kien, nhung
|
||||
// 62 kB cho ban do lon nhat van du nho de gui mot lan luc ket noi.
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// Noi them vao `out`. Tra ve so run da ghi.
|
||||
size_t RleEncode(const std::vector<int8_t>& data, std::string* out);
|
||||
|
||||
// Doc `count` run tu `buf` bat dau tu `offset`, ghi vao `out`.
|
||||
// Tra false neu du lieu cut hoac kich thuoc khong khop `expected_size`.
|
||||
bool RleDecode(const std::string& buf, size_t offset, size_t run_count,
|
||||
size_t expected_size, std::vector<int8_t>* out);
|
||||
|
||||
// So byte can de ma hoa `data` (= so run * 3). Dung de quyet dinh gui
|
||||
// MAP_FULL hay MAP_PATCH ma khong phai ma hoa hai lan.
|
||||
size_t RleEncodedSize(const std::vector<int8_t>& data);
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_RLE_H
|
||||
51
libs/vslam_web/include/vslam/web_config.h
Normal file
51
libs/vslam_web/include/vslam/web_config.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef VSLAM_WEB_CONFIG_H
|
||||
#define VSLAM_WEB_CONFIG_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct WebConfig {
|
||||
// --- Mang -------------------------------------------------------------
|
||||
std::string bind_address = "0.0.0.0";
|
||||
uint16_t port = 8080;
|
||||
int io_threads = 1; // 1 la du: moi viec nang deu o luong khac
|
||||
|
||||
// --- Bao mat ----------------------------------------------------------
|
||||
// Neu dat: moi POST /api/* can header X-Auth-Token, WS upgrade can ?token=
|
||||
// Neu bo trong: log WARN to luc khoi dong.
|
||||
//
|
||||
// BAT DOI XUNG CO Y (xem web_server.cc):
|
||||
// BAT e-stop -> luon duoc phep, khong can token (fail-safe)
|
||||
// GO e-stop -> BAT BUOC co token
|
||||
// reset bumper -> BAT BUOC co token
|
||||
// Mot endpoint e-stop khong xac thuc tren WiFi LAN nghia la bat ky ai trong
|
||||
// mang deu e-stop duoc; nhung te hon nhieu la GO duoc e-stop cua nguoi khac.
|
||||
std::string auth_token;
|
||||
|
||||
// --- Tan so gui -------------------------------------------------------
|
||||
double state_hz = 10.0; // JSON: pose, safety, slam, health
|
||||
double scan_hz = 10.0; // giam mau tu 25 Hz cua LiDAR
|
||||
double cloud_hz = 2.0;
|
||||
double path_hz = 1.0;
|
||||
// MAP khong co tan so: chi gui khi map_version doi.
|
||||
|
||||
// --- Giam bang thong --------------------------------------------------
|
||||
uint32_t cloud_max_points = 4000; // voxel-stride truoc khi ma hoa
|
||||
float cloud_quant_m = 0.01f; // luong tu hoa i16 -> dai +-327 m
|
||||
double path_min_step_m = 0.1; // thua hoa quy dao
|
||||
|
||||
// Hang doi gui moi phien. Day thi drop khung CU NHAT khong phai MAP.
|
||||
// MAP khong bao gio bi drop -- thay vao do bat co map_pending.
|
||||
size_t max_queued_frames = 4;
|
||||
|
||||
// --- Asset ------------------------------------------------------------
|
||||
// Rong = dung asset nhung trong binary (mac dinh, va la cach .deb chay).
|
||||
// Co gia tri = doc tu dia, che do dev: sua CSS khong phai build lai.
|
||||
std::string asset_dir;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_WEB_CONFIG_H
|
||||
95
libs/vslam_web/include/vslam/web_server.h
Normal file
95
libs/vslam_web/include/vslam/web_server.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#ifndef VSLAM_WEB_SERVER_H
|
||||
#define VSLAM_WEB_SERVER_H
|
||||
|
||||
// Web UI mot trang thay RViz. HTTP + WebSocket tren Boost.Beast.
|
||||
//
|
||||
// KIEN TRUC:
|
||||
// * io_context chay tren `io_threads` luong (mac dinh 1 -- moi viec nang
|
||||
// nam o luong khac).
|
||||
// * Publish() goi tu luong web_tx o 10 Hz, KHONG BAO GIO CHAN: no serialize
|
||||
// roi asio::post sang strand cua tung phien.
|
||||
// * Luong web KHONG BAO GIO sua state cua estimator. Moi POST /api/* di qua
|
||||
// CommandHandler -> hang doi lenh -> luong so huu tai nguyen drain.
|
||||
//
|
||||
// GIAO THUC: mot WebSocket /ws, hai loai khung.
|
||||
// Text = JSON, du lieu tan so thap, nguoi doc duoc.
|
||||
// Binary = scan / cloud / map / path, header 16 byte (xem binary_codec.h).
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "vslam/web_config.h"
|
||||
#include "vslam/web_snapshot.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// Trang thai dung chung cua server. Dinh nghia trong web_server.cc.
|
||||
//
|
||||
// KHONG lam struct long trong WebServer: cac lop phien (HttpSession,
|
||||
// WsSession) o file .cc phai truy cap duoc, ma mot kieu long private thi ho
|
||||
// khong goi ten duoc. Day la cung lop loi da gap o ConfigNode::Impl.
|
||||
class WebServerImpl;
|
||||
|
||||
class WebServer {
|
||||
public:
|
||||
// Xu ly lenh tu POST /api/<cmd>. `json_args` la body tho (co the rong).
|
||||
// Duoc goi TREN LUONG IO -- phai tra ve ngay, khong duoc chan. Cach dung
|
||||
// dung la day vao hang doi roi tra Ok.
|
||||
using CommandHandler =
|
||||
std::function<StatusResponse(const std::string& cmd, const std::string& json_args)>;
|
||||
|
||||
// Teleop tu joypad cam ung, gia tri chuan hoa [-1, 1] o 20 Hz.
|
||||
// TOAN BO shaping (ramp gia toc, deadman) lam o C++ trong teleop_shaper,
|
||||
// KHONG o JS: khong bao gio tin browser ve dinh dang chuyen dong, vi WS co
|
||||
// the lag hoac mat ket noi giua chung.
|
||||
using TeleopHandler = std::function<void(double vx_norm, double wz_norm)>;
|
||||
|
||||
WebServer(WebConfig cfg, CommandHandler on_command, TeleopHandler on_teleop);
|
||||
~WebServer();
|
||||
|
||||
WebServer(const WebServer&) = delete;
|
||||
WebServer& operator=(const WebServer&) = delete;
|
||||
|
||||
StatusResponse Start();
|
||||
void Stop();
|
||||
|
||||
// Khong chan. An toan goi tu bat ky luong nao.
|
||||
void Publish(const WebSnapshot& snap);
|
||||
|
||||
// Khoi hinh hoc 3D cua the gioi, phuc vu tai `GET /api/world.bin`.
|
||||
//
|
||||
// Dat MOT lan luc khoi dong roi thoi -- the gioi khong doi trong mot phien.
|
||||
// Trinh duyet tai no mot lan de dung do dac THAT thay vi khoi hop dung tu
|
||||
// luoi chiem dung. Rong = khong co, trinh duyet quay ve khoi hop.
|
||||
void SetWorldMesh(std::string blob);
|
||||
|
||||
size_t client_count() const;
|
||||
|
||||
// Cong thuc su dang nghe. Khac cfg.port khi cau hinh port=0 (test).
|
||||
uint16_t port() const;
|
||||
|
||||
struct Stats {
|
||||
uint64_t connections_total = 0;
|
||||
uint64_t frames_sent = 0;
|
||||
uint64_t frames_dropped = 0; // hang doi phien day
|
||||
uint64_t bytes_sent = 0;
|
||||
uint64_t commands_handled = 0;
|
||||
uint64_t auth_failures = 0;
|
||||
};
|
||||
Stats stats() const;
|
||||
|
||||
private:
|
||||
void DoAccept();
|
||||
|
||||
// shared_ptr chu khong unique_ptr: moi phien HTTP/WS giu mot tham chieu toi
|
||||
// trang thai nay va co the song lau hon loi goi Stop() mot chut (async
|
||||
// handler dang bay). unique_ptr o day la use-after-free luc tat may.
|
||||
std::shared_ptr<WebServerImpl> impl_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_WEB_SERVER_H
|
||||
142
libs/vslam_web/include/vslam/web_snapshot.h
Normal file
142
libs/vslam_web/include/vslam/web_snapshot.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#ifndef VSLAM_WEB_SNAPSHOT_H
|
||||
#define VSLAM_WEB_SNAPSHOT_H
|
||||
|
||||
// Anh chup trang thai he thong tai mot thoi diem, do luong web_tx dung tu
|
||||
// Channel::Latest() cua tung kenh roi day sang WebServer::Publish().
|
||||
//
|
||||
// Moi truong du lieu lon la shared_ptr<const T>: khong copy cloud/scan/map.
|
||||
// Neu khong co client nao ket noi thi web_tx khong serialize gi ca -- kiem
|
||||
// bang WebServer::client_count() TRUOC khi dung Snapshot.
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/stats.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct SafetySnapshot {
|
||||
std::string state = "OK"; // ten trang thai FSM
|
||||
std::string detail;
|
||||
double min_front = 0.0; // met; INFINITY neu khong do duoc
|
||||
int front_rays = 0; // tong so tia trong quat truoc
|
||||
int front_blanked = 0; // tia bi sector mask cat (sentinel range_max+1)
|
||||
int front_usable = 0; // tia thuc su dung duoc
|
||||
bool estop = false;
|
||||
bool bumper_latched = false;
|
||||
bool cliff = false;
|
||||
};
|
||||
|
||||
struct SlamSnapshot {
|
||||
std::string mode = "idle"; // mapping | localization | update_map | idle
|
||||
bool localized = false;
|
||||
double score = 0.0;
|
||||
int loop_closure_id = 0;
|
||||
uint32_t map_version = 0;
|
||||
double process_ms = 0.0;
|
||||
};
|
||||
|
||||
struct TeleopSnapshot {
|
||||
double linear_speed = 0.0; // scale hien tai, chinh duoc luc chay
|
||||
double angular_speed = 0.0;
|
||||
double max_linear_speed = 0.0;
|
||||
double max_angular_speed = 0.0;
|
||||
};
|
||||
|
||||
struct HealthSnapshot {
|
||||
double lidar_hz = 0.0;
|
||||
double camera_hz = 0.0;
|
||||
double imu_hz = 0.0;
|
||||
double ekf_hz = 0.0;
|
||||
double clock_skew = 1.0; // he so skew cua ClockSync (1.0 = khop)
|
||||
double cpu_pct = 0.0;
|
||||
double rss_mb = 0.0;
|
||||
// Ten kenh -> so ban tin bi drop. Day la thu queue cua ROS khong cho biet.
|
||||
std::vector<std::pair<std::string, uint64_t>> drops;
|
||||
};
|
||||
|
||||
// Hinh hoc robot cho trinh ve 3D, lay tu `robot.yaml` qua `RobotModel`.
|
||||
//
|
||||
// ⚠️ TRINH DUYET KHONG DUOC TU DOAN KICH THUOC. Truoc khi co khoi nay, app.js
|
||||
// hardcode `L = 0.30, W = 0.17` -- khong khop voi bat ky so nao trong
|
||||
// robot.yaml (ban kinh that la 0.24). Do la dung lop bug "hai cho phai giu
|
||||
// dong bo" ma `RobotModel` sinh ra de xoa, chi la no da tron sang JavaScript.
|
||||
//
|
||||
// Gui moi khung: ~120 byte, khong dang ke so voi ngan sach 120 kB/s. Doi lai
|
||||
// UI khong can bat tay rieng va khong bao gio hien sai kich thuoc sau khi
|
||||
// nguoi dung sua robot.yaml.
|
||||
struct RobotShape {
|
||||
bool valid = false;
|
||||
|
||||
double radius = 0.24; // ban kinh bao than, cho footprint
|
||||
double wheel_radius = 0.075;
|
||||
double wheel_separation = 0.36;
|
||||
double body_half_width = 0.17;
|
||||
|
||||
// Vi tri cam bien trong base_link (met). z do tu TAM TRUC banh, khong tu
|
||||
// san -- trinh ve tu cong wheel_radius khi can toa do so voi san.
|
||||
double laser_x = 0.24, laser_y = 0.0, laser_z = 0.125;
|
||||
double camera_x = 0.19, camera_y = 0.0, camera_z = 0.725;
|
||||
double camera_pitch = 0.2618; // rad, DUONG la chuc XUONG
|
||||
|
||||
// Goc mo cua camera depth, de ve frustum. Khong doc duoc tu robot.yaml --
|
||||
// day la thong so ONG KINH, lay tu CameraInfo khi co, khong thi mac dinh
|
||||
// theo D410 (69 do ngang).
|
||||
double camera_hfov = 1.204; // rad
|
||||
double camera_vfov = 0.733;
|
||||
|
||||
// Cua so cliff_detect, de ve vung mu len san. Duoi `cliff_min` camera
|
||||
// KHONG THAY GI -- do la vung hinh hoc, khong phai lua chon.
|
||||
double cliff_min = 1.10;
|
||||
double cliff_max = 2.50;
|
||||
};
|
||||
|
||||
struct WebSnapshot {
|
||||
UnixTime stamp;
|
||||
|
||||
RobotShape robot;
|
||||
|
||||
Pose2D pose_map; // pose trong frame map
|
||||
Pose2D pose_odom;
|
||||
double vx = 0.0;
|
||||
double vyaw = 0.0;
|
||||
double slip_metric = 0.0;
|
||||
|
||||
double roll = 0.0; // DO tu IMU, khong uoc luong
|
||||
double pitch = 0.0;
|
||||
bool accel_ok = true;
|
||||
|
||||
// ============ GROUND TRUTH ============
|
||||
// CHI co khi chay mo phong. Day la tuong duong cua /ground_truth/odom trong
|
||||
// repo cu, noi ghi ro "khong estimator nao duoc subscribe".
|
||||
//
|
||||
// No len web vi do la cach DUY NHAT nhin thay drift. RViz ve duoc ca pose
|
||||
// uoc luong lan ground truth canh nhau; khong co no thi mot ban do dep va
|
||||
// mot ban do SAI trong y het nhau.
|
||||
//
|
||||
// ⚠️ KHONG stage nao duoc doc gia tri nay. No di thang tu nguon len web.
|
||||
bool have_ground_truth = false;
|
||||
Pose2D ground_truth;
|
||||
double gt_roll = 0.0; // nghieng THAT, de doi chung gravity_align
|
||||
double gt_pitch = 0.0;
|
||||
|
||||
SafetySnapshot safety;
|
||||
SlamSnapshot slam;
|
||||
HealthSnapshot health;
|
||||
TeleopSnapshot teleop;
|
||||
|
||||
LaserScanPtr scan;
|
||||
PointCloudXYZPtr cloud;
|
||||
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.
|
||||
OccupancyGridPtr world_map;
|
||||
std::vector<Pose2D> trajectory;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_WEB_SNAPSHOT_H
|
||||
Reference in New Issue
Block a user