upload
This commit is contained in:
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