//
// Copyright 2010-2011,2014 Ettus Research LLC
//
// 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 .
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace po = boost::program_options;
static bool stop_signal_called = false;
void sig_int_handler(int){stop_signal_called = true;}
template void recv_to_file(
uhd::usrp::multi_usrp::sptr usrp,
const std::string &cpu_format,
const std::string &wire_format,
const std::string &stream_args_args,
const std::string &file,
size_t samps_per_buff,
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
){
unsigned long long num_total_samps = 0;
//create a receive streamer
uhd::stream_args_t stream_args(cpu_format,wire_format);
stream_args.args = stream_args_args;
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
uhd::rx_metadata_t md;
std::vector buff(samps_per_buff);
std::ofstream outfile;
if (not null)
outfile.open(file.c_str(), std::ofstream::binary);
bool overflow_message = true;
//setup streaming
uhd::stream_cmd_t stream_cmd((num_requested_samples == 0)?
uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS:
uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE
);
stream_cmd.num_samps = size_t(num_requested_samples);
stream_cmd.stream_now = true;
stream_cmd.time_spec = uhd::time_spec_t();
std::cout << "Issueing stream cmd" << std::endl;
rx_stream->issue_stream_cmd(stream_cmd);
std::cout << "Done" << std::endl;
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 SizeMap;
SizeMap mapSizes;
while(not stop_signal_called and (num_requested_samples != num_total_samps or num_requested_samples == 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;
break;
}
if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW){
if (overflow_message) {
overflow_message = false;
std::cerr << boost::format(
"Got an overflow indication. Please consider the following:\n"
" Your write medium must sustain a rate of %fMB/s.\n"
" Dropped samples will not be written to the file.\n"
" Please modify this example for your purposes.\n"
" This message will not appear again.\n"
) % (usrp->get_rx_rate()*sizeof(samp_type)/1e6);
}
continue;
}
if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
std::string error = str(boost::format("Receiver error: %s") % md.strerror());
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;
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;
}
}
stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
std::cout << "Issueing stop stream cmd" << std::endl;
rx_stream->issue_stream_cmd(stream_cmd);
std::cout << "Done" << std::endl;
// Run recv until nothing is left
int num_post_samps = 0;
do {
num_post_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0);
} while(num_post_samps and md.error_code == uhd::rx_metadata_t::ERROR_CODE_NONE);
if (outfile.is_open())
outfile.close();
if (stats) {
std::cout << std::endl;
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 get_sensor_fn_t;
bool check_locked_sensor(std::vector 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[]){
uhd::set_thread_priority_safe();
//variables to be set by po
std::string args, file, type, ant, subdev, ref, wirefmt, streamargs, blockid, coeffs;
size_t total_num_samps, spb, radio_id;
double rate, freq, gain, bw, total_time, setup_time;
//setup the program options
po::options_description desc("Allowed options");
desc.add_options()
("help", "help message")
("args", po::value(&args)->default_value(""), "multi uhd device address args")
("streamargs", po::value(&streamargs)->default_value(""), "stream args")
("file", po::value(&file)->default_value("usrp_samples.dat"), "name of the file to write binary samples to")
("type", po::value(&type)->default_value("short"), "sample type: double, float, or short")
("nsamps", po::value(&total_num_samps)->default_value(0), "total number of samples to receive")
("duration", po::value(&total_time)->default_value(0), "total number of seconds to receive")
("time", po::value(&total_time), "(DEPRECATED) will go away soon! Use --duration instead")
("spb", po::value(&spb)->default_value(10000), "samples per buffer")
("rate", po::value(&rate)->default_value(1e6), "rate of incoming samples")
("freq", po::value(&freq)->default_value(0.0), "RF center frequency in Hz")
("gain", po::value(&gain), "gain for the RF chain")
("ant", po::value(&ant), "antenna selection")
("subdev", po::value(&subdev), "subdevice specification")
("radio-id", po::value(&radio_id)->default_value(0), "Radio ID to use (0 or 1). Excludes the use of --subdev.")
("bw", po::value(&bw), "analog frontend filter bandwidth in Hz")
("ref", po::value(&ref)->default_value("internal"), "reference source (internal, external, mimo)")
("wirefmt", po::value(&wirefmt)->default_value("sc16"), "wire format (sc8 or sc16)")
("setup", po::value(&setup_time)->default_value(1.0), "seconds of setup time")
("progress", "periodically display short-term bandwidth")
("stats", "show average bandwidth on exit")
("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")
("int-n", "tune USRP with integer-N tuning")
("blockid", po::value(&blockid)->default_value(""), "rfnoc block id")
("coeffs", po::value(&coeffs)->default_value("coeffs.txt"), "list of coefficients")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
//print the help message
if (vm.count("help")) {
std::cout << boost::format("UHD RX samples to file %s") % desc << std::endl;
std::cout
<< std::endl
<< "This application streams data from a single channel of a USRP device to a file.\n"
<< 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;
if (vm.count("subdev") > 1 and vm.count("radio-id") > 1) {
std::cout << "Error: Cannot use both --subdev and --radio-id!" << std::endl;
return ~0;
}
//create a usrp device
std::cout << std::endl;
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
//Lock mboard clocks
usrp->set_clock_source(ref);
//always select the subdevice first, the channel mapping affects the other settings
if (vm.count("subdev")) usrp->set_rx_subdev_spec(subdev);
uhd::usrp::subdev_spec_t rx_subdev = usrp->get_rx_subdev_spec();
if (rx_subdev.size() == 0 or (rx_subdev.size()-1 < radio_id)) {
std::cout << "Error: radio-id exceeds number of radios (" << rx_subdev.size() << ")" << std::endl;
return ~0;
}
// Radio-ID is only really useful on Generation-3 devices, so we add
// some compat code for older devices
if (not usrp->is_device3() and radio_id > 0) {
// In this case, we need to manually modify the subdev spec
rx_subdev[0] = rx_subdev[radio_id];
rx_subdev.resize(1);
radio_id = 0;
usrp->set_rx_subdev_spec(rx_subdev);
}
std::cout << boost::format("Using Radio channel: %d (%s:%s)")
% radio_id % rx_subdev[radio_id].db_name % rx_subdev[radio_id].sd_name << std::endl;
std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
//set the sample rate
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;
usrp->set_rx_rate(rate, radio_id);
std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate(radio_id)/1e6) << std::endl << std::endl;
//set the center frequency
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;
uhd::tune_request_t tune_request(freq);
if(vm.count("int-n")) tune_request.args = uhd::device_addr_t("mode_n=integer");
usrp->set_rx_freq(tune_request, radio_id);
std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq(radio_id)/1e6) << std::endl << std::endl;
}
//set the rf gain
if (vm.count("gain")) {
std::cout << boost::format("Setting RX Gain: %f dB...") % gain << std::endl;
usrp->set_rx_gain(gain, radio_id);
std::cout << boost::format("Actual RX Gain: %f dB...") % usrp->get_rx_gain(radio_id) << std::endl << std::endl;
}
//set the IF filter bandwidth
if (vm.count("bw")) {
std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % (bw/1e6) << std::endl;
usrp->set_rx_bandwidth(bw, radio_id);
std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % (usrp->get_rx_bandwidth()/1e6) << std::endl << std::endl;
}
//set the antenna
if (vm.count("ant")) usrp->set_rx_antenna(ant, radio_id);
boost::this_thread::sleep(boost::posix_time::seconds(setup_time)); //allow for some setup time
//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, radio_id), 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, radio_id), 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, radio_id), setup_time);
}
if (total_num_samps == 0){
std::signal(SIGINT, &sig_int_handler);
std::cout << "Press Ctrl + C to stop streaming..." << std::endl;
}
uhd::rfnoc::block_id_t radio_ctrl_id(0, "Radio", radio_id);
if (usrp->is_device3()) {
// Clear the E310
usrp->get_device3()->clear();
// Clear all previous channels
usrp->clear_channels();
// Create a pointer to our FIR filter
uhd::rfnoc::fir_block_ctrl::sptr fir_block_ctrl_imp =
usrp->get_device3()->find_block_ctrl< uhd::rfnoc::fir_block_ctrl >(blockid);
// Read in our filter taps (single column of ints) from a file
std::vector coeffsVector;
std::ifstream coeffsFile(coeffs);
if (coeffsFile) {
int value;
while (coeffsFile >> value) coeffsVector.push_back(value);
}
coeffsFile.close();
std::cout << "-- Read " << coeffsVector.size() << " filter taps.\n";
// Set the taps on our FIR Filter
fir_block_ctrl_imp->set_taps(coeffsVector);
// Pass through our stream arguments to the blocks
fir_block_ctrl_imp->set_args(streamargs);
usrp->get_device3()->get_block_ctrl(radio_ctrl_id)->set_args(streamargs);
// Connect our blocks together
std::cout << "Connecting Blocks..." << std::endl;
usrp->connect(radio_ctrl_id, fir_block_ctrl_imp->get_block_id());
// Set the RX Channel to the ARM
usrp->set_rx_channel(fir_block_ctrl_imp->get_block_id());
}
#define recv_to_file_args(format) \
(usrp, format, wirefmt, streamargs, 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 >recv_to_file_args("fc64");
else if (type == "float") recv_to_file >recv_to_file_args("fc32");
else if (type == "short") recv_to_file >recv_to_file_args("sc16");
else throw std::runtime_error("Unknown type " + type);
//finished
std::cout << std::endl << "Done!" << std::endl << std::endl;
return EXIT_SUCCESS;
}