fastsa/main.cpp

383 lines
13 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>
2023-09-13 14:31:19 +08:00
#include <semaphore>
2023-09-11 19:20:51 +08:00
#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:51 +08:00
namespace net = std::experimental::net;
2023-09-13 14:31:19 +08:00
using namespace std::literals::chrono_literals;
2023-08-23 19:57:01 +08:00
2023-09-13 10:40:07 +08:00
void fft_mag(std::complex<float>* in, float* out, size_t len);
2023-09-13 16:03:40 +08:00
static const size_t frame_len = 16384;
2023-09-13 10:45:02 +08:00
static const int wf_width = 1000;
2023-09-13 14:31:19 +08:00
static const int wf_height = 1200;
2023-09-13 10:45:02 +08:00
2023-09-13 14:31:19 +08:00
static std::atomic<bool> shutdown_threads;
2023-09-13 10:45:02 +08:00
2023-09-13 14:31:19 +08:00
static std::atomic<float> tec_current;
2023-08-23 18:44:23 +08:00
2023-09-13 14:31:19 +08:00
static void tec_thread()
{
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.27"), 23));
while(!shutdown_threads) {
// FIXME: net::write seems unimplemented as of libstdc++ 13
tec_socket.write_some(net::buffer(std::format("pwm 0 i_set {:.6f}\n", (float)tec_current)));
std::string reply;
net::read(tec_socket, net::dynamic_buffer(reply),
[&reply](auto ec, auto n) -> std::size_t
{
if(ec || (reply.size() > 0 && reply.compare(reply.size()-1, 1, "\n") == 0))
return 0;
else
return 1;
});
2023-09-13 15:50:46 +08:00
std::this_thread::sleep_for(15ms);
2023-09-13 14:31:19 +08:00
}
}
2023-08-23 18:44:23 +08:00
2023-09-13 14:31:19 +08:00
static std::atomic<bool> poh_accept_input;
static std::binary_semaphore poh_input_ready{0};
2023-09-13 16:03:40 +08:00
static std::complex<float> poh_input_frame[frame_len];
2023-09-13 14:31:19 +08:00
static std::mutex wf_poh_mutex;
static unsigned int wf_poh[wf_width*wf_height];
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;
2023-09-13 14:31:19 +08:00
static void poh_thread()
2023-09-13 10:45:02 +08:00
{
2023-09-13 16:03:40 +08:00
std::vector<float> frame_mag(frame_len);
2023-09-11 19:20:51 +08:00
2023-09-13 16:03:40 +08:00
poh_accept_input = true; // accept first frame
2023-09-13 14:31:19 +08:00
while(!shutdown_threads) {
poh_input_ready.acquire();
2023-09-11 19:20:51 +08:00
2023-09-13 16:03:40 +08:00
fft_mag(poh_input_frame, frame_mag.data(), frame_len);
poh_accept_input = true; // poh_input_frame not used again until next iteration
2023-09-11 19:20:51 +08:00
// stabilize laser
2023-09-13 14:31:19 +08:00
float freq_peak_local;
2023-09-13 16:03:40 +08:00
freq_peak_local = 40.0f*float(distance(frame_mag.begin(), max_element(frame_mag.begin(), frame_mag.end())))/float(frame_len);
2023-09-13 14:31:19 +08:00
freq_peak = freq_peak_local;
float freq_error = freq_peak_local - freq_setpoint;
tec_current = std::max(tec_bias+tec_p*freq_error, 0.0f);
2023-09-11 19:20:51 +08:00
// update waterfall
2023-08-23 15:53:50 +08:00
{
2023-09-13 14:31:19 +08:00
std::lock_guard<std::mutex> guard(wf_poh_mutex);
std::memmove(&wf_poh[wf_width], &wf_poh[0], sizeof(int)*wf_width*(wf_height - 1));
2023-09-13 10:45:02 +08:00
for(int i=0;i<wf_width;i++) {
2023-09-13 16:03:40 +08:00
int j = i*frame_len/wf_width;
wf_poh[i] = 0xff000000 | 0x010101*std::min(int(frame_mag[j]/900.0f), 255);
2023-08-23 18:44:23 +08:00
}
2023-09-13 14:31:19 +08:00
wf_poh[int(freq_setpoint*wf_width)/40] = 0xff0000ff;
2023-08-23 15:53:50 +08:00
}
2023-09-13 14:31:19 +08:00
}
}
2023-09-13 14:40:18 +08:00
static std::atomic<bool> ls_accept_input;
static std::binary_semaphore ls_input_ready{0};
2023-09-13 16:03:40 +08:00
static std::complex<float> ls_input_frame[frame_len];
2023-09-13 14:40:18 +08:00
static std::mutex wf_ls_mutex;
static unsigned int wf_ls[wf_width*wf_height];
static void ls_thread()
{
2023-09-13 16:03:40 +08:00
std::vector<float> frame_mag(frame_len);
2023-09-13 14:40:18 +08:00
2023-09-13 16:01:46 +08:00
ls_accept_input = true;
2023-09-13 14:40:18 +08:00
while(!shutdown_threads) {
ls_input_ready.acquire();
2023-09-13 16:03:40 +08:00
fft_mag(ls_input_frame, frame_mag.data(), frame_len);
2023-09-13 16:01:46 +08:00
ls_accept_input = true;
2023-09-13 14:40:18 +08:00
{
std::lock_guard<std::mutex> guard(wf_ls_mutex);
std::memmove(&wf_ls[wf_width], &wf_ls[0], sizeof(int)*wf_width*(wf_height - 1));
for(int i=0;i<wf_width;i++) {
2023-09-13 16:03:40 +08:00
int j = i*frame_len/wf_width;
wf_ls[i] = 0xff000000 | 0x010101*std::min(int(frame_mag[j]/900.0f), 255);
2023-09-13 14:40:18 +08:00
}
}
}
}
2023-09-13 14:31:19 +08:00
static std::atomic<int> frames_processed;
static std::atomic<int> frames_dropped;
static auto brf_last_second = std::chrono::time_point<std::chrono::steady_clock>::min();
static int brf_frames_processed_cnt = 0;
static int brf_frames_dropped_cnt = 0;
2023-09-11 19:20:51 +08:00
2023-09-13 14:31:19 +08:00
static void* brf_dispatch(struct bladerf* dev, struct bladerf_stream* stream,
struct bladerf_metadata* meta, void* samples_v, size_t num_samples, void* user_data)
{
2023-09-13 14:40:18 +08:00
if(poh_accept_input && ls_accept_input) {
2023-09-13 14:31:19 +08:00
std::complex<int16_t>* samples = (std::complex<int16_t>*)samples_v;
2023-09-13 16:03:40 +08:00
for(size_t i=0;i<frame_len;i++) {
poh_input_frame[i] = samples[2*i];
ls_input_frame[i] = samples[2*i+1];
}
2023-09-13 14:31:19 +08:00
poh_accept_input = false;
poh_input_ready.release();
2023-09-13 14:40:18 +08:00
ls_accept_input = false;
ls_input_ready.release();
2023-09-13 14:31:19 +08:00
brf_frames_processed_cnt++;
} else
brf_frames_dropped_cnt++;
if((std::chrono::steady_clock::now() - brf_last_second) >= 1s) {
frames_processed = brf_frames_processed_cnt;
frames_dropped = brf_frames_dropped_cnt;
brf_frames_processed_cnt = 0;
brf_frames_dropped_cnt = 0;
brf_last_second += 1s;
2023-09-11 19:20:51 +08:00
}
2023-09-13 14:31:19 +08:00
if(shutdown_threads)
return BLADERF_STREAM_SHUTDOWN;
else
return samples_v;
}
static void brf_thread()
{
struct bladerf_devinfo brf_dev_info;
struct bladerf* brf_dev;
struct bladerf_stream* brf_stream;
void** brf_buffers;
int status;
bladerf_init_devinfo(&brf_dev_info);
if((status = bladerf_open_with_devinfo(&brf_dev, &brf_dev_info)) != 0) {
std::cerr << "cannot open bladeRF device: " << bladerf_strerror(status) << std::endl;
return;
}
for(int i=0;i<2;i++) {
if((status = bladerf_set_frequency(brf_dev, BLADERF_CHANNEL_RX(i), 81'000'000)) != 0) {
2023-09-13 14:31:19 +08:00
std::cerr << "failed to set bladeRF frequency: " << bladerf_strerror(status) << std::endl;
bladerf_close(brf_dev);
return;
}
if((status = bladerf_set_sample_rate(brf_dev, BLADERF_CHANNEL_RX(i), 40'000'000, NULL)) != 0) {
std::cerr << "failed to set bladeRF sample rate: " << bladerf_strerror(status) << std::endl;
bladerf_close(brf_dev);
return;
}
if((status = bladerf_set_bandwidth(brf_dev, BLADERF_CHANNEL_RX(i), 35'000'000, NULL)) != 0) {
std::cerr << "failed to set bladeRF bandwidth: " << bladerf_strerror(status) << std::endl;
bladerf_close(brf_dev);
return;
}
if((status = bladerf_set_gain(brf_dev, BLADERF_CHANNEL_RX(i), 20)) != 0) {
std::cerr << "failed to set bladeRF gain: " << bladerf_strerror(status) << std::endl;
bladerf_close(brf_dev);
return;
}
}
if((status = bladerf_init_stream(&brf_stream, brf_dev, brf_dispatch, &brf_buffers,
2023-09-13 16:03:40 +08:00
16, BLADERF_FORMAT_SC16_Q11, 2*frame_len, 8, NULL)) != 0) {
2023-09-13 14:31:19 +08:00
std::cerr << "failed to init bladeRF stream: " << bladerf_strerror(status) << std::endl;
return;
}
if((status = bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(0), true) != 0)) {
std::cerr << "failed to enable bladeRF RX: " << bladerf_strerror(status) << std::endl;
bladerf_deinit_stream(brf_stream);
bladerf_close(brf_dev);
return;
}
if((status = bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(1), true) != 0)) {
std::cerr << "failed to enable bladeRF RX: " << bladerf_strerror(status) << std::endl;
bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(0), false);
bladerf_deinit_stream(brf_stream);
bladerf_close(brf_dev);
return;
}
brf_last_second = std::chrono::steady_clock::now();
if((status = bladerf_stream(brf_stream, BLADERF_RX_X2)) != 0) {
std::cerr << "bladeRF stream failed: " << bladerf_strerror(status) << std::endl;
bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(1), false);
bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(0), false);
bladerf_deinit_stream(brf_stream);
bladerf_close(brf_dev);
return;
};
bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(1), false);
bladerf_enable_module(brf_dev, BLADERF_CHANNEL_RX(0), false);
bladerf_deinit_stream(brf_stream);
bladerf_close(brf_dev);
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-13 15:57:50 +08:00
static GLFWwindow* window = glfwCreateWindow(2600, 1350, "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-09-13 11:14:14 +08:00
GLuint wftex[2];
for(size_t i=0;i<2;i++) {
glGenTextures(1, &wftex[i]);
glBindTexture(GL_TEXTURE_2D, wftex[i]);
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-08-23 13:12:28 +08:00
2023-09-13 14:31:19 +08:00
static std::thread brf_thread_h = std::thread(brf_thread);
static auto JoinBRF = []() {
brf_thread_h.join();
2023-08-23 18:44:23 +08:00
};
2023-09-13 14:31:19 +08:00
std::atexit(JoinBRF);
static std::thread poh_thread_h = std::thread(poh_thread);
static auto JoinPOH = []() {
poh_thread_h.join();
};
std::atexit(JoinPOH);
2023-09-13 14:40:18 +08:00
static std::thread ls_thread_h = std::thread(ls_thread);
static auto JoinLS = []() {
ls_thread_h.join();
};
std::atexit(JoinLS);
2023-09-13 14:31:19 +08:00
static std::thread tec_thread_h = std::thread(tec_thread);
static auto JoinTEC = []() {
tec_thread_h.join();
2023-09-07 20:17:02 +08:00
};
2023-09-13 14:31:19 +08:00
std::atexit(JoinTEC);
2023-08-23 18:44:23 +08:00
2023-09-13 14:31:19 +08:00
shutdown_threads = false;
static auto SetShutdown = []() {
shutdown_threads = true;
2023-08-23 15:53:50 +08:00
};
2023-09-13 14:31:19 +08:00
std::atexit(SetShutdown);
2023-08-23 15:53:50 +08:00
2023-08-23 13:12:28 +08:00
bool exit = false;
2023-09-13 11:14:14 +08:00
bool update_wf = true;
2023-09-13 15:50:46 +08:00
float freq_setpoint_local = 25.8f;
float tec_bias_local = 0.105f;
float tec_p_local = -0.022f;
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);
2023-09-13 11:14:14 +08:00
if(update_wf) {
#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glBindTexture(GL_TEXTURE_2D, wftex[0]);
{
2023-09-13 14:31:19 +08:00
std::lock_guard<std::mutex> guard(wf_poh_mutex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wf_width, wf_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, wf_poh);
2023-09-13 11:14:14 +08:00
}
glBindTexture(GL_TEXTURE_2D, wftex[1]);
{
2023-09-13 14:40:18 +08:00
std::lock_guard<std::mutex> guard(wf_ls_mutex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wf_width, wf_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, wf_ls);
2023-09-13 11:14:14 +08:00
}
2023-08-23 15:53:50 +08:00
}
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-13 11:14:14 +08:00
ImGui::BeginTable("fastsa", 3, ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable);
2023-09-13 15:57:50 +08:00
ImGui::TableSetupColumn("SBS laser control", 0, 380.0f);
ImGui::TableSetupColumn("Pump/output heterodyne", 0, 1200.0f);
ImGui::TableSetupColumn("Lineshape", 0, 1200.0f);
2023-09-13 11:14:14 +08:00
ImGui::TableHeadersRow();
2023-08-23 13:12:28 +08:00
ImGui::TableNextColumn();
2023-09-13 11:14:14 +08:00
ImGui::Checkbox("Update waterfall plots", &update_wf);
ImGui::Text("Baseband peak: %.3f MHz", (float)freq_peak);
2023-09-13 15:57:50 +08:00
ImGui::Text("TEC current: %.3f mA", (float)tec_current*1000.0f);
2023-09-11 20:00:48 +08:00
ImGui::SliderFloat("Setpoint", &freq_setpoint_local, 0.0f, 40.0f);
ImGui::SliderFloat("TEC bias", &tec_bias_local, 0.0f, 0.5f);
2023-09-13 16:03:54 +08:00
ImGui::SliderFloat("TEC P", &tec_p_local, -1.0f, 0.0f);
2023-09-11 20:00:48 +08:00
freq_setpoint = freq_setpoint_local;
tec_bias = tec_bias_local;
2023-09-13 15:50:46 +08:00
tec_p = 0.01f*tec_p_local;
2023-08-23 13:12:28 +08:00
if(ImGui::Button("Exit"))
exit = true;
2023-09-13 14:31:19 +08:00
ImGui::Text("Frames processed: %d", (int)frames_processed);
ImGui::Text("Frames dropped: %d", (int)frames_dropped);
2023-08-23 13:12:28 +08:00
ImGui::TableNextColumn();
2023-09-13 11:14:14 +08:00
ImGui::Image((void*)(intptr_t)wftex[0], ImVec2(wf_width, wf_height));
ImGui::TableNextColumn();
ImGui::Image((void*)(intptr_t)wftex[1], ImVec2(wf_width, wf_height));
2023-08-23 13:12:28 +08:00
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;
}