Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (131 loc) · 5.48 KB
/
Copy pathmain.cpp
File metadata and controls
165 lines (131 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <uhd/types/tune_request.hpp>
#include <uhd/utils/thread_priority.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/transport/udp_simple.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <complex>
#include <fftw3.h>
uhd::usrp::multi_usrp::sptr usrp;
std::string addr("127.0.0.1");
std::string device_args("addr=192.168.10.6");
std::string subdev("A:0");
std::string ant("TX/RX");
std::string ref("internal");
std::string port("7124"); //UDP Port
double rate(1e6);
double freq(1955e6);
double gain(20);
double bw(20e6);
size_t total_num_samps(200e6);
void setupUSRP();
int UHD_SAFE_MAIN(int argc, char *argv[]) {
uhd::set_thread_priority_safe();
setupUSRP();
//Check Ref and LO Lock detect
std::vector<std::string> sensor_names;
sensor_names = usrp->get_rx_sensor_names(0);
if (std::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);
std::cout << boost::format("Checking RX: %s ...") % lo_locked.to_pp_string() << std::endl;
UHD_ASSERT_THROW(lo_locked.to_bool());
}
//create fftw_plan for later
fftwf_plan p;
fftwf_complex *in, *out;
//create a receive streamer
uhd::stream_args_t stream_args("fc32", "sc16"); //complex floats
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
//setup streaming
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
stream_cmd.num_samps = total_num_samps;
stream_cmd.stream_now = true;
rx_stream->issue_stream_cmd(stream_cmd);
//loop until total number of samples reached
size_t num_acc_samps = 0; //number of accumulated samples
uhd::rx_metadata_t md;
std::vector<std::complex<float> > buff(rx_stream->get_max_num_samps());
uhd::transport::udp_simple::sptr udp_xport = uhd::transport::udp_simple::make_connected(addr, port);
size_t buffer_size = rx_stream->get_max_num_samps();
in = (fftwf_complex*)fftwf_alloc_complex(rx_stream->get_max_num_samps());
out = in;
p = fftwf_plan_dft_1d(buffer_size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
while (num_acc_samps < total_num_samps) {
size_t num_rx_samps = rx_stream->recv(
in, buffer_size, md
);
//handle the error codes
switch (md.error_code) {
case uhd::rx_metadata_t::ERROR_CODE_NONE:
break;
case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT:
if (num_acc_samps == 0) continue;
std::cout << boost::format(
"Got timeout before all samples received, possible packet loss, exiting loop..."
) << std::endl;
goto done_loop;
default:
std::cout << boost::format(
"Got error code 0x%x, exiting loop..."
) % md.error_code << std::endl;
goto done_loop;
}
fftwf_execute(p);
std::vector<float> output(buffer_size);
// Convert to power spectrum, which is the magnitude of each frequency component squared.
/*for (int i = 0; i < buffer_size; i++)
{
output.at(i) = std::sqrtf(powf(in[i][0], 2), powf(in[i][1], 2)); // take magnitude
}
*/
/*
for (std::vector<std::complex<float> >::iterator it = buff.begin(); it != buff.end(); it++) {
*it = std::norm(*it);
}*/
//send complex single precision floating point samples over udpstd::
udp_xport->send(boost::asio::buffer(in, num_rx_samps * sizeof(fftwf_complex)));
num_acc_samps += num_rx_samps;
} done_loop:
//cleanup fftw
fftwf_destroy_plan(p);
fftwf_free(in); fftwf_free(out);
//finished
std::cout << std::endl << "Done!" << std::endl << std::endl;
return EXIT_SUCCESS;
}
void setupUSRP() {
//create a usrp device
std::cout << std::endl;
std::cout << boost::format("Creating the usrp device with: %s...") % device_args << std::endl;
usrp = uhd::usrp::multi_usrp::make(device_args);
std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
//Lock mboard clocks
usrp->set_clock_source(ref);
//always select the subdevice first, the channel mapping affects the other settings
usrp->set_rx_subdev_spec(subdev);
std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
//set the rx sample rate
std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate / 1e6) << std::endl;
usrp->set_rx_rate(rate);
std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate() / 1e6) << std::endl << std::endl;
//set the rx center frequency
std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq / 1e6) << std::endl;
uhd::tune_request_t tune_request(freq);
tune_request.args = uhd::device_addr_t("mode_n=integer");
usrp->set_rx_freq(tune_request);
std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq() / 1e6) << std::endl << std::endl;
//set the rx rf gain
std::cout << boost::format("Setting RX Gain: %f dB...") % gain << std::endl;
usrp->set_rx_gain(gain);
std::cout << boost::format("Actual RX Gain: %f dB...") % usrp->get_rx_gain() << std::endl << std::endl;
//set the analog frontend filter bandwidth
std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % (bw / 1e6) << std::endl;
usrp->set_rx_bandwidth(bw);
std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % (usrp->get_rx_bandwidth() / 1e6) << std::endl << std::endl;
//set the antenna
usrp->set_rx_antenna(ant);
boost::this_thread::sleep(boost::posix_time::seconds(1)); //allow for some setup time
}