Projet

Général

Profil

« Précédent | Suivant » 

Révision 248

Ajouté par clbouillot2 il y a presque 4 ans

Implantation des équations pour le filtre de kalman, quelques soucis encore

Voir les différences:

sp4a3_kalman.c
}
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
A[0][0]=(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*B[1][1];
A[0][1]=(-1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*B[0][1];
A[1][0]=(-1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*B[1][0];
A[1][1]=(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*B[0][0];
/*B[0][0]=(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*A[1][1];
B[0][1]=-(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*A[0][1];
B[1][0]=-(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*A[1][0];
B[1][1]=(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*A[0][0];*/
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if (i!=j){
B[i][j]=-(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0])*A[i][j]);
B[i][i]=(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0])*A[j][j]);
}
}
}
}
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
......
}
}
/*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;
for (i=0;i<n;i++){
for (j=0;j<m;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,sum;
for (i=0;i<na;i++){
for (j=0;j<mb;j++){
sum=0;
for(k=0;k<ma;k++){
sum+=A[i][k]*B[k][j];
}
out[i][j]=sum;
}
}
}
*/
void tests_unitaires(void){
//Matrices d'entrée
double T21a[2][1]={{7},{-5}};
......
Add_Mat_Mat(4,1,T41a,4,1,T41b,R41); if (!Equal_Mat_Mat(RAT41,R41)) error("Erreur calcul Addition 4x1");
Sub_Mat_Mat(2,1,T21a,2,1,T21b,R21); if (!Equal_Mat_Mat(RST21,R21)) error("Erreur calcul Soustraction 2x1");
Sub_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RST44,R44)) error("Erreur calcul Soustraction 4x4");
/*Mul_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RMT44T44,R44)) error("Erreur calcul Multiplication 4x4 4x4");
Mul_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RMT44T44,R44)) error("Erreur calcul Multiplication 4x4 4x4");
Mul_Mat_Mat(4,4,T44a,4,1,T41a,R41); if (!Equal_Mat_Mat(RMT44T41,R41)) error("Erreur calcul Multiplication 4x4 4x1");
Mul_Mat_Mat(4,4,T44a,4,2,T42a,R42); if (!Equal_Mat_Mat(RMT44T42,R42)) error("Erreur calcul Multiplication 4x4 4x2");
Mul_Mat_Mat(4,2,T42a,2,1,T21a,R41); if (!Equal_Mat_Mat(RMT42T21,R41)) error("Erreur calcul Multiplication 4x2 2x1");
......
Mul_Mat_Mat(4,2,T42a,2,4,T24,R44); if (!Equal_Mat_Mat(RMT42T24,R44)) error("Erreur calcul Multiplication 4x2 2x4");
Mul_Mat_Mat(2,4,T24,4,1,T41a,R21); if (!Equal_Mat_Mat(RMT24T41,R21)) error("Erreur calcul Multiplication 2x4 4x1");
Mul_Mat_Mat(2,4,T24,4,2,T42a,R22); if (!Equal_Mat_Mat(RMT24T42,R22)) error("Erreur calcul Multiplication 2x4 4x2");
Mul_Mat_Mat(2,4,T24,4,4,T44a,R24); if (!Equal_Mat_Mat(RMT24T44,R24)) error("Erreur calcul Multiplication 2x4 4x4");*/
Mul_Mat_Mat(2,4,T24,4,4,T44a,R24); if (!Equal_Mat_Mat(RMT24T44,R24)) error("Erreur calcul Multiplication 2x4 4x4");
printf("Test unitaires OK.\n");
}
......
debug=0; ///Mettre à 1 pour afficher les matrices.
///Ajouter votre code ci-dessous///
// Kalman
// X = F*X
// X = F*X
double Res1 [4][1];
X=Mul_Mat_Mat(4,4,F,4,1,X,Res1);
Plot_Mat(X," X(k+1|k) = ");
//P = F*P*F'+Q;
//P = F*P*F'+Q;
double Res2 [4][4],FP [4][4], Res3[4][4], Res4[4][4], FPFt[4][4];
FP=Mul_Mat_Mat(4,4,F,4,4,P,Res2); //multiplication de F et P
FPFt=Mul_Mat_Mat(4,4,FP,4,4,FT,Res3); //multiplication de F*P et F'
P=Add_Mat_Mat(4,4,FPFt,4,4,Q,Res4);
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
// K = P*H' / ( H*P*H' + R);
// K = P*H' / ( H*P*H' + R);
double Res9 [2][2],Res10[4][2], Res11[2][2],Res12[4][2],Res13[2][2],PHt[4][2],HPHt[2][2],HPHtR[2][2],InvHPHtR[2][2];
PHt=Mul_Mat_Mat(4,4,P,4,2,HT,Res10); //multiplication de P et H'
HPHt=Mul_Mat_Mat(2,4,H,4,2,PHt,Res9); //multiplication de H'*P et H
HPHtR=Add_Mat_Mat(2,2,HPHt,2,2,R,Res11); //addition de H*P*H' et R
InvHPHtR=Inverse_Mat_22(2,2,HPHtR,Res13); // Inversion de H*P*H' + R => 1/H*P*H' + R
K=Mul_Mat_Mat(4,2,PHt,2,2,InvHPHtR,Res12);
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)");
double Res14 [2][1], Res15[4][1], Res16[?][?], Res17[?][?],HX[2][1],Vect[2][1], KVect[4][1],KVectHX[2][1];
HX=Mul_Mat_Mat(2,4,H,4,1,X,Res14);
Vect=[x[i],y[i]];
KVect=Mul_Mat_Mat(4,2,K,2,1,Vect,Res15);
KVectHX=Sub_Mat_Mat(4,1,KVect,2,1,HX,Res16);
X=Add_Mat_Mat(4,1,X,2,1,KVectHX,Res17);
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 Res6 [4][4],Res7[4][4], Res8[4][4],KH[4][4],KHP[4][4];
KH=Mul_Mat_Mat(4,2,K,2,4,H,Res6); //multiplication de K et H
KHP=Mul_Mat_Mat(4,4,KH,4,4,P,Res7); //multiplication de K*H et P
P=Sub_Mat_Mat(4,4,P,4,4,KHP,Res8);
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