Advertisement
Guest User

Untitled

a guest
Feb 8th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. //
  2. // Copyright 2010-2011 Ettus Research LLC
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17.  
  18. #include <uhd/utils/thread_priority.hpp>
  19. #include <uhd/utils/safe_main.hpp>
  20. #include <uhd/usrp/multi_usrp.hpp>
  21. #include <boost/program_options.hpp>
  22. #include <boost/thread/thread.hpp> //sleep
  23. #include <boost/format.hpp>
  24. #include <iostream>
  25. #include <complex>
  26.  
  27. namespace po = boost::program_options;
  28.  
  29. int UHD_SAFE_MAIN(int argc, char *argv[]){
  30. uhd::set_thread_priority_safe();
  31.  
  32. //variables to be set by po
  33. std::string args;
  34. double seconds_in_future;
  35. size_t total_num_samps;
  36. double rate;
  37.  
  38. //setup the program options
  39. po::options_description desc("Allowed options");
  40. desc.add_options()
  41. ("help", "help message")
  42. ("args", po::value<std::string>(&args)->default_value(""), "single uhd device address args")
  43. ("secs", po::value<double>(&seconds_in_future)->default_value(1.5), "number of seconds in the future to receive")
  44. ("nsamps", po::value<size_t>(&total_num_samps)->default_value(10000), "total number of samples to receive")
  45. ("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of incoming samples")
  46. ("dilv", "specify to disable inner-loop verbose")
  47. ;
  48. po::variables_map vm;
  49. po::store(po::parse_command_line(argc, argv, desc), vm);
  50. po::notify(vm);
  51.  
  52. //print the help message
  53. if (vm.count("help")){
  54. std::cout << boost::format("UHD RX Timed Samples %s") % desc << std::endl;
  55. return ~0;
  56. }
  57.  
  58. bool verbose = vm.count("dilv") == 0;
  59.  
  60. //create a usrp device
  61. std::cout << std::endl;
  62. std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
  63. uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
  64. std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
  65.  
  66. //set the rx sample rate
  67. std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate/1e6) << std::endl;
  68. usrp->set_rx_rate(rate);
  69. std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate()/1e6) << std::endl << std::endl;
  70.  
  71. std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
  72.  
  73. const double gps_time_at_pps = usrp->get_mboard_sensor("gps_time").to_int();
  74. const uhd::time_spec_t gps_time_at_next_pps(gps_time_at_pps + 1);
  75.  
  76. std::cout << boost::format(
  77. "gps_time_at_next_pps: %u full secs, %f frac secs"
  78. ) % gps_time_at_next_pps.get_full_secs() % gps_time_at_next_pps.get_frac_secs() << std::endl;
  79.  
  80. usrp->set_time_next_pps(gps_time_at_next_pps);
  81. boost::this_thread::sleep(boost::posix_time::seconds(1));
  82.  
  83. const uhd::time_spec_t its_the_time_now = usrp->get_time_now();
  84. std::cout << boost::format(
  85. "its_the_time_now: %u full secs, %f frac secs"
  86. ) % its_the_time_now.get_full_secs() % its_the_time_now.get_frac_secs() << std::endl;
  87.  
  88. //create a receive streamer
  89. uhd::stream_args_t stream_args("fc32"); //complex floats
  90. uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
  91.  
  92. //setup streaming
  93. std::cout << std::endl;
  94. std::cout << boost::format(
  95. "Begin streaming %u samples, %f seconds in the future..."
  96. ) % total_num_samps % seconds_in_future << std::endl;
  97. uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
  98. stream_cmd.num_samps = total_num_samps;
  99. stream_cmd.stream_now = false;
  100. stream_cmd.time_spec = gps_time_at_next_pps+uhd::time_spec_t(seconds_in_future);
  101. usrp->issue_stream_cmd(stream_cmd);
  102.  
  103. //meta-data will be filled in by recv()
  104. uhd::rx_metadata_t md;
  105.  
  106. //allocate buffer to receive with samples
  107. std::vector<std::complex<float> > buff(rx_stream->get_max_num_samps());
  108.  
  109. //the first call to recv() will block this many seconds before receiving
  110. double timeout = seconds_in_future + 0.1; //timeout (delay before receive + padding)
  111.  
  112. size_t num_acc_samps = 0; //number of accumulated samples
  113. while(num_acc_samps < total_num_samps){
  114. //receive a single packet
  115. size_t num_rx_samps = rx_stream->recv(
  116. &buff.front(), buff.size(), md, timeout, true
  117. );
  118.  
  119. //use a small timeout for subsequent packets
  120. timeout = 0.1;
  121.  
  122. //handle the error code
  123. if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) break;
  124. if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
  125. throw std::runtime_error(str(boost::format(
  126. "Unexpected error code 0x%x"
  127. ) % md.error_code));
  128. }
  129.  
  130. if(verbose) std::cout << boost::format(
  131. "Received packet: %u samples, %u full secs, %f frac secs"
  132. ) % num_rx_samps % md.time_spec.get_full_secs() % md.time_spec.get_frac_secs() << std::endl;
  133.  
  134. num_acc_samps += num_rx_samps;
  135. }
  136.  
  137. if (num_acc_samps < total_num_samps) std::cerr << "Receive timeout before all samples received..." << std::endl;
  138.  
  139. //finished
  140. std::cout << std::endl << "Done!" << std::endl << std::endl;
  141.  
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement