Пример #1
0
void uhd_device::restart(uhd::time_spec_t ts)
{
	uhd::stream_cmd_t cmd = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
	usrp_dev->issue_stream_cmd(cmd);

	flush_recv(50);

	usrp_dev->set_time_now(ts);
	aligned = false;

	cmd = uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;
	cmd.stream_now = true;
	usrp_dev->issue_stream_cmd(cmd);
}
Пример #2
0
/***********************************************************************
 * Data capture routine
 **********************************************************************/
static void capture_samples(
    uhd::usrp::multi_usrp::sptr usrp,
    uhd::rx_streamer::sptr rx_stream,
    std::vector<samp_type > &buff,
    const size_t nsamps_requested
){
    buff.resize(nsamps_requested);
    uhd::rx_metadata_t md;

    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
    stream_cmd.num_samps = buff.size();
    stream_cmd.stream_now = true;
    usrp->issue_stream_cmd(stream_cmd);
    const size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md);

    //validate the received data
    if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
        throw std::runtime_error(str(boost::format(
            "Receiver error: %s"
        ) % md.strerror()));
    }
    //we can live if all the data didnt come in
    if (num_rx_samps > buff.size()/2){
        buff.resize(num_rx_samps);
        return;
    }
    if (num_rx_samps != buff.size()){
        throw std::runtime_error("did not get all the samples requested");
    }
}
Пример #3
0
void init_stream() {
    boost::this_thread::sleep(boost::posix_time::milliseconds(WARM_UP_TIME));
    stream_cmd.time_spec = time_start_recv  = uhd::time_spec_t(2.0) + usrp->get_time_now();
	cout << "Time to start receiving: " << time_start_recv.get_real_secs() << endl;
    stream_cmd.stream_now   = false;
    usrp->issue_stream_cmd(stream_cmd);
}
Пример #4
0
/***********************************************************************
 * RX Hammer
 **********************************************************************/
void rx_hammer(uhd::usrp::multi_usrp::sptr usrp, const std::string &rx_cpu, const std::string &rx_otw){
    uhd::set_thread_priority_safe();

    //create a receive streamer
    uhd::stream_args_t stream_args(rx_cpu, rx_otw);
    for (size_t ch = 0; ch < usrp->get_num_mboards(); ch++) //linear channel mapping
        stream_args.channels.push_back(ch);
    uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);

    //print pre-test summary
    std::cout << boost::format(
        "Testing receive rate %f Msps"
    ) % (usrp->get_rx_rate()/1e6) << std::endl;

    //setup variables and allocate buffer
    uhd::rx_metadata_t md;
    const size_t max_samps_per_packet = rx_stream->get_max_num_samps();
    std::vector<char> buff(max_samps_per_packet*uhd::convert::get_bytes_per_item(rx_cpu));
    std::vector<void *> buffs;
    for (size_t ch = 0; ch < stream_args.channels.size(); ch++)
        buffs.push_back(&buff.front()); //same buffer for each channel
    bool had_an_overflow = false;
    uhd::time_spec_t last_time;
    const double rate = usrp->get_rx_rate();
    double timeout = 1;

    uhd::stream_cmd_t cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
    cmd.time_spec = usrp->get_time_now() + uhd::time_spec_t(0.05);
    cmd.stream_now = (buffs.size() == 1);
    srand( time(NULL) );

    while (not boost::this_thread::interruption_requested()){
        cmd.num_samps = rand() % 100000;
        usrp->issue_stream_cmd(cmd);
        num_rx_samps += rx_stream->recv(buffs, max_samps_per_packet, md, timeout, true);

        //handle the error codes
        switch(md.error_code){
        case uhd::rx_metadata_t::ERROR_CODE_NONE:
            if (had_an_overflow){
                had_an_overflow = false;
                num_dropped_samps += boost::math::iround((md.time_spec - last_time).get_real_secs()*rate);
            }
            break;

        case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW:
            had_an_overflow = true;
            last_time = md.time_spec;
            num_overflows++;
            break;

        default:
            std::cerr << "Error code: " << md.error_code << std::endl;
            std::cerr << "Unexpected error on recv, continuing..." << std::endl;
            break;
        }
    }
}
Пример #5
0
bool uhd_device::stop()
{
	uhd::stream_cmd_t stream_cmd = 
		uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;

	usrp_dev->issue_stream_cmd(stream_cmd);

	started = false;
	return true;
}
Пример #6
0
/***********************************************************************
 * Data capture routine
 **********************************************************************/
static void capture_samples(
    uhd::usrp::multi_usrp::sptr usrp,
    uhd::rx_streamer::sptr rx_stream,
    std::vector<samp_type > &buff,
    const size_t nsamps_requested)
{
    buff.resize(nsamps_requested);
    uhd::rx_metadata_t md;

    // Right after the stream is started, there will be transient data.
    // That transient data is discarded and only "good" samples are returned.
    size_t nsamps_to_discard = size_t(usrp->get_rx_rate() * 0.001); // 1ms to be discarded
    std::vector<samp_type> discard_buff(nsamps_to_discard);

    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
    stream_cmd.num_samps = buff.size() + nsamps_to_discard;
    stream_cmd.stream_now = true;
    usrp->issue_stream_cmd(stream_cmd);
    size_t num_rx_samps = 0;

    // Discard the transient samples.
    rx_stream->recv(&discard_buff.front(), discard_buff.size(), md);
    if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)
    {
        throw std::runtime_error(str(boost::format(
            "Receiver error: %s"
        ) % md.strerror()));
    }

    // Now capture the data we want
    num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md);

    //validate the received data
    if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)
    {
        throw std::runtime_error(str(boost::format(
            "Receiver error: %s"
        ) % md.strerror()));
    }

    //we can live if all the data didnt come in
    if (num_rx_samps > buff.size()/2)
    {
        buff.resize(num_rx_samps);
        return;
    }
    if (num_rx_samps != buff.size())
        throw std::runtime_error("did not get all the samples requested");
}
Пример #7
0
bool uhd_device::stop()
{
	if (!started)
		return false;

	started = false;

	uhd::stream_cmd_t stream_cmd = 
		uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;

	usrp_dev->issue_stream_cmd(stream_cmd);

	delete async_event_thrd;

	return true;
}
Пример #8
0
static inline void test_device(
    uhd::usrp::multi_usrp::sptr usrp,
    double rx_rate_sps,
    double duration_secs
){
    const size_t max_samps_per_packet = usrp->get_device()->get_max_recv_samps_per_packet();
    std::cout << boost::format("Testing receive rate %f Msps (%f second run)") % (rx_rate_sps/1e6) % duration_secs << std::endl;

    //allocate recv buffer and metatdata
    uhd::rx_metadata_t md;
    std::vector<std::complex<float> > buff(max_samps_per_packet);

    //flush the buffers in the recv path
    while(usrp->get_device()->recv(
        &buff.front(), buff.size(), md,
        uhd::io_type_t::COMPLEX_FLOAT32,
        uhd::device::RECV_MODE_ONE_PACKET
    )){
        /* NOP */
    };

    //declare status variables
    bool got_first_packet = false;
    size_t total_recv_packets = 0;
    size_t total_lost_samples = 0;
    size_t total_recv_samples = 0;
    uhd::time_spec_t initial_time_spec;
    uhd::time_spec_t next_expected_time_spec;

    usrp->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
    do {
        size_t num_rx_samps = usrp->get_device()->recv(
            &buff.front(), buff.size(), md,
            uhd::io_type_t::COMPLEX_FLOAT32,
            uhd::device::RECV_MODE_ONE_PACKET
        );

        //handle the error codes
        switch(md.error_code){
        case uhd::rx_metadata_t::ERROR_CODE_NONE:
        case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW:
            break;

        default:
            std::cerr << "Error code: " << md.error_code << std::endl;
            std::cerr << "Unexpected error on recv, exit test..." << std::endl;
            return;
        }

        if (not md.has_time_spec){
            std::cerr << "Metadata missing time spec, exit test..." << std::endl;
            return;
        }

        total_recv_samples += num_rx_samps;
        total_recv_packets++;

        if (not got_first_packet){
            initial_time_spec = md.time_spec;
            next_expected_time_spec = initial_time_spec;
            got_first_packet = true;
        }

        double approx_lost_samps = rx_rate_sps*(md.time_spec - next_expected_time_spec).get_real_secs();
        total_lost_samples += std::max(0, boost::math::iround(approx_lost_samps));
        next_expected_time_spec = md.time_spec + uhd::time_spec_t(0, num_rx_samps, rx_rate_sps);

    } while((next_expected_time_spec - initial_time_spec) < uhd::time_spec_t(duration_secs));
    usrp->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);

    //print a summary
    std::cout << std::endl; //go to newline, recv may spew SXSYSZ...
    std::cout << boost::format("    Received packets: %d") % total_recv_packets << std::endl;
    std::cout << boost::format("    Received samples: %d") % total_recv_samples << std::endl;
    std::cout << boost::format("    Lost samples: %d") % total_lost_samples << std::endl;
    size_t packets_lost = boost::math::iround(double(total_lost_samples)/max_samps_per_packet);
    std::cout << boost::format("    Lost packets: %d (approximate)") % packets_lost << std::endl;
    double actual_rx_rate_sps = (total_recv_samples*rx_rate_sps)/(total_recv_samples+total_lost_samples);
    std::cout << boost::format("    Sustained receive rate: %f Msps") % (actual_rx_rate_sps/1e6) << std::endl;
    std::cout << std::endl << std::endl;
}
Пример #9
0
int calculateTask(  const char*                   outputFileName,
                    const int                     binSize,
                    const unsigned long long	    maximum_samples,
                    uhd::usrp::multi_usrp::sptr&  usrp )
{
  ///////////////////////////////////////////////////////////
  //
  //Initialization Section
  ///////////////////////////////////////////////////////////

  //Number of bytes in a single-precision float
  const int   FLOAT_SIZE = sizeof(float);
  //Initialize and open the input/output files
  FILE *outputFile;

  if(!openFiles( outputFileName, outputFile ))
    return 0;

  //Setup the input buffer and tracking variables
  int                   return_code       = 1;

  //Setup the USRP for streaming
  vector<float complex>   usrpBuffer( binSize );
  float                   energy;
  uhd::stream_args_t      stream_args(__USRP_CPU_FMT, __USRP_WIRE_FMT );
  uhd::rx_streamer::sptr  usrp_rx_stream = usrp->get_rx_stream(stream_args);
  uhd::rx_metadata_t      rx_md;
  unsigned long long int  samples_recorded = 0;
  unsigned long long int  buffer_samples_recorded = 0;
  uhd::stream_cmd_t       usrp_stream_command(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);

  usrp_stream_command.stream_now  = true;
  usrp_stream_command.time_spec   = uhd::time_spec_t();

  ///////////////////////////////////////////////////////////
  //
  //Work Section
  ///////////////////////////////////////////////////////////
  cout << "Begin Data Collection" << endl;
  //Start streaming!
  usrp->issue_stream_cmd( usrp_stream_command );

  while( (samples_recorded < maximum_samples) and return_code )
  {
    energy = 0.0f;
    //Read in the I-Q of fft_interval_size samples...
    buffer_samples_recorded = usrp_rx_stream->recv( &usrpBuffer.front(),
                                                    usrpBuffer.size(),
                                                    rx_md );

    //Check the USRP for errors (including Overflow indication)
    if( rx_md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE )
    {
      //There was a USRP-related problem
      switch( rx_md.error_code ){
        case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW:
          cout << "O";
          break;
        case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT:
          cout << "USRP Timeout" << endl;
          return_code = 0;
          break;
        default:
          cout << "Unexpected USRP Error: " << rx_md.error_code;
          return_code = 0;
      }
    }
    samples_recorded += buffer_samples_recorded;
    //Compute magnitude (we don't want to store phase information)
    for(int i = 0; i < binSize; i++ )
      energy += pow(cabsf( usrpBuffer[i] ), 2);

    //Write results to the output file
    fwrite(&energy, FLOAT_SIZE, 1, outputFile );
  }

  ///////////////////////////////////////////////////////////
  //
  //Cleanup Section
  ///////////////////////////////////////////////////////////

  //Toss out any leftovers and cleanup
  fclose(outputFile);

  return 1;
}
Пример #10
0
void transceive(
    uhd::usrp::multi_usrp::sptr usrp,
    uhd::tx_streamer::sptr tx_stream,
    uhd::rx_streamer::sptr rx_stream,
    unsigned int npulses,
    float pulse_time,
    //std::complex<int16_t>* txbuff,
    std::vector<std::complex<int16_t> >* txbuff0,
    std::vector<std::complex<int16_t> >* txbuff1,
    float tx_ontime,
    std::complex<int16_t>** outdata,
    size_t samps_per_pulse
){
    int debug = 0;
    if (debug){
        std::cout << "samps_per_pulse: " << samps_per_pulse << std::endl;
    }
    //create metadeta tags for transmit streams
    uhd::time_spec_t start_time = usrp->get_time_now() + 0.05;
    uhd::tx_metadata_t md;
    md.start_of_burst = true;
    md.end_of_burst = false;
    md.has_time_spec = true;
    md.time_spec = start_time;
    std::vector<std::complex<int16_t> *> vec_ptr;
    vec_ptr.resize(1);
    //vec_ptr[0] = &txbuff->front();
    
    usrp->set_gpio_attr("RXA","CTRL",0x0, TR_BIT); //GPIO mode
    usrp->set_gpio_attr("RXA","DDR",TR_BIT, TR_BIT); //Direction out
    
    //create metadata tags for receive stream
    uhd::rx_metadata_t rxmd;
    std::vector<std::complex<int16_t> > buff(samps_per_pulse,0);
    if (verbose) std::cout << "rx buff size: " << buff.size() << std::endl;
    if (verbose) std::cout << "tx buff size: " << txbuff0->size() << std::endl;
    uhd::stream_cmd_t stream_cmd = uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE;
    stream_cmd.num_samps = npulses*samps_per_pulse;
    stream_cmd.stream_now = false;
    stream_cmd.time_spec = start_time + 22 / usrp->get_rx_rate(); //Digital hardware delay is 22 samples long.  Found by experiment.
    if (verbose) std::cout << "time spec: " << stream_cmd.time_spec.get_real_secs() << std::endl;
    
    //loop for every pulse in the sequence
    size_t spb;
    std::vector<std::complex<int16_t>* > rx_dptr;
    rx_dptr.resize(usrp->get_rx_num_channels());
    spb = tx_stream->get_max_num_samps();
    if (verbose) std::cout << "npulses: " << npulses << std::endl;
    boost::thread_group rx_threads;
    boost::thread_group tx_threads;
    for (int ipulse=0; ipulse<npulses; ipulse++){
        if (debug) std::cout << "pulse number: " << ipulse << std::endl;
        for (size_t ichan=0; ichan<usrp->get_rx_num_channels(); ichan++){
         rx_dptr[ichan] = ipulse*samps_per_pulse + outdata[ichan];
        }
        
        float timeout = 1.1;
        
        //usrp->set_command_time(start_time-50e-6,0);
        //usrp->set_gpio_attr("RXA","OUT",TR_BIT, TR_BIT);
        
        if (ipulse==0){
            if (verbose) std::cout << "time spec: " << stream_cmd.time_spec.get_real_secs() << std::endl;
            if (verbose) std::cout << "Issuing stream command to start collecting samples\n";
            usrp->issue_stream_cmd(stream_cmd);
        }
        
        //usrp->set_command_time(start_time+tx_ontime,0);
        //usrp->set_gpio_attr("RXA","OUT",0x0, TR_BIT);
        
        size_t acc_samps=0;
        if (ipulse%2 == 0) {
            vec_ptr[0] = &txbuff0->front();
        }
        if (ipulse%2 == 1) {
            vec_ptr[0] = &txbuff1->front();
        }
        
        if (ipulse != npulses-1) {
             tx_threads.create_thread(boost::bind(tx_worker,
                 txbuff0->size(), tx_stream, start_time, vec_ptr[0], 0));
        }
        if (ipulse == npulses-1) {
             tx_threads.create_thread(boost::bind(tx_worker,
                 txbuff0->size(), tx_stream, start_time, vec_ptr[0], 1));
        }
        
        rx_threads.join_all();
        rx_threads.create_thread(boost::bind(rx_worker,
         rx_stream, samps_per_pulse, rx_dptr));
        
        //for (int k=0; k<10; k++){
        // //std::cout << "raw data: " << outdata[0][i][k] << "\t" << outdata[1][i][k] << std::endl;
        // std::cout << "raw data: " << rx_dptr[0][k] << " " << rx_dptr[1][k] << std::endl;
        //}
        //for (int k=0; k<samps_per_pulse; k++)
        //    outdata[i][k] += buff[k];
        
        
        start_time += float(pulse_time);
    }
    tx_threads.join_all();
    rx_threads.join_all();
}
Пример #11
0
int UHD_SAFE_MAIN(int argc, char *argv[]){
	size_t rx_cnt;
	rx_cnt = 0;

	uhd::set_thread_priority_safe();
	uhd::time_spec_t refer;

	po::options_description desc("Allowed options");
	desc.add_options()
		("help", "help message")
		("r0", po::value<string>(&usrp_ip)->default_value("addr=192.168.10.10" ), "usrp's IP")
		("in", po::value<string>(&in_name)->default_value("wn_trace/src_data_1.bin"), "binary samples file")
		("out", po::value<string>(&out_name)->default_value("wn_trace/recv_signal.bin"), "signal file")
		("i", po::value<double>(&inter)->default_value(SAMPLE_P), "interval of two sampling")
		("f", po::value<double>(&freq)->default_value(2.49), "RF center frequency in Hz")
		("g", po::value<double>(&gain)->default_value(30.0), "gain for the RF chain")
		("s", po::value<double>(&r_sec)->default_value(1), "recording seconds")
		("c", po::value<size_t>(&r_cnt)->default_value(90), "round count");

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);
	po::notify(vm);


	if (vm.count("help")){
		cout << boost::format("UHD TX samples from file %s") % desc << endl;
		return ~0;
	}
	// Initial systems
	init_sys();

	size_t cleaning, done_cleaning;
	done_cleaning = 0;
	cleaning = 0;
	while(cleaning < ANT_CNT) {
		if (!done_cleaning) {
			usrp->get_device()->recv(pkt, SYM_LEN, rx_md, C_FLOAT32, R_ONE_PKT);
			if(rx_md.time_spec.get_real_secs() >= time_start_recv.get_real_secs()) {
				done_cleaning = 1;
				cleaning++;
			}
		}
		// cout << cleaning << "-" << done_cleaning << " Clean ant" << i << " buff:" << rx_md.time_spec.get_real_secs() << endl;
	}
	
	// TODO:
	// Receive Signals
	// HINT: You have to receive signals here
	// How many signals you have to recv? Ans: s_cnt
	// using rx_cnt+=usrp->get_device()->recv(...)
	// using pkt to record the received samples
	
	// remove content of within while loop
	cout << endl << "# of recv samples: " << s_cnt << endl;
	gr_complex *current = pkt;
	while(rx_cnt < s_cnt) {
		size_t  read_cnt = 80;
		//At last recv(), modify read_cnt to receive the remaining samples
		if (s_cnt - rx_cnt < read_cnt){
		  read_cnt = s_cnt - rx_cnt;
		}
			// what is the number of remaining samples? read_cnt = ...;
		rx_cnt += usrp->get_device()->recv(current, read_cnt, rx_md, C_FLOAT32, R_ONE_PKT);
		current = current + read_cnt;
		//rx_cnt is the total number you have received
		//cout << rx_cnt << " hello " << endl;
		cout<<abs(*pkt)<<endl<<endl;
		if (rx_cnt < 100)
			cout << "Ant" << " recving at " << rx_md.time_spec.get_real_secs() << endl;
	}
	

	stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
	usrp->issue_stream_cmd(stream_cmd);
	
	boost::this_thread::sleep(boost::posix_time::seconds(1)); //allow for some setup time
	
	
	// End systems
	dump_signals();
	end_sys();	
	
	cout << "Terminate systems ... " << endl << endl;
	return 0;
}
Пример #12
0
void storeDataX(uhd::rx_streamer::sptr rx_stream, uhd::usrp::multi_usrp::sptr dev, size_t buffer_size, uint nDetect){



  // Create storage for a single buffer from USRP
  short *buff_short;
  buff_short=new short[2*buffer_size]; 
 
  short *storage_short;
  storage_short=new short [2*nDetect];

  uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
	  
    std::cout << "Stop the transmitter by pressing ctrl-c \n";
	  
      stream_cmd.num_samps = buffer_size;
      stream_cmd.stream_now = true;
    
       stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;

       dev->issue_stream_cmd(stream_cmd);


  uhd::rx_metadata_t md;
  size_t n_rx_samps=0;

  int n_rx_last;

  while (1){
    n_rx_samps=0;
    // Fill the storage buffer loop
    while (n_rx_samps<nDetect){
      n_rx_last=0;
      // Fill buff_short
      while (n_rx_last==0) {
	n_rx_last=rx_stream->recv(&buff_short[0], buffer_size, md, 3.0);
	//std::this_thread::yield();
      };

      sec_count++;
      // Check if no overflow
      if (n_rx_last!=(int)buffer_size) {
	std::cerr << "I expect the buffer size to be always the same!\n";
	std::cout<<"Read only:"<<n_rx_last<<"\n";
	std::cout<<"Buffer:"<<buffer_size<<"\n";
	exit(1); 
      };
      
      // Fill storage
      int i1=2*n_rx_samps;
      int i2=0;   
      while ((i1<(int) (2*nDetect)) && (i2<2*((int) buffer_size))){	  
	storage_short[i1]=buff_short[i2];
	i1++; i2++;
      };
      
      //storage_short=buff_short;
      n_rx_samps=n_rx_samps+n_rx_last;
      //std::cout << "n_rx_samps=" << n_rx_samps  << std::endl;	 
    }//storage_short now full


    mtxQ.lock();
    bufferQ.push(buff_short);
    mtxQ.unlock();
    //delete buff_short;
    buff_short=new short [2*buffer_size]; // Change memory cell used

    //usleep(1000000/4);
    sem_post( &isReady); // Gives the start to detection part

  }//end while 1
}