upload
This commit is contained in:
124
libs/vslam_odometry/include/vslam/ekf_se2.h
Normal file
124
libs/vslam_odometry/include/vslam/ekf_se2.h
Normal file
@@ -0,0 +1,124 @@
|
||||
#ifndef VSLAM_EKF_SE2_H
|
||||
#define VSLAM_EKF_SE2_H
|
||||
|
||||
// EKF cho pose SE(2). Thay robot_localization/ekf_localization_node.
|
||||
//
|
||||
// ================== VI SAO CHI 5 TRANG THAI ==================
|
||||
// robot_localization mang 15 trang thai. Cau hinh that trong ekf_odom.yaml
|
||||
// dat two_d_mode=true (khoa z/roll/pitch va dao ham cua chung) va chi bat
|
||||
// DUNG BA phep do:
|
||||
// odom0 -> vx (chi so 6), vyaw (chi so 11)
|
||||
// imu0 -> vyaw (chi so 11)
|
||||
// Nghia la 10/15 trang thai khong bao gio duoc quan sat va chi bi mang theo.
|
||||
//
|
||||
// O day giu dung 5 trang thai co that: [x, y, yaw, vx, vyaw].
|
||||
//
|
||||
// ================== HAI QUYET DINH BE NGUYEN TU BAN CU ==================
|
||||
//
|
||||
// 1. FUSE VAN TOC, KHONG FUSE POSE TUYET DOI.
|
||||
// Tich phan pose hai lan trong filter (mot lan o wheel_odom, mot lan nua
|
||||
// trong EKF) lam sai so cong don nhanh hon han. wheel_odom van xuat pose
|
||||
// de hien thi, nhung EKF chi an twist.
|
||||
//
|
||||
// 2. KHONG FUSE GIA TOC TUYEN TINH tu IMU.
|
||||
// Tren robot banh, accel nhieu manh do rung (san gach + banh cung) va co
|
||||
// bias troi. Dua vao thuong lam ket qua TE HON chu khong tot hon. Chi lay
|
||||
// gyro Z.
|
||||
//
|
||||
// ================== Z, ROLL, PITCH DI DAU ==================
|
||||
// Pose la SE(2): z gia dinh 0, roll/pitch duoc DO tu IMU chu khong bao gio
|
||||
// uoc luong. Chung khong nam trong filter nay. GravityAlign lo phan do.
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "vslam/unix_time.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct EkfSe2Config {
|
||||
double rate_hz = 50.0;
|
||||
|
||||
// Khong nhan phep do nao trong bao lau thi coi la khong lanh manh.
|
||||
double sensor_timeout = 0.2;
|
||||
|
||||
// Nhieu qua trinh, duong cheo. Lay tu process_noise_covariance cua
|
||||
// ekf_odom.yaml tai cac chi so tuong ung:
|
||||
// x=0 y=1 yaw=5 vx=6 vyaw=11
|
||||
// MOI gia tri o day la doan tu datasheet, cho hieu chuan that. Dung tin
|
||||
// chung nhu so do.
|
||||
double q_x = 0.05;
|
||||
double q_y = 0.05;
|
||||
double q_yaw = 0.06;
|
||||
double q_vx = 0.025;
|
||||
double q_vyaw = 0.02;
|
||||
|
||||
// Hiep phuong sai ban dau.
|
||||
double p0_pose = 1e-3;
|
||||
double p0_vel = 1e-2;
|
||||
|
||||
// Bo phep do lech qua nhieu so voi du doan (kiem Mahalanobis).
|
||||
// 0 = tat. Gia tri ~5 nghia la bo diem lech qua 5 sigma.
|
||||
//
|
||||
// Bat cai nay chi an toan KHI covariance dau vao dung. Voi bug yaw-rate cu
|
||||
// (thieu 2 bac, xem wheel_odometry.cc) thi moi phep do deu trong nhu 11
|
||||
// sigma va cong nay se bo HET -- nen mac dinh la TAT.
|
||||
double mahalanobis_threshold = 0.0;
|
||||
};
|
||||
|
||||
struct EkfSe2Stats {
|
||||
uint64_t predicts = 0;
|
||||
uint64_t odom_updates = 0;
|
||||
uint64_t imu_updates = 0;
|
||||
uint64_t rejected_outliers = 0;
|
||||
uint64_t time_resets = 0;
|
||||
double last_mahalanobis = 0.0;
|
||||
};
|
||||
|
||||
class EkfSe2 {
|
||||
public:
|
||||
explicit EkfSe2(EkfSe2Config cfg = EkfSe2Config());
|
||||
~EkfSe2();
|
||||
|
||||
EkfSe2(const EkfSe2&) = delete;
|
||||
EkfSe2& operator=(const EkfSe2&) = delete;
|
||||
|
||||
// Day trang thai toi `now`. An toan goi nhieu lan.
|
||||
void Predict(UnixTime now);
|
||||
|
||||
// Van toc tu encoder. `var_*` lay tu twist.covariance cua Odometry --
|
||||
// day chinh la cho wheel_odom bao "dang truot, dung tin toi".
|
||||
void UpdateOdomTwist(UnixTime stamp, double vx, double vyaw, double var_vx,
|
||||
double var_vyaw);
|
||||
|
||||
// Van toc goc tu gyro Z.
|
||||
void UpdateImuYawRate(UnixTime stamp, double vyaw, double var);
|
||||
|
||||
Pose2D pose() const;
|
||||
double vx() const;
|
||||
double vyaw() const;
|
||||
|
||||
// Odometry day du, gom ca covariance -- de xuat ra web va cho SLAM.
|
||||
Odometry State(const std::string& odom_frame, const std::string& base_frame) const;
|
||||
|
||||
// Canh odom -> base_footprint. EkfSe2 la NGUOI GHI DUY NHAT cua canh nay.
|
||||
Transform OdomToBase() const;
|
||||
|
||||
// false neu chua nhan phep do nao trong `sensor_timeout`.
|
||||
bool Healthy(UnixTime now) const;
|
||||
|
||||
void SetPose(const Pose2D& p);
|
||||
void Reset();
|
||||
|
||||
EkfSe2Stats stats() const;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_EKF_SE2_H
|
||||
113
libs/vslam_odometry/include/vslam/wheel_odometry.h
Normal file
113
libs/vslam_odometry/include/vslam/wheel_odometry.h
Normal file
@@ -0,0 +1,113 @@
|
||||
#ifndef VSLAM_WHEEL_ODOMETRY_H
|
||||
#define VSLAM_WHEEL_ODOMETRY_H
|
||||
|
||||
// Odometry banh xe + phat hien truot.
|
||||
//
|
||||
// Port tu robot_drivers/src/wheel_odom_node.cpp cua vSLAM_ws.
|
||||
// TOAN BO mo hinh covariance duoc BE NGUYEN: no da duoc chinh bang so do thuc
|
||||
// tren robot that, va cac con so trong comment o wheel_odometry.cc la do
|
||||
// duoc chu khong phai doan.
|
||||
//
|
||||
// ================== VI SAO CAN PHAT HIEN TRUOT ==================
|
||||
// San nha hang co nuoc va dau mo. Banh truot thi encoder NOI DOI: no bao robot
|
||||
// da di, thuc te robot dung yen hoac truot ngang.
|
||||
//
|
||||
// Gyro Z khong bi anh huong boi truot banh. So hai nguon:
|
||||
// omega_encoder = (v_phai - v_trai) / wheel_separation
|
||||
// omega_gyro = imu.angular_velocity.z
|
||||
// Lech nhieu => dang truot => NANG covariance de back-end tu dong tin encoder
|
||||
// it di. Khong vut bo du lieu, chi ha do tin cay.
|
||||
//
|
||||
// ================== KHONG PHAT TF ==================
|
||||
// Lop nay KHONG ghi canh odom->base_footprint. Viec do la cua EkfSe2, noi
|
||||
// encoder va IMU duoc fuse dung cach. Moi canh TF co dung mot nguoi ghi.
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "vslam/unix_time.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct WheelOdometryConfig {
|
||||
// Hinh hoc. PHAI khop robot.yaml -- day la gia tri duy nhat, khong con ban
|
||||
// sao trong URDF nua.
|
||||
double wheel_radius = 0.075;
|
||||
double wheel_separation = 0.36;
|
||||
|
||||
// Truot: lech omega vuot nguong nay thi coi la dang truot.
|
||||
double slip_omega_threshold = 0.15;
|
||||
double slip_cov_multiplier = 100.0;
|
||||
|
||||
// IMU cu hon nguong nay thi coi nhu khong co gyro.
|
||||
double imu_timeout = 0.5;
|
||||
|
||||
// Sigma co ban, nhan voi QUANG DUONG (khong phai thoi gian).
|
||||
double sigma_x = 0.02;
|
||||
double sigma_y = 0.02;
|
||||
double sigma_yaw = 0.02;
|
||||
|
||||
// Xem khoi comment lon trong wheel_odometry.cc -- hai gia tri nay la ket
|
||||
// qua cua mot bug da do duoc, khong phai tham so tuy chinh.
|
||||
double yaw_rate_sigma_floor = 0.02;
|
||||
double yaw_rate_sigma_no_imu = 0.50;
|
||||
|
||||
std::string odom_frame = "odom";
|
||||
std::string base_frame = "base_footprint";
|
||||
};
|
||||
|
||||
struct WheelOdometryStats {
|
||||
uint64_t updates = 0;
|
||||
uint64_t slip_events = 0;
|
||||
uint64_t time_resets = 0; // dt <= 0: reset sim hoac replay nhay lui
|
||||
uint64_t no_imu_updates = 0; // chay "mu", khong phat hien duoc truot
|
||||
double last_slip_metric = 0.0;
|
||||
double total_distance = 0.0;
|
||||
};
|
||||
|
||||
class WheelOdometry {
|
||||
public:
|
||||
explicit WheelOdometry(WheelOdometryConfig cfg);
|
||||
|
||||
// Gyro Z tu IMU. Goi truoc Update() de phat hien truot hoat dong.
|
||||
void FeedGyroZ(UnixTime stamp, double wz);
|
||||
|
||||
// left_rad / right_rad: goc quay TICH LUY cua tung banh.
|
||||
//
|
||||
// Tra false khi chua du du lieu de tich phan (mau dau tien, hoac dt <= 0).
|
||||
// Khi do `out` khong duoc ghi.
|
||||
bool Update(UnixTime stamp, double left_rad, double right_rad, Odometry* out);
|
||||
|
||||
// |omega_encoder - gyro_z| cua lan cap nhat gan nhat.
|
||||
double slip_metric() const { return slip_metric_; }
|
||||
bool slipping() const { return slipping_; }
|
||||
|
||||
Pose2D pose() const { return Pose2D{x_, y_, theta_}; }
|
||||
void SetPose(const Pose2D& p);
|
||||
void Reset();
|
||||
|
||||
const WheelOdometryStats& stats() const { return stats_; }
|
||||
|
||||
private:
|
||||
WheelOdometryConfig cfg_;
|
||||
|
||||
bool initialized_ = false;
|
||||
double prev_pos_l_ = 0.0, prev_pos_r_ = 0.0;
|
||||
UnixTime prev_time_;
|
||||
|
||||
double x_ = 0.0, y_ = 0.0, theta_ = 0.0;
|
||||
|
||||
bool have_imu_ = false;
|
||||
double gyro_z_ = 0.0;
|
||||
UnixTime last_imu_time_;
|
||||
|
||||
double slip_metric_ = 0.0;
|
||||
bool slipping_ = false;
|
||||
|
||||
WheelOdometryStats stats_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_WHEEL_ODOMETRY_H
|
||||
Reference in New Issue
Block a user