00001 #include <cstdlib>
00002 #include <iostream>
00003 #include <fstream>
00004 
00005 #include <cmath>
00006 
00007 
00008 #include "plane.h"
00009 
00010 using namespace std;
00011 using namespace Kalman;
00012 
00013 
00014 float uniform(void)
00015 {
00016    return((((float)rand())/(RAND_MAX-1) - 0.5f)* 3.464101615138f);
00017 }
00018 
00019 
00020 float normal()
00021 {
00022   int n = 6;
00023   int i;
00024   float temp = 0.0;
00025 
00026   for(i = 0; i < n; i++) 
00027     temp += uniform();
00028   temp /= sqrt((float)n);
00029   return temp;
00030 }
00031 
00032 
00033 int main() {
00034 
00035         ifstream dataInput;
00036         ofstream dataOutput;
00037         std::string tmpStr;
00038 
00039         const unsigned NTRY = 500;
00040         const unsigned n = 4;   
00041         const unsigned m = 2;   
00042 
00043         
00044         selectKVectorContext(createKVectorContext(" ", "[ ", " ];", 4));
00045         selectKMatrixContext(createKMatrixContext(" ", " ;\n  ", "[ ", " ];", 4));
00046 
00047         cPlaneEKF filter;
00048 
00049         static const double _P0[] = {100.0*100.0, 0.0, 0.0, 0.0,
00050                                                                  0.0, 10.0*10.0, 0.0, 0.0,
00051                                                                  0.0, 0.0, 25.0*25.0, 0.0,
00052                                                                  0.0, 0.0, 0.0, 10.0*10.0}; 
00053 
00054         Vector x(n);
00055         Matrix P0(n, n, _P0);
00056 
00057         Vector F(NTRY);
00058         Matrix Measure(m,NTRY);
00059 
00060         dataInput.open("../Matlab/data.m",ifstream::in);
00061         dataOutput.open("../Matlab/trajectory_udu_load.m", ofstream::out | ofstream::trunc);
00062 
00063         if (dataInput.fail())
00064         {
00065                 cout<<"Unable to open input file!"<<endl;
00066                 return 0;
00067         }
00068 
00069         if (dataOutput.fail())
00070         {
00071                 cout<<"Unable to open output file!"<<endl;
00072                 return 0;
00073         }
00074 
00075         cout<<"Loading inputs and measures from file <data.m>."<<endl;
00076 
00077         
00078         dataInput>>tmpStr;
00079         dataInput>>tmpStr;
00080         dataInput>>F;
00081 
00082         if (dataInput.fail())
00083         {
00084                 cout<<"IO error!"<<endl;
00085                 return 0;
00086         }
00087 
00088         
00089         selectKVectorContext(createKVectorContext(";", "[ ", " ];", 4));
00090 
00091 
00092         
00093         dataInput>>tmpStr;
00094         dataInput>>tmpStr;
00095         dataInput>>tmpStr;
00096         dataInput>>Measure;
00097 
00098         if (dataInput.fail())
00099         {
00100                 cout<<"IO error!"<<endl;
00101                 return 0;
00102         }
00103 
00104         unsigned i=1, j=1;
00105         Vector z(m);
00106 
00107         
00108         cout<<"angle: "<<Measure(1,1)<<"rayon: "<<Measure(2,1)<<endl;
00109         x(1) = cos(Measure(1,1))*Measure(2,1);
00110         x(2) = 60;
00111         x(3) = sin(Measure(1,1))*Measure(2,1);
00112         x(4) = 0;
00113 
00114         filter.init(x, P0);
00115 
00116         cout << "xp(" << ":," << i<<") = " << filter.getX()<<endl;
00117 
00118         dataOutput<<"trajectory_udu(" << ":," << i <<") = " << filter.getX()<<endl;
00119 
00120         for (i = 2; i <= NTRY; ++i) 
00121         {
00122                 
00123                 for(j = 1; j <= m; j++)
00124                         z(j) = Measure(j,i);
00125 
00126                 Vector u(1, F(i));
00127 
00128                 filter.step(u, z);
00129 
00130                 cout << "xp(" << ":," << i<<") = " << filter.getX()<<endl;
00131                 dataOutput<<"trajectory_udu(" << ":," << i<<") = " << filter.getX()<<endl;
00132         }
00133 
00134 
00135         dataOutput.close();
00136         dataInput.close();
00137 
00138         return EXIT_SUCCESS;
00139 }