upload
This commit is contained in:
14
libs/vslam_odometry/CMakeLists.txt
Normal file
14
libs/vslam_odometry/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
# Odometry banh xe + EKF SE(2).
|
||||
#
|
||||
# KHONG them dep ngoai nao: Eigen da den qua vslam_common. Do la co y -- lib
|
||||
# nay phai build duoc truoc khi co PCL, OpenCV hay RTAB-Map, de P3 chay som.
|
||||
|
||||
vslam_add_library(vslam_odometry
|
||||
PUBLIC_HDRS
|
||||
wheel_odometry.h
|
||||
ekf_se2.h
|
||||
PUBLIC_DEPS
|
||||
vslam_common
|
||||
FIND_DEPS
|
||||
vslam_common
|
||||
)
|
||||
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
|
||||
255
libs/vslam_odometry/src/ekf_se2.cc
Normal file
255
libs/vslam_odometry/src/ekf_se2.cc
Normal file
@@ -0,0 +1,255 @@
|
||||
#include "vslam/ekf_se2.h"
|
||||
|
||||
#include <Eigen/Cholesky>
|
||||
#include <Eigen/Core>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
#include <cmath>
|
||||
#include <mutex>
|
||||
|
||||
#include "vslam/geometry.h"
|
||||
#include "vslam/log.h"
|
||||
|
||||
namespace vslam {
|
||||
namespace {
|
||||
|
||||
// Chi so trang thai: [x, y, yaw, vx, vyaw]
|
||||
constexpr int kX = 0, kY = 1, kYaw = 2, kVx = 3, kVyaw = 4, kN = 5;
|
||||
|
||||
using Vec5 = Eigen::Matrix<double, kN, 1>;
|
||||
using Mat5 = Eigen::Matrix<double, kN, kN>;
|
||||
|
||||
} // namespace
|
||||
|
||||
struct EkfSe2::Impl {
|
||||
EkfSe2Config cfg;
|
||||
|
||||
mutable std::mutex mu;
|
||||
Vec5 x = Vec5::Zero();
|
||||
Mat5 P = Mat5::Identity();
|
||||
Mat5 Q = Mat5::Zero();
|
||||
|
||||
bool have_time = false;
|
||||
UnixTime last_predict;
|
||||
UnixTime last_measurement;
|
||||
|
||||
EkfSe2Stats stats;
|
||||
|
||||
explicit Impl(EkfSe2Config c) : cfg(std::move(c)) {
|
||||
Q(kX, kX) = cfg.q_x;
|
||||
Q(kY, kY) = cfg.q_y;
|
||||
Q(kYaw, kYaw) = cfg.q_yaw;
|
||||
Q(kVx, kVx) = cfg.q_vx;
|
||||
Q(kVyaw, kVyaw) = cfg.q_vyaw;
|
||||
|
||||
P = Mat5::Zero();
|
||||
P(kX, kX) = P(kY, kY) = P(kYaw, kYaw) = cfg.p0_pose;
|
||||
P(kVx, kVx) = P(kVyaw, kVyaw) = cfg.p0_vel;
|
||||
}
|
||||
|
||||
// Goi khi da giu mu.
|
||||
void PredictLocked(double dt) {
|
||||
if (dt <= 0.0) return;
|
||||
|
||||
const double yaw = x(kYaw);
|
||||
const double vx = x(kVx);
|
||||
const double c = std::cos(yaw), s = std::sin(yaw);
|
||||
|
||||
// Mo hinh van toc khong doi trong he than robot.
|
||||
// x' = x + vx*cos(yaw)*dt
|
||||
// y' = y + vx*sin(yaw)*dt
|
||||
// yaw' = yaw + vyaw*dt
|
||||
// vx' = vx
|
||||
// vyaw' = vyaw
|
||||
//
|
||||
// KHONG co vy: robot vi sai khong truot ngang duoc. Do la ly do
|
||||
// odom0_config dat vy = false trong ban cu.
|
||||
x(kX) += vx * c * dt;
|
||||
x(kY) += vx * s * dt;
|
||||
x(kYaw) = NormalizeAngle(x(kYaw) + x(kVyaw) * dt);
|
||||
|
||||
Mat5 F = Mat5::Identity();
|
||||
F(kX, kYaw) = -vx * s * dt;
|
||||
F(kX, kVx) = c * dt;
|
||||
F(kY, kYaw) = vx * c * dt;
|
||||
F(kY, kVx) = s * dt;
|
||||
F(kYaw, kVyaw) = dt;
|
||||
|
||||
// Nhieu qua trinh ty le voi dt: bo qua dt se lam filter qua tu tin khi
|
||||
// chay o tan so cao va qua bao thu khi chay cham.
|
||||
P = F * P * F.transpose() + Q * dt;
|
||||
stats.predicts++;
|
||||
}
|
||||
|
||||
// Cap nhat vo huong. Tra false neu bi bo vi lech qua nhieu.
|
||||
bool UpdateScalarLocked(int state_idx, double z, double var) {
|
||||
if (!(var > 0.0) || !std::isfinite(z)) return false;
|
||||
|
||||
Eigen::Matrix<double, 1, kN> H = Eigen::Matrix<double, 1, kN>::Zero();
|
||||
H(0, state_idx) = 1.0;
|
||||
|
||||
const double y = z - x(state_idx); // phan du
|
||||
const double S = (H * P * H.transpose())(0, 0) + var;
|
||||
if (!(S > 0.0)) return false;
|
||||
|
||||
const double mahalanobis = std::sqrt(y * y / S);
|
||||
stats.last_mahalanobis = mahalanobis;
|
||||
|
||||
if (cfg.mahalanobis_threshold > 0.0 &&
|
||||
mahalanobis > cfg.mahalanobis_threshold) {
|
||||
stats.rejected_outliers++;
|
||||
VSLAM_WARN_THROTTLE(2.0, "ekf: bo phep do lech " << mahalanobis
|
||||
<< " sigma o trang thai "
|
||||
<< state_idx);
|
||||
return false;
|
||||
}
|
||||
|
||||
const Vec5 K = P * H.transpose() / S;
|
||||
x += K * y;
|
||||
x(kYaw) = NormalizeAngle(x(kYaw));
|
||||
|
||||
// Dang Joseph: giu P doi xung xac dinh duong ke ca khi co sai so lam tron.
|
||||
// Dang don gian (I-KH)P mat tinh chat do sau vai nghin buoc, va khi P mat
|
||||
// xac dinh duong thi filter phan ky ma khong co dau hieu gi.
|
||||
const Mat5 IKH = Mat5::Identity() - K * H;
|
||||
P = IKH * P * IKH.transpose() + K * var * K.transpose();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
EkfSe2::EkfSe2(EkfSe2Config cfg) : impl_(std::make_unique<Impl>(std::move(cfg))) {}
|
||||
EkfSe2::~EkfSe2() = default;
|
||||
|
||||
void EkfSe2::Predict(UnixTime now) {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
if (!impl_->have_time) {
|
||||
impl_->last_predict = now;
|
||||
impl_->have_time = true;
|
||||
return;
|
||||
}
|
||||
const double dt = SecondsBetween(now, impl_->last_predict);
|
||||
if (dt < 0.0) {
|
||||
// Thoi gian lui: reset mo phong hoac phat lai ban ghi.
|
||||
VSLAM_WARN("ekf: thoi gian lui " << dt << " s -- reset moc thoi gian");
|
||||
impl_->last_predict = now;
|
||||
impl_->stats.time_resets++;
|
||||
return;
|
||||
}
|
||||
impl_->PredictLocked(dt);
|
||||
impl_->last_predict = now;
|
||||
}
|
||||
|
||||
void EkfSe2::UpdateOdomTwist(UnixTime stamp, double vx, double vyaw,
|
||||
double var_vx, double var_vyaw) {
|
||||
Predict(stamp);
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
// var_* den tu twist.covariance cua wheel_odom -- day chinh la cho no bao
|
||||
// "dang truot, dung tin toi".
|
||||
const bool a = impl_->UpdateScalarLocked(kVx, vx, var_vx);
|
||||
const bool b = impl_->UpdateScalarLocked(kVyaw, vyaw, var_vyaw);
|
||||
if (a || b) {
|
||||
impl_->stats.odom_updates++;
|
||||
impl_->last_measurement = stamp;
|
||||
}
|
||||
}
|
||||
|
||||
void EkfSe2::UpdateImuYawRate(UnixTime stamp, double vyaw, double var) {
|
||||
Predict(stamp);
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
if (impl_->UpdateScalarLocked(kVyaw, vyaw, var)) {
|
||||
impl_->stats.imu_updates++;
|
||||
impl_->last_measurement = stamp;
|
||||
}
|
||||
}
|
||||
|
||||
Pose2D EkfSe2::pose() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return Pose2D{impl_->x(kX), impl_->x(kY), impl_->x(kYaw)};
|
||||
}
|
||||
|
||||
double EkfSe2::vx() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->x(kVx);
|
||||
}
|
||||
|
||||
double EkfSe2::vyaw() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->x(kVyaw);
|
||||
}
|
||||
|
||||
Odometry EkfSe2::State(const std::string& odom_frame,
|
||||
const std::string& base_frame) const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
Odometry o;
|
||||
o.header.stamp = impl_->last_predict;
|
||||
o.header.frame_id = odom_frame;
|
||||
o.child_frame_id = base_frame;
|
||||
|
||||
o.pose.pose.position.x = impl_->x(kX);
|
||||
o.pose.pose.position.y = impl_->x(kY);
|
||||
o.pose.pose.position.z = 0.0;
|
||||
o.pose.pose.orientation = QuaternionFromYaw(impl_->x(kYaw));
|
||||
|
||||
for (int i = 0; i < 36; ++i) o.pose.covariance[i] = 0.0;
|
||||
o.pose.covariance[0] = impl_->P(kX, kX);
|
||||
o.pose.covariance[1] = impl_->P(kX, kY);
|
||||
o.pose.covariance[6] = impl_->P(kY, kX);
|
||||
o.pose.covariance[7] = impl_->P(kY, kY);
|
||||
o.pose.covariance[14] = 1e6; // z khong quan sat duoc
|
||||
o.pose.covariance[21] = 1e6; // roll -> do tu IMU, khong uoc luong
|
||||
o.pose.covariance[28] = 1e6; // pitch -> do tu IMU, khong uoc luong
|
||||
o.pose.covariance[35] = impl_->P(kYaw, kYaw);
|
||||
|
||||
o.twist.twist.linear.x = impl_->x(kVx);
|
||||
o.twist.twist.angular.z = impl_->x(kVyaw);
|
||||
for (int i = 0; i < 36; ++i) o.twist.covariance[i] = 0.0;
|
||||
o.twist.covariance[0] = impl_->P(kVx, kVx);
|
||||
o.twist.covariance[14] = 1e6;
|
||||
o.twist.covariance[21] = 1e6;
|
||||
o.twist.covariance[28] = 1e6;
|
||||
o.twist.covariance[35] = impl_->P(kVyaw, kVyaw);
|
||||
return o;
|
||||
}
|
||||
|
||||
Transform EkfSe2::OdomToBase() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
Transform t;
|
||||
t.translation = Vector3{impl_->x(kX), impl_->x(kY), 0.0};
|
||||
t.rotation = QuaternionFromYaw(impl_->x(kYaw));
|
||||
return t;
|
||||
}
|
||||
|
||||
bool EkfSe2::Healthy(UnixTime now) const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
if (impl_->last_measurement.isZero()) return false;
|
||||
return SecondsBetween(now, impl_->last_measurement) < impl_->cfg.sensor_timeout;
|
||||
}
|
||||
|
||||
void EkfSe2::SetPose(const Pose2D& p) {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
impl_->x(kX) = p.x;
|
||||
impl_->x(kY) = p.y;
|
||||
impl_->x(kYaw) = NormalizeAngle(p.theta);
|
||||
// Dat lai pose = biet chac vi tri -> ha hiep phuong sai pose ve gia tri dau.
|
||||
// Van toc KHONG dat lai: robot co the dang chay khi bi dat lai pose.
|
||||
impl_->P(kX, kX) = impl_->P(kY, kY) = impl_->P(kYaw, kYaw) = impl_->cfg.p0_pose;
|
||||
impl_->P(kX, kY) = impl_->P(kY, kX) = 0.0;
|
||||
}
|
||||
|
||||
void EkfSe2::Reset() {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
impl_->x = Vec5::Zero();
|
||||
impl_->P = Mat5::Zero();
|
||||
impl_->P(kX, kX) = impl_->P(kY, kY) = impl_->P(kYaw, kYaw) = impl_->cfg.p0_pose;
|
||||
impl_->P(kVx, kVx) = impl_->P(kVyaw, kVyaw) = impl_->cfg.p0_vel;
|
||||
impl_->have_time = false;
|
||||
impl_->last_measurement = UnixTime();
|
||||
impl_->stats = EkfSe2Stats{};
|
||||
}
|
||||
|
||||
EkfSe2Stats EkfSe2::stats() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->stats;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
177
libs/vslam_odometry/src/wheel_odometry.cc
Normal file
177
libs/vslam_odometry/src/wheel_odometry.cc
Normal file
@@ -0,0 +1,177 @@
|
||||
#include "vslam/wheel_odometry.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "vslam/geometry.h"
|
||||
#include "vslam/log.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
WheelOdometry::WheelOdometry(WheelOdometryConfig cfg) : cfg_(std::move(cfg)) {}
|
||||
|
||||
void WheelOdometry::FeedGyroZ(UnixTime stamp, double wz) {
|
||||
gyro_z_ = wz;
|
||||
last_imu_time_ = stamp;
|
||||
have_imu_ = true;
|
||||
}
|
||||
|
||||
void WheelOdometry::SetPose(const Pose2D& p) {
|
||||
x_ = p.x;
|
||||
y_ = p.y;
|
||||
theta_ = NormalizeAngle(p.theta);
|
||||
}
|
||||
|
||||
void WheelOdometry::Reset() {
|
||||
initialized_ = false;
|
||||
x_ = y_ = theta_ = 0.0;
|
||||
slip_metric_ = 0.0;
|
||||
slipping_ = false;
|
||||
stats_ = WheelOdometryStats{};
|
||||
}
|
||||
|
||||
bool WheelOdometry::Update(UnixTime stamp, double pos_l, double pos_r,
|
||||
Odometry* out) {
|
||||
if (!initialized_) {
|
||||
prev_pos_l_ = pos_l;
|
||||
prev_pos_r_ = pos_r;
|
||||
prev_time_ = stamp;
|
||||
initialized_ = true;
|
||||
VSLAM_INFO("wheel_odom: da nhan encoder dau tien, bat dau tich phan");
|
||||
return false;
|
||||
}
|
||||
|
||||
const double dt = SecondsBetween(stamp, prev_time_);
|
||||
if (dt <= 0.0) {
|
||||
// Nhay lui thoi gian: xay ra khi reset mo phong hoac phat lai ban ghi.
|
||||
// Reset trang thai thay vi tich phan mot dt am -- tich phan dt am se day
|
||||
// pose di GIAT LUI, va EKF phia sau khong co cach nao biet.
|
||||
VSLAM_WARN("wheel_odom: dt <= 0 (" << dt << " s) -- reset trang thai tich phan");
|
||||
prev_pos_l_ = pos_l;
|
||||
prev_pos_r_ = pos_r;
|
||||
prev_time_ = stamp;
|
||||
stats_.time_resets++;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Quang duong tung banh. Chuan hoa DELTA de an toan voi ca hai kieu bao cao
|
||||
// vi tri: tich luy khong gioi han, hoac cuon ve [-pi, pi].
|
||||
const double d_l = NormalizeAngle(pos_l - prev_pos_l_) * cfg_.wheel_radius;
|
||||
const double d_r = NormalizeAngle(pos_r - prev_pos_r_) * cfg_.wheel_radius;
|
||||
|
||||
const double d_center = 0.5 * (d_l + d_r);
|
||||
const double d_theta = (d_r - d_l) / cfg_.wheel_separation;
|
||||
|
||||
// --------- Phat hien truot ---------
|
||||
const double omega_encoder = d_theta / dt;
|
||||
slip_metric_ = 0.0;
|
||||
slipping_ = false;
|
||||
|
||||
const bool imu_fresh =
|
||||
have_imu_ && (SecondsBetween(stamp, last_imu_time_) < cfg_.imu_timeout);
|
||||
if (imu_fresh) {
|
||||
slip_metric_ = std::fabs(omega_encoder - gyro_z_);
|
||||
slipping_ = slip_metric_ > cfg_.slip_omega_threshold;
|
||||
if (slipping_) {
|
||||
stats_.slip_events++;
|
||||
VSLAM_WARN_THROTTLE(2.0, "wheel_odom: nghi TRUOT BANH -- omega encoder "
|
||||
<< omega_encoder << " vs gyro " << gyro_z_
|
||||
<< " rad/s (lech " << slip_metric_
|
||||
<< "). Nang covariance.");
|
||||
}
|
||||
} else {
|
||||
stats_.no_imu_updates++;
|
||||
VSLAM_WARN_THROTTLE(10.0,
|
||||
"wheel_odom: khong co IMU moi -> KHONG phat hien duoc "
|
||||
"truot banh. Odometry dang chay 'mu'.");
|
||||
}
|
||||
|
||||
// --------- Tich phan Runge-Kutta bac 2 ---------
|
||||
// Dung goc GIUA doan thay vi goc dau doan. Voi mot robot xoay 0.8 rad/s va
|
||||
// chu ky 20 ms, khac biet giua hai cach la ~0.008 rad moi buoc -- nho, nhung
|
||||
// no tich luy theo mot chieu va thanh troi yaw sau vai phut.
|
||||
const double theta_mid = theta_ + 0.5 * d_theta;
|
||||
x_ += d_center * std::cos(theta_mid);
|
||||
y_ += d_center * std::sin(theta_mid);
|
||||
theta_ = NormalizeAngle(theta_ + d_theta);
|
||||
|
||||
stats_.updates++;
|
||||
stats_.total_distance += std::fabs(d_center);
|
||||
stats_.last_slip_metric = slip_metric_;
|
||||
|
||||
// --------- Dong goi ---------
|
||||
out->header.stamp = stamp;
|
||||
out->header.frame_id = cfg_.odom_frame;
|
||||
out->child_frame_id = cfg_.base_frame;
|
||||
|
||||
out->pose.pose.position.x = x_;
|
||||
out->pose.pose.position.y = y_;
|
||||
out->pose.pose.position.z = 0.0; // 2.5D: robot o tren mat phang
|
||||
out->pose.pose.orientation = QuaternionFromYaw(theta_);
|
||||
|
||||
out->twist.twist.linear.x = d_center / dt;
|
||||
out->twist.twist.angular.z = omega_encoder;
|
||||
|
||||
// Covariance ty le voi QUANG DUONG, khong phai thoi gian: sai so odometry
|
||||
// tich luy theo quang duong di duoc. Mot robot dung yen khong tich them sai
|
||||
// so, du dung bao lau.
|
||||
const double mult = slipping_ ? cfg_.slip_cov_multiplier : 1.0;
|
||||
const double travel = std::max(std::fabs(d_center), 1e-4);
|
||||
const double turn = std::max(std::fabs(d_theta), 1e-4);
|
||||
|
||||
const double cov_x = std::pow(cfg_.sigma_x * travel, 2) * mult;
|
||||
const double cov_y = std::pow(cfg_.sigma_y * travel, 2) * mult;
|
||||
const double cov_yaw = std::pow(cfg_.sigma_yaw * turn, 2) * mult;
|
||||
|
||||
// Chi so: 0=x, 7=y, 14=z, 21=roll, 28=pitch, 35=yaw
|
||||
for (int i = 0; i < 36; ++i) out->pose.covariance[i] = 0.0;
|
||||
out->pose.covariance[0] = cov_x;
|
||||
out->pose.covariance[7] = cov_y;
|
||||
out->pose.covariance[14] = 1e6; // z khong quan sat duoc
|
||||
out->pose.covariance[21] = 1e6; // roll -> IMU lo
|
||||
out->pose.covariance[28] = 1e6; // pitch -> IMU lo
|
||||
out->pose.covariance[35] = cov_yaw;
|
||||
|
||||
for (int i = 0; i < 36; ++i) out->twist.covariance[i] = 0.0;
|
||||
out->twist.covariance[0] = cov_x / (dt * dt);
|
||||
out->twist.covariance[7] = cov_y / (dt * dt);
|
||||
out->twist.covariance[14] = 1e6;
|
||||
out->twist.covariance[21] = 1e6;
|
||||
out->twist.covariance[28] = 1e6;
|
||||
|
||||
// ======================================================================
|
||||
// YAW-RATE: KHAI BAO DO BAT DINH DA DO, KHONG PHAI DOAN
|
||||
//
|
||||
// Khac cac thanh phan o tren: chung dung cov ty le quang duong roi nhan mot
|
||||
// he so co dinh khi truot. Yaw-rate thi KHONG.
|
||||
//
|
||||
// Ly do: slip_metric = |omega_encoder - gyro_z| CHINH LA sai so cua phep do
|
||||
// banh xe, do duoc tung chu ky. Lay no lam sigma la khai bao do bat dinh DA
|
||||
// DO, thay vi mot he so doan truoc.
|
||||
//
|
||||
// Cach cu (cov_yaw/dt^2 voi he so co dinh 100) THIEU ~2 BAC. Da do khi robot
|
||||
// xoay tai cho tren robot that:
|
||||
// sai so that : omega encoder -3.635 vs gyro -1.377 = 2.26 rad/s
|
||||
// sigma khai bao: (0.02*0.02)^2 * 100 / 0.02^2 = 0.04 -> 0.2 rad/s
|
||||
// EKF nhan mot diem 11-sigma ma van tin -> yaw troi.
|
||||
//
|
||||
// Hau qua da do o tang tren: global_scan_init ghep scan bang odom, nen sau
|
||||
// khi xoay 301 do no khop ra HUONG SAI 40.8 do (vi tri van dung 0.17 m).
|
||||
// Khong xoay thi huong DUNG -- vi khong tich luy sai so odom nao.
|
||||
//
|
||||
// San khi KHONG co gyro: luc do slip_metric = 0, va neu dung thang thi
|
||||
// sigma = 0 -> EKF tin banh xe TUYET DOI, te hon ca truoc khi sua. Vi vay
|
||||
// co yaw_rate_sigma_no_imu.
|
||||
// ======================================================================
|
||||
const double sigma_wr = imu_fresh
|
||||
? std::max(slip_metric_, cfg_.yaw_rate_sigma_floor)
|
||||
: cfg_.yaw_rate_sigma_no_imu;
|
||||
out->twist.covariance[35] = sigma_wr * sigma_wr;
|
||||
|
||||
prev_pos_l_ = pos_l;
|
||||
prev_pos_r_ = pos_r;
|
||||
prev_time_ = stamp;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user