From 115982d279cb1ed7c3312d8841319989422b5e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Izzo?= Date: Sun, 15 May 2022 20:25:57 +0200 Subject: [PATCH] Add demod log script Add script to receive the demodulated data from Module17/radio, to use it you need to add in meson.build:12 the ENABLE_DEMOD_LOG define. Build and flash into the platform, build and run scripts/get_demod_log, turn on the radio, remember to turn of radio before you terminate get_demod_log, print the output with `scripts/plot_m17_demod_csv.py serial_demod_log.csv`. TG-81 --- .gitignore | 3 + openrtx/src/protocols/M17/M17Demodulator.cpp | 12 +- openrtx/src/rtx/OpMode_M17.cpp | 4 + scripts/get_demod_log.c | 111 +++++++++++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 scripts/get_demod_log.c diff --git a/.gitignore b/.gitignore index b64bb4db..4791dce5 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,6 @@ subprojects/tinyusb # ignore bin2sgl !scripts/bin2sgl.Linux.bin !scripts/bin2sgl.exe + +# Compiled script +scripts/get_demod_log diff --git a/openrtx/src/protocols/M17/M17Demodulator.cpp b/openrtx/src/protocols/M17/M17Demodulator.cpp index 2890f307..2711222d 100644 --- a/openrtx/src/protocols/M17/M17Demodulator.cpp +++ b/openrtx/src/protocols/M17/M17Demodulator.cpp @@ -29,9 +29,9 @@ #include #include #include +#ifndef PLATFORM_LINUX #include - -// #define ENABLE_DEMOD_LOG +#endif using namespace M17; @@ -49,7 +49,13 @@ typedef struct } log_entry_t; -static RingBuffer< log_entry_t, 128 > logBuf; +#ifdef PLATFORM_LINUX +#define LOG_QUEUE 160000 +#else +#define LOG_QUEUE 1024 +#endif + +static RingBuffer< log_entry_t, LOG_QUEUE > logBuf; static bool logRunning; static pthread_t logThread; diff --git a/openrtx/src/rtx/OpMode_M17.cpp b/openrtx/src/rtx/OpMode_M17.cpp index e6b6ab07..daef330a 100644 --- a/openrtx/src/rtx/OpMode_M17.cpp +++ b/openrtx/src/rtx/OpMode_M17.cpp @@ -77,8 +77,10 @@ void OpMode_M17::update(rtxStatus_t *const status, const bool newCfg) if(type == M17FrameType::STREAM) { M17StreamFrame sf = decoder.getStreamFrame(); + #ifndef ENABLE_DEMOD_LOG codec_pushFrame(sf.payload().data(), false); codec_pushFrame(sf.payload().data() + 8, false); + #endif } } } @@ -89,7 +91,9 @@ void OpMode_M17::update(rtxStatus_t *const status, const bool newCfg) audio_disableMic(); audio_enableAmp(); codec_stop(); + #ifndef ENABLE_DEMOD_LOG codec_startDecode(SINK_SPK); + #endif decoder.reset(); demodulator.startBasebandSampling(); diff --git a/scripts/get_demod_log.c b/scripts/get_demod_log.c new file mode 100644 index 00000000..5d8499b1 --- /dev/null +++ b/scripts/get_demod_log.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int fd; + +int set_interface_attribs (int fd, int speed, int parity) +{ + struct termios tty; + if (tcgetattr (fd, &tty) != 0) + { + printf("error %d from tcgetattr", errno); + return -1; + } + + cfsetospeed (&tty, speed); + cfsetispeed (&tty, speed); + + tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars + // disable IGNBRK for mismatched speed tests; otherwise receive break + // as \000 chars + tty.c_iflag &= ~IGNBRK; // disable break processing + tty.c_lflag = 0; // no signaling chars, no echo, + // no canonical processing + tty.c_oflag = 0; // no remapping, no delays + tty.c_cc[VMIN] = 0; // read doesn't block + tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout + + tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl + + tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, + // enable reading + tty.c_cflag &= ~(PARENB | PARODD); // shut off parity + tty.c_cflag |= parity; + tty.c_cflag &= ~CSTOPB; + tty.c_cflag &= ~CRTSCTS; + + if (tcsetattr (fd, TCSANOW, &tty) != 0) + { + printf ("error %d from tcsetattr", errno); + return -1; + } + return 0; +} + +void set_blocking (int fd, int should_block) +{ + struct termios tty; + memset (&tty, 0, sizeof tty); + if (tcgetattr (fd, &tty) != 0) + { + printf ("error %d from tggetattr", errno); + return; + } + + tty.c_cc[VMIN] = should_block ? 1 : 0; + tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout + + if (tcsetattr (fd, TCSANOW, &tty) != 0) + printf ("error %d setting term attributes", errno); +} + +typedef struct +{ + int16_t sample; + int32_t conv; + float conv_th; + int32_t sample_index; + float qnt_pos_avg; + float qnt_neg_avg; + int32_t symbol; + int32_t frame_index; +} +log_entry_t; + +int main() { + //char *portname = "/dev/ttyACM0"; + char *portname = "/dev/serial/by-id/usb-STMicroelectronics_STM32_Virtual_ComPort_in_FS_Mode_00000000050C-if00"; + fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); + while (fd < 0) + { + printf ("error %d opening %s: %s", errno, portname, strerror (errno)); + sleep(1); + fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); + } + set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) + set_blocking (fd, 0); // set no blocking + log_entry_t log = { 0 }; + FILE *csv_log = fopen("serial_demod_log.csv", "w"); + fprintf(csv_log, "Sample,Convolution,Threshold,Index,Max,Min,Symbol,I\n"); + while(true) + { + read(fd, &log, sizeof(log)); + fprintf(csv_log, "%" PRId16 ",%d,%f,%d,%f,%f,%d,%d\n", + log.sample, + log.conv, + log.conv_th, + log.sample_index, + log.qnt_pos_avg, + log.qnt_neg_avg, + log.symbol, + log.frame_index); + fflush(csv_log); + } + fclose(csv_log); +}