upload
This commit is contained in:
255
app/src/runtime.h
Normal file
255
app/src/runtime.h
Normal file
@@ -0,0 +1,255 @@
|
||||
#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 <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/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/rtabmap_slam.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;
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
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();
|
||||
void WebLoop();
|
||||
void SlamLoop();
|
||||
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_;
|
||||
#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_;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// Doc-only sau Start(), nen khong can mutex.
|
||||
bool slam_enabled_ = false;
|
||||
bool slam_should_save_ = false;
|
||||
// Tran toc do dang dung (m/s): tu robot.yaml, hoac --max-speed ghi de.
|
||||
double speed_limit_ = 0.35;
|
||||
#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 web_thread_;
|
||||
std::thread slam_thread_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_APP_RUNTIME_H
|
||||
Reference in New Issue
Block a user