UnscentedKalmanFilterHarness.cpp

Go to the documentation of this file.
00001 #include "indii/ml/filter/UnscentedKalmanFilter.hpp"
00002 #include "indii/ml/aux/vector.hpp"
00003 #include "indii/ml/aux/matrix.hpp"
00004 
00005 #include "MobileRobotUnscentedKalmanFilterModel.hpp"
00006 #include "MobileRobot.hpp"
00007 
00008 #include <math.h>
00009 #include <iostream>
00010 #include <fstream>
00011 
00012 #define SYSTEM_SIZE 5
00013 #define SYSTEM_NOISE_SIZE 2
00014 #define MEAS_SIZE 1
00015 #define MEAS_NOISE_SIZE 1
00016 #define ACTUAL_SIZE 3
00017 #define STEPS 250
00018 
00019 using namespace std;
00020 using namespace indii::ml::filter;
00021 
00022 namespace aux = indii::ml::aux;
00023 
00024 /**
00025  * @file UnscentedKalmanFilterHarness.cpp
00026  *
00027  * Basic test of UnscentedKalmanFilter.
00028  *
00029  * Results are output into files as follows:
00030  *
00031  * @section actualUKF results/UnscentedKalmanFilterHarness_actual.out
00032  *
00033  * Actual state of the robot at each time. Columns are as follows:
00034  *
00035  * @li time
00036  * @li x coordinate
00037  * @li y coordinate
00038  * @li orientation (radians)
00039  *
00040  * @section measUKF results/UnscentedKalmanFilterHarness_meas.out
00041  * 
00042  * Measurement at each time step. Columns are as follows:
00043  *
00044  * @li time
00045  * @li measurement
00046  *
00047  * @section predUKF results/UnscentedKalmanFilterHarness_pred.out
00048  *
00049  * Predicted state at each time step. Columns are as follows:
00050  *
00051  * @li time
00052  * @li mean x coordinate
00053  * @li mean y coordinate
00054  * @li mean orientation
00055  * @li The remaining columns give the covariance matrix between the above
00056  * state variables.
00057  *
00058  * @section resultsUKF Results
00059  *
00060  * Results are as follows:
00061  *
00062  * \image html UnscentedKalmanFilterHarness.png "Results, c.f. BFL Tutorial Figures 3.2 and 3.3"
00063  * \image latex UnscentedKalmanFilterHarness.eps "Results, c.f. BFL Tutorial Figures 3.2 and 3.3"
00064  */
00065 
00066 void outputVector(ofstream& out, aux::vector vec);
00067 
00068 void outputMatrix(ofstream& out, aux::matrix mat);
00069 
00070 /**
00071  * Run tests
00072  */
00073 int main(int argc, const char* argv) {
00074   /* define model */
00075   MobileRobotUnscentedKalmanFilterModel model;
00076 
00077   /* set up robot simulator */
00078   MobileRobot robot(0.1, 5e-3);
00079 
00080   /* initial state */
00081   aux::vector mu(SYSTEM_SIZE);
00082   aux::symmetric_matrix sigma(SYSTEM_SIZE);
00083 
00084   mu.clear();
00085   mu(0) = -1.0;
00086   mu(1) = 1.0;
00087   mu(2) = 0.8;
00088   mu(3) = 0.1;
00089   mu(4) = 5e-3;
00090 
00091   sigma.clear();
00092   sigma(0,0) = 1.0;
00093   sigma(1,1) = 1.0;
00094   sigma(2,2) = 0.01;
00095   sigma(3,3) = 1e-6;
00096   sigma(4,4) = 1e-6;
00097 
00098   aux::GaussianPdf x0(mu, sigma);
00099 
00100   /* create filter */
00101   UnscentedKalmanFilter<unsigned int> filter(&model, x0);
00102 
00103   /* estimate and output results */
00104   aux::vector meas(MEAS_SIZE);
00105   aux::vector actual(ACTUAL_SIZE);
00106   aux::GaussianPdf pred(SYSTEM_SIZE);
00107   unsigned int t = 0;
00108 
00109   ofstream fmeas("results/UnscentedKalmanFilterHarness_meas.out");
00110   ofstream factual("results/UnscentedKalmanFilterHarness_actual.out");
00111   ofstream fpred("results/UnscentedKalmanFilterHarness_pred.out");
00112 
00113   /* output initial state */
00114   pred = filter.getFilteredState();
00115   actual = robot.getState();
00116 
00117   cerr << t << ' ';
00118 
00119   factual << t << '\t';
00120   outputVector(factual, actual);
00121   factual << endl;
00122 
00123   fpred << t << '\t';
00124   outputVector(fpred, pred.getExpectation());
00125   fpred << '\t';
00126   outputMatrix(fpred, pred.getCovariance());
00127   fpred << endl;
00128 
00129   for (t = 1; t <= STEPS; t++) {
00130       robot.move();
00131       meas = robot.measure();
00132       filter.filter(t, meas);
00133       pred = filter.getFilteredState();
00134       actual = robot.getState();
00135 
00136       cerr << t << ' ';
00137 
00138       /* output measurement */
00139       fmeas << t << '\t';
00140       outputVector(fmeas, meas);
00141       fmeas << endl;
00142 
00143       /* output actual state */
00144       factual << t << '\t';
00145       outputVector(factual, actual);
00146       factual << endl;
00147 
00148       /* output filtered state */
00149       fpred << t << '\t';
00150       outputVector(fpred, pred.getExpectation());
00151       fpred << '\t';
00152       outputMatrix(fpred, pred.getCovariance());
00153       fpred << endl;
00154   }
00155 
00156   fmeas.close();
00157   factual.close();
00158   fpred.close();
00159 
00160   return 0;
00161 }
00162 
00163 void outputVector(ofstream& out, aux::vector vec) {
00164   aux::vector::iterator iter, end;
00165   iter = vec.begin();
00166   end = vec.end();
00167   while (iter != end) {
00168     out << *iter;
00169     iter++;
00170     if (iter != end) {
00171       out << '\t';
00172     }
00173   }
00174 }
00175 
00176 void outputMatrix(ofstream& out, aux::matrix mat) {
00177   unsigned int i, j;
00178   for (j = 0; j < mat.size2(); j++) {
00179     for (i = 0; i < mat.size1(); i++) {
00180       out << mat(i,j);
00181       if (i != mat.size1() - 1 || j != mat.size2() - 1) {
00182         out << '\t';
00183       }
00184     }
00185   }
00186 }

Generated on Wed Dec 17 15:05:50 2008 for dysii Filtering Test Suite by  doxygen 1.5.3