Files
vslam_cpp/libs/vslam_common/include/vslam/vslam_common.h
trung09122002 0a1b1b13f1 update
2026-09-23 13:36:48 +07:00

313 lines
9.9 KiB
C++

#ifndef VSLAM_VSLAM_COMMON_H
#define VSLAM_VSLAM_COMMON_H
// Dinh nghia lai cac kieu ban tin thuong dung, thay cho sensor_msgs /
// geometry_msgs / nav_msgs. Khuon mau: xloc_cpp/include/xloc_common.h.
//
// HAI QUY TAC BAT BUOC cho moi struct trong file nay:
//
// 1. KHONG chua kieu Eigen fixed-size (Eigen::Vector3d, Matrix4d, ...).
// Dung double x, y, z. Eigen fixed-size co yeu cau can chinh 16/32 byte;
// dat chung trong struct di qua bien .so la nguon loi alignment kinh dien
// tren arm64 khi hai phia compile voi EIGEN_MAX_ALIGN_BYTES khac nhau.
// Chuyen sang Eigen o trong ham, khong o trong kieu du lieu.
//
// 2. KHONG include header cua ROS, PCL, OpenCV hay RTAB-Map. File nay phai
// compile duoc voi mot Eigen-less, PCL-less toolchain. Cac lib phia tren
// tu chuyen doi sang kieu cua ho o bien cua minh.
//
// Vung du lieu lon (anh, point cloud, scan) luon di qua he thong duoi dang
// std::shared_ptr<const T>. Khong bao gio copy. Xem channel.h.
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "vslam/unix_time.h"
namespace vslam {
// ---------------------------------------------------------------------------
// Trang thai tra ve
// ---------------------------------------------------------------------------
enum class StatusCode : int32_t {
kOk = 0,
kInvalidArgument = 1,
kNotFound = 2,
kFailedPrecondition = 3,
kUnavailable = 4,
kTimeout = 5,
kInternal = 6,
kUnauthenticated = 7,
};
struct StatusResponse {
StatusCode code = StatusCode::kOk;
std::string message;
bool ok() const { return code == StatusCode::kOk; }
explicit operator bool() const { return ok(); }
static StatusResponse Ok() { return {}; }
static StatusResponse Error(StatusCode c, std::string m) {
return StatusResponse{c, std::move(m)};
}
};
// ---------------------------------------------------------------------------
// std_msgs
// ---------------------------------------------------------------------------
struct Header {
uint32_t seq = 0;
UnixTime stamp;
std::string frame_id;
};
// ---------------------------------------------------------------------------
// geometry_msgs
// ---------------------------------------------------------------------------
struct Point {
double x = 0.0, y = 0.0, z = 0.0;
};
struct Vector3 {
double x = 0.0, y = 0.0, z = 0.0;
};
struct Quaternion {
double x = 0.0, y = 0.0, z = 0.0, w = 1.0;
};
struct Pose {
Point position;
Quaternion orientation;
};
struct Pose2D {
double x = 0.0, y = 0.0, theta = 0.0;
};
struct PoseStamped {
Header header;
Pose pose;
};
struct PoseWithCovariance {
Pose pose;
double covariance[36] = {}; // row-major 6x6: x y z roll pitch yaw
};
struct PoseWithCovarianceStamped {
Header header;
PoseWithCovariance pose;
};
struct Twist {
Vector3 linear;
Vector3 angular;
};
struct TwistWithCovariance {
Twist twist;
double covariance[36] = {};
};
// Phep bien doi cung. Dung trong TransformTree.
struct Transform {
Vector3 translation;
Quaternion rotation;
};
struct TransformStamped {
Header header; // header.frame_id = frame CHA
std::string child_frame_id;
Transform transform;
};
// ---------------------------------------------------------------------------
// nav_msgs
// ---------------------------------------------------------------------------
struct Odometry {
Header header; // header.frame_id = frame tham chieu (vd "odom")
std::string child_frame_id; // vd "base_footprint"
PoseWithCovariance pose;
TwistWithCovariance twist;
};
// value: -1 = chua biet, 0..100 = xac suat bi chiem.
// Bo cuc giong nav_msgs/OccupancyGrid: hang chinh, goc o (origin), o (0,0)
// nam o phan tu data[0].
struct OccupancyGrid {
Header header;
float resolution = 0.05f; // met / o
uint32_t width = 0;
uint32_t height = 0;
Pose origin; // goc duoi-trai cua o (0,0), trong frame header
std::vector<int8_t> data; // size = width * height
};
// Ban do VOXEL: dang thua, chi nhung o BI CHIEM.
//
// Nam o vslam_common chu khong o vslam_slam vi `vslam_web` phai ma hoa duoc
// kieu nay ma no chi phu thuoc vslam_common -- y het ly do OccupancyGrid nam
// o day.
//
// Chi so la SO NGUYEN CO DAU quanh goc (0,0,0) cua frame map:
// index = floor(toa_do / resolution)
// Goc ghim vinh vien tai goc cua map frame, khong troi theo ban do, nho vay
// hai lan xuat khac nhau so sanh truc tiep duoc bang phep tru tap hop.
// int16 o 0.10 m = +-3276 m, thua cho moi toa nha trong nha.
//
// Ten la `VoxelIndex` chu khong phai `VoxelKey`: `vslam::VoxelKey` DA TON TAI
// la mot HAM trong depth_filter.h. C++ cho phep ham va lop trung ten trong
// cung mot scope, nhung ham CHE ten lop -- `std::vector<VoxelKey>` khi do bat
// phai cai ham va bao "expected a type". Da mac dung loi nay mot lan.
struct VoxelIndex {
int16_t x = 0;
int16_t y = 0;
int16_t z = 0;
};
struct VoxelGrid {
Header header; // frame_id = kMapFrame
float resolution = 0.10f; // kich thuoc o PHAT (khac Grid/CellSize cua SLAM)
double origin_x = 0.0; // luon 0 hien tai; giu tren day de doi sau nay
double origin_y = 0.0;
double origin_z = 0.0;
// Tang khi tap cu KHONG con dung nua (do thi vua duoc toi uu lai sau mot
// vong dong, hoac doi resolution). Client thay epoch la se tai lai tu dau
// thay vi ap delta.
uint32_t epoch = 0;
bool truncated = false; // cham tran max_cells -> ban do bi cat bot
std::vector<VoxelIndex> cells; // DA SAP XEP theo khoa goi, khong trung
};
using VoxelGridPtr = std::shared_ptr<const VoxelGrid>;
// ---------------------------------------------------------------------------
// sensor_msgs
// ---------------------------------------------------------------------------
struct Imu {
Header header;
Quaternion orientation;
double orientation_covariance[9] = {}; // [0] < 0 nghia la khong co orientation
Vector3 angular_velocity; // rad/s
double angular_velocity_covariance[9] = {};
Vector3 linear_acceleration; // m/s^2
double linear_acceleration_covariance[9] = {};
};
struct FluidPressure {
Header header;
double fluid_pressure = 0.0; // Pa
double variance = 0.0;
};
struct Temperature {
Header header;
double temperature = 0.0; // do C
double variance = 0.0;
};
// BA NGU NGHIA CUA MOT TIA -- doc truoc khi cham vao bat cu code nao doc scan.
// Duoc dinh nghia o scan_sanitizer va safety_monitor dua vao dung ba thu nay:
//
// NaN : khong do duoc (duoi range_min, hoac driver tra 0).
// +inf : do duoc nhung khong co phan xa (vuot range_max).
// range_max + 1.0 : SENTINEL -- tia bi che boi than robot, da bi cat co
// y boi sector mask. KHAC HAN voi hai truong hop tren:
// day khong phai "khong thay gi", ma la "khong duoc
// phep nhin". safety_monitor dem rieng so tia nay
// (front_blanked) va khong tinh vao front_usable.
//
// Nham ba thu nay lai lam robot hoac dung im (SCAN_SPARSE) hoac dam vao tuong.
struct LaserScan {
Header header;
float angle_min = 0.0f; // rad
float angle_max = 0.0f;
float angle_increment = 0.0f;
float time_increment = 0.0f; // giay giua hai tia
float scan_time = 0.0f; // giay giua hai sweep
float range_min = 0.0f; // met
float range_max = 0.0f;
std::vector<float> ranges;
std::vector<float> intensities; // co the rong
};
struct CameraInfo {
Header header;
uint32_t width = 0;
uint32_t height = 0;
double k[9] = {}; // ma tran noi 3x3, hang chinh
double d[5] = {}; // meo anh (plumb bob)
uint32_t binning_x = 1;
uint32_t binning_y = 1;
double fx() const { return k[0]; }
double fy() const { return k[4]; }
double cx() const { return k[2]; }
double cy() const { return k[5]; }
// Noi suy sau khi decimate: crop_decimate cua image_proc cap nhat binning
// chu KHONG chia lai K. Consumer phai tu bu, dung nhu image_geometry lam.
double fx_binned() const { return k[0] / static_cast<double>(binning_x); }
double fy_binned() const { return k[4] / static_cast<double>(binning_y); }
double cx_binned() const { return k[2] / static_cast<double>(binning_x); }
double cy_binned() const { return k[5] / static_cast<double>(binning_y); }
};
enum class PixelFormat : uint8_t {
kMono8 = 0, // 8UC1 -- infra1 / infra2
kMono16 = 1, // 16UC1 -- depth thang do theo depth_scale
kFloat32 = 2, // 32FC1 -- depth met
};
// Anh KHONG so huu bo nho khi den tu librealsense: `data` tro vao buffer cua
// SDK va `keep_alive` giu rs2::frame song. Xem realsense_driver.
struct Image {
Header header;
uint32_t width = 0;
uint32_t height = 0;
uint32_t step = 0; // byte moi hang
PixelFormat format = PixelFormat::kMono8;
float depth_scale = 0.001f; // met moi don vi, chi co nghia voi kMono16
const uint8_t* data = nullptr;
std::shared_ptr<void> keep_alive; // giu nguon buffer song
size_t size_bytes() const { return static_cast<size_t>(step) * height; }
};
struct PointXYZ {
float x = 0.0f, y = 0.0f, z = 0.0f;
};
// Cloud phang (khong to chuc theo anh). Cac buoc loc tra ve kieu nay.
struct PointCloudXYZ {
Header header;
std::vector<PointXYZ> points;
bool is_dense = false; // false nghia la co the co NaN
};
// ---------------------------------------------------------------------------
// Bi danh cho con tro chia se -- moi thu di qua Channel deu la kieu nay.
// ---------------------------------------------------------------------------
using LaserScanPtr = std::shared_ptr<const LaserScan>;
using ImuPtr = std::shared_ptr<const Imu>;
using ImagePtr = std::shared_ptr<const Image>;
using PointCloudXYZPtr = std::shared_ptr<const PointCloudXYZ>;
using OccupancyGridPtr = std::shared_ptr<const OccupancyGrid>;
using OdometryPtr = std::shared_ptr<const Odometry>;
} // namespace vslam
#endif // VSLAM_VSLAM_COMMON_H