From 2a627ba157e3d2af50d3c3bff1e484121cf1f673 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 28 Oct 2024 17:25:22 -0400 Subject: [PATCH] added rate limited can msg --- middleware/include/can_utility.h | 10 ++++++++++ middleware/src/can_utility.c | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 middleware/include/can_utility.h create mode 100644 middleware/src/can_utility.c diff --git a/middleware/include/can_utility.h b/middleware/include/can_utility.h new file mode 100644 index 0000000..45940bc --- /dev/null +++ b/middleware/include/can_utility.h @@ -0,0 +1,10 @@ +#include "can_handler.h" +#include "FreeRTOS.h" + +typedef struct { + can_msg_t can_msg; + osThreadId_t msg_timer; + uint8_t msg_rate; /* in messages per second */ +} rl_can_msg_t; + +HAL_StatusTypeDef send_rl_can_msg(can_t *can, rl_can_msg_t *rl_can_msg); \ No newline at end of file diff --git a/middleware/src/can_utility.c b/middleware/src/can_utility.c new file mode 100644 index 0000000..70f6d8b --- /dev/null +++ b/middleware/src/can_utility.c @@ -0,0 +1,18 @@ + +#include "can_utility.h" + +/** + * Sends a rate limited can message + */ +HAL_StatusTypeDef send_rl_can_msg(can_t *can, rl_can_msg_t *rl_can_msg) +{ + if (osTimerIsRunning(rl_can_msg->msg_timer)) { + return HAL_BUSY; + } + + can_send_msg(can, &rl_can_msg->can_msg); + osTimerStart(rl_can_msg->msg_timer, + pdMS_TO_TICKS(rl_can_msg->msg_rate * 1000)); + + return HAL_OK; +} \ No newline at end of file