diff options
Diffstat (limited to 'host/examples/rx_samples_to_file.cpp')
-rw-r--r-- | host/examples/rx_samples_to_file.cpp | 207 |
1 files changed, 164 insertions, 43 deletions
diff --git a/host/examples/rx_samples_to_file.cpp b/host/examples/rx_samples_to_file.cpp index 1e833defa..b0e498125 100644 --- a/host/examples/rx_samples_to_file.cpp +++ b/host/examples/rx_samples_to_file.cpp @@ -38,16 +38,24 @@ template<typename samp_type> void recv_to_file( const std::string &wire_format, const std::string &file, size_t samps_per_buff, - int num_requested_samples + unsigned long long num_requested_samples, + double time_requested = 0.0, + bool bw_summary = false, + bool stats = false, + bool null = false, + bool enable_size_map = false, + bool continue_on_bad_packet = false ){ - int num_total_samps = 0; + unsigned long long num_total_samps = 0; //create a receive streamer uhd::stream_args_t stream_args(cpu_format,wire_format); uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); uhd::rx_metadata_t md; std::vector<samp_type> buff(samps_per_buff); - std::ofstream outfile(file.c_str(), std::ofstream::binary); + std::ofstream outfile; + if (not null) + outfile.open(file.c_str(), std::ofstream::binary); bool overflow_message = true; //setup streaming @@ -59,9 +67,20 @@ template<typename samp_type> void recv_to_file( stream_cmd.stream_now = true; stream_cmd.time_spec = uhd::time_spec_t(); rx_stream->issue_stream_cmd(stream_cmd); + + boost::system_time start = boost::get_system_time(); + unsigned long long ticks_requested = (long)(time_requested * (double)boost::posix_time::time_duration::ticks_per_second()); + boost::posix_time::time_duration ticks_diff; + boost::system_time last_update = start; + unsigned long long last_update_samps = 0; + + typedef std::map<size_t,size_t> SizeMap; + SizeMap mapSizes; while(not stop_signal_called and (num_requested_samples != num_total_samps or num_requested_samples == 0)){ - size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0); + boost::system_time now = boost::get_system_time(); + + size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0, enable_size_map); if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) { std::cout << boost::format("Timeout while streaming") << std::endl; @@ -81,17 +100,113 @@ template<typename samp_type> void recv_to_file( continue; } if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){ - throw std::runtime_error(str(boost::format( + std::string error = str(boost::format( "Unexpected error code 0x%x" - ) % md.error_code)); + ) % md.error_code); + + if (continue_on_bad_packet){ + std::cerr << error << std::endl; + continue; + } + else + throw std::runtime_error(error); } + + if (enable_size_map){ + SizeMap::iterator it = mapSizes.find(num_rx_samps); + if (it == mapSizes.end()) + mapSizes[num_rx_samps] = 0; + mapSizes[num_rx_samps] += 1; + } num_total_samps += num_rx_samps; - outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); + if (outfile.is_open()) + outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); + + if (bw_summary){ + last_update_samps += num_rx_samps; + boost::posix_time::time_duration update_diff = now - last_update; + if (update_diff.ticks() > boost::posix_time::time_duration::ticks_per_second()) { + double t = (double)update_diff.ticks() / (double)boost::posix_time::time_duration::ticks_per_second(); + double r = (double)last_update_samps / t; + std::cout << boost::format("\t%f Msps") % (r/1e6) << std::endl; + last_update_samps = 0; + last_update = now; + } + } + + ticks_diff = now - start; + if (ticks_requested > 0){ + if ((unsigned long long)ticks_diff.ticks() > ticks_requested) + break; + } } + + if (outfile.is_open()) + outfile.close(); + + if (stats){ + std::cout << std::endl; - outfile.close(); + double t = (double)ticks_diff.ticks() / (double)boost::posix_time::time_duration::ticks_per_second(); + std::cout << boost::format("Received %d samples in %f seconds") % num_total_samps % t << std::endl; + double r = (double)num_total_samps / t; + std::cout << boost::format("%f Msps") % (r/1e6) << std::endl; + + if (enable_size_map) { + std::cout << std::endl; + std::cout << "Packet size map (bytes: count)" << std::endl; + for (SizeMap::iterator it = mapSizes.begin(); it != mapSizes.end(); it++) + std::cout << it->first << ":\t" << it->second << std::endl; + } + } +} + +typedef boost::function<uhd::sensor_value_t (const std::string&)> get_sensor_fn_t; + +bool check_locked_sensor(std::vector<std::string> sensor_names, const char* sensor_name, get_sensor_fn_t get_sensor_fn, double setup_time){ + if (std::find(sensor_names.begin(), sensor_names.end(), sensor_name) == sensor_names.end()) + return false; + + boost::system_time start = boost::get_system_time(); + boost::system_time first_lock_time; + + std::cout << boost::format("Waiting for \"%s\": ") % sensor_name; + std::cout.flush(); + + while (true){ + if ((not first_lock_time.is_not_a_date_time()) and + (boost::get_system_time() > (first_lock_time + boost::posix_time::seconds(setup_time)))) + { + std::cout << " locked." << std::endl; + break; + } + + if (get_sensor_fn(sensor_name).to_bool()){ + if (first_lock_time.is_not_a_date_time()) + first_lock_time = boost::get_system_time(); + std::cout << "+"; + std::cout.flush(); + } + else{ + first_lock_time = boost::system_time(); //reset to 'not a date time' + + if (boost::get_system_time() > (start + boost::posix_time::seconds(setup_time))){ + std::cout << std::endl; + throw std::runtime_error(str(boost::format("timed out waiting for consecutive locks on sensor \"%s\"") % sensor_name)); + } + + std::cout << "_"; + std::cout.flush(); + } + + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + } + + std::cout << std::endl; + + return true; } int UHD_SAFE_MAIN(int argc, char *argv[]){ @@ -100,7 +215,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ //variables to be set by po std::string args, file, type, ant, subdev, ref, wirefmt; size_t total_num_samps, spb; - double rate, freq, gain, bw; + double rate, freq, gain, bw, total_time, setup_time; //setup the program options po::options_description desc("Allowed options"); @@ -110,15 +225,23 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ ("file", po::value<std::string>(&file)->default_value("usrp_samples.dat"), "name of the file to write binary samples to") ("type", po::value<std::string>(&type)->default_value("short"), "sample type: double, float, or short") ("nsamps", po::value<size_t>(&total_num_samps)->default_value(0), "total number of samples to receive") + ("time", po::value<double>(&total_time)->default_value(0), "total number of seconds to receive") ("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer") - ("rate", po::value<double>(&rate), "rate of incoming samples") - ("freq", po::value<double>(&freq), "RF center frequency in Hz") + ("rate", po::value<double>(&rate)->default_value(1e6), "rate of incoming samples") + ("freq", po::value<double>(&freq)->default_value(0.0), "RF center frequency in Hz") ("gain", po::value<double>(&gain), "gain for the RF chain") ("ant", po::value<std::string>(&ant), "daughterboard antenna selection") ("subdev", po::value<std::string>(&subdev), "daughterboard subdevice specification") ("bw", po::value<double>(&bw), "daughterboard IF filter bandwidth in Hz") ("ref", po::value<std::string>(&ref)->default_value("internal"), "waveform type (internal, external, mimo)") ("wirefmt", po::value<std::string>(&wirefmt)->default_value("sc16"), "wire format (sc8 or sc16)") + ("setup", po::value<double>(&setup_time)->default_value(1.0), "seconds of setup time") + ("progress", "show average bandwidth on exit") + ("stats", "periodically display short-term bandwidth") + ("sizemap", "track packet size and display breakdown on exit") + ("null", "run without writing to file") + ("continue", "don't abort on a bad packet") + ("skip-lo", "skip checking LO lock status") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); @@ -129,6 +252,15 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ std::cout << boost::format("UHD RX samples to file %s") % desc << std::endl; return ~0; } + + bool bw_summary = vm.count("progress") > 0; + bool stats = vm.count("stats") > 0; + bool null = vm.count("null") > 0; + bool enable_size_map = vm.count("sizemap") > 0; + bool continue_on_bad_packet = vm.count("continue") > 0; + + if (enable_size_map) + std::cout << "Packet size tracking enabled - will only recv one packet at a time!" << std::endl; //create a usrp device std::cout << std::endl; @@ -144,8 +276,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl; //set the sample rate - if (not vm.count("rate")){ - std::cerr << "Please specify the sample rate with --rate" << std::endl; + if (rate <= 0.0){ + std::cerr << "Please specify a valid sample rate" << std::endl; return ~0; } std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate/1e6) << std::endl; @@ -153,13 +285,11 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate()/1e6) << std::endl << std::endl; //set the center frequency - if (not vm.count("freq")){ - std::cerr << "Please specify the center frequency with --freq" << std::endl; - return ~0; - } - std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq/1e6) << std::endl; - usrp->set_rx_freq(freq); - std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq()/1e6) << std::endl << std::endl; + if (vm.count("freq")){ //with default of 0.0 this will always be true + std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq/1e6) << std::endl; + usrp->set_rx_freq(freq); + std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq()/1e6) << std::endl << std::endl; + } //set the rf gain if (vm.count("gain")){ @@ -178,37 +308,28 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ //set the antenna if (vm.count("ant")) usrp->set_rx_antenna(ant); - boost::this_thread::sleep(boost::posix_time::seconds(1)); //allow for some setup time + boost::this_thread::sleep(boost::posix_time::seconds(setup_time)); //allow for some setup time - //Check Ref and LO Lock detect - std::vector<std::string> sensor_names; - sensor_names = usrp->get_rx_sensor_names(0); - if (std::find(sensor_names.begin(), sensor_names.end(), "lo_locked") != sensor_names.end()) { - uhd::sensor_value_t lo_locked = usrp->get_rx_sensor("lo_locked",0); - std::cout << boost::format("Checking RX: %s ...") % lo_locked.to_pp_string() << std::endl; - UHD_ASSERT_THROW(lo_locked.to_bool()); - } - sensor_names = usrp->get_mboard_sensor_names(0); - if ((ref == "mimo") and (std::find(sensor_names.begin(), sensor_names.end(), "mimo_locked") != sensor_names.end())) { - uhd::sensor_value_t mimo_locked = usrp->get_mboard_sensor("mimo_locked",0); - std::cout << boost::format("Checking RX: %s ...") % mimo_locked.to_pp_string() << std::endl; - UHD_ASSERT_THROW(mimo_locked.to_bool()); - } - if ((ref == "external") and (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end())) { - uhd::sensor_value_t ref_locked = usrp->get_mboard_sensor("ref_locked",0); - std::cout << boost::format("Checking RX: %s ...") % ref_locked.to_pp_string() << std::endl; - UHD_ASSERT_THROW(ref_locked.to_bool()); - } + //check Ref and LO Lock detect + if (not vm.count("skip-lo")){ + check_locked_sensor(usrp->get_rx_sensor_names(0), "lo_locked", boost::bind(&uhd::usrp::multi_usrp::get_rx_sensor, usrp, _1, 0), setup_time); + if (ref == "mimo") + check_locked_sensor(usrp->get_mboard_sensor_names(0), "mimo_locked", boost::bind(&uhd::usrp::multi_usrp::get_mboard_sensor, usrp, _1, 0), setup_time); + if (ref == "external") + check_locked_sensor(usrp->get_mboard_sensor_names(0), "ref_locked", boost::bind(&uhd::usrp::multi_usrp::get_mboard_sensor, usrp, _1, 0), setup_time); + } if (total_num_samps == 0){ std::signal(SIGINT, &sig_int_handler); std::cout << "Press Ctrl + C to stop streaming..." << std::endl; } +#define recv_to_file_args(format) \ + (usrp, format, wirefmt, file, spb, total_num_samps, total_time, bw_summary, stats, null, enable_size_map, continue_on_bad_packet) //recv to file - if (type == "double") recv_to_file<std::complex<double> >(usrp, "fc64", wirefmt, file, spb, total_num_samps); - else if (type == "float") recv_to_file<std::complex<float> >(usrp, "fc32", wirefmt, file, spb, total_num_samps); - else if (type == "short") recv_to_file<std::complex<short> >(usrp, "sc16", wirefmt, file, spb, total_num_samps); + if (type == "double") recv_to_file<std::complex<double> >recv_to_file_args("fc64"); + else if (type == "float") recv_to_file<std::complex<float> >recv_to_file_args("fc32"); + else if (type == "short") recv_to_file<std::complex<short> >recv_to_file_args("sc16"); else throw std::runtime_error("Unknown type " + type); //finished |