Skip to content

Commit

Permalink
adbms debug mode stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Jan 18, 2025
1 parent eda79ab commit 0c881b1
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 45 deletions.
3 changes: 3 additions & 0 deletions Core/Inc/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
25 changes: 25 additions & 0 deletions Core/Inc/segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 30 additions & 7 deletions Core/Src/analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,47 @@ 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]);

for (int cell = 0; cell < num_cells; cell++) {
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);

/*
Expand Down
5 changes: 2 additions & 3 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
74 changes: 48 additions & 26 deletions Core/Src/segment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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]),
Expand Down Expand Up @@ -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);
Expand 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])
Expand Down
90 changes: 81 additions & 9 deletions Core/Src/shep_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}

0 comments on commit 0c881b1

Please sign in to comment.