智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 6743|回复: 13
打印 上一主题 下一主题

[软件类] 之前上传的卡尔曼滤波代码的真正出处

[复制链接]

10

主题

53

帖子

0

精华

高级会员

Rank: 4

积分
818
威望
559
贡献
145
兑换币
79
注册时间
2011-7-11
在线时间
57 小时
跳转到指定楼层
1#
发表于 2012-2-10 19:45:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/***************************************************************************/
/* 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(hudson@rotomotion.com)                      */  
/* -------------------------------                                         */
/* Compilation  procedure:                                                 */
/*       Linux                                                             */
/*      gcc68 -c  XXXXXX.c (to create object file)                         */
/*      gcc68 -o  XXXXXX.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,                   */
/* MA  02111-1307  USA                                                     */
/***************************************************************************/
#include <math.h>
#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 P[2][2] = {{ 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=E[vvT]
* 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 ]
*       [ 0  0 ]
*/
void stateUpdate(const float q_m){
float q;
float Pdot[4];
/* Unbias our gyro */
q = q_m - q_bias;//当前角速度:测量值-估计值
/*
  * Compute the derivative of the covariance matrix
  * (equation 22-1)
  * Pdot = A*P + P*A' + Q
  *
  */
Pdot[0] = Q_angle - P[0][1] - P[1][0]; /* 0,0 */
Pdot[1] = - P[1][1];          /* 0,1 */
Pdot[2] = - P[1][1];           /* 1,0 */
Pdot[3] = 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 */
P[0][0] += Pdot[0] * dt;
P[0][1] += Pdot[1] * dt;
P[1][0] += Pdot[2] * dt;
P[1][1] += Pdot[3] * 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 don't actually use this, so
  * we comment it out.
  */
float h_0 = 1;
/* const float  h_1 = 0; */
/*
  * Precompute PH' as the term is used twice
  * Note that H[0,1] = h_1 is zero, so that term is not not computed
  */
const float PHt_0 = h_0*P[0][0]; /* + h_1*P[0][1] = 0*/
const float PHt_1 = h_0*P[1][0]; /* + h_1*P[1][1] = 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 * P[0][0]*/
float Y_1 = h_0 * P[0][1];
  
P[0][0] -= K_0 * Y_0;
P[0][1] -= K_0 * Y_1;
P[1][0] -= K_1 * Y_0;
P[1][1] -= 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;
}

29

主题

700

帖子

0

精华

跨届大侠

Rank: 10Rank: 10Rank: 10

积分
6749

论坛元老奖章热心会员奖章在线王奖章优秀会员奖章活跃会员奖章

QQ
威望
2529
贡献
1556
兑换币
665
注册时间
2011-1-10
在线时间
1332 小时
2#
发表于 2012-2-10 20:49:02 | 只看该作者
谢谢!
回复 支持 反对

使用道具 举报

5

主题

246

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1511

热心会员奖章推广达人奖章

威望
927
贡献
298
兑换币
14
注册时间
2010-9-18
在线时间
143 小时
3#
发表于 2012-2-10 21:08:47 | 只看该作者
好贴
回复 支持 反对

使用道具 举报

5

主题

17

帖子

0

精华

高级会员

Rank: 4

积分
613
威望
449
贡献
54
兑换币
0
注册时间
2011-1-21
在线时间
55 小时
4#
发表于 2012-2-10 21:08:53 | 只看该作者
赞一个 这个代码的注释太全了
回复 支持 反对

使用道具 举报

6

主题

279

帖子

0

精华

高级会员

Rank: 4

积分
869
QQ
威望
676
贡献
107
兑换币
0
注册时间
2011-10-18
在线时间
43 小时
5#
发表于 2012-2-10 21:34:41 | 只看该作者
呵呵
回复 支持 反对

使用道具 举报

27

主题

244

帖子

0

精华

跨届大侠

Rank: 10Rank: 10Rank: 10

积分
6556

论坛元老奖章热心会员奖章

QQ
威望
1277
贡献
4939
兑换币
188
注册时间
2011-12-31
在线时间
170 小时
6#
发表于 2012-2-11 10:28:42 | 只看该作者
/*
   * Compute the derivative of the covariance matrix
   * (equation 22-1)
   * Pdot = A*P + P*A' + Q
  *
   */
请问这一句中: Pdot = A*P + P*A' + Q 是不是有问题?
kalman中应该是Pdot=A*P*A'+Q
我用matlab仿真了,如果Pdot = A*P + P*A' + Q的话,bias波动很严重,不收敛
但Pdot=A*P*A'+Q的话,效果就好多了
请问大家用这个程序仿真有问题没?如果程序是对的,那为什么Pdot = A*P + P*A' + Q???请高手解释一下
回复 支持 反对

使用道具 举报

19

主题

667

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2730

热心会员奖章特殊贡献奖章论坛骨干奖章推广达人奖章在线王奖章活跃会员奖章优秀会员奖章

威望
1504
贡献
364
兑换币
29
注册时间
2012-2-4
在线时间
431 小时
毕业学校
外国语学校
7#
发表于 2012-2-11 11:16:32 | 只看该作者
论坛元老还真是好学啊!!厉害!!
回复 支持 反对

使用道具 举报

0

主题

1

帖子

0

精华

注册会员

Rank: 2

积分
63
威望
126
贡献
27
兑换币
0
注册时间
2012-2-23
在线时间
4 小时
毕业学校
沈大
8#
发表于 2012-2-23 14:31:00 | 只看该作者
6楼说的,就是我要说的,请大侠们指点
回复 支持 反对

使用道具 举报

2

主题

52

帖子

0

精华

高级会员

Rank: 4

积分
638
威望
400
贡献
86
兑换币
4
注册时间
2011-8-29
在线时间
76 小时
9#
发表于 2012-2-23 14:35:13 | 只看该作者
q_bias是什么呀,bias是什么呀。
回复 支持 反对

使用道具 举报

4

主题

32

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
394
威望
252
贡献
74
兑换币
0
注册时间
2012-3-8
在线时间
34 小时
毕业学校
南阳理工学院
10#
发表于 2012-3-15 12:20:06 | 只看该作者
学习中
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2025-5-4 04:27 , Processed in 0.209780 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表