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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 void outputVector(ofstream& out, aux::vector vec);
00067
00068 void outputMatrix(ofstream& out, aux::matrix mat);
00069
00070
00071
00072
00073 int main(int argc, const char* argv) {
00074
00075 MobileRobotUnscentedKalmanFilterModel model;
00076
00077
00078 MobileRobot robot(0.1, 5e-3);
00079
00080
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
00101 UnscentedKalmanFilter<unsigned int> filter(&model, x0);
00102
00103
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
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
00139 fmeas << t << '\t';
00140 outputVector(fmeas, meas);
00141 fmeas << endl;
00142
00143
00144 factual << t << '\t';
00145 outputVector(factual, actual);
00146 factual << endl;
00147
00148
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 }