test_linear_kalman.cpp
Go to the documentation of this file.
1 // $Id: kalman_mobile.cpp 5925 2006-03-14 21:23:49Z tdelaet $
2 // Copyright (C) 2006 Tinne De Laet <first dot last at mech dot kuleuven dot be>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation; either version 2.1 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 /* Demonstration program for the Bayesian Filtering Library.
19  Mobile robot localization with respect to wall with different possibilities for filter
20 */
21 
22 
24 
27 
30 
31 #include "../mobile_robot.h"
32 
33 #include <iostream>
34 #include <fstream>
35 
36 // Include file with properties
37 #include "../mobile_robot_wall_cts.h"
38 
39 using namespace MatrixWrapper;
40 using namespace BFL;
41 using namespace std;
42 
43 
44 
45 /* The purpose of this program is to construct a kalman filter for the problem
46  of localisation of a mobile robot equipped with an ultrasonic sensor.
47  In this case the orientation is known, which simplifies the model considerably:
48  The system model will become linear.
49  The ultrasonic measures the distance to the wall (it can be switched off:
50  see mobile_robot_wall_cts.h)
51 
52  The necessary SYSTEM MODEL is:
53 
54  x_k = x_{k-1} + v_{k-1} * cos(theta) * delta_t
55  y_k = y_{k-1} + v_{k-1} * sin(theta) * delta_t
56 
57  The used MEASUREMENT MODEL:
58  measuring the (perpendicular) distance z to the wall y = ax + b
59 
60  set WALL_CT = 1/sqrt(pow(a,2) + 1)
61  z = WALL_CT * a * x - WALL_CT * y + WALL_CT * b + GAUSSIAN_NOISE
62  or Z = H * X_k + J * U_k
63 
64  where
65 
66  H = [ WALL_CT * a - WALL_CT 0 ]
67  and GAUSSIAN_NOISE = N((WALL_CT * b), SIGMA_MEAS_NOISE)
68 
69 */
70 
71 
72 int main(int argc, char** argv)
73 {
74  cerr << "==================================================" << endl
75  << "Test of kalman filter" << endl
76  << "Mobile robot localisation example" << endl
77  << "==================================================" << endl;
78 
79 
80  /****************************
81  * Linear system model *
82  ***************************/
83 
84  // Create the matrices A and B for the linear system model
85  Matrix A(2,2);
86  A(1,1) = 1.0;
87  A(1,2) = 0.0;
88  A(2,1) = 0.0;
89  A(2,2) = 1.0;
90  Matrix B(2,2);
91  B(1,1) = cos(0.8);
92  B(1,2) = 0.0;
93  B(2,1) = sin(0.8);
94  B(2,2) = 0.0;
95 
96  vector<Matrix> AB(2);
97  AB[0] = A;
98  AB[1] = B;
99 
100  // create gaussian
101  ColumnVector sysNoise_Mu(2);
102  sysNoise_Mu(1) = MU_SYSTEM_NOISE_X;
103  sysNoise_Mu(2) = MU_SYSTEM_NOISE_Y;
104 
105  SymmetricMatrix sysNoise_Cov(2);
106  sysNoise_Cov = 0.0;
107  sysNoise_Cov(1,1) = SIGMA_SYSTEM_NOISE_X;
108  sysNoise_Cov(1,2) = 0.0;
109  sysNoise_Cov(2,1) = 0.0;
110  sysNoise_Cov(2,2) = SIGMA_SYSTEM_NOISE_Y;
111 
112  Gaussian system_Uncertainty(sysNoise_Mu, sysNoise_Cov);
113 
114  // create the model
115  LinearAnalyticConditionalGaussian sys_pdf(AB, system_Uncertainty);
117 
118 
119  /*********************************
120  * Initialise measurement model *
121  ********************************/
122 
123  // create matrix H for linear measurement model
124  Matrix H(1,2);
125  double wall_ct = 2/(sqrt(pow(RICO_WALL,2.0) + 1));
126  H = 0.0;
127  H(1,1) = wall_ct * RICO_WALL;
128  H(1,2) = 0 - wall_ct;
129 
130  // Construct the measurement noise (a scalar in this case)
131  ColumnVector measNoise_Mu(1);
132  measNoise_Mu(1) = MU_MEAS_NOISE;
133 
134  SymmetricMatrix measNoise_Cov(1);
135  measNoise_Cov(1,1) = SIGMA_MEAS_NOISE;
136  Gaussian measurement_Uncertainty(measNoise_Mu, measNoise_Cov);
137 
138  // create the model
139  LinearAnalyticConditionalGaussian meas_pdf(H, measurement_Uncertainty);
141 
142 
143  /****************************
144  * Linear prior DENSITY *
145  ***************************/
146  // Continuous Gaussian prior (for Kalman filters)
147  ColumnVector prior_Mu(2);
148  prior_Mu(1) = PRIOR_MU_X;
149  prior_Mu(2) = PRIOR_MU_Y;
150  SymmetricMatrix prior_Cov(2);
151  prior_Cov(1,1) = PRIOR_COV_X;
152  prior_Cov(1,2) = 0.0;
153  prior_Cov(2,1) = 0.0;
154  prior_Cov(2,2) = PRIOR_COV_Y;
155  Gaussian prior(prior_Mu,prior_Cov);
156 
157 
158 
159 
160  /******************************
161  * Construction of the Filter *
162  ******************************/
163  ExtendedKalmanFilter filter(&prior);
164 
165 
166 
167 
168  /***************************
169  * initialise MOBILE ROBOT *
170  **************************/
171  // Model of mobile robot in world with one wall
172  // The model is used to simultate the distance measurements.
173  MobileRobot mobile_robot;
174  ColumnVector input(2);
175  input(1) = 0.1;
176  input(2) = 0.0;
177 
178  /*******************
179  * ESTIMATION LOOP *
180  *******************/
181  cout << "MAIN: Starting estimation" << endl;
182  unsigned int time_step;
183  for (time_step = 0; time_step < NUM_TIME_STEPS-1; time_step++)
184  {
185  // DO ONE STEP WITH MOBILE ROBOT
186  mobile_robot.Move(input);
187 
188  // DO ONE MEASUREMENT
189  ColumnVector measurement = mobile_robot.Measure();
190 
191  // UPDATE FILTER
192  filter.Update(&sys_model,input,&meas_model,measurement);
193  } // estimation loop
194 
195 
196 
197  Pdf<ColumnVector> * posterior = filter.PostGet();
198  cout << "After " << time_step+1 << " timesteps " << endl;
199  cout << " Posterior Mean = " << endl << posterior->ExpectedValueGet() << endl
200  << " Covariance = " << endl << posterior->CovarianceGet() << "" << endl;
201 
202 
203  cout << "======================================================" << endl
204  << "End of the Kalman filter for mobile robot localisation" << endl
205  << "======================================================"
206  << endl;
207 
208 
209  return 0;
210 }
int main(int argc, char **argv)
#define MU_MEAS_NOISE
#define SIGMA_MEAS_NOISE
Class PDF: Virtual Base class representing Probability Density Functions.
Definition: pdf.h:53
This is a class simulating a mobile robot.
Definition: mobile_robot.h:47
virtual MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
MatrixWrapper::ColumnVector Measure()
Class representing Gaussian (or normal density)
Definition: gaussian.h:27
#define MU_SYSTEM_NOISE_X
#define RICO_WALL
#define PRIOR_COV_Y
virtual T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
void Move(MatrixWrapper::ColumnVector inputs)
#define PRIOR_MU_X
virtual Gaussian * PostGet()
Get Posterior density.
Class for linear analytic systemmodels with additive gaussian noise.
#define PRIOR_COV_X
Class for linear analytic measurementmodels with additive gaussian noise.
#define SIGMA_SYSTEM_NOISE_X
#define PRIOR_MU_Y
virtual bool Update(SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Full Update (system with inputs/sensing params)
Definition: filter.h:56
#define MU_SYSTEM_NOISE_Y
#define SIGMA_SYSTEM_NOISE_Y
#define NUM_TIME_STEPS


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Mon Jun 10 2019 12:47:59