test_discrete_filter.cpp
Go to the documentation of this file.
1 // $Id: test_discrete_filter.cpp tdelaet $
2 // Copyright (C) 2007 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 // Include file with properties
23 #include "../mobile_robot_wall_cts.h"
24 
25 #include <filter/histogramfilter.h>
26 
28 
29 #include <pdf/discretepdf.h>
31 #include "../mobile_robot.h"
32 
33 #include <iostream>
34 #include <fstream>
35 
36 using namespace MatrixWrapper;
37 using namespace BFL;
38 using namespace std;
39 
40 
41 
42 /* The purpose of this program is to construct a histogram filter for a very
43  simple example, which is a 1d mobile robot localisation.
44  Furthermore the mobile robot is equipped with an ultrasonic sensor which
45  directly measures the robot's position.
46 
47  The necessary SYSTEM MODEL is discrete.
48 
49  The MEASUREMENT MODEL is a home made one which takes into account the discrete nature
50  of the state under consideration and some extra gaussian measurement noise
51 
52 */
53 
54 
55 int main(int argc, char** argv)
56 {
57  cerr << "==================================================" << endl
58  << "Test of histogram filter" << endl
59  << "1D Mobile robot localisation example" << endl
60  << "==================================================" << endl;
61 
62 
63  int num_states = 20;
64  int num_cond_args = 1;
65  /****************************
66  * Discrete system model *
67  ***************************/
68  int cond_arg_dims[num_cond_args];
69  cond_arg_dims[0] = num_states;
70  DiscreteConditionalPdf sys_pdf(num_states,num_cond_args,cond_arg_dims); // no inputs
71  std::vector<int> cond_args(num_cond_args);
72  double prob_diag = 0.9;
73  double prob_nondiag = (1-prob_diag)/(num_states-1);
74  for (int state_kMinusOne = 0 ; state_kMinusOne < num_states ; state_kMinusOne++)
75  {
76  cond_args[0] = state_kMinusOne;
77  for (int state_k = 0 ; state_k < num_states ; state_k++)
78  {
79  if (state_kMinusOne == state_k) sys_pdf.ProbabilitySet(prob_diag,state_k,cond_args);
80  else sys_pdf.ProbabilitySet(prob_nondiag,state_k,cond_args);
81  }
82  }
83  DiscreteSystemModel sys_model(&sys_pdf);
84  std::cerr << "discrete system model created" << std::endl;
85 
86  /*********************************
87  * Initialise measurement model *
88  ********************************/
89  // Construct the measurement noise (a scalar in this case)
90  ColumnVector measNoise_Mu(MEAS_SIZE);
91  measNoise_Mu(1) = 0.0;
92 
93  SymmetricMatrix meas_noise_Cov(MEAS_SIZE);
94  meas_noise_Cov(1,1) = SIGMA_MEAS_NOISE;
95  ColumnVector meas_noise_Mu(1);
96  meas_noise_Mu(1) = MU_MEAS_NOISE;
97  Gaussian measurement_Uncertainty(meas_noise_Mu, meas_noise_Cov);
98 
99  // create the model
100  ConditionalUniformMeasPdf1d meas_pdf(measurement_Uncertainty);
102  std::cout << "measurement model created" << std::endl;
103 
104  /****************************
105  * Uniform prior DENSITY *
106  ***************************/
107  DiscretePdf prior(num_states); //equal probability is set for all states
108  std::cout << "uniform prior density created" << std::endl;
109 
110  /******************************
111  * Construction of the Filter *
112  ******************************/
113  HistogramFilter<ColumnVector> filter(&prior);
114  DiscretePdf * prior_test = filter.PostGet();
115  std::cout << "filter created" << std::endl;
116 
117  /***************************
118  * Initialise MOBILE ROBOT *
119  **************************/
120  // Model of mobile robot in world with one wall
121  // The model is used to simultate the distance measurements.
122  MobileRobot mobile_robot;
123  ColumnVector input(2);
124  input(1) = 0.1;
125  input(2) = 0.0;
126 
127 
128  /*******************
129  * ESTIMATION LOOP *
130  *******************/
131  cout << "MAIN: Starting estimation" << endl;
132  unsigned int time_step;
133  for (time_step = 0; time_step < NUM_TIME_STEPS; time_step++)
134  {
135  // DO ONE STEP WITH MOBILE ROBOT
136  mobile_robot.Move(input);
137 
138  // DO ONE MEASUREMENT
139  // TODO: mobile_robot returns negative measurements
140  ColumnVector measurement = mobile_robot.Measure();
141  measurement(1) = -measurement(1);
142 
143  // UPDATE FILTER
144  filter.Update(&sys_model,&meas_model,measurement);
145  //filter.Update(&sys_model);
146  //filter.Update(&meas_model,measurement);
147 
148  } // estimation loop
149 
150 
151 
152  DiscretePdf * posterior = filter.PostGet();
153  cout << "After " << time_step+1 << " timesteps " << endl;
154  cout << " Posterior probabilities = " << endl;
155  for (int state = 0 ; state< posterior->NumStatesGet() ; state++) cout << state << ": " << (double)(posterior->ProbabilityGet(state)) << endl;
156 
157 
158  cout << "======================================================" << endl
159  << "End of the Histogram filter for 1D mobile robot localisation" << endl
160  << "======================================================"
161  << endl;
162 
163 
164  return 0;
165 }
Class representing a PDF on a discrete variable.
Definition: discretepdf.h:34
#define MU_MEAS_NOISE
#define SIGMA_MEAS_NOISE
Class representing the histogram filter.
This is a class simulating a mobile robot.
Definition: mobile_robot.h:47
Probability ProbabilityGet(const int &state) const
Implementation of virtual base class method.
MatrixWrapper::ColumnVector Measure()
#define MEAS_SIZE
Class representing Gaussian (or normal density)
Definition: gaussian.h:27
unsigned int NumStatesGet() const
Get the number of discrete States.
int main(int argc, char **argv)
virtual DiscretePdf * PostGet()
Get Posterior density.
void Move(MatrixWrapper::ColumnVector inputs)
Abstract Class representing all FULLY Discrete Conditional PDF&#39;s.
Class for discrete System Models.
virtual bool Update(SystemModel< int > *const sysmodel, const int &u, MeasurementModel< MeasVar, int > *const measmodel, const MeasVar &z, const int &s)
Full Update (system with inputs/sensing params)
void ProbabilitySet(const double &prob, const int &input, const std::vector< int > &condargs) const
Set the probability (Typical for discrete Pdf&#39;s)
#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