From d020dcc4a3be20d4ad475b87adafee1c7cbd7291 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Fri, 20 Jan 2023 17:52:35 +0100 Subject: [PATCH] Added bandwidth and clock selection to USRP source --- source_modules/usrp_source/src/main.cpp | 101 +++++++++++++++++++++++- 1 file changed, 97 insertions(+), 4 deletions(-) diff --git a/source_modules/usrp_source/src/main.cpp b/source_modules/usrp_source/src/main.cpp index 43310cd7..3ddaedd8 100644 --- a/source_modules/usrp_source/src/main.cpp +++ b/source_modules/usrp_source/src/main.cpp @@ -137,13 +137,15 @@ public: samplerates.clear(); auto srList = dev->get_rx_rates(chanId); for (auto& l : srList) { + // If a single value, add it to the list if (l.step() == 0.0 || l.start() == l.stop()) { samplerates.define(l.start(), getBandwdithScaled(l.start()), l.start()); + continue; } - else { - for (double f = l.start(); f <= l.stop(); f += l.step()) { - samplerates.define(f, getBandwdithScaled(f), f); - } + + // Otherwise, save all entries to the list + for (double f = l.start(); f <= l.stop(); f += l.step()) { + samplerates.define(f, getBandwdithScaled(f), f); } } @@ -157,9 +159,38 @@ public: // Get gain range gainRange = dev->get_rx_gain_range(chanId)[0]; + // Get bandwidth ranges + bandwidths.clear(); + bandwidths.define(0, "Auto", 0); + uhd::meta_range_t bwRange = dev->get_rx_bandwidth_range(chanId); + for (const auto& r : bwRange) { + // If a single value, add it to the list + if (r.step() == 0.0 || r.start() == r.stop()) { + bandwidths.define((int)r.start(), getBandwdithScaled(r.start()), r.start()); + continue; + } + + // Otherwise, save all entries to the list + for (double i = r.start(); i <= r.stop(); i += r.step()) { + bandwidths.define((int)i, getBandwdithScaled(i), i); + } + } + + // Get clock sources + clockSources.clear(); + auto cSources = dev->get_clock_sources(0); + for (const auto& s : cSources) { + std::string name = s; + name[0] = std::toupper(name[0]); + clockSources.define(s, name, s); + spdlog::warn(s); + } + // Load settings srId = 0; antId = 0; + bwId = 0; + csId = 0; gain = gainRange.start(); config.acquire(); if (config.conf["devices"][selectedSer].contains("channels") && config.conf["devices"][selectedSer]["channels"].contains(selectedChan)) { @@ -172,6 +203,14 @@ public: std::string ant = cconf["antenna"]; if (antennas.keyExists(ant)) { antId = antennas.keyId(ant); } } + if (cconf.contains("bandwidth")) { + int bw = cconf["bandwidth"]; + if (bandwidths.keyExists(bw)) { bwId = bandwidths.keyId(bw); } + } + if (cconf.contains("clock")) { + std::string clk = cconf["clock"]; + if (clockSources.keyExists(clk)) { csId = clockSources.keyId(clk); } + } if (cconf.contains("gain")) { gain = cconf["gain"]; gain = std::clamp(gain, gainRange.start(), gainRange.stop()); @@ -184,6 +223,24 @@ public: core::setInputSampleRate(sampleRate); } + void setBandwidth(double bw) { + if (bw > 0.0) { + dev->set_rx_bandwidth(bw, chanId); + return; + } + + // If on auto, select the best depending on the samplerate + // Note: Starts at 1 because 0 is the 'Auto' entry. + int bestId; + for (int i = 1; i < bandwidths.size(); i++) { + bestId = i; + if (bandwidths[i] >= sampleRate) { break; } + } + + // Set it + dev->set_rx_bandwidth(bandwidths[bestId], chanId); + } + private: std::string getBandwdithScaled(double bw) { char buf[1024]; @@ -235,6 +292,8 @@ private: _this->dev->set_rx_antenna(_this->antennas.key(_this->antId), _this->chanId); _this->dev->set_rx_gain(_this->gain, _this->chanId); _this->dev->set_rx_freq(_this->freq, _this->chanId); + _this->dev->set_clock_source(_this->clockSources.key(_this->csId)); + _this->setBandwidth(_this->bandwidths[_this->bwId]); uhd::stream_args_t sargs; sargs.channels.clear(); @@ -344,6 +403,36 @@ private: } } + if (_this->bandwidths.size() > 2) { + SmGui::LeftLabel("Bandwidth"); + SmGui::FillWidth(); + if (SmGui::Combo(CONCAT("##_usrp_bw_sel_", _this->name), &_this->bwId, _this->bandwidths.txt)) { + if (_this->running) { + _this->setBandwidth(_this->bandwidths[_this->bwId]); + } + if (!_this->selectedSer.empty() && !_this->selectedChan.empty()) { + config.acquire(); + config.conf["devices"][_this->selectedSer]["channels"][_this->selectedChan]["bandwidth"] = _this->bandwidths.key(_this->bwId); + config.release(true); + } + } + } + + if (_this->clockSources.size() > 1) { + SmGui::LeftLabel("Clock"); + SmGui::FillWidth(); + if (SmGui::Combo(CONCAT("##_usrp_clk_sel_", _this->name), &_this->csId, _this->clockSources.txt)) { + if (_this->running) { + _this->dev->set_clock_source(_this->clockSources.key(_this->csId)); + } + if (!_this->selectedSer.empty()) { + config.acquire(); + config.conf["devices"][_this->selectedSer]["channels"][_this->selectedChan]["clock"] = _this->clockSources.key(_this->csId); + config.release(true); + } + } + } + SmGui::LeftLabel("Gain"); SmGui::FillWidth(); if (SmGui::SliderFloatWithSteps(CONCAT("##_usrp_gain_", _this->name), &_this->gain, _this->gainRange.start(), _this->gainRange.stop(), _this->gainRange.step(), SmGui::FMT_STR_FLOAT_DB_ONE_DECIMAL)) { @@ -396,6 +485,8 @@ private: int chanId = 0; int srId = 0; int antId = 0; + int bwId = 0; + int csId = 0; std::string selectedSer = ""; std::string selectedChan = ""; float gain = 0.0f; @@ -404,6 +495,8 @@ private: OptionList channels; OptionList samplerates; OptionList antennas; + OptionList bandwidths; + OptionList clockSources; uhd::range_t gainRange; uhd::usrp::multi_usrp::sptr dev;