Skip to content

Commit 7280d7f

Browse files
committed
controls: Sleep longer in text mode to not flood the bus
Rate limit the main loop so that each can message cannot be sent more than a certain number of times per second (right now it's 1000ms/10ms period = 100Hz).
1 parent 84855f7 commit 7280d7f

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CC=gcc
2-
CFLAGS=-I/usr/include/SDL2 -Wall -Werror
2+
CFLAGS=-I/usr/include/SDL2 -Wall -Werror -std=gnu99
33
LDFLAGS=-lSDL2 -lSDL2_image
44

55
all: icsim controls

controls.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#define DOOR_UNLOCKED 1
5656
#define MAX_SPEED 150.0 // Limiter 260.0 is full gauge speed
5757
#define ACCEL_RATE 8.0 // 0-MAX_SPEED in seconds
58+
#define MIN_MESSAGE_PERIOD 10 // Time in ms between CAN messages
5859

5960
#if !(DISABLE_SDL)
6061
#define SCREEN_WIDTH 835
@@ -571,6 +572,7 @@ int main(int argc, char *argv[]) {
571572
int running = 1;
572573
int enable_canfd = 1;
573574
int enable_background_traffic = 1;
575+
int last_ms = 0;
574576
struct stat st;
575577
struct ui_t ui = {0};
576578

@@ -959,13 +961,18 @@ int main(int argc, char *argv[]) {
959961
}
960962
}
961963
#endif // !DISABLE_SDL
962-
clock_gettime(CLOCK_MONOTONIC, &currentTime);
963-
current_ms = currentTime.tv_sec * 1000 + currentTime.tv_nsec / 1000000;
964964
check_accel();
965965
check_turn();
966966
check_locks();
967967
ui.redraw();
968-
usleep(5000);
968+
969+
// Limit max messages per second
970+
while (current_ms - last_ms < MIN_MESSAGE_PERIOD) {
971+
usleep(1000);
972+
clock_gettime(CLOCK_MONOTONIC, &currentTime);
973+
current_ms = currentTime.tv_sec * 1000 + currentTime.tv_nsec / 1000000;
974+
}
975+
last_ms = current_ms;
969976

970977
} // while(running)
971978

0 commit comments

Comments
 (0)