#ifndef VSLAM_TRANSFORM_TREE_H #define VSLAM_TRANSFORM_TREE_H // Thay tf2_ros::Buffer. // // Khong dung tf3 (github.com/Huandaotien/tf3, cai ma xloc_cpp dung) vi: // submodule third_party/tf3 chua bao gio duoc checkout nen khong kiem chung // duoc, license cua fork ca nhan chua ro, va no la thu vien TONG QUAT cho cay // tuy y voi re-parent va da publisher. Bai toan o day hep hon nhieu. // // BE MAT API van giu tuong thich tf3 (lookupTransform / setTransform) de sau // nay muon doi sang tf3 chi la thay header. // // CAY TF CUA HE NAY -- 9 frame, va chi BA canh la dong: // // map // | <- slam, ~1 Hz, tu getMapCorrection() // odom // | <- ekf, 50 Hz // base_footprint // | <- gravity_align, 50 Hz (roll/pitch DO duoc) // base_link // |-- laser_link tinh // |-- imu_link tinh // `-- camera_link tinh // `-- camera_depth_optical_frame tinh // |-- camera_infra1_optical_frame tinh // `-- camera_infra2_optical_frame tinh // // Moi canh tinh nap tu robot.yaml. Day chinh la thu THAY URDF, va no xoa luon // bug "hai cho phai giu dong bo" ma PROJECT_CONTEXT.md ghi: hien gia tri hinh // hoc nam ca trong robot.urdf.xacro lan robot.yaml. // // QUY TAC BAT BUOC: MOI CANH CO DUNG MOT NGUOI GHI. // Vi pham quy tac nay la lop bug TF kho debug nhat. localization.launch cua // repo cu da phai canh bao dieu nay bang mot khoi comment o dau file. // // Vi cha cua moi frame co dinh luc khoi tao, duong di child->root duoc tinh // truoc thanh mot mang: vong lookup khong tra map, khong cap phat. #include #include #include #include #include "vslam/unix_time.h" #include "vslam/vslam_common.h" namespace vslam { using FrameId = uint16_t; constexpr FrameId kInvalidFrame = 0xFFFF; class TransformTree { public: // history_sec: giu bao lau lich su canh dong. // slots_per_edge: so mau moi canh dong (ring buffer). // 50 Hz * 10 s = 500, nen 512 la du cho canh nhanh nhat. explicit TransformTree(double history_sec = 10.0, size_t slots_per_edge = 512); ~TransformTree(); TransformTree(const TransformTree&) = delete; TransformTree& operator=(const TransformTree&) = delete; // --- Dang ky frame ------------------------------------------------------ // Tra ve id on dinh cho ten frame. Goi nhieu lan cung ten tra cung id. // Chuoi chi bam MOT LAN o day; moi thao tac sau deu dung FrameId. FrameId Resolve(const std::string& name); // kInvalidFrame neu chua dang ky. FrameId Find(const std::string& name) const; const std::string& Name(FrameId id) const; size_t frame_count() const; // --- Canh tinh ---------------------------------------------------------- // Dat canh tinh parent -> child. Goi lai voi cung cap se GHI DE (cho phep, // vi nap lai config la thao tac hop le). // Nem ConfigError neu tao vong. StatusResponse SetStatic(FrameId parent, FrameId child, const Transform& tf); StatusResponse SetStatic(const std::string& parent, const std::string& child, const Transform& tf); // --- Canh dong ---------------------------------------------------------- // Dat mau moi cho canh dong parent -> child. // // Tra false neu: // * stamp lui ve qua khu so voi mau moi nhat (du hieu ban ghi bi xao tron // thu tu, hoac hai nguoi cung ghi mot canh -- vi pham quy tac tren) // * canh nay da duoc khai la tinh // // Lan goi dau tien tren mot cap (parent, child) se dang ky canh do la dong. bool SetTransform(FrameId parent, FrameId child, UnixTime stamp, const Transform& tf); // --- Tra cuu ------------------------------------------------------------ // Bien doi dua diem tu `source` sang `target` tai thoi diem `stamp`. // // Noi suy: LERP tinh tien + SLERP quay giua hai mau kep `stamp`. // Ngoai suy: TU CHOI neu stamp > mau moi nhat + extrapolation_limit. // Day la khac biet co y voi tf2: tf2 nem TransformExtrapolationException va // rat nhieu code goi bat roi bo qua im lang. O day tra false va dien `err`. bool Lookup(FrameId target, FrameId source, UnixTime stamp, Transform* out, std::string* err = nullptr) const; bool Lookup(const std::string& target, const std::string& source, UnixTime stamp, Transform* out, std::string* err = nullptr) const; // Dung mau moi nhat cua tung canh, khong noi suy. Tuong duong // lookupTransform(..., ros::Time(0), ...) cua tf2. bool LookupLatest(FrameId target, FrameId source, Transform* out, std::string* err = nullptr) const; // Thoi diem moi nhat ma CA HAI nhanh deu co du lieu. kTimeZero neu khong co. UnixTime LatestCommonTime(FrameId a, FrameId b) const; // Co du lieu de tra cuu tai `stamp` khong, khong thuc su tinh. bool CanTransform(FrameId target, FrameId source, UnixTime stamp) const; // --- Cau hinh ----------------------------------------------------------- void SetExtrapolationLimit(double sec); double extrapolation_limit() const; // Danh sach canh dong kem tan so va tuoi mau moi nhat -- ra /api/status. struct EdgeStatus { std::string parent; std::string child; bool is_static = false; uint64_t samples = 0; UnixTime latest; double hz = 0.0; }; std::vector EdgeStatuses() const; // In cay ra chuoi nhieu dong, dung de chan doan. Thay rqt_tf_tree. std::string DumpTree() const; private: struct Impl; std::unique_ptr impl_; }; using TransformTreePtr = std::shared_ptr; // Tuong duong xloc_cpp/include/tf_bridge.h: thu hep be mat xuong dung mot // thao tac ma cac stage can -- dua mot frame ve frame theo doi. class TfBridge { public: TfBridge(std::string tracking_frame, double lookup_timeout_sec, std::shared_ptr tree); bool LookupToTracking(UnixTime time, FrameId frame, Transform* out) const; bool LookupToTracking(UnixTime time, const std::string& frame, Transform* out) const; const std::string& tracking_frame() const { return tracking_frame_; } private: std::string tracking_frame_; double lookup_timeout_sec_; std::shared_ptr tree_; }; } // namespace vslam #endif // VSLAM_TRANSFORM_TREE_H