Projet

Général

Profil

« Précédent | Suivant » 

Révision 352

Ajouté par annoyau il y a presque 4 ans

Filtre de Kalman fini

Voir les différences:

sp4a3_kalman.c
void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
int i,j;
for (i=0;i<na;i++)
{
for (j=0;j<ma;j++)
{
out [i][j] = A[i][j] + b[i][j];
}
}
}
void Init_Mat(int na,int ma,double A[na][ma], double out[na][ma]){
int i=0, j=0;
for (i=0; i<na; i++)
for (j=0; j<ma; j++){
out[i][j]=A[i][j];
}
}
}
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
int i,j;
double det=0;
double B1[n][m];
det = A[0][0]*A[1][1] - A[0][1]*A[1][0];
B1[0][0] = A[1][1];
B1[0][1] = A[0][1]*(-1);
B1[1][0] = A[1][0]*(-1);
B1[1][1] = A[0][0];
if (det !=0){
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
B[i][j]= (1/det)*B1[i][j];
}
}
}
}
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
......
}
void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
int i,j;
for (i=0;i<na;i++)
{
for (j=0;j<ma;j++)
{
out [i][j] = A[i][j] - b[i][j];
}
}
}
void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
int i,j,k,i1,j1;
double somme[4][4];
for (i1=0;i1<na;i1++)
{
for (j1=0;j1<mb;j1++)
{
somme[i1][j1]=0;
}
}
for (i=0;i<na;i++)
{
for (j=0;j<mb;j++)
{
for (k=0;k<nb;k++)
{
somme[i][j] += A[i][k]*B[k][j];
out[i][j] = somme [i][j];
}
}
}
}
......
{
t -= t0;x -= x0;y -= y0;
debug=0; ///Mettre à 1 pour afficher les matrices.
debug=1; ///Mettre à 1 pour afficher les matrices.
///Ajouter votre code ci-dessous///
// Kalman
// X = F*X
Plot_Mat(X," X(k+1|k) = ");
// X = F*X
double matx[4][1];
Init_Mat(4, 1, X, matx);
Mul_Mat_Mat(4, 4, F, 4, 1, matx, X);
Plot_Mat(X," X(k+1|k) = ");
//P = F*P*F'+Q;
double out[4][4], out1[4][4];
Mul_Mat_Mat(4, 4, F, 4, 4, P, out);
Mul_Mat_Mat(4, 4, out, 4, 4, FT, out1);
Add_Mat_Mat(4, 4, out1, 4, 4, Q, P);
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
// K = P*H' / ( H*P*H' + R);
double mat[4][2];
Mul_Mat_Mat(4, 4, P, 4, 2, HT, mat);
Plot_Mat(mat,"P(k+1|k).HT = ");
double mat1[2][2], mat2[2][2];
Mul_Mat_Mat(2, 4, H, 4, 2, mat, mat1);
Add_Mat_Mat(2, 2, mat1, 2, 2, R, mat2);
Plot_Mat(mat2, "H.P(k+1|k).HT + R = ");
double mat3[2][2];
Inverse_Mat_22(2,2,mat2,mat3);
Mul_Mat_Mat(4,2,mat,2,2,mat3,K);
Plot_Mat(K,"K= ");
//P = F*P*F'+Q;
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
// K = P*H' / ( H*P*H' + R);
Plot_Mat(K,"K = ");
//X = X + K*([xb(i);yb(i)]-H*X);
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
//X = X + K*([xb(i);yb(i)]-H*X);
double Obs[2][1]={{x}, {y}}, Delta[2][1], mat4[2][1];
Mul_Mat_Mat(2, 4, H, 4, 1, X, mat4);
Sub_Mat_Mat(2, 1, Obs, 2, 1, mat4, Delta);
Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
double mat5[4][1];
Mul_Mat_Mat(4, 2, K, 2, 1, Delta, mat5);
Add_Mat_Mat(4, 1, X, 4, 1, mat5, X);
Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
// P = P - K*H*P;
// P = P - K*H*P;
double mat6 [4][4];
Mul_Mat_Mat(4,2,K,2,4,H,mat6);
double mat7[4][4];
Mul_Mat_Mat(4,4,mat6,4,4,P,mat7);
Sub_Mat_Mat(4,4,P,4,4,mat7,P);
Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
/// La matrice X doit contenir la position filtrée ///

Formats disponibles : Unified diff