Révision 285
Ajouté par jeleroy il y a presque 4 ans
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;
|
||
if((na == nb)&&(ma == mb)){
|
||
for(i= 0; i < na; i++){
|
||
for(j=0;j < ma;j++){
|
||
out[i][j] = A[i][j] + b[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 out[n][m]){
|
||
int i,j;
|
||
double T[2][2];
|
||
|
||
T[0][0] = A[1][1];
|
||
T[0][1] = -A[0][1];
|
||
T[1][0] = -A[1][0];
|
||
T[1][1] = A[0][0];
|
||
|
||
for(i= 0; i < n; i++){
|
||
for(j=0;j < m;j++){
|
||
out[i][j] = 1/(A[0][0]*A[1][1]-A[0][1]*A[1][0])*T[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]){
|
||
|
||
if((na == nb)&&(ma == mb)){
|
||
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;
|
||
if(ma == nb){
|
||
for(i = 0; i < na; i++)
|
||
{
|
||
for(j = 0; j < mb; j++)
|
||
{
|
||
out[i][j]=0;
|
||
for(k = 0; k < nb; k++)
|
||
{
|
||
out[i][j] += A[i][k] * B[k][j];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
... | ... | |
}
|
||
|
||
int main(int argc,char **argv){
|
||
|
||
int i;
|
||
tests_unitaires();
|
||
|
||
FILE* fichier = fopen("pos_t_x_y.dat","r");
|
||
... | ... | |
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},
|
||
... | ... | |
double F[4][4] = {{1, 0, dt, 0},
|
||
{0, 1, 0, dt},
|
||
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
{0, 0, 0, 1}};
|
||
double FP[4][4];
|
||
double FPFtransposee[4][4];
|
||
double HP[4][4];
|
||
double HPHT[4][4];
|
||
double HPHTplusR[4][4];
|
||
double INV_HPHTplusR[2][2];
|
||
double PHtransposee[4][2];
|
||
double FX[4][1];
|
||
double XplusKHXmoinsxy[4][1];
|
||
|
||
double HX[2][1];
|
||
double HXmoinsxy[2][1];
|
||
double KHXmoinsxy[4][1];
|
||
|
||
double KH[4][4];
|
||
double KHP[4][4];
|
||
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &x, &y)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
|
||
|
||
if (cpt ==0)
|
||
|
||
if (cpt == 0)
|
||
{
|
||
t0=t;x0=x;y0=y;
|
||
x=x-x0;y=y-y0;
|
||
... | ... | |
else
|
||
{
|
||
t -= t0;x -= x0;y -= y0;
|
||
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
double xy[2][1] = {{x},{y}};
|
||
debug=1; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
|
||
// X = F*X
|
||
// X = F*X
|
||
Mul_Mat_Mat(4,4,F,4,1,X, FX);
|
||
for(i = 0;i < 4;i++){
|
||
X[i][0] = FX[i][0];
|
||
}
|
||
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
|
||
//P = F*P*F'+Q;
|
||
//P = F*P*F'+Q;
|
||
Mul_Mat_Mat(4,4,F,4,4,P, FP);
|
||
Mul_Mat_Mat(4,4,FP,4,4,FT, FPFtransposee);
|
||
Add_Mat_Mat(4,4,FPFtransposee,4,4,Q, P);
|
||
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);
|
||
Mul_Mat_Mat(4,4,P,4,2,HT, PHtransposee);
|
||
Mul_Mat_Mat(2,4,H,4,4,P, HP);
|
||
Mul_Mat_Mat(2,4,HP,4,2,HT, HPHT);
|
||
Add_Mat_Mat(2,2,HPHT,2,2,R, HPHTplusR);
|
||
Mul_Mat_Mat(2,4,HP,4,2,HT, HPHT);
|
||
Inverse_Mat_22(2,2,HPHTplusR, INV_HPHTplusR);
|
||
Mul_Mat_Mat(4,2,PHtransposee,2,2,INV_HPHTplusR, K);
|
||
Plot_Mat(K,"K = ");
|
||
|
||
//X = X + K*([xb(i);yb(i)]-H*X);
|
||
//X = X + K*([xb(i);yb(i)]-H*X);
|
||
Mul_Mat_Mat(2,4,H,4,1,X, HX);
|
||
Sub_Mat_Mat(2,1,xy,2,1,HX, HXmoinsxy);
|
||
Mul_Mat_Mat(4,2,K,2,1,HXmoinsxy, KHXmoinsxy);
|
||
Add_Mat_Mat(4,1,X,4,1,KHXmoinsxy, XplusKHXmoinsxy);
|
||
|
||
for(i=0;i<4;i++){
|
||
X[i][0] = XplusKHXmoinsxy[i][0];
|
||
|
||
}
|
||
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
// P = P - K*H*P;
|
||
// P = P - K*H*P;
|
||
Mul_Mat_Mat(4,2,K,2,4,H, KH);
|
||
Mul_Mat_Mat(4,4,KH,4,4,P, KHP);
|
||
Sub_Mat_Mat(4,4,P,4,4,KHP,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
Implantation des opérations sur les matrices et de l'itération du filtre de Kalman et test (code fonctionnel)