Skip to content

Commit

Permalink
Added non-critical faults and included test in can_handler.c
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Mar 26, 2024
1 parent 742be1d commit cbf2ba4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Core/Inc/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ typedef enum {
} bms_fault_t;
// clang-format on

/**
* @brief Enuemrated possible non-critical fault codes for the BMS
* @note the values increase at powers of two to perform bitwise operations on a main fault code
* to set or get the error codes
*/
typedef enum {
/* non-critical cases */
FAILED_CAN_RECEIVAL = 0x1,

} bms_nc_fault_t;

/**
* @brief Stores critical values for the pack, and where that critical value can be found
*
Expand Down Expand Up @@ -123,6 +134,23 @@ typedef struct {
bool is_charger_connected;
} acc_data_t;

typedef struct
{
nertimer_t timer;
int timeout;

int can_receivals;


/**
* @brief Note that this is a 32 bit integer, so there are 32 max possible fault codes
*/
uint32_t fault_code;
uint32_t fault_status;

} nc_fault_collection_t;


/**
* @brief Represents individual BMS states
*/
Expand Down
9 changes: 9 additions & 0 deletions Core/Inc/stateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ uint32_t sm_fault_return(acc_data_t *accData);
*/
uint32_t sm_fault_eval(fault_eval_t* index);

/**
* @brief Used to collect non-critical fault codes to append to the current status
*
* @param nc_fault_data
* @param fault_code
* @return fault_code
*/
void sm_nc_fault_collect(nc_fault_collection_t *nc_fault_data);

/**
* @brief handles the state machine, calls the appropriate handler function and runs every loop functions
*
Expand Down
10 changes: 8 additions & 2 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
#include "analyzer.h"
#include "ringbuffer.h"
#include "can_handler.h"
#include "stateMachine.h"

ringbuffer_t* can1_rx_queue = NULL;
ringbuffer_t* can2_rx_queue = NULL;
nc_fault_collection_t* nc_fault_collection = NULL;

void can_receive_callback(CAN_HandleTypeDef* hcan)
{
CAN_RxHeaderTypeDef rx_header;
can_msg_t new_msg;

/* Read in CAN message */
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, new_msg.data) != HAL_OK) {


nc_fault_collection->fault_code = FAILED_CAN_RECEIVAL;
sm_nc_fault_collect(nc_fault_collection);
// TODO add non crtical fault capability - could create one for failed can receieve
return;
}


nc_fault_collection->can_receivals += 1;
new_msg.len = rx_header.DLC;
new_msg.id = rx_header.StdId;
if (hcan == &hcan1) {
Expand Down
24 changes: 24 additions & 0 deletions Core/Src/stateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern UART_HandleTypeDef huart4;

acc_data_t* prevAccData;
uint32_t bms_fault = FAULTS_CLEAR;
uint32_t bms_nc_fault = FAULTS_CLEAR;

BMSState_t current_state = BOOT_STATE;
uint32_t previousFault = 0;
Expand Down Expand Up @@ -291,6 +292,29 @@ uint32_t sm_fault_eval(fault_eval_t* index)
return 0;
}

void sm_nc_fault_collect(nc_fault_collection_t *nc_fault_data) {

bool condition;

switch (nc_fault_data->fault_code) {
case FAILED_CAN_RECEIVAL: condition = nc_fault_data->can_receivals == 0; break;
default: true;
}

if (!is_timer_active(&nc_fault_data->timer) && condition)
{
start_timer(&nc_fault_data->timer, nc_fault_data->timeout);
}
else {
if (is_timer_expired(&nc_fault_data->timer)) {
nc_fault_data->fault_status |= nc_fault_data->fault_code;
}
if (!condition) {
cancel_timer(&nc_fault_data->timer);
}
}
}

bool sm_charging_check(acc_data_t* bmsdata)
{
if (!compute_charger_connected())
Expand Down

0 comments on commit cbf2ba4

Please sign in to comment.