From 0c881b148cca0d0ad417e38dc88a3d728dbde4ec Mon Sep 17 00:00:00 2001 From: Scott A <89099102+Sabramz@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:04:22 -0500 Subject: [PATCH] adbms debug mode stuff --- Core/Inc/datastructs.h | 3 ++ Core/Inc/segment.h | 25 ++++++++++++ Core/Src/analyzer.c | 37 +++++++++++++---- Core/Src/main.c | 5 +-- Core/Src/segment.c | 74 ++++++++++++++++++++++------------ Core/Src/shep_tasks.c | 90 +++++++++++++++++++++++++++++++++++++----- 6 files changed, 189 insertions(+), 45 deletions(-) diff --git a/Core/Inc/datastructs.h b/Core/Inc/datastructs.h index f3e19b4..d923ed4 100644 --- a/Core/Inc/datastructs.h +++ b/Core/Inc/datastructs.h @@ -29,6 +29,9 @@ typedef struct { /* True if chip is alpha, False if Chip is Beta */ bool alpha; + + /* For temperatures of on-board therms. Length 1 if Alpha, length 2 if Beta. */ + int8_t on_board_temp; } chipdata_t; /** diff --git a/Core/Inc/segment.h b/Core/Inc/segment.h index 7c5f7ef..98033d7 100644 --- a/Core/Inc/segment.h +++ b/Core/Inc/segment.h @@ -50,4 +50,29 @@ bool cell_is_balancing(uint8_t chip_num, uint8_t cell_num); */ bool segment_is_balancing(cell_asic chips[NUM_CHIPS]); +/** + * @brief Do a single shot, redundant C-ADC measurement and read + * the contents of Status Register Group C, which contains the + * CSxFLT bits indicating whether the difference between the + * C and S ADC measurements was above the CTH[2:0] set in config + * register A. + * + * @param chips Pointer to accumulator data struct. + */ +void get_adc_comparison(acc_data_t *bmsdata); + +/** + * @brief Read the serial ID of the chip. + * + * @param chips Array of chips to read. + */ +void read_serial_id(cell_asic chips[NUM_CHIPS]); + +/** + * @brief Read voltages in every register connected to AUX2 ADC. + * + * @param chips Array of chips to get voltages of. + */ +void read_aux2_registers(cell_asic chips[NUM_CHIPS]); + #endif \ No newline at end of file diff --git a/Core/Src/analyzer.c b/Core/Src/analyzer.c index 4a7fd02..e318b6c 100644 --- a/Core/Src/analyzer.c +++ b/Core/Src/analyzer.c @@ -149,10 +149,21 @@ uint8_t get_num_cells(chipdata_t *chip_data) } } +/** + * @brief Calculate a cell temperature based on the thermistor reading. + * + * @param x The thremistor reading. + * @return int8_t Temperature in celsius. + */ +int8_t calc_cell_temp(uint16_t x) +{ + /* Polynomial fit of temperatures -7 -> 65 celsius vs. thermistor voltage. */ + return 0.6984 * pow(x, 4) + 4.4933 * pow(x, 3) - 10.278 * pow(x, 2) + + 34.184 * x + 2.7608; +} + void calc_cell_temps(acc_data_t *bmsdata) { - // TODO: DELETE THIS JUST USING FOR PROFILING HOW LONG THIS SHIT TAKES - uint32_t start = HAL_GetTick(); for (int chip = 0; chip < NUM_CHIPS; chip++) { uint8_t num_cells = get_num_cells(&bmsdata->chip_data[chip]); @@ -160,13 +171,25 @@ void calc_cell_temps(acc_data_t *bmsdata) int16_t x = bmsdata->chips[chip] .aux.a_codes[THERM_MAP[cell]]; - /* Polynomial fit of temperatures -7 -> 65 celsius vs. thermistor voltage. */ - int8_t temp = 0.6984 * pow(x, 4) + 4.4933 * pow(x, 3) - - 10.278 * pow(x, 2) + 34.184 * x + 2.7608; - bmsdata->chip_data[chip].cell_temp[cell] = temp; + bmsdata->chip_data[chip].cell_temp[cell] = + calc_cell_temp(x); + } + + // Calculate onboard therm temps + + if (!bmsdata->chip_data[chip].alpha) { + // Take average of both onboard therms + bmsdata->chip_data[chip].on_board_temp = + (calc_cell_temp(( + bmsdata->chips[chip].aux.a_codes[6])) + + calc_cell_temp( + bmsdata->chips[chip].aux.a_codes[7])) / + 2; + } else { + bmsdata->chip_data[chip].on_board_temp = calc_cell_temp( + bmsdata->chips[chip].aux.a_codes[7]); } } - printf("%ld\n", HAL_GetTick() - start); /* diff --git a/Core/Src/main.c b/Core/Src/main.c index 116447d..d0cc9a0 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -40,7 +40,7 @@ //#ifdef DEBUG_EVERYTHING //#define DEBUG_CHARGING -#define DEBUG_STATS +// #define DEBUG_STATS #define DEBUG_VOLTAGES // #define DEBUG_RAW_VOLTAGES #define DEBUG_RAW_VOLTAGES_FORMATTED @@ -380,6 +380,7 @@ int main(void) /* Messaging */ can_dispatch_handle = osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes); assert(can_dispatch_handle); + can_receive_thread = osThreadNew(vCanReceive, NULL, &can_receive_attributes); assert(can_receive_thread); @@ -1239,9 +1240,7 @@ void watchdog_pet(void) void StartDefaultTask(void *argument) { /* USER CODE BEGIN 5 */ - #ifdef DEBUG_STATS acc_data_t* bmsdata = (acc_data_t*) argument; - #endif bool alt = true; diff --git a/Core/Src/segment.c b/Core/Src/segment.c index 20d57ff..6ef3c77 100644 --- a/Core/Src/segment.c +++ b/Core/Src/segment.c @@ -34,8 +34,6 @@ extern SPI_HandleTypeDef hspi1; uint8_t therm_avg_counter = 0; -chipdata_t previous_data[NUM_CHIPS] = {}; - nertimer_t variance_timer; uint32_t pec_error_count = 0; @@ -357,9 +355,23 @@ void read_adbms_data(cell_asic chips[NUM_CHIPS], uint8_t command[2], TYPE type, chips[chip].cccrc.stat_pec + chips[chip].cccrc.comm_pec + chips[chip].cccrc.pwm_pec; if (pec_error_count > 0) { - printf("PEC COUNT: %ld\n", pec_error_count); + printf("PEC COUNT: %ld | Chip: %d | CMD: %d\n", + pec_error_count, chip, type); } + + chips[chip].cccrc.cfgr_pec = 0; + chips[chip].cccrc.sid_pec = 0; + chips[chip].cccrc.cell_pec = 0; + chips[chip].cccrc.acell_pec = 0; + chips[chip].cccrc.scell_pec = 0; + chips[chip].cccrc.fcell_pec = 0; + chips[chip].cccrc.aux_pec = 0; + chips[chip].cccrc.raux_pec = 0; + chips[chip].cccrc.stat_pec = 0; + chips[chip].cccrc.comm_pec = 0; + chips[chip].cccrc.pwm_pec = 0; } + pec_error_count = 0; } /** @@ -384,7 +396,7 @@ void segment_init(acc_data_t *bmsdata) for (int chip = 0; chip < NUM_CHIPS; chip++) { init_chip(&bmsdata->chips[chip]); // TODO: Make sure this is accurate - bmsdata->chip_data->alpha = chip % 2 == 0; + bmsdata->chip_data[chip].alpha = chip % 2 == 0; } write_config_regs(bmsdata->chips); } @@ -426,17 +438,10 @@ void get_s_adc_voltages(cell_asic chips[NUM_CHIPS]) // read_adbms_data(chip, RDSVF, S_volt, F); } -/** - * @brief Do a single shot, redundant C-ADC measurement and read - * the contents of Status Register Group C, which contains the - * CSxFLT bits indicating whether the difference between the - * C and S ADC measurements was above the CTH[2:0] set in config - * register A. - * - * @param chips Pointer to accumulator data struct. - */ void get_adc_comparison(acc_data_t *bmsdata) { + // TODO: S-ADC measurements are all over the place. + write_config_regs(bmsdata->chips); // Take single shot measurement @@ -452,7 +457,7 @@ void get_adc_comparison(acc_data_t *bmsdata) for (uint8_t cell = 0; cell < cells; cell++) { if (NER_GET_BIT(bmsdata->chips[chip].statc.cs_flt, cell)) { - printf("ADC VOLTAGE DISCREPANCY ERROR\nChip %d, Cell %d\nC-ADC: %f, S-ADC%f\n", + printf("ADC VOLTAGE DISCREPANCY ERROR\nChip %d, Cell %d\nC-ADC: %f, S-ADC: %f\n", chip + 1, cell + 1, getVoltage(bmsdata->chips[chip] .cell.c_codes[cell]), @@ -551,9 +556,9 @@ void read_aux2_registers(cell_asic chips[NUM_CHIPS]) /** * @brief Read status registers. * - * @param chips Array of chips to read voltages of. + * @param chips Array of chips to read. */ -void adBms6830_read_status_registers(cell_asic chips[NUM_CHIPS]) +void read_status_registers(cell_asic chips[NUM_CHIPS]) { write_config_regs(chips); adBms6830_Adax(AUX_OW_OFF, PUP_DOWN, AUX_ALL); @@ -566,23 +571,40 @@ void adBms6830_read_status_registers(cell_asic chips[NUM_CHIPS]) read_adbms_data(chips, RDSTATE, Status, E); } +/** + * @brief Read status and aux registers in one command. + * + * @param chips Array of chips to read. + */ +void read_status_aux_registers(cell_asic chips[NUM_CHIPS]) +{ + write_config_regs(chips); + adBms6830_Adax(AUX_OW_OFF, PUP_DOWN, AUX_ALL); + adBmsPollAdc(PLAUX1); + + read_adbms_data(chips, RDASALL, Rdasall, ALL_GRP); +} + +/** + * @brief Read the serial ID of the chip. + * + * @param chips Array of chips to read. + */ +void read_serial_id(cell_asic chips[NUM_CHIPS]) +{ + read_adbms_data(chips, RDSID, Sid, NONE); +} + void segment_retrieve_data(acc_data_t *bmsdata) { // printf("Get C adc voltages\n"); get_c_adc_voltages(bmsdata->chips); - // get_s_adc_voltages(bmsdata->chips); - // The GPIOs in the AUX registers contain voltage readings from the therms. - // printf("Get therms\n"); - read_aux_registers(bmsdata->chips); - // If you want redundant Thermistor readings, uncomment the following. - read_aux2_registers(bmsdata->chips); + read_status_aux_registers(bmsdata->chips); - /* Save the contents of the reading so that we can use it to fill in missing - * data */ - memcpy(previous_data, bmsdata->chip_data, - sizeof(chipdata_t) * NUM_CHIPS); + // If you want redundant Thermistor readings, uncomment the following. + // read_aux2_registers(bmsdata->chips); } bool segment_is_balancing(cell_asic chips[NUM_CHIPS]) diff --git a/Core/Src/shep_tasks.c b/Core/Src/shep_tasks.c index ded20e6..0438a32 100644 --- a/Core/Src/shep_tasks.c +++ b/Core/Src/shep_tasks.c @@ -111,40 +111,112 @@ void vDebugMode(void *pv_params) acc_data_t *bmsdata = (acc_data_t *)pv_params; while (69 < 420) { - for (int c = 0; c < NUM_CHIPS; c++) { + // get_adc_comparison(bmsdata); + + // read_serial_id(bmsdata->chips); + + read_aux2_registers(bmsdata->chips); + + for (int chip = 0; chip < NUM_CHIPS; chip++) { uint8_t num_cells = - get_num_cells(&bmsdata->chip_data[c]); + get_num_cells(&bmsdata->chip_data[chip]); for (int cell = 0; cell < num_cells; cell += 2) { compute_send_cell_data_message( - bmsdata->chip_data[c].alpha, + bmsdata->chip_data[chip].alpha, - bmsdata->chip_data[c].cell_temp[cell], + bmsdata->chip_data[chip].cell_temp[cell], 10000 * getVoltage( - bmsdata->chips[c] + bmsdata->chips[chip] .cell .c_codes[cell]), 10000 * getVoltage( - bmsdata->chips[c] + bmsdata->chips[chip] .cell .c_codes[cell + 1]), - c, + chip, cell, cell + 1, - (bmsdata->chips[c].tx_cfgb.dcc >> + (bmsdata->chips[chip].tx_cfgb.dcc >> cell) & 1, - (bmsdata->chips[c].tx_cfgb.dcc >> + (bmsdata->chips[chip].tx_cfgb.dcc >> (cell + 1)) & 1); osDelay(6); } + + // Send chip status messages + if (!bmsdata->chip_data[chip].alpha) { + compute_send_beta_status_a_message( + 10000 * getVoltage( + bmsdata->chip_data[chip] + .cell_temp[10]), + 10000 * getVoltage( + bmsdata->chips[chip] + .cell + .c_codes[10]), + NER_GET_BIT( + bmsdata->chips[chip].tx_cfgb.dcc, + 10), + chip, + bmsdata->chip_data[chip].on_board_temp, + (getVoltage(bmsdata->chips[chip] + .stata.itmp) / + 0.0075) - + 273, + 10000 * getVoltage( + bmsdata->chips[chip] + .raux + .ra_codes[9])); + compute_send_beta_status_b_message( + 10000 * getVoltage( + bmsdata->chips[chip] + .stata.vref2), + 10000 * getVoltage(bmsdata->chips[chip] + .statb.va), + 10000 * getVoltage(bmsdata->chips[chip] + .statb.vd), + chip, + 10000 * getVoltage(bmsdata->chips[chip] + .statb.vr4k), + 10000 * getVoltage( + bmsdata->chips[chip] + .raux + .ra_codes[8])); + } else { + compute_send_alpha_status_a_message( + bmsdata->chip_data->on_board_temp, chip, + (getVoltage(bmsdata->chips[chip] + .stata.itmp) / + 0.0075) - + 273, + 10000 * getVoltage( + bmsdata->chips[chip] + .raux + .ra_codes[9]), + 10000 * getVoltage( + bmsdata->chips[chip] + .raux + .ra_codes[8])); + compute_send_alpha_status_b_message( + 10000 * getVoltage(bmsdata->chips[chip] + .statb.vr4k), + chip, + 10000 * getVoltage( + bmsdata->chips[chip] + .stata.vref2), + 10000 * getVoltage(bmsdata->chips[chip] + .statb.va), + 10000 * getVoltage(bmsdata->chips[chip] + .statb.vd)); + } } } } \ No newline at end of file