Révision 467
Ajouté par Lea DUVIVIER il y a environ 3 ans
branch/duvivier_lea/sp4a3/sp4a3_kalman.c | ||
---|---|---|
|
||
|
||
void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double R[na][ma]){
|
||
|
||
int i,j;
|
||
for(i=0;i<na;i++)
|
||
{
|
||
for(j=0;j<ma;j++)
|
||
{
|
||
R[i][j]=A[i][j]+B[i][j];
|
||
}
|
||
}
|
||
}
|
||
|
||
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
|
||
float det;
|
||
det=(1/(A[0][0]*A[1][1]-A[1][0]*A[0][1]));
|
||
B[0][0]=A[1][1]*det;
|
||
B[0][1]=-A[0][1]*det;
|
||
B[1][0]=-A[1][0]*det;
|
||
B[1][1]=A[0][0]*det;
|
||
|
||
}
|
||
|
||
... | ... | |
}
|
||
|
||
void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double R[na][ma]){
|
||
|
||
int i,j;
|
||
nb=na;
|
||
mb=ma;
|
||
for(i=0;i<na;i++)
|
||
{
|
||
for(j=0;j<ma;j++)
|
||
{
|
||
R[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 R[na][mb]){
|
||
|
||
int i,j,k,a,b;
|
||
for(a=0;a<na;a++)
|
||
{
|
||
for(b=0;b<mb;b++)
|
||
{
|
||
R[a][b]=0;
|
||
}
|
||
}
|
||
for(i=0;i<na;i++)
|
||
{
|
||
for(j=0;j<mb;j++)
|
||
|
||
{ for(k=0;k<nb;k++)
|
||
{
|
||
R[i][j]+=A[i][k]*B[k][j];
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
... | ... | |
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
Transpose_Mat(4,4,F,FT);
|
||
double X1[4][1];
|
||
double P1[4][4]; /*Déclaration des différents tampons*/
|
||
double tampon1[4][4];
|
||
double tampon2[4][4];
|
||
double tampon3[4][2];
|
||
double tampon4[2][4];
|
||
double tampon5[2][2];
|
||
double tampon6[2][2];
|
||
double tampon7[2][2];
|
||
double X2[4][1];
|
||
double tampon8[2][1];
|
||
double tampon9[4][1];
|
||
double P2[4][4];
|
||
double tampon10[4][4];
|
||
double tampon11[4][4];
|
||
double delta[2][1];
|
||
double obser[2][1];
|
||
int a,b,c,d;
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
... | ... | |
xobs=xobs-x0;yobs=yobs-y0;
|
||
Plot_Mat(F,"F = ");
|
||
Plot_Mat(H,"H = ");
|
||
Plot_Mat(R,"R = ");
|
||
Plot_Mat(R,"R = ");
|
||
|
||
}
|
||
else
|
||
{
|
||
... | ... | |
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
|
||
// Kalman
|
||
Mul_Mat_Mat(4,4,F,4,1,X,X1); /*Calcul des equations qui servent au filtre de Kalman*/
|
||
Mul_Mat_Mat(4,4,F,4,4,P,tampon1);
|
||
Mul_Mat_Mat(4,4,tampon1,4,4,FT,tampon2);
|
||
Add_Mat_Mat(4,4,tampon2,4,4,Q,P1);
|
||
Mul_Mat_Mat(4,4,P1,4,2,HT,tampon3);
|
||
Mul_Mat_Mat(2,4,H,4,4,P1,tampon4);
|
||
Mul_Mat_Mat(2,4,tampon4,4,2,HT,tampon5);
|
||
Add_Mat_Mat(2,2,tampon5,2,2,R,tampon6);
|
||
Inverse_Mat_22(2,2,tampon6,tampon7);
|
||
Mul_Mat_Mat(4,2,tampon3,2,2,tampon7,K);
|
||
Mul_Mat_Mat(2,4,H,4,1,X1,tampon8);
|
||
obser[0][0]=xobs;
|
||
obser[1][0]=yobs; /*On met ici obser car les valeurs changent en fonction des iterations*/
|
||
Sub_Mat_Mat(2,1,obser,2,1,tampon8,delta);
|
||
Mul_Mat_Mat(4,2,K,2,1,delta,tampon9);
|
||
Add_Mat_Mat(4,1,X1,4,1,tampon9,X2);
|
||
Mul_Mat_Mat(4,2,K,2,4,H,tampon10);
|
||
Mul_Mat_Mat(4,4,tampon10,4,4,P1,tampon11);
|
||
Sub_Mat_Mat(4,4,P1,4,4,tampon11,P2);
|
||
// X = F*X
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
|
||
... | ... | |
// P = P - K*H*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 ///
|
||
}
|
||
/// La matrice X doit contenir la position filtrée ///
|
||
for (a=0;a<4;a++)
|
||
{
|
||
|
||
X[a][0]=X2[a][0]; /*Ici on prend la valeur de X2 pour X car on a besoin de mettre à jour le vecteur */
|
||
|
||
|
||
}
|
||
for(b=0;b<4;b++)
|
||
{
|
||
for(c=0;c<4;c++)
|
||
|
||
{
|
||
P[b][c]=P2[b][c]; /*De même*/
|
||
}
|
||
}
|
||
}
|
||
|
||
t = cpt * dt;
|
||
dx = (xobs - oldx)/dt;
|
||
dy = (yobs - oldy)/dt;
|
||
... | ... | |
system ("gnuplot -p -e \"plot 'output.dat' u 9 w l , 'vitesse_reelle.dat' u 2 w l\";");
|
||
return 0;
|
||
}
|
||
|
||
|
Formats disponibles : Unified diff
Filtre de Kalman fini et fonctionnel ( affichage des bonnes courbes)