Advertisement
carruinar

TxGPS

Mar 18th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. #include <uhd/utils/thread_priority.hpp>
  2. #include <uhd/utils/safe_main.hpp>
  3. #include <uhd/usrp/multi_usrp.hpp>
  4. #include <boost/program_options.hpp>
  5. #include <boost/format.hpp>
  6. #include <boost/thread.hpp>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <complex>
  10.  
  11. namespace po = boost::program_options;
  12.  
  13. template<typename samp_type> void send_from_file(
  14.     uhd::usrp::multi_usrp::sptr usrp,
  15.     const std::string &cpu_format,
  16.     const std::string &file,
  17.     size_t samps_per_buff
  18. ){
  19.     //create a transmit streamer
  20.     uhd::stream_args_t stream_args(cpu_format);
  21.     uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
  22.  
  23.     uhd::tx_metadata_t md;
  24.     md.start_of_burst = false;
  25.     md.end_of_burst = false;
  26.     std::vector<samp_type> buff(samps_per_buff);
  27.     std::ifstream infile(file.c_str(), std::ifstream::binary);
  28.  
  29.     //loop until the entire file has been read
  30.     while(not md.end_of_burst){
  31.  
  32.         infile.read((char*)&buff.front(), buff.size()*sizeof(samp_type));
  33.         size_t num_tx_samps = infile.gcount()/sizeof(samp_type);
  34.  
  35.         md.end_of_burst = infile.eof();
  36.  
  37.         tx_stream->send(&buff.front(), num_tx_samps, md);
  38.     }
  39.  
  40.     infile.close();
  41. }
  42.  
  43. int UHD_SAFE_MAIN(int argc, char *argv[]){
  44.     uhd::set_thread_priority_safe();
  45.  
  46.     //variables to be set by po
  47.     std::string args, file, type, ant, subdev, ref;
  48.     size_t spb;
  49.     double rate, freq, gain, bw;
  50.  
  51.     //setup the program options
  52.     po::options_description desc("Allowed options");
  53.     desc.add_options()
  54.         ("help", "help message")
  55.         ("args", po::value<std::string>(&args)->default_value(""), "multi uhd device address args")
  56.         ("file", po::value<std::string>(&file)->default_value("/home/caruiz_ext/Escritorio/prueba/2013_04_04_GNSS_SIGNAL_at_CTTC_SPAIN/example.dat"), "name of the file to read binary samples from")
  57.         ("type", po::value<std::string>(&type)->default_value("float"), "sample type: double, float, or short")
  58.         ("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer")
  59.         ("rate", po::value<double>(&rate), "rate of outgoing samples")
  60.         ("freq", po::value<double>(&freq), "RF center frequency in Hz")
  61.         ("gain", po::value<double>(&gain), "gain for the RF chain")
  62.         ("ant", po::value<std::string>(&ant), "daughterboard antenna selection")
  63.         ("subdev", po::value<std::string>(&subdev), "daughterboard subdevice specification")
  64.         ("bw", po::value<double>(&bw), "daughterboard IF filter bandwidth in Hz")
  65.         ("ref", po::value<std::string>(&ref)->default_value("internal"), "waveform type (internal, external, mimo)")
  66.     ;
  67.     po::variables_map vm;
  68.     po::store(po::parse_command_line(argc, argv, desc), vm);
  69.     po::notify(vm);
  70.  
  71.     //print the help message
  72.     if (vm.count("help")){
  73.         std::cout << boost::format("UHD TX samples from file %s") % desc << std::endl;
  74.         return ~0;
  75.     }
  76.  
  77.     //create a usrp device
  78.     std::cout << std::endl;
  79.     std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
  80.     uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
  81.  
  82.     //Lock mboard clocks
  83.     usrp->set_clock_source(ref);
  84.  
  85.     //always select the subdevice first, the channel mapping affects the other settings
  86.     if (vm.count("subdev")) usrp->set_tx_subdev_spec(subdev);
  87.  
  88.     std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
  89.  
  90.     //set the sample rate
  91.     if (not vm.count("rate")){
  92.         std::cerr << "Please specify the sample rate with --rate" << std::endl;
  93.         return ~0;
  94.     }
  95.     std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate/1e6) << std::endl;
  96.     usrp->set_tx_rate(rate);
  97.     std::cout << boost::format("Actual TX Rate: %f Msps...") % (usrp->get_tx_rate()/1e6) << std::endl << std::endl;
  98.  
  99.     //set the center frequency
  100.     if (not vm.count("freq")){
  101.         std::cerr << "Please specify the center frequency with --freq" << std::endl;
  102.         return ~0;
  103.     }
  104.     std::cout << boost::format("Setting TX Freq: %f MHz...") % (freq/1e6) << std::endl;
  105.     usrp->set_tx_freq(freq);
  106.     std::cout << boost::format("Actual TX Freq: %f MHz...") % (usrp->get_tx_freq()/1e6) << std::endl << std::endl;
  107.  
  108.     //set the rf gain
  109.     if (vm.count("gain")){
  110.         std::cout << boost::format("Setting TX Gain: %f dB...") % gain << std::endl;
  111.         usrp->set_tx_gain(gain);
  112.         std::cout << boost::format("Actual TX Gain: %f dB...") % usrp->get_tx_gain() << std::endl << std::endl;
  113.     }
  114.  
  115.     //set the IF filter bandwidth
  116.     if (vm.count("bw")){
  117.         std::cout << boost::format("Setting TX Bandwidth: %f MHz...") % bw << std::endl;
  118.         usrp->set_tx_bandwidth(bw);
  119.         std::cout << boost::format("Actual TX Bandwidth: %f MHz...") % usrp->get_tx_bandwidth() << std::endl << std::endl;
  120.     }
  121.  
  122.     //set the antenna
  123.     if (vm.count("ant")) usrp->set_tx_antenna(ant);
  124.  
  125.     boost::this_thread::sleep(boost::posix_time::seconds(1)); //allow for some setup time
  126.  
  127.     //Check Ref and LO Lock detect
  128.     std::vector<std::string> sensor_names;
  129.     sensor_names = usrp->get_tx_sensor_names(0);
  130.     if (std::find(sensor_names.begin(), sensor_names.end(), "lo_locked") != sensor_names.end()) {
  131.         uhd::sensor_value_t lo_locked = usrp->get_tx_sensor("lo_locked",0);
  132.         std::cout << boost::format("Checking TX: %s ...") % lo_locked.to_pp_string() << std::endl;
  133.         UHD_ASSERT_THROW(lo_locked.to_bool());
  134.     }
  135.     sensor_names = usrp->get_mboard_sensor_names(0);
  136.     if ((ref == "mimo") and (std::find(sensor_names.begin(), sensor_names.end(), "mimo_locked") != sensor_names.end())) {
  137.         uhd::sensor_value_t mimo_locked = usrp->get_mboard_sensor("mimo_locked",0);
  138.         std::cout << boost::format("Checking TX: %s ...") % mimo_locked.to_pp_string() << std::endl;
  139.         UHD_ASSERT_THROW(mimo_locked.to_bool());
  140.     }
  141.     if ((ref == "external") and (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end())) {
  142.         uhd::sensor_value_t ref_locked = usrp->get_mboard_sensor("ref_locked",0);
  143.         std::cout << boost::format("Checking TX: %s ...") % ref_locked.to_pp_string() << std::endl;
  144.         UHD_ASSERT_THROW(ref_locked.to_bool());
  145.     }
  146.  
  147.     //send from file
  148.     if (type == "double") send_from_file<std::complex<double> >(usrp, "fc64", file, spb);
  149.     else if (type == "float") send_from_file<std::complex<float> >(usrp, "fc32", file, spb);
  150.     else if (type == "short") send_from_file<std::complex<short> >(usrp, "sc16", file, spb);
  151.     else throw std::runtime_error("Unknown type " + type);
  152.  
  153.     //finished
  154.     std::cout << std::endl << "Done!" << std::endl << std::endl;
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement