From c64b80342c640993b982ab826a58b9da834bf3e9 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Tue, 9 Jan 2024 19:45:46 -0500 Subject: [PATCH] i hate everything --- Core/Src/analyzer.c | 2 +- Core/Src/main.c | 45 +++++++++++++++++++++-------------------- Core/Src/segment.c | 13 ++++++------ Core/Src/stateMachine.c | 8 +++----- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Core/Src/analyzer.c b/Core/Src/analyzer.c index 21049ef..183803b 100644 --- a/Core/Src/analyzer.c +++ b/Core/Src/analyzer.c @@ -400,7 +400,7 @@ void calc_open_cell_voltage() /* If we are within the current threshold for open voltage measurments */ else if (bmsdata->pack_current < (OCV_CURR_THRESH * 10) && bmsdata->pack_current > (-OCV_CURR_THRESH * 10)) { - if (is_timer_expired(&ocvTimer)) { + if (is_timer_expired(&ocvTimer) || !is_timer_active(&ocvTimer)) { for (uint8_t chip = 0; chip < NUM_CHIPS; chip++) { for (uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) { /* Sets open cell voltage to a moving average of OCV_AVG values */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 275a85d..cd9f828 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -108,34 +108,35 @@ int _write(int file, char* ptr, int len) { const void print_bms_stats(acc_data_t *acc_data) { + static nertimer_t debug_stat_timer; static const uint16_t PRINT_STAT_WAIT = 500; //ms - if(!is_timer_expired(&debug_stat_timer)) return; - + if(!is_timer_expired(&debug_stat_timer) && debug_stat_timer.active) return; + HAL_UART_Transmit(&huart4, (char*)"butts", 5, 1000); //TODO get this from eeprom once implemented // question - should we read from eeprom here, or do that on loop and store locally? //printf("Prev Fault: %#x", previousFault); - printf("Current: %f\n", (float)(acc_data->pack_current) / 10.0); - printf("Min, Max, Avg Temps: %ld, %ld, %d\n", acc_data->min_temp.val, acc_data->max_temp.val, acc_data->avg_temp); + printf("Current: %f\r\n", (float)(acc_data->pack_current) / 10.0); + printf("Min, Max, Avg Temps: %ld, %ld, %d\r\n", acc_data->min_temp.val, acc_data->max_temp.val, acc_data->avg_temp); printf("Min, Max, Avg, Delta Voltages: %ld, %ld, %d, %d\n", acc_data->min_voltage.val, acc_data->max_voltage.val, acc_data->avg_voltage, acc_data->delt_voltage); - printf("DCL: %d\n", acc_data->discharge_limit); - printf("CCL: %d\n", acc_data->charge_limit); - printf("SoC: %d\n", acc_data->soc); - printf("Is Balancing?: %d\n", segment_is_balancing()); + printf("DCL: %d\r\n", acc_data->discharge_limit); + printf("CCL: %d\r\n", acc_data->charge_limit); + printf("SoC: %d\r\n", acc_data->soc); + printf("Is Balancing?: %d\r\n", segment_is_balancing()); printf("State: "); - if (current_state == 0) printf("BOOT\n"); - else if (current_state == 1) printf("READY\n"); - else if (current_state == 2) printf("CHARGING\n"); - else if (current_state == 1) printf("FAULTED\n"); - printf("Raw Cell Voltage:\n"); + if (current_state == 0) printf("BOOT\r\n"); + else if (current_state == 1) printf("READY\r\n"); + else if (current_state == 2) printf("CHARGING\r\n"); + else if (current_state == 1) printf("FAULTED\r\n"); + printf("Raw Cell Voltage:\r\n"); for(uint8_t c = 0; c < NUM_CHIPS; c++) { for(uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) { printf("%d\t", acc_data->chip_data[c].voltage_reading[cell]); } - printf("\n"); + printf("\r\n"); } printf("Open Cell Voltage:\n"); @@ -145,27 +146,27 @@ const void print_bms_stats(acc_data_t *acc_data) { printf("%d\t", acc_data->chip_data[c].open_cell_voltage[cell]); } - printf("\n"); + printf("\r\n"); } - printf("Cell Temps:\n"); + printf("Cell Temps:\r\n"); for(uint8_t c = 0; c < NUM_CHIPS; c++) { for(uint8_t cell = 17; cell < 28; cell++) { printf("%d\t", acc_data->chip_data[c].thermistor_reading[cell]); } - printf("\n"); + printf("\r\n"); } - printf("Avg Cell Temps:\n"); + printf("Avg Cell Temps:\r\n"); for(uint8_t c = 0; c < NUM_CHIPS; c++) { for(uint8_t cell = 17; cell < 28; cell++) { printf("%d\t", acc_data->chip_data[c].thermistor_value[cell]); } - printf("\n"); + printf("\r\n"); } start_timer(&debug_stat_timer, PRINT_STAT_WAIT); @@ -213,7 +214,7 @@ int main(void) MX_I2C1_Init(); /* USER CODE BEGIN 2 */ - // segment_init(); + segment_init(); /* USER CODE END 2 */ /* Infinite loop */ @@ -241,8 +242,8 @@ int main(void) sm_handle_state(acc_data); /* check for inbound CAN */ - get_can1_msg(); - get_can2_msg(); + // get_can1_msg(); + // get_can2_msg(); #ifdef DEBUG_STATS diff --git a/Core/Src/segment.c b/Core/Src/segment.c index d7c3f38..c960e9b 100644 --- a/Core/Src/segment.c +++ b/Core/Src/segment.c @@ -53,11 +53,12 @@ void push_chip_configuration() { LTC6804_wrcfg(ltc68041, NUM_CHIPS, local_config void segment_init() { - //printf("Initializing Segments..."); + printf("Initializing Segments..."); - ltc68041 = LTC6804_initialize(&hspi1, GPIOA, 4); + ltc68041 = malloc(sizeof(ltc_config)); + LTC6804_initialize(ltc68041, &hspi1, GPIOA, 4); - // pull_chip_configuration(); + pull_chip_configuration(); for (int c = 0; c < NUM_CHIPS; c++) { local_config[c][0] = 0xF8; @@ -176,7 +177,7 @@ int pull_voltages() * just copy over the contents of the last good reading and the fault status * from the most recent attempt */ - if (!is_timer_expired(&voltage_reading_timer)) { + 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, sizeof(segment_data[i].voltage_reading)); @@ -331,13 +332,13 @@ void segment_enable_balancing(bool balance_enable) configure_discharge(c, DICHARGE_ALL_COMMAND); discharge_commands[c] = DICHARGE_ALL_COMMAND; } - //push_chip_configuration(); + push_chip_configuration(); } else { for (int c = 0; c < NUM_CHIPS; c++) { configure_discharge(c, 0); discharge_commands[c] = 0; } - //push_chip_configuration(); + push_chip_configuration(); } } diff --git a/Core/Src/stateMachine.c b/Core/Src/stateMachine.c index 231eed8..94eb636 100644 --- a/Core/Src/stateMachine.c +++ b/Core/Src/stateMachine.c @@ -113,7 +113,7 @@ void handle_charging(acc_data_t* bmsdata) } /* Send CAN message, but not too often */ - if (is_timer_expired(&charger_message_timer)) { + if (is_timer_expired(&charger_message_timer) || !is_timer_active(&charger_message_timer)) { compute_send_charging_message( (MAX_CHARGE_VOLT * NUM_CELLS_PER_CHIP * NUM_CHIPS), bmsdata); start_timer(&charger_message_timer, CHARGE_MESSAGE_WAIT); @@ -174,11 +174,9 @@ void sm_handle_state(acc_data_t* bmsdata) state_test[0] = current_state + '0'; - HAL_UART_Transmit(&huart4, (char*)state_test, 1, 1000); - /* send relevant CAN msgs */ // clang-format off - if (is_timer_expired(&can_msg_timer)) + if (is_timer_expired(&can_msg_timer) || !is_timer_active(&can_msg_timer)) { compute_send_acc_status_message(bmsdata); compute_send_current_message(bmsdata); @@ -297,7 +295,7 @@ bool sm_charging_check(acc_data_t* bmsdata) { if (!compute_charger_connected()) return false; - if (!is_timer_expired(&charge_timeout)) + if (!is_timer_expired(&charge_timeout) && is_timer_active(&charge_timeout)) return false; if (bmsdata->max_voltage.val >= (MAX_CHARGE_VOLT * 10000)