From 0cd504b5d6b3cfc19b5e01343c4a3db0591c0058 Mon Sep 17 00:00:00 2001 From: Oleksandr Suvorov Date: Thu, 29 Oct 2026 14:54:52 +0200 Subject: [PATCH] telemetry_decoder: report 180-deg PLL lock for BeiDou DNAV and GLONASS The BeiDou DNAV and GLONASS GNAV decoders find from the sign of the preamble (or time mark) correlation that the carrier loop is locked 180 degrees off and invert the symbols they decode, but they never set Flag_PLL_180_deg_phase_locked, so the flag that reaches the observables is the one the tracking block set. Keep the sign of the last frame that passed its CRC check and, as the GPS and Galileo decoders do, report it in Flag_PLL_180_deg_phase_locked and add half a cycle to the carrier phase while it is set. The new unit tests feed BeiDou D1 subframes and GLONASS strings, normal and inverted, to the decoders and check the reported flag and carrier phase, a polarity change, and a GLONASS string that fails its Hamming check. Signed-off-by: Oleksandr Suvorov --- docs/CHANGELOG.md | 6 + .../beidou_dnav_telemetry_decoder_gs.cc | 13 + .../beidou_dnav_telemetry_decoder_gs.h | 7 +- .../glonass_gnav_telemetry_decoder_gs.cc | 14 + .../glonass_gnav_telemetry_decoder_gs.h | 5 +- .../beidou_dnav_telemetry_decoder_test.cc | 229 ++++++++++++++++ .../glonass_gnav_telemetry_decoder_test.cc | 246 ++++++++++++++++++ tests/unit_tests_core.cc | 2 + 8 files changed, 517 insertions(+), 5 deletions(-) create mode 100644 tests/unit-tests/signal-processing-blocks/telemetry_decoder/beidou_dnav_telemetry_decoder_test.cc create mode 100644 tests/unit-tests/signal-processing-blocks/telemetry_decoder/glonass_gnav_telemetry_decoder_test.cc diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3bfef74ed..c4a94a974 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -25,6 +25,12 @@ All notable changes to GNSS-SDR will be documented in this file. - Fixed the Galileo E5b BGD scaling and the ionospheric scaling of E5b measurements in GPS L5 + Galileo E5b receivers, where the factors were referenced to the E5a wavelength held in the first slot instead of to E1. +- The BeiDou DNAV (B1I, B3I) and GLONASS GNAV (L1, L2 C/A) telemetry decoders + now report the 180-degree carrier phase lock they detect from the sign of the + preamble (or time mark) in `Flag_PLL_180_deg_phase_locked`, and add half a + cycle to the carrier phase while it is set, as the GPS and Galileo telemetry + decoders already did. Before, they inverted the decoded symbols but left the + flag the tracking block had set. ### Improvements in Availability: diff --git a/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.cc b/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.cc index fdd958f50..7ce787e3a 100644 --- a/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.cc +++ b/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.cc @@ -82,6 +82,7 @@ beidou_dnav_telemetry_decoder_gs::beidou_dnav_telemetry_decoder_gs(const Tlm_Con d_flag_SOW_set(false), d_flag_frame_sync(false), d_flag_preamble(false), + d_flag_PLL_180_deg_phase_locked(false), d_flag_valid_word(false), d_sent_tlm_failed_msg(false), d_dump(conf.dump), @@ -502,6 +503,7 @@ int beidou_dnav_telemetry_decoder_gs::general_work(int noutput_items __attribute if (d_nav.get_flag_CRC_test() == true) { d_CRC_error_counter = 0; + d_flag_PLL_180_deg_phase_locked = corr_value < 0; d_flag_preamble = true; // valid preamble indicator (initialized to false every work()) d_preamble_index = d_sample_counter; // record the preamble sample stamp (t_P) if (!d_flag_frame_sync) @@ -560,6 +562,7 @@ int beidou_dnav_telemetry_decoder_gs::general_work(int noutput_items __attribute if (d_nav.get_flag_CRC_test() == true) { d_CRC_error_counter = 0; + d_flag_PLL_180_deg_phase_locked = corr_value < 0; d_flag_preamble = true; // valid preamble indicator (initialized to false every work()) d_preamble_index = d_sample_counter; // record the preamble sample stamp (t_P) if (!d_flag_frame_sync) @@ -637,6 +640,16 @@ int beidou_dnav_telemetry_decoder_gs::general_work(int noutput_items __attribute { current_symbol.TOW_at_current_symbol_ms = d_TOW_at_current_symbol_ms; current_symbol.Flag_valid_word = d_flag_valid_word; + if (d_flag_PLL_180_deg_phase_locked == true) + { + // correct the accumulated phase for the Costas loop phase shift, if required + current_symbol.Carrier_phase_rads += GNSS_PI; + current_symbol.Flag_PLL_180_deg_phase_locked = true; + } + else + { + current_symbol.Flag_PLL_180_deg_phase_locked = false; + } if (d_dump == true) { diff --git a/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.h b/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.h index b3542e228..8390d3cde 100644 --- a/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.h +++ b/src/algorithms/telemetry_decoder/gnuradio_blocks/beidou_dnav_telemetry_decoder_gs.h @@ -112,9 +112,10 @@ private: const char d_band; // Frequency band the block is configured for ('1' for B1I, '3' for B3I) - bool d_flag_SOW_set; // Indicates when time of week is set - bool d_flag_frame_sync; // Indicate when a frame sync is achieved - bool d_flag_preamble; // Flag indicating when preamble was found + bool d_flag_SOW_set; // Indicates when time of week is set + bool d_flag_frame_sync; // Indicate when a frame sync is achieved + bool d_flag_preamble; // Flag indicating when preamble was found + bool d_flag_PLL_180_deg_phase_locked; // Preamble of the last decoded frame came inverted bool d_flag_valid_word; bool d_sent_tlm_failed_msg; diff --git a/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.cc b/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.cc index fe7dcd163..f46217672 100644 --- a/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.cc +++ b/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.cc @@ -19,6 +19,7 @@ #include "glonass_gnav_telemetry_decoder_gs.h" +#include "MATH_CONSTANTS.h" #include "display.h" #include "dump_logger_helper.h" #include "glonass_gnav_almanac.h" @@ -69,6 +70,7 @@ glonass_gnav_telemetry_decoder_gs::glonass_gnav_telemetry_decoder_gs(const Tlm_C d_band(frequency_band == 2 ? '2' : '1'), d_flag_frame_sync(false), d_flag_preamble(false), + d_flag_PLL_180_deg_phase_locked(false), d_dump(conf.dump), d_dump_mat(conf.dump_mat), d_remove_dat(conf.remove_dat), @@ -239,6 +241,7 @@ void glonass_gnav_telemetry_decoder_gs::reset() d_CRC_error_counter = 0; d_flag_frame_sync = false; d_flag_preamble = false; + d_flag_PLL_180_deg_phase_locked = false; d_TOW_at_current_symbol = 0; // Invalidate the propagated TOW until a new string 5 is decoded; otherwise // Flag_valid_word would remain true with a TOW offset by the outage duration @@ -355,6 +358,7 @@ int glonass_gnav_telemetry_decoder_gs::general_work(int noutput_items __attribut if (crc_ok == true) { d_CRC_error_counter = 0; + d_flag_PLL_180_deg_phase_locked = corr_value < 0; d_flag_preamble = true; // valid preamble indicator (initialized to false every work()) d_preamble_index = d_sample_counter; // record the preamble sample stamp (t_P) if (!d_flag_frame_sync) @@ -413,6 +417,16 @@ int glonass_gnav_telemetry_decoder_gs::general_work(int noutput_items __attribut if (d_flag_frame_sync == true && d_nav.is_flag_TOW_set() == true) { current_symbol.Flag_valid_word = true; + if (d_flag_PLL_180_deg_phase_locked == true) + { + // correct the accumulated phase for the Costas loop phase shift, if required + current_symbol.Carrier_phase_rads += GNSS_PI; + current_symbol.Flag_PLL_180_deg_phase_locked = true; + } + else + { + current_symbol.Flag_PLL_180_deg_phase_locked = false; + } if (d_enable_navdata_monitor && !d_nav_msg_packet.nav_message.empty()) { d_nav_msg_packet.prn = static_cast(current_symbol.PRN); diff --git a/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.h b/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.h index a33217182..9f404adaa 100644 --- a/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.h +++ b/src/algorithms/telemetry_decoder/gnuradio_blocks/glonass_gnav_telemetry_decoder_gs.h @@ -98,8 +98,9 @@ private: const char d_band; // Frequency band the block is configured for ('1' or '2') - bool d_flag_frame_sync; // Indicate when a frame sync is achieved - bool d_flag_preamble; // Flag indicating when preamble was found + bool d_flag_frame_sync; // Indicate when a frame sync is achieved + bool d_flag_preamble; // Flag indicating when preamble was found + bool d_flag_PLL_180_deg_phase_locked; // Preamble of the last decoded frame came inverted const bool d_dump; const bool d_dump_mat; const bool d_remove_dat; diff --git a/tests/unit-tests/signal-processing-blocks/telemetry_decoder/beidou_dnav_telemetry_decoder_test.cc b/tests/unit-tests/signal-processing-blocks/telemetry_decoder/beidou_dnav_telemetry_decoder_test.cc new file mode 100644 index 000000000..2cf06dc11 --- /dev/null +++ b/tests/unit-tests/signal-processing-blocks/telemetry_decoder/beidou_dnav_telemetry_decoder_test.cc @@ -0,0 +1,229 @@ +/*! + * \file beidou_dnav_telemetry_decoder_test.cc + * \brief Checks the 180-degree carrier phase lock reported by the BeiDou DNAV + * telemetry decoder + * \author Oleksandr Suvorov, 2026. github(at)say.od.ua + * + * ----------------------------------------------------------------------------- + * + * GNSS-SDR is a Global Navigation Satellite System software-defined receiver. + * This file is part of GNSS-SDR. + * + * Copyright (C) 2010-2026 (see AUTHORS file for a list of contributors) + * SPDX-License-Identifier: GPL-3.0-or-later + * + * ----------------------------------------------------------------------------- + */ + +#include "Beidou_DNAV.h" +#include "MATH_CONSTANTS.h" +#include "beidou_dnav_telemetry_decoder_gs.h" +#include "gnss_satellite.h" +#include "gnss_synchro.h" +#include "tlm_conf.h" +#include +#include +#include +#include +#include +#include + +#ifdef GR_GREATER_38 +#include +#else +#include +#endif + + +class BeidouDnavTelemetryDecoderTest : public ::testing::Test +{ +protected: + void SetUp() override + { + decoder = beidou_dnav_make_telemetry_decoder_gs(Tlm_Conf{}, 1); + decoder->set_satellite(Gnss_Satellite("Beidou", 10)); // D1 (non-GEO) satellite + // Allocate GNU Radio buffers, then drive work directly without scheduler threads. + auto source = gr::blocks::vector_source_b::make(std::vector(sizeof(Gnss_Synchro), 0), false, sizeof(Gnss_Synchro)); + auto sink = gr::blocks::null_sink::make(sizeof(Gnss_Synchro)); + top_block = gr::make_top_block("beidou_dnav_telemetry_decoder_test"); + top_block->connect(source, 0, decoder, 0); + top_block->connect(decoder, 0, sink, 0); + top_block->run(); + decoder->reset(); + symbol.System = 'C'; + symbol.Signal[0] = 'B'; + symbol.Signal[1] = '1'; + symbol.PRN = 10; + symbol.Flag_valid_symbol_output = true; + symbol.fs = 25000000; + symbol.CN0_dB_hz = 40.0; + symbol.Carrier_phase_rads = CARRIER_PHASE_RADS; + } + + // Symbols (+1 for a bit 1) of a D1 subframe 4 with the given SOW and no + // almanac page, encoded as transmitted: BCH(15,11) and, from word 2 on, + // the two codewords of each word interleaved bit by bit. + static std::vector subframe(uint32_t sow, bool inverted = false) + { + std::array bits{}; + const char* preamble = BEIDOU_DNAV_PREAMBLE; + for (int32_t i = 0; i < BEIDOU_DNAV_PREAMBLE_LENGTH_BITS; i++) + { + bits[i] = static_cast(preamble[i] == '1'); + } + set_field(bits, D1_FRAID, 4); + set_field(bits, D1_SOW, sow); + + std::vector symbols; + symbols.reserve(BEIDOU_DNAV_SUBFRAME_SYMBOLS); + // Word 1: 15 bits sent as they are, then one codeword + for (int32_t i = 0; i < 15; i++) + { + symbols.push_back(bits[i] ? 1.0F : -1.0F); + } + for (const auto bit : bch_encode(&bits[15])) + { + symbols.push_back(bit ? 1.0F : -1.0F); + } + // Words 2-10: 22 information bits in two interleaved codewords + for (uint32_t word = 1; word < BEIDOU_DNAV_WORDS_SUBFRAME; word++) + { + const auto first = bch_encode(&bits[word * 30]); + const auto second = bch_encode(&bits[word * 30 + 11]); + for (int32_t i = 0; i < 15; i++) + { + symbols.push_back(first[i] ? 1.0F : -1.0F); + symbols.push_back(second[i] ? 1.0F : -1.0F); + } + } + if (inverted) + { + for (auto& value : symbols) + { + value = -value; + } + } + return symbols; + } + + // Feeds the symbols and returns the decoder output for the last one + Gnss_Synchro feed(const std::vector& symbols) + { + Gnss_Synchro output{}; + gr_vector_const_void_star inputs{&symbol}; + gr_vector_void_star outputs{&output}; + gr_vector_int available{1}; + for (const auto value : symbols) + { + symbol.Prompt_I = value; + symbol.Tracking_sample_counter += 500000; + EXPECT_EQ(decoder->general_work(1, available, inputs, outputs), 1); + } + return output; + } + + // Feeds subframes with consecutive SOWs, the given ones inverted, and + // returns the output at the end of each + std::vector feed_subframes(int32_t count, const std::vector& inverted = {}) + { + std::vector outputs; + for (int32_t i = 0; i < count; i++) + { + const bool invert = i < static_cast(inverted.size()) && inverted[i]; + outputs.push_back(feed(subframe(FIRST_SOW + static_cast(i) * 6, invert))); + } + return outputs; + } + + // TOW reported with the last symbol of subframe n: the end of that + // subframe, which is the SOW of the next one, in GPS time + static uint32_t tow_at_end_of_subframe(int32_t n) + { + return (FIRST_SOW + static_cast(n + 1) * 6 + BEIDOU_DNAV_BDT2GPST_LEAP_SEC_OFFSET) * 1000; + } + + static constexpr double CARRIER_PHASE_RADS = 1.0; + static constexpr uint32_t FIRST_SOW = 345600; + Gnss_Synchro symbol{}; + +private: + template + static void set_field(std::array& bits, const std::vector>& field, uint32_t value) + { + int32_t length = 0; + for (const auto& part : field) + { + length += part.second; + } + for (const auto& part : field) + { + for (int32_t i = 0; i < part.second; i++) + { + length--; + bits[part.first - 1 + i] = static_cast((value >> length) & 1U); + } + } + } + + // Systematic BCH(15,11) codeword with generator x^4 + x + 1 + static std::array bch_encode(const uint8_t* info) + { + std::array codeword{}; + std::array reg{}; + for (int32_t i = 0; i < 11; i++) + { + codeword[i] = info[i]; + const uint8_t feedback = info[i] ^ reg[3]; + reg[3] = reg[2]; + reg[2] = reg[1]; + reg[1] = reg[0] ^ feedback; + reg[0] = feedback; + } + for (int32_t i = 0; i < 4; i++) + { + codeword[11 + i] = reg[3 - i]; + } + return codeword; + } + + beidou_dnav_telemetry_decoder_gs_sptr decoder; + gr::top_block_sptr top_block; +}; + + +TEST_F(BeidouDnavTelemetryDecoderTest, NormalPolarityClearsTheTrackingFlag) +{ + symbol.Flag_PLL_180_deg_phase_locked = true; + const auto outputs = feed_subframes(6); + const auto& output = outputs.back(); + ASSERT_TRUE(output.Flag_valid_word); + EXPECT_EQ(output.TOW_at_current_symbol_ms, tow_at_end_of_subframe(5)); + EXPECT_FALSE(output.Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(output.Carrier_phase_rads, CARRIER_PHASE_RADS); +} + + +TEST_F(BeidouDnavTelemetryDecoderTest, InvertedPolaritySetsTheFlagAndAddsHalfACycle) +{ + const auto outputs = feed_subframes(6, std::vector(6, true)); + const auto& output = outputs.back(); + ASSERT_TRUE(output.Flag_valid_word); + EXPECT_EQ(output.TOW_at_current_symbol_ms, tow_at_end_of_subframe(5)); + EXPECT_TRUE(output.Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(output.Carrier_phase_rads, CARRIER_PHASE_RADS + GNSS_PI); +} + + +TEST_F(BeidouDnavTelemetryDecoderTest, PolarityChangeIsReportedFromTheNextDecodedSubframe) +{ + // The decoder decodes a subframe once it has received the preamble of the + // next one, so the output at the end of subframe n reflects subframe n - 1. + const auto outputs = feed_subframes(8, {false, false, false, false, false, true, true, true}); + ASSERT_TRUE(outputs[5].Flag_valid_word); + EXPECT_FALSE(outputs[5].Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(outputs[5].Carrier_phase_rads, CARRIER_PHASE_RADS); + ASSERT_TRUE(outputs[7].Flag_valid_word); + EXPECT_EQ(outputs[7].TOW_at_current_symbol_ms, tow_at_end_of_subframe(7)); + EXPECT_TRUE(outputs[7].Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(outputs[7].Carrier_phase_rads, CARRIER_PHASE_RADS + GNSS_PI); +} diff --git a/tests/unit-tests/signal-processing-blocks/telemetry_decoder/glonass_gnav_telemetry_decoder_test.cc b/tests/unit-tests/signal-processing-blocks/telemetry_decoder/glonass_gnav_telemetry_decoder_test.cc new file mode 100644 index 000000000..31f2d6f45 --- /dev/null +++ b/tests/unit-tests/signal-processing-blocks/telemetry_decoder/glonass_gnav_telemetry_decoder_test.cc @@ -0,0 +1,246 @@ +/*! + * \file glonass_gnav_telemetry_decoder_test.cc + * \brief Checks the 180-degree carrier phase lock reported by the GLONASS GNAV + * telemetry decoder + * \author Oleksandr Suvorov, 2026. github(at)say.od.ua + * + * ----------------------------------------------------------------------------- + * + * GNSS-SDR is a Global Navigation Satellite System software-defined receiver. + * This file is part of GNSS-SDR. + * + * Copyright (C) 2010-2026 (see AUTHORS file for a list of contributors) + * SPDX-License-Identifier: GPL-3.0-or-later + * + * ----------------------------------------------------------------------------- + */ + +#include "GLONASS_L1_L2_CA.h" +#include "MATH_CONSTANTS.h" +#include "glonass_gnav_telemetry_decoder_gs.h" +#include "gnss_satellite.h" +#include "gnss_synchro.h" +#include "tlm_conf.h" +#include +#include +#include +#include +#include +#include + +#ifdef GR_GREATER_38 +#include +#else +#include +#endif + + +class GlonassGnavTelemetryDecoderTest : public ::testing::Test +{ +protected: + void SetUp() override + { + decoder = glonass_gnav_make_telemetry_decoder_gs(Tlm_Conf{}, 1); + decoder->set_satellite(Gnss_Satellite("Glonass", 1)); + // Allocate GNU Radio buffers, then drive work directly without scheduler threads. + auto source = gr::blocks::vector_source_b::make(std::vector(sizeof(Gnss_Synchro), 0), false, sizeof(Gnss_Synchro)); + auto sink = gr::blocks::null_sink::make(sizeof(Gnss_Synchro)); + top_block = gr::make_top_block("glonass_gnav_telemetry_decoder_test"); + top_block->connect(source, 0, decoder, 0); + top_block->connect(decoder, 0, sink, 0); + top_block->run(); + decoder->reset(); + symbol.System = 'R'; + symbol.Signal[0] = '1'; + symbol.Signal[1] = 'G'; + symbol.PRN = 1; + symbol.Flag_valid_symbol_output = true; + symbol.fs = 6625000; + symbol.CN0_dB_hz = 40.0; + symbol.Carrier_phase_rads = CARRIER_PHASE_RADS; + } + + // Symbols of a time mark followed by a string with the given number, as + // transmitted: Hamming code, relative code and meander. The decoder takes + // the time decoded from strings 1, 4 and 5. A string with a damaged + // Hamming code has two data bits flipped, which the code cannot correct. + static std::vector string_after_time_mark(uint32_t string_id, bool inverted = false, bool damaged = false) + { + std::array bits{}; // bits[0] is the first (idle) bit + set_field(bits, STRING_ID, string_id); + switch (string_id) + { + case 1: + set_field(bits, T_K_HR, 12); + break; + case 4: + set_field(bits, N_T, 100); + set_field(bits, N, 1); + break; + case 5: + set_field(bits, N_4, 8); + break; + default: + break; + } + add_hamming_code(bits); + if (damaged) + { + bits[10] ^= 1U; + bits[11] ^= 1U; + } + + std::vector symbols(GLONASS_GNAV_PREAMBLE_SAMPLES); + uint8_t relative = 0; + for (int32_t i = 0; i < GLONASS_GNAV_STRING_BITS; i++) + { + if (i > 0) + { + relative ^= bits[i]; + } + symbols.push_back(relative ? 1.0F : -1.0F); + symbols.push_back(relative ? -1.0F : 1.0F); + } + if (inverted) + { + for (auto& value : symbols) + { + value = -value; + } + } + return symbols; + } + + // Feeds the symbols and returns the decoder output for the last one + Gnss_Synchro feed(const std::vector& symbols) + { + Gnss_Synchro output{}; + gr_vector_const_void_star inputs{&symbol}; + gr_vector_void_star outputs{&output}; + gr_vector_int available{1}; + for (const auto value : symbols) + { + symbol.Prompt_I = value; + symbol.Tracking_sample_counter += 66250; + EXPECT_EQ(decoder->general_work(1, available, inputs, outputs), 1); + } + return output; + } + + // Feeds strings and returns the output at the end of each. The decoder + // first decodes the fifth string, so strings 1 to 5 go from there on; + // string 5 is not repeated afterwards, as a new one would carry a new time. + std::vector feed_strings(int32_t count, const std::vector& inverted = {}, int32_t damaged = -1) + { + std::vector outputs; + for (int32_t i = 0; i < count; i++) + { + uint32_t string_id = 1; + if (i >= FIRST_DECODED_STRING && i < FIRST_DECODED_STRING + 5) + { + string_id = static_cast(i - FIRST_DECODED_STRING + 1); + } + else if (i >= FIRST_DECODED_STRING + 5) + { + string_id = static_cast((i - FIRST_DECODED_STRING - 5) % 4 + 1); + } + const bool invert = i < static_cast(inverted.size()) && inverted[i]; + outputs.push_back(feed(string_after_time_mark(string_id, invert, i == damaged))); + } + return outputs; + } + + static constexpr double CARRIER_PHASE_RADS = 1.0; + static constexpr int32_t FIRST_DECODED_STRING = 4; + static constexpr int32_t TIME_SET_STRING = FIRST_DECODED_STRING + 4; + Gnss_Synchro symbol{}; + +private: + static void set_field(std::array& bits, const std::vector>& field, uint32_t value) + { + const int32_t start = field[0].first - 1; + const int32_t length = field[0].second; + for (int32_t i = 0; i < length; i++) + { + bits[start + i] = static_cast((value >> (length - i - 1)) & 1U); + } + } + + // Check bits 1 to 8 (the last eight bits of the string) of the Hamming + // code. ICD bit k is bits[GLONASS_GNAV_STRING_BITS - k]. + static void add_hamming_code(std::array& bits) + { + const auto icd_bit = [&bits](int32_t k) -> uint8_t& { return bits[GLONASS_GNAV_STRING_BITS - k]; }; + const auto parity = [&icd_bit](const std::vector& indexes) { + uint8_t sum = 0; + for (const auto k : indexes) + { + sum ^= icd_bit(k); + } + return sum; + }; + const std::array*, 7> checks{&GLONASS_GNAV_CRC_I_INDEX, &GLONASS_GNAV_CRC_J_INDEX, + &GLONASS_GNAV_CRC_K_INDEX, &GLONASS_GNAV_CRC_L_INDEX, &GLONASS_GNAV_CRC_M_INDEX, &GLONASS_GNAV_CRC_N_INDEX, + &GLONASS_GNAV_CRC_P_INDEX}; + uint8_t check_sum = 0; + for (int32_t k = 1; k <= 7; k++) + { + icd_bit(k) = parity(*checks[k - 1]); + check_sum ^= icd_bit(k); + } + icd_bit(8) = check_sum ^ parity(GLONASS_GNAV_CRC_Q_INDEX); + } + + glonass_gnav_telemetry_decoder_gs_sptr decoder; + gr::top_block_sptr top_block; +}; + + +TEST_F(GlonassGnavTelemetryDecoderTest, NormalPolarityClearsTheTrackingFlag) +{ + symbol.Flag_PLL_180_deg_phase_locked = true; + const auto outputs = feed_strings(TIME_SET_STRING + 2); + EXPECT_FALSE(outputs[TIME_SET_STRING - 1].Flag_valid_word); + const auto& output = outputs.back(); + ASSERT_TRUE(output.Flag_valid_word); + EXPECT_FALSE(output.Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(output.Carrier_phase_rads, CARRIER_PHASE_RADS); +} + + +TEST_F(GlonassGnavTelemetryDecoderTest, InvertedPolaritySetsTheFlagAndAddsHalfACycle) +{ + const auto outputs = feed_strings(TIME_SET_STRING + 2, std::vector(TIME_SET_STRING + 2, true)); + const auto& output = outputs.back(); + ASSERT_TRUE(output.Flag_valid_word); + EXPECT_TRUE(output.Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(output.Carrier_phase_rads, CARRIER_PHASE_RADS + GNSS_PI); +} + + +TEST_F(GlonassGnavTelemetryDecoderTest, PolarityChangeIsReportedFromTheNextValidString) +{ + // The string after a time mark is decoded once its last symbol arrives + std::vector inverted(TIME_SET_STRING + 3, false); + inverted[TIME_SET_STRING + 2] = true; + const auto outputs = feed_strings(TIME_SET_STRING + 3, inverted); + ASSERT_TRUE(outputs[TIME_SET_STRING + 1].Flag_valid_word); + EXPECT_FALSE(outputs[TIME_SET_STRING + 1].Flag_PLL_180_deg_phase_locked); + ASSERT_TRUE(outputs[TIME_SET_STRING + 2].Flag_valid_word); + EXPECT_TRUE(outputs[TIME_SET_STRING + 2].Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(outputs[TIME_SET_STRING + 2].Carrier_phase_rads, CARRIER_PHASE_RADS + GNSS_PI); +} + + +TEST_F(GlonassGnavTelemetryDecoderTest, StringFailingTheHammingCheckKeepsThePreviousDecision) +{ + std::vector inverted(TIME_SET_STRING + 4, false); + inverted[TIME_SET_STRING + 2] = true; + inverted[TIME_SET_STRING + 3] = true; + const auto outputs = feed_strings(TIME_SET_STRING + 4, inverted, TIME_SET_STRING + 2); + ASSERT_TRUE(outputs[TIME_SET_STRING + 2].Flag_valid_word); + EXPECT_FALSE(outputs[TIME_SET_STRING + 2].Flag_PLL_180_deg_phase_locked); + EXPECT_DOUBLE_EQ(outputs[TIME_SET_STRING + 2].Carrier_phase_rads, CARRIER_PHASE_RADS); + ASSERT_TRUE(outputs[TIME_SET_STRING + 3].Flag_valid_word); + EXPECT_TRUE(outputs[TIME_SET_STRING + 3].Flag_PLL_180_deg_phase_locked); +} diff --git a/tests/unit_tests_core.cc b/tests/unit_tests_core.cc index 2691c73eb..0c1a1d8d7 100644 --- a/tests/unit_tests_core.cc +++ b/tests/unit_tests_core.cc @@ -49,8 +49,10 @@ #include "unit-tests/signal-processing-blocks/resampler/mmse_resampler_test.cc" #include "unit-tests/signal-processing-blocks/sources/gnss_sdr_valve_test.cc" #include "unit-tests/signal-processing-blocks/sources/unpack_2bit_samples_test.cc" +#include "unit-tests/signal-processing-blocks/telemetry_decoder/beidou_dnav_telemetry_decoder_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/deterministic_telemetry_fault_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/galileo_fnav_inav_decoder_test.cc" +#include "unit-tests/signal-processing-blocks/telemetry_decoder/glonass_gnav_telemetry_decoder_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/gps_l1c_telemetry_decoder_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/sbas_l1_telemetry_decoder_test.cc"