The following snippet code shows how to run a Kalman Filter (KF) using the implementation provided by the library.
19 #include <Eigen/Dense>
22 using namespace Eigen;
31 std::unique_ptr<GaussianPrediction> prediction,
32 std::unique_ptr<GaussianCorrection> correction,
33 std::size_t simulation_steps
36 predicted_state_(initial_state.dim_linear, initial_state.dim_circular),
37 corrected_state_(initial_state),
38 simulation_steps_(simulation_steps)
42 bool run_condition()
override
44 if (step_number() < simulation_steps_)
51 bool initialization_step()
override
57 void filtering_step()
override
59 prediction().predict(corrected_state_, predicted_state_);
60 correction().freeze_measurements();
61 correction().correct(predicted_state_, corrected_state_);
67 std::vector<std::string> log_file_names(
const std::string& folder_path,
const std::string& file_name_prefix)
override
69 return {folder_path +
"/" + file_name_prefix +
"_pred_mean",
70 folder_path +
"/" + file_name_prefix +
"_cor_mean"};
76 logger(predicted_state_.mean().transpose(), corrected_state_.mean().transpose());
84 std::size_t simulation_steps_;
88 int main(
int argc,
char* argv[])
90 std::cout <<
"Running a KF filter on a simulated target." << std::endl;
92 const bool write_to_file = (argc > 1 ? std::string(argv[1]) ==
"ON" :
false);
94 std::cout <<
"Data is logged in the test folder with prefix testKF." << std::endl;
98 Vector4d initial_simulated_state(10.0f, 0.0f, 10.0f, 0.0f);
99 std::size_t simulation_time = 100;
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;
121 double tilde_q = 10.0f;
128 std::unique_ptr<KFPrediction> kf_prediction = utils::make_unique<KFPrediction>(std::move(wna));
137 std::unique_ptr<SimulatedStateModel> simulated_state_model = utils::make_unique<SimulatedStateModel>(std::move(target_model), initial_simulated_state, simulation_time);
140 simulated_state_model->enable_log(
"./",
"testKF");
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);
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);
152 simulated_linear_sensor->enable_log(
"./",
"testKF");
155 std::unique_ptr<KFCorrection> kf_correction = utils::make_unique<KFCorrection>(std::move(simulated_linear_sensor));
159 std::cout <<
"Constructing Kalman filter..." << std::flush;
161 KFSimulation kf(initial_state, std::move(kf_prediction), std::move(kf_correction), simulation_time);
164 kf.enable_log(
"./",
"testKF");
166 std::cout <<
"done!" << std::endl;
170 std::cout <<
"Booting Kalman filter..." << std::flush;
174 std::cout <<
"completed!" << std::endl;
179 std::cout <<
"Running Kalman filter..." << std::flush;
183 std::cout <<
"waiting..." << std::flush;
188 std::cout <<
"completed!" << std::endl;