/*! * \file vtl_engine.h * \brief Class that implements a Vector Tracking Loop (VTL) Kalman filter engine * \author Javier Arribas, 2022. jarribas(at)cttc.es * * ----------------------------------------------------------------------------- * * GNSS-SDR is a Global Navigation Satellite System software-defined receiver. * This file is part of GNSS-SDR. * * Copyright (C) 2010-2022 (see AUTHORS file for a list of contributors) * SPDX-License-Identifier: GPL-3.0-or-later * * ----------------------------------------------------------------------------- */ #include "vtl_engine.h" #include "iostream" #include using namespace std; Vtl_Engine::Vtl_Engine() { } Vtl_Engine::~Vtl_Engine() { } bool Vtl_Engine::vtl_loop(Vtl_Data& new_data) { //TODO: Implement main VTL loop here using arma::as_scalar; static uint64_t refSampleCounter = new_data.sample_counter; double delta_t_vtl = (new_data.sample_counter - refSampleCounter) / 5000000.0; refSampleCounter = new_data.sample_counter; // ################## Kalman filter initialization ###################################### //State variables int n_of_states=11; static arma::mat kf_P_x = arma::eye(n_of_states, n_of_states) * 1.0; //TODO: use a real value.; static arma::mat kf_x = arma::zeros(n_of_states, 1); arma::mat kf_dx = arma::zeros(n_of_states, 1); // covariances (static) kf_R = arma::zeros(2 * new_data.sat_number, 2 * new_data.sat_number); double kf_dt = delta_t_vtl; //0.05; kf_Q = arma::eye(n_of_states, n_of_states); kf_F = arma::eye(n_of_states, n_of_states); bool test = kf_F_fill(kf_F,kf_dt); //kf_H = arma::zeros(2 * new_data.sat_number, n_of_states); kf_y = arma::zeros(2 * new_data.sat_number, 1); kf_yerr = arma::zeros(2 * new_data.sat_number, 1); kf_xerr = arma::zeros(n_of_states, 1); kf_S = arma::zeros(2 * new_data.sat_number, 2 * new_data.sat_number); // kf_P_y innovation covariance matrix // ################## Kalman Tracking ###################################### static uint32_t counter = 0; //counter counter = counter + 1; //uint64_t //new_data.kf_state.print("new_data kf initial"); uint32_t closure_point=3; if (counter < closure_point) { // // receiver solution from rtklib_solver kf_dx=kf_x; kf_x(0) = new_data.rx_p(0); kf_x(1) = new_data.rx_p(1); kf_x(2) = new_data.rx_p(2); kf_x(3) = new_data.rx_v(0); kf_x(4) = new_data.rx_v(1); kf_x(5) = new_data.rx_v(2); kf_x(6) = 0; kf_x(7) = 0; kf_x(8) = 0; kf_x(9) = new_data.rx_dts(0) * SPEED_OF_LIGHT_M_S; kf_x(10) = new_data.rx_dts(1) * SPEED_OF_LIGHT_M_S; kf_dx=kf_x-kf_dx; kf_dx = kf_F * kf_dx; // state prediction } else { // receiver solution from previous KF step // kf_x(0) = x_u; // kf_x(1) = y_u; // kf_x(2) = z_u; // kf_x(3) = xDot_u; // kf_x(4) = yDot_u; // kf_x(5) = zDot_u; // kf_x(6) = xDot2_u; // kf_x(7) = yDot2_u; // kf_x(8) = zDot2_u; // kf_x(9) = cdeltat_u; // kf_x(10) = cdeltatDot_u; //kf_P_x = new_data.kf_P; } // State error Covariance Matrix Q (PVT) //careful, values for V and T could not be adecuate. kf_Q(0, 0) = 100.0; kf_Q(1, 1) = 100.0; kf_Q(2, 2) = 100.0; kf_Q(3, 3) = 8.0; kf_Q(4, 4) = 8.0; kf_Q(5, 5) = 8.0; kf_Q(6, 6) = .1; kf_Q(7, 7) = .1; kf_Q(8, 8) = .1; kf_Q(9, 9) = 4.0; kf_Q(10, 10) = 100.0; // Measurement error Covariance Matrix R assembling for (int32_t i = 0; i < new_data.sat_number; i++) { // It is diagonal 2*NSatellite x 2*NSatellite (NSat psudorange error;NSat pseudo range rate error) kf_R(i, i) = 20.0;//*50.0/new_data.sat_CN0_dB_hz(i); //TODO: fill with real values. kf_R(i + new_data.sat_number, i + new_data.sat_number) = 1.0;//*50.0/new_data.sat_CN0_dB_hz(i); // if(50.0*50.0/new_data.sat_CN0_dB_hz(i)>70||5.0*50.0/new_data.sat_CN0_dB_hz(i)>7){ // kf_R(i, i) = 10e4; // kf_R(i + new_data.sat_number, i + new_data.sat_number) = 10e4; // } } // arma::mat test = kf_P_x.diag(); // test.print("P diagonal"); // test = kf_Q.diag(); // test.print("Q diagonal"); // Kalman state prediction (time update) //new_data.kf_state=kf_x; //kf_x = kf_F * kf_x; // state prediction //from error state variables to variables // From state variables definition // TODO: cast to type properly d = arma::zeros(new_data.sat_number, 1); rho_pri = arma::zeros(new_data.sat_number, 1); rhoDot_pri = arma::zeros(new_data.sat_number, 1); rho_pri_filt = arma::zeros(new_data.sat_number, 1); rhoDot_pri_filt = arma::zeros(new_data.sat_number, 1); doppler_hz_filt = arma::zeros(new_data.sat_number, 1); a_x = arma::zeros(new_data.sat_number, 1); a_y = arma::zeros(new_data.sat_number, 1); a_z = arma::zeros(new_data.sat_number, 1); test = obsv_calc(rho_pri,rhoDot_pri,a_x, a_y, a_z,new_data.sat_number,new_data.sat_p,new_data.sat_v,kf_x); for (int32_t i = 0; i < new_data.sat_number; i++) //neccesary quantities { new_data.sat_LOS(i, 0) = a_x(i); new_data.sat_LOS(i, 1) = a_y(i); new_data.sat_LOS(i, 2) = a_z(i); } // cout<.5){ kf_xerr.print("kf_xERR: "); cout<<"kf_dt: "<5){ // dump_vtl_file << "pr_m" // << "," << new_data.pr_m(0) << "," << new_data.pr_m(1) << "," << new_data.pr_m(2) // << "," << new_data.pr_m(3) << "," << new_data.pr_m(4)<<"," << new_data.pr_m(5) << endl; // dump_vtl_file << "prDot_m" // << "," << new_data.doppler_hz(0)<< "," << new_data.doppler_hz(1)<< "," << new_data.doppler_hz(2) // << "," << new_data.doppler_hz(3) << "," << new_data.doppler_hz(4)<< "," << new_data.doppler_hz(5)<< endl; // dump_vtl_file << "sat_position" // << "," << kf_xerr(0) << "," << kf_xerr(1) << "," << "," << kf_xerr(5) // << "," << kf_xerr(6) << "," << kf_xerr(7) << endl; // dump_vtl_file << "sat_velocity" // << "," << kf_xerr(0) << "," << kf_xerr(1) << "," << kf_xerr(2) << "," << kf_xerr(3) << "," << kf_xerr(4) << "," << kf_xerr(5) // << "," << kf_xerr(6) << "," << kf_xerr(7) << endl; } dump_vtl_file << "kf_state" << "," << kf_x(0) << "," << kf_x(1) << "," << kf_x(2) << "," << kf_x(3) << "," << kf_x(4) << "," << kf_x(5) << "," << kf_x(6) << "," << kf_x(7)<< "," << kf_x(8) <<"," << kf_x(9) <<"," << kf_x(10)<< endl; dump_vtl_file << "kf_xerr" << "," << kf_xerr(0) << "," << kf_xerr(1) << "," << kf_xerr(2) << "," << kf_xerr(3) << "," << kf_xerr(4) << "," << kf_xerr(5) << "," << kf_xerr(6) << "," << kf_xerr(7)<< "," << kf_xerr(8) <<"," << kf_xerr(9) <<"," << kf_xerr(10)<< endl; dump_vtl_file << "rtklib_state" << "," << new_data.rx_p(0) << "," << new_data.rx_p(1) << "," << new_data.rx_p(2) << "," << new_data.rx_v(0) << "," << new_data.rx_v(1) << "," << new_data.rx_v(2) << "," << new_data.rx_dts(0) << "," << new_data.rx_dts(1) << "," << delta_t_vtl << endl; // dump_vtl_file << "filt_dopp_sat" // << "," << doppler_hz_filt(0) << "," << doppler_hz_filt(1) << "," << doppler_hz_filt(2) << "," << doppler_hz_filt(3) << "," << doppler_hz_filt(4) <