Révision 545
Ajouté par Victor SOUDY il y a presque 3 ans
sp4a3_kalman.c | ||
---|---|---|
|
||
#include "sp4a3_kalman_extra.h"
|
||
|
||
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
|
||
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n])
|
||
{
|
||
int i,j;
|
||
for (i=0;i<n;i++)
|
||
{
|
||
... | ... | |
|
||
|
||
|
||
void tests_unitaires(void){
|
||
void tests_unitaires(void)
|
||
{
|
||
//Matrices d'entrée
|
||
double T21a[2][1]={{7},{-5}};
|
||
double T21b[2][1]={{-3},{46}};
|
||
... | ... | |
printf("Test unitaires OK.\n");
|
||
}
|
||
|
||
int main(int argc,char **argv){
|
||
int main(int argc,char **argv)
|
||
{
|
||
|
||
tests_unitaires();
|
||
|
||
... | ... | |
double t0,x0,y0;
|
||
double xobs,yobs;
|
||
double oldx,oldy;
|
||
double dx=0,dy=0,dt=0.1;
|
||
int cpt = 0;
|
||
double dx=0,dy=0;
|
||
double dt=0.1;
|
||
int cpt = 0,i,j;
|
||
|
||
// kalman param
|
||
/// Kalman paramètres
|
||
double sigma_etat = 10.0;
|
||
double sigma_observation = 2.0;
|
||
double X[4][1] = {{0},{0},{0},{0}};
|
||
|
||
double sigma_observation = 2.0;
|
||
double X[4][1];
|
||
double P[4][4] = {{sigma_etat*sigma_etat, 0, 0, 0},
|
||
{0, sigma_etat*sigma_etat, 0, 0},
|
||
{0, 0, 0, 0},
|
||
{0, 0, 0, 0}};
|
||
|
||
double X1[4][1];
|
||
double P1[4][4] = {{sigma_etat*sigma_etat, 0, 0, 0},
|
||
{0, sigma_etat*sigma_etat, 0, 0},
|
||
{0, 0, 0, 0},
|
||
{0, 0, 0, 0}};
|
||
|
||
double X2[4][1];
|
||
double P2[4][4] = {{sigma_etat*sigma_etat, 0, 0, 0},
|
||
{0, sigma_etat*sigma_etat, 0, 0},
|
||
{0, 0, 0, 0},
|
||
{0, 0, 0, 0}};
|
||
|
||
double Q[4][4] = {{0, 0, 0, 0},
|
||
... | ... | |
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
/// Matrices intermédiaires
|
||
double Mint[4][4];
|
||
double Mint2[4][4];
|
||
double Mint3[4][4];
|
||
double Mint4[4][4];
|
||
double Mint5[4][4];
|
||
double Mint6[4][4];
|
||
double Mint7[4][4];
|
||
double Mint8[4][4];
|
||
double Mint9[4][4];
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0)
|
||
{
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
if (cpt ==0)
|
||
... | ... | |
{
|
||
t -= t0;xobs -= x0;yobs -= y0;
|
||
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
debug=0; //Mettre à 1 pour afficher les matrices.
|
||
|
||
// X = F*X
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
/// Kalman
|
||
double Delta[2][1];
|
||
double XY[2][1]={{xobs},{yobs}}; //vect observation
|
||
|
||
//P = F*P*F'+Q;
|
||
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
/// X = F*X /X1 pour X à l'état suivant
|
||
Mul_Mat_Mat(4,4,F,4,1,X,X1);
|
||
Plot_Mat(X," X(k|k) = ");
|
||
Plot_Mat(X1," X(k+1|k) = ");
|
||
|
||
// K = P*H' / ( H*P*H' + R);
|
||
///P = F*P*F'+Q /P1 pour P à l'état suivant
|
||
Mul_Mat_Mat(4,4,F,4,4,P,Mint);
|
||
Mul_Mat_Mat(4,4,Mint,4,4,FT,Mint2);
|
||
Add_Mat_Mat(4,4,Mint2,4,4,Q,P1);
|
||
Plot_Mat(P1,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
/// K = P*H' / ( H*P*H' + R);
|
||
Mul_Mat_Mat(4,4,P1,4,2,HT,Mint);
|
||
Mul_Mat_Mat(2,4,H,4,4,P1,Mint2);
|
||
Mul_Mat_Mat(2,4,Mint2,4,2,HT,Mint3);
|
||
Add_Mat_Mat(2,2,Mint3,2,2,R,Mint6);
|
||
Inverse_Mat_22(2,2,Mint6,Mint5);
|
||
Mul_Mat_Mat(4,2,Mint,2,2,Mint5,K);
|
||
Plot_Mat(K,"K = ");
|
||
|
||
//X = X + K*([xobs(i);yobs(i)]-H*X);
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
///X = X + K*([xobs(i);yobs(i)]-H*X); //XY matrice d'observation
|
||
Mul_Mat_Mat(2,4,H,4,1,X1,Mint2);
|
||
Sub_Mat_Mat(2,1,XY,2,1,Mint2,Delta);
|
||
Mul_Mat_Mat(4,2,K,2,1,Delta,Mint7);
|
||
Add_Mat_Mat(4,1,X1,4,1,Mint7,X2);
|
||
Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
Plot_Mat(X2," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
// P = P - K*H*P;
|
||
Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
/// P = P - K*H*P;
|
||
Mul_Mat_Mat(4,2,K,2,4,H,Mint8);
|
||
Mul_Mat_Mat(4,4,Mint8,4,4,P1,Mint9);
|
||
Sub_Mat_Mat(4,4,P1,4,4,Mint9,P2);
|
||
Plot_Mat(P2," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
/// les 'états suivants' calculés précédements deviennent les états initiaux;
|
||
for (i=0;i<4;i++)
|
||
{
|
||
for (j=0;j<1;j++)
|
||
{
|
||
X[i][j]=X2[i][j];
|
||
}
|
||
}
|
||
for (i=0;i<4;i++)
|
||
{
|
||
for (j=0;j<4;j++)
|
||
{
|
||
P[i][j]=P2[i][j];
|
||
}
|
||
}
|
||
|
||
/// La matrice X doit contenir la position filtrée ///
|
||
}
|
||
t = cpt * dt;
|
||
dx = (xobs - oldx)/dt;
|
Formats disponibles : Unified diff
Commit final, programme fonctionnel et indenté