diff options
Diffstat (limited to 'host/examples/twinrx_freq_hopping.cpp')
-rw-r--r-- | host/examples/twinrx_freq_hopping.cpp | 101 |
1 files changed, 55 insertions, 46 deletions
diff --git a/host/examples/twinrx_freq_hopping.cpp b/host/examples/twinrx_freq_hopping.cpp index f4ce3e528..ea350928c 100644 --- a/host/examples/twinrx_freq_hopping.cpp +++ b/host/examples/twinrx_freq_hopping.cpp @@ -7,15 +7,12 @@ // FFT conversion #include "ascii_art_dft.hpp" - -#include <uhd/utils/thread.hpp> -#include <uhd/utils/safe_main.hpp> #include <uhd/usrp/multi_usrp.hpp> - +#include <uhd/utils/safe_main.hpp> +#include <uhd/utils/thread.hpp> #include <boost/program_options.hpp> #include <boost/thread.hpp> #include <boost/thread/thread_time.hpp> - #include <fstream> /* @@ -23,8 +20,8 @@ * motherboard and a TwinRX daughterboard. * * The TwinRX daughterboard is different than previous daughterboards in that it has two - * RX channels, each with a set of Local Oscillators (LOs). Either channel can be configured - * to use either LO set, allowing for the two channels to share an LO source. + * RX channels, each with a set of Local Oscillators (LOs). Either channel can be + * configured to use either LO set, allowing for the two channels to share an LO source. * * The TwinRX can be used like any other daughterboard, as the multi_usrp::set_rx_freq() * function will automatically calculate and set the two LO frequencies as needed. @@ -35,7 +32,8 @@ * * 1. Tune across the given frequency range, storing the calculated LO frequencies along * the way. - * 2. Use timed commands to tell the TwinRX to receive bursts of samples at given intervals. + * 2. Use timed commands to tell the TwinRX to receive bursts of samples at given + * intervals. * 3. For each frequency, tune the LOs for the inactive channel for the next frequency and * receive at the current frequency. * 4. If applicable, send the next timed command for streaming. @@ -44,7 +42,7 @@ namespace pt = boost::posix_time; namespace po = boost::program_options; -typedef std::vector<std::complex<float> > recv_buff_t; +typedef std::vector<std::complex<float>> recv_buff_t; typedef std::vector<recv_buff_t> recv_buffs_t; // Global objects @@ -67,8 +65,8 @@ const int X300_COMMAND_FIFO_DEPTH = 16; // This is a helper function for receiving samples from the USRP -static void twinrx_recv(recv_buff_t &buffer) { - +static void twinrx_recv(recv_buff_t& buffer) +{ size_t num_acc_samps = 0; uhd::rx_metadata_t md; @@ -77,9 +75,10 @@ static void twinrx_recv(recv_buff_t &buffer) { size_t num_to_recv = std::min<size_t>(recv_spb, (spb - num_acc_samps)); // recv call will block until samples are ready or the call times out - size_t num_recvd = rx_stream->recv(&buffer[num_acc_samps], num_to_recv, md, receive_interval); + size_t num_recvd = + rx_stream->recv(&buffer[num_acc_samps], num_to_recv, md, receive_interval); - if(md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) { + if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) { std::cout << md.strerror() << std::endl; break; } @@ -88,18 +87,20 @@ static void twinrx_recv(recv_buff_t &buffer) { } // Function to write the acquisition FFT to a binary file -static void write_fft_to_file(const std::string &fft_path) { +static void write_fft_to_file(const std::string& fft_path) +{ std::cout << "Calculating FFTs (this may take a while)... " << std::flush; std::ofstream ofile(fft_path.c_str(), std::ios::binary); - BOOST_FOREACH(const recv_buff_t &buff, buffs) { - std::vector<float> fft = ascii_art_dft::log_pwr_dft(&buff.front(), buff.size()); - ofile.write((char*)&fft[0], (sizeof(float)*fft.size())); - } + BOOST_FOREACH (const recv_buff_t& buff, buffs) { + std::vector<float> fft = ascii_art_dft::log_pwr_dft(&buff.front(), buff.size()); + ofile.write((char*)&fft[0], (sizeof(float) * fft.size())); + } ofile.close(); std::cout << "done." << std::endl; } -int UHD_SAFE_MAIN(int argc, char *argv[]){ +int UHD_SAFE_MAIN(int argc, char* argv[]) +{ uhd::set_thread_priority_safe(); // Program options @@ -129,22 +130,25 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); - if(vm.count("help")) { + if (vm.count("help")) { std::cout << "TwinRX Frequency Hopping Example - " << desc << std::endl; return EXIT_SUCCESS; } // Create a USRP device - std::cout << boost::format("\nCreating the USRP device with args: \"%s\"...\n") % args; + std::cout << boost::format("\nCreating the USRP device with args: \"%s\"...\n") + % args; usrp = uhd::usrp::multi_usrp::make(args); // Make sure the USRP is an X3xx with a TwinRX uhd::dict<std::string, std::string> info = usrp->get_usrp_rx_info(); - if(info.get("mboard_id").find("X3") == std::string::npos) { - throw uhd::runtime_error("This example can only be used with an X-Series motherboard."); + if (info.get("mboard_id").find("X3") == std::string::npos) { + throw uhd::runtime_error( + "This example can only be used with an X-Series motherboard."); } - if(info.get("rx_id").find("TwinRX") == std::string::npos) { - throw uhd::runtime_error("This example can only be used with a TwinRX daughterboard."); + if (info.get("rx_id").find("TwinRX") == std::string::npos) { + throw uhd::runtime_error( + "This example can only be used with a TwinRX daughterboard."); } // Validate frequency range @@ -156,26 +160,31 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ end_freq = rx_freq_range.stop(); } if (start_freq < rx_freq_range.start() or end_freq > rx_freq_range.stop()) { - throw uhd::runtime_error((boost::format("Start and stop frequencies must be between %d and %d MHz") - % (rx_freq_range.start() / 1e6) % (rx_freq_range.stop() / 1e6)).str()); + throw uhd::runtime_error( + (boost::format("Start and stop frequencies must be between %d and %d MHz") + % (rx_freq_range.start() / 1e6) % (rx_freq_range.stop() / 1e6)) + .str()); } if (start_freq > end_freq) { throw uhd::runtime_error("Start frequency must be less than end frequency."); } if ((end_freq - start_freq) > 0 and (end_freq - start_freq) < rate) { - throw uhd::runtime_error("The sample rate must be less than the range between the start and end frequencies."); + throw uhd::runtime_error("The sample rate must be less than the range between " + "the start and end frequencies."); } // Set TwinRX settings usrp->set_rx_subdev_spec(subdev); - // Set the unused channel to not use any LOs. This allows the active channel to control them. + // Set the unused channel to not use any LOs. This allows the active channel to + // control them. usrp->set_rx_lo_source("disabled", uhd::usrp::multi_usrp::ALL_LOS, UNUSED_CHAN); // Set user settings std::cout << boost::format("Setting antenna to: %s\n") % ant; usrp->set_rx_antenna(ant, ACTIVE_CHAN); - std::cout << boost::format("Actual antenna: %s\n") % usrp->get_rx_antenna(ACTIVE_CHAN); + std::cout << boost::format("Actual antenna: %s\n") + % usrp->get_rx_antenna(ACTIVE_CHAN); std::cout << boost::format("Setting sample rate to: %d\n") % rate; usrp->set_rx_rate(rate); @@ -189,7 +198,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ uhd::stream_args_t stream_args("fc32", "sc16"); stream_args.channels.push_back(0); rx_stream = usrp->get_rx_stream(stream_args); - recv_spb = rx_stream->get_max_num_samps(); + recv_spb = rx_stream->get_max_num_samps(); // Calculate the frequency hops for (double rx_freq = start_freq; rx_freq <= end_freq; rx_freq += rate) { @@ -204,10 +213,11 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ usrp->set_rx_freq(rf_freqs[0], ACTIVE_CHAN); usrp->set_time_now(uhd::time_spec_t(0.0)); - // Configure the stream command which will be issued to acquire samples at each frequency - stream_cmd.num_samps = spb; + // Configure the stream command which will be issued to acquire samples at each + // frequency + stream_cmd.num_samps = spb; stream_cmd.stream_now = false; - stream_cmd.time_spec = uhd::time_spec_t(0.0); + stream_cmd.time_spec = uhd::time_spec_t(0.0); // Stream commands will be scheduled at regular intervals uhd::time_spec_t receive_interval_ts = uhd::time_spec_t(receive_interval); @@ -216,14 +226,14 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ size_t num_initial_cmds = std::min<size_t>(X300_COMMAND_FIFO_DEPTH, rf_freqs.size()); size_t num_issued_commands; - for (num_issued_commands = 0; num_issued_commands < num_initial_cmds; num_issued_commands++) { + for (num_issued_commands = 0; num_issued_commands < num_initial_cmds; + num_issued_commands++) { stream_cmd.time_spec += receive_interval_ts; rx_stream->issue_stream_cmd(stream_cmd); } // Hop frequencies and acquire bursts of samples at each until done sweeping - while(1) { - + while (1) { std::cout << "Scanning..." << std::endl; auto start_time = boost::get_system_time(); @@ -234,11 +244,12 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ usrp->set_rx_lo_source(lo_src, uhd::usrp::multi_usrp::ALL_LOS, ACTIVE_CHAN); // Preconfigure the next frequency - usrp->set_rx_freq(rf_freqs[(i+1) % rf_freqs.size()], UNUSED_CHAN); + usrp->set_rx_freq(rf_freqs[(i + 1) % rf_freqs.size()], UNUSED_CHAN); // Program the current frequency - // This frequency was already pre-programmed in the previous iteration so the local oscillators - // are already tuned. This call will only configure front-end filter, amplifiers, etc + // This frequency was already pre-programmed in the previous iteration so the + // local oscillators are already tuned. This call will only configure + // front-end filter, amplifiers, etc usrp->set_rx_freq(rf_freqs[i], ACTIVE_CHAN); // Receive one burst of samples @@ -253,16 +264,15 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ } auto end_time = boost::get_system_time(); - std::cout - << boost::format("Sweep done in %d milliseconds.\n") - % ((end_time - start_time).total_milliseconds() * 1000); + std::cout << boost::format("Sweep done in %d milliseconds.\n") + % ((end_time - start_time).total_milliseconds() * 1000); // Optionally convert received samples to FFT and write to file - if(vm.count("fft-path")) { + if (vm.count("fft-path")) { write_fft_to_file(fft_path); } - if (!vm.count("repeat")){ + if (!vm.count("repeat")) { break; } } @@ -272,4 +282,3 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ usrp.reset(); return EXIT_SUCCESS; } - |