Skip to content

Commit

Permalink
Merge pull request #132 from Northeastern-Electric-Racing/Caio-CanRat…
Browse files Browse the repository at this point in the history
…eLimiting

Can Message Rate Limiting
  • Loading branch information
Sabramz authored Nov 12, 2024
2 parents b659a6f + df769fa commit d9a421f
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 69 deletions.
44 changes: 43 additions & 1 deletion Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@
#include "stm32f4xx_hal.h"
#include <stdint.h>
#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

#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;

Expand All @@ -19,6 +34,28 @@ extern ringbuffer_t *can2_rx_queue;
#define CAN_MSG_QUEUE_SIZE 50 /* messages */
extern osMessageQueueId_t can_outbound_queue;

typedef struct {
uint32_t prev_tick;
uint32_t msg_rate; /* in milliseconds */
} rl_data_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,
0x0000
Expand All @@ -45,4 +82,9 @@ int8_t get_can2_msg();
*/
osStatus_t queue_can_msg(can_msg_t msg);

/**
* 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
9 changes: 9 additions & 0 deletions Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,11 +22,19 @@ 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.c */
extern rl_data_t rl_data[RL_MSG_COUNT]; /* defined in can_handler.c */

/**
* @brief inits the compute interface
*/
uint8_t compute_init();

/**
* @brief initializes all CAN messages and message rates
*/
void init_can_msg_config();

/**
* @brief sets safeguard bool to check whether charging is enabled or disabled
*
Expand Down
64 changes: 64 additions & 0 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,6 +77,18 @@ osStatus_t queue_can_msg(can_msg_t msg)
if (!can_outbound_queue)
return -1;

rl_data_t *rl_data = get_rl_msg(msg.id);

if (rl_data != NULL && rl_data->msg_rate != 0) {
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();
}
}

osStatus_t res = osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U);

if (res) {
Expand All @@ -84,3 +99,52 @@ osStatus_t queue_can_msg(can_msg_t msg)

return res;
}

rl_data_t *get_rl_msg(uint32_t can_id)
{
switch (can_id) {
case CHARGE_CANID:
return &rl_data[CHARGE];
break;
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:
break;
}

return NULL;
}
Loading

0 comments on commit d9a421f

Please sign in to comment.