489 lines
20 KiB
C++
489 lines
20 KiB
C++
#ifndef VSLAM_APP_RUNTIME_H
|
|
#define VSLAM_APP_RUNTIME_H
|
|
|
|
// Tien trinh duy nhat. So huu moi luong va moi stage.
|
|
//
|
|
// ================== BAN DO LUONG ==================
|
|
// luong tan so uu tien lam gi
|
|
// ------------------------------------------------------------------
|
|
// safety 20 Hz SCHED_FIFO 50 DUONG DUY NHAT toi motor
|
|
// source theo nguon FIFO 45 driver / sim / replay
|
|
// web_tx 10 Hz CFS serialize va phat
|
|
//
|
|
// Cac stage con lai chay INLINE trong callback cua nguon, tren luong source.
|
|
// Do la co y: chuoi perception (depth -> cloud -> loc -> can trong luc ->
|
|
// cliff) tung la ba nodelet chung mot manager; o day no la ba loi goi ham
|
|
// trong mot ham. Khong intrusive_ptr, khong dispatch theo topic, va thu tu
|
|
// buoc la thu tu dong lenh.
|
|
//
|
|
// ================== QUY TAC SONG CON ==================
|
|
// Luong `safety` KHONG chia se mutex nao voi luong `slam`.
|
|
// `Rtabmap::process()` chan 100-600 ms; mot mutex chung la du de treo vong an
|
|
// toan trong ngan do.
|
|
//
|
|
// Trong ban ROS dieu nay duoc bao dam MIEN PHI boi tien trinh rieng +
|
|
// respawn="true". O day no la quy tac review, va chi ThreadSanitizer moi kiem
|
|
// duoc. Cac mutex duoi day duoc dat ten theo DU LIEU chung bao ve, khong
|
|
// theo luong, de review thay ngay cai nao cham vao cai nao.
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "app_config.h"
|
|
#include "vslam/driver_interfaces.h"
|
|
#include "vslam/ekf_se2.h"
|
|
#include "vslam/gamepad.h"
|
|
#include "vslam/geometry.h"
|
|
#include "vslam/perception_pipeline.h"
|
|
#include "vslam/safety_monitor.h"
|
|
#include "vslam/scan_sanitizer.h"
|
|
#include "vslam/teleop_model.h"
|
|
#include "vslam/web_server.h"
|
|
#include "vslam/wheel_odometry.h"
|
|
|
|
#ifdef VSLAM_HAVE_SLAM
|
|
#include "vslam/global_localizer.h"
|
|
#include "vslam/rtabmap_slam.h"
|
|
#include "vslam/visual_verifier.h"
|
|
#include "vslam/voxel_export.h"
|
|
#include "vslam/vpr_encoder.h"
|
|
#endif
|
|
|
|
namespace vslam {
|
|
|
|
// Nguon du lieu: sim, replay, hay phan cung that. `vslam_node` KHONG phan
|
|
// biet duoc -- do la ca ly do cac interface driver ton tai.
|
|
enum class SourceKind { kSim, kReplay, kLive };
|
|
|
|
struct RuntimeOptions {
|
|
SourceKind source = SourceKind::kSim;
|
|
std::string replay_path;
|
|
std::string map_yaml; // chi co nghia voi sim
|
|
std::string world_path; // .world cua Gazebo; uu tien hon map_yaml
|
|
double sim_start_x = 0.0, sim_start_y = 0.0, sim_start_yaw = 0.0;
|
|
uint16_t port_override = 0; // 0 = dung system.yaml
|
|
std::string token_override;
|
|
|
|
// Tay cam: 0 = theo system.yaml, 1 = --gamepad, -1 = --no-gamepad.
|
|
// Ba trang thai chu khong phai bool: "khong noi gi" khac han "noi la tat".
|
|
int gamepad_override = 0;
|
|
std::string gamepad_device; // rong = giu device trong system.yaml
|
|
|
|
// Tran toc do, GHI DE ca robot.yaml lan safety.yaml. 0 = khong ghi de.
|
|
//
|
|
// ⚠️ CHO MO PHONG. `robot.yaml base.max_linear = 0.35` la gioi han CO HOC
|
|
// that cua base; co nay khong lam base that chay nhanh hon, no chi go tran
|
|
// trong mo phong de quet mot toa nha 58 m khong mat nua tieng.
|
|
//
|
|
// Phai ghi de CA HAI: lop safety kep lenh o `safety.yaml max_linear`, nen
|
|
// chi nang tran teleop thoi thi so tren man hinh len 2.0 con robot van chay
|
|
// 0.35 -- do dung la thu da xay ra truoc khi co co nay.
|
|
double max_speed_override = 0.0;
|
|
// Dat mot buc KINH chan ngang hanh lang o x = met nay. 0 = khong dat.
|
|
// Kinh vo hinh voi LiDAR va depth; chi sieu am thay. Dung de chay nhanh
|
|
// US_BLOCKED va BUMPER cua FSM -- hai nhanh chua bao gio chay end-to-end.
|
|
double sim_glass_at = 0.0;
|
|
bool force_slam = false; // --slam: ghi de slam.yaml enabled
|
|
bool have_slam_db = false; // --slam-db "" nghia la DB trong RAM
|
|
std::string slam_db;
|
|
std::string slam_mode_override; // "mapping" | "localization"
|
|
// Pose ban dau cho che do localization -- tuong duong /initialpose cua ban
|
|
// ROS, thu ma `dock_init.py` phat tu dock.
|
|
bool have_slam_init_pose = false;
|
|
Pose2D slam_init_pose;
|
|
// --slam-reloc: localization KHONG pose ban dau -- tu dinh vi lai bang khop
|
|
// scan LiDAR toan ban do (vslam/global_localizer.h).
|
|
bool slam_reloc = false;
|
|
// --slam-reloc-auto: robot TU XOAY va TU DI de gom scan (reloc_exec cua ban
|
|
// ROS). Mac dinh TAT: robot tu chay khi CHUA biet minh o dau.
|
|
bool slam_reloc_auto = false;
|
|
// --slam-reloc-vpr [ENGINE]: CAMERA cung dinh vi lai -- VPR (TensorRT) goi y cho, PnP cho
|
|
// pose, chot khi TRUNG khop LiDAR. Can -DVSLAM_WITH_TENSORRT=ON; khong co thi canh bao
|
|
// roi chay bang LiDAR nhu cu.
|
|
bool slam_reloc_vpr = false;
|
|
std::string vpr_engine; // rong = $VSLAM_RESOURCE_DIR/vpr/eigenplaces_r50_fp16.plan
|
|
};
|
|
|
|
class Runtime {
|
|
public:
|
|
Runtime(AppConfig* cfg, RuntimeOptions opts);
|
|
~Runtime();
|
|
|
|
Runtime(const Runtime&) = delete;
|
|
Runtime& operator=(const Runtime&) = delete;
|
|
|
|
StatusResponse Start();
|
|
void RequestStop();
|
|
void Join();
|
|
|
|
private:
|
|
// wiring.cc -- noi duy nhat noi nguon voi stage. Tuong duong toan bo 20
|
|
// file .launch cua ban ROS.
|
|
StatusResponse Wire();
|
|
|
|
// runtime.cc
|
|
StatusResponse OpenSource();
|
|
void SafetyLoop();
|
|
|
|
// Bom lenh tay cam vao `raw_cmd_` o tan so co dinh. Xem ghi chu "bay cua
|
|
// joydev" trong gamepad.h: khong co vong nay thi giu can gat dung yen se bi
|
|
// hieu la MAT lenh.
|
|
void GamepadLoop();
|
|
|
|
// Do DO LECH giua dam may camera va scan LiDAR, bang so.
|
|
//
|
|
// Hai cam bien nhin cung mot buc tuong. Neu ngoai lai cua camera trong
|
|
// robot.yaml dung thi hai tap diem phai chong khit. Lech bao nhieu va lech
|
|
// theo huong nao thi ham nay in ra -- thay vi bat nguoi doc uoc luong tren
|
|
// anh chup man hinh, viec da dan den vai vong sua sai.
|
|
void ReportCloudScanAlign(const PointCloudXYZ& cloud);
|
|
void WebLoop();
|
|
void SlamLoop();
|
|
#ifdef VSLAM_HAVE_SLAM
|
|
// Dinh vi lai tu dong (--slam-reloc). CHI luong slam goi, TRUOC Process():
|
|
// gom scan theo goc da xoay, du thi khop toan cuc, chot thi SetInitialPose.
|
|
void RelocStep(const LaserScan& scan, const Pose2D& odom);
|
|
// Sau Process(): scan tai pose dang tin con khop ban do khong -- tut lau
|
|
// thi quay lai gom (nghi da lac).
|
|
void RelocWatch(const LaserScan& scan, const Pose2D& pose_map);
|
|
// Diem scan (frame laser) -> frame cua `pose` (odom hoac map), cat max_range.
|
|
std::vector<Point> ScanToFrame(const LaserScan& scan, const Pose2D& pose) const;
|
|
// `pending` = CHUA dinh vi xong: web khong ve robot theo pose uoc luong.
|
|
void SetRelocStatus(const std::string& s, bool pending);
|
|
|
|
// --- Tu chay (--slam-reloc-auto) ---
|
|
// Lenh di qua `raw_cmd_` NHU MOI NGUON KHAC -> van qua lop an toan.
|
|
void RelocCommand(double v, double w);
|
|
// Co ai KHAC ghi raw_cmd_ trong 0.5 s qua khong (tay cam, web).
|
|
bool RelocHumanCommanding();
|
|
// Chon huong thoang nhat trong scan, dat muc tieu QUAY + DI. false = khong
|
|
// co cho nao du rong.
|
|
bool RelocPlanDrive(const LaserScan& scan, const Pose2D& odom);
|
|
void RelocStartSweep();
|
|
void RelocHalt(const std::string& why);
|
|
// Sau khi CHOT: cho robot DUNG YEN, gom scan luc dung yen (khong nhoe do
|
|
// quay), khop cuc bo quanh pose vua chot roi moi dat pose ban dau.
|
|
void RelocSettle(const LaserScan& scan, const Pose2D& odom,
|
|
std::chrono::steady_clock::time_point now);
|
|
void RelocFinish(const Pose2D& map_from_odom, const Pose2D& odom, const LaserScan& scan,
|
|
const std::string& how);
|
|
// Bat dau pha dung yen + tinh chinh sau khi CHOT -- chung cho LiDAR va camera.
|
|
void RelocBeginSettle(const Pose2D& map_from_odom, double ratio, const Pose2D& odom,
|
|
std::chrono::steady_clock::time_point now, const std::string& src);
|
|
// Camera: cu 30 do xoay chup mot anh -> VPR top-5 -> PnP -> so voi LiDAR cuc bo quanh
|
|
// node anh. true = da CHOT (da vao RelocBeginSettle).
|
|
bool RelocCamera(const LaserScan& scan, const Pose2D& odom,
|
|
std::chrono::steady_clock::time_point now);
|
|
#endif
|
|
void ApplyThreadPriority(std::thread* t, int prio, const char* name);
|
|
|
|
AppConfig* cfg_;
|
|
RuntimeOptions opts_;
|
|
|
|
// --- Nguon ---
|
|
// Giu bang con tro kieu co so: node khong biet minh dang chay live, replay
|
|
// hay sim. Khong co nhanh `if (replay)` nao trong logic.
|
|
struct SourceHolder;
|
|
std::unique_ptr<SourceHolder> source_;
|
|
LidarInterface* lidar_ = nullptr;
|
|
CameraInterface* camera_ = nullptr;
|
|
McuInterface* mcu_ = nullptr;
|
|
BaseInterface* base_ = nullptr;
|
|
ClockPtr clock_;
|
|
|
|
// --- Stage ---
|
|
std::unique_ptr<ScanSanitizer> sanitizer_;
|
|
std::unique_ptr<WheelOdometry> wheel_odom_;
|
|
std::unique_ptr<EkfSe2> ekf_;
|
|
std::unique_ptr<PerceptionPipeline> perception_;
|
|
std::unique_ptr<SafetyMonitor> safety_;
|
|
std::unique_ptr<TeleopModel> teleop_;
|
|
std::unique_ptr<WebServer> web_;
|
|
|
|
// Tay cam. `gamepad_` doc thiet bi tren luong rieng; `gamepad_map_` bien
|
|
// trang thai tho thanh van toc va CHI luong `gamepad` cham vao.
|
|
//
|
|
// ⚠️ Tay cam KHONG di qua `teleop_`. Do la co y va giong base_kit: node joy
|
|
// ben do co tham so toc do rieng, doc lap voi ban phim. Nho vay hai nguon
|
|
// nhap khong tranh trang thai cua mot mo hinh chung -- chung chi gap nhau o
|
|
// `raw_cmd_`, va luat nhuong quyen la cua so idle 1.5 s.
|
|
std::unique_ptr<GamepadReader> gamepad_;
|
|
std::unique_ptr<GamepadMapper> gamepad_map_;
|
|
// Doc-only sau Wire(). Giu rieng vi `--gamepad` co the ghi de config.
|
|
double gamepad_rate_hz_ = 50.0;
|
|
#ifdef VSLAM_HAVE_SLAM
|
|
std::unique_ptr<RtabmapSlam> slam_;
|
|
#endif
|
|
|
|
// --- Du lieu chia se, moi mutex bao ve dung mot nhom ---
|
|
|
|
// scan_mu_: scan da ve sinh gan nhat. Luong source ghi, luong safety va
|
|
// web_tx doc.
|
|
mutable std::mutex scan_mu_;
|
|
LaserScanPtr latest_scan_;
|
|
|
|
// odom_mu_: wheel_odom_ va ekf_. CHI luong source cham vao chung; hai luong
|
|
// kia chi doc ban sao pose.
|
|
mutable std::mutex odom_mu_;
|
|
Odometry latest_odom_;
|
|
bool have_odom_ = false;
|
|
|
|
// perc_mu_: ket qua perception.
|
|
mutable std::mutex perc_mu_;
|
|
PointCloudXYZPtr latest_cloud_;
|
|
PointCloudXYZPtr latest_cliff_cloud_;
|
|
bool cliff_flag_ = false;
|
|
UnixTime cliff_stamp_;
|
|
bool have_cliff_ = false;
|
|
std::string cliff_detail_;
|
|
|
|
// depth_mu_: khung depth THO gan nhat, danh RIENG cho SLAM 3D.
|
|
//
|
|
// ⚠️ RIENG khoi `perc_mu_`, khong dung chung. Luong safety doc
|
|
// `cliff_flag_` duoi `perc_mu_`; cho luong slam cung cho vao do la dat mot
|
|
// vong lap 100-600 ms sau cung mot mutex voi vong an toan 20 Hz.
|
|
//
|
|
// ⚠️ Giu `ImagePtr` o day la GHIM mot khung cua librealsense:
|
|
// `Image::data` tro thang vao buffer cua SDK va `keep_alive` giu `rs2::frame`
|
|
// song. Nhieu nhat hai khung bi ghim cung luc (mot o day, mot dang bay trong
|
|
// SlamLoop) tren pool 16 -- do la ly do day la mot LATCH chu khong phai mot
|
|
// hang doi.
|
|
mutable std::mutex depth_mu_;
|
|
ImagePtr latest_depth_;
|
|
ImagePtr latest_infra1_;
|
|
std::shared_ptr<const CameraInfo> latest_depth_info_;
|
|
UnixTime latest_depth_stamp_;
|
|
bool have_depth_ = false;
|
|
// Nghieng (gravity_align) LUC chup khung depth tren -- xem
|
|
// SlamInput::camera_to_base.
|
|
double latest_depth_roll_ = 0.0;
|
|
double latest_depth_pitch_ = 0.0;
|
|
// base_link <- camera_depth_optical_frame, tinh. Doc-only sau Start().
|
|
Transform slam_cam_to_link_ = TransformIdentity();
|
|
|
|
// mcu_mu_: sieu am va bumper.
|
|
//
|
|
// Hai cam bien nay o tren MCU vi ca hai deu la cam bien MUC THAP phai toi
|
|
// duoc vong an toan ke ca khi may tinh chinh dang ban. Luong source ghi,
|
|
// luong safety doc o 20 Hz.
|
|
mutable std::mutex mcu_mu_;
|
|
std::map<std::string, double> ultrasonic_;
|
|
UnixTime us_stamp_;
|
|
bool have_us_ = false;
|
|
bool bumper_ = false;
|
|
UnixTime bumper_stamp_;
|
|
bool have_bumper_ = false;
|
|
|
|
// cmd_mu_: lenh teleop THO, chua qua lop an toan.
|
|
//
|
|
// Luong web ghi, luong safety doc o 20 Hz. Luong web KHONG BAO GIO goi
|
|
// thang SetVelocity -- xem ghi chu trong wiring.cc.
|
|
mutable std::mutex cmd_mu_;
|
|
Twist raw_cmd_;
|
|
UnixTime raw_cmd_stamp_;
|
|
bool have_raw_cmd_ = false;
|
|
// Stamp cua lan GHI GAN NHAT boi dinh vi lai tu chay. raw_cmd_stamp_ moi hon
|
|
// cai nay nghia la NGUOI (tay cam/web) vua ra lenh -> tu chay nhuong.
|
|
UnixTime reloc_cmd_stamp_;
|
|
|
|
// teleop_mu_: TeleopModel co trang thai (toc do dieu chinh duoc luc chay).
|
|
mutable std::mutex teleop_mu_;
|
|
|
|
// safety_mu_: SafetyMonitor. KHONG duoc chia se voi bat ky mutex nao ma
|
|
// luong slam cham toi.
|
|
mutable std::mutex safety_mu_;
|
|
SafetyOutput last_safety_;
|
|
|
|
// Quy dao de ve len web.
|
|
mutable std::mutex traj_mu_;
|
|
std::vector<Pose2D> trajectory_;
|
|
UnixTime last_traj_stamp_;
|
|
|
|
// slam_mu_: ket qua cua SLAM.
|
|
//
|
|
// ⚠️ QUY TAC SONG CON: mutex nay KHONG duoc luong safety cham vao, va luong
|
|
// slam KHONG duoc giu bat ky mutex nao trong luc goi `Process()` --
|
|
// `Rtabmap::process()` chan 100-600 ms (p95 do duoc 722-1008 ms tren may
|
|
// nay). Xem SlamLoop().
|
|
//
|
|
// Luong slam chi cham `scan_mu_` va `odom_mu_` de COPY mot shared_ptr va
|
|
// mot Pose2D, roi nha ngay. Do la khuon mau `Channel::Latest()`: mot lan
|
|
// copy duoi mutex, khong bao gio giu lock khi goi code nang.
|
|
mutable std::mutex slam_mu_;
|
|
Pose2D map_correction_;
|
|
bool slam_localized_ = false;
|
|
uint64_t slam_map_version_ = 0;
|
|
uint64_t slam_published_version_ = 0;
|
|
// Thanh phan ban do lan truoc, de chi log KHI DOI. Xem ghi chu o cho log.
|
|
size_t last_map_occ_ = 0;
|
|
size_t last_map_free_ = 0;
|
|
OccupancyGridPtr slam_map_;
|
|
double slam_process_ms_ = 0.0;
|
|
int slam_loops_ = 0;
|
|
// Phat hien KE CAN -- co che cat sai so cong don that su cua SLAM scan-only.
|
|
int slam_prox_id_ = 0;
|
|
uint64_t slam_prox_total_ = 0;
|
|
|
|
#ifdef VSLAM_HAVE_SLAM
|
|
// Duoi `slam_mu_`: tap voxel day du gan nhat va payload delta DA MA HOA.
|
|
// Ma hoa mot lan o luong slam roi dung chung cho moi session -- y het cach
|
|
// khung MAP_FULL dung chung mot bo dem.
|
|
VoxelGridPtr slam_voxels_;
|
|
std::shared_ptr<const std::string> slam_voxel_delta_;
|
|
|
|
// ⚠️ RIENG cua luong SlamLoop, KHONG duoi mutex nao: chi vong do doc va ghi.
|
|
// Dat chung vao `slam_mu_` la keo mot vector 150k phan tu vao trong lock ma
|
|
// WebLoop giu 10 lan moi giay.
|
|
std::vector<VoxelIndex> prev_voxel_cells_;
|
|
uint32_t voxel_epoch_ = 0;
|
|
uint32_t voxel_seq_ = 0;
|
|
|
|
// ⚠️ Dinh vi lai tu dong: RIENG cua luong SlamLoop, KHONG duoi mutex nao --
|
|
// tru `reloc_status_` (duoi slam_mu_, WebLoop doc).
|
|
std::unique_ptr<LikelihoodField> reloc_field_; // null = khong bat --slam-reloc
|
|
GlobalLocalizerConfig reloc_cfg_;
|
|
std::unique_ptr<HypothesisTracker> reloc_tracker_;
|
|
bool reloc_active_ = false; // dang gom/khop: CHUA dua gi cho RTAB-Map
|
|
std::vector<Point> reloc_pts_; // diem da gom, frame odom
|
|
double reloc_swept_ = 0.0; // rad da xoay trong vong gom nay
|
|
Pose2D reloc_prev_odom_;
|
|
bool reloc_have_prev_ = false;
|
|
std::chrono::steady_clock::time_point reloc_t0_; // dau vong gom
|
|
std::chrono::steady_clock::time_point reloc_epoch_; // moc thoi gian cho tracker
|
|
int reloc_cycle_ = 0;
|
|
double reloc_ref_score_ = -1.0; // diem luc chot -- moc cho canh "da lac"
|
|
bool reloc_lost_pending_ = false;
|
|
std::chrono::steady_clock::time_point reloc_lost_since_;
|
|
std::chrono::steady_clock::time_point reloc_watch_next_;
|
|
Transform slam_laser_to_base_ = TransformIdentity(); // doc-only sau Start()
|
|
std::string reloc_status_; // duoi slam_mu_
|
|
bool reloc_pending_ = false; // duoi slam_mu_: chua dinh vi xong
|
|
|
|
// Tu chay: XOAY (gom) -> khop -> chua chot thi QUAY ve cho thoang -> DI ->
|
|
// XOAY... Tran cung 6 vong / 12 m roi DUNG HAN goi nguoi (nhu ban ROS).
|
|
enum class RelocExec { kOff, kSweep, kTurn, kDrive, kHalted };
|
|
RelocExec reloc_exec_ = RelocExec::kOff;
|
|
double reloc_turn_target_ = 0.0; // huong odom can quay toi (rad)
|
|
Pose2D reloc_drive_start_;
|
|
double reloc_drive_len_ = 0.0; // quang duong du dinh cua pha DI
|
|
double reloc_total_drive_ = 0.0; // cong don cho mot lan dinh vi
|
|
int reloc_exec_cycles_ = 0;
|
|
std::chrono::steady_clock::time_point reloc_phase_t0_;
|
|
std::chrono::steady_clock::time_point reloc_yield_until_; // nguoi thang -> nhuong
|
|
|
|
// Pha "dung yen roi tinh chinh" (RelocSettle). Gom luc DANG QUAY lam dam
|
|
// diem nhoe 1-2 do moi scan (odom doc tre hon luc quet) -- mo phong
|
|
// 2026-09-29: chot lech 0.12 m va RTAB-Map khong sua vi robot dung ngay.
|
|
bool reloc_settle_ = false;
|
|
Pose2D reloc_settle_T_; // T_map_odom luc chot, chua tinh chinh
|
|
double reloc_settle_ratio_ = 0.0;
|
|
std::chrono::steady_clock::time_point reloc_settle_t0_;
|
|
std::chrono::steady_clock::time_point reloc_still_since_;
|
|
bool reloc_still_ = false;
|
|
Pose2D reloc_settle_prev_odom_;
|
|
std::vector<Point> reloc_settle_pts_;
|
|
int reloc_settle_scans_ = 0;
|
|
std::string reloc_settle_src_; // "LiDAR, ty le .." | "camera+LiDAR (..)" -- log/web
|
|
|
|
// --- Camera trong dinh vi lai (--slam-reloc-vpr). CHI luong slam cham. ---
|
|
std::unique_ptr<VprEncoder> vpr_; // null = khong dung camera
|
|
std::unique_ptr<VisualVerifier> vverif_;
|
|
std::vector<int> vpr_ids_; // node ban do co mo ta
|
|
std::vector<float> vpr_desc_; // vpr_ids_.size() x dim, da chuan hoa
|
|
struct MapShot {
|
|
VisualFrame f; // ORB + depth + K + T_base_optical
|
|
cv::Matx44d T_map_base;
|
|
};
|
|
std::map<int, MapShot> vpr_shots_; // node ban do, nap khi can roi giu
|
|
bool reloc_cam_have_last_ = false;
|
|
double reloc_cam_last_theta_ = 0.0; // odom.theta lan chup gan nhat
|
|
UnixTime reloc_cam_last_stamp_;
|
|
int reloc_cam_shots_ = 0; // anh da thu trong vong gom nay
|
|
int reloc_cam_best_inl_ = 0; // inlier PnP tot nhat vong nay
|
|
#endif
|
|
|
|
// Doc-only sau Start(), nen khong can mutex.
|
|
bool slam_enabled_ = false;
|
|
bool slam_should_save_ = false;
|
|
// slam.camera_3d VA co SLAM that. Quyet dinh co latch depth hay khong: tat
|
|
// thi ca duong nay khong ton mot lan copy con tro nao.
|
|
bool slam_use_depth_ = false;
|
|
// Ban do voxel 3D bat hay khong. Bang camera_3d, giu rieng de doc duoc o
|
|
// WebLoop ma khong phai cham vao cfg_->slam.
|
|
bool voxel_enabled_ = false;
|
|
// Tran toc do dang dung (m/s): tu robot.yaml, hoac --max-speed ghi de.
|
|
double speed_limit_ = 0.35;
|
|
// Gia toc thuc su dung. Bang robot.yaml, TRU khi `--max-speed` noi tran --
|
|
// khi do no noi theo cung ti le, xem ghi chu trong runtime.cc.
|
|
double accel_limit_ = 0.30;
|
|
// Sai so vs ground truth, de ghi log dinh ky. Chi co nghia khi chay sim.
|
|
// Lay ground truth cua mo phong. Rong khi khong chay sim.
|
|
//
|
|
// Dung std::function chu khong con tro toi SimSource: `wiring.cc` khong
|
|
// thay dinh nghia cua `SourceHolder`, va keo them mot include vao day chi
|
|
// de doc mot Pose2D la khong dang.
|
|
std::function<Pose2D()> gt_fn_;
|
|
|
|
// Pose wheel_odom lan truoc, de tinh DELTA cho EKF. Duoi `odom_mu_`.
|
|
Pose2D prev_odom_;
|
|
UnixTime prev_odom_stamp_;
|
|
bool have_prev_odom_ = false;
|
|
double gyro_z_ = 0.0;
|
|
UnixTime gyro_stamp_;
|
|
// Tich phan gyro giua hai lan cap nhat odom, va tong thoi gian da phu.
|
|
double gyro_dtheta_ = 0.0;
|
|
double gyro_dt_ = 0.0;
|
|
bool have_gyro_ = false;
|
|
|
|
// Ground truth CHOT cung luc voi EKF, duoi `odom_mu_`. Doc tuoi luc chup
|
|
// snapshot roi dem voi pose cu la do DO TRE chu khong phai sai so.
|
|
Pose2D gt_at_ekf_;
|
|
bool have_gt_at_ekf_ = false;
|
|
|
|
// Do lon phep nan cua SLAM, cho dong log CHECK.
|
|
double mc_now_ = 0.0;
|
|
double mc_deg_ = 0.0;
|
|
double gt_err_max_ = 0.0;
|
|
// ⚠️ steady_clock: day la mot CHU KY, khong phai mot moc thoi gian.
|
|
std::chrono::steady_clock::time_point gt_log_next_{};
|
|
#ifdef VSLAM_HAVE_SLAM
|
|
SlamMode slam_mode_ = SlamMode::kMapping;
|
|
#endif
|
|
|
|
// Nghieng, CHI de hien thi. Luong source ghi o 200 Hz, luong web doc o
|
|
// 10 Hz.
|
|
//
|
|
// ⚠️ ThreadSanitizer da bat dung cho nay: truoc do WebLoop goi thang
|
|
// `perception_->gravity().roll()` trong khi luong source dang ghi bien do
|
|
// trong `FeedImu`. `GravityAlign` co hop dong la MOT LUONG -- sua bang
|
|
// cach them khoa vao thu vien se pha hop dong do va bat moi nguoi dung
|
|
// tra gia cho mot nhu cau chi cua rieng web UI.
|
|
//
|
|
// Ba atomic rieng, KHONG phai mot bo ba nguyen tu: doc co the lay roll cua
|
|
// mau N va pitch cua mau N+1. Voi mot so hien tren man hinh o 10 Hz thi
|
|
// dieu do vo hai, va noi ro o day con hon gia vo rang no nguyen tu.
|
|
std::atomic<double> tilt_roll_{0.0};
|
|
std::atomic<double> tilt_pitch_{0.0};
|
|
std::atomic<bool> tilt_accel_ok_{false};
|
|
|
|
std::atomic<bool> estop_{false};
|
|
std::atomic<bool> stop_{false};
|
|
|
|
std::thread source_thread_;
|
|
std::thread safety_thread_;
|
|
std::thread gamepad_thread_;
|
|
std::thread web_thread_;
|
|
std::thread slam_thread_;
|
|
};
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_APP_RUNTIME_H
|