The following snippet code shows how to run an Unscented Kalman Filter (KF) using the implementation provided by the library.
20 #include <Eigen/Dense>
23 using namespace Eigen;
32 std::unique_ptr<GaussianPrediction> prediction,
33 std::unique_ptr<GaussianCorrection> correction,
34 std::size_t simulation_steps
37 predicted_state_(initial_state.dim_linear, initial_state.dim_circular),
38 corrected_state_(initial_state),
39 simulation_steps_(simulation_steps)
43 bool run_condition()
override
45 if (step_number() < simulation_steps_)
52 bool initialization_step()
override
58 void filtering_step()
override
60 prediction().predict(corrected_state_, predicted_state_);
61 correction().freeze_measurements();
62 correction().correct(predicted_state_, corrected_state_);
68 std::vector<std::string> log_file_names(
const std::string& folder_path,
const std::string& file_name_prefix)
override
70 return {folder_path +
"/" + file_name_prefix +
"_pred_mean",
71 folder_path +
"/" + file_name_prefix +
"_cor_mean"};
77 logger(predicted_state_.mean().transpose(), corrected_state_.mean().transpose());
85 std::size_t simulation_steps_;
89 int main(
int argc,
char* argv[])
91 std::cout <<
"Running a UKF filter on a simulated target." << std::endl;
93 const bool write_to_file = (argc > 1 ? std::string(argv[1]) ==
"ON" :
false);
95 std::cout <<
"Data is logged in the test folder with prefix testUKF." << std::endl;
99 Vector4d initial_simulated_state(10.0f, 0.0f, 10.0f, 0.0f);
100 std::size_t simulation_time = 100;
109 std::size_t state_size = 4;
111 Vector4d initial_mean(4.0f, 0.04f, 15.0f, 0.4f);
112 Matrix4d initial_covariance;
113 initial_covariance << pow(0.05, 2), 0, 0, 0,
114 0, pow(0.05, 2), 0, 0,
115 0, 0, pow(0.01, 2), 0,
116 0, 0, 0, pow(0.01, 2);
117 initial_state.mean() = initial_mean;
118 initial_state.covariance() = initial_covariance;
127 double tilde_q = 10.0f;
134 std::unique_ptr<UKFPrediction> ukf_prediction = utils::make_unique<UKFPrediction>(std::move(wna), alpha, beta, kappa);
143 std::unique_ptr<SimulatedStateModel> simulated_state_model = utils::make_unique<SimulatedStateModel>(std::move(target_model), initial_simulated_state, simulation_time);
146 simulated_state_model->enable_log(
"./",
"testUKF");
149 double sigma_x = 10.0;
150 double sigma_y = 10.0;
151 Eigen::MatrixXd R(2, 2);
152 R << std::pow(sigma_x, 2.0), 0.0,
153 0.0, std::pow(sigma_y, 2.0);
155 std::unique_ptr<AdditiveMeasurementModel> simulated_linear_sensor = utils::make_unique<SimulatedLinearSensor>(std::move(simulated_state_model),
SimulatedLinearSensor::LinearMatrixComponent{ 4, std::vector<std::size_t>{ 0, 2 } }, R);
158 simulated_linear_sensor->enable_log(
"./",
"testUKF");
161 std::unique_ptr<UKFCorrection> ukf_correction = utils::make_unique<UKFCorrection>(std::move(simulated_linear_sensor), alpha, beta, kappa);
165 std::cout <<
"Constructing unscented Kalman filter..." << std::flush;
167 UKFSimulation ukf(initial_state, std::move(ukf_prediction), std::move(ukf_correction), simulation_time);
170 ukf.enable_log(
"./",
"testUKF");
172 std::cout <<
"done!" << std::endl;
176 std::cout <<
"Booting unscented Kalman filter..." << std::flush;
180 std::cout <<
"completed!" << std::endl;
185 std::cout <<
"Running unscented Kalman filter..." << std::flush;
189 std::cout <<
"waiting..." << std::flush;
194 std::cout <<
"completed!" << std::endl;