金牌会员
- 积分
- 1549
- 威望
- 781
- 贡献
- 396
- 兑换币
- 89
- 注册时间
- 2012-5-26
- 在线时间
- 236 小时
|
void Kalman_Filter(void)
{
static float P[2][2] = {{ 0.005, 0.005}, { 0.005, 0.005 }};//误差相关矩阵
static float q_bias=0; //陀螺仪偏差
static const float dt = 0.005; //dt的取值为kalman滤波器采样时间
static float A[2][2]={{1, -0.005 }, {0,1}} ,a[2][2]={{1,0}, {-0.005,1}} ;//a为A的转置
static float angle_err;
static float X[2][2]={{0,0}, {0,0}} ,Y[2][2]={{0,0}, {0,0}};
static float PHt_0=0,PHt_1=0;
static float En=0,t_0=0,t_1=0;
float h_0 = 1.0;
int i,j,k;
g_fgyroangle=g_fGyroSpeed*dt;
g_fCarAngle=g_fCarAngle-q_bias * dt; //X(K|K-1)=A*X(K-1|K-1)
for(i=0;i<2;i++) //P(K|K-1)=A*P(K-1|K-1)*A'+Q
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
X[i][j]+=A[i][k]*P[k][j] ;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
Y[i][j]+=X[i][k]*a[k][j] ;
}
}
P[0][0] =Y[0][0]+ Q_angle;
P[0][1]= Y[0][1] ;
P[1][0]=Y[1][0];
P[1][1] =Y[1][1]+Q_gyro ;
//Y矩阵清零 ,X矩阵清零
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
Y[i][j]=0;
X[i][j]=0;
}
}
angle_err = g_fGravityAngle - g_fCarAngle;
PHt_0 = h_0*P[0][0];
PHt_1 = h_0*P[1][0];
En = R_angle + h_0 * PHt_0;
K_0 = PHt_0 / En;
K_1 = PHt_1 / En;
t_0 = PHt_0;
t_1 = h_0 * P[0][1];
P[0][0] -= K_0 * t_0;
P[0][1] -= K_0 * t_1;
P[1][0] -= K_1 * t_0;
P[1][1] -= K_1 * t_1;
g_fCarAngle += K_0 * angle_err;
q_bias += K_1 * angle_err;
g_fangle_dot = g_fGyroSpeed-q_bias;
}
|
|