diff --git a/Core/Inc/bmsConfig.h b/Core/Inc/bmsConfig.h index b070d58..24dd17f 100644 --- a/Core/Inc/bmsConfig.h +++ b/Core/Inc/bmsConfig.h @@ -4,7 +4,7 @@ // Hardware definition #define NUM_SEGMENTS 6 #define NUM_CHIPS NUM_SEGMENTS* 2 -#define NUM_CELLS_PER_CHIP 10 +#define NUM_CELLS_PER_CHIP 9 #define NUM_THERMS_PER_CHIP 32 #define NUM_RELEVANT_THERMS 5 diff --git a/Core/Src/main.c b/Core/Src/main.c index f2f2b68..298e73e 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -267,9 +267,8 @@ int main(void) * Not state specific */ segment_retrieve_data(acc_data->chip_data); - acc_data->pack_current = (int16_t)compute_get_pack_current(); - //volatile float temp = compute_get_pack_current(); - printf("Pack Current: %d\r\n", acc_data->pack_current); + acc_data->pack_current = compute_get_pack_current(); + @@ -286,7 +285,7 @@ int main(void) HAL_GPIO_WritePin(Watchdog_Out_GPIO_Port, Watchdog_Out_Pin, GPIO_PIN_RESET); #ifdef DEBUG_STATS - //print_bms_stats(acc_data); + print_bms_stats(acc_data); #endif // TODO - possibly optimize timing, every loop might be excessive diff --git a/Core/Src/segment.c b/Core/Src/segment.c index 283865e..27f3962 100644 --- a/Core/Src/segment.c +++ b/Core/Src/segment.c @@ -102,6 +102,10 @@ int pull_voltages() * just copy over the contents of the last good reading and the fault status * from the most recent attempt */ + + /* our segments are mapped backwards and in pairs, so they are read in 1,0 then 3,2, etc*/ + int mapping_correction[12] = {1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10}; + if (!is_timer_expired(&voltage_reading_timer) && voltage_reading_timer.active) { for (uint8_t i = 0; i < NUM_CHIPS; i++) { memcpy(segment_data[i].voltage_reading, previous_data[i].voltage_reading, @@ -121,31 +125,41 @@ int pull_voltages() */ if (LTC6804_rdcv(ltc68041, 0, NUM_CHIPS, segment_voltages) == -1) { for (uint8_t i = 0; i < NUM_CHIPS; i++) { - memcpy(segment_data[i].voltage_reading, previous_data[i].voltage_reading, - sizeof(segment_data[i].voltage_reading)); + int corrected_index = mapping_correction[i]; + memcpy(segment_data[corrected_index].voltage_reading, previous_data[i].voltage_reading, + sizeof(segment_data[corrected_index].voltage_reading)); } return 1; } /* If the read was successful, copy the voltage data */ for (uint8_t i = 0; i < NUM_CHIPS; i++) { - for (uint8_t j = 0; j < NUM_CELLS_PER_CHIP; j++) { - if (abs(segment_voltages[i][j] - previous_data[i].voltage_reading[j]) + int corrected_index = mapping_correction[i]; + /* correction to account for missing index, see more info below */ + int dest_index = 0; + for (uint8_t j = 0; j < NUM_CELLS_PER_CHIP + 1; j++) { + + + if (abs(segment_voltages[i][dest_index] - previous_data[i].voltage_reading[dest_index]) > MAX_VOLT_DELTA) { - segment_data[i].voltage_reading[j] = previous_data[i].voltage_reading[j]; - segment_data[i].bad_volt_diff_count[j]++; + segment_data[corrected_index].voltage_reading[dest_index] = previous_data[i].voltage_reading[dest_index]; + segment_data[corrected_index].bad_volt_diff_count[dest_index]++; - if (segment_data[i].bad_volt_diff_count[j] > MAX_VOLT_DELTA_COUNT) { - segment_data[i].bad_volt_diff_count[j] = 0; - segment_data[i].voltage_reading[j] = segment_voltages[i][j]; + if (segment_data[corrected_index].bad_volt_diff_count[dest_index] > MAX_VOLT_DELTA_COUNT) { + segment_data[corrected_index].bad_volt_diff_count[dest_index] = 0; + segment_data[corrected_index].voltage_reading[dest_index] = segment_voltages[i][j]; } } else { - segment_data[i].bad_volt_diff_count[j] = 0; - segment_data[i].voltage_reading[j] = segment_voltages[i][j]; + segment_data[corrected_index].bad_volt_diff_count[dest_index] = 0; + segment_data[corrected_index].voltage_reading[dest_index] = segment_voltages[i][dest_index]; } } + + /* cell 6 on every chip is not a real reading, we need to have the array skip this */ + if (dest_index != 5) dest_index++; } + /* Start the timer between readings if successful */ start_timer(&voltage_reading_timer, VOLTAGE_WAIT_TIME); return 0;