Skip to content

Commit eb47610

Browse files
committed
add filter, T, and speedup
1 parent d875160 commit eb47610

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

MIDAS/src/hilsim/stream/main.cpp

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <fstream>
55
#include <cstdint>
66
#include <cstring>
7+
#include <cstdlib>
78

89
#include <thread>
910
#include <queue>
@@ -19,7 +20,9 @@
1920
#define STR(x) STR2(x)
2021
#pragma message "__cplusplus=" STR(__cplusplus)
2122

22-
size_t struct_sizes[16];
23+
size_t struct_sizes[READING_DISC_COUNT];
24+
bool ignore_disc[READING_DISC_COUNT];
25+
2326

2427
#define ASSOCIATE(ty, id) struct_sizes[id] = sizeof(ty)
2528
// #define DEBUG
@@ -205,6 +208,8 @@ int main(int argc, char** argv) {
205208
size_t num_read = 0;
206209
uint32_t _inf_checksum;
207210

211+
float skip_threshold = 0;
212+
208213
auto start_time = std::chrono::high_resolution_clock::now();
209214
auto current_time = std::chrono::high_resolution_clock::now();
210215

@@ -228,7 +233,53 @@ int main(int argc, char** argv) {
228233
char _inbuf[255];
229234
memcpy(_inbuf, line.c_str(), line.length() + 2);
230235

236+
for(int i = 0; i < READING_DISC_COUNT; i++) {
237+
ignore_disc[i] = false;
238+
}
239+
240+
// Command list
241+
// S <COMport:str> - Set serial output
242+
// o <Filepath:str> - Set output log file
243+
// d <disc_id:int> - [DEBUG] get size of discrim
244+
// l <Filepath:str> - load file for streaming
245+
// n - [DEBUG] gets next line and prints
246+
// N <num_lines:int> - Skips num_lines from the input
247+
// s - Stream data as fast as possible
248+
// r - Stream data in realtime
249+
250+
// i <disc_id:int> - Ignore this disc_id when streaming
251+
// R <disc-id:int> <intv:float> - Enable this sensor report.
252+
// T <filter_thresh:float> - Entries will be probabilistically skipped if there is high latency. This sets the threshold (in seconds) at which 100% will be skipped.
253+
// If unset / 0, then no entries are skipped
254+
255+
231256
switch(_inbuf[0]) {
257+
case 'i':
258+
{
259+
int discrim_int;
260+
sscanf(_inbuf + 1, " %i", &discrim_int);
261+
262+
if(discrim_int >= 1 && discrim_int < READING_DISC_COUNT) {
263+
ignore_disc[discrim_int] = true;
264+
printf(".IGNORE %i\n", discrim_int);
265+
}
266+
267+
fflush(stdout);
268+
}
269+
270+
break;
271+
case 'T':
272+
{
273+
float n;
274+
sscanf(_inbuf + 1, " %f", &n);
275+
if (n >= 0 && n < 100) {
276+
skip_threshold = n;
277+
}
278+
printf(".SKIP_T %f\n", skip_threshold);
279+
fflush(stdout);
280+
}
281+
break;
282+
232283
case 'S':
233284
// (S)erial <COM>: Set serial port
234285
{
@@ -370,16 +421,27 @@ int main(int argc, char** argv) {
370421

371422
while (true) {
372423
current_time = std::chrono::high_resolution_clock::now();
424+
373425
auto duration = current_time - start_time;
374426
millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
375427

376428
while(millis > (cur_entry_time - first_entry_time)) {
377429
if(read_entry(entry)) {
378430
num_read++;
379431
cur_entry_time = entry.ts;
380-
// printf("[%u] {time: %u / %u}: (d%u) <size: %uB> (CRC 0x%x)\n", num_read, entry.ts, millis, entry.disc, entry._data_size, entry.crc);
381-
// fflush(stdout);
382-
send_data(Serial, entry);
432+
433+
auto latency = millis - (cur_entry_time - first_entry_time);
434+
435+
// check filter
436+
if(!ignore_disc[entry.disc]) {
437+
float randm = static_cast<float>(rand())/static_cast<float>(RAND_MAX);
438+
int threshold_ms = (int)(skip_threshold * 1000);
439+
440+
if(latency < randm*threshold_ms) {
441+
send_data(Serial, entry);
442+
}
443+
}
444+
383445
} else {
384446
break;
385447
}

0 commit comments

Comments
 (0)