robot_pose_ekf源码解读

原创

robot_pose_ekf源码解读

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/zhxue_11/article/details/83828877

写在前面:本文中对应的文件会用文件表示,对应的函数用粗体表示,对应的变量用斜体表示。会略过一些简单的以及注释写明白的部分。本文会假定读者已经已经对卡尔曼滤波器有一定的了解并且已经阅读过本文中将会使用的贝叶斯滤波器bfl包教程。鉴于本人水平有限难免会有疏漏,欢迎一起来交流,这是我的邮箱:zhxue146@foxmail.com

Let’s the fun begin().



以main函数开始:OdomEstimationNode的实例

odom_estimation_node.cpp文件中开始,这个文件是对整个传感器融合过程进行ROS包装,odom_estimation_node.cpp中将会使用到的函数和变量均定义在odom_estimation_node.h中。从其中的main函数开始运行。对类OdomEstimationNode实例my_filter_node

  OdomEstimationNode my_filter_node;

     
     
  • 1

OdomEstimation的实例

OdomEstimationNode实例化过程中声明了许多变量,比较重要的是它为系统、里程计、imu等声明了概率密度函数(简写为pdf)、模型,然后声明了滤波器为扩展卡尔曼滤波器(EKF)。

而重要的是第113行:

  OdomEstimation my_filter_;

     
     
  • 1

OdomEstimation声明在odom_estimation.h中,实例为my_filter_

创建系统模型和测量更新模型:OdomEstimation的构造函数

OdomEstimation的构造函数声明在odom_estimation.h中,公式在odom_estimation.cpp中。构造函数为各个传感器以及系统本身创造pdf以及模型。系统模型和传感器模型模型分为两种。其中系统模型(在概率机器人中也写做预测模型,以下统一为系统模型)中声明属于robot_pose_ekf包自身设定的概率密度函数NonLinearAnalyticConditionalGaussianOdo。它声明在nonlinearanalyticconditionalgaussianodo.h,表达式在nonlinearanalyticconditionalgaussianodo.cpp,构造函数如下所示:

  #define NUMCONDARGUMENTS_MOBILE 2
  // 继承自AnalyticConditionalGaussianAdditiveNoise,两个参数:第一个是增加的噪音,第二个是条件参数的数目(本例中为2)
  NonLinearAnalyticConditionalGaussianOdo::NonLinearAnalyticConditionalGaussianOdo(const Gaussian& additiveNoise)
    : AnalyticConditionalGaussianAdditiveNoise(additiveNoise,NUMCONDARGUMENTS_MOBILE),
      df(6,6)
    {
      // 初始化导数矩阵
      for (unsigned int i=1; i<=6; i++){
        for (unsigned int j=1; j<=6; j++){
	  if (i==j) df(i,j) = 1;
	  else df(i,j) = 0;
        }
      }
    }

     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

回到odom_estimation.h中,系统模型的声明如下:

    // 创造系统模型:
    // 初始化系统模型噪音的均值和协方差矩阵
    ColumnVector sysNoise_Mu(6);  sysNoise_Mu = 0;
    SymmetricMatrix sysNoise_Cov(6); sysNoise_Cov = 0;
    for (unsigned int i=1; i<=6; i++) sysNoise_Cov(i,i) = pow(1000.0,2);
    // 采用均值和协方差矩阵生成代表系统不确定性的高斯分布
    Gaussian system_Uncertainty(sysNoise_Mu, sysNoise_Cov);
    // 利用高斯分布生成系统噪音的概率密度函数(pdf),根据pdf创建带有高斯不确定度的系统模型。
    sys_pdf_   = new NonLinearAnalyticConditionalGaussianOdo(system_Uncertainty);
    sys_model_ = new AnalyticSystemModelGaussianUncertainty(sys_pdf_);

     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

初始化系统模型之后,剩下是传感器的测量更新模型的初始化。内容都大同小异,只以里程计odom的测量更新模型为例。测量更新模型都是线性模型,和BFL包教程中的第一个例子完全一样,代码如下:

    // 创造里程计测量更新模型
    // 初始化代表测量噪音的高斯分布
    ColumnVector measNoiseOdom_Mu(6);  measNoiseOdom_Mu = 0;
    SymmetricMatrix measNoiseOdom_Cov(6);  measNoiseOdom_Cov = 0;
    for (unsigned int i=1; i<=6; i++) measNoiseOdom_Cov(i,i) = 1;
    Gaussian measurement_Uncertainty_Odom(measNoiseOdom_Mu, measNoiseOdom_Cov);
    // 初始化代表测量更新线性模型本身的高斯分布
    Matrix Hodom(6,6);  Hodom = 0;
    Hodom(1,1) = 1;    Hodom(2,2) = 1;    Hodom(6,6) = 1;
    // 用线性模型和噪音生成pdf以及测量更新模型
    odom_meas_pdf_   = new LinearAnalyticConditionalGaussian(Hodom, measurement_Uncertainty_Odom);
    odom_meas_model_ = new LinearAnalyticMeasurementModelGaussianUncertainty(odom_meas_pdf_);

     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

数据读取:OdomEstimationNode的构造函数

模型的创建终于结束了,回到odom_estimation_node.cpp运行OdomEstimationNode的构造函数,这一部分就比较简单了。读取将要使用哪些传感器、更新的频率、tf树转换等。到第101行比较重要,但是先不展开说,只是摘录如下:

    timer_ = nh_private.createTimer(ros::Duration(1.0/max(freq,1.0)), &OdomEstimationNode::spin, this);

     
     
  • 1

广播(advertise)要发布的话题,记录滤波开始的时间,

传感器数据读取与滤波器坐标转换:odomCallback与imucallback

如果使用了某个传感器就订阅该传感器的话题并执行对应的callback函数。仍然以里程计为例,其订阅代码如下:

    // 订阅里程计消息
    if (odom_used_){
      ROS_DEBUG("Odom sensor can be used");
      odom_sub_ = nh.subscribe("odom", 10, &OdomEstimationNode::odomCallback, this);
    }
    else ROS_DEBUG("Odom sensor will NOT be used");

     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

对应的callback函数还挺重要的,因为读取的过程中已经为滤波器收集数据了,其代码如下:

  // odom的callback函数
  void OdomEstimationNode::odomCallback(const OdomConstPtr& odom)
  {
    odom_callback_counter_++;
<span class="token function">ROS_DEBUG</span><span class="token punctuation">(</span><span class="token string">"Odom callback at time %f "</span><span class="token punctuation">,</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span>Time<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">assert</span><span class="token punctuation">(</span>odom_used_<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 接收数据</span>
odom_stamp_ <span class="token operator">=</span> odom<span class="token operator">-&gt;</span>header<span class="token punctuation">.</span>stamp<span class="token punctuation">;</span>
odom_time_  <span class="token operator">=</span> Time<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Quaternion q<span class="token punctuation">;</span>
<span class="token comment">// 将四元数msg(消息)转换为四元数</span>
tf<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">quaternionMsgToTF</span><span class="token punctuation">(</span>odom<span class="token operator">-&gt;</span>pose<span class="token punctuation">.</span>pose<span class="token punctuation">.</span>orientation<span class="token punctuation">,</span> q<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// odom的转换,从里程计坐标系到base_footprint转换,q代表坐标系的旋转,Vector3代表坐标系的平移。</span>
odom_meas_  <span class="token operator">=</span> <span class="token function">Transform</span><span class="token punctuation">(</span>q<span class="token punctuation">,</span> <span class="token function">Vector3</span><span class="token punctuation">(</span>odom<span class="token operator">-&gt;</span>pose<span class="token punctuation">.</span>pose<span class="token punctuation">.</span>position<span class="token punctuation">.</span>x<span class="token punctuation">,</span> odom<span class="token operator">-&gt;</span>pose<span class="token punctuation">.</span>pose<span class="token punctuation">.</span>position<span class="token punctuation">.</span>y<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 读取协方差矩阵</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">unsigned</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">6</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">unsigned</span> <span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> j<span class="token operator">&lt;</span><span class="token number">6</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token function">odom_covariance_</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span> j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> odom<span class="token operator">-&gt;</span>pose<span class="token punctuation">.</span>covariance<span class="token punctuation">[</span><span class="token number">6</span><span class="token operator">*</span>i<span class="token operator">+</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">//给滤波器增加一个测量更新,这个函数接下来会说。因为这里需要的是从base_footprint_frame_--&gt;wheelodom的转换,所以前面的到的odom_meas要反转。</span>
my_filter_<span class="token punctuation">.</span><span class="token function">addMeasurement</span><span class="token punctuation">(</span><span class="token function">StampedTransform</span><span class="token punctuation">(</span>odom_meas_<span class="token punctuation">.</span><span class="token function">inverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> 
odom_stamp_<span class="token punctuation">,</span> base_footprint_frame_<span class="token punctuation">,</span> <span class="token string">"wheelodom"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> odom_covariance_<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 启动odom,初始化并检查odom是否一直在激活状态</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>odom_active_<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>odom_initializing_<span class="token punctuation">)</span><span class="token punctuation">{</span>
odom_initializing_ <span class="token operator">=</span> true<span class="token punctuation">;</span>
odom_init_stamp_ <span class="token operator">=</span> odom_stamp_<span class="token punctuation">;</span>
<span class="token function">ROS_INFO</span><span class="token punctuation">(</span><span class="token string">"Initializing Odom sensor"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      
  <span class="token punctuation">}</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span> filter_stamp_ <span class="token operator">&gt;=</span> odom_init_stamp_<span class="token punctuation">)</span><span class="token punctuation">{</span>
odom_active_ <span class="token operator">=</span> true<span class="token punctuation">;</span>
odom_initializing_ <span class="token operator">=</span> false<span class="token punctuation">;</span>
<span class="token function">ROS_INFO</span><span class="token punctuation">(</span><span class="token string">"Odom sensor activated"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      
  <span class="token punctuation">}</span>
  <span class="token keyword">else</span> <span class="token function">ROS_DEBUG</span><span class="token punctuation">(</span><span class="token string">"Waiting to activate Odom, because Odom measurements are still %f sec in the future."</span><span class="token punctuation">,</span> 
	    <span class="token punctuation">(</span>odom_init_stamp_ <span class="token operator">-</span> filter_stamp_<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>debug_<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token comment">// write to file</span>
  <span class="token keyword">double</span> tmp<span class="token punctuation">,</span> yaw<span class="token punctuation">;</span>
  odom_meas_<span class="token punctuation">.</span><span class="token function">getBasis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getEulerYPR</span><span class="token punctuation">(</span>yaw<span class="token punctuation">,</span> tmp<span class="token punctuation">,</span> tmp<span class="token punctuation">)</span><span class="token punctuation">;</span>
  odom_file_<span class="token operator">&lt;&lt;</span> fixed <span class="token operator">&lt;&lt;</span><span class="token function">setprecision</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span>Time<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> <span class="token string">" "</span> <span class="token operator">&lt;&lt;</span> odom_meas_<span class="token punctuation">.</span><span class="token function">getOrigin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">x</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> <span class="token string">" "</span> <span class="token operator">&lt;&lt;</span> odom_meas_<span class="token punctuation">.</span><span class="token function">getOrigin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">y</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> <span class="token string">"  "</span> <span class="token operator">&lt;&lt;</span> yaw <span class="token operator">&lt;&lt;</span> <span class="token string">"  "</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

接下来是关于:

my_filter_.addMeasurement(StampedTransform(odom_meas_.inverse(), 
    odom_stamp_, base_footprint_frame_, "wheelodom"), odom_covariance_);

 
 
  • 1
  • 2

该函数被定义在odom_estimation.cpp第332行,定义如下:

  void OdomEstimation::addMeasurement(const StampedTransform& meas, const MatrixWrapper::SymmetricMatrix& covar)
  {
    // 检查协方差矩阵
    for (unsigned int i=0; i<covar.rows(); i++){
      if (covar(i+1,i+1) == 0){
        ROS_ERROR("Covariance specified for measurement on topic %s is zero", meas.child_frame_id_.c_str());
        return;
      }
    }
    // 增加测量,定义在odom_estimation.cpp第322行,实际上就是把对应的转换记录下来
    addMeasurement(meas);
    if (meas.child_frame_id_ == "wheelodom") odom_covariance_ = covar;
    else if (meas.child_frame_id_ == "imu")  imu_covariance_  = covar;
    else if (meas.child_frame_id_ == "vo")   vo_covariance_   = covar;
    else if (meas.child_frame_id_ == "gps")  gps_covariance_  = covar;
    else ROS_ERROR("Adding a measurement for an unknown sensor %s", meas.child_frame_id_.c_str());
  };

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

imucallback与odomcallback略有不同,主要体现在imu转换到base_footprint的tf直接在机器人状态中寻找。


滤波器更新

滤波器初始化:OdomEstimationNode::spin其①

终于回到第101行了,重新抄写如下:

    timer_ = nh_private.createTimer(ros::Duration(1.0/max(freq,1.0)), &OdomEstimationNode::spin, this);

 
 
  • 1

开始都是一些参数检测,都很明白,我就不赘述。

因为这是我们第一次调用这个函数,当然没有对滤波器进行初始化,所以427行至457行这次loop也跳过。

因为本文使用的是imu和里程计融合,所以直接以这句初始化滤波器:

      else if ( odom_active_  && !gps_used_ && !my_filter_.isInitialized()){
        my_filter_.initialize(odom_meas_, odom_stamp_);
        ROS_INFO("Kalman filter initialized with odom measurement");
      }

 
 
  • 1
  • 2
  • 3
  • 4

my_filter_.initialize定义在odom_estimation.cpp中,代码如下:

  // 初始化滤波器的概率
  void OdomEstimation::initialize(const Transform& prior, const Time& time)
  {
    // 设定先验分布的均值prior_Mu和协方差矩阵prior_Cov
    ColumnVector prior_Mu(6); 
    // 将TF树分解至xyz以及偏航、俯仰、滚转(YPR)
    decomposeTransform(prior, prior_Mu(1), prior_Mu(2), prior_Mu(3), prior_Mu(4), prior_Mu(5), prior_Mu(6));
    SymmetricMatrix prior_Cov(6); 
    for (unsigned int i=1; i<=6; i++) {
      for (unsigned int j=1; j<=6; j++){
	if (i==j)  prior_Cov(i,j) = pow(0.001,2);
	else prior_Cov(i,j) = 0;
      }
    }
    // 根据以上建立一个新的高斯分布,再用这个高斯分布建立新的扩展卡尔曼滤波器
    prior_  = new Gaussian(prior_Mu,prior_Cov);
    filter_ = new ExtendedKalmanFilter(prior_);
<span class="token comment">// 记录初始值</span>
<span class="token function">addMeasurement</span><span class="token punctuation">(</span><span class="token function">StampedTransform</span><span class="token punctuation">(</span>prior<span class="token punctuation">,</span> time<span class="token punctuation">,</span> output_frame_<span class="token punctuation">,</span> base_footprint_frame_<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
filter_estimate_old_vec_ <span class="token operator">=</span> prior_Mu<span class="token punctuation">;</span>
filter_estimate_old_ <span class="token operator">=</span> prior<span class="token punctuation">;</span>
filter_time_old_     <span class="token operator">=</span> time<span class="token punctuation">;</span>

<span class="token comment">// 给滤波器初始化的flag设定为true</span>
filter_initialized_ <span class="token operator">=</span> true<span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

至此滤波器初始化完成

滤波器更新与消息发布:OdomEstimationNode::spin其②

上文中为第一次spin,完成了滤波器初始化,之后的每次调用spin都会更新滤波器同时发布消息。

首先仍然是一些传感器检查,检查传感器是否仍然在工作。之后正式开始更新传感器与发布消息:

      // 只有当滤波器已经初始化才开始更新滤波器
      if ( my_filter_.isInitialized() )  {
        bool diagnostics = true;
        // 这一句if中实际更新滤波器,也是核心语句,接下来会分析
        if (my_filter_.update(odom_active_, imu_active_,gps_active_, vo_active_,  filter_stamp_, diagnostics)){
      <span class="token comment">// 发布最近的估计与协方差矩阵</span>
      my_filter_<span class="token punctuation">.</span><span class="token function">getEstimate</span><span class="token punctuation">(</span>output_<span class="token punctuation">)</span><span class="token punctuation">;</span>
      pose_pub_<span class="token punctuation">.</span><span class="token function">publish</span><span class="token punctuation">(</span>output_<span class="token punctuation">)</span><span class="token punctuation">;</span>
      ekf_sent_counter_<span class="token operator">++</span><span class="token punctuation">;</span>
      
      <span class="token comment">// 广播最近的变换</span>
      StampedTransform tmp<span class="token punctuation">;</span>
      my_filter_<span class="token punctuation">.</span><span class="token function">getEstimate</span><span class="token punctuation">(</span>ros<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">Time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> tmp<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vo_active_ <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>gps_active_<span class="token punctuation">)</span>
        tmp<span class="token punctuation">.</span><span class="token function">getOrigin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setZ</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      odom_broadcaster_<span class="token punctuation">.</span><span class="token function">sendTransform</span><span class="token punctuation">(</span><span class="token function">StampedTransform</span><span class="token punctuation">(</span>tmp<span class="token punctuation">,</span> tmp<span class="token punctuation">.</span>stamp_<span class="token punctuation">,</span> output_frame_<span class="token punctuation">,</span> base_footprint_frame_<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      
      <span class="token keyword">if</span> <span class="token punctuation">(</span>debug_<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// write to file</span>
        ColumnVector estimate<span class="token punctuation">;</span> 
        my_filter_<span class="token punctuation">.</span><span class="token function">getEstimate</span><span class="token punctuation">(</span>estimate<span class="token punctuation">)</span><span class="token punctuation">;</span>
        corr_file_ <span class="token operator">&lt;&lt;</span> fixed <span class="token operator">&lt;&lt;</span> <span class="token function">setprecision</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span>ros<span class="token punctuation">:</span><span class="token punctuation">:</span>Time<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span><span class="token string">" "</span><span class="token punctuation">;</span>
        
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">unsigned</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">&lt;=</span><span class="token number">6</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        corr_file_ <span class="token operator">&lt;&lt;</span> <span class="token function">estimate</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> <span class="token string">" "</span><span class="token punctuation">;</span>
        corr_file_ <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>self_diagnose_ <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>diagnostics<span class="token punctuation">)</span>
      <span class="token function">ROS_WARN</span><span class="token punctuation">(</span><span class="token string">"Robot pose ekf diagnostics discovered a potential problem"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

my_filter_.update定义在odom_estimation.cpp第159-320行。第161行-第174行是一些参数检查,之后开始才真正的测量更新。因为各个传感器测量更新内容类似,这里只以里程计odom举例,其代码如下:

    // 开始滤波器更新
    // --------------------
    // 首先只加上系统噪音
    ColumnVector vel_desi(2); vel_desi = 0;
    filter_->Update(sys_model_, vel_desi);
<span class="token comment">// 开始里程计测量更新</span>
<span class="token comment">// ------------------------</span>
<span class="token function">ROS_DEBUG</span><span class="token punctuation">(</span><span class="token string">"Process odom meas"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>odom_active<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>transformer_<span class="token punctuation">.</span><span class="token function">canTransform</span><span class="token punctuation">(</span>base_footprint_frame_<span class="token punctuation">,</span><span class="token string">"wheelodom"</span><span class="token punctuation">,</span> filter_time<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">ROS_ERROR</span><span class="token punctuation">(</span><span class="token string">"filter time older than odom message buffer"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> false<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  transformer_<span class="token punctuation">.</span><span class="token function">lookupTransform</span><span class="token punctuation">(</span><span class="token string">"wheelodom"</span><span class="token punctuation">,</span> base_footprint_frame_<span class="token punctuation">,</span> filter_time<span class="token punctuation">,</span> odom_meas_<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>odom_initialized_<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token comment">// 将绝对里程测量值转换至水平平面内的相对里程计测量值(坐标转换tf仍然由旋转和平移构成)</span>
Transform odom_rel_frame <span class="token operator">=</span>  <span class="token function">Transform</span><span class="token punctuation">(</span>tf<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">createQuaternionFromYaw</span><span class="token punctuation">(</span><span class="token function">filter_estimate_old_vec_</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> 
				      filter_estimate_old_<span class="token punctuation">.</span><span class="token function">getOrigin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> odom_meas_old_<span class="token punctuation">.</span><span class="token function">inverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> odom_meas_<span class="token punctuation">;</span>
ColumnVector <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">// 将转换分解到x,y,z,Rx,Ry,Rz</span>
<span class="token function">decomposeTransform</span><span class="token punctuation">(</span>odom_rel_frame<span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 角度溢出的话就进行修正</span>
<span class="token function">angleOverflowCorrect</span><span class="token punctuation">(</span><span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">filter_estimate_old_vec_</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 更新滤波器</span>
<span class="token comment">// 更新协方差矩阵</span>
odom_meas_pdf_<span class="token operator">-&gt;</span><span class="token function">AdditiveNoiseSigmaSet</span><span class="token punctuation">(</span>odom_covariance_ <span class="token operator">*</span> <span class="token function">pow</span><span class="token punctuation">(</span>dt<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">ROS_DEBUG</span><span class="token punctuation">(</span><span class="token string">"Update filter with odom measurement %f %f %f %f %f %f"</span><span class="token punctuation">,</span> 
              <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 给里程计系统模型更新这次的测量值</span>
filter_<span class="token operator">-&gt;</span><span class="token function">Update</span><span class="token punctuation">(</span>odom_meas_model_<span class="token punctuation">,</span> odom_rel<span class="token punctuation">)</span><span class="token punctuation">;</span>
diagnostics_odom_rot_rel_ <span class="token operator">=</span> <span class="token function">odom_rel</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">else</span><span class="token punctuation">{</span>
odom_initialized_ <span class="token operator">=</span> true<span class="token punctuation">;</span>
diagnostics_odom_rot_rel_ <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  odom_meas_old_ <span class="token operator">=</span> odom_meas_<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 传感器未初始化</span>
<span class="token keyword">else</span> odom_initialized_ <span class="token operator">=</span> false<span class="token punctuation">;</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

结束

由此,整个robot_pose_ekf包源码解读就结束了。

这里还得重新强调一下,如果看到任何问题或者错误,欢迎与我练习~

如果感觉对整个过程还是有点糊涂的话,这里给出我理解的流程图:

Created with Raphaël 2.2.0 创建系统和测量更新模型 初始化滤波器 传感器 仍然 有效吗? 更新滤波器 发布消息和变化 等待传感器 yes no
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e9f16cbbc2.css" rel="stylesheet">
                </div>
</article>
<div class="postTime"> 
    <div class="article-bar-bottom">
        <span class="time">
            文章最后发布于: 2018-11-10 12:01:24            </span>
    </div>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值