double uhd_device::setRxGain(double db) { usrp_dev->set_rx_gain(db); rx_gain = usrp_dev->get_rx_gain(); LOG(INFO) << "Set RX gain to " << rx_gain << "dB"; return rx_gain; }
int setup_device(uhd::usrp::multi_usrp::sptr usrp, double rx_gain, double tx_gain, double freq, double rate){ //create a usrp device std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl; usrp->set_rx_rate(rate); usrp->set_rx_freq(freq); usrp->set_rx_gain(rx_gain); usrp->set_tx_rate(rate); usrp->set_tx_freq(freq); usrp->set_tx_gain(tx_gain); }
void init_usrp() { cout << "Initial USRP" << endl; usrp = uhd::usrp::multi_usrp::make(usrp_ip); usrp->set_rx_rate(rate); usrp->set_tx_rate(rate); usrp->set_rx_freq(freq); usrp->set_tx_freq(freq); usrp->set_rx_gain(gain); uhd::meta_range_t rx_range = usrp->get_rx_gain_range(); }
void uhd_device::init_gains() { uhd::gain_range_t range; range = usrp_dev->get_tx_gain_range(); tx_gain_min = range.start(); tx_gain_max = range.stop(); range = usrp_dev->get_rx_gain_range(); rx_gain_min = range.start(); rx_gain_max = range.stop(); usrp_dev->set_tx_gain((tx_gain_min + tx_gain_max) / 2); usrp_dev->set_rx_gain((rx_gain_min + rx_gain_max) / 2); tx_gain = usrp_dev->get_tx_gain(); rx_gain = usrp_dev->get_rx_gain(); return; }
/*********************************************************************** * Function to find optimal RX gain setting (for the current frequency) **********************************************************************/ UHD_INLINE void set_optimal_rx_gain( uhd::usrp::multi_usrp::sptr usrp, uhd::rx_streamer::sptr rx_stream, double wave_freq = 0.0) { const double gain_step = 3.0; const double gain_compression_threshold = gain_step * 0.5; const double actual_rx_rate = usrp->get_rx_rate(); const double actual_tx_freq = usrp->get_tx_freq(); const double actual_rx_freq = usrp->get_rx_freq(); const double bb_tone_freq = actual_tx_freq - actual_rx_freq + wave_freq; const size_t nsamps = size_t(actual_rx_rate / default_fft_bin_size); std::vector<samp_type> buff(nsamps); uhd::gain_range_t rx_gain_range = usrp->get_rx_gain_range(); double rx_gain = rx_gain_range.start() + gain_step; double curr_dbrms = 0.0; double prev_dbrms = 0.0; double delta = 0.0; // No sense in setting the gain where this is no gain range if (rx_gain_range.stop() - rx_gain_range.start() < gain_step) return; // The algorithm below cycles through the RX gain range // looking for the point where the signal begins to get // clipped and the gain begins to be compressed. It does // this by looking for the gain setting where the increase // in the tone is less than the gain step by more than the // gain compression threshold (curr - prev < gain - threshold). // Initialize prev_dbrms value usrp->set_rx_gain(rx_gain); capture_samples(usrp, rx_stream, buff, nsamps); prev_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate); rx_gain += gain_step; // Find RX gain where signal begins to clip while (rx_gain <= rx_gain_range.stop()) { usrp->set_rx_gain(rx_gain); capture_samples(usrp, rx_stream, buff, nsamps); curr_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate); delta = curr_dbrms - prev_dbrms; // check if the gain is compressed beyone the threshold if (delta < gain_step - gain_compression_threshold) break; // if so, we are done prev_dbrms = curr_dbrms; rx_gain += gain_step; } // The rx_gain value at this point is the gain setting where clipping // occurs or the gain setting that is just beyond the gain range. // The gain is reduced by 2 steps to make sure it is within the range and // under the point where it is clipped with enough room to make adjustments. rx_gain -= 2 * gain_step; // Make sure the gain is within the range. rx_gain = rx_gain_range.clip(rx_gain); // Finally, set the gain. usrp->set_rx_gain(rx_gain); }
int setupUSRP( uhd::usrp::multi_usrp::sptr& usrp, const float center_freq, const float sample_rate, const int rx_gain, const char* dev_addr) { //Initialize the USRP to the specified address usrp = uhd::usrp::multi_usrp::make(string(dev_addr)); //Define the clock reference usrp->set_clock_source(__USRP_CLK_SRC); //Output some useful information cout << "Using the following USRP device: " << endl << usrp->get_pp_string() << endl; //Try setting the sample rate. If the rate we get is not the same as the //requested rate, we will return with a warning to ensure the user is aware //of the actual sample rate usrp->set_rx_rate( sample_rate ); if( usrp->get_rx_rate() != sample_rate ) { ios_base::fmtflags originalFlags = cout.flags(); cout.setf(ios_base::left,ios_base::floatfield); cout.precision(15); cout << "WARNING! Requested rate = " << sample_rate << endl << "WARNING! Actual rate = " << usrp->get_rx_rate() << endl; cout.setf(originalFlags); } //Try setting the center frequency. Like above, if we get a different //frequency than the one we're requesting, we will spit out a warning for the //user usrp->set_rx_freq( center_freq ); if( usrp->get_rx_freq() != center_freq ) { ios_base::fmtflags originalFlags = cout.flags(); cout.setf(ios_base::left,ios_base::floatfield); cout.precision(15); cout << "WARNING! Requested frequency = " << center_freq << endl << "WARNING! Actual frequency = " << usrp->get_rx_freq() << endl; cout.setf(originalFlags); } //Set the RX gain. There really shouldn't be any problems here, but the user //might request something silly like 50dB of gain when the module can't //accomodate. So we'll perform a similar check here. usrp->set_rx_gain( rx_gain ); if( usrp->get_rx_gain() != rx_gain ) { cout << "WARNING! Requested gain = " << rx_gain << endl << "WARNING! Actual gain = " << usrp->get_rx_gain() << endl; } //Ensure the LO locked vector<string> sensor_names; sensor_names = usrp->get_rx_sensor_names(0); if( find(sensor_names.begin(), sensor_names.end(), "lo_locked") != sensor_names.end() ) { uhd::sensor_value_t lo_locked = usrp->get_rx_sensor("lo_locked",0); cout << "Checking RX: " << endl << lo_locked.to_pp_string() << endl; UHD_ASSERT_THROW(lo_locked.to_bool()); //We should probably catch this } return 1; }
/*********************************************************************** * Set standard defaults for devices **********************************************************************/ static inline void set_optimum_defaults(uhd::usrp::multi_usrp::sptr usrp){ uhd::property_tree::sptr tree = usrp->get_device()->get_tree(); // Will work on 1st subdev, top-level must make sure it's the right one uhd::usrp::subdev_spec_t subdev_spec = usrp->get_rx_subdev_spec(); const uhd::fs_path mb_path = "/mboards/0"; const std::string mb_name = tree->access<std::string>(mb_path / "name").get(); if (mb_name.find("USRP2") != std::string::npos or mb_name.find("N200") != std::string::npos or mb_name.find("N210") != std::string::npos or mb_name.find("X300") != std::string::npos or mb_name.find("X310") != std::string::npos){ usrp->set_tx_rate(12.5e6); usrp->set_rx_rate(12.5e6); } else if (mb_name.find("B100") != std::string::npos){ usrp->set_tx_rate(4e6); usrp->set_rx_rate(4e6); } else if (mb_name.find("E100") != std::string::npos or mb_name.find("E110") != std::string::npos){ usrp->set_tx_rate(4e6); usrp->set_rx_rate(8e6); } else{ throw std::runtime_error("self-calibration is not supported for this hardware"); } const uhd::fs_path tx_fe_path = "/mboards/0/dboards/" + subdev_spec[0].db_name + "/tx_frontends/0"; const std::string tx_name = tree->access<std::string>(tx_fe_path / "name").get(); if (tx_name.find("WBX") != std::string::npos){ usrp->set_tx_gain(0); } else if (tx_name.find("SBX") != std::string::npos){ usrp->set_tx_gain(0); } else if (tx_name.find("CBX") != std::string::npos){ usrp->set_tx_gain(0); } else if (tx_name.find("RFX") != std::string::npos){ usrp->set_tx_gain(0); } else{ throw std::runtime_error("self-calibration is not supported for this hardware"); } const uhd::fs_path rx_fe_path = "/mboards/0/dboards/" + subdev_spec[0].db_name + "/rx_frontends/0"; const std::string rx_name = tree->access<std::string>(rx_fe_path / "name").get(); if (rx_name.find("WBX") != std::string::npos){ usrp->set_rx_gain(25); } else if (rx_name.find("SBX") != std::string::npos){ usrp->set_rx_gain(25); } else if (rx_name.find("CBX") != std::string::npos){ usrp->set_rx_gain(25); } else if (rx_name.find("RFX") != std::string::npos){ usrp->set_rx_gain(25); } else{ throw std::runtime_error("self-calibration is not supported for this hardware"); } }