Skip to content

Commit

Permalink
Therm averaging algo
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Apr 27, 2024
1 parent 6b341c8 commit debad16
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
81 changes: 45 additions & 36 deletions Core/Src/analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ const uint8_t RELEVANT_THERM_MAP_L[NUM_CELLS_PER_CHIP][NUM_RELEVANT_THERMS] =
{6 + MUX_OFFSET, 8 + MUX_OFFSET, NO_THERM},
};

uint8_t THERM_DISABLE[NUM_CHIPS][NUM_THERMS_PER_CHIP] =
uint8_t THERM_DISABLE[NUM_CHIPS][NUM_THERMS_PER_CHIP] =
{
{1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1 ,1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 ,1, 0, 0, 0, 0, 0, 0, 1 ,0},
{1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0},
Expand Down Expand Up @@ -193,47 +193,56 @@ void calc_state_of_charge();

void calc_cell_temps()
{
static uint8_t num_samples = 5; /* Samples for LPF */

for (uint8_t c = 0; c < NUM_CHIPS; c++) {
//const uint8_t (*therm_map)[NUM_RELEVANT_THERMS] = (c % 2 == 0) ? RELEVANT_THERM_MAP_L : RELEVANT_THERM_MAP_H;
uint16_t therm_sum = 0;
uint8_t therm_count = 0;
int16_t therm_sum = 0;
uint8_t therm_count = 0;

for (uint8_t therm = 0; therm < NUM_THERMS_PER_CHIP; therm++) {

if (THERM_DISABLE[c][therm]) continue;
for (uint8_t therm = 0; therm < NUM_THERMS_PER_CHIP; therm++) {

therm_sum += bmsdata->chip_data[c].thermistor_value[therm];
therm_count++;
}

uint8_t temp_avg = therm_sum / therm_count;
for (uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) {
if (THERM_DISABLE[c][therm]) continue;

/* Takes the average temperature of all the relevant thermistors */
bmsdata->chip_data[c].cell_temp[cell] = temp_avg;
therm_sum += bmsdata->chip_data[c].thermistor_value[therm];
therm_count++;
}

/* Cleansing value */
if (bmsdata->chip_data[c].cell_temp[cell] > MAX_TEMP) {
bmsdata->chip_data[c].cell_temp[cell] = MAX_TEMP;
}
int8_t temp_avg = therm_sum / therm_count;

for (uint8_t therm = 0; therm < NUM_THERMS_PER_CHIP; therm++) {
bmsdata->chip_data[c].thermistor_value[therm] = temp_avg;
bmsdata->chip_data[c].thermistor_value[therm] = (int8_t)((((int64_t)prevbmsdata->chip_data[c].thermistor_value[therm] * (num_samples - 1))
+ bmsdata->chip_data[c].thermistor_value[therm]) / num_samples);
}

for (uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) {

/* Takes the average temperature of all the relevant thermistors */
bmsdata->chip_data[c].cell_temp[cell] = bmsdata->chip_data[c].thermistor_value[0]; /* Note, all temp readings are the same at this point */

/* Low Pass Filter */


/* Cleansing value */
if (bmsdata->chip_data[c].cell_temp[cell] > MAX_TEMP) {
bmsdata->chip_data[c].cell_temp[cell] = MAX_TEMP;
}
// uint8_t therm_count = 0;
// int temp_sum = 0;
// for (uint8_t therm = 0; therm < NUM_RELEVANT_THERMS; therm++) {
// uint8_t thermNum = therm_map[cell][therm];

// if (thermNum != NO_THERM) {
// temp_sum += bmsdata->chip_data[c].thermistor_value[thermNum];
// therm_count++;
// }
// }

// /* Takes the average temperature of all the relevant thermistors */
// bmsdata->chip_data[c].cell_temp[cell] = temp_sum / therm_count;
// therm_count = 0;



}
// uint8_t therm_count = 0;
// int temp_sum = 0;
// for (uint8_t therm = 0; therm < NUM_RELEVANT_THERMS; therm++) {
// uint8_t thermNum = therm_map[cell][therm];

// if (thermNum != NO_THERM) {
// temp_sum += bmsdata->chip_data[c].thermistor_value[thermNum];
// therm_count++;
// }
// }

// /* Takes the average temperature of all the relevant thermistors */
// bmsdata->chip_data[c].cell_temp[cell] = temp_sum / therm_count;
// therm_count = 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const void print_bms_stats(acc_data_t *acc_data)
for (uint8_t cell = 0; cell < NUM_THERMS_PER_CHIP; cell++) {

if (THERM_DISABLE[c][cell]) continue;
printf("%d ", acc_data->chip_data[c].thermistor_reading[cell]);
printf("%d ", acc_data->chip_data[c].thermistor_value[cell]);
}

printf("\r\n");
Expand Down
2 changes: 0 additions & 2 deletions Core/Src/segment.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ int pull_thermistors()
segment_data[corrected_index].thermistor_value[therm + 15]
= segment_data[corrected_index].thermistor_reading[therm + 15];



if (raw_temp_voltages[c][0] == LTC_BAD_READ
|| raw_temp_voltages[c][1] == LTC_BAD_READ) {
memcpy(segment_data[corrected_index].thermistor_reading, previous_data[c].thermistor_reading,
Expand Down

0 comments on commit debad16

Please sign in to comment.