fastsa/main.cpp

269 lines
9.7 KiB
C++
Raw Normal View History

2023-08-23 18:44:23 +08:00
#include <iostream>
2023-08-23 15:06:05 +08:00
#include <cstdlib>
2023-08-23 18:44:23 +08:00
#include <cstring>
2023-08-23 15:53:50 +08:00
#include <thread>
#include <atomic>
#include <mutex>
2023-08-23 18:44:23 +08:00
#include <vector>
2023-08-23 21:29:42 +08:00
#include <complex>
#include <chrono>
2023-09-11 19:20:51 +08:00
#include <format>
#include <experimental/net>
2023-08-23 15:06:05 +08:00
2023-08-22 23:41:16 +08:00
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
2023-09-07 20:17:02 +08:00
#include <libbladeRF.h>
2023-08-23 18:44:23 +08:00
2023-09-11 19:20:20 +08:00
#define POCKETFFT_CACHE_SIZE (16*1024*1024)
#define POCKETFFT_NO_MULTITHREADING // MT doesn't seem to help at small sizes
2023-08-23 19:57:01 +08:00
#include <pocketfft_hdronly.h>
2023-09-11 19:20:51 +08:00
namespace net = std::experimental::net;
2023-08-23 19:57:01 +08:00
2023-08-23 15:53:50 +08:00
static std::atomic<bool> terminate_dsp;
2023-08-23 18:44:23 +08:00
2023-09-07 20:17:02 +08:00
struct bladerf* bladerf_dev;
2023-08-23 18:44:23 +08:00
2023-08-23 15:53:50 +08:00
static std::mutex waterfall_data_mutex;
2023-09-07 21:00:22 +08:00
static int waterfall_width = 1000;
static int waterfall_height = 2000;
static unsigned int waterfall_data[1000*2000];
2023-08-23 15:53:50 +08:00
2023-09-11 20:00:48 +08:00
static std::atomic<float> freq_setpoint;
static std::atomic<float> freq_peak;
static std::atomic<float> tec_bias;
static std::atomic<float> tec_p;
static std::atomic<int> fps;
2023-08-23 15:53:50 +08:00
static void dsp_thread() {
using namespace std::literals::chrono_literals;
2023-09-11 19:20:51 +08:00
net::io_context io_context;
net::ip::tcp::socket tec_socket(io_context);
tec_socket.connect(net::ip::tcp::endpoint(net::ip::make_address("192.168.1.91"), 5555));
2023-09-07 21:00:22 +08:00
size_t len = 16384;
2023-09-07 20:17:02 +08:00
std::vector<std::complex<int16_t>> frames(len);
std::vector<std::complex<float>> frames_f(len);
2023-08-23 21:29:42 +08:00
pocketfft::shape_t shape{len};
2023-09-07 21:00:22 +08:00
pocketfft::stride_t stride(1);
2023-09-11 20:00:48 +08:00
stride[0] = sizeof(std::complex<float>);
2023-08-23 21:29:42 +08:00
pocketfft::shape_t axes;
2023-09-07 21:00:22 +08:00
axes.push_back(0);
2023-09-11 20:00:48 +08:00
std::vector<std::complex<float>> frames_ft_rev(len);
std::vector<float> frames_mag(len);
2023-09-11 19:20:51 +08:00
int iterations = 0;
auto last_second = std::chrono::steady_clock::now();
2023-09-11 19:20:51 +08:00
auto last_tec = std::chrono::steady_clock::now();
2023-08-23 15:53:50 +08:00
while(!terminate_dsp) {
2023-09-07 20:17:02 +08:00
int status;
if((status = bladerf_sync_rx(bladerf_dev, frames.data(), len, NULL, 0)) != 0) {
std::cerr << "failed to receive samples from bladeRF: " << bladerf_strerror(status) << std::endl;
2023-08-23 19:46:36 +08:00
break;
}
2023-09-11 19:20:51 +08:00
// FFT
2023-08-23 21:29:42 +08:00
for(size_t i=0;i<len;i++)
frames_f[i] = frames[i];
2023-09-11 20:00:48 +08:00
pocketfft::c2c(shape, stride, stride, axes, pocketfft::FORWARD, frames_f.data(), frames_ft_rev.data(), 1.0f);
// negative frequencies first
for(size_t i=0;i<len/2;i++)
frames_mag[i] = abs(frames_ft_rev[i + len/2]);
for(size_t i=0;i<len/2;i++)
frames_mag[i + len/2] = abs(frames_ft_rev[i]);
2023-09-11 19:20:51 +08:00
// stabilize laser
if((std::chrono::steady_clock::now() - last_tec) >= 100ms) {
2023-09-11 20:00:48 +08:00
float freq_peak_local;
freq_peak_local = 40.0f*float(distance(frames_mag.begin(), max_element(frames_mag.begin(), frames_mag.end())))/float(len);
freq_peak = freq_peak_local;
2023-09-11 19:20:51 +08:00
// FIXME: net::write seems unimplemented as of libstdc++ 13
2023-09-11 20:00:48 +08:00
float freq_error = freq_peak_local - freq_setpoint;
tec_socket.write_some(net::buffer(std::format(":SOUR2:VOLT {:.3f}\n",
2023-09-11 20:40:28 +08:00
std::max(tec_bias+tec_p*freq_error, 0.0f))));
2023-09-11 19:20:51 +08:00
last_tec += 100ms;
}
// update waterfall
2023-08-23 15:53:50 +08:00
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
2023-08-23 21:29:42 +08:00
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], sizeof(int)*waterfall_width*(waterfall_height - 1));
2023-08-23 18:44:23 +08:00
for(int i=0;i<waterfall_width;i++) {
2023-09-07 21:00:22 +08:00
int j = i*len/waterfall_width;
2023-09-11 20:00:48 +08:00
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(frames_mag[j]/900.0f), 255);
2023-08-23 18:44:23 +08:00
}
2023-09-11 20:00:48 +08:00
waterfall_data[int(freq_setpoint*waterfall_width)/40] = 0xff0000ff;
2023-08-23 15:53:50 +08:00
}
2023-09-11 19:20:51 +08:00
// FPS counter
iterations++;
if((std::chrono::steady_clock::now() - last_second) >= 1s) {
fps = iterations;
iterations = 0;
last_second += 1s;
}
2023-09-11 19:20:51 +08:00
}
2023-08-23 15:53:50 +08:00
}
2023-08-23 19:46:36 +08:00
int main(int argc, char* argv[])
2023-08-22 23:41:16 +08:00
{
2023-09-07 20:17:02 +08:00
if(!glfwInit()) {
2023-08-23 18:44:23 +08:00
std::cerr << "failed to initialize GLFW" << std::endl;
2023-08-22 23:41:16 +08:00
return 1;
2023-08-23 18:44:23 +08:00
}
2023-08-23 15:06:05 +08:00
std::atexit(glfwTerminate);
2023-08-22 23:41:16 +08:00
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
2023-09-07 21:00:22 +08:00
static GLFWwindow* window = glfwCreateWindow(1280, 2200, "fastsa", nullptr, nullptr);
2023-09-07 20:17:02 +08:00
if(window == nullptr) {
2023-08-23 18:44:23 +08:00
std::cerr << "failed to create GLFW window" << std::endl;
2023-08-22 23:41:16 +08:00
return 1;
}
2023-08-23 15:06:05 +08:00
static auto DestroyWindow = []() {
glfwDestroyWindow(window);
};
std::atexit(DestroyWindow);
2023-08-22 23:41:16 +08:00
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
2023-08-23 13:12:28 +08:00
IMGUI_CHECKVERSION();
2023-08-22 23:41:16 +08:00
ImGui::CreateContext();
2023-08-23 15:06:05 +08:00
static auto ImGuiDestroyContext = []() {
ImGui::DestroyContext();
};
std::atexit(ImGuiDestroyContext);
2023-08-23 13:12:28 +08:00
ImGuiIO& io = ImGui::GetIO();
2023-08-22 23:41:16 +08:00
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
2023-08-23 15:06:05 +08:00
std::atexit(ImGui_ImplGlfw_Shutdown);
2023-08-22 23:41:16 +08:00
ImGui_ImplOpenGL3_Init("#version 130");
2023-08-23 15:06:05 +08:00
std::atexit(ImGui_ImplOpenGL3_Shutdown);
2023-08-22 23:41:16 +08:00
2023-08-23 13:12:28 +08:00
GLuint waterfall;
glGenTextures(1, &waterfall);
glBindTexture(GL_TEXTURE_2D, waterfall);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2023-09-07 20:17:02 +08:00
int status;
struct bladerf_devinfo dev_info;
bladerf_init_devinfo(&dev_info);
if((status = bladerf_open_with_devinfo(&bladerf_dev, &dev_info)) != 0) {
std::cerr << "cannot open bladeRF device: " << bladerf_strerror(status) << std::endl;
2023-08-23 18:44:23 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
static auto BladeRFClose = []() {
bladerf_close(bladerf_dev);
2023-08-23 18:44:23 +08:00
};
2023-09-07 20:17:02 +08:00
std::atexit(BladeRFClose);
2023-09-11 20:39:47 +08:00
if((status = bladerf_set_frequency(bladerf_dev, BLADERF_CHANNEL_RX(0), 1'655'000'000)) != 0) {
2023-09-07 20:17:02 +08:00
std::cerr << "failed to set bladeRF frequency: " << bladerf_strerror(status) << std::endl;
2023-08-23 19:46:36 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
if((status = bladerf_set_sample_rate(bladerf_dev, BLADERF_CHANNEL_RX(0), 40'000'000, NULL)) != 0) {
std::cerr << "failed to set bladeRF sample rate: " << bladerf_strerror(status) << std::endl;
2023-08-23 19:46:36 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
if((status = bladerf_set_bandwidth(bladerf_dev, BLADERF_CHANNEL_RX(0), 35'000'000, NULL)) != 0) {
std::cerr << "failed to set bladeRF bandwidth: " << bladerf_strerror(status) << std::endl;
2023-08-23 19:46:36 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
if((status = bladerf_set_gain(bladerf_dev, BLADERF_CHANNEL_RX(0), 20)) != 0) {
std::cerr << "failed to set bladeRF gain: " << bladerf_strerror(status) << std::endl;
2023-08-23 19:46:36 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
if((status = bladerf_sync_config(bladerf_dev, BLADERF_RX_X1, BLADERF_FORMAT_SC16_Q11,
16, /* num_buffers */
8192, /* buffer_size */
8, /* num_transfers */
3500 /* timeout_ms */)) != 0) {
std::cerr << "failed to set bladeRF sync settings: " << bladerf_strerror(status) << std::endl;
2023-08-23 19:46:36 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
if((status = bladerf_enable_module(bladerf_dev, BLADERF_RX, true) != 0)) {
std::cerr << "failed to enable bladeRF RX: " << bladerf_strerror(status) << std::endl;
2023-08-23 18:44:23 +08:00
return 1;
}
2023-09-07 20:17:02 +08:00
static auto BladeRFDisableRX = []() {
bladerf_enable_module(bladerf_dev, BLADERF_RX, false);
};
std::atexit(BladeRFDisableRX);
2023-08-23 18:44:23 +08:00
2023-08-23 15:53:50 +08:00
terminate_dsp = false;
static std::thread dsp_thread_h = std::thread(dsp_thread);
static auto TerminateDSP = []() {
terminate_dsp = true;
dsp_thread_h.join();
};
std::atexit(TerminateDSP);
2023-08-23 13:12:28 +08:00
bool exit = false;
2023-09-07 21:26:26 +08:00
bool update = true;
2023-09-11 20:00:48 +08:00
float freq_setpoint_local = 0.0f;
float tec_bias_local = 0.02f;
float tec_p_local = 0.001f;
2023-08-23 15:53:50 +08:00
while(!exit && !glfwWindowShouldClose(window)) {
2023-08-22 23:41:16 +08:00
glfwPollEvents();
2023-08-23 13:12:28 +08:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, waterfall);
#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
2023-09-07 21:26:26 +08:00
if(update) {
2023-08-23 15:53:50 +08:00
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, waterfall_width, waterfall_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, waterfall_data);
}
2023-08-22 23:41:16 +08:00
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2023-08-23 13:12:28 +08:00
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
2023-09-07 19:15:07 +08:00
ImGui::Begin("fastsa", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize);
2023-08-23 13:12:28 +08:00
2023-09-07 19:15:07 +08:00
ImGui::BeginTable("fastsa", 2, ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable);
2023-09-11 20:00:48 +08:00
ImGui::TableSetupColumn("", 0, 280.0f);
ImGui::TableSetupColumn("", 0, 1000.0f);
2023-08-23 13:12:28 +08:00
ImGui::TableNextColumn();
2023-09-07 21:26:26 +08:00
ImGui::Checkbox("Update", &update);
2023-09-11 20:00:48 +08:00
ImGui::Text("Peak: %.3f", (float)freq_peak);
ImGui::SliderFloat("Setpoint", &freq_setpoint_local, 0.0f, 40.0f);
ImGui::SliderFloat("TEC bias", &tec_bias_local, 0.0f, 0.5f);
ImGui::SliderFloat("TEC P", &tec_p_local, -0.05f, 0.05f);
freq_setpoint = freq_setpoint_local;
tec_bias = tec_bias_local;
tec_p = tec_p_local;
2023-08-23 13:12:28 +08:00
if(ImGui::Button("Exit"))
exit = true;
ImGui::Text("FPS: %d", (int)fps);
2023-08-23 13:12:28 +08:00
ImGui::TableNextColumn();
ImGui::Image((void*)(intptr_t)waterfall, ImVec2(waterfall_width, waterfall_height));
ImGui::EndTable();
ImGui::End();
ImGui::PopStyleVar(1);
2023-08-22 23:41:16 +08:00
ImGui::Render();
2023-08-23 13:12:28 +08:00
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
2023-08-22 23:41:16 +08:00
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glfwSwapBuffers(window);
}
2023-08-22 23:24:17 +08:00
return 0;
}