root / host / examples / rx_samples_to_file.cpp @ master
History | View | Annotate | Download (9.24 KB)
| 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 <uhd/exception.hpp> |
| 22 |
#include <boost/program_options.hpp> |
| 23 |
#include <boost/format.hpp> |
| 24 |
#include <boost/thread.hpp> |
| 25 |
#include <iostream> |
| 26 |
#include <fstream> |
| 27 |
#include <csignal> |
| 28 |
#include <complex> |
| 29 |
|
| 30 |
namespace po = boost::program_options;
|
| 31 |
|
| 32 |
static bool stop_signal_called = false; |
| 33 |
void sig_int_handler(int){stop_signal_called = true;} |
| 34 |
|
| 35 |
template<typename samp_type> void recv_to_file( |
| 36 |
uhd::usrp::multi_usrp::sptr usrp, |
| 37 |
const std::string &cpu_format, |
| 38 |
const std::string &wire_format, |
| 39 |
const std::string &file, |
| 40 |
size_t samps_per_buff, |
| 41 |
int num_requested_samples
|
| 42 |
){
|
| 43 |
int num_total_samps = 0; |
| 44 |
//create a receive streamer
|
| 45 |
uhd::stream_args_t stream_args(cpu_format,wire_format); |
| 46 |
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); |
| 47 |
|
| 48 |
uhd::rx_metadata_t md; |
| 49 |
std::vector<samp_type> buff(samps_per_buff); |
| 50 |
std::ofstream outfile(file.c_str(), std::ofstream::binary); |
| 51 |
bool overflow_message = true; |
| 52 |
|
| 53 |
//setup streaming
|
| 54 |
uhd::stream_cmd_t stream_cmd((num_requested_samples == 0)?
|
| 55 |
uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS: |
| 56 |
uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE |
| 57 |
); |
| 58 |
stream_cmd.num_samps = num_requested_samples; |
| 59 |
stream_cmd.stream_now = true;
|
| 60 |
stream_cmd.time_spec = uhd::time_spec_t(); |
| 61 |
usrp->issue_stream_cmd(stream_cmd); |
| 62 |
|
| 63 |
while(not stop_signal_called and (num_requested_samples != num_total_samps or num_requested_samples == 0)){ |
| 64 |
size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0); |
| 65 |
|
| 66 |
if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
|
| 67 |
std::cout << boost::format("Timeout while streaming") << std::endl;
|
| 68 |
break;
|
| 69 |
} |
| 70 |
if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW){
|
| 71 |
if (overflow_message){
|
| 72 |
overflow_message = false;
|
| 73 |
std::cerr << boost::format( |
| 74 |
"Got an overflow indication. Please consider the following:\n"
|
| 75 |
" Your write medium must sustain a rate of %fMB/s.\n"
|
| 76 |
" Dropped samples will not be written to the file.\n"
|
| 77 |
" Please modify this example for your purposes.\n"
|
| 78 |
" This message will not appear again.\n"
|
| 79 |
) % (usrp->get_rx_rate()*sizeof(samp_type)/1e6); |
| 80 |
} |
| 81 |
continue;
|
| 82 |
} |
| 83 |
if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
|
| 84 |
throw std::runtime_error(str(boost::format(
|
| 85 |
"Unexpected error code 0x%x"
|
| 86 |
) % md.error_code)); |
| 87 |
} |
| 88 |
|
| 89 |
num_total_samps += num_rx_samps; |
| 90 |
|
| 91 |
outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); |
| 92 |
} |
| 93 |
|
| 94 |
outfile.close(); |
| 95 |
} |
| 96 |
|
| 97 |
int UHD_SAFE_MAIN(int argc, char *argv[]){ |
| 98 |
uhd::set_thread_priority_safe(); |
| 99 |
|
| 100 |
//variables to be set by po
|
| 101 |
std::string args, file, type, ant, subdev, ref, wirefmt;
|
| 102 |
size_t total_num_samps, spb; |
| 103 |
double rate, freq, gain, bw;
|
| 104 |
|
| 105 |
//setup the program options
|
| 106 |
po::options_description desc("Allowed options");
|
| 107 |
desc.add_options() |
| 108 |
("help", "help message") |
| 109 |
("args", po::value<std::string>(&args)->default_value(""), "multi uhd device address args") |
| 110 |
("file", po::value<std::string>(&file)->default_value("usrp_samples.dat"), "name of the file to write binary samples to") |
| 111 |
("type", po::value<std::string>(&type)->default_value("short"), "sample type: double, float, or short") |
| 112 |
("nsamps", po::value<size_t>(&total_num_samps)->default_value(0), "total number of samples to receive") |
| 113 |
("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer") |
| 114 |
("rate", po::value<double>(&rate), "rate of incoming samples") |
| 115 |
("freq", po::value<double>(&freq), "RF center frequency in Hz") |
| 116 |
("gain", po::value<double>(&gain), "gain for the RF chain") |
| 117 |
("ant", po::value<std::string>(&ant), "daughterboard antenna selection") |
| 118 |
("subdev", po::value<std::string>(&subdev), "daughterboard subdevice specification") |
| 119 |
("bw", po::value<double>(&bw), "daughterboard IF filter bandwidth in Hz") |
| 120 |
("ref", po::value<std::string>(&ref)->default_value("internal"), "waveform type (internal, external, mimo)") |
| 121 |
("wirefmt", po::value<std::string>(&wirefmt)->default_value("sc16"), "wire format (sc8 or sc16)") |
| 122 |
; |
| 123 |
po::variables_map vm; |
| 124 |
po::store(po::parse_command_line(argc, argv, desc), vm); |
| 125 |
po::notify(vm); |
| 126 |
|
| 127 |
//print the help message
|
| 128 |
if (vm.count("help")){ |
| 129 |
std::cout << boost::format("UHD RX samples to file %s") % desc << std::endl;
|
| 130 |
return ~0; |
| 131 |
} |
| 132 |
|
| 133 |
//create a usrp device
|
| 134 |
std::cout << std::endl; |
| 135 |
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
|
| 136 |
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args); |
| 137 |
|
| 138 |
//Lock mboard clocks
|
| 139 |
usrp->set_clock_source(ref); |
| 140 |
|
| 141 |
//always select the subdevice first, the channel mapping affects the other settings
|
| 142 |
if (vm.count("subdev")) usrp->set_rx_subdev_spec(subdev); |
| 143 |
|
| 144 |
std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
|
| 145 |
|
| 146 |
//set the sample rate
|
| 147 |
if (not vm.count("rate")){ |
| 148 |
std::cerr << "Please specify the sample rate with --rate" << std::endl;
|
| 149 |
return ~0; |
| 150 |
} |
| 151 |
std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate/1e6) << std::endl; |
| 152 |
usrp->set_rx_rate(rate); |
| 153 |
std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate()/1e6) << std::endl << std::endl; |
| 154 |
|
| 155 |
//set the center frequency
|
| 156 |
if (not vm.count("freq")){ |
| 157 |
std::cerr << "Please specify the center frequency with --freq" << std::endl;
|
| 158 |
return ~0; |
| 159 |
} |
| 160 |
std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq/1e6) << std::endl; |
| 161 |
usrp->set_rx_freq(freq); |
| 162 |
std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq()/1e6) << std::endl << std::endl; |
| 163 |
|
| 164 |
//set the rf gain
|
| 165 |
if (vm.count("gain")){ |
| 166 |
std::cout << boost::format("Setting RX Gain: %f dB...") % gain << std::endl;
|
| 167 |
usrp->set_rx_gain(gain); |
| 168 |
std::cout << boost::format("Actual RX Gain: %f dB...") % usrp->get_rx_gain() << std::endl << std::endl;
|
| 169 |
} |
| 170 |
|
| 171 |
//set the IF filter bandwidth
|
| 172 |
if (vm.count("bw")){ |
| 173 |
std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % bw << std::endl;
|
| 174 |
usrp->set_rx_bandwidth(bw); |
| 175 |
std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % usrp->get_rx_bandwidth() << std::endl << std::endl;
|
| 176 |
} |
| 177 |
|
| 178 |
//set the antenna
|
| 179 |
if (vm.count("ant")) usrp->set_rx_antenna(ant); |
| 180 |
|
| 181 |
boost::this_thread::sleep(boost::posix_time::seconds(1)); //allow for some setup time |
| 182 |
|
| 183 |
//Check Ref and LO Lock detect
|
| 184 |
std::vector<std::string> sensor_names;
|
| 185 |
sensor_names = usrp->get_rx_sensor_names(0);
|
| 186 |
if (std::find(sensor_names.begin(), sensor_names.end(), "lo_locked") != sensor_names.end()) { |
| 187 |
uhd::sensor_value_t lo_locked = usrp->get_rx_sensor("lo_locked",0); |
| 188 |
std::cout << boost::format("Checking RX: %s ...") % lo_locked.to_pp_string() << std::endl;
|
| 189 |
UHD_ASSERT_THROW(lo_locked.to_bool()); |
| 190 |
} |
| 191 |
sensor_names = usrp->get_mboard_sensor_names(0);
|
| 192 |
if ((ref == "mimo") and (std::find(sensor_names.begin(), sensor_names.end(), "mimo_locked") != sensor_names.end())) { |
| 193 |
uhd::sensor_value_t mimo_locked = usrp->get_mboard_sensor("mimo_locked",0); |
| 194 |
std::cout << boost::format("Checking RX: %s ...") % mimo_locked.to_pp_string() << std::endl;
|
| 195 |
UHD_ASSERT_THROW(mimo_locked.to_bool()); |
| 196 |
} |
| 197 |
if ((ref == "external") and (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end())) { |
| 198 |
uhd::sensor_value_t ref_locked = usrp->get_mboard_sensor("ref_locked",0); |
| 199 |
std::cout << boost::format("Checking RX: %s ...") % ref_locked.to_pp_string() << std::endl;
|
| 200 |
UHD_ASSERT_THROW(ref_locked.to_bool()); |
| 201 |
} |
| 202 |
|
| 203 |
if (total_num_samps == 0){ |
| 204 |
std::signal(SIGINT, &sig_int_handler); |
| 205 |
std::cout << "Press Ctrl + C to stop streaming..." << std::endl;
|
| 206 |
} |
| 207 |
|
| 208 |
//recv to file
|
| 209 |
if (type == "double") recv_to_file<std::complex<double> >(usrp, "fc64", wirefmt, file, spb, total_num_samps); |
| 210 |
else if (type == "float") recv_to_file<std::complex<float> >(usrp, "fc32", wirefmt, file, spb, total_num_samps); |
| 211 |
else if (type == "short") recv_to_file<std::complex<short> >(usrp, "sc16", wirefmt, file, spb, total_num_samps); |
| 212 |
else throw std::runtime_error("Unknown type " + type); |
| 213 |
|
| 214 |
//finished
|
| 215 |
std::cout << std::endl << "Done!" << std::endl << std::endl;
|
| 216 |
|
| 217 |
return EXIT_SUCCESS;
|
| 218 |
} |