卡尔曼滤波算法代码总结(20150311).doc_第1页
卡尔曼滤波算法代码总结(20150311).doc_第2页
卡尔曼滤波算法代码总结(20150311).doc_第3页
卡尔曼滤波算法代码总结(20150311).doc_第4页
卡尔曼滤波算法代码总结(20150311).doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*/* kalman.c */* 1-d kalman filter algorithm, using an inclinometer and gyro */* author: rich chi ooi */* version: 1.0 */* date:30.05.2003 */* adapted from trammel hudson() */* - */* compilationprocedure: */* linux */* gcc68 -cxxxxxx.c (to create object file) */* gcc68 -oxxxxxx.hex xxxxxx.o ppwa.o */*upload data : */* ul filename.txt */*/* in this version: */*/* this is a free software; you can redistribute it and/or modify */* it under the terms of the gnu general public license as published */* by the free software foundation; either version 2 of the license, */* or (at your option) any later version. */* */* this code is distributed in the hope that it will be useful, */* but without any warranty; without even the implied warranty of */* merchantability or fitness for a particular purpose.see the */* gnu general public license for more details. */* */* you should have received a copy of the gnu general public license */* along with autopilot; if not, write to the free software */* foundation, inc., 59 temple place, suite 330, boston, */* ma02111-1307usa */*/#include #include eyebot.h/* the state is updated with gyro rate measurement every 20ms* change this value if you update at a different rate.*/static const float dt = 0.02;/* the covariance matrix.this is updated at every time step to* determine how well the sensors are tracking the actual state.*/static float p22 = 1, 0 , 0, 1 ;/* our two states, the angle and the gyro bias.as a byproduct of computing* the angle, we also have an unbiased angular rate available.these are* read-only to the user of the module.*/float angle;float q_bias;float rate;/* the r represents the measurement covariance noise.r=evvt* in this case,it is a 1x1 matrix that says that we expect* 0.1 rad jitter from the inclinometer.* for a 1x1 matrix in this case v = 0.1*/static const float r_angle = 0.001 ;/* q is a 2x2 matrix that represents the process covariance noise.* in this case, it indicates how much we trust the inclinometer* relative to the gyros.*/static const float q_angle = 0.001;static const float q_gyro= 0.0015;/* state_update is called every dt with a biased gyro measurement* by the user of the module.it updates the current angle and* rate estimate.* the pitch gyro measurement should be scaled into real units, but* does not need any bias removal.the filter will track the bias.* a = 0 -1 * 00 */void stateupdate(const float q_m)float q;float pdot4;/* unbias our gyro */q = q_m - q_bias;/当前角速度:测量值-估计值/* compute the derivative of the covariance matrix* (equation 22-1)* pdot = a*p + p*a + q*/pdot0 = q_angle - p01 - p10; /* 0,0 */pdot1 = - p11; /* 0,1 */pdot2 = - p11; /* 1,0 */pdot3 = q_gyro; /* 1,1 */* store our unbias gyro estimate */rate = q;/* update our angle estimate* angle += angle_dot * dt* += (gyro - gyro_bias) * dt* += q * dt*/angle += q * dt;/角速度积分累加到 估计角度/* update the covariance matrix */p00 += pdot0 * dt;p01 += pdot1 * dt;p10 += pdot2 * dt;p11 += pdot3 * dt;/* kalman_update is called by a user of the module when a new* inclinoometer measurement is available.* this does not need to be called every time step, but can be if* the accelerometer data are available at the same rate as the* rate gyro measurement.* h= 1 0 * because the angle measurement directly corresponds to the angle* estimate and the angle measurement has no relation to the gyro* bias.*/void kalmanupdate(const float incangle)/* compute our measured angle and the error in our estimate */float angle_m = incangle;float angle_err = angle_m - angle;/1.12 zk-h*xk_dot/* h_0 shows how the state measurement directly relates to* the state estimate. * * h = h_0 h_1* the h_1 shows that the state measurement does not relate* to the gyro bias estimate.we dont actually use this, so* we comment it out.*/float h_0 = 1;/* const floath_1 = 0; */* precompute ph as the term is used twice* note that h0,1 = h_1 is zero, so that term is not not computed*/const float pht_0 = h_0*p00; /* + h_1*p01 = 0*/const float pht_1 = h_0*p10; /* + h_1*p11 = 0*/* compute the error estimate:* (equation 21-1)* e = h p h + r*/float e = r_angle +(h_0 * pht_0);/* compute the kalman filter gains:* (equation 21-2)* k = p h inv(e) */float k_0 = pht_0 / e;float k_1 = pht_1 / e;/* update covariance matrix:* (equation 21-3)* p = p - k h p * let* y = h p*/float y_0 = pht_0;/*h_0 * p00*/float y_1 = h_0 * p01;p00 -= k_0 * y_0;p01 -= k_0 * y_1;p10 -= k_1 * y_0;p11 -= k_1 * y_1;/* update our state estimate:* xnew = x + k * error* err is a measurement of the difference in the measured state* and the estimate state.in our case, it is just the difference* between the inclinometer measured angle and the estimated angle.*/angle += k_0 * angle_err;q_bias += k_1 * angle_err;/p-760946791.html /现在智能小车上用的卡尔曼滤波算法。由于做平衡小车,然后对那段滤波算法很疑惑,然后网上讲的又比较少,我看了一段时间的书。这是小弟的对这段卡尔曼滤波程序的一点理解,因为基础薄弱(大二),有错的请多多包涵。先上程序,这是抄的不知道谁的代码。抱歉了。不过这程序好像都写的差不多void kalman_filter(float gyro,float accel)angle+=(gyro - q_bias) * dt; pdot0=q_angle - pp01 - pp10; /pdot1= - pp11;pdot2= - pp11;/pdot3=q_gyro;pp00 += pdot0 * dt; pp01 += pdot1 * dt; pp10 += pdot2 * dt;pp11 += pdot3 * dt; angle_err = accel - angle;pct_0 = c_0 * pp00;pct_1 = c_0 * pp10;e = r_angle + c_0 * pct_0;k_0 = pct_0 / e;k_1 = pct_1 / e;t_0 = pct_0;t_1 = c_0 * pp01;pp00 -= k_0 * t_0;pp01 -= k_0 * t_1;pp10 -= k_1 * t_0;pp11 -= k_1 * t_1;angle+= k_0 * angle_err;q_bias+= k_1 * angle_err;gyro_x = gyro - q_bias;首先是卡尔曼滤波的5个方程x(k|k-1)=a x(k-1|k-1)+b u(k) . (1)/先验估计p(k|k-1)=a p(k-1|k-1) a+q (2)/协方差矩阵的预测kg(k)= p(k|k-1) h / (h p(k|k-1) h + r) (3)/计算卡尔曼增益x(k|k)= x(k|k-1)+kg(k) (z(k) - h x(k|k-1) (4)通过卡尔曼增益进行修正p(k|k)=(i-kg(k) h)p(k|k-1) (5)/跟新协方差阵5个式子比较抽象,现在直接用实例来说,对于角度来说,我们认为此时的角度可以近似认为是上一时刻的角度值加上上一时刻陀螺仪测得的角加速度值乘以时间,因为,角度微分等于时间的微分乘以角速度。但是陀螺仪有个静态漂移(而且还是变化的),静态漂移就是静止了没有角速度然后陀螺仪也会输出一个值,这个值肯定是没有意义的,计算时要把它减去。由此我们得到了当前角度的预测值 angleangle=angle+(gyro - q_bias) * dt; 其中等号左边angle为此时的角度,等号右边angle为上一时刻的角度,gyro 为陀螺仪测的角速度的值,dt是两次滤波之间的时间间隔。float dt=0.005; 这是程序中的定义同时 q_bias也是一个变化的量。但是就预测来说认为现在的漂移跟上一时刻是相同的即q_bias=q_bias将两个式子写成矩阵的形式得到上式,这个式子对应于卡尔曼滤波的第一个式子x(k|k-1)=a x(k-1|k-1)+b u(k) . (1)/先验估计x(k|k-1)为2维列向量,a为2维方阵,x(k-1|k-1)为2维列向量,b 为2维列向量,u(k) 为gyro二,这里是卡尔曼滤波的第二个式子接着是预测方差阵的预测值,这里首先要给出两个值,一个是漂移的噪声,一个是角度值的噪声,(所谓噪声就是数据的方差值)p(k|k-1)=a p(k-1|k-1) a+q 这里的q为向量 的协方差矩阵,即因为漂移噪声还有角度噪声是相互独立的,则=0;=0又由性质可知cov(x,x)=d(x)即方差,所以得到的矩阵如下,这里的两个方差值是开始就给出的常数程序中的定义如下float q_angle=0.001; float q_gyro=0.003;接着是这一部分a p(k-1|k-1) a,其中的(p(k-1)|p(k-1))为上一时刻的预测方差阵卡尔曼滤波的目标就是要让这个预测方差阵最小。其中p(k-1|k-1)设为,第一式已知a为则计算a p(k-1|k-1) a+q(就是个矩阵乘法和加法,算算吧)结果如下很小为了计算简便忽略不计。于是得到a,b,c,d分别和矩阵的p00,p01,p10,p11计算过程转化为如下程序,代换即可 pdot0=q_angle - pp01 - pp10; pdot1= - pp11;pdot2= - pp11;pdot3=q_gyro;pp00 += pdot0 * dt; pp01 += pdot1 * dt; pp10 += pdot2 * dt;pp11 += pdot3 * dt; 三,这里是卡尔曼滤波的第三个式子 kg(k)= p(k|k-1) h / (h p(k|k-1) h + r) (3)/计算卡尔曼增益即计算卡尔曼增益,这是个二维向量设为,这里的 = 为由此kg=p(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论