Skip to content

Commit

Permalink
controls: Sleep longer in text mode to not flood the bus
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Grazfather committed May 24, 2018
1 parent 84855f7 commit 7280d7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC=gcc
CFLAGS=-I/usr/include/SDL2 -Wall -Werror
CFLAGS=-I/usr/include/SDL2 -Wall -Werror -std=gnu99
LDFLAGS=-lSDL2 -lSDL2_image

all: icsim controls
Expand Down
13 changes: 10 additions & 3 deletions controls.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#define DOOR_UNLOCKED 1
#define MAX_SPEED 150.0 // Limiter 260.0 is full gauge speed
#define ACCEL_RATE 8.0 // 0-MAX_SPEED in seconds
#define MIN_MESSAGE_PERIOD 10 // Time in ms between CAN messages

#if !(DISABLE_SDL)
#define SCREEN_WIDTH 835
Expand Down Expand Up @@ -571,6 +572,7 @@ int main(int argc, char *argv[]) {
int running = 1;
int enable_canfd = 1;
int enable_background_traffic = 1;
int last_ms = 0;
struct stat st;
struct ui_t ui = {0};

Expand Down Expand Up @@ -959,13 +961,18 @@ int main(int argc, char *argv[]) {
}
}
#endif // !DISABLE_SDL
clock_gettime(CLOCK_MONOTONIC, &currentTime);
current_ms = currentTime.tv_sec * 1000 + currentTime.tv_nsec / 1000000;
check_accel();
check_turn();
check_locks();
ui.redraw();
usleep(5000);

// Limit max messages per second
while (current_ms - last_ms < MIN_MESSAGE_PERIOD) {
usleep(1000);
clock_gettime(CLOCK_MONOTONIC, &currentTime);
current_ms = currentTime.tv_sec * 1000 + currentTime.tv_nsec / 1000000;
}
last_ms = current_ms;

} // while(running)

Expand Down

0 comments on commit 7280d7f

Please sign in to comment.