Révision 581
Ajouté par Orlani RIVERA il y a presque 3 ans
branch/RIVERA_Orlani/sp4a3/sp4a3_kalman.c | ||
---|---|---|
// Pour compiler : gcc sp4a3_kalman.c -lm
|
||
|
||
#include <stdlib.h>
|
||
#include <stdio.h>
|
||
#include <stdio.h> //inclusion des bibliothèques nécessaires
|
||
#include <math.h>
|
||
#include "sp4a3_kalman_extra.h"
|
||
|
||
|
||
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;
|
||
int j;
|
||
for(i=0; i<na; i++) //on parcourt les lignes
|
||
// Fonction pour additionner les matrices
|
||
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++) // on parcourt les lignes
|
||
{
|
||
for(j=0; j<ma; j++) // on parcourt les colonnes
|
||
for (j=0;j<ma;j++) // On parcourt les colonnes
|
||
{
|
||
R[i][j] = A[i][j]+B[i][j];
|
||
R[i][j]=A[i][j]+B[i][j]; // addition des termes ayant les mêmes indices
|
||
}
|
||
}
|
||
}
|
||
|
||
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
|
||
float det;
|
||
det = (A[0][0]*A[1][1]-A[1][0]*A[0][1]);
|
||
if(det !=0){
|
||
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 Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
|
||
int i,j;
|
||
for (i=0;i<n;i++)
|
||
for (j=0;j<m;j++)
|
||
R[j][i]=A[i][j];
|
||
// Fonction pour inverser une matrice 2x2
|
||
void Inverse_Mat_22(int n,int m,double A[n][m],double R[n][m])
|
||
{
|
||
double det=1/((A[0][0]*A[1][1])-(A[0][1]*A[1][0]));
|
||
R[0][0]=A[1][1]*det;
|
||
R[1][1]=A[0][0]*det;
|
||
R[0][1]=-A[0][1]*det;
|
||
R[1][0]=-A[1][0]*det;
|
||
//printf("\n %f %f\n %f %f\n",R[0][0] ,R[0][1] ,R[1][0] ,R[1][1]);
|
||
}
|
||
|
||
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;
|
||
int j;
|
||
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;
|
||
int j;
|
||
int k;
|
||
|
||
for(i=0; i<na; i++)
|
||
// Fonction pour faire la transposée d'une matrice
|
||
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n])
|
||
{
|
||
int i,j;
|
||
for (i=0;i<n;i++)
|
||
{
|
||
for(j=0; j<mb; j++)
|
||
for (j=0;j<m;j++)
|
||
{
|
||
R[i][j] = 0;
|
||
for(k=0; k<ma; k++)
|
||
R[i][j] += A[i][k]*B[k][j];
|
||
R[j][i]=A[i][j]; // Inversion des indices des lignes/colonnes de la matrice
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void tests_unitaires(void){
|
||
//Matrices d'entrée
|
||
double T21a[2][1]={{7},{-5}};
|
||
double T21b[2][1]={{-3},{46}};
|
||
double T22a[2][2]={{12,78},{-5,13}};
|
||
double T22b[2][2]={{-25,36},{7,42}};
|
||
double T24[2][4]={{7,-71,-12,3},{41,123,-5,10}};
|
||
double T41a[4][1]={{45},{-123},{-78},{-410}};
|
||
double T41b[4][1]={{-10},{45},{27},{-9}};
|
||
double T42a[4][2]={{-73,45},{10,12},{-41,-35},{8,-23}};
|
||
double T44a[4][4]={{1,2,7,4},{6,5,7,8},{9,8,7,6},{5,4,3,2}};
|
||
double T44b[4][4]={{12,13,14,15},{21,22,23,40},{78,45,12,3},{54,10,12,47}};
|
||
|
||
//Matrices résultat
|
||
double R21[2][1],R22[2][2],R24[2][4],R41[4][1],R42[4][2],R44[4][4];
|
||
|
||
//Matrices de validation
|
||
double RST21[2][1]={{10},{-51}};
|
||
double RInvT22[2][2]={{0.02380952380952381,-0.1428571428571428},{0.009157509157509158,0.02197802197802198}};
|
||
double RAT22[2][2]={{-13,114},{2,55}};
|
||
double RTT24[4][2]={{7,41},{-71,123},{-12,-5},{3,10}};
|
||
double RMT24T41[2][1]={{8754},{-16994}};
|
||
double RMT24T42[2][2]={{-705,-186},{-1478,3266}};
|
||
double RMT24T44[2][4]={{-512,-425,-523,-606},{784,697,1143,1138}};
|
||
double RAT41[4][1]={{35},{-78},{-51},{-419}};
|
||
double RMT42T21[4][1]={{-736},{10},{-112},{171}};
|
||
double RMT42T22[4][2]={{-1101,-5109},{60,936},{-317,-3653},{211,325}};
|
||
double RMT42T24[4][4]={{1334,10718,651,231},{562,766,-180,150},{-1722,-1394,667,-473},{-887,-3397,19,-206}};
|
||
double RTT44[4][4]={{1,6,9,5},{2,5,8,4},{7,7,7,3},{4,8,6,2}};
|
||
double RMT44T41[4][1]={{-2387},{-4171},{-3585},{-1321}};
|
||
double RMT44T42[4][2]={{-308,-268},{-611,-99},{-816,118},{-432,122}};
|
||
double RAT44[4][4]={{13,15,21,19},{27,27,30,48},{87,53,19,9},{59,14,15,49}};
|
||
double RST44[4][4]={{-11,-11,-7,-11},{-15,-17,-16,-32},{-69,-37,-5,3},{-49,-6,-9,-45}};
|
||
double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
|
||
|
||
printf("Execution des tests unitaires.\n");
|
||
Transpose_Mat(2,4,T24,R42); if (!Equal_Mat_Mat(RTT24,R42)) error("Erreur calcul Transposition 2x4");
|
||
Transpose_Mat(4,4,T44a,R44); if (!Equal_Mat_Mat(RTT44,R44)) error("Erreur calcul Transposition 4x4");
|
||
Inverse_Mat_22(2,2,T22a,R22); if (!Equal_Mat_Mat(RInvT22,R22)) error("Erreur calcul Inversion 2x2");
|
||
Add_Mat_Mat(2,2,T22a,2,2,T22b,R22); if (!Equal_Mat_Mat(RAT22,R22)) error("Erreur calcul Addition 2x2");
|
||
Add_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RAT44,R44)) error("Erreur calcul Addition 4x4");
|
||
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,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,2,T22a,R42); if (!Equal_Mat_Mat(RMT42T22,R42)) error("Erreur calcul Multiplication 4x2 2x2");
|
||
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");
|
||
printf("Test unitaires OK.\n");
|
||
}
|
||
|
||
int main(int argc,char **argv){
|
||
|
||
tests_unitaires();
|
||
|
||
FILE* fichier = fopen("pos_t_x_y.dat","r");
|
||
if (fichier == NULL)
|
||
error("Impossible d'ouvrir le fichier GPGGA_data.dat");
|
||
|
||
FILE * Fout = Fout = fopen("output.dat","w");
|
||
if (fichier == NULL)
|
||
error("Impossible d'ouvrir le fichier output.dat");
|
||
|
||
printf("Kalman\n");
|
||
double t = 0;
|
||
double t0,x0,y0;
|
||
double xobs,yobs;
|
||
double oldx,oldy;
|
||
double dx=0,dy=0,dt=0.1;
|
||
int cpt = 0;
|
||
|
||
// kalman param
|
||
double sigma_etat = 10.0;
|
||
double sigma_observation = 2.0;
|
||
double X[4][1] = {{0},{0},{0},{0}};
|
||
|
||
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 Q[4][4] = {{0, 0, 0, 0},
|
||
{0, 0, 0, 0},
|
||
{0, 0, 0.1, 0},
|
||
{0, 0, 0, 0.1}};
|
||
|
||
double R[2][2] = {{sigma_observation*sigma_observation, 0},
|
||
{0 , sigma_observation*sigma_observation}};
|
||
|
||
double K[4][2];
|
||
double H[2][4] = {{1, 0, 0, 0},
|
||
{0, 1, 0, 0}};
|
||
double HT[4][2];
|
||
Transpose_Mat(2,4,H,HT);
|
||
|
||
double F[4][4] = {{1, 0, dt, 0},
|
||
{0, 1, 0, dt},
|
||
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FR[4][4];
|
||
double X1[4][1];
|
||
double FxP[4][4];
|
||
double FxPxFT[4][4];
|
||
double PA[4][4];
|
||
double PAxHT[4][2];
|
||
double HxPA[2][4];
|
||
double HxPAxHT[2][2];
|
||
double HxPAxHTplusR[2][2];
|
||
double invHxPAxHTplusR[2][2];
|
||
double HxX1[2][1];
|
||
double DELTA[2][1];
|
||
double KxDELTA[4][1];
|
||
double X2[4][1];
|
||
double KxH[4][4];
|
||
double KxHxPA[4][4];
|
||
double PB[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
double Obs[2][1] ={{xobs}, {yobs}};
|
||
if (cpt ==0)
|
||
{
|
||
t0=t;x0=xobs;y0=yobs;
|
||
xobs=xobs-x0;yobs=yobs-y0;
|
||
Plot_Mat(F,"F = ");
|
||
Plot_Mat(H,"H = ");
|
||
Plot_Mat(R,"R = ");
|
||
}
|
||
else
|
||
{
|
||
t -= t0;xobs -= x0;yobs -= y0;
|
||
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
double Obs[2][1] ={{xobs}, {yobs}};
|
||
Mul_Mat_Mat(4, 4, F, 4, 1, X, X1);// X = F*X
|
||
Plot_Mat(XA," X(k+1|k) = ");
|
||
|
||
Mul_Mat_Mat(4, 4, F, 4, 4, P, FxP);
|
||
Mul_Mat_Mat(4, 4, FxP, 4, 4, FR, FxPxFr);
|
||
Add_Mat_Mat(4, 4, FxPxFT, 4, 4, Q, PA);//P = F*P*F'+Q;
|
||
Plot_Mat(PA,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
Mul_Mat_Mat(4, 4, PA, 4, 2, HT, PAxHT);
|
||
Mul_Mat_Mat(2, 4, H, 4, 4, PA, HxPA);
|
||
Mul_Mat_Mat(2, 4, HxPA, 4, 2, HT, HxPAxHT);
|
||
Add_Mat_Mat(2, 2, HxPAxHT, 2, 2, R, HxPAxHTplusR);
|
||
Inverse_Mat_22(2, 2, HxPAxHTplusR, invHxPAxHTplusR);
|
||
Mul_Mat_Mat(4, 2, PAxHT, 2, 2, invHxPAxHTplusR, K);// K = P*H' / ( H*P*H' + R);
|
||
Plot_Mat(K,"K = ");
|
||
// Pour compiler : gcc sp4a3_kalman.c -lm
|
||
|
||
#include <stdlib.h>
|
||
#include <stdio.h>
|
||
#include <math.h>
|
||
|
||
#include "sp4a3_kalman_extra.h"
|
||
|
||
|
||
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;
|
||
int j;
|
||
for(i=0; i<na; i++) //on parcourt les lignes
|
||
// Fonction pour faire la différence de deux matrices
|
||
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;
|
||
for(i=0;i<na;i++)
|
||
{
|
||
for(j=0; j<ma; j++) // on parcourt les colonnes
|
||
for (j=0;j<ma;j++)
|
||
{
|
||
R[i][j] = A[i][j]+B[i][j];
|
||
R[i][j]=A[i][j]-B[i][j]; // Soustraction des termes ayant les mêmes indices
|
||
}
|
||
}
|
||
}
|
||
|
||
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
|
||
float det;
|
||
det = (A[0][0]*A[1][1]-A[1][0]*A[0][1]);
|
||
if(det !=0){
|
||
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 Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
|
||
int i,j;
|
||
for (i=0;i<n;i++)
|
||
for (j=0;j<m;j++)
|
||
R[j][i]=A[i][j];
|
||
}
|
||
|
||
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;
|
||
int j;
|
||
for(i=0; i<na; i++)
|
||
// Fonction pour multiplier deux matrices
|
||
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;
|
||
for(i=0;i<na;i++)
|
||
{
|
||
for(j=0; j<ma; j++)
|
||
for (j=0;j<mb;j++)
|
||
{
|
||
R[i][j] = A[i][j]-B[i][j];
|
||
R[i][j]=0;
|
||
for (k=0;k<ma;k++)
|
||
{
|
||
R[i][j]+=A[i][k]*B[k][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;
|
||
int j;
|
||
int k;
|
||
|
||
for(i=0; i<na; i++)
|
||
{
|
||
for(j=0; j<mb; j++)
|
||
{
|
||
R[i][j] = 0;
|
||
for(k=0; k<ma; k++)
|
||
R[i][j] += A[i][k]*B[k][j];
|
||
}
|
||
}
|
||
}
|
||
//Tests unitaires
|
||
void tests_unitaires(void)
|
||
{
|
||
//Matrices d'entrée
|
||
double T21a[2][1]={{7},{-5}};
|
||
double T21b[2][1]={{-3},{46}};
|
||
double T22a[2][2]={{12,78},{-5,13}};
|
||
double T22b[2][2]={{-25,36},{7,42}};
|
||
double T24[2][4]={{7,-71,-12,3},{41,123,-5,10}};
|
||
double T41a[4][1]={{45},{-123},{-78},{-410}};
|
||
double T41b[4][1]={{-10},{45},{27},{-9}};
|
||
double T42a[4][2]={{-73,45},{10,12},{-41,-35},{8,-23}};
|
||
double T44a[4][4]={{1,2,7,4},{6,5,7,8},{9,8,7,6},{5,4,3,2}};
|
||
double T44b[4][4]={{12,13,14,15},{21,22,23,40},{78,45,12,3},{54,10,12,47}};
|
||
|
||
//Matrices résultat
|
||
double R21[2][1],R22[2][2],R24[2][4],R41[4][1],R42[4][2],R44[4][4];
|
||
|
||
void tests_unitaires(void){
|
||
//Matrices d'entrée
|
||
double T21a[2][1]={{7},{-5}};
|
||
double T21b[2][1]={{-3},{46}};
|
||
double T22a[2][2]={{12,78},{-5,13}};
|
||
double T22b[2][2]={{-25,36},{7,42}};
|
||
double T24[2][4]={{7,-71,-12,3},{41,123,-5,10}};
|
||
double T41a[4][1]={{45},{-123},{-78},{-410}};
|
||
double T41b[4][1]={{-10},{45},{27},{-9}};
|
||
double T42a[4][2]={{-73,45},{10,12},{-41,-35},{8,-23}};
|
||
double T44a[4][4]={{1,2,7,4},{6,5,7,8},{9,8,7,6},{5,4,3,2}};
|
||
double T44b[4][4]={{12,13,14,15},{21,22,23,40},{78,45,12,3},{54,10,12,47}};
|
||
//Matrices de validation
|
||
double RST21[2][1]={{10},{-51}};
|
||
double RInvT22[2][2]={{0.02380952380952381,-0.1428571428571428},{0.009157509157509158,0.02197802197802198}};
|
||
double RAT22[2][2]={{-13,114},{2,55}};
|
||
double RTT24[4][2]={{7,41},{-71,123},{-12,-5},{3,10}};
|
||
double RMT24T41[2][1]={{8754},{-16994}};
|
||
double RMT24T42[2][2]={{-705,-186},{-1478,3266}};
|
||
double RMT24T44[2][4]={{-512,-425,-523,-606},{784,697,1143,1138}};
|
||
double RAT41[4][1]={{35},{-78},{-51},{-419}};
|
||
double RMT42T21[4][1]={{-736},{10},{-112},{171}};
|
||
double RMT42T22[4][2]={{-1101,-5109},{60,936},{-317,-3653},{211,325}};
|
||
double RMT42T24[4][4]={{1334,10718,651,231},{562,766,-180,150},{-1722,-1394,667,-473},{-887,-3397,19,-206}};
|
||
double RTT44[4][4]={{1,6,9,5},{2,5,8,4},{7,7,7,3},{4,8,6,2}};
|
||
double RMT44T41[4][1]={{-2387},{-4171},{-3585},{-1321}};
|
||
double RMT44T42[4][2]={{-308,-268},{-611,-99},{-816,118},{-432,122}};
|
||
double RAT44[4][4]={{13,15,21,19},{27,27,30,48},{87,53,19,9},{59,14,15,49}};
|
||
double RST44[4][4]={{-11,-11,-7,-11},{-15,-17,-16,-32},{-69,-37,-5,3},{-49,-6,-9,-45}};
|
||
double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
|
||
printf("Execution des tests unitaires.\n");
|
||
|
||
//Matrices résultat
|
||
double R21[2][1],R22[2][2],R24[2][4],R41[4][1],R42[4][2],R44[4][4];
|
||
|
||
//Matrices de validation
|
||
double RST21[2][1]={{10},{-51}};
|
||
double RInvT22[2][2]={{0.02380952380952381,-0.1428571428571428},{0.009157509157509158,0.02197802197802198}};
|
||
double RAT22[2][2]={{-13,114},{2,55}};
|
||
double RTT24[4][2]={{7,41},{-71,123},{-12,-5},{3,10}};
|
||
double RMT24T41[2][1]={{8754},{-16994}};
|
||
double RMT24T42[2][2]={{-705,-186},{-1478,3266}};
|
||
double RMT24T44[2][4]={{-512,-425,-523,-606},{784,697,1143,1138}};
|
||
double RAT41[4][1]={{35},{-78},{-51},{-419}};
|
||
double RMT42T21[4][1]={{-736},{10},{-112},{171}};
|
||
double RMT42T22[4][2]={{-1101,-5109},{60,936},{-317,-3653},{211,325}};
|
||
double RMT42T24[4][4]={{1334,10718,651,231},{562,766,-180,150},{-1722,-1394,667,-473},{-887,-3397,19,-206}};
|
||
double RTT44[4][4]={{1,6,9,5},{2,5,8,4},{7,7,7,3},{4,8,6,2}};
|
||
double RMT44T41[4][1]={{-2387},{-4171},{-3585},{-1321}};
|
||
double RMT44T42[4][2]={{-308,-268},{-611,-99},{-816,118},{-432,122}};
|
||
double RAT44[4][4]={{13,15,21,19},{27,27,30,48},{87,53,19,9},{59,14,15,49}};
|
||
double RST44[4][4]={{-11,-11,-7,-11},{-15,-17,-16,-32},{-69,-37,-5,3},{-49,-6,-9,-45}};
|
||
double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
|
||
|
||
printf("Execution des tests unitaires.\n");
|
||
Transpose_Mat(2,4,T24,R42); if (!Equal_Mat_Mat(RTT24,R42)) error("Erreur calcul Transposition 2x4");
|
||
Transpose_Mat(4,4,T44a,R44); if (!Equal_Mat_Mat(RTT44,R44)) error("Erreur calcul Transposition 4x4");
|
||
Inverse_Mat_22(2,2,T22a,R22); if (!Equal_Mat_Mat(RInvT22,R22)) error("Erreur calcul Inversion 2x2");
|
||
Add_Mat_Mat(2,2,T22a,2,2,T22b,R22); if (!Equal_Mat_Mat(RAT22,R22)) error("Erreur calcul Addition 2x2");
|
||
Add_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RAT44,R44)) error("Erreur calcul Addition 4x4");
|
||
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,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,2,T22a,R42); if (!Equal_Mat_Mat(RMT42T22,R42)) error("Erreur calcul Multiplication 4x2 2x2");
|
||
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");
|
||
printf("Test unitaires OK.\n");
|
||
Transpose_Mat(2,4,T24,R42); if (!Equal_Mat_Mat(RTT24,R42)) error("Erreur calcul Transposition 2x4");
|
||
Transpose_Mat(4,4,T44a,R44); if (!Equal_Mat_Mat(RTT44,R44)) error("Erreur calcul Transposition 4x4");
|
||
Inverse_Mat_22(2,2,T22a,R22); if (!Equal_Mat_Mat(RInvT22,R22)) error("Erreur calcul Inversion 2x2");
|
||
Add_Mat_Mat(2,2,T22a,2,2,T22b,R22); if (!Equal_Mat_Mat(RAT22,R22)) error("Erreur calcul Addition 2x2");
|
||
Add_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RAT44,R44)) error("Erreur calcul Addition 4x4");
|
||
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,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,2,T22a,R42); if (!Equal_Mat_Mat(RMT42T22,R42)) error("Erreur calcul Multiplication 4x2 2x2");
|
||
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");
|
||
printf("Test unitaires OK.\n");
|
||
}
|
||
|
||
int main(int argc,char **argv){
|
||
|
||
tests_unitaires();
|
||
|
||
FILE* fichier = fopen("pos_t_x_y.dat","r");
|
||
if (fichier == NULL)
|
||
// Programe principal
|
||
int main(int argc,char **argv)
|
||
{
|
||
tests_unitaires();
|
||
FILE* fichier = fopen("pos_t_x_y.dat","r");
|
||
if (fichier == NULL)
|
||
error("Impossible d'ouvrir le fichier GPGGA_data.dat");
|
||
|
||
FILE * Fout = Fout = fopen("output.dat","w");
|
||
if (fichier == NULL)
|
||
FILE * Fout = Fout = fopen("output.dat","w");
|
||
if (fichier == NULL)
|
||
error("Impossible d'ouvrir le fichier output.dat");
|
||
printf("Kalman\n");
|
||
double t = 0;
|
||
double t0,x0,y0;
|
||
double xobs,yobs;
|
||
double oldx,oldy;
|
||
double dx=0,dy=0,dt=0.1;
|
||
int cpt = 0,i,j;
|
||
|
||
printf("Kalman\n");
|
||
double t = 0;
|
||
double t0,x0,y0;
|
||
double xobs,yobs;
|
||
double oldx,oldy;
|
||
double dx=0,dy=0,dt=0.1;
|
||
int cpt = 0;
|
||
// kalman paramètres
|
||
double sigma_etat = 10.0;
|
||
double sigma_observation = 2.0;
|
||
double X[4][1] = {{0},{0},{0},{0}};
|
||
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 Q[4][4] = {{0, 0, 0, 0},
|
||
{0, 0, 0, 0},
|
||
{0, 0, 0.1, 0},
|
||
{0, 0, 0, 0.1}};
|
||
double R[2][2] = {{sigma_observation*sigma_observation, 0},
|
||
{0 , sigma_observation*sigma_observation}};
|
||
double K[4][2];
|
||
double H[2][4] = {{1, 0, 0, 0},
|
||
{0, 1, 0, 0}};
|
||
double HT[4][2];
|
||
Transpose_Mat(2,4,H,HT);
|
||
double F[4][4] = {{1, 0, dt, 0},
|
||
{0, 1, 0, dt},
|
||
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
// kalman param
|
||
double sigma_etat = 10.0;
|
||
double sigma_observation = 2.0;
|
||
double X[4][1] = {{0},{0},{0},{0}};
|
||
//création de matrices intermédiaires
|
||
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 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];
|
||
double Delta[2][1];
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0)
|
||
{
|
||
printf("-------------%04d--------------\n",cpt);
|
||
if (cpt ==0)
|
||
{
|
||
t0=t;x0=xobs;y0=yobs;
|
||
xobs=xobs-x0;yobs=yobs-y0;
|
||
Plot_Mat(F,"F = ");
|
||
Plot_Mat(H,"H = ");
|
||
Plot_Mat(R,"R = ");
|
||
}
|
||
else
|
||
{
|
||
t -= t0;xobs -= x0;yobs -= y0;
|
||
debug=1;
|
||
///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
double XY[2][1]={{xobs},{yobs}};
|
||
|
||
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}};
|
||
// X = F*X
|
||
Mul_Mat_Mat(4,4,F,4,1,X,X1);
|
||
Plot_Mat(X1," X(k+1|k) = ");
|
||
|
||
double Q[4][4] = {{0, 0, 0, 0},
|
||
{0, 0, 0, 0},
|
||
{0, 0, 0.1, 0},
|
||
{0, 0, 0, 0.1}};
|
||
//P = F*P*F'+Q;
|
||
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 = ");
|
||
|
||
double R[2][2] = {{sigma_observation*sigma_observation, 0},
|
||
{0 , sigma_observation*sigma_observation}};
|
||
// 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 = ");
|
||
|
||
double K[4][2];
|
||
double H[2][4] = {{1, 0, 0, 0},
|
||
{0, 1, 0, 0}};
|
||
double HT[4][2];
|
||
Transpose_Mat(2,4,H,HT);
|
||
//X = X + K*([xobs(i);yobs(i)]-H*X);
|
||
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 = ");
|
||
|
||
double F[4][4] = {{1, 0, dt, 0},
|
||
{0, 1, 0, dt},
|
||
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FR[4][4];
|
||
double X1[4][1];
|
||
double FxP[4][4];
|
||
double FxPxFR[4][4];
|
||
double PA[4][4];
|
||
double PAxHT[4][2];
|
||
double HxPA[2][4];
|
||
double HxPAxHT[2][2];
|
||
double HxPAxHTplusR[2][2];
|
||
double invHxPAxHTplusR[2][2];
|
||
double HxXA[2][1];
|
||
double DELTA[2][1];
|
||
double KxDELTA[4][1];
|
||
double X2[4][1];
|
||
double KxH[4][4];
|
||
double KxHxPA[4][4];
|
||
double PB[4][4];
|
||
Transpose_Mat(4,4,F,FR);
|
||
// 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) = ");
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
double Obs[2][1] ={{xobs}, {yobs}};
|
||
if (cpt ==0)
|
||
{
|
||
t0=t;x0=xobs;y0=yobs;
|
||
xobs=xobs-x0;yobs=yobs-y0;
|
||
Plot_Mat(F,"F = ");
|
||
Plot_Mat(H,"H = ");
|
||
Plot_Mat(R,"R = ");
|
||
}
|
||
else
|
||
{
|
||
t -= t0;xobs -= x0;yobs -= y0;
|
||
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
double Obs[2][1] ={{xobs}, {yobs}};
|
||
Mul_Mat_Mat(4, 4, F, 4, 1, X, X1);// X = F*X
|
||
Plot_Mat(X1," X(k+1|k) = ");
|
||
|
||
Mul_Mat_Mat(4, 4, F, 4, 4, P, FxP);
|
||
Mul_Mat_Mat(4, 4, FxP, 4, 4, FT, FxPxFR);
|
||
Add_Mat_Mat(4, 4, FxPxFR, 4, 4, Q, PA);//P = F*P*F'+Q;
|
||
Plot_Mat(PA,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
Mul_Mat_Mat(4, 4, PA, 4, 2, HT, PAxHT);
|
||
Mul_Mat_Mat(2, 4, H, 4, 4, PA, HxPA);
|
||
Mul_Mat_Mat(2, 4, HxPA, 4, 2, HT, HxPAxHT);
|
||
Add_Mat_Mat(2, 2, HxPAxHT, 2, 2, R, HxPAxHTplusR);
|
||
Inverse_Mat_22(2, 2, HxPAxHTplusR, invHxPAxHTplusR);
|
||
Mul_Mat_Mat(4, 2, PAxHT, 2, 2, invHxPAxHTplusR, K);// K = P*H' / ( H*P*H' + R);
|
||
Plot_Mat(K,"K = ");
|
||
|
||
Mul_Mat_Mat(2, 4, H, 4, 1, XA, HxX1);
|
||
Sub_Mat_Mat(2, 1, Obs, 2, 1, HxXA, DELTA);
|
||
Mul_Mat_Mat(4, 2, K, 2, 1, DELTA, KxDELTA);
|
||
Add_Mat_Mat(4, 1, X1, 4, 1, KxDELTA, X2);//X = X + K*([xobs(i);yobs(i)]-H*X);
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
Plot_Mat(XB," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
Mul_Mat_Mat(4, 2, K, 2, 4, H, KxH);
|
||
Mul_Mat_Mat(4, 4, KxH, 4, 4, PA, KxHxPA);
|
||
Sub_Mat_Mat(4, 4, PA, 4, 4, KxHxPA, PB);// P = P - K*H*P;
|
||
Plot_Mat(PB," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
/// La matrice X doit contenir la position filtrée ///
|
||
int i;
|
||
int j;
|
||
for(i=0; i<4; i++)
|
||
{
|
||
X[i][0] = X2[i][0];
|
||
for(j=0; j<4; j++)
|
||
{
|
||
P[i][j]=PB[i][j];
|
||
//X(k+1|k) devient X(k+1|k+1) et X(k|k) devient X(k+1|k) ( X=X1; X1=X2 );
|
||
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;
|
||
dy = (yobs - oldy)/dt;
|
||
fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,xobs,yobs,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
|
||
oldx = xobs;
|
||
oldy = yobs;
|
||
cpt ++;
|
||
}
|
||
fclose(Fout);
|
||
fclose(fichier);
|
||
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 9 w l , 'vitesse_reelle.dat' u 2 w l\";");
|
||
return 0;
|
||
t = cpt * dt;
|
||
dx = (xobs - oldx)/dt;
|
||
dy = (yobs - oldy)/dt;
|
||
fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,xobs,yobs,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
|
||
oldx = xobs;
|
||
oldy = yobs;
|
||
cpt ++;
|
||
}
|
||
fclose(Fout);
|
||
fclose(fichier);
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 9 w l , 'vitesse_reelle.dat' u 2 w l\";");
|
||
return 0;
|
||
}
|
||
Mul_Mat_Mat(2, 4, H, 4, 1, X1, HxXA);
|
||
Sub_Mat_Mat(2, 1, Obs, 2, 1, HxX1, DELTA);
|
||
Mul_Mat_Mat(4, 2, K, 2, 1, DELTA, KxDELTA);
|
||
Add_Mat_Mat(4, 1, X1, 4, 1, KxDELTA, X2);//X = X + K*([xobs(i);yobs(i)]-H*X);
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
Plot_Mat(XB," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
Mul_Mat_Mat(4, 2, K, 2, 4, H, KxH);
|
||
Mul_Mat_Mat(4, 4, KxH, 4, 4, PA, KxHxPA);
|
||
Sub_Mat_Mat(4, 4, PA, 4, 4, KxHxPA, PB);// P = P - K*H*P;
|
||
Plot_Mat(PB," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
/// La matrice X doit contenir la position filtrée ///
|
||
int i;
|
||
int j;
|
||
for(i=0; i<4; i++)
|
||
{
|
||
X[i][0] = X2[i][0];
|
||
for(j=0; j<4; j++)
|
||
{
|
||
P[i][j]=PB[i][j];
|
||
}
|
||
}
|
||
}
|
||
t = cpt * dt;
|
||
dx = (xobs - oldx)/dt;
|
||
dy = (yobs - oldy)/dt;
|
||
fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,xobs,yobs,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
|
||
oldx = xobs;
|
||
oldy = yobs;
|
||
cpt ++;
|
||
}
|
||
fclose(Fout);
|
||
fclose(fichier);
|
||
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 9 w l , 'vitesse_reelle.dat' u 2 w l\";");
|
||
return 0;
|
||
}
|
Formats disponibles : Unified diff
correction TP3, identation, ajout de commentaires