2010-03-24 01:17:47 +00:00
|
|
|
//
|
2011-01-14 00:22:07 +00:00
|
|
|
// Copyright 2010-2011 Ettus Research LLC
|
2010-03-24 01:17:47 +00:00
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
|
2010-06-17 02:18:06 +00:00
|
|
|
#include <uhd/utils/thread_priority.hpp>
|
2010-03-27 08:02:58 +00:00
|
|
|
#include <uhd/utils/safe_main.hpp>
|
2011-02-01 02:01:27 +00:00
|
|
|
#include <uhd/usrp/multi_usrp.hpp>
|
2010-03-24 01:17:47 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <complex>
|
|
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
2010-03-27 08:02:58 +00:00
|
|
|
int UHD_SAFE_MAIN(int argc, char *argv[]){
|
2010-06-17 02:18:06 +00:00
|
|
|
uhd::set_thread_priority_safe();
|
|
|
|
|
|
2010-03-24 01:17:47 +00:00
|
|
|
//variables to be set by po
|
2010-04-20 19:05:33 +00:00
|
|
|
std::string args;
|
2011-01-12 22:03:25 +00:00
|
|
|
double seconds_in_future;
|
2010-03-24 01:17:47 +00:00
|
|
|
size_t total_num_samps;
|
2011-05-02 00:26:15 +00:00
|
|
|
double rate;
|
2010-03-24 01:17:47 +00:00
|
|
|
|
|
|
|
|
//setup the program options
|
|
|
|
|
po::options_description desc("Allowed options");
|
|
|
|
|
desc.add_options()
|
|
|
|
|
("help", "help message")
|
2010-09-20 23:59:44 +00:00
|
|
|
("args", po::value<std::string>(&args)->default_value(""), "single uhd device address args")
|
2011-03-12 00:22:20 +00:00
|
|
|
("secs", po::value<double>(&seconds_in_future)->default_value(1.5), "number of seconds in the future to receive")
|
|
|
|
|
("nsamps", po::value<size_t>(&total_num_samps)->default_value(10000), "total number of samples to receive")
|
2010-10-28 02:18:26 +00:00
|
|
|
("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of incoming samples")
|
2010-07-07 20:06:02 +00:00
|
|
|
("dilv", "specify to disable inner-loop verbose")
|
2010-03-24 01:17:47 +00:00
|
|
|
;
|
|
|
|
|
po::variables_map vm;
|
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
2010-05-18 21:26:43 +00:00
|
|
|
po::notify(vm);
|
2010-03-24 01:17:47 +00:00
|
|
|
|
|
|
|
|
//print the help message
|
|
|
|
|
if (vm.count("help")){
|
|
|
|
|
std::cout << boost::format("UHD RX Timed Samples %s") % desc << std::endl;
|
|
|
|
|
return ~0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 20:06:02 +00:00
|
|
|
bool verbose = vm.count("dilv") == 0;
|
|
|
|
|
|
2010-03-24 01:17:47 +00:00
|
|
|
//create a usrp device
|
|
|
|
|
std::cout << std::endl;
|
2010-04-20 19:05:33 +00:00
|
|
|
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
|
2011-02-01 02:01:27 +00:00
|
|
|
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
|
|
|
|
|
std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
|
2010-03-24 01:17:47 +00:00
|
|
|
|
2010-10-28 02:18:26 +00:00
|
|
|
//set the rx sample rate
|
|
|
|
|
std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate/1e6) << std::endl;
|
2011-02-01 02:01:27 +00:00
|
|
|
usrp->set_rx_rate(rate);
|
|
|
|
|
std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate()/1e6) << std::endl << std::endl;
|
2010-10-28 02:18:26 +00:00
|
|
|
|
|
|
|
|
std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
|
2011-02-01 02:01:27 +00:00
|
|
|
usrp->set_time_now(uhd::time_spec_t(0.0));
|
2010-03-24 01:17:47 +00:00
|
|
|
|
|
|
|
|
//setup streaming
|
|
|
|
|
std::cout << std::endl;
|
2010-07-26 18:46:08 +00:00
|
|
|
std::cout << boost::format(
|
2011-01-12 22:03:25 +00:00
|
|
|
"Begin streaming %u samples, %f seconds in the future..."
|
2010-07-26 18:46:08 +00:00
|
|
|
) % total_num_samps % seconds_in_future << std::endl;
|
2010-04-04 02:43:20 +00:00
|
|
|
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
|
|
|
|
|
stream_cmd.num_samps = total_num_samps;
|
2010-03-26 01:36:16 +00:00
|
|
|
stream_cmd.stream_now = false;
|
|
|
|
|
stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future);
|
2011-02-01 02:01:27 +00:00
|
|
|
usrp->issue_stream_cmd(stream_cmd);
|
|
|
|
|
|
2011-03-12 00:22:20 +00:00
|
|
|
//meta-data will be filled in by recv()
|
|
|
|
|
uhd::rx_metadata_t md;
|
|
|
|
|
|
|
|
|
|
//allocate buffer to receive with samples
|
2011-02-01 02:01:27 +00:00
|
|
|
std::vector<std::complex<float> > buff(usrp->get_device()->get_max_recv_samps_per_packet());
|
2010-03-24 01:17:47 +00:00
|
|
|
|
2011-03-12 00:22:20 +00:00
|
|
|
//the first call to recv() will block this many seconds before receiving
|
|
|
|
|
double timeout = seconds_in_future + 0.1; //timeout (delay before receive + padding)
|
|
|
|
|
|
2010-03-24 01:17:47 +00:00
|
|
|
size_t num_acc_samps = 0; //number of accumulated samples
|
|
|
|
|
while(num_acc_samps < total_num_samps){
|
2011-03-12 00:22:20 +00:00
|
|
|
//receive a single packet
|
2011-02-01 02:01:27 +00:00
|
|
|
size_t num_rx_samps = usrp->get_device()->recv(
|
2010-07-03 23:50:45 +00:00
|
|
|
&buff.front(), buff.size(), md,
|
2010-06-25 01:55:08 +00:00
|
|
|
uhd::io_type_t::COMPLEX_FLOAT32,
|
2011-03-12 00:22:20 +00:00
|
|
|
uhd::device::RECV_MODE_ONE_PACKET, timeout
|
2010-03-30 21:07:19 +00:00
|
|
|
);
|
2010-07-13 03:39:21 +00:00
|
|
|
|
2011-03-12 00:22:20 +00:00
|
|
|
//use a small timeout for subsequent packets
|
|
|
|
|
timeout = 0.1;
|
|
|
|
|
|
|
|
|
|
//handle the error code
|
|
|
|
|
if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) break;
|
|
|
|
|
if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
|
|
|
|
|
throw std::runtime_error(str(boost::format(
|
|
|
|
|
"Unexpected error code 0x%x"
|
|
|
|
|
) % md.error_code));
|
2010-05-24 21:38:25 +00:00
|
|
|
}
|
2010-03-24 01:17:47 +00:00
|
|
|
|
2010-07-10 00:35:15 +00:00
|
|
|
if(verbose) std::cout << boost::format(
|
2011-03-12 00:22:20 +00:00
|
|
|
"Received packet: %u samples, %u full secs, %f frac secs"
|
2010-07-10 00:35:15 +00:00
|
|
|
) % num_rx_samps % md.time_spec.get_full_secs() % md.time_spec.get_frac_secs() << std::endl;
|
2010-03-24 01:17:47 +00:00
|
|
|
|
|
|
|
|
num_acc_samps += num_rx_samps;
|
2011-03-12 00:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (num_acc_samps < total_num_samps) std::cerr << "Receive timeout before all samples received..." << std::endl;
|
2010-03-24 01:17:47 +00:00
|
|
|
|
2010-03-26 01:36:16 +00:00
|
|
|
//finished
|
2010-03-24 01:17:47 +00:00
|
|
|
std::cout << std::endl << "Done!" << std::endl << std::endl;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|