From 511bf85bf3dcc3792f95abcedc472c115f097466 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 1 Nov 2024 10:51:32 -0400 Subject: [PATCH 01/14] rate limited functions --- Core/Inc/can_handler.h | 14 +++++++++++++- Core/Src/can_handler.c | 16 ++++++++++++++++ Core/Src/shep_tasks.c | 3 +++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 95a382b..4c63bda 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -5,7 +5,8 @@ #include "stm32f4xx_hal.h" #include #include "ringbuffer.h" -#include "cmsis_os2.h" +#include "FreeRTOS.h" +#include "datastructs.h" #define NUM_INBOUND_CAN1_IDS 1 #define NUM_INBOUND_CAN2_IDS 1 @@ -19,6 +20,13 @@ extern ringbuffer_t *can2_rx_queue; #define CAN_MSG_QUEUE_SIZE 50 /* messages */ extern osMessageQueueId_t can_outbound_queue; +typedef struct { + osThreadId_t msg_timer; + uint8_t msg_rate; /* in messages per second */ +} rl_msg_t; + +typedef void (*rl_func_t)(acc_data_t *); + static const uint32_t can1_id_list[NUM_INBOUND_CAN1_IDS] = { //CANID_X, 0x0000 @@ -45,4 +53,8 @@ int8_t get_can2_msg(); */ osStatus_t queue_can_msg(can_msg_t msg); +/* initialize rate limited can message */ +HAL_StatusTypeDef rl_msg_init(rl_msg_t *rl_msg, uint8_t msg_rate); +void rl_bms_func(rl_msg_t *rl_msg, acc_data_t *acc_data, rl_func_t rl_func); + #endif // CAN_HANDLER_H \ No newline at end of file diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 7ab9f1b..b5a1b31 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -84,3 +84,19 @@ osStatus_t queue_can_msg(can_msg_t msg) return res; } + +HAL_StatusTypeDef rl_msg_init(rl_msg_t *rl_msg, uint8_t msg_rate) +{ + rl_msg->msg_timer = osTimerNew(NULL, osTimerOnce, NULL, NULL); + rl_msg->msg_rate = msg_rate; +} + +void rl_bms_func(rl_msg_t *rl_msg, acc_data_t *acc_data, rl_func_t rl_func) +{ + if (osTimerIsRunning(rl_msg->msg_timer)) { + return; + } + + rl_func(acc_data); + osTimerStart(rl_msg->msg_timer, pdMS_TO_TICKS(rl_msg->msg_rate * 1000)); +} diff --git a/Core/Src/shep_tasks.c b/Core/Src/shep_tasks.c index e1a0947..8208614 100644 --- a/Core/Src/shep_tasks.c +++ b/Core/Src/shep_tasks.c @@ -42,6 +42,9 @@ const osThreadAttr_t analyzer_attrs = { .name = "Analyzer", void vAnalyzer(void *pv_params) { acc_data_t *bmsdata = (acc_data_t *)pv_params; + rl_msg_t *rl_charge_msg; + rl_msg_init(rl_charge_msg, 3); + for (;;) { osThreadFlagsWait(ANALYZER_FLAG, osFlagsWaitAny, osWaitForever); From afa3709073bc371c4f37314cfcccc98de7ba53f1 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 1 Nov 2024 12:39:24 -0400 Subject: [PATCH 02/14] structure of rate limited can messages --- Core/Inc/can_handler.h | 12 ++++++++---- Core/Src/can_handler.c | 34 +++++++++++++++++++++++++--------- Core/Src/compute.c | 4 ++-- Core/Src/shep_tasks.c | 6 ++++-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 4c63bda..50112f0 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -11,6 +11,9 @@ #define NUM_INBOUND_CAN1_IDS 1 #define NUM_INBOUND_CAN2_IDS 1 +#define CHARGE_CANID 0x176 +#define DISCHARGE_CANID 0x156 + extern CAN_HandleTypeDef hcan1; extern CAN_HandleTypeDef hcan2; @@ -23,9 +26,9 @@ extern osMessageQueueId_t can_outbound_queue; typedef struct { osThreadId_t msg_timer; uint8_t msg_rate; /* in messages per second */ -} rl_msg_t; +} rl_can_msg_t; -typedef void (*rl_func_t)(acc_data_t *); +typedef enum { CHARGE, DISCHARGE, RL_MSG_COUNT } rate_lim_t; static const uint32_t can1_id_list[NUM_INBOUND_CAN1_IDS] = { //CANID_X, @@ -54,7 +57,8 @@ int8_t get_can2_msg(); osStatus_t queue_can_msg(can_msg_t msg); /* initialize rate limited can message */ -HAL_StatusTypeDef rl_msg_init(rl_msg_t *rl_msg, uint8_t msg_rate); -void rl_bms_func(rl_msg_t *rl_msg, acc_data_t *acc_data, rl_func_t rl_func); +void rl_can_msg_init(rate_lim_t rl, uint8_t msg_rate); + +rl_can_msg_t *get_rl_msg(uint32_t can_id); #endif // CAN_HANDLER_H \ No newline at end of file diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index b5a1b31..5dbfa6a 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -7,6 +7,8 @@ ringbuffer_t *can1_rx_queue = NULL; ringbuffer_t *can2_rx_queue = NULL; +rl_can_msg_t rl_msgs[RL_MSG_COUNT]; + void can_receive_callback(CAN_HandleTypeDef *hcan) { CAN_RxHeaderTypeDef rx_header; @@ -74,6 +76,14 @@ osStatus_t queue_can_msg(can_msg_t msg) if (!can_outbound_queue) return -1; + rl_can_msg_t *rl_can_msg = get_rl_msg(msg.id); + if (rl_can_msg != NULL && osTimerIsRunning(rl_can_msg->msg_timer)) { + return -1; + } else { + osTimerStart(rl_can_msg->msg_timer, + pdMS_TO_TICKS(rl_can_msg->msg_rate * 1000)); + } + osStatus_t res = osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U); if (res) { @@ -85,18 +95,24 @@ osStatus_t queue_can_msg(can_msg_t msg) return res; } -HAL_StatusTypeDef rl_msg_init(rl_msg_t *rl_msg, uint8_t msg_rate) +void rl_can_msg_init(rate_lim_t rl, uint8_t msg_rate) { - rl_msg->msg_timer = osTimerNew(NULL, osTimerOnce, NULL, NULL); - rl_msg->msg_rate = msg_rate; + rl_can_msg_t rl_msg = { .msg_timer = osTimerNew(NULL, osTimerOnce, NULL, + NULL), + .msg_rate = msg_rate }; + rl_msgs[rl] = rl_msg; } -void rl_bms_func(rl_msg_t *rl_msg, acc_data_t *acc_data, rl_func_t rl_func) +rl_can_msg_t *get_rl_msg(uint32_t can_id) { - if (osTimerIsRunning(rl_msg->msg_timer)) { - return; + switch (can_id) { + case CHARGE_CANID: + return &rl_msgs[CHARGE]; + break; + case DISCHARGE_CANID: + return &rl_msgs[DISCHARGE]; + break; + default: + return NULL; } - - rl_func(acc_data); - osTimerStart(rl_msg->msg_timer, pdMS_TO_TICKS(rl_msg->msg_rate * 1000)); } diff --git a/Core/Src/compute.c b/Core/Src/compute.c index 75fa652..4576341 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -293,7 +293,7 @@ void compute_send_mc_discharge_message(acc_data_t *bmsdata) can_msg_t mc_msg = { 0 }; mc_msg.id = - 0x156; // 0x0A is the dcl id, 0x22 is the device id set by us + DISCHARGE_CANID; // 0x0A is the dcl id, 0x22 is the device id set by us mc_msg.len = 8; memcpy(mc_msg.data, &discharge_data, sizeof(discharge_data)); @@ -314,7 +314,7 @@ void compute_send_mc_charge_message(acc_data_t *bmsdata) can_msg_t mc_msg = { 0 }; mc_msg.id = - 0x176; // 0x0A is the dcl id, 0x157 is the device id set by us + CHARGE_CANID; // 0x0A is the dcl id, 0x157 is the device id set by us mc_msg.len = 8; memcpy(mc_msg.data, &charge_data, sizeof(charge_data)); diff --git a/Core/Src/shep_tasks.c b/Core/Src/shep_tasks.c index 8208614..48af8c9 100644 --- a/Core/Src/shep_tasks.c +++ b/Core/Src/shep_tasks.c @@ -20,6 +20,8 @@ #define ANALYZER_FLAG 1 +extern rl_can_msg_t rl_msgs[RL_MSG_COUNT]; /* defined in can_handler.c */ + osThreadId_t get_segment_data_thread; const osThreadAttr_t get_segment_data_attrs = { .name = "Get Segment Data", .stack_size = 2048, @@ -42,8 +44,8 @@ const osThreadAttr_t analyzer_attrs = { .name = "Analyzer", void vAnalyzer(void *pv_params) { acc_data_t *bmsdata = (acc_data_t *)pv_params; - rl_msg_t *rl_charge_msg; - rl_msg_init(rl_charge_msg, 3); + rl_can_msg_init(DISCHARGE, 3); + rl_can_msg_init(CHARGE, 2); for (;;) { osThreadFlagsWait(ANALYZER_FLAG, osFlagsWaitAny, osWaitForever); From a53a951b61b9632bcd5ac8773557e6582ee4887a Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 1 Nov 2024 12:44:54 -0400 Subject: [PATCH 03/14] added docs --- Core/Inc/can_handler.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 50112f0..556292c 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -59,6 +59,7 @@ osStatus_t queue_can_msg(can_msg_t msg); /* initialize rate limited can message */ void rl_can_msg_init(rate_lim_t rl, uint8_t msg_rate); +/* gets the rate limit can message from the list of rl messages */ rl_can_msg_t *get_rl_msg(uint32_t can_id); #endif // CAN_HANDLER_H \ No newline at end of file From f7fed0e2560779575798cc589768cc0cad0a6d1a Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 3 Nov 2024 10:55:42 -0500 Subject: [PATCH 04/14] used two lists to store can msgs and rl data --- Core/Inc/can_handler.h | 19 +++++++++++-------- Core/Inc/compute.h | 6 ++++++ Core/Src/can_handler.c | 25 +++++++------------------ Core/Src/compute.c | 42 ++++++++++++++++++++++++++++++------------ Core/Src/shep_tasks.c | 4 ---- 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 556292c..3b9289e 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -11,6 +11,7 @@ #define NUM_INBOUND_CAN1_IDS 1 #define NUM_INBOUND_CAN2_IDS 1 +/*TODO: define ids of all rate limited can messages */ #define CHARGE_CANID 0x176 #define DISCHARGE_CANID 0x156 @@ -24,12 +25,15 @@ extern ringbuffer_t *can2_rx_queue; extern osMessageQueueId_t can_outbound_queue; typedef struct { - osThreadId_t msg_timer; - uint8_t msg_rate; /* in messages per second */ -} rl_can_msg_t; + nertimer_t timer; + uint8_t msg_rate; /* in milliseconds */ +} rl_data_t; typedef enum { CHARGE, DISCHARGE, RL_MSG_COUNT } rate_lim_t; +can_msg_t can_msgs[RL_MSG_COUNT]; +rl_data_t rl_data[RL_MSG_COUNT]; + static const uint32_t can1_id_list[NUM_INBOUND_CAN1_IDS] = { //CANID_X, 0x0000 @@ -56,10 +60,9 @@ int8_t get_can2_msg(); */ osStatus_t queue_can_msg(can_msg_t msg); -/* initialize rate limited can message */ -void rl_can_msg_init(rate_lim_t rl, uint8_t msg_rate); - -/* gets the rate limit can message from the list of rl messages */ -rl_can_msg_t *get_rl_msg(uint32_t can_id); +/** + * returns the rate limit data based on the specific can id + */ +rl_data_t *get_rl_msg(uint32_t can_id); #endif // CAN_HANDLER_H \ No newline at end of file diff --git a/Core/Inc/compute.h b/Core/Inc/compute.h index 5b73fde..632303b 100644 --- a/Core/Inc/compute.h +++ b/Core/Inc/compute.h @@ -5,6 +5,7 @@ #include "stateMachine.h" #include "ringbuffer.h" #include "can.h" +#include "can_handler.h" #define CURRENT_SENSOR_PIN_L A1 #define CURRENT_SENSOR_PIN_H A0 @@ -26,6 +27,11 @@ extern can_t can2; // p2p can bus with charger */ uint8_t compute_init(); +/** + * @brief inits all rate limited can messages + */ +void init_rl_config(); + /** * @brief sets safeguard bool to check whether charging is enabled or disabled * diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 5dbfa6a..7f2db38 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -7,8 +7,6 @@ ringbuffer_t *can1_rx_queue = NULL; ringbuffer_t *can2_rx_queue = NULL; -rl_can_msg_t rl_msgs[RL_MSG_COUNT]; - void can_receive_callback(CAN_HandleTypeDef *hcan) { CAN_RxHeaderTypeDef rx_header; @@ -76,12 +74,11 @@ osStatus_t queue_can_msg(can_msg_t msg) if (!can_outbound_queue) return -1; - rl_can_msg_t *rl_can_msg = get_rl_msg(msg.id); - if (rl_can_msg != NULL && osTimerIsRunning(rl_can_msg->msg_timer)) { - return -1; + rl_data_t *rl_data = get_rl_msg(msg.id); + if (rl_data != NULL && is_timer_active(&rl_data->timer)) { + return 0; } else { - osTimerStart(rl_can_msg->msg_timer, - pdMS_TO_TICKS(rl_can_msg->msg_rate * 1000)); + start_timer(&rl_data->timer, rl_data->msg_rate); } osStatus_t res = osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U); @@ -95,22 +92,14 @@ osStatus_t queue_can_msg(can_msg_t msg) return res; } -void rl_can_msg_init(rate_lim_t rl, uint8_t msg_rate) -{ - rl_can_msg_t rl_msg = { .msg_timer = osTimerNew(NULL, osTimerOnce, NULL, - NULL), - .msg_rate = msg_rate }; - rl_msgs[rl] = rl_msg; -} - -rl_can_msg_t *get_rl_msg(uint32_t can_id) +rl_data_t *get_rl_msg(uint32_t can_id) { switch (can_id) { case CHARGE_CANID: - return &rl_msgs[CHARGE]; + return &rl_data[CHARGE]; break; case DISCHARGE_CANID: - return &rl_msgs[DISCHARGE]; + return &rl_data[DISCHARGE]; break; default: return NULL; diff --git a/Core/Src/compute.c b/Core/Src/compute.c index 4576341..bde6480 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -92,9 +92,34 @@ uint8_t compute_init(acc_data_t *bmsdata) HAL_ADC_Start(&hadc2); + /* Initializing rate limited messages */ + init_rl_config(); + return 0; } +void init_rl_config() +{ + can_msg_t discharge_msg = { 0 }; + discharge_msg.id = + DISCHARGE_CANID; // 0x0A is the dcl id, 0x22 is the device id set by us + discharge_msg.len = 8; + + can_msg_t charge_msg = { 0 }; + charge_msg.id = + CHARGE_CANID; // 0x0A is the dcl id, 0x157 is the device id set by us + charge_msg.len = 8; + + rl_data_t rl_discharge_data = { .msg_rate = 100 }; + rl_data_t rl_charge_data = { .msg_rate = 200 }; + + can_msgs[DISCHARGE] = discharge_msg; + can_msgs[CHARGE] = charge_msg; + + rl_data[DISCHARGE] = rl_discharge_data; + rl_data[CHARGE] = rl_charge_data; +} + void compute_enable_charging(bool enable_charging) { is_charging_enabled = enable_charging; @@ -291,13 +316,10 @@ void compute_send_mc_discharge_message(acc_data_t *bmsdata) endian_swap(&discharge_data.max_discharge, sizeof(discharge_data.max_discharge)); - can_msg_t mc_msg = { 0 }; - mc_msg.id = - DISCHARGE_CANID; // 0x0A is the dcl id, 0x22 is the device id set by us - mc_msg.len = 8; - memcpy(mc_msg.data, &discharge_data, sizeof(discharge_data)); + memcpy(can_msgs[DISCHARGE].data, &discharge_data, + sizeof(discharge_data)); - queue_can_msg(mc_msg); + queue_can_msg(can_msgs[DISCHARGE]); } void compute_send_mc_charge_message(acc_data_t *bmsdata) @@ -312,13 +334,9 @@ void compute_send_mc_charge_message(acc_data_t *bmsdata) /* convert to big endian */ endian_swap(&charge_data.max_charge, sizeof(charge_data.max_charge)); - can_msg_t mc_msg = { 0 }; - mc_msg.id = - CHARGE_CANID; // 0x0A is the dcl id, 0x157 is the device id set by us - mc_msg.len = 8; - memcpy(mc_msg.data, &charge_data, sizeof(charge_data)); + memcpy(can_msgs[CHARGE].data, &charge_data, sizeof(charge_data)); - queue_can_msg(mc_msg); + queue_can_msg(can_msgs[CHARGE]); } void compute_send_acc_status_message(acc_data_t *bmsdata) diff --git a/Core/Src/shep_tasks.c b/Core/Src/shep_tasks.c index 48af8c9..3bb3c8e 100644 --- a/Core/Src/shep_tasks.c +++ b/Core/Src/shep_tasks.c @@ -20,8 +20,6 @@ #define ANALYZER_FLAG 1 -extern rl_can_msg_t rl_msgs[RL_MSG_COUNT]; /* defined in can_handler.c */ - osThreadId_t get_segment_data_thread; const osThreadAttr_t get_segment_data_attrs = { .name = "Get Segment Data", .stack_size = 2048, @@ -44,8 +42,6 @@ const osThreadAttr_t analyzer_attrs = { .name = "Analyzer", void vAnalyzer(void *pv_params) { acc_data_t *bmsdata = (acc_data_t *)pv_params; - rl_can_msg_init(DISCHARGE, 3); - rl_can_msg_init(CHARGE, 2); for (;;) { osThreadFlagsWait(ANALYZER_FLAG, osFlagsWaitAny, osWaitForever); From 325a75d6dfb171f208ffeeb3e262c636e860407e Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 3 Nov 2024 11:00:29 -0500 Subject: [PATCH 05/14] naming conflict --- Core/Inc/can_handler.h | 2 +- Core/Src/compute.c | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 3b9289e..c29aa0d 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -31,7 +31,7 @@ typedef struct { typedef enum { CHARGE, DISCHARGE, RL_MSG_COUNT } rate_lim_t; -can_msg_t can_msgs[RL_MSG_COUNT]; +can_msg_t bms_can_msgs[RL_MSG_COUNT]; rl_data_t rl_data[RL_MSG_COUNT]; static const uint32_t can1_id_list[NUM_INBOUND_CAN1_IDS] = { diff --git a/Core/Src/compute.c b/Core/Src/compute.c index bde6480..6374919 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -113,8 +113,8 @@ void init_rl_config() rl_data_t rl_discharge_data = { .msg_rate = 100 }; rl_data_t rl_charge_data = { .msg_rate = 200 }; - can_msgs[DISCHARGE] = discharge_msg; - can_msgs[CHARGE] = charge_msg; + bms_can_msgs[DISCHARGE] = discharge_msg; + bms_can_msgs[CHARGE] = charge_msg; rl_data[DISCHARGE] = rl_discharge_data; rl_data[CHARGE] = rl_charge_data; @@ -316,10 +316,10 @@ void compute_send_mc_discharge_message(acc_data_t *bmsdata) endian_swap(&discharge_data.max_discharge, sizeof(discharge_data.max_discharge)); - memcpy(can_msgs[DISCHARGE].data, &discharge_data, + memcpy(bms_can_msgs[DISCHARGE].data, &discharge_data, sizeof(discharge_data)); - queue_can_msg(can_msgs[DISCHARGE]); + queue_can_msg(bms_can_msgs[DISCHARGE]); } void compute_send_mc_charge_message(acc_data_t *bmsdata) @@ -334,9 +334,9 @@ void compute_send_mc_charge_message(acc_data_t *bmsdata) /* convert to big endian */ endian_swap(&charge_data.max_charge, sizeof(charge_data.max_charge)); - memcpy(can_msgs[CHARGE].data, &charge_data, sizeof(charge_data)); + memcpy(bms_can_msgs[CHARGE].data, &charge_data, sizeof(charge_data)); - queue_can_msg(can_msgs[CHARGE]); + queue_can_msg(bms_can_msgs[CHARGE]); } void compute_send_acc_status_message(acc_data_t *bmsdata) From 7568c6111644eedceaff46f3d0d94649f926da40 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 3 Nov 2024 11:04:15 -0500 Subject: [PATCH 06/14] fixed multiple definitions --- Core/Inc/compute.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/Inc/compute.h b/Core/Inc/compute.h index 632303b..04ecb7a 100644 --- a/Core/Inc/compute.h +++ b/Core/Inc/compute.h @@ -22,6 +22,9 @@ typedef enum { FAN1, FAN2, FAN3, FAN4, FAN5, FAN6, FANMAX } fan_select_t; extern can_t can1; // main can bus, used by most peripherals extern can_t can2; // p2p can bus with charger +extern can_msg_t bms_can_msgs[RL_MSG_COUNT]; /* defined in can_handler.h */ +extern rl_data_t rl_data[RL_MSG_COUNT]; /* defined in can_handler.h */ + /** * @brief inits the compute interface */ From 1eda63be279bdce71b8d7f7dd1789d81cfea9e26 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 3 Nov 2024 11:10:07 -0500 Subject: [PATCH 07/14] moved definition --- Core/Inc/can_handler.h | 3 --- Core/Inc/compute.h | 4 ++-- Core/Src/can_handler.c | 3 +++ 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index c29aa0d..de8c42b 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -31,9 +31,6 @@ typedef struct { typedef enum { CHARGE, DISCHARGE, RL_MSG_COUNT } rate_lim_t; -can_msg_t bms_can_msgs[RL_MSG_COUNT]; -rl_data_t rl_data[RL_MSG_COUNT]; - static const uint32_t can1_id_list[NUM_INBOUND_CAN1_IDS] = { //CANID_X, 0x0000 diff --git a/Core/Inc/compute.h b/Core/Inc/compute.h index 04ecb7a..44b1eb8 100644 --- a/Core/Inc/compute.h +++ b/Core/Inc/compute.h @@ -22,8 +22,8 @@ typedef enum { FAN1, FAN2, FAN3, FAN4, FAN5, FAN6, FANMAX } fan_select_t; extern can_t can1; // main can bus, used by most peripherals extern can_t can2; // p2p can bus with charger -extern can_msg_t bms_can_msgs[RL_MSG_COUNT]; /* defined in can_handler.h */ -extern rl_data_t rl_data[RL_MSG_COUNT]; /* defined in can_handler.h */ +extern can_msg_t bms_can_msgs[RL_MSG_COUNT]; /* defined in can_handler.c */ +extern rl_data_t rl_data[RL_MSG_COUNT]; /* defined in can_handler.c */ /** * @brief inits the compute interface diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 7f2db38..d5c0547 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -7,6 +7,9 @@ ringbuffer_t *can1_rx_queue = NULL; ringbuffer_t *can2_rx_queue = NULL; +can_msg_t bms_can_msgs[RL_MSG_COUNT]; +rl_data_t rl_data[RL_MSG_COUNT]; + void can_receive_callback(CAN_HandleTypeDef *hcan) { CAN_RxHeaderTypeDef rx_header; From 13a0843acf785e5bc43cd95b7a1ea5f7a30b8429 Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 5 Nov 2024 18:15:24 -0500 Subject: [PATCH 08/14] tested rate limiting --- Core/Inc/can_handler.h | 2 +- Core/Src/can_handler.c | 12 ++++++++---- Core/Src/compute.c | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index de8c42b..6c18dbd 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -26,7 +26,7 @@ extern osMessageQueueId_t can_outbound_queue; typedef struct { nertimer_t timer; - uint8_t msg_rate; /* in milliseconds */ + uint32_t msg_rate; /* in milliseconds */ } rl_data_t; typedef enum { CHARGE, DISCHARGE, RL_MSG_COUNT } rate_lim_t; diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index d5c0547..ff23ec5 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -78,10 +78,14 @@ osStatus_t queue_can_msg(can_msg_t msg) return -1; rl_data_t *rl_data = get_rl_msg(msg.id); - if (rl_data != NULL && is_timer_active(&rl_data->timer)) { - return 0; - } else { - start_timer(&rl_data->timer, rl_data->msg_rate); + + if (rl_data != NULL) { + if (!is_timer_active(&rl_data->timer) || + is_timer_expired(&rl_data->timer)) { + start_timer(&rl_data->timer, rl_data->msg_rate); + } else if (is_timer_active(&rl_data->timer)) { + return 0; + } } osStatus_t res = osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U); diff --git a/Core/Src/compute.c b/Core/Src/compute.c index 6374919..3bbbaff 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -110,8 +110,8 @@ void init_rl_config() CHARGE_CANID; // 0x0A is the dcl id, 0x157 is the device id set by us charge_msg.len = 8; - rl_data_t rl_discharge_data = { .msg_rate = 100 }; - rl_data_t rl_charge_data = { .msg_rate = 200 }; + rl_data_t rl_discharge_data = { .msg_rate = 0 }; + rl_data_t rl_charge_data = { .msg_rate = 0 }; bms_can_msgs[DISCHARGE] = discharge_msg; bms_can_msgs[CHARGE] = charge_msg; From d6788f8abd5faa14aea0d361abc4326f39232e96 Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 5 Nov 2024 18:47:33 -0500 Subject: [PATCH 09/14] all can msgs can now be rate limited --- Core/Inc/can_handler.h | 33 ++++++++-- Core/Src/can_handler.c | 37 ++++++++++- Core/Src/compute.c | 141 ++++++++++++++++++++++++----------------- 3 files changed, 149 insertions(+), 62 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 6c18dbd..e830ba0 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -11,9 +11,19 @@ #define NUM_INBOUND_CAN1_IDS 1 #define NUM_INBOUND_CAN2_IDS 1 -/*TODO: define ids of all rate limited can messages */ -#define CHARGE_CANID 0x176 -#define DISCHARGE_CANID 0x156 +#define CHARGE_CANID 0x176 +#define DISCHARGE_CANID 0x156 +#define ACC_STATUS_CANID 0x80 +#define BMS_STATUS_CANID 0x81 +#define SHUTDOWN_CTRL_CANID 0x82 +#define CELL_DATA_CANID 0x83 +#define CELL_VOLTAGE_CANID 0x87 +#define CURRENT_CANID 0x86 +#define CELL_TEMP_CANID 0x84 +#define SEGMENT_TEMP_CANID 0x85 +#define FAULT_CANID 0x703 +#define NOISE_CANID 0x88 +#define DEBUG_CANID 0x702 extern CAN_HandleTypeDef hcan1; extern CAN_HandleTypeDef hcan2; @@ -29,7 +39,22 @@ typedef struct { uint32_t msg_rate; /* in milliseconds */ } rl_data_t; -typedef enum { CHARGE, DISCHARGE, RL_MSG_COUNT } rate_lim_t; +typedef enum { + CHARGE, + DISCHARGE, + ACC_STATUS, + BMS_STATUS, + SHUTDOWN_CTRL, + CELL_DATA, + CELL_VOLTAGE, + CURRENT, + CELL_TEMP, + SEGMENT_TEMP, + FAULT, + NOISE, + DEBUG, + RL_MSG_COUNT +} rate_lim_t; static const uint32_t can1_id_list[NUM_INBOUND_CAN1_IDS] = { //CANID_X, diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index ff23ec5..1bfc3d1 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -108,7 +108,42 @@ rl_data_t *get_rl_msg(uint32_t can_id) case DISCHARGE_CANID: return &rl_data[DISCHARGE]; break; + case ACC_STATUS_CANID: + return &rl_data[ACC_STATUS]; + break; + case BMS_STATUS_CANID: + return &rl_data[BMS_STATUS]; + break; + case SHUTDOWN_CTRL_CANID: + return &rl_data[SHUTDOWN_CTRL]; + break; + case CELL_DATA_CANID: + return &rl_data[CELL_DATA]; + break; + case CELL_VOLTAGE_CANID: + return &rl_data[CELL_VOLTAGE]; + break; + case CURRENT_CANID: + return &rl_data[CURRENT]; + break; + case CELL_TEMP_CANID: + return &rl_data[CELL_TEMP]; + break; + case SEGMENT_TEMP_CANID: + return &rl_data[SEGMENT_TEMP]; + break; + case FAULT_CANID: + return &rl_data[FAULT]; + break; + case NOISE_CANID: + return &rl_data[NOISE]; + break; + case DEBUG_CANID: + return &rl_data[DEBUG]; + break; default: - return NULL; + break; } + + return NULL; } diff --git a/Core/Src/compute.c b/Core/Src/compute.c index 3bbbaff..209c06c 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -103,18 +103,60 @@ void init_rl_config() can_msg_t discharge_msg = { 0 }; discharge_msg.id = DISCHARGE_CANID; // 0x0A is the dcl id, 0x22 is the device id set by us - discharge_msg.len = 8; can_msg_t charge_msg = { 0 }; charge_msg.id = CHARGE_CANID; // 0x0A is the dcl id, 0x157 is the device id set by us - charge_msg.len = 8; + + can_msg_t acc_status_msg; + acc_status_msg.id = ACC_STATUS_CANID; + + can_msg_t bms_status_msg; + bms_status_msg.id = BMS_STATUS_CANID; + + can_msg_t shutdown_ctrl_msg; + shutdown_ctrl_msg.id = SHUTDOWN_CTRL_CANID; + + can_msg_t cell_data_msg; + cell_data_msg.id = CELL_DATA_CANID; + + can_msg_t cell_voltage_msg; + cell_voltage_msg.id = CELL_VOLTAGE_CANID; + + can_msg_t current_msg; + current_msg.id = CURRENT_CANID; + + can_msg_t cell_temp_msg; + cell_temp_msg.id = CELL_TEMP_CANID; + + can_msg_t segment_temp_msg; + segment_temp_msg.id = SEGMENT_TEMP_CANID; + + can_msg_t fault_msg; + fault_msg.id = FAULT_CANID; + + can_msg_t noise_msg; + noise_msg.id = NOISE_CANID; + + can_msg_t debug_msg; + debug_msg.id = DEBUG_CANID; rl_data_t rl_discharge_data = { .msg_rate = 0 }; rl_data_t rl_charge_data = { .msg_rate = 0 }; bms_can_msgs[DISCHARGE] = discharge_msg; bms_can_msgs[CHARGE] = charge_msg; + bms_can_msgs[ACC_STATUS] = acc_status_msg; + bms_can_msgs[BMS_STATUS] = bms_status_msg; + bms_can_msgs[SHUTDOWN_CTRL] = shutdown_ctrl_msg; + bms_can_msgs[CELL_DATA] = cell_data_msg; + bms_can_msgs[CELL_VOLTAGE] = cell_voltage_msg; + bms_can_msgs[CURRENT] = current_msg; + bms_can_msgs[CELL_TEMP] = cell_temp_msg; + bms_can_msgs[SEGMENT_TEMP] = segment_temp_msg; + bms_can_msgs[FAULT] = fault_msg; + bms_can_msgs[NOISE] = noise_msg; + bms_can_msgs[DEBUG] = debug_msg; rl_data[DISCHARGE] = rl_discharge_data; rl_data[CHARGE] = rl_charge_data; @@ -316,6 +358,7 @@ void compute_send_mc_discharge_message(acc_data_t *bmsdata) endian_swap(&discharge_data.max_discharge, sizeof(discharge_data.max_discharge)); + bms_can_msgs[DISCHARGE].len = 8; memcpy(bms_can_msgs[DISCHARGE].data, &discharge_data, sizeof(discharge_data)); @@ -334,6 +377,7 @@ void compute_send_mc_charge_message(acc_data_t *bmsdata) /* convert to big endian */ endian_swap(&charge_data.max_charge, sizeof(charge_data.max_charge)); + bms_can_msgs[CHARGE].len = 8; memcpy(bms_can_msgs[CHARGE].data, &charge_data, sizeof(charge_data)); queue_can_msg(bms_can_msgs[CHARGE]); @@ -364,12 +408,11 @@ void compute_send_acc_status_message(acc_data_t *bmsdata) endian_swap(&acc_status_msg_data.pack_ah, sizeof(acc_status_msg_data.pack_ah)); - can_msg_t acc_msg; - acc_msg.id = 0x80; - acc_msg.len = sizeof(acc_status_msg_data); - memcpy(acc_msg.data, &acc_status_msg_data, sizeof(acc_status_msg_data)); + bms_can_msgs[ACC_STATUS].len = sizeof(acc_status_msg_data); + memcpy(bms_can_msgs[ACC_STATUS].data, &acc_status_msg_data, + sizeof(acc_status_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[ACC_STATUS]); } void compute_send_bms_status_message(acc_data_t *bmsdata, int bms_state, @@ -394,12 +437,11 @@ void compute_send_bms_status_message(acc_data_t *bmsdata, int bms_state, endian_swap(&bms_status_msg_data.fault, sizeof(bms_status_msg_data.fault)); - can_msg_t acc_msg; - acc_msg.id = 0x81; - acc_msg.len = sizeof(bms_status_msg_data); - memcpy(acc_msg.data, &bms_status_msg_data, sizeof(bms_status_msg_data)); + bms_can_msgs[BMS_STATUS].len = sizeof(bms_status_msg_data); + memcpy(bms_can_msgs[BMS_STATUS].data, &bms_status_msg_data, + sizeof(bms_status_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[BMS_STATUS]); } void compute_send_shutdown_ctrl_message(uint8_t mpe_state) @@ -410,13 +452,11 @@ void compute_send_shutdown_ctrl_message(uint8_t mpe_state) shutdown_control_msg_data.mpeState = mpe_state; - can_msg_t acc_msg; - acc_msg.id = 0x82; - acc_msg.len = sizeof(shutdown_control_msg_data); - memcpy(acc_msg.data, &shutdown_control_msg_data, + bms_can_msgs[SHUTDOWN_CTRL].len = sizeof(shutdown_control_msg_data); + memcpy(bms_can_msgs[SHUTDOWN_CTRL].data, &shutdown_control_msg_data, sizeof(shutdown_control_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[SHUTDOWN_CTRL]); } void compute_send_cell_data_message(acc_data_t *bmsdata) @@ -446,12 +486,11 @@ void compute_send_cell_data_message(acc_data_t *bmsdata) endian_swap(&cell_data_msg_data.volt_avg, sizeof(cell_data_msg_data.volt_avg)); - can_msg_t acc_msg; - acc_msg.id = 0x83; - acc_msg.len = sizeof(cell_data_msg_data); - memcpy(acc_msg.data, &cell_data_msg_data, sizeof(cell_data_msg_data)); + bms_can_msgs[CELL_DATA].len = sizeof(cell_data_msg_data); + memcpy(bms_can_msgs[CELL_DATA].data, &cell_data_msg_data, + sizeof(cell_data_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[CELL_DATA]); } void compute_send_cell_voltage_message(uint8_t cell_id, @@ -481,13 +520,11 @@ void compute_send_cell_voltage_message(uint8_t cell_id, endian_swap(&cell_voltage_msg_data.openVoltage, sizeof(cell_voltage_msg_data.openVoltage)); - can_msg_t acc_msg; - acc_msg.id = 0x87; - acc_msg.len = sizeof(cell_voltage_msg_data); - memcpy(acc_msg.data, &cell_voltage_msg_data, + bms_can_msgs[CELL_VOLTAGE].len = sizeof(cell_voltage_msg_data); + memcpy(bms_can_msgs[CELL_VOLTAGE].data, &cell_voltage_msg_data, sizeof(cell_voltage_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[CELL_VOLTAGE]); } void compute_send_current_message(acc_data_t *bmsdata) @@ -510,13 +547,11 @@ void compute_send_current_message(acc_data_t *bmsdata) endian_swap(¤t_status_msg_data.pack_curr, sizeof(current_status_msg_data.pack_curr)); - can_msg_t acc_msg; - acc_msg.id = 0x86; - acc_msg.len = sizeof(current_status_msg_data); - memcpy(acc_msg.data, ¤t_status_msg_data, + bms_can_msgs[CURRENT].len = sizeof(current_status_msg_data); + memcpy(bms_can_msgs[CURRENT].data, ¤t_status_msg_data, sizeof(current_status_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[CURRENT]); } void compute_send_cell_temp_message(acc_data_t *bmsdata) @@ -545,12 +580,11 @@ void compute_send_cell_temp_message(acc_data_t *bmsdata) endian_swap(&cell_temp_msg_data.average_temp, sizeof(cell_temp_msg_data.average_temp)); - can_msg_t acc_msg; - acc_msg.id = 0x84; - acc_msg.len = sizeof(cell_temp_msg_data); - memcpy(acc_msg.data, &cell_temp_msg_data, sizeof(cell_temp_msg_data)); + bms_can_msgs[CELL_TEMP].len = sizeof(cell_temp_msg_data); + memcpy(bms_can_msgs[CELL_TEMP].data, &cell_temp_msg_data, + sizeof(cell_temp_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[CELL_TEMP]); } void compute_send_segment_temp_message(acc_data_t *bmsdata) @@ -578,13 +612,11 @@ void compute_send_segment_temp_message(acc_data_t *bmsdata) segment_temp_msg_data.segment6_average_temp = bmsdata->segment_average_temps[5]; - can_msg_t acc_msg; - acc_msg.id = 0x85; - acc_msg.len = sizeof(segment_temp_msg_data); - memcpy(acc_msg.data, &segment_temp_msg_data, + bms_can_msgs[SEGMENT_TEMP].len = sizeof(segment_temp_msg_data); + memcpy(bms_can_msgs[SEGMENT_TEMP].data, &segment_temp_msg_data, sizeof(segment_temp_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[SEGMENT_TEMP]); } void compute_send_fault_message(uint8_t status, int16_t curr, int16_t in_dcl) @@ -603,12 +635,11 @@ void compute_send_fault_message(uint8_t status, int16_t curr, int16_t in_dcl) sizeof(fault_msg_data.pack_curr)); endian_swap(&fault_msg_data.dcl, sizeof(fault_msg_data.dcl)); - can_msg_t acc_msg; - acc_msg.id = 0x703; - acc_msg.len = 5; - memcpy(acc_msg.data, &fault_msg_data, sizeof(fault_msg_data)); + bms_can_msgs[FAULT].len = 5; + memcpy(bms_can_msgs[FAULT].data, &fault_msg_data, + sizeof(fault_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[FAULT]); } void compute_send_voltage_noise_message(acc_data_t *bmsdata) @@ -635,13 +666,11 @@ void compute_send_voltage_noise_message(acc_data_t *bmsdata) voltage_noise_msg_data.seg6_noise = bmsdata->segment_noise_percentage[5]; - can_msg_t acc_msg; - acc_msg.id = 0x88; - acc_msg.len = sizeof(voltage_noise_msg_data); - memcpy(acc_msg.data, &voltage_noise_msg_data, + bms_can_msgs[NOISE].len = sizeof(voltage_noise_msg_data); + memcpy(bms_can_msgs[NOISE].data, &voltage_noise_msg_data, sizeof(voltage_noise_msg_data)); - queue_can_msg(acc_msg); + queue_can_msg(bms_can_msgs[NOISE]); } void compute_send_debug_message(uint8_t debug0, uint8_t debug1, uint16_t debug2, @@ -659,16 +688,14 @@ void compute_send_debug_message(uint8_t debug0, uint8_t debug1, uint16_t debug2, debug_msg_data.debug2 = debug2; debug_msg_data.debug3 = debug3; - can_msg_t debug_msg; - debug_msg.id = 0x702; - debug_msg.len = 8; // yaml decodes this msg as 8 bytes + bms_can_msgs[DEBUG].len = 8; // yaml decodes this msg as 8 bytes endian_swap(&debug_msg_data.debug2, sizeof(debug_msg_data.debug2)); endian_swap(&debug_msg_data.debug3, sizeof(debug_msg_data.debug3)); - memcpy(debug_msg.data, &debug_msg_data, 8); + memcpy(bms_can_msgs[DEBUG].data, &debug_msg_data, 8); - queue_can_msg(debug_msg); + queue_can_msg(bms_can_msgs[DEBUG]); } void change_adc1_channel(uint8_t channel) From b6692828355d50431a1b49c4483f2bc5d20a0e8d Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 5 Nov 2024 19:35:31 -0500 Subject: [PATCH 10/14] check for zero msg rate --- Core/Src/can_handler.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 1bfc3d1..32a3bd2 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -80,8 +80,9 @@ osStatus_t queue_can_msg(can_msg_t msg) rl_data_t *rl_data = get_rl_msg(msg.id); if (rl_data != NULL) { - if (!is_timer_active(&rl_data->timer) || - is_timer_expired(&rl_data->timer)) { + if (rl_data->msg_rate != 0 && + (!is_timer_active(&rl_data->timer) || + is_timer_expired(&rl_data->timer))) { start_timer(&rl_data->timer, rl_data->msg_rate); } else if (is_timer_active(&rl_data->timer)) { return 0; From c9fc2adf1d54d69edd968b1e1ec72b0c9cb23fdb Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 5 Nov 2024 20:02:36 -0500 Subject: [PATCH 11/14] fixed mistake --- Core/Src/can_handler.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 32a3bd2..4df7159 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -79,10 +79,9 @@ osStatus_t queue_can_msg(can_msg_t msg) rl_data_t *rl_data = get_rl_msg(msg.id); - if (rl_data != NULL) { - if (rl_data->msg_rate != 0 && - (!is_timer_active(&rl_data->timer) || - is_timer_expired(&rl_data->timer))) { + if (rl_data != NULL && rl_data->msg_rate != 0) { + if (!is_timer_active(&rl_data->timer) || + is_timer_expired(&rl_data->timer)) { start_timer(&rl_data->timer, rl_data->msg_rate); } else if (is_timer_active(&rl_data->timer)) { return 0; From f3e444be55c54da5e61ef5154c7684ab09a06ff0 Mon Sep 17 00:00:00 2001 From: Caio Date: Thu, 7 Nov 2024 16:40:50 -0500 Subject: [PATCH 12/14] removed length definition in config --- Core/Inc/can_handler.h | 2 +- Core/Src/can_handler.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index e830ba0..94bfa5d 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -35,7 +35,7 @@ extern ringbuffer_t *can2_rx_queue; extern osMessageQueueId_t can_outbound_queue; typedef struct { - nertimer_t timer; + uint32_t prev_tick; uint32_t msg_rate; /* in milliseconds */ } rl_data_t; diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 4df7159..c5fca6b 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -80,11 +80,12 @@ osStatus_t queue_can_msg(can_msg_t msg) rl_data_t *rl_data = get_rl_msg(msg.id); if (rl_data != NULL && rl_data->msg_rate != 0) { - if (!is_timer_active(&rl_data->timer) || - is_timer_expired(&rl_data->timer)) { - start_timer(&rl_data->timer, rl_data->msg_rate); - } else if (is_timer_active(&rl_data->timer)) { + if (HAL_GetTick() <= + pdMS_TO_TICKS(rl_data->prev_tick) + rl_data->msg_rate) { + // block message return 0; + } else { + rl_data->prev_tick = HAL_GetTick(); } } From 48d55a34ce69b8027e0ad17d8f22ae579c932607 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 8 Nov 2024 15:52:07 -0500 Subject: [PATCH 13/14] constant lenghts defined in config --- Core/Src/compute.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Core/Src/compute.c b/Core/Src/compute.c index 209c06c..3023980 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -103,43 +103,56 @@ void init_rl_config() can_msg_t discharge_msg = { 0 }; discharge_msg.id = DISCHARGE_CANID; // 0x0A is the dcl id, 0x22 is the device id set by us + discharge_msg.len = 8; can_msg_t charge_msg = { 0 }; charge_msg.id = CHARGE_CANID; // 0x0A is the dcl id, 0x157 is the device id set by us + charge_msg.len = 8; can_msg_t acc_status_msg; acc_status_msg.id = ACC_STATUS_CANID; + acc_status_msg.len = 8; can_msg_t bms_status_msg; bms_status_msg.id = BMS_STATUS_CANID; + bms_status_msg.len = 8; can_msg_t shutdown_ctrl_msg; shutdown_ctrl_msg.id = SHUTDOWN_CTRL_CANID; + shutdown_ctrl_msg.len = 1; can_msg_t cell_data_msg; cell_data_msg.id = CELL_DATA_CANID; + cell_data_msg.len = 8; can_msg_t cell_voltage_msg; cell_voltage_msg.id = CELL_VOLTAGE_CANID; + cell_voltage_msg.len = 8; can_msg_t current_msg; current_msg.id = CURRENT_CANID; + current_msg.len = 6; can_msg_t cell_temp_msg; cell_temp_msg.id = CELL_TEMP_CANID; + cell_temp_msg.len = 8; can_msg_t segment_temp_msg; segment_temp_msg.id = SEGMENT_TEMP_CANID; + segment_temp_msg.len = 6; can_msg_t fault_msg; fault_msg.id = FAULT_CANID; + fault_msg.len = 5; can_msg_t noise_msg; noise_msg.id = NOISE_CANID; + noise_msg.len = 6; can_msg_t debug_msg; debug_msg.id = DEBUG_CANID; + debug_msg.len = 8; // yaml decodes this to 8 bytes rl_data_t rl_discharge_data = { .msg_rate = 0 }; rl_data_t rl_charge_data = { .msg_rate = 0 }; @@ -358,7 +371,6 @@ void compute_send_mc_discharge_message(acc_data_t *bmsdata) endian_swap(&discharge_data.max_discharge, sizeof(discharge_data.max_discharge)); - bms_can_msgs[DISCHARGE].len = 8; memcpy(bms_can_msgs[DISCHARGE].data, &discharge_data, sizeof(discharge_data)); @@ -377,7 +389,6 @@ void compute_send_mc_charge_message(acc_data_t *bmsdata) /* convert to big endian */ endian_swap(&charge_data.max_charge, sizeof(charge_data.max_charge)); - bms_can_msgs[CHARGE].len = 8; memcpy(bms_can_msgs[CHARGE].data, &charge_data, sizeof(charge_data)); queue_can_msg(bms_can_msgs[CHARGE]); @@ -408,7 +419,6 @@ void compute_send_acc_status_message(acc_data_t *bmsdata) endian_swap(&acc_status_msg_data.pack_ah, sizeof(acc_status_msg_data.pack_ah)); - bms_can_msgs[ACC_STATUS].len = sizeof(acc_status_msg_data); memcpy(bms_can_msgs[ACC_STATUS].data, &acc_status_msg_data, sizeof(acc_status_msg_data)); @@ -437,7 +447,6 @@ void compute_send_bms_status_message(acc_data_t *bmsdata, int bms_state, endian_swap(&bms_status_msg_data.fault, sizeof(bms_status_msg_data.fault)); - bms_can_msgs[BMS_STATUS].len = sizeof(bms_status_msg_data); memcpy(bms_can_msgs[BMS_STATUS].data, &bms_status_msg_data, sizeof(bms_status_msg_data)); @@ -452,7 +461,6 @@ void compute_send_shutdown_ctrl_message(uint8_t mpe_state) shutdown_control_msg_data.mpeState = mpe_state; - bms_can_msgs[SHUTDOWN_CTRL].len = sizeof(shutdown_control_msg_data); memcpy(bms_can_msgs[SHUTDOWN_CTRL].data, &shutdown_control_msg_data, sizeof(shutdown_control_msg_data)); @@ -486,7 +494,6 @@ void compute_send_cell_data_message(acc_data_t *bmsdata) endian_swap(&cell_data_msg_data.volt_avg, sizeof(cell_data_msg_data.volt_avg)); - bms_can_msgs[CELL_DATA].len = sizeof(cell_data_msg_data); memcpy(bms_can_msgs[CELL_DATA].data, &cell_data_msg_data, sizeof(cell_data_msg_data)); @@ -520,7 +527,6 @@ void compute_send_cell_voltage_message(uint8_t cell_id, endian_swap(&cell_voltage_msg_data.openVoltage, sizeof(cell_voltage_msg_data.openVoltage)); - bms_can_msgs[CELL_VOLTAGE].len = sizeof(cell_voltage_msg_data); memcpy(bms_can_msgs[CELL_VOLTAGE].data, &cell_voltage_msg_data, sizeof(cell_voltage_msg_data)); @@ -547,7 +553,6 @@ void compute_send_current_message(acc_data_t *bmsdata) endian_swap(¤t_status_msg_data.pack_curr, sizeof(current_status_msg_data.pack_curr)); - bms_can_msgs[CURRENT].len = sizeof(current_status_msg_data); memcpy(bms_can_msgs[CURRENT].data, ¤t_status_msg_data, sizeof(current_status_msg_data)); @@ -580,7 +585,6 @@ void compute_send_cell_temp_message(acc_data_t *bmsdata) endian_swap(&cell_temp_msg_data.average_temp, sizeof(cell_temp_msg_data.average_temp)); - bms_can_msgs[CELL_TEMP].len = sizeof(cell_temp_msg_data); memcpy(bms_can_msgs[CELL_TEMP].data, &cell_temp_msg_data, sizeof(cell_temp_msg_data)); @@ -612,7 +616,6 @@ void compute_send_segment_temp_message(acc_data_t *bmsdata) segment_temp_msg_data.segment6_average_temp = bmsdata->segment_average_temps[5]; - bms_can_msgs[SEGMENT_TEMP].len = sizeof(segment_temp_msg_data); memcpy(bms_can_msgs[SEGMENT_TEMP].data, &segment_temp_msg_data, sizeof(segment_temp_msg_data)); @@ -635,7 +638,6 @@ void compute_send_fault_message(uint8_t status, int16_t curr, int16_t in_dcl) sizeof(fault_msg_data.pack_curr)); endian_swap(&fault_msg_data.dcl, sizeof(fault_msg_data.dcl)); - bms_can_msgs[FAULT].len = 5; memcpy(bms_can_msgs[FAULT].data, &fault_msg_data, sizeof(fault_msg_data)); @@ -666,7 +668,6 @@ void compute_send_voltage_noise_message(acc_data_t *bmsdata) voltage_noise_msg_data.seg6_noise = bmsdata->segment_noise_percentage[5]; - bms_can_msgs[NOISE].len = sizeof(voltage_noise_msg_data); memcpy(bms_can_msgs[NOISE].data, &voltage_noise_msg_data, sizeof(voltage_noise_msg_data)); @@ -688,8 +689,6 @@ void compute_send_debug_message(uint8_t debug0, uint8_t debug1, uint16_t debug2, debug_msg_data.debug2 = debug2; debug_msg_data.debug3 = debug3; - bms_can_msgs[DEBUG].len = 8; // yaml decodes this msg as 8 bytes - endian_swap(&debug_msg_data.debug2, sizeof(debug_msg_data.debug2)); endian_swap(&debug_msg_data.debug3, sizeof(debug_msg_data.debug3)); From df769fa0346cf83d8c6cbc3951348b532081ddda Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 12 Nov 2024 16:22:55 -0500 Subject: [PATCH 14/14] renaming --- Core/Inc/compute.h | 4 ++-- Core/Src/compute.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Core/Inc/compute.h b/Core/Inc/compute.h index 44b1eb8..f44f088 100644 --- a/Core/Inc/compute.h +++ b/Core/Inc/compute.h @@ -31,9 +31,9 @@ extern rl_data_t rl_data[RL_MSG_COUNT]; /* defined in can_handler.c */ uint8_t compute_init(); /** - * @brief inits all rate limited can messages + * @brief initializes all CAN messages and message rates */ -void init_rl_config(); +void init_can_msg_config(); /** * @brief sets safeguard bool to check whether charging is enabled or disabled diff --git a/Core/Src/compute.c b/Core/Src/compute.c index 3023980..1a1d3f5 100644 --- a/Core/Src/compute.c +++ b/Core/Src/compute.c @@ -92,13 +92,13 @@ uint8_t compute_init(acc_data_t *bmsdata) HAL_ADC_Start(&hadc2); - /* Initializing rate limited messages */ - init_rl_config(); + /* Initializing can messages limited messages */ + init_can_msg_config(); return 0; } -void init_rl_config() +void init_can_msg_config() { can_msg_t discharge_msg = { 0 }; discharge_msg.id = @@ -154,7 +154,7 @@ void init_rl_config() debug_msg.id = DEBUG_CANID; debug_msg.len = 8; // yaml decodes this to 8 bytes - rl_data_t rl_discharge_data = { .msg_rate = 0 }; + rl_data_t rl_discharge_data = { .msg_rate = 5000 }; rl_data_t rl_charge_data = { .msg_rate = 0 }; bms_can_msgs[DISCHARGE] = discharge_msg;