free5GRAN  V1.0
rf.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Telecom Paris
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
17 #include "rf.h"
18 #include <uhd.h>
19 #include <uhd/utils/thread.hpp>
20 #include <boost/program_options.hpp>
21 #include <boost/format.hpp>
22 #include <boost/thread.hpp>
23 #include <iostream>
24 #include <complex>
25 #include <vector>
26 using namespace std;
27 
28 /*
29  * Initialize USRP device
30  */
31 rf::rf(double sample_rate, double center_frequency, double gain, double bandwidth,
32  const string &subdev, const string &antenna_mode, const string &ref, const string &device_args) {
33 
46  uhd::set_thread_priority_safe();
47 
48  this->sample_rate = sample_rate;
49  this->center_frequency = center_frequency;
50  this->gain = gain;
51  this->bandwidth = bandwidth;
52  this->device_args = device_args;
53  this->subdev = subdev;
54  this->antenna_mode = antenna_mode;
55  this->ref = ref;
56 
57  uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(device_args);
58  this->usrp = usrp;
59  this->usrp->set_clock_source(this->ref);
60  this->usrp->set_rx_subdev_spec(this->subdev );
61  this->usrp->set_rx_rate(this->sample_rate);
62  uhd::tune_request_t tune_request(this->center_frequency);
63  this->usrp->set_rx_freq(tune_request);
64  this->usrp->set_rx_gain(this->gain);
65  this->usrp->set_rx_bandwidth(this->bandwidth);
66  this->usrp->set_rx_antenna(this->antenna_mode);
67 }
68 
69 double rf::getSampleRate() const {
74  return usrp->get_rx_rate();
75 }
76 
77 /*
78  * Get samples from USRP device
79  */
80 void rf::get_samples(vector<complex<float>> *buff, double &time_first_sample) {
91  size_t num_samples = buff->size();
92  uhd::set_thread_priority_safe();
93  uhd::stream_args_t stream_args("fc32", "sc16");
94  uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
95  uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
96  stream_cmd.num_samps = size_t(num_samples);
97  stream_cmd.stream_now = true;
98  rx_stream->issue_stream_cmd(stream_cmd);
99 
100  uhd::rx_metadata_t md;
101 
102  size_t total_rcvd_samples = 0;
103 
104  while (total_rcvd_samples<num_samples){
105  size_t num_rx_samps = rx_stream->recv(&buff->front(), buff->size() - total_rcvd_samples, md, 10.0, false);
106  if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
107  cout << boost::format("Timeout while streaming") << endl;
108  break;
109  }
110  if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW) {
111  cout << boost::format("Overflow\n") << endl;
112 
113  continue;
114  }
115  if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) {
116  string error = str(boost::format("Receiver error: %s") % md.strerror());
117  }
118  total_rcvd_samples += num_rx_samps;
119  }
120  uhd::time_spec_t time_spec = md.time_spec;
121  time_first_sample = time_spec.get_full_secs();
122  time_first_sample += time_spec.get_frac_secs();
123 }
124 
125 
126 void rf::setSampleRate(double rate) {
132  this->usrp->set_rx_rate(rate);
133  this->sample_rate = rate;
134  usrp->set_rx_bandwidth(rate);
135 }
136 
138  return this->center_frequency;
139 }
140 
141 void rf::setCenterFrequency(double freq) {
147  uhd::tune_request_t tune_request(freq);
148  this->usrp->set_rx_freq(tune_request);
149  this->center_frequency = freq;
150 
151 }
152 
153 void rf::setGain(double gain) {
159  this->gain = gain;
160  this->usrp->set_rx_gain(this->gain);
161 }
162 
163 double rf::getGain() {
164  return this->gain;
165 }
double getSampleRate() const
Definition: rf.cpp:69
void setSampleRate(double rate)
Definition: rf.cpp:126
void get_samples(vector< complex< float >> *buff, double &time_first_sample)
Definition: rf.cpp:80
void setCenterFrequency(double freq)
Definition: rf.cpp:141
rf(double sample_rate, double center_frequency, double gain, double bandwidth, const string &subdev, const string &antenna_mode, const string &ref, const string &device_args)
Definition: rf.cpp:31
void setGain(double gain)
Definition: rf.cpp:153
double getGain()
Definition: rf.cpp:163
double getCenterFrequency()
Definition: rf.cpp:137