卡尔曼滤波算法代码总结(20150311)

上传人:仙*** 文档编号:102435827 上传时间:2022-06-06 格式:DOC 页数:6 大小:100.50KB
收藏 版权申诉 举报 下载
卡尔曼滤波算法代码总结(20150311)_第1页
第1页 / 共6页
卡尔曼滤波算法代码总结(20150311)_第2页
第2页 / 共6页
卡尔曼滤波算法代码总结(20150311)_第3页
第3页 / 共6页
资源描述:

《卡尔曼滤波算法代码总结(20150311)》由会员分享,可在线阅读,更多相关《卡尔曼滤波算法代码总结(20150311)(6页珍藏版)》请在装配图网上搜索。

1、/*/* 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 */* - */* pilationprocedure: */* Linux */* gcc68 -c#.c */* gcc68 -o#.hex #.o ppwa.o */*Upload data : */* ul filename.txt */*/* I

2、n 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 any later version. */* */* this code is distributed in the hope that

3、 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;

4、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 cova

5、riance 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 puting* the angle, we also have an unbiased angular rate available.These are* read-only

6、 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 ;/*

7、 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

8、 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 stateUpdatefloat q;float Pdot4;/* Unbias our gyro */q = q_m - q_bias;/当前角速度:测量值-估计值/

9、* pute the derivative of the covariance matrix* * 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* += * dt* += q * dt

10、*/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 b

11、e 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/* pute our measured angle and the error in ou

12、r 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*

13、 we ment it out.*/float h_0 = 1;/* const floath_1 = 0; */* Prepute PH as the term is used twice* Note that H0,1 = h_1 is zero, so that term is not not puted*/const float PHt_0 = h_0*P00; /* + h_1*P01 = 0*/const float PHt_1 = h_0*P10; /* + h_1*P11 = 0*/* pute the error estimate:* * E = H P H + R*/flo

14、at E = R_angle +;/* pute the Kalman filter gains:* * K = P H inv */float K_0 = PHt_0 / E;float K_1 = PHt_1 / E;/* Update covariance matrix:* * 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;/* Upd

15、ate 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; /现在智能小车上用的

16、卡尔曼滤波算法.由于做平衡小车,然后对那段滤波算法很疑惑,然后网上讲的又比较少,我看了一段时间的书.这是小弟的对这段卡尔曼滤波程序的一点理解,因为基础薄弱大二,有错的请多多包涵.先上程序,这是抄的不知道谁的代码.抱歉了.不过这程序好像都写的差不多void Kalman_FilterAngle+= * dt; Pdot0=Q_angle - PP01 - PP10; /Pdot1= - PP11;Pdot2= - PP11;/Pdot3=Q_gyro;PP00 += Pdot0 * dt; PP01 += Pdot1 * dt; PP10 += Pdot2 * dt;PP11 += Pdot3

17、* 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

18、;首先是卡尔曼滤波的5个方程X=A X+B U . /先验估计P=A P A+Q /协方差矩阵的预测Kg= P H / H P H + R /计算卡尔曼增益X= X+Kg Z-H X 通过卡尔曼增益进行修正P=I-Kg HP /跟新协方差阵5个式子比较抽象,现在直接用实例来说,对于角度来说,我们认为此时的角度可以近似认为是上一时刻的角度值加上上一时刻陀螺仪测得的角加速度值乘以时间,因为,角度微分等于时间的微分乘以角速度.但是陀螺仪有个静态漂移而且还是变化的,静态漂移就是静止了没有角速度然后陀螺仪也会输出一个值,这个值肯定是没有意义的,计算时要把它减去.由此我们得到了当前角度的预测值 Angle

19、Angle=Angle+ * dt; 其中等号左边Angle为此时的角度,等号右边Angle为上一时刻的角度,Gyro 为陀螺仪测的角速度的值,dt是两次滤波之间的时间间隔.float dt=0.005; 这是程序中的定义同时 Q_bias也是一个变化的量.但是就预测来说认为现在的漂移跟上一时刻是相同的即Q_bias=Q_bias将两个式子写成矩阵的形式得到上式,这个式子对应于卡尔曼滤波的第一个式子X=A X+B U . /先验估计X为2维列向量,A为2维方阵,X为2维列向量,B 为2维列向量,U 为Gyro二,这里是卡尔曼滤波的第二个式子接着是预测方差阵的预测值,这里首先要给出两个值,一个是

20、漂移的噪声,一个是角度值的噪声,所谓噪声就是数据的方差值P=A P A+Q 这里的Q为向量 的协方差矩阵,即因为漂移噪声还有角度噪声是相互独立的,则=0;=0又由性质可知covx,x=Dx即方差,所以得到的矩阵如下,这里的两个方差值是开始就给出的常数程序中的定义如下float Q_angle=0.001; float Q_gyro=0.003;接着是这一部分A P A,其中的Pk-1|P为上一时刻的预测方差阵卡尔曼滤波的目标就是要让这个预测方差阵最小.其中P设为,第一式已知A为则计算A P A+Q就是个矩阵乘法和加法,算算吧结果如下很小为了计算简便忽略不计.于是得到a,b,c,d分别和矩阵的P

21、00,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= P H / H P H + R /计算卡尔曼增益即计算卡尔曼增益,这是个二维向量设为,这里的 = 为由此kg=P+R,这里又有一个常数R,程序中的定义如下float R_angle=0.5;这个指的是角

22、度测量噪声值,则式子的分母=P00+R_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;四,用误差还有卡尔曼增益来修正X= X+Kg Z-H X 通过卡尔曼增益进行修正这个矩阵带进去就行了Zk=Accel.注意这个是加速度计算出来的角度Angle_err = Accel - Angle;对应程序如下Angle+= K_0 * Angle_err;Q_bias+= K_1 * Angle_err;同时为了PID控制还有下次

23、的使用把角速度算出来了Gyro_x = Gyro - Q_bias;五,最后一步对矩阵P进行更新,因为下一次滤波时要用到PP00 -= K_0 * t_0;PP01 -= K_0 * t_1;PP10 -= K_1 * t_0;PP11 -= K_1 * t_1;P=I-Kg HP /跟预测方差阵这个很简单,矩阵带进去算就行了六,总结卡尔曼滤波一共只需要给很少的初始值量,float Q_angle=0.001; float Q_gyro=0.003;还有float R_angle=0.5;以与系统的初始量angle还有Q_bias还有预测误差矩阵P,程序里给的是0数组理论上由于卡尔曼滤波是迭代的算法,当时间充分长以后.滤波估值将与初始值的选取无关.但是实际上并不是如此,比如测量方差值一直在变化.6 / 6

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!