From 2b877e304d52c406720050aa55eed97b6f7869be Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 7 May 2017 14:22:21 +0200 Subject: Add WIP for OutputUHDFeedback --- src/OutputUHDFeedback.cpp | 245 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 src/OutputUHDFeedback.cpp (limited to 'src/OutputUHDFeedback.cpp') diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp new file mode 100644 index 0000000..dfe0f74 --- /dev/null +++ b/src/OutputUHDFeedback.cpp @@ -0,0 +1,245 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the + Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2017 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + +DESCRIPTION: + This presents a TCP socket to an external tool which calculates + a Digital Predistortion model from a short sequence of transmit + samples and corresponding receive samples. +*/ + +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#include +#include +#include +#include "OutputUHDFeedback.h" +#include "Utils.h" + +using namespace std; +typedef std::complex complexf; + +OutputUHDFeedback::OutputUHDFeedback() +{ + running = false; +} + +void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port) +{ + myUsrp = usrp; + burstRequest.state = BurstRequestState::None; + + if (port) { + m_port = port; + running = true; + + rx_burst_thread = boost::thread(&OutputUHDFeedback::ReceiveBurstThread, this); + burst_tcp_thread = boost::thread(&OutputUHDFeedback::ServeFeedbackThread, this); + } +} + +OutputUHDFeedback::~OutputUHDFeedback() +{ + running = false; + rx_burst_thread.join(); + burst_tcp_thread.join(); +} + +void OutputUHDFeedback::set_tx_frame( + const std::vector &buf, + const struct frame_timestamp& ts) +{ + boost::mutex::scoped_lock lock(burstRequest.mutex); + + if (burstRequest.state == BurstRequestState::SaveTransmitFrame) { + const size_t n = std::min( + burstRequest.frame_length * sizeof(complexf), buf.size()); + + burstRequest.tx_samples.clear(); + burstRequest.tx_samples.resize(n); + copy(buf.begin(), buf.begin() + n, burstRequest.tx_samples.begin()); + + burstRequest.tx_second = ts.timestamp_sec; + burstRequest.tx_pps = ts.timestamp_pps; + + // Prepare the next state + burstRequest.rx_second = ts.timestamp_sec; + burstRequest.rx_pps = ts.timestamp_pps; + burstRequest.state = BurstRequestState::SaveReceiveFrame; + + lock.unlock(); + burstRequest.mutex_notification.notify_one(); + } + else { + lock.unlock(); + } +} + +void OutputUHDFeedback::ReceiveBurstThread() +{ + set_thread_name("uhdreceiveburst"); + + uhd::stream_args_t stream_args("fc32"); //complex floats + auto rxStream = myUsrp->get_rx_stream(stream_args); + + while (running) { + boost::mutex::scoped_lock lock(burstRequest.mutex); + while (burstRequest.state != BurstRequestState::SaveReceiveFrame) { + if (not running) break; + burstRequest.mutex_notification.wait(lock); + } + + if (not running) break; + + uhd::stream_cmd_t cmd( + uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); + cmd.num_samps = burstRequest.frame_length; + cmd.stream_now = false; + + double pps = burstRequest.rx_pps / 16384000.0; + cmd.time_spec = uhd::time_spec_t(burstRequest.rx_second, pps); + + rxStream->issue_stream_cmd(cmd); + + uhd::rx_metadata_t md; + burstRequest.rx_samples.resize(burstRequest.frame_length * sizeof(complexf)); + rxStream->recv(&burstRequest.rx_samples[0], burstRequest.frame_length, md); + + burstRequest.rx_second = md.time_spec.get_full_secs(); + burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; + + burstRequest.state = BurstRequestState::Acquired; + + lock.unlock(); + burstRequest.mutex_notification.notify_one(); + } +} + +void OutputUHDFeedback::ServeFeedbackThread() +{ + set_thread_name("uhdservefeedback"); + + int server_sock = -1; + try { + if ((server_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { + throw std::runtime_error("Can't create TCP socket"); + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(m_port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + throw std::runtime_error("Can't bind TCP socket"); + } + + if (listen(server_sock, 1) < 0) { + throw std::runtime_error("Can't listen TCP socket"); + } + + while (running) { + struct sockaddr_in client; + socklen_t client_len = sizeof(client); + int client_sock = accept(server_sock, + (struct sockaddr*)&client, &client_len); + + if (client_sock < 0) { + throw runtime_error("Could not establish new connection"); + } + + while (running) { + uint8_t request_version = 0; + int read = recv(client_sock, &request_version, 1, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read request verson failed"; + } + + if (request_version != 1) { + etiLog.level(info) << "DPD Feedback Server wrong request version"; + break; + } + + uint32_t num_samples = 0; + read = recv(client_sock, &num_samples, 4, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read num samples failed"; + } + + // We are ready to issue the request now + { + boost::mutex::scoped_lock lock(burstRequest.mutex); + burstRequest.frame_length = num_samples; + burstRequest.state = BurstRequestState::SaveTransmitFrame; + + lock.unlock(); + } + + // Wait for the result to be ready + boost::mutex::scoped_lock lock(burstRequest.mutex); + while (burstRequest.state != BurstRequestState::Acquired) { + if (not running) break; + burstRequest.mutex_notification.wait(lock); + } + + burstRequest.state = BurstRequestState::None; + lock.unlock(); + + if (send(client_sock, + &burstRequest.tx_second, + sizeof(burstRequest.tx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.tx_pps, + sizeof(burstRequest.tx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_pps failed"; + break; + } + +#warning "Send buf" + } + + close(client_sock); + } + } + catch (runtime_error &e) { + etiLog.level(error) << "DPD Feedback Server fault: " << e.what(); + } + + running = false; + + if (server_sock != -1) { + close(server_sock); + } +} -- cgit v1.2.3 From 4fad4de6ec39b2741f8545ed78aa58ea0a6edc6c Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 08:28:47 +0200 Subject: UHD Feedback: Do not send the beginning of the frame --- src/OutputUHD.cpp | 2 +- src/OutputUHDFeedback.cpp | 48 ++++++++++++++++++++++++++++++++--------------- src/OutputUHDFeedback.h | 10 +++++----- 3 files changed, 39 insertions(+), 21 deletions(-) (limited to 'src/OutputUHDFeedback.cpp') diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 6dc8878..6edf7df 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -284,7 +284,7 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); - uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort); + uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort, myConf.sampleRate); MDEBUG("OutputUHD:UHD ready.\n"); } diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index dfe0f74..73497a1 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -30,7 +30,14 @@ DESCRIPTION: along with ODR-DabMod. If not, see . */ +#ifdef HAVE_CONFIG_H +# include +#endif + +#ifdef HAVE_OUTPUT_UHD + #include +#include #include #include #include "OutputUHDFeedback.h" @@ -41,17 +48,18 @@ typedef std::complex complexf; OutputUHDFeedback::OutputUHDFeedback() { - running = false; + m_running = false; } -void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port) +void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, uint32_t sampleRate) { - myUsrp = usrp; + m_usrp = usrp; + m_sampleRate = sampleRate; burstRequest.state = BurstRequestState::None; if (port) { m_port = port; - running = true; + m_running = true; rx_burst_thread = boost::thread(&OutputUHDFeedback::ReceiveBurstThread, this); burst_tcp_thread = boost::thread(&OutputUHDFeedback::ServeFeedbackThread, this); @@ -60,14 +68,14 @@ void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port) OutputUHDFeedback::~OutputUHDFeedback() { - running = false; + m_running = false; rx_burst_thread.join(); burst_tcp_thread.join(); } void OutputUHDFeedback::set_tx_frame( const std::vector &buf, - const struct frame_timestamp& ts) + const struct frame_timestamp &buf_ts) { boost::mutex::scoped_lock lock(burstRequest.mutex); @@ -77,7 +85,15 @@ void OutputUHDFeedback::set_tx_frame( burstRequest.tx_samples.clear(); burstRequest.tx_samples.resize(n); - copy(buf.begin(), buf.begin() + n, burstRequest.tx_samples.begin()); + // A frame will always begin with the NULL symbol, which contains + // no power. Instead of taking n samples at the beginning of the + // frame, we take them at the end and adapt the timestamp accordingly. + + const size_t start_ix = buf.size() - n; + copy(buf.begin() + start_ix, buf.end(), burstRequest.tx_samples.begin()); + + frame_timestamp ts = buf_ts; + ts += (1.0 * start_ix) / (sizeof(complexf) * m_sampleRate); burstRequest.tx_second = ts.timestamp_sec; burstRequest.tx_pps = ts.timestamp_pps; @@ -100,16 +116,16 @@ void OutputUHDFeedback::ReceiveBurstThread() set_thread_name("uhdreceiveburst"); uhd::stream_args_t stream_args("fc32"); //complex floats - auto rxStream = myUsrp->get_rx_stream(stream_args); + auto rxStream = m_usrp->get_rx_stream(stream_args); - while (running) { + while (m_running) { boost::mutex::scoped_lock lock(burstRequest.mutex); while (burstRequest.state != BurstRequestState::SaveReceiveFrame) { - if (not running) break; + if (not m_running) break; burstRequest.mutex_notification.wait(lock); } - if (not running) break; + if (not m_running) break; uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); @@ -158,7 +174,7 @@ void OutputUHDFeedback::ServeFeedbackThread() throw std::runtime_error("Can't listen TCP socket"); } - while (running) { + while (m_running) { struct sockaddr_in client; socklen_t client_len = sizeof(client); int client_sock = accept(server_sock, @@ -168,7 +184,7 @@ void OutputUHDFeedback::ServeFeedbackThread() throw runtime_error("Could not establish new connection"); } - while (running) { + while (m_running) { uint8_t request_version = 0; int read = recv(client_sock, &request_version, 1, 0); if (!read) break; // done reading @@ -202,7 +218,7 @@ void OutputUHDFeedback::ServeFeedbackThread() // Wait for the result to be ready boost::mutex::scoped_lock lock(burstRequest.mutex); while (burstRequest.state != BurstRequestState::Acquired) { - if (not running) break; + if (not m_running) break; burstRequest.mutex_notification.wait(lock); } @@ -237,9 +253,11 @@ void OutputUHDFeedback::ServeFeedbackThread() etiLog.level(error) << "DPD Feedback Server fault: " << e.what(); } - running = false; + m_running = false; if (server_sock != -1) { close(server_sock); } } + +#endif diff --git a/src/OutputUHDFeedback.h b/src/OutputUHDFeedback.h index 31f7547..afc06b0 100644 --- a/src/OutputUHDFeedback.h +++ b/src/OutputUHDFeedback.h @@ -79,7 +79,7 @@ struct UHDReceiveBurstRequest { std::vector rx_samples; }; - +// Serve TX samples and RX feedback samples over a TCP connection class OutputUHDFeedback { public: OutputUHDFeedback(); @@ -87,12 +87,11 @@ class OutputUHDFeedback { OutputUHDFeedback& operator=(const OutputUHDFeedback& other) = delete; ~OutputUHDFeedback(); - void setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port); + void setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, uint32_t sampleRate); void set_tx_frame(const std::vector &buf, const struct frame_timestamp& ts); - private: // Thread that reacts to burstRequests and receives from the USRP void ReceiveBurstThread(void); @@ -105,9 +104,10 @@ class OutputUHDFeedback { UHDReceiveBurstRequest burstRequest; - bool running = false; + bool m_running = false; uint16_t m_port = 0; - uhd::usrp::multi_usrp::sptr myUsrp; + uint32_t m_sampleRate = 0; + uhd::usrp::multi_usrp::sptr m_usrp; }; -- cgit v1.2.3 From 5ac10e37d07cfe723ea4b396f08563889dff5a2b Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 10:20:23 +0200 Subject: Properly terminate dpd server on ctrl-c --- src/OutputUHDFeedback.cpp | 120 ++++++++++++++++++++++++++++++++++++++-------- src/OutputUHDFeedback.h | 12 +++-- 2 files changed, 107 insertions(+), 25 deletions(-) (limited to 'src/OutputUHDFeedback.cpp') diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 73497a1..8584839 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -40,6 +40,7 @@ DESCRIPTION: #include #include #include +#include #include "OutputUHDFeedback.h" #include "Utils.h" @@ -48,7 +49,7 @@ typedef std::complex complexf; OutputUHDFeedback::OutputUHDFeedback() { - m_running = false; + m_running.store(false); } void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, uint32_t sampleRate) @@ -59,7 +60,7 @@ void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, u if (port) { m_port = port; - m_running = true; + m_running.store(true); rx_burst_thread = boost::thread(&OutputUHDFeedback::ReceiveBurstThread, this); burst_tcp_thread = boost::thread(&OutputUHDFeedback::ServeFeedbackThread, this); @@ -68,7 +69,11 @@ void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, u OutputUHDFeedback::~OutputUHDFeedback() { - m_running = false; + m_running.store(false); + + rx_burst_thread.interrupt(); + burst_tcp_thread.interrupt(); + rx_burst_thread.join(); burst_tcp_thread.join(); } @@ -79,9 +84,13 @@ void OutputUHDFeedback::set_tx_frame( { boost::mutex::scoped_lock lock(burstRequest.mutex); + assert(buf.size() % sizeof(complexf) == 0); + if (burstRequest.state == BurstRequestState::SaveTransmitFrame) { const size_t n = std::min( - burstRequest.frame_length * sizeof(complexf), buf.size()); + burstRequest.num_samples * sizeof(complexf), buf.size()); + + burstRequest.num_samples = n / sizeof(complexf); burstRequest.tx_samples.clear(); burstRequest.tx_samples.resize(n); @@ -129,7 +138,7 @@ void OutputUHDFeedback::ReceiveBurstThread() uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); - cmd.num_samps = burstRequest.frame_length; + cmd.num_samps = burstRequest.num_samples; cmd.stream_now = false; double pps = burstRequest.rx_pps / 16384000.0; @@ -138,9 +147,10 @@ void OutputUHDFeedback::ReceiveBurstThread() rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; - burstRequest.rx_samples.resize(burstRequest.frame_length * sizeof(complexf)); - rxStream->recv(&burstRequest.rx_samples[0], burstRequest.frame_length, md); + burstRequest.rx_samples.resize(burstRequest.num_samples * sizeof(complexf)); + rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); + // The recv might have happened at another time than requested burstRequest.rx_second = md.time_spec.get_full_secs(); burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; @@ -151,13 +161,32 @@ void OutputUHDFeedback::ReceiveBurstThread() } } +static int accept_with_timeout(int server_socket, int timeout_ms, struct sockaddr_in *client) +{ + struct pollfd fds[1]; + fds[0].fd = server_socket; + fds[0].events = POLLIN | POLLOUT; + + int retval = poll(fds, 1, timeout_ms); + + if (retval == -1) { + throw std::runtime_error("TCP Socket accept error: " + to_string(errno)); + } + else if (retval) { + socklen_t client_len = sizeof(struct sockaddr_in); + return accept(server_socket, (struct sockaddr*)&client, &client_len); + } + else { + return -2; + } +} + void OutputUHDFeedback::ServeFeedbackThread() { set_thread_name("uhdservefeedback"); - int server_sock = -1; try { - if ((server_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { + if ((m_server_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { throw std::runtime_error("Can't create TCP socket"); } @@ -166,31 +195,32 @@ void OutputUHDFeedback::ServeFeedbackThread() addr.sin_port = htons(m_port); addr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + if (bind(m_server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { throw std::runtime_error("Can't bind TCP socket"); } - if (listen(server_sock, 1) < 0) { + if (listen(m_server_sock, 1) < 0) { throw std::runtime_error("Can't listen TCP socket"); } while (m_running) { struct sockaddr_in client; - socklen_t client_len = sizeof(client); - int client_sock = accept(server_sock, - (struct sockaddr*)&client, &client_len); + int client_sock = accept_with_timeout(m_server_sock, 1000, &client); - if (client_sock < 0) { + if (client_sock == -1) { throw runtime_error("Could not establish new connection"); } + else if (client_sock == -2) { + continue; + } while (m_running) { uint8_t request_version = 0; - int read = recv(client_sock, &request_version, 1, 0); + ssize_t read = recv(client_sock, &request_version, 1, 0); if (!read) break; // done reading if (read < 0) { etiLog.level(info) << - "DPD Feedback Server Client read request verson failed"; + "DPD Feedback Server Client read request version failed"; } if (request_version != 1) { @@ -209,7 +239,7 @@ void OutputUHDFeedback::ServeFeedbackThread() // We are ready to issue the request now { boost::mutex::scoped_lock lock(burstRequest.mutex); - burstRequest.frame_length = num_samples; + burstRequest.num_samples = num_samples; burstRequest.state = BurstRequestState::SaveTransmitFrame; lock.unlock(); @@ -225,6 +255,15 @@ void OutputUHDFeedback::ServeFeedbackThread() burstRequest.state = BurstRequestState::None; lock.unlock(); + if (send(client_sock, + &burstRequest.num_samples, + sizeof(burstRequest.num_samples), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send num_samples failed"; + break; + } + if (send(client_sock, &burstRequest.tx_second, sizeof(burstRequest.tx_second), @@ -243,7 +282,45 @@ void OutputUHDFeedback::ServeFeedbackThread() break; } -#warning "Send buf" + const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); + + assert(burstRequest.tx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.tx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_frame failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_second, + sizeof(burstRequest.rx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_pps, + sizeof(burstRequest.rx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_pps failed"; + break; + } + + assert(burstRequest.rx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.rx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_frame failed"; + break; + } } close(client_sock); @@ -255,8 +332,9 @@ void OutputUHDFeedback::ServeFeedbackThread() m_running = false; - if (server_sock != -1) { - close(server_sock); + if (m_server_sock != -1) { + close(m_server_sock); + m_server_sock = -1; } } diff --git a/src/OutputUHDFeedback.h b/src/OutputUHDFeedback.h index afc06b0..32668b6 100644 --- a/src/OutputUHDFeedback.h +++ b/src/OutputUHDFeedback.h @@ -44,6 +44,7 @@ DESCRIPTION: #include #include #include +#include #include "Log.h" #include "TimestampDecoder.h" @@ -62,21 +63,23 @@ struct UHDReceiveBurstRequest { BurstRequestState state; - // In the SaveTransmit states, frame_length samples are saved into + // In the SaveTransmit states, num_samples complexf samples are saved into // the vectors - size_t frame_length; + size_t num_samples; // The timestamp of the first sample of the TX buffers uint32_t tx_second; uint32_t tx_pps; // in units of 1/16384000s + // Samples contain complexf, but since our internal representation is uint8_t + // we keep it like that std::vector tx_samples; // The timestamp of the first sample of the RX buffers uint32_t rx_second; uint32_t rx_pps; - std::vector rx_samples; + std::vector rx_samples; // Also, actually complexf }; // Serve TX samples and RX feedback samples over a TCP connection @@ -104,7 +107,8 @@ class OutputUHDFeedback { UHDReceiveBurstRequest burstRequest; - bool m_running = false; + std::atomic_bool m_running; + int m_server_sock = -1; uint16_t m_port = 0; uint32_t m_sampleRate = 0; uhd::usrp::multi_usrp::sptr m_usrp; -- cgit v1.2.3 From 201d2cd2e0431a5ea79fb69561c27555f3a03dc1 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 10:44:33 +0200 Subject: Add dpd example script to fetch samples --- dpd/README.md | 10 +++ dpd/show_spectrum.py | 99 ++++++++++++++++++++++ src/OutputUHDFeedback.cpp | 208 +++++++++++++++++++++++----------------------- 3 files changed, 214 insertions(+), 103 deletions(-) create mode 100644 dpd/README.md create mode 100755 dpd/show_spectrum.py (limited to 'src/OutputUHDFeedback.cpp') diff --git a/dpd/README.md b/dpd/README.md new file mode 100644 index 0000000..828a483 --- /dev/null +++ b/dpd/README.md @@ -0,0 +1,10 @@ +Digital Predistortion for ODR-DabMod +==================================== + +This folder contains work in progress for digital predistortion. It requires: + +- USRP B200. +- Power amplifier. +- A feedback connection from the power amplifier output, at an appropriate power level for the B200. + Usually this is done with a directional coupler. +- ODR-DabMod with enabled dpd_port, and with a samplerate of 8192000 samples per second. diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py new file mode 100755 index 0000000..6c489e0 --- /dev/null +++ b/dpd/show_spectrum.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This is an example tool that shows how to connect to ODR-DabMod's dpd TCP server +# and get samples from there. +# +# Since the TX and RX samples are not perfectly aligned, the tool has to align them properly, +# which is done in two steps: First on sample-level using a correlation, then with subsample +# accuracy using a FFT approach. +# +# It requires SciPy and matplotlib. +# +# Copyright (C) 2017 Matthias P. Braendli +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import sys +import socket +import struct +import numpy as np +import matplotlib as mp +import scipy.signal + +SIZEOF_SAMPLE = 8 # complex floats + +if len(sys.argv) != 3: + print("Usage: show_spectrum.py ") + sys.exit(1) + +port = int(sys.argv[1]) +num_samps_to_request = int(sys.argv[2]) + + +def get_samples(port, num_samps_to_request): + """Connect to ODR-DabMod, retrieve TX and RX samples, load + into numpy arrays, and return a tuple + (tx_timestamp, tx_samples, rx_timestamp, rx_samples) + where the timestamps are doubles, and the samples are numpy + arrays of complex floats, both having the same size + """ + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(('localhost', port)) + + print("Send version"); + s.sendall(b"\x01") + + print("Send request for {} samples".format(num_samps_to_request)) + s.sendall(struct.pack("=I", num_samps_to_request)) + + print("Wait for TX metadata") + num_samps, tx_second, tx_pps = struct.unpack("=III", s.recv(12)) + tx_ts = tx_second + tx_pps / 16384000.0 + + print("Receiving {} TX samples".format(num_samps)) + txframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) + txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + + print("Wait for RX metadata") + rx_second, rx_pps = struct.unpack("=II", s.recv(8)) + rx_ts = rx_second + rx_pps / 16384000.0 + + print("Receiving {} RX samples".format(num_samps)) + rxframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) + rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + + print("Disconnecting") + s.close() + + return (tx_ts, txframe, rx_ts, rxframe) + + +tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) + +print("Received {} & {} frames at {} and {}".format( + len(txframe), len(rxframe), tx_ts, rx_ts)) + + +# The MIT License (MIT) +# +# Copyright (c) 2017 Matthias P. Braendli +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 8584839..09b73ba 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -38,8 +38,10 @@ DESCRIPTION: #include #include +#include #include #include +#include #include #include "OutputUHDFeedback.h" #include "Utils.h" @@ -214,113 +216,113 @@ void OutputUHDFeedback::ServeFeedbackThread() continue; } - while (m_running) { - uint8_t request_version = 0; - ssize_t read = recv(client_sock, &request_version, 1, 0); - if (!read) break; // done reading - if (read < 0) { - etiLog.level(info) << - "DPD Feedback Server Client read request version failed"; - } - - if (request_version != 1) { - etiLog.level(info) << "DPD Feedback Server wrong request version"; - break; - } - - uint32_t num_samples = 0; - read = recv(client_sock, &num_samples, 4, 0); - if (!read) break; // done reading - if (read < 0) { - etiLog.level(info) << - "DPD Feedback Server Client read num samples failed"; - } - - // We are ready to issue the request now - { - boost::mutex::scoped_lock lock(burstRequest.mutex); - burstRequest.num_samples = num_samples; - burstRequest.state = BurstRequestState::SaveTransmitFrame; - - lock.unlock(); - } - - // Wait for the result to be ready + uint8_t request_version = 0; + ssize_t read = recv(client_sock, &request_version, 1, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read request version failed: " << strerror(errno); + break; + } + + if (request_version != 1) { + etiLog.level(info) << "DPD Feedback Server wrong request version"; + break; + } + + uint32_t num_samples = 0; + read = recv(client_sock, &num_samples, 4, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read num samples failed"; + break; + } + + // We are ready to issue the request now + { boost::mutex::scoped_lock lock(burstRequest.mutex); - while (burstRequest.state != BurstRequestState::Acquired) { - if (not m_running) break; - burstRequest.mutex_notification.wait(lock); - } + burstRequest.num_samples = num_samples; + burstRequest.state = BurstRequestState::SaveTransmitFrame; - burstRequest.state = BurstRequestState::None; lock.unlock(); + } + + // Wait for the result to be ready + boost::mutex::scoped_lock lock(burstRequest.mutex); + while (burstRequest.state != BurstRequestState::Acquired) { + if (not m_running) break; + burstRequest.mutex_notification.wait(lock); + } + + burstRequest.state = BurstRequestState::None; + lock.unlock(); + + if (send(client_sock, + &burstRequest.num_samples, + sizeof(burstRequest.num_samples), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send num_samples failed"; + break; + } + + if (send(client_sock, + &burstRequest.tx_second, + sizeof(burstRequest.tx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.tx_pps, + sizeof(burstRequest.tx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_pps failed"; + break; + } + + const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); + + assert(burstRequest.tx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.tx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_frame failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_second, + sizeof(burstRequest.rx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_pps, + sizeof(burstRequest.rx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_pps failed"; + break; + } - if (send(client_sock, - &burstRequest.num_samples, - sizeof(burstRequest.num_samples), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send num_samples failed"; - break; - } - - if (send(client_sock, - &burstRequest.tx_second, - sizeof(burstRequest.tx_second), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send tx_second failed"; - break; - } - - if (send(client_sock, - &burstRequest.tx_pps, - sizeof(burstRequest.tx_pps), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send tx_pps failed"; - break; - } - - const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); - - assert(burstRequest.tx_samples.size() == frame_bytes); - if (send(client_sock, - &burstRequest.tx_samples[0], - frame_bytes, - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send tx_frame failed"; - break; - } - - if (send(client_sock, - &burstRequest.rx_second, - sizeof(burstRequest.rx_second), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send rx_second failed"; - break; - } - - if (send(client_sock, - &burstRequest.rx_pps, - sizeof(burstRequest.rx_pps), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send rx_pps failed"; - break; - } - - assert(burstRequest.rx_samples.size() == frame_bytes); - if (send(client_sock, - &burstRequest.rx_samples[0], - frame_bytes, - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send rx_frame failed"; - break; - } + assert(burstRequest.rx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.rx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_frame failed"; + break; } close(client_sock); -- cgit v1.2.3 From a656cee6c9c230bb921c6bb6be0f0180460a96b4 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 11:23:15 +0200 Subject: DPD: handle incomplete frames --- dpd/show_spectrum.py | 31 +++++++++++++++++++++++-------- src/OutputUHD.cpp | 2 +- src/OutputUHDFeedback.cpp | 25 ++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 12 deletions(-) (limited to 'src/OutputUHDFeedback.cpp') diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index 6c489e0..c1d5fe5 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -30,6 +30,15 @@ if len(sys.argv) != 3: port = int(sys.argv[1]) num_samps_to_request = int(sys.argv[2]) +def recv_exact(sock, num_bytes): + bufs = [] + while num_bytes > 0: + b = sock.recv(num_bytes) + if len(b) == 0: + break + num_bytes -= len(b) + bufs.append(b) + return b''.join(bufs) def get_samples(port, num_samps_to_request): """Connect to ODR-DabMod, retrieve TX and RX samples, load @@ -49,20 +58,26 @@ def get_samples(port, num_samps_to_request): s.sendall(struct.pack("=I", num_samps_to_request)) print("Wait for TX metadata") - num_samps, tx_second, tx_pps = struct.unpack("=III", s.recv(12)) + num_samps, tx_second, tx_pps = struct.unpack("=III", recv_exact(s, 12)) tx_ts = tx_second + tx_pps / 16384000.0 - print("Receiving {} TX samples".format(num_samps)) - txframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) - txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + if num_samps > 0: + print("Receiving {} TX samples".format(num_samps)) + txframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) + txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + else: + txframe = np.array([], dtype=np.complex64) print("Wait for RX metadata") - rx_second, rx_pps = struct.unpack("=II", s.recv(8)) + rx_second, rx_pps = struct.unpack("=II", recv_exact(s, 8)) rx_ts = rx_second + rx_pps / 16384000.0 - print("Receiving {} RX samples".format(num_samps)) - rxframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) - rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + if num_samps > 0: + print("Receiving {} RX samples".format(num_samps)) + rxframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) + rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + else: + txframe = np.array([], dtype=np.complex64) print("Disconnecting") s.close() diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 6edf7df..5e9e17c 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -50,7 +50,7 @@ using namespace std; // Maximum number of frames that can wait in uwd.frames -static const size_t FRAMES_MAX_SIZE = 2; +static const size_t FRAMES_MAX_SIZE = 8; typedef std::complex complexf; diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 09b73ba..3ef5648 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -138,6 +138,8 @@ void OutputUHDFeedback::ReceiveBurstThread() if (not m_running) break; + etiLog.level(debug) << "Prepare RX stream command for " << burstRequest.num_samples; + uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); cmd.num_samps = burstRequest.num_samples; @@ -146,16 +148,28 @@ void OutputUHDFeedback::ReceiveBurstThread() double pps = burstRequest.rx_pps / 16384000.0; cmd.time_spec = uhd::time_spec_t(burstRequest.rx_second, pps); + const double usrp_time = m_usrp->get_time_now().get_real_secs(); + const double cmd_time = cmd.time_spec.get_real_secs(); + + etiLog.level(debug) << + "RX stream command ts=" << std::fixed << cmd_time << " Delta=" << cmd_time - usrp_time; + rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; burstRequest.rx_samples.resize(burstRequest.num_samples * sizeof(complexf)); - rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); + size_t samples_read = rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); + assert(samples_read <= burstRequest.num_samples); + burstRequest.rx_samples.resize(samples_read * sizeof(complexf)); // The recv might have happened at another time than requested burstRequest.rx_second = md.time_spec.get_full_secs(); burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; + etiLog.level(debug) << "Read " << samples_read << " RX feedback samples " + << "at time " << std::fixed << burstRequest.tx_second << "." << + burstRequest.tx_pps / 16384000.0; + burstRequest.state = BurstRequestState::Acquired; lock.unlock(); @@ -258,6 +272,11 @@ void OutputUHDFeedback::ServeFeedbackThread() burstRequest.state = BurstRequestState::None; lock.unlock(); + burstRequest.num_samples = std::min(burstRequest.num_samples, + std::min( + burstRequest.tx_samples.size() / sizeof(complexf), + burstRequest.rx_samples.size() / sizeof(complexf))); + if (send(client_sock, &burstRequest.num_samples, sizeof(burstRequest.num_samples), @@ -287,7 +306,7 @@ void OutputUHDFeedback::ServeFeedbackThread() const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); - assert(burstRequest.tx_samples.size() == frame_bytes); + assert(burstRequest.tx_samples.size() >= frame_bytes); if (send(client_sock, &burstRequest.tx_samples[0], frame_bytes, @@ -315,7 +334,7 @@ void OutputUHDFeedback::ServeFeedbackThread() break; } - assert(burstRequest.rx_samples.size() == frame_bytes); + assert(burstRequest.rx_samples.size() >= frame_bytes); if (send(client_sock, &burstRequest.rx_samples[0], frame_bytes, -- cgit v1.2.3 From 2759e2e2a86e97eeee5934d5d9a3b4c911a47229 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 14:48:39 +0200 Subject: DPD: Fix RX feedback sampling --- src/OutputUHDFeedback.cpp | 69 ++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'src/OutputUHDFeedback.cpp') diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 3ef5648..9e3aab2 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -138,8 +138,6 @@ void OutputUHDFeedback::ReceiveBurstThread() if (not m_running) break; - etiLog.level(debug) << "Prepare RX stream command for " << burstRequest.num_samples; - uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); cmd.num_samps = burstRequest.num_samples; @@ -148,6 +146,10 @@ void OutputUHDFeedback::ReceiveBurstThread() double pps = burstRequest.rx_pps / 16384000.0; cmd.time_spec = uhd::time_spec_t(burstRequest.rx_second, pps); + // We need to free the mutex while we recv(), because otherwise we block the + // TX thread + lock.unlock(); + const double usrp_time = m_usrp->get_time_now().get_real_secs(); const double cmd_time = cmd.time_spec.get_real_secs(); @@ -157,9 +159,14 @@ void OutputUHDFeedback::ReceiveBurstThread() rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; - burstRequest.rx_samples.resize(burstRequest.num_samples * sizeof(complexf)); - size_t samples_read = rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); - assert(samples_read <= burstRequest.num_samples); + + std::vector buf(cmd.num_samps * sizeof(complexf)); + + const double timeout = 60; + size_t samples_read = rxStream->recv(&buf[0], cmd.num_samps, md, timeout); + + lock.lock(); + burstRequest.rx_samples = std::move(buf); burstRequest.rx_samples.resize(samples_read * sizeof(complexf)); // The recv might have happened at another time than requested @@ -197,6 +204,22 @@ static int accept_with_timeout(int server_socket, int timeout_ms, struct sockadd } } +static ssize_t sendall(int socket, const void *buffer, size_t buflen) +{ + uint8_t *buf = (uint8_t*)buffer; + while (buflen > 0) { + ssize_t sent = send(socket, buf, buflen, 0); + if (sent < 0) { + return -1; + } + else { + buf += sent; + buflen -= sent; + } + } + return buflen; +} + void OutputUHDFeedback::ServeFeedbackThread() { set_thread_name("uhdservefeedback"); @@ -277,28 +300,24 @@ void OutputUHDFeedback::ServeFeedbackThread() burstRequest.tx_samples.size() / sizeof(complexf), burstRequest.rx_samples.size() / sizeof(complexf))); - if (send(client_sock, - &burstRequest.num_samples, - sizeof(burstRequest.num_samples), - 0) < 0) { + uint32_t num_samples_32 = burstRequest.num_samples; + if (sendall(client_sock, &num_samples_32, sizeof(num_samples_32)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send num_samples failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.tx_second, - sizeof(burstRequest.tx_second), - 0) < 0) { + sizeof(burstRequest.tx_second)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send tx_second failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.tx_pps, - sizeof(burstRequest.tx_pps), - 0) < 0) { + sizeof(burstRequest.tx_pps)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send tx_pps failed"; break; @@ -307,38 +326,34 @@ void OutputUHDFeedback::ServeFeedbackThread() const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); assert(burstRequest.tx_samples.size() >= frame_bytes); - if (send(client_sock, + if (sendall(client_sock, &burstRequest.tx_samples[0], - frame_bytes, - 0) < 0) { + frame_bytes) < 0) { etiLog.level(info) << "DPD Feedback Server Client send tx_frame failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.rx_second, - sizeof(burstRequest.rx_second), - 0) < 0) { + sizeof(burstRequest.rx_second)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send rx_second failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.rx_pps, - sizeof(burstRequest.rx_pps), - 0) < 0) { + sizeof(burstRequest.rx_pps)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send rx_pps failed"; break; } assert(burstRequest.rx_samples.size() >= frame_bytes); - if (send(client_sock, + if (sendall(client_sock, &burstRequest.rx_samples[0], - frame_bytes, - 0) < 0) { + frame_bytes) < 0) { etiLog.level(info) << "DPD Feedback Server Client send rx_frame failed"; break; -- cgit v1.2.3 From 1deec1418cd49e27bc2f7ddd6cd22ac6607642b3 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 15:21:22 +0200 Subject: DPD: Set RX gain and frequency, add gain to RX and config --- dpd/dpd.ini | 3 ++ src/ConfigParser.cpp | 1 + src/OutputUHD.cpp | 70 +++++++++++++++++++++++++++++++---------------- src/OutputUHD.h | 1 + src/OutputUHDFeedback.cpp | 10 +++---- 5 files changed, 56 insertions(+), 29 deletions(-) (limited to 'src/OutputUHDFeedback.cpp') diff --git a/dpd/dpd.ini b/dpd/dpd.ini index 906827b..625df73 100644 --- a/dpd/dpd.ini +++ b/dpd/dpd.ini @@ -1,6 +1,8 @@ [remotecontrol] telnet=1 telnetport=2121 +zmqctrl=1 +zmqctrlendpoint=tcp://127.0.0.1:9400 [log] syslog=0 @@ -32,6 +34,7 @@ pps_source=none behaviour_refclk_lock_lost=ignore max_gps_holdover_time=600 dpd_port=50055 +rxgain=10 [delaymanagement] ; Use synchronous=1 so that the USRP time is set. This works diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 8892642..459811f 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -218,6 +218,7 @@ static void parse_configfile( } outputuhd_conf.txgain = pt.get("uhdoutput.txgain", 0.0); + outputuhd_conf.rxgain = pt.get("uhdoutput.rxgain", 0.0); outputuhd_conf.frequency = pt.get("uhdoutput.frequency", 0); std::string chan = pt.get("uhdoutput.channel", ""); outputuhd_conf.dabMode = mod_settings.dabMode; diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 5e9e17c..f764fb8 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -80,6 +80,36 @@ void uhd_msg_handler(uhd::msg::type_t type, const std::string &msg) } } +static void tune_usrp_to( + uhd::usrp::multi_usrp::sptr usrp, + double lo_offset, + double frequency) +{ + if (lo_offset != 0.0) { + etiLog.level(info) << std::fixed << std::setprecision(3) << + "OutputUHD:Setting freq to " << frequency << + " with LO offset " << lo_offset << "..."; + + const auto tr = uhd::tune_request_t(frequency, lo_offset); + uhd::tune_result_t result = usrp->set_tx_freq(tr); + + etiLog.level(debug) << "OutputUHD:" << + std::fixed << std::setprecision(0) << + " Target RF: " << result.target_rf_freq << + " Actual RF: " << result.actual_rf_freq << + " Target DSP: " << result.target_dsp_freq << + " Actual DSP: " << result.actual_dsp_freq; + } + else { + //set the centre frequency + etiLog.level(info) << std::fixed << std::setprecision(3) << + "OutputUHD:Setting freq to " << frequency << "..."; + usrp->set_tx_freq(frequency); + } + + usrp->set_rx_freq(frequency); +} + // Check function for GPS TIMELOCK sensor from the ODR LEA-M8F board GPSDO bool check_gps_timelock(uhd::usrp::multi_usrp::sptr usrp) { @@ -165,6 +195,7 @@ OutputUHD::OutputUHD( /* register the parameters that can be remote controlled */ RC_ADD_PARAMETER(txgain, "UHD analog daughterboard TX gain"); + RC_ADD_PARAMETER(rxgain, "UHD analog daughterboard RX gain for DPD feedback"); RC_ADD_PARAMETER(freq, "UHD transmission frequency"); RC_ADD_PARAMETER(muting, "Mute the output by stopping the transmitter"); RC_ADD_PARAMETER(staticdelay, "Set static delay (uS) between 0 and 96000"); @@ -223,31 +254,14 @@ OutputUHD::OutputUHD( throw std::runtime_error("Cannot set USRP sample rate. Aborted."); } - if (myConf.lo_offset != 0.0) { - etiLog.level(info) << std::fixed << std::setprecision(3) << - "OutputUHD:Setting freq to " << myConf.frequency << - " with LO offset " << myConf.lo_offset << "..."; - - const auto tr = uhd::tune_request_t(myConf.frequency, myConf.lo_offset); - uhd::tune_result_t result = myUsrp->set_tx_freq(tr); - - etiLog.level(debug) << "OutputUHD:" << - std::fixed << std::setprecision(0) << - " Target RF: " << result.target_rf_freq << - " Actual RF: " << result.actual_rf_freq << - " Target DSP: " << result.target_dsp_freq << - " Actual DSP: " << result.actual_dsp_freq; - } - else { - //set the centre frequency - etiLog.level(info) << std::fixed << std::setprecision(3) << - "OutputUHD:Setting freq to " << myConf.frequency << "..."; - myUsrp->set_tx_freq(myConf.frequency); - } + tune_usrp_to(myUsrp, myConf.lo_offset, myConf.frequency); myConf.frequency = myUsrp->get_tx_freq(); etiLog.level(info) << std::fixed << std::setprecision(3) << - "OutputUHD:Actual frequency: " << myConf.frequency; + "OutputUHD:Actual TX frequency: " << myConf.frequency; + + etiLog.level(info) << std::fixed << std::setprecision(3) << + "OutputUHD:Actual RX frequency: " << myUsrp->get_tx_freq(); myUsrp->set_tx_gain(myConf.txgain); MDEBUG("OutputUHD:Actual TX Gain: %f ...\n", myUsrp->get_tx_gain()); @@ -284,6 +298,9 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); + myUsrp->set_rx_gain(myConf.rxgain); + MDEBUG("OutputUHD:Actual RX Gain: %f ...\n", myUsrp->get_rx_gain()); + uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort, myConf.sampleRate); MDEBUG("OutputUHD:UHD ready.\n"); @@ -910,9 +927,13 @@ void OutputUHD::set_parameter(const string& parameter, const string& value) ss >> myConf.txgain; myUsrp->set_tx_gain(myConf.txgain); } + else if (parameter == "rxgain") { + ss >> myConf.rxgain; + myUsrp->set_rx_gain(myConf.rxgain); + } else if (parameter == "freq") { ss >> myConf.frequency; - myUsrp->set_tx_freq(myConf.frequency); + tune_usrp_to(myUsrp, myConf.lo_offset, myConf.frequency); myConf.frequency = myUsrp->get_tx_freq(); } else if (parameter == "muting") { @@ -951,6 +972,9 @@ const string OutputUHD::get_parameter(const string& parameter) const if (parameter == "txgain") { ss << myConf.txgain; } + else if (parameter == "rxgain") { + ss << myConf.rxgain; + } else if (parameter == "freq") { ss << myConf.frequency; } diff --git a/src/OutputUHD.h b/src/OutputUHD.h index 1246fc5..c966c7e 100644 --- a/src/OutputUHD.h +++ b/src/OutputUHD.h @@ -189,6 +189,7 @@ struct OutputUHDConfig { double frequency = 0.0; double lo_offset = 0.0; double txgain = 0.0; + double rxgain = 0.0; bool enableSync = false; bool muteNoTimestamps = false; unsigned dabMode = 0; diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 9e3aab2..788b0a9 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -153,9 +153,6 @@ void OutputUHDFeedback::ReceiveBurstThread() const double usrp_time = m_usrp->get_time_now().get_real_secs(); const double cmd_time = cmd.time_spec.get_real_secs(); - etiLog.level(debug) << - "RX stream command ts=" << std::fixed << cmd_time << " Delta=" << cmd_time - usrp_time; - rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; @@ -173,9 +170,10 @@ void OutputUHDFeedback::ReceiveBurstThread() burstRequest.rx_second = md.time_spec.get_full_secs(); burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; - etiLog.level(debug) << "Read " << samples_read << " RX feedback samples " - << "at time " << std::fixed << burstRequest.tx_second << "." << - burstRequest.tx_pps / 16384000.0; + etiLog.level(debug) << "DPD: acquired " << samples_read << " RX feedback samples " << + "at time " << burstRequest.tx_second << " + " << + std::fixed << burstRequest.tx_pps / 16384000.0 << + " Delta=" << cmd_time - usrp_time; burstRequest.state = BurstRequestState::Acquired; -- cgit v1.2.3 From a2957184946e48bd4644325a044698fbe0c24ee7 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 13 May 2017 15:27:56 +0200 Subject: Print info about DPD port --- src/OutputUHDFeedback.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/OutputUHDFeedback.cpp') diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 788b0a9..2a99e6b 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -240,6 +240,8 @@ void OutputUHDFeedback::ServeFeedbackThread() throw std::runtime_error("Can't listen TCP socket"); } + etiLog.level(info) << "DPD Feedback server listening on port " << m_port; + while (m_running) { struct sockaddr_in client; int client_sock = accept_with_timeout(m_server_sock, 1000, &client); -- cgit v1.2.3