Skip to content

Commit

Permalink
Merge pull request #147 from Northeastern-Electric-Racing/81-fault-pr…
Browse files Browse the repository at this point in the history
…iority

81 fault priority
  • Loading branch information
tszwinglitw authored Jan 9, 2025
2 parents 0dfb523 + 1d632b7 commit ad5ebd8
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 83 deletions.
6 changes: 4 additions & 2 deletions Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
#define DISCHARGE_CANID 0x156
#define ACC_STATUS_CANID 0x80
#define BMS_STATUS_CANID 0x81
#define FAULT_STATUS_CANID 0x89
#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 FAULT_CANID 0x703 // TODO: cleanup
#define NOISE_CANID 0x88
#define DEBUG_CANID 0x702
#define FAULT_TIMER_CANID 0x6FF
Expand All @@ -39,11 +40,12 @@ typedef enum {
BMS_STATUS,
SHUTDOWN_CTRL,
CELL_DATA,
FAULT_STATUS,
CELL_VOLTAGE,
CURRENT,
CELL_TEMP,
SEGMENT_TEMP,
FAULT,
FAULT, // TODO: cleanup
NOISE,
DEBUG,
FAULT_TIMER,
Expand Down
8 changes: 8 additions & 0 deletions Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ void compute_set_fault(int fault_state);
*/
void compute_send_acc_status_message(acc_data_t *bmsdata);

/**
* @brief sends fault status message
*
* @param bms_state
*
*/
void compute_send_fault_status_message(acc_data_t *bmsdata);

/**
* @brief sends BMS status message
*
Expand Down
16 changes: 10 additions & 6 deletions Core/Inc/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ typedef struct {
* to set or get the error codes
*/
// clang-format off
typedef enum {
enum {
FAULTS_CLEAR = 0x0,

/* Orion BMS faults */
/* Shepherd BMS faults */
CELLS_NOT_BALANCING = 0x1,
CELL_VOLTAGE_TOO_HIGH = 0x4,
CELL_VOLTAGE_TOO_LOW = 0x2,
Expand All @@ -61,7 +61,8 @@ typedef enum {
CHARGE_LIMIT_ENFORCEMENT_FAULT = 0x20000,

MAX_FAULTS = 0x80000000 /* Maximum allowable fault code */
} bms_fault_t;
};

// clang-format on

/**
Expand All @@ -84,7 +85,7 @@ typedef struct {
/* Array of data from all chips in the system */
chipdata_t chip_data[NUM_CHIPS];

int fault_status;
int fault_status; // FIXME: this field is unused

int16_t pack_current; /* this value is multiplied by 10 to account for decimal precision */
uint16_t pack_voltage;
Expand All @@ -103,7 +104,9 @@ typedef struct {
/**
* @brief Note that this is a 32 bit integer, so there are 32 max possible fault codes
*/
uint32_t fault_code;
// uint32_t fault_code;
uint32_t fault_code_crit;
uint32_t fault_code_noncrit;

/* Max, min, and avg thermistor readings */
crit_cellval_t max_temp;
Expand Down Expand Up @@ -178,7 +181,8 @@ typedef struct {
int data_2;
int lim_2;

bool is_faulted;
bool is_critical;
// bool is_faulted; /* note: unused field */
} fault_eval_t;

#endif
10 changes: 5 additions & 5 deletions Core/Inc/stateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ bool sm_charging_check(acc_data_t *bmsdata);
* @note Should be bitwise OR'ed with the current fault status
*
* @param accData
* @return uint32_t
* @return uint64_t to be cast to a bms_fault_t
*/
uint32_t sm_fault_return(acc_data_t *accData);
uint64_t sm_fault_return(acc_data_t *accData);

/**
* @brief Used in parellel to faultReturn(), calculates each fault to append the
* fault status
*
* @param index
* @return fault_code
* @param fault_item
* @return fault_status
*/
uint32_t sm_fault_eval(fault_eval_t *index);
bool sm_fault_eval(fault_eval_t *fault_item);

/**
* @brief handles the state machine, calls the appropriate handler function and
Expand Down
15 changes: 10 additions & 5 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ void init_can_msg_config()

can_msg_t bms_status_msg;
bms_status_msg.id = BMS_STATUS_CANID;
bms_status_msg.len = 8;
bms_status_msg.len = 4;

can_msg_t fault_status_msg;
fault_status_msg.id = FAULT_STATUS_CANID;
fault_status_msg.len = 8;

can_msg_t shutdown_ctrl_msg;
shutdown_ctrl_msg.id = SHUTDOWN_CTRL_CANID;
Expand All @@ -84,9 +88,9 @@ void init_can_msg_config()
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 fault_detail_msg;
fault_detail_msg.id = FAULT_CANID;
fault_detail_msg.len = 5;

can_msg_t noise_msg;
noise_msg.id = NOISE_CANID;
Expand All @@ -107,13 +111,14 @@ void init_can_msg_config()
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[FAULT_STATUS] = fault_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[FAULT] = fault_detail_msg;
bms_can_msgs[NOISE] = noise_msg;
bms_can_msgs[DEBUG] = debug_msg;
bms_can_msgs[FAULT_TIMER] = fault_timer_msg;
Expand Down
28 changes: 22 additions & 6 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,28 +323,44 @@ void compute_send_acc_status_message(acc_data_t *bmsdata)
queue_can_msg(bms_can_msgs[ACC_STATUS]);
}

void compute_send_fault_status_message(acc_data_t *bmsdata)
{
struct __attribute__((__packed__)) {
uint32_t fault_crit;
uint32_t fault_noncrit;
} fault_status_msg_data;

/* convert to big endian */
endian_swap(&fault_status_msg_data.fault_crit,
sizeof(fault_status_msg_data.fault_crit));
endian_swap(&fault_status_msg_data.fault_noncrit,
sizeof(fault_status_msg_data.fault_noncrit));

fault_status_msg_data.fault_crit = bmsdata->fault_code_crit;
fault_status_msg_data.fault_noncrit = bmsdata->fault_code_noncrit;

memcpy(bms_can_msgs[FAULT_STATUS].data, &fault_status_msg_data,
sizeof(fault_status_msg_data));

queue_can_msg(bms_can_msgs[FAULT_STATUS]);
}

void compute_send_bms_status_message(acc_data_t *bmsdata, int bms_state,
bool balance)
{
struct __attribute__((__packed__)) {
uint8_t state;
uint32_t fault;
int8_t temp_avg;
uint8_t temp_internal;
uint8_t balance;
} bms_status_msg_data;

bms_status_msg_data.temp_avg = (int8_t)(bmsdata->avg_temp);
bms_status_msg_data.state = (uint8_t)(bms_state);
bms_status_msg_data.fault = bmsdata->fault_code;
bms_status_msg_data.temp_internal = (uint8_t)(0);
bms_status_msg_data.balance =
(uint8_t)(balance); // segment_is_balancing()

/* convert to big endian */
endian_swap(&bms_status_msg_data.fault,
sizeof(bms_status_msg_data.fault));

memcpy(bms_can_msgs[BMS_STATUS].data, &bms_status_msg_data,
sizeof(bms_status_msg_data));

Expand Down
4 changes: 3 additions & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ int main(void)

acc_data_t *acc_data = malloc(sizeof(acc_data_t));
acc_data->is_charger_connected = false;
acc_data->fault_code = FAULTS_CLEAR;
acc_data->fault_code_crit = FAULTS_CLEAR;
acc_data->fault_code_noncrit = FAULTS_CLEAR;

/* USER CODE END Init */

Expand Down Expand Up @@ -1217,6 +1218,7 @@ void StartDefaultTask(void *argument)

compute_send_bms_status_message(bmsdata, current_state,
segment_is_balancing());
compute_send_fault_status_message(bmsdata);

HAL_IWDG_Refresh(&hiwdg);

Expand Down
Loading

0 comments on commit ad5ebd8

Please sign in to comment.