Bayes Filters Library
The first Kalman filter

The following snippet code shows how to run a Kalman Filter (KF) using the implementation provided by the library.

1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
15 #include <BayesFilters/utils.h>
16 
17 #include <string>
18 
19 #include <Eigen/Dense>
20 
21 using namespace bfl;
22 using namespace Eigen;
23 
24 
25 class KFSimulation : public GaussianFilter
26 {
27 public:
28  KFSimulation
29  (
30  Gaussian& initial_state,
31  std::unique_ptr<GaussianPrediction> prediction,
32  std::unique_ptr<GaussianCorrection> correction,
33  std::size_t simulation_steps
34  ) noexcept :
35  GaussianFilter(std::move(prediction), std::move(correction)),
36  predicted_state_(initial_state.dim_linear, initial_state.dim_circular),
37  corrected_state_(initial_state),
38  simulation_steps_(simulation_steps)
39  { }
40 
41 protected:
42  bool run_condition() override
43  {
44  if (step_number() < simulation_steps_)
45  return true;
46  else
47  return false;
48  }
49 
50 
51  bool initialization_step() override
52  {
53  return true;
54  }
55 
56 
57  void filtering_step() override
58  {
59  prediction().predict(corrected_state_, predicted_state_);
60  correction().freeze_measurements();
61  correction().correct(predicted_state_, corrected_state_);
62 
63  log();
64  }
65 
66 
67  std::vector<std::string> log_file_names(const std::string& folder_path, const std::string& file_name_prefix) override
68  {
69  return {folder_path + "/" + file_name_prefix + "_pred_mean",
70  folder_path + "/" + file_name_prefix + "_cor_mean"};
71  }
72 
73 
74  void log() override
75  {
76  logger(predicted_state_.mean().transpose(), corrected_state_.mean().transpose());
77  }
78 
79 private:
80  Gaussian predicted_state_;
81 
82  Gaussian corrected_state_;
83 
84  std::size_t simulation_steps_;
85 };
86 
87 
88 int main(int argc, char* argv[])
89 {
90  std::cout << "Running a KF filter on a simulated target." << std::endl;
91 
92  const bool write_to_file = (argc > 1 ? std::string(argv[1]) == "ON" : false);
93  if (write_to_file)
94  std::cout << "Data is logged in the test folder with prefix testKF." << std::endl;
95 
96 
97  /* A set of parameters needed to run a Kalman filter in a simulated environment. */
98  Vector4d initial_simulated_state(10.0f, 0.0f, 10.0f, 0.0f);
99  std::size_t simulation_time = 100;
100 
101 
102  /* Step 1 - Initialization */
103 
104  Gaussian initial_state(4);
105  Vector4d initial_mean(4.0f, 0.04f, 15.0f, 0.4f);
106  Matrix4d initial_covariance;
107  initial_covariance << pow(0.05, 2), 0, 0, 0,
108  0, pow(0.05, 2), 0, 0,
109  0, 0, pow(0.01, 2), 0,
110  0, 0, 0, pow(0.01, 2);
111  initial_state.mean() = initial_mean;
112  initial_state.covariance() = initial_covariance;
113 
114 
115  /* Step 2 - Prediction */
116 
117  /* Step 2.1 - Define the state model. */
118 
119  /* Initialize a white noise acceleration state model. */
120  double T = 1.0f;
121  double tilde_q = 10.0f;
122 
123  std::unique_ptr<LinearStateModel> wna = utils::make_unique<WhiteNoiseAcceleration>(WhiteNoiseAcceleration::Dim::TwoD, T, tilde_q);
124 
125  /* Step 2.2 - Define the prediction step. */
126 
127  /* Initialize the Kalman filter prediction step and pass the ownership of the state model. */
128  std::unique_ptr<KFPrediction> kf_prediction = utils::make_unique<KFPrediction>(std::move(wna));
129 
130 
131  /* Step 3 - Correction */
132 
133  /* Step 3.1 - Define where the measurement are originated from (simulated in this case). */
134 
135  /* Initialize simulated target model with a white noise acceleration. */
136  std::unique_ptr<StateModel> target_model = utils::make_unique<WhiteNoiseAcceleration>(WhiteNoiseAcceleration::Dim::TwoD, T, tilde_q);
137  std::unique_ptr<SimulatedStateModel> simulated_state_model = utils::make_unique<SimulatedStateModel>(std::move(target_model), initial_simulated_state, simulation_time);
138 
139  if (write_to_file)
140  simulated_state_model->enable_log("./", "testKF");
141 
142  /* Step 3.2 - Initialize a measurement model (a linear sensor reading x and y coordinates). */
143  double sigma_x = 10.0;
144  double sigma_y = 10.0;
145  Eigen::MatrixXd R(2, 2);
146  R << std::pow(sigma_x, 2.0), 0.0,
147  0.0, std::pow(sigma_y, 2.0);
148 
149  std::unique_ptr<LinearMeasurementModel> simulated_linear_sensor = utils::make_unique<SimulatedLinearSensor>(std::move(simulated_state_model), SimulatedLinearSensor::LinearMatrixComponent{ 4, std::vector<std::size_t>{ 0, 2 } }, R);
150 
151  if (write_to_file)
152  simulated_linear_sensor->enable_log("./", "testKF");
153 
154  /* Step 3.3 - Initialize the Kalman filter correction step and pass the ownership of the measurement model. */
155  std::unique_ptr<KFCorrection> kf_correction = utils::make_unique<KFCorrection>(std::move(simulated_linear_sensor));
156 
157 
158  /* Step 4 - Assemble the Kalman filter. */
159  std::cout << "Constructing Kalman filter..." << std::flush;
160 
161  KFSimulation kf(initial_state, std::move(kf_prediction), std::move(kf_correction), simulation_time);
162 
163  if (write_to_file)
164  kf.enable_log("./", "testKF");
165 
166  std::cout << "done!" << std::endl;
167 
168 
169  /* Step 5 - Boot the filter. */
170  std::cout << "Booting Kalman filter..." << std::flush;
171 
172  kf.boot();
173 
174  std::cout << "completed!" << std::endl;
175 
176 
177  /* Step 6 - Run the filter and wait until it is closed. */
178  /* Note that since this is a simulation, the filter will end upon simulation termination. */
179  std::cout << "Running Kalman filter..." << std::flush;
180 
181  kf.run();
182 
183  std::cout << "waiting..." << std::flush;
184 
185  if (!kf.wait())
186  return EXIT_FAILURE;
187 
188  std::cout << "completed!" << std::endl;
189 
190  return EXIT_SUCCESS;
191 }
bfl::GaussianFilter
Definition: GaussianFilter.h:21
bfl::LinearModel::LinearMatrixComponent
std::pair< std::size_t, std::vector< std::size_t > > LinearMatrixComponent
Pair of data representing.
Definition: LinearModel.h:37
GaussianFilter.h
SimulatedLinearSensor.h
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::WhiteNoiseAcceleration::Dim::TwoD
@ TwoD
utils.h
SimulatedStateModel.h
bfl::Gaussian
Definition: Gaussian.h:22
Gaussian.h
KFCorrection.h
WhiteNoiseAcceleration.h
KFPrediction.h