#include "vslam/ekf_se2.h" #include #include #include #include #include #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; using Mat5 = Eigen::Matrix; } // 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; // Da chuyen sang mo hinh chuyen dong theo delta chua. Xem PredictLocked(). bool use_delta_motion = false; 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; // ⚠️ Khi mo hinh chuyen dong theo DELTA dang chay, phep du doan nay KHONG // duoc dich pose nua. // // `UpdateImuYawRate` chay 200 Hz va cung goi `Predict`, nen pose se bi // tich phan HAI LAN: mot lan boi delta cua wheel odometry, mot lan boi // trang thai van toc o day. Da do: sai goc **148 do** sau mot lan xoay // tai cho -- lon den muc khong the nham voi sai so tinh vi nao. if (use_delta_motion) { // Chi noi rong hiep phuong sai theo thoi gian, khong dich trang thai. P += Q * dt; 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 H = Eigen::Matrix::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(std::move(cfg))) {} EkfSe2::~EkfSe2() = default; void EkfSe2::Predict(UnixTime now) { std::lock_guard 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 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::UpdateOdomDelta(UnixTime stamp, double d_trans, double d_theta_enc, double d_theta_gyro, double var_trans, double var_theta_enc, double var_theta_gyro) { std::lock_guard lk(impl_->mu); // Tron hai nguon goc theo nghich dao phuong sai. Khi banh truot, // `var_theta_enc` phong len va gyro thang the -- do la ca ly do EKF ton tai. double d_theta = d_theta_enc; if (var_theta_enc > 0.0 && var_theta_gyro > 0.0) { const double we = 1.0 / var_theta_enc, wg = 1.0 / var_theta_gyro; d_theta = (we * d_theta_enc + wg * d_theta_gyro) / (we + wg); } auto& x = impl_->x; auto& P = impl_->P; // Tich phan diem GIUA: dung cung bac voi RK2 ma wheel_odom dung, de hai ben // khong le nhau mot bac. const double mid = x(kYaw) + 0.5 * d_theta; const double c = std::cos(mid), s = std::sin(mid); x(kX) += d_trans * c; x(kY) += d_trans * s; x(kYaw) = NormalizeAngle(x(kYaw) + d_theta); // Van toc van la trang thai, de bao cao va de phat hien truot -- nhung // chung KHONG con tham gia vao phep tich phan pose nua. const double dt = impl_->last_predict.isZero() ? 0.0 : SecondsBetween(stamp, impl_->last_predict); if (dt > 1e-6) { x(kVx) = d_trans / dt; x(kVyaw) = d_theta / dt; } // Lan truyen hiep phuong sai cua mo hinh chuyen dong. Eigen::Matrix F = Eigen::Matrix::Identity(); F(kX, kYaw) = -d_trans * s; F(kY, kYaw) = d_trans * c; P = F * P * F.transpose(); P(kX, kX) += var_trans * c * c; P(kY, kY) += var_trans * s * s; P(kYaw, kYaw) += (var_theta_enc > 0.0 && var_theta_gyro > 0.0) ? 1.0 / (1.0 / var_theta_enc + 1.0 / var_theta_gyro) : var_theta_enc; impl_->last_predict = stamp; impl_->last_measurement = stamp; impl_->use_delta_motion = true; impl_->stats.odom_updates++; } void EkfSe2::UpdateImuYawRate(UnixTime stamp, double vyaw, double var) { Predict(stamp); std::lock_guard lk(impl_->mu); if (impl_->UpdateScalarLocked(kVyaw, vyaw, var)) { impl_->stats.imu_updates++; impl_->last_measurement = stamp; } } Pose2D EkfSe2::pose() const { std::lock_guard lk(impl_->mu); return Pose2D{impl_->x(kX), impl_->x(kY), impl_->x(kYaw)}; } double EkfSe2::vx() const { std::lock_guard lk(impl_->mu); return impl_->x(kVx); } double EkfSe2::vyaw() const { std::lock_guard lk(impl_->mu); return impl_->x(kVyaw); } Odometry EkfSe2::State(const std::string& odom_frame, const std::string& base_frame) const { std::lock_guard 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 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 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 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 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 lk(impl_->mu); return impl_->stats; } } // namespace vslam