Skip to content

Commit

Permalink
ported sht30
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Donahue committed Nov 13, 2023
1 parent 84363b4 commit b8756cc
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 14 deletions.
20 changes: 13 additions & 7 deletions Core/Inc/analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@
//#include <nerduino.h> Replace
#include "datastructs.h"
#include "segment.h"
#include "sht30.h"

/* We want to make sure we aren't doing useless analysis on the same set of data since we are
* backfilling segment data */
#define ANALYSIS_INTERVAL VOLTAGE_WAIT_TIME

//#define MAX_SIZE_OF_HIST_QUEUE 300000U //bytes

/**
* @brief Calculates the PWM required to drive the fans at the current moment in time
*
* @param bmsdata
* @return uint8_t
*/
uint8_t analyzer_calc_fan_pwm();

/**
* @brief Pushes in a new data point if we have waited long enough
*
* @param data
*/
void analyzer_push(acc_data_t* data);
void analyzer_push(acc_data_t* accdata);

/**
* @brief Calculates the PWM required to drive the fans at the current moment in time
*
* @param bmsdata
* @return uint8_t
* @brief Create a new object to store the most recent data point for the temp sensor
*/
uint8_t analyzer_calc_fan_pwm();
void analyzer_sht30_init();

/**
* @brief Pointer to the address of the most recent data point
* @brief Pointer to the address of the most recent data point
*/
extern acc_data_t* bmsdata;

Expand Down
4 changes: 4 additions & 0 deletions Core/Inc/bmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@
#define LOW_CELL_TIME 15000
#define HIGH_TEMP_TIME 60000
#define INT_CELL_COMM_TIME 1000
#define HIGH_INT_TEMP_TIME 0 // TODO SET THIS
#define CURR_ERR_MARG 50 // in A * 10

#define DCDC_CURRENT_DRAW 2 // in A, this is generous

//Compute limits
#define MAX_INTERNAL_TEMP 0 // TODO SET THIS

#define CAN_MESSAGE_WAIT 10

#endif
3 changes: 3 additions & 0 deletions Core/Inc/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include "bmsConfig.h"
#include "timer.h"
#include "sht30.h"

/**
* @brief Individual chip data
Expand Down Expand Up @@ -121,6 +122,8 @@ typedef struct {
uint16_t boost_setting;

bool is_charger_connected;

sht30_t* sht30_data;
} acc_data_t;

/**
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "datastructs.h"

// global that passes cell comm faults upstream to sm
bool cell_comm_fault_status;
extern bool cell_comm_fault_status;

/**
* @brief Initializes the segments
Expand Down
15 changes: 12 additions & 3 deletions Core/Src/analyzer.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "analyzer.h"
#include <stdlib.h>
#include "stm32f4xx_hal.h"

acc_data_t* bmsdata;

acc_data_t* prevbmsdata;

extern I2C_HandleTypeDef hi2c1;


// clang-format off
/**
* @brief Mapping Cell temperature to the cell resistance based on the
Expand Down Expand Up @@ -452,13 +455,13 @@ uint8_t analyzer_calc_fan_pwm()
/ (2 * 5);
}

void analyzer_push(acc_data_t* data)
void analyzer_push(acc_data_t* accdata)
{
if (prevbmsdata != NULL)
free(bmsdata);

prevbmsdata = bmsdata;
bmsdata = data;
bmsdata = accdata;

disable_therms();

Expand All @@ -477,10 +480,16 @@ void analyzer_push(acc_data_t* data)
calc_cont_dcl();
calc_cont_ccl();
calc_state_of_charge();
sht30_get_temp_humid(accdata->sht30_data);

is_first_reading_ = false;
}

void analyzer_sht30_init(acc_data_t* accdata)
{
accdata->sht30_data->i2c_handle = &hi2c1;
}

void disable_therms()
{
int8_t temp_rep_1 = 25; /* Iniitalize to room temp (necessary to stabilize when the BMS first
Expand Down
3 changes: 3 additions & 0 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ int main(void)
/* USER CODE BEGIN Init */
compute_set_fault(0);
segment_init();
analyzer_sht30_init();
/* USER CODE END Init */

/* Configure the system clock */
Expand All @@ -214,6 +215,7 @@ int main(void)
MX_UART4_Init();
MX_USB_OTG_FS_PCD_Init();
MX_I2C1_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */
Expand All @@ -223,6 +225,7 @@ int main(void)
for(;;) {
/* Create a dynamically allocated structure */
acc_data_t *acc_data = malloc(sizeof(acc_data_t));
acc_data->sht30_data = malloc(sizeof(sht30_t));

//acc_data->faultCode = FAULTS_CLEAR;

Expand Down
2 changes: 2 additions & 0 deletions Core/Src/segment.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ nertimer_t variance_timer;
int voltage_error = FAULTS_CLEAR;
int therm_error = FAULTS_CLEAR;

bool cell_comm_fault_status = false;

uint16_t therm_settle_time_ = 0;

const uint32_t VOLT_TEMP_CONV[91] = { 44260, 43970, 43670, 43450, 43030, 42690, 42340, 41980, 41620,
Expand Down
8 changes: 5 additions & 3 deletions Core/Src/stateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ uint32_t sm_fault_return(acc_data_t* accData)
static nertimer_t low_cell_timer = {0};
static nertimer_t high_temp_timer = {0};
static nertimer_t int_cell_comm_tmr = {0};
static nertimer_t int_temp_tmr = {0};
static fault_eval_t* fault_table = NULL;
static acc_data_t* fault_data = NULL;

Expand All @@ -219,13 +220,14 @@ uint32_t sm_fault_return(acc_data_t* accData)
// clang-format off
// ___________FAULT ID____________ __________TIMER___________ _____________DATA________________ __OPERATOR__ __________________________THRESHOLD____________________________ _______TIMER LENGTH_________ _____________FAULT CODE_________________ ___OPERATOR 2__ _______________DATA 2______________ __THRESHOLD 2__
fault_table[0] = (fault_eval_t) {.id = "Discharge Current Limit", .timer = ovr_curr_timer, .data_1 = fault_data->pack_current, .optype_1 = GT, .lim_1 = (fault_data->discharge_limit + DCDC_CURRENT_DRAW)*10*1.04, .timeout = OVER_CURR_TIME, .code = DISCHARGE_LIMIT_ENFORCEMENT_FAULT, .optype_2 = NOP/* ---------------------------UNUSED------------------- */ };
fault_table[1] = (fault_eval_t) {.id = "Charge Current Limit", .timer = ovr_chgcurr_timer, .data_1 = fault_data->pack_current, .optype_1 = GT, .lim_1 = (fault_data->charge_limit)*10, .timeout = OVER_CHG_CURR_TIME, .code = CHARGE_LIMIT_ENFORCEMENT_FAULT, .optype_2 = LT, .data_2 = fault_data->pack_current, .lim_2 = 0 };
fault_table[1] = (fault_eval_t) {.id = "Charge Current Limit", .timer = ovr_chgcurr_timer, .data_1 = fault_data->pack_current, .optype_1 = GT, .lim_1 = (fault_data->charge_limit)*10, .timeout = OVER_CHG_CURR_TIME, .code = CHARGE_LIMIT_ENFORCEMENT_FAULT, .optype_2 = LT, .data_2 = fault_data->pack_current, .lim_2 = 0 };
fault_table[2] = (fault_eval_t) {.id = "Low Cell Voltage", .timer = undr_volt_timer, .data_1 = fault_data->min_voltage.val, .optype_1 = LT, .lim_1 = MIN_VOLT * 10000, .timeout = UNDER_VOLT_TIME, .code = CELL_VOLTAGE_TOO_LOW, .optype_2 = NOP/* ---------------------------UNUSED-------------------*/ };
fault_table[3] = (fault_eval_t) {.id = "High Cell Voltage", .timer = ovr_chgvolt_timer, .data_1 = fault_data->max_voltage.val, .optype_1 = GT, .lim_1 = MAX_CHARGE_VOLT * 10000, .timeout = OVER_VOLT_TIME, .code = CELL_VOLTAGE_TOO_HIGH, .optype_2 = NOP/* ---------------------------UNUSED-------------------*/ };
fault_table[4] = (fault_eval_t) {.id = "High Cell Voltage", .timer = ovr_volt_timer, .data_1 = fault_data->max_voltage.val, .optype_1 = GT, .lim_1 = MAX_VOLT * 10000, .timeout = OVER_VOLT_TIME, .code = CELL_VOLTAGE_TOO_HIGH, .optype_2 = EQ, .data_2 = fault_data->is_charger_connected, .lim_2 = false };
fault_table[4] = (fault_eval_t) {.id = "High Cell Voltage", .timer = ovr_volt_timer, .data_1 = fault_data->max_voltage.val, .optype_1 = GT, .lim_1 = MAX_VOLT * 10000, .timeout = OVER_VOLT_TIME, .code = CELL_VOLTAGE_TOO_HIGH, .optype_2 = EQ, .data_2 = fault_data->is_charger_connected,.lim_2 = false };
fault_table[5] = (fault_eval_t) {.id = "High Temp", .timer = high_temp_timer, .data_1 = fault_data->max_temp.val, .optype_1 = GT, .lim_1 = MAX_CELL_TEMP, .timeout = LOW_CELL_TIME, .code = PACK_TOO_HOT, .optype_2 = NOP/* ----------------------------------------------------*/ };
fault_table[6] = (fault_eval_t) {.id = "Internal Cell Comm Fault",.timer = int_cell_comm_tmr, .data_1 = cell_comm_fault_status, .optype_1 = NEQ,.lim_1 = FAULTS_CLEAR, .timeout = INT_CELL_COMM_TIME, .code = INTERNAL_CELL_COMM_FAULT, .optype_2 = NOP/* --------------------------UNUSED--------------------*/ };
fault_table[6] = (fault_eval_t) {.id = "Internal Cell Comm Fault",.timer = int_cell_comm_tmr, .data_1 = cell_comm_fault_status, .optype_1 = NEQ,.lim_1 = FAULTS_CLEAR, .timeout = INT_CELL_COMM_TIME, .code = INTERNAL_CELL_COMM_FAULT, .optype_2 = NOP/* --------------------------UNUSED--------------------*/ };
fault_table[7] = (fault_eval_t) {.id = "Extremely Low Voltage", .timer = low_cell_timer, .data_1 = fault_data->min_voltage.val, .optype_1 = LT, .lim_1 = 900, .timeout = HIGH_TEMP_TIME, .code = LOW_CELL_VOLTAGE, .optype_2 = NOP/* --------------------------UNUSED--------------------*/ };
fault_table[8] = (fault_eval_t) {.id = "High Internal Temp", .timer = int_temp_tmr, .data_1 =fault_data->sht30_data->temp, .optype_1 = GT, .lim_1 = MAX_INTERNAL_TEMP, .timeout = HIGH_INT_TEMP_TIME, .code = INTERNAL_THERMAL_ERROR, .optype_2 = NOP /* -----------------------------------UNUSED-----------*/ };
fault_table[8] = (fault_eval_t) {.id = NULL};
// clang-format on
}
Expand Down

0 comments on commit b8756cc

Please sign in to comment.