From fa549b09a2c95781667052d3d6ceda9b0dd0c86a Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sat, 13 Jul 2019 23:05:57 +0200 Subject: [PATCH] Do not produce empty files --- src/algorithms/PVT/libs/rtklib_solver.cc | 29 +++++++ .../gnuradio_blocks/hybrid_observables_gs.cc | 76 +++++++++++-------- .../gnuradio_blocks/hybrid_observables_gs.h | 5 +- 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/src/algorithms/PVT/libs/rtklib_solver.cc b/src/algorithms/PVT/libs/rtklib_solver.cc index 283584b7e..d0d24e59f 100644 --- a/src/algorithms/PVT/libs/rtklib_solver.cc +++ b/src/algorithms/PVT/libs/rtklib_solver.cc @@ -67,6 +67,25 @@ #include #include +#if HAS_STD_FILESYSTEM +#include +namespace errorlib = std; +#if HAS_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif +#else +#include // for create_directories, exists +#include // for path, operator<< +#include // for filesystem +#include // for error_code +namespace fs = boost::filesystem; +namespace errorlib = boost::system; +#endif + Rtklib_Solver::Rtklib_Solver(int nchannels, std::string dump_filename, bool flag_dump_to_file, bool flag_dump_to_mat, const rtk_t &rtk) { @@ -341,6 +360,7 @@ Rtklib_Solver::~Rtklib_Solver() { if (d_dump_file.is_open() == true) { + auto pos = d_dump_file.tellp(); try { d_dump_file.close(); @@ -349,6 +369,15 @@ Rtklib_Solver::~Rtklib_Solver() { LOG(WARNING) << "Exception in destructor closing the RTKLIB dump file " << ex.what(); } + if (pos == 0) + { + errorlib::error_code ec; + if (!fs::remove(fs::path(d_dump_filename), ec)) + { + std::cerr << "Problem removing temporary file " << d_dump_filename << '\n'; + } + d_flag_dump_mat_enabled = false; + } } if (d_flag_dump_mat_enabled) { diff --git a/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.cc b/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.cc index 91e4760f8..09f9a8f23 100644 --- a/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.cc +++ b/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.cc @@ -46,6 +46,8 @@ #include // for move #if HAS_STD_FILESYSTEM +#include +namespace errorlib = std; #if HAS_STD_FILESYSTEM_EXPERIMENTAL #include namespace fs = std::experimental::filesystem; @@ -54,8 +56,12 @@ namespace fs = std::experimental::filesystem; namespace fs = std::filesystem; #endif #else -#include +#include // for create_directories, exists +#include // for path, operator<< +#include // for filesystem +#include // for error_code namespace fs = boost::filesystem; +namespace errorlib = boost::system; #endif @@ -64,33 +70,6 @@ hybrid_observables_gs_sptr hybrid_observables_gs_make(unsigned int nchannels_in, return hybrid_observables_gs_sptr(new hybrid_observables_gs(nchannels_in, nchannels_out, dump, dump_mat, std::move(dump_filename))); } -void hybrid_observables_gs::msg_handler_pvt_to_observables(const pmt::pmt_t &msg) -{ - gr::thread::scoped_lock lock(d_setlock); // require mutex with work function called by the scheduler - try - { - if (pmt::any_ref(msg).type() == typeid(double)) - { - double new_rx_clock_offset_s; - new_rx_clock_offset_s = boost::any_cast(pmt::any_ref(msg)); - T_rx_offset_ms = new_rx_clock_offset_s * 1000.0; - T_rx_TOW_ms = T_rx_TOW_ms - static_cast(round(T_rx_offset_ms)); - T_rx_remnant_to_20ms = (T_rx_TOW_ms % 20); - //d_Rx_clock_buffer.clear(); // Clear all the elements in the buffer - for (uint32_t n = 0; n < d_nchannels_out; n++) - { - d_gnss_synchro_history->clear(n); - } - - LOG(INFO) << "Corrected new RX Time offset: " << T_rx_offset_ms << "[ms]"; - } - } - catch (boost::bad_any_cast &e) - { - LOG(WARNING) << "msg_handler_pvt_to_observables Bad any cast!"; - } -} - hybrid_observables_gs::hybrid_observables_gs(uint32_t nchannels_in, uint32_t nchannels_out, @@ -110,7 +89,7 @@ hybrid_observables_gs::hybrid_observables_gs(uint32_t nchannels_in, d_nchannels_out = nchannels_out; d_nchannels_in = nchannels_in; T_rx_offset_ms = 0; - d_gnss_synchro_history = new Gnss_circular_deque(500, d_nchannels_out); + d_gnss_synchro_history = std::make_shared>(500, d_nchannels_out); // ############# ENABLE DATA FILE LOG ################# if (d_dump) @@ -169,9 +148,9 @@ hybrid_observables_gs::hybrid_observables_gs(uint32_t nchannels_in, hybrid_observables_gs::~hybrid_observables_gs() { - delete d_gnss_synchro_history; if (d_dump_file.is_open()) { + auto pos = d_dump_file.tellp(); try { d_dump_file.close(); @@ -180,6 +159,15 @@ hybrid_observables_gs::~hybrid_observables_gs() { LOG(WARNING) << "Exception in destructor closing the dump file " << ex.what(); } + if (pos == 0) + { + errorlib::error_code ec; + if (!fs::remove(fs::path(d_dump_filename), ec)) + { + std::cerr << "Problem removing temporary file " << d_dump_filename << '\n'; + } + d_dump_mat = false; + } } if (d_dump_mat) { @@ -195,6 +183,34 @@ hybrid_observables_gs::~hybrid_observables_gs() } +void hybrid_observables_gs::msg_handler_pvt_to_observables(const pmt::pmt_t &msg) +{ + gr::thread::scoped_lock lock(d_setlock); // require mutex with work function called by the scheduler + try + { + if (pmt::any_ref(msg).type() == typeid(double)) + { + double new_rx_clock_offset_s; + new_rx_clock_offset_s = boost::any_cast(pmt::any_ref(msg)); + T_rx_offset_ms = new_rx_clock_offset_s * 1000.0; + T_rx_TOW_ms = T_rx_TOW_ms - static_cast(round(T_rx_offset_ms)); + T_rx_remnant_to_20ms = (T_rx_TOW_ms % 20); + //d_Rx_clock_buffer.clear(); // Clear all the elements in the buffer + for (uint32_t n = 0; n < d_nchannels_out; n++) + { + d_gnss_synchro_history->clear(n); + } + + LOG(INFO) << "Corrected new RX Time offset: " << T_rx_offset_ms << "[ms]"; + } + } + catch (boost::bad_any_cast &e) + { + LOG(WARNING) << "msg_handler_pvt_to_observables Bad any cast!"; + } +} + + int32_t hybrid_observables_gs::save_matfile() { // READ DUMP FILE diff --git a/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.h b/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.h index f21510254..39112b160 100644 --- a/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.h +++ b/src/algorithms/observables/gnuradio_blocks/hybrid_observables_gs.h @@ -40,6 +40,7 @@ #include // for gr_vector_int #include // for int32_t #include // for string, ofstream +#include // for shared_ptr #include // for vector class Gnss_Synchro; @@ -94,8 +95,8 @@ private: double T_rx_offset_ms; std::string d_dump_filename; std::ofstream d_dump_file; - boost::circular_buffer d_Rx_clock_buffer; // time history - Gnss_circular_deque* d_gnss_synchro_history; // Tracking observable history + boost::circular_buffer d_Rx_clock_buffer; // time history + std::shared_ptr> d_gnss_synchro_history; // Tracking observable history void msg_handler_pvt_to_observables(const pmt::pmt_t& msg); double compute_T_rx_s(const Gnss_Synchro& a); bool interp_trk_obs(Gnss_Synchro& interpolated_obs, const uint32_t& ch, const uint64_t& rx_clock);