example.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <fstream>
4 
5 #include <cmath>
6 
7 // Example of Extented Kalman Filter
8 #include "plane.h"
9 
10 using namespace std;
11 using namespace Kalman;
12 
13 // Simple uniform distribution of zero mean and unit variance
14 float uniform(void)
15 {
16  return((((float)rand())/(RAND_MAX-1) - 0.5f)* 3.464101615138f);
17 }
18 
19 // Simple approximation of normal dist. by the sum of uniform dist.
20 float normal()
21 {
22  int n = 6;
23  int i;
24  float temp = 0.0;
25 
26  for(i = 0; i < n; i++)
27  temp += uniform();
28  temp /= sqrt((float)n);
29  return temp;
30 }
31 
32 
33 int main() {
34 
35  ifstream dataInput;
36  ofstream dataOutput;
37  std::string tmpStr;
38 
39  const unsigned NTRY = 500;
40  const unsigned n = 4; //nb states
41  const unsigned m = 2; //nb measures
42 
43  // Matlab format
44  selectKVectorContext(createKVectorContext(" ", "[ ", " ];", 4));
45  selectKMatrixContext(createKMatrixContext(" ", " ;\n ", "[ ", " ];", 4));
46 
47  cPlaneEKF filter;
48 
49  static const double _P0[] = {100.0*100.0, 0.0, 0.0, 0.0,
50  0.0, 10.0*10.0, 0.0, 0.0,
51  0.0, 0.0, 25.0*25.0, 0.0,
52  0.0, 0.0, 0.0, 10.0*10.0};
53 
54  Vector x(n);
55  Matrix P0(n, n, _P0);
56 
57  Vector F(NTRY);
58  Matrix Measure(m,NTRY);
59 
60  dataInput.open("../Matlab/data.m",ifstream::in);
61  dataOutput.open("../Matlab/trajectory_udu_load.m", ofstream::out | ofstream::trunc);
62 
63  if (dataInput.fail())
64  {
65  cout<<"Unable to open input file!"<<endl;
66  return 0;
67  }
68 
69  if (dataOutput.fail())
70  {
71  cout<<"Unable to open output file!"<<endl;
72  return 0;
73  }
74 
75  cout<<"Loading inputs and measures from file <data.m>."<<endl;
76 
77  //Read the inputs vector. This vector have been generated by the Matlab script <generation.m>
78  dataInput>>tmpStr;
79  dataInput>>tmpStr;
80  dataInput>>F;
81 
82  if (dataInput.fail())
83  {
84  cout<<"IO error!"<<endl;
85  return 0;
86  }
87 
88  //Output as a Matlab colonn vector. This will be used by the Matlab script <result.m>
89  selectKVectorContext(createKVectorContext(";", "[ ", " ];", 4));
90 
91 
92  //Read the measures matrix. This matrix have been generated by the Matlab script <generation.m>
93  dataInput>>tmpStr;
94  dataInput>>tmpStr;
95  dataInput>>tmpStr;
96  dataInput>>Measure;
97 
98  if (dataInput.fail())
99  {
100  cout<<"IO error!"<<endl;
101  return 0;
102  }
103 
104  unsigned i=1, j=1;
105  Vector z(m);
106 
107  //Initiale estimate
108  cout<<"angle: "<<Measure(1,1)<<"rayon: "<<Measure(2,1)<<endl;
109  x(1) = cos(Measure(1,1))*Measure(2,1);
110  x(2) = 60;
111  x(3) = sin(Measure(1,1))*Measure(2,1);
112  x(4) = 0;
113 
114  filter.init(x, P0);
115 
116  cout << "xp(" << ":," << i<<") = " << filter.getX()<<endl;
117 
118  dataOutput<<"trajectory_udu(" << ":," << i <<") = " << filter.getX()<<endl;
119 
120  for (i = 2; i <= NTRY; ++i)
121  {
122  // filter
123  for(j = 1; j <= m; j++)
124  z(j) = Measure(j,i);
125 
126  Vector u(1, F(i));
127 
128  filter.step(u, z);
129 
130  cout << "xp(" << ":," << i<<") = " << filter.getX()<<endl;
131  dataOutput<<"trajectory_udu(" << ":," << i<<") = " << filter.getX()<<endl;
132  }
133 
134 
135  dataOutput.close();
136  dataInput.close();
137 
138  return EXIT_SUCCESS;
139 }
KVectorContext createKVectorContext(std::string elemDelim=" ", std::string startDelim="", std::string endDelim="", unsigned prec=4)
Creates a vector printing context.
Definition: kstatics.cpp:85
Definition: plane.h:6
float uniform(void)
Definition: example.cpp:14
void step(Vector &u_, const Vector &z_)
Makes one prediction-correction step.
KMatrixContext createKMatrixContext(std::string elemDelim=" ", std::string rowDelim="\n", std::string startDelim="", std::string endDelim="", unsigned prec=4)
Creates a matrix printing context.
Definition: kstatics.cpp:144
void init(Vector &x_, Matrix &P_)
Sets initial conditions for the Kalman Filter.
KVectorContext selectKVectorContext(KVectorContext c)
Selects a vector printing context as the current context.
Definition: kstatics.cpp:103
const Vector & getX() const
Returns the corrected state (a posteriori state estimate).
float normal()
Definition: example.cpp:20
KMatrixContext selectKMatrixContext(KMatrixContext c)
Selects a matrix printing context as the current context.
Definition: kstatics.cpp:164
int main()
Definition: example.cpp:33


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45