当前位置: 首页 > news >正文

卡尔曼滤波

通过 Eigen 矩阵运算实现线性卡尔曼滤波。模拟的是一辆带火箭发动机的汽车,一开始沿着X轴匀速运动,到X轴中间开启火箭发动机匀加速向Y轴起飞。同理可以仿真(x,y,z,yaw,pitch,raw) 6自由度的真实飞行情况

#include <iostream>
#include <Eigen/LU>
#include <Eigen/core>using namespace Eigen;using Matrix6f = Eigen::Matrix<float, 6, 6>;
using Vector6f = Eigen::Matrix<float, 6, 1>;class CalmanFilter
{
public:CalmanFilter(){// 初始状态不确定度P << std::pow(0,2), 0, 0, 0, 0, 0, 0, std::pow(1,2), 0, 0, 0, 0,0, 0, std::pow(0.1,2), 0, 0, 0,0, 0, 0, std::pow(0,2), 0, 0,0, 0, 0, 0, std::pow(1,2), 0,0, 0, 0, 0, 0, std::pow(0.1, 2);// 环境不确定度Q << std::pow(0,2), 0, 0, 0, 0, 0, 0, std::pow(0,2), 0, 0, 0, 0,0, 0, std::pow(0,2), 0, 0, 0,0, 0, 0, std::pow(0,2), 0, 0,0, 0, 0, 0, std::pow(0,2), 0,0, 0, 0, 0, 0, std::pow(0,2);// 测量不确定度float xVariance = 5;float yVariance = 5;R << std::pow(xVariance,2), 0,0, std::pow(xVariance,2);// 隐变量到观测变量的映射H << 1, 0, 0, 0, 0, 0,0, 0, 0, 1, 0, 0;}void init(const Vector6f& x){this->x = x;this->fx = x;}Matrix6f getA(float dt){Matrix6f A;A << 1, dt, 0.5*dt*dt, 0, 0, 0, 0, 1, dt, 0, 0, 0,0, 0, 1, 0, 0, 0,0, 0, 0, 1, dt, 0.5*dt*dt,0, 0, 0, 0, 1, dt,0, 0, 0, 0, 0, 1;return A;}Matrix<float, 6, 2> getB(float dt){Matrix<float, 6, 2> B;B << 0.5*dt*dt, 0,dt, 0,0, 0,0, 0.5*dt*dt,0, dt,0, 0;return B;}void forcast(const Matrix6f& A, const Matrix<float, 6, 2>& B, const Vector2f& u){fx = A * x + B * u;fP = A * P * A.transpose() + Q;}void calibration(const Vector2f& z){Matrix<float, 6, 2> K = P * H.transpose() * (H * P * H.transpose() + R).inverse();x = fx + (K * (z - H * fx));P = fP - K * H * fP;}Vector2f getEstimation() const{return H * x;}private:Vector6f fx; // x, vx, ax, y, vy, ayMatrix6f fP; // fx 的协方差Matrix6f Q; // 环境干扰协方差Vector6f x; // fx 校准值Matrix6f P; // fP 校准值Matrix2f R; // 观测协方差Matrix<float, 2, 6> H; // 隐状态到观测状态的映射
};int main()
{// 测试数据float a = 1.1;std::vector<Vector2f> real;std::vector<Vector2f> observations;for (int i = 0; i < 100; ++i) {float x = i;float y = 0;if (i >= 50) {float t = (i - 50)/10.;y = 0.5 * a * t * t;}real.emplace_back(x, y);observations.emplace_back(x + rand() % 5, y + rand() % 5);}CalmanFilter filter;Vector6f initState;initState << observations[0][0], 0, 0, observations[0][1], 0, 0;filter.init(initState);float dt = 0.1;Matrix6f A =  filter.getA(dt);Matrix<float, 6, 2> B = filter.getB(dt);Vector2f u;u << 0, 0;std::vector<Vector2f> estimations;for (int i = 0; i < observations.size(); ++i) {if (i >= 50)u[1] = a;filter.forcast(A, B, u);Vector2f d;filter.calibration(observations[i]);Vector2f est = filter.getEstimation();estimations.push_back(est);}for (int i = 0; i < estimations.size(); ++i) {std::cout << i << " est: " << estimations[i] << " real: " << real[i] << " obs: " << observations[i] << std::endl;}
}

http://www.lryc.cn/news/246146.html

相关文章:

  • 不受平台限制,Sketch 网页版震撼登场
  • 如何使用.pth训练模型
  • C++11线程以及线程同步
  • 深度学习之基于YoloV3杂草识别系统
  • Linux 命令vim(编辑器)
  • 轻松配置PPPoE连接:路由器设置和步骤详解
  • 电源控制系统架构(PCSA)之系统分区电源域
  • Linux:docker基础操作(3)
  • 【Axure教程】用中继器制作卡片多条件搜索效果
  • Linux中vi常用命令-批量替换
  • logback-spring.xml的内容格式
  • nodejs+vue+elementui+express青少年编程课程在线考试系统
  • Navicat 技术指引 | GaussDB 数据查看器
  • Docker的registry
  • 【vue_3】关于超链接的问题
  • redis优化秒杀和消息队列
  • arm-eabi-gcc 和 arm-none-eabi-gcc 都是基于 GCC 的交叉编译器
  • 《大话设计模式》(持续更新中)
  • 人工智能原理复习--绪论
  • [网络] 字节一面~ 2. HTTP 2 与 HTTP 1.x 有什么区别
  • 自己动手实现一个深度学习算法——八、深度学习
  • js闭包的必要条件及创建和消失(生命周期)
  • 鸿蒙开发-ArkTS 语言-基础语法
  • GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF
  • 电子学会C/C++编程等级考试2022年03月(三级)真题解析
  • 理解 JUnit, JaCoCo 到 SonarQube 的过程及 Maven 配置
  • 人工智能关键技术决定机器人产业的前途
  • 2023华为ICT网络初赛试题回顾
  • Hands-on Machine Learning with Scikit-Learn,Keras TensorFlow
  • 242. 有效的字母异位词