code cleaning

This commit is contained in:
Carles Fernandez
2015-06-06 01:21:00 +02:00
parent 8630cf2fbe
commit 7e04308a67
7 changed files with 334 additions and 353 deletions
@@ -40,10 +40,10 @@
using google::LogMessage;
// logging levels
#define EVENT 2 // logs important events which don't occur every block
#define FLOW 3 // logs the function calls of block processing functions
#define SAMP_SYNC 4 // about 1 log entry per sample -> high output
#define LMORE 5 //
#define EVENT 2 // logs important events which don't occur every block
#define FLOW 3 // logs the function calls of block processing functions
#define SAMP_SYNC 4 // about 1 log entry per sample -> high output
#define LMORE 5 //
@@ -74,18 +74,18 @@ gps_l2_m_telemetry_decoder_cc::gps_l2_m_telemetry_decoder_cc(
d_satellite = Gnss_Satellite(satellite.get_system(), satellite.get_PRN());
LOG(INFO) << "GPS L2C M TELEMETRY PROCESSING: satellite " << d_satellite;
d_fs_in = fs_in;
d_block_size = GPS_L2_SAMPLES_PER_SYMBOL * GPS_L2_SYMBOLS_PER_BIT * GPS_L2_CNAV_DATA_PAGE_BITS*2; // two CNAV frames
d_decimation_output_factor=0;
d_block_size = GPS_L2_SAMPLES_PER_SYMBOL * GPS_L2_SYMBOLS_PER_BIT * GPS_L2_CNAV_DATA_PAGE_BITS * 2; // two CNAV frames
d_decimation_output_factor = 0;
//set_output_multiple (1);
d_average_count=0;
d_flag_invert_buffer_symbols=false;
d_flag_invert_input_symbols=false;
d_channel=0;
d_iono_queue=0;
d_ephemeris_queue=0;
d_flag_valid_word=false;
d_TOW_at_current_symbol=0;
d_TOW_at_Preamble=0;
d_average_count = 0;
d_flag_invert_buffer_symbols = false;
d_flag_invert_input_symbols = false;
d_channel = 0;
d_iono_queue = 0;
d_ephemeris_queue = 0;
d_flag_valid_word = false;
d_TOW_at_current_symbol = 0;
d_TOW_at_Preamble = 0;
//set_history(d_samples_per_bit*8); // At least a history of 8 bits are needed to correlate with the preamble
}
@@ -97,7 +97,6 @@ gps_l2_m_telemetry_decoder_cc::~gps_l2_m_telemetry_decoder_cc()
}
void gps_l2_m_telemetry_decoder_cc::forecast (int noutput_items, gr_vector_int &ninput_items_required)
{
unsigned ninputs = ninput_items_required.size ();
@@ -106,140 +105,118 @@ void gps_l2_m_telemetry_decoder_cc::forecast (int noutput_items, gr_vector_int &
//LOG(INFO) << "forecast(): " << "noutput_items=" << noutput_items << "\tninput_items_required ninput_items_required.size()=" << ninput_items_required.size();
}
void gps_l2_m_telemetry_decoder_cc::set_decimation(int decimation)
{
d_decimation_output_factor = decimation;
}
int gps_l2_m_telemetry_decoder_cc::general_work (int noutput_items, gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
{
//LOG(INFO) << "general_work(): " << "noutput_items=" << noutput_items << "\toutput_items real size=" << output_items.size() << "\tninput_items size=" << ninput_items.size() << "\tinput_items real size=" << input_items.size() << "\tninput_items[0]=" << ninput_items[0];
// get pointers on in- and output gnss-synchro objects
const Gnss_Synchro *in = (const Gnss_Synchro *) input_items[0]; // input
Gnss_Synchro *out = (Gnss_Synchro *) output_items[0]; // output
Gnss_Synchro *out = (Gnss_Synchro *) output_items[0]; // output
// store the time stamp of the first sample in the processed sample block
double sample_stamp = in[0].Tracking_timestamp_secs;
bool flag_new_cnav_frame=false;
int last_frame_preamble_start=0;
bool flag_new_cnav_frame = false;
int last_frame_preamble_start = 0;
// copy correlation samples into samples vector
//for (int i = 0; i < noutput_items; i++)
/// {
// check if channel is in tracking state
// {
d_sample_buf.push_back(in[0].Prompt_I);
// }
// }
d_sample_buf.push_back(in[0].Prompt_I);
consume_each(1); //one by one
// decode only if enough samples in buffer
if(d_sample_buf.size() >= d_block_size)
{
if (in[0].Flag_valid_tracking==false) // check if the tracking is locked
{
//LOG(INFO)<< "Discarting channel "<<d_channel<<" tracking not ready!"<<std::endl;
d_flag_valid_word=false;
}else{
d_flag_invert_buffer_symbols=d_flag_invert_input_symbols;
while (true)
{
if (in[0].Flag_valid_tracking == false) // check if the tracking is locked
{
//LOG(INFO)<< "Discarting channel "<<d_channel<<" tracking not ready!"<<std::endl;
d_flag_valid_word = false;
}
else
{
d_flag_invert_buffer_symbols = d_flag_invert_input_symbols;
while (true)
{
if (d_flag_invert_buffer_symbols == true)
{
for (std::vector<double>::iterator symbol_it = d_sample_buf.begin(); symbol_it != d_sample_buf.end(); symbol_it++)
{
*symbol_it = -(*symbol_it);
}
//LOG(INFO)<<"Inverting buffer symbols";
}
// align symbols in pairs
// and obtain the bits by decoding the symbols (viterbi decoder)
// they can be already aligned or shifted by one position
std::vector<int> bits;
bool symbol_alignment = d_symbol_aligner_and_decoder.get_bits(d_sample_buf, bits);
if (d_flag_invert_buffer_symbols==true)
{
// for (int m=0;m<d_sample_buf.size();m++)
// {
// d_sample_buf.at(m)=-d_sample_buf.at(m);
// }
for (std::vector<double>::iterator symbol_it = d_sample_buf.begin(); symbol_it != d_sample_buf.end(); symbol_it++)
{
*symbol_it = -(*symbol_it);
}
//LOG(INFO)<<"Inverting buffer symbols";
}
//std::stringstream ss;
//for (std::vector<int>::const_iterator bit_it = bits.begin(); bit_it < bits.end(); ++bit_it)
// {
// ss << *bit_it;
// }
// LOG(INFO) << "get_bits=" << ss.str() << std::endl;
//debug
// std::stringstream ss2;
// for (std::vector<double>::const_iterator symbol_it = d_sample_buf.begin(); symbol_it < d_sample_buf.end(); ++symbol_it)
// {
//
// ss2<<*symbol_it<<",";
// if(*symbol_it>=0)
// {
// ss2<<'1';
// }else{
// ss2<<'0';
// }
// }
//LOG(INFO)<<"get_symbols="<<ss2.str();
// search for preambles
// and extract the corresponding message candidates
std::vector<msg_candiate_int_t> msg_candidates;
d_frame_detector.get_frame_candidates(bits, msg_candidates);
// align symbols in pairs
// and obtain the bits by decoding the symbols (viterbi decoder)
// they can be already aligned or shifted by one position
std::vector<int> bits;
bool symbol_alignment = d_symbol_aligner_and_decoder.get_bits(d_sample_buf, bits);
std::stringstream ss;
for (std::vector<int>::const_iterator bit_it = bits.begin(); bit_it < bits.end(); ++bit_it)
{
ss << *bit_it;
}
// LOG(INFO)<<"get_bits="<<ss.str()<<std::endl;
// search for preambles
// and extract the corresponding message candidates
std::vector<msg_candiate_int_t> msg_candidates;
d_frame_detector.get_frame_candidates(bits, msg_candidates);
// verify checksum
// and return the valid messages
std::vector<msg_candiate_int_t> valid_msgs;
d_crc_verifier.get_valid_frames(msg_candidates, valid_msgs);
if (valid_msgs.size()==0)
{
if (d_flag_invert_buffer_symbols==d_flag_invert_input_symbols)
{
d_flag_invert_buffer_symbols=not d_flag_invert_buffer_symbols;
}else{//already tested the symbol inversion but CRC still fail
LOG(INFO)<<"Discarting this buffer, no CNAV frames detected CH "<<this->d_channel;
break;
}
}else{ //at least one frame has good CRC, keep the invert sign for the next frames
d_flag_invert_input_symbols=d_flag_invert_buffer_symbols;
std::vector<int> tmp_msg;
std::string msg;
LOG(INFO)<<valid_msgs.size()<<" GOOD L2C CNAV FRAME DETECTED! CH "<<this->d_channel;
for (unsigned int i=0;i<valid_msgs.size();i++)
{
tmp_msg =valid_msgs.at(i).second;
d_CNAV_Message.decode_page(tmp_msg);
std::cout<<"Valid CNAV frame with relative preamble start at "<<valid_msgs.at(i).first<<std::endl;
flag_new_cnav_frame=true;
d_flag_valid_word=true;
last_frame_preamble_start=valid_msgs.at(i).first;
// 4. Push the new navigation data to the queues
if (d_CNAV_Message.have_new_ephemeris() == true)
{
// get ephemeris object for this SV
Gps_CNAV_Ephemeris ephemeris = d_CNAV_Message.get_ephemeris();//notice that the read operation will clear the valid flag
std::cout<<"New GPS CNAV Ephemeris received for SV "<<ephemeris.i_satellite_PRN<<std::endl;
d_ephemeris_queue->push(ephemeris);
}
if (d_CNAV_Message.have_new_iono() == true)
{
Gps_CNAV_Iono iono= d_CNAV_Message.get_iono(); //notice that the read operation will clear the valid flag
std::cout<<"New GPS CNAV IONO model received for SV "<<d_satellite.get_PRN()<<std::endl;
d_iono_queue->push(iono);
}
}
break;
}
}
}
// verify checksum
// and return the valid messages
std::vector<msg_candiate_int_t> valid_msgs;
d_crc_verifier.get_valid_frames(msg_candidates, valid_msgs);
if (valid_msgs.size() == 0)
{
if (d_flag_invert_buffer_symbols == d_flag_invert_input_symbols)
{
d_flag_invert_buffer_symbols = not d_flag_invert_buffer_symbols;
}
else
{//already tested the symbol inversion but CRC still fail
LOG(INFO) << "Discarting this buffer, no CNAV frames detected CH " << this->d_channel;
break;
}
}
else
{ //at least one frame has good CRC, keep the invert sign for the next frames
d_flag_invert_input_symbols = d_flag_invert_buffer_symbols;
std::vector<int> tmp_msg;
std::string msg;
LOG(INFO) << valid_msgs.size() << " GOOD L2C CNAV FRAME DETECTED! CH " <<this->d_channel;
for (unsigned int i = 0;i < valid_msgs.size(); i++)
{
tmp_msg = valid_msgs.at(i).second;
d_CNAV_Message.decode_page(tmp_msg);
std::cout << "Valid CNAV frame with relative preamble start at " << valid_msgs.at(i).first << std::endl;
flag_new_cnav_frame = true;
d_flag_valid_word = true;
last_frame_preamble_start = valid_msgs.at(i).first;
// 4. Push the new navigation data to the queues
if (d_CNAV_Message.have_new_ephemeris() == true)
{
// get ephemeris object for this SV
Gps_CNAV_Ephemeris ephemeris = d_CNAV_Message.get_ephemeris(); //notice that the read operation will clear the valid flag
std::cout << "New GPS CNAV Ephemeris received for SV " << ephemeris.i_satellite_PRN << std::endl;
d_ephemeris_queue->push(ephemeris);
}
if (d_CNAV_Message.have_new_iono() == true)
{
Gps_CNAV_Iono iono = d_CNAV_Message.get_iono(); //notice that the read operation will clear the valid flag
std::cout << "New GPS CNAV IONO model received for SV " << d_satellite.get_PRN() << std::endl;
d_iono_queue->push(iono);
}
}
break;
}
}
}
// clear all processed samples in the input buffer
d_sample_buf.clear();
}
@@ -250,36 +227,39 @@ int gps_l2_m_telemetry_decoder_cc::general_work (int noutput_items, gr_vector_in
//1. Copy the current tracking output
current_synchro_data = in[0];
if (d_flag_valid_word==true)
{
double Prn_timestamp_at_preamble_ms=0;
//2. Add the telemetry decoder information
if (flag_new_cnav_frame==true)
{
//update TOW at the preamble instant
Prn_timestamp_at_preamble_ms = (in[0].Tracking_timestamp_secs * 1000.0)-(d_block_size-last_frame_preamble_start)*GPS_L2_M_PERIOD;
d_TOW_at_Preamble = d_CNAV_Message.d_TOW - GPS_L2_CNAV_DATA_PAGE_DURATION_S;
d_TOW_at_current_symbol = d_TOW_at_Preamble + (d_block_size-last_frame_preamble_start)*GPS_L2_M_PERIOD;
current_synchro_data.d_TOW = d_TOW_at_Preamble;
current_synchro_data.d_TOW_at_current_symbol = d_TOW_at_current_symbol;
current_synchro_data.d_TOW_hybrid_at_current_symbol = current_synchro_data.d_TOW_at_current_symbol;
current_synchro_data.Flag_preamble = false;
current_synchro_data.Prn_timestamp_ms = in[0].Tracking_timestamp_secs * 1000.0;
current_synchro_data.Prn_timestamp_at_preamble_ms = Prn_timestamp_at_preamble_ms;
}else{
d_TOW_at_current_symbol = d_TOW_at_Preamble + (d_block_size-last_frame_preamble_start)*GPS_L2_M_PERIOD;
current_synchro_data.d_TOW = d_TOW_at_Preamble;
current_synchro_data.d_TOW_at_current_symbol = d_TOW_at_current_symbol;
current_synchro_data.d_TOW_hybrid_at_current_symbol = current_synchro_data.d_TOW_at_current_symbol;
current_synchro_data.Flag_preamble = false;
current_synchro_data.Prn_timestamp_ms = in[0].Tracking_timestamp_secs * 1000.0;
current_synchro_data.Prn_timestamp_at_preamble_ms = Prn_timestamp_at_preamble_ms;
}
current_synchro_data.Flag_valid_word = true;
}else{
current_synchro_data.Flag_valid_word = false;
}
if (d_flag_valid_word == true)
{
double Prn_timestamp_at_preamble_ms = 0;
//2. Add the telemetry decoder information
if (flag_new_cnav_frame == true)
{
//update TOW at the preamble instant
Prn_timestamp_at_preamble_ms = (in[0].Tracking_timestamp_secs * 1000.0) - (d_block_size - last_frame_preamble_start) * GPS_L2_M_PERIOD;
d_TOW_at_Preamble = d_CNAV_Message.d_TOW - GPS_L2_CNAV_DATA_PAGE_DURATION_S;
d_TOW_at_current_symbol = d_TOW_at_Preamble + (d_block_size - last_frame_preamble_start) * GPS_L2_M_PERIOD;
current_synchro_data.d_TOW = d_TOW_at_Preamble;
current_synchro_data.d_TOW_at_current_symbol = d_TOW_at_current_symbol;
current_synchro_data.d_TOW_hybrid_at_current_symbol = current_synchro_data.d_TOW_at_current_symbol;
current_synchro_data.Flag_preamble = false;
current_synchro_data.Prn_timestamp_ms = in[0].Tracking_timestamp_secs * 1000.0;
current_synchro_data.Prn_timestamp_at_preamble_ms = Prn_timestamp_at_preamble_ms;
}
else
{
d_TOW_at_current_symbol = d_TOW_at_Preamble + (d_block_size - last_frame_preamble_start) * GPS_L2_M_PERIOD;
current_synchro_data.d_TOW = d_TOW_at_Preamble;
current_synchro_data.d_TOW_at_current_symbol = d_TOW_at_current_symbol;
current_synchro_data.d_TOW_hybrid_at_current_symbol = current_synchro_data.d_TOW_at_current_symbol;
current_synchro_data.Flag_preamble = false;
current_synchro_data.Prn_timestamp_ms = in[0].Tracking_timestamp_secs * 1000.0;
current_synchro_data.Prn_timestamp_at_preamble_ms = Prn_timestamp_at_preamble_ms;
}
current_synchro_data.Flag_valid_word = true;
}
else
{
current_synchro_data.Flag_valid_word = false;
}
d_average_count++;
if (d_average_count == d_decimation_output_factor)
@@ -346,8 +326,8 @@ void gps_l2_m_telemetry_decoder_cc::symbol_aligner_and_decoder::reset()
bool gps_l2_m_telemetry_decoder_cc::symbol_aligner_and_decoder::get_bits(const std::vector<double> symbols, std::vector<int> &bits)
{
const int traceback_depth = 5*d_KK;
int nbits_requested = symbols.size()/GPS_L2_SYMBOLS_PER_BIT;
const int traceback_depth = 5 * d_KK;
int nbits_requested = symbols.size() / GPS_L2_SYMBOLS_PER_BIT;
int nbits_decoded;
// fill two vectors with the two possible symbol alignments
std::vector<double> symbols_vd1(symbols); // aligned symbol vector -> copy input symbol vector
@@ -448,12 +428,12 @@ void gps_l2_m_telemetry_decoder_cc::frame_detector::get_frame_candidates(const s
// ### helper class for checking the CRC of the message candidates ###
void gps_l2_m_telemetry_decoder_cc::crc_verifier::reset()
{
}
void gps_l2_m_telemetry_decoder_cc::crc_verifier::get_valid_frames(const std::vector<msg_candiate_int_t> msg_candidates, std::vector<msg_candiate_int_t> &valid_msgs)
{
std::vector <unsigned char> tmp_msg;
@@ -481,11 +461,9 @@ void gps_l2_m_telemetry_decoder_cc::crc_verifier::get_valid_frames(const std::ve
void gps_l2_m_telemetry_decoder_cc::crc_verifier::zerropad_back_and_convert_to_bytes(const std::vector<int> msg_candidate, std::vector<unsigned char> &bytes)
{
std::stringstream ss;
//std::stringstream ss;
const size_t bits_per_byte = 8;
unsigned char byte = 0;
//LOG(INFO) << "zerropad_back_and_convert_to_bytes():" << byte;
@@ -494,7 +472,7 @@ void gps_l2_m_telemetry_decoder_cc::crc_verifier::zerropad_back_and_convert_to_b
int idx_bit = candidate_bit_it - msg_candidate.begin();
int bit_pos_in_current_byte = (bits_per_byte - 1) - (idx_bit % bits_per_byte);
byte |= (unsigned char)(*candidate_bit_it) << bit_pos_in_current_byte;
ss << *candidate_bit_it;
// ss << *candidate_bit_it;
if (idx_bit % bits_per_byte == bits_per_byte - 1)
{
bytes.push_back(byte);
@@ -512,7 +490,7 @@ void gps_l2_m_telemetry_decoder_cc::crc_verifier::zerropad_back_and_convert_to_b
void gps_l2_m_telemetry_decoder_cc::crc_verifier::zerropad_front_and_convert_to_bytes(const std::vector<int> msg_candidate, std::vector<unsigned char> &bytes)
{
std::stringstream ss;
//std::stringstream ss;
const size_t bits_per_byte = 8;
unsigned char byte = 0;
int idx_bit = 6; // insert 6 zeros at the front to fit the 250bits into a multiple of bytes
@@ -521,7 +499,7 @@ void gps_l2_m_telemetry_decoder_cc::crc_verifier::zerropad_front_and_convert_to_
{
int bit_pos_in_current_byte = (bits_per_byte - 1) - (idx_bit % bits_per_byte);
byte |= (unsigned char)(*candidate_bit_it) << bit_pos_in_current_byte;
ss << *candidate_bit_it;
// ss << *candidate_bit_it;
if (idx_bit % bits_per_byte == bits_per_byte - 1)
{
bytes.push_back(byte);
@@ -536,12 +514,14 @@ void gps_l2_m_telemetry_decoder_cc::crc_verifier::zerropad_front_and_convert_to_
// << std::setfill(' ') << std::resetiosflags(std::ios::hex);
}
void gps_l2_m_telemetry_decoder_cc::set_iono_queue(concurrent_queue<Gps_CNAV_Iono> *iono_queue)
{
d_iono_queue=iono_queue;
d_iono_queue = iono_queue;
}
void gps_l2_m_telemetry_decoder_cc::set_ephemeris_queue(concurrent_queue<Gps_CNAV_Ephemeris> *ephemeris_queue)
{
d_ephemeris_queue=ephemeris_queue;
d_ephemeris_queue = ephemeris_queue;
}
@@ -68,9 +68,11 @@ public:
void set_channel(int channel); //!< Set receiver's channel
// queues to communicate broadcasted CNAV data to other blocks of GNSS-SDR
void set_iono_queue(concurrent_queue<Gps_CNAV_Iono> *iono_queue); //!< Set iono queue
void set_ephemeris_queue(concurrent_queue<Gps_CNAV_Ephemeris> *ephemeris_queue); //!< Set ephemeris queue
void set_iono_queue(concurrent_queue<Gps_CNAV_Iono> *iono_queue); //!< Set iono queue
void set_ephemeris_queue(concurrent_queue<Gps_CNAV_Ephemeris> *ephemeris_queue); //!< Set ephemeris queue
void set_decimation(int decimation);
/*!
* \brief This is where all signal processing takes place
*/
@@ -158,7 +160,6 @@ private:
void zerropad_back_and_convert_to_bytes(const std::vector<int> msg_candidate, std::vector<unsigned char> &bytes);
} d_crc_verifier;
Gps_CNAV_Navigation_Message d_CNAV_Message;
};