Skip to content

Commit

Permalink
fix: increment sensing interval
Browse files Browse the repository at this point in the history
  • Loading branch information
federico-carbone committed Aug 6, 2024
1 parent 911152a commit 406b8c0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 23 deletions.
9 changes: 9 additions & 0 deletions Core/Inc/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ typedef enum {
ERRORS_UNDERVOLTAGE,
ERRORS_OVERVOLTAGE,
ERRORS_OVERTEMPERATURE,
ERRORS_UNDERTEMPERATURE,
ERRORS_NUM
} ERRORS_TypeDef;

void error_init(void);

void error_timerElapsed_irq(TIM_HandleTypeDef *htim);

void error_set_undervoltage(uint8_t index);
void error_set_overvoltage(uint8_t index);
void error_set_overtemp(uint8_t index);
void error_set_undertemp(uint8_t index);

void error_reset_undervoltage(uint8_t index);
void error_reset_overvoltage(uint8_t index);
void error_reset_overtemp(uint8_t index);
void error_reset_undertemp(uint8_t index);

uint8_t error_get_overvoltage(uint8_t index);
uint8_t error_get_undervoltage(uint8_t index);
uint8_t error_get_overtemp(uint8_t index);
uint8_t error_get_undertemp(uint8_t index);

#endif // ERRORS_H
6 changes: 3 additions & 3 deletions Core/Src/can_send_timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ void can_send_timebase_init(void) {

TIMEBASE_init(&can_send_timebase_handle, &htim10, 1000);

TIMEBASE_add_interval(&can_send_timebase_handle, 1000, &interval);
TIMEBASE_add_interval(&can_send_timebase_handle, 10000, &interval);
TIMEBASE_register_callback(&can_send_timebase_handle, interval, battery_pack_general_cb);

TIMEBASE_add_interval(&can_send_timebase_handle, 200000, &interval);
TIMEBASE_register_callback(&can_send_timebase_handle, interval, cell_voltage_cb);
TIMEBASE_register_callback(&can_send_timebase_handle, interval, status_cb);

TIMEBASE_add_interval(&can_send_timebase_handle, 20000, &interval);
TIMEBASE_register_callback(&can_send_timebase_handle, interval, temp_cb);
}

Expand Down
28 changes: 14 additions & 14 deletions Core/Src/can_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ void can_send_msg(uint32_t id) {
tx_header.DLC = mcb_bmslv_helo_pack(buffer, &msgs.bmslv_helo, MCB_BMSLV_HELO_LENGTH);
break;
case MCB_BMSLV_TEMP1_FRAME_ID:
msgs.bmslv_temp2.ntc1_resistance = mcb_bmslv_temp2_ntc1_resistance_encode(ntc_get_ext_resistance(0));
msgs.bmslv_temp2.ntc2_resistance = mcb_bmslv_temp2_ntc2_resistance_encode(ntc_get_ext_resistance(1));
msgs.bmslv_temp2.ntc3_resistance = mcb_bmslv_temp2_ntc3_resistance_encode(ntc_get_ext_resistance(2));
msgs.bmslv_temp2.ntc4_resistance = mcb_bmslv_temp2_ntc4_resistance_encode(ntc_get_ext_resistance(3));
msgs.bmslv_temp2.ntc5_resistance = mcb_bmslv_temp2_ntc5_resistance_encode(ntc_get_ext_resistance(4));
msgs.bmslv_temp2.ntc6_resistance = mcb_bmslv_temp2_ntc6_resistance_encode(ntc_get_ext_resistance(5));
msgs.bmslv_temp1.ntc1_resistance = mcb_bmslv_temp1_ntc1_resistance_encode(ntc_get_ext_temp(0)*1000);
msgs.bmslv_temp1.ntc2_resistance = mcb_bmslv_temp1_ntc2_resistance_encode(ntc_get_ext_temp(1)*1000);
msgs.bmslv_temp1.ntc3_resistance = mcb_bmslv_temp1_ntc3_resistance_encode(ntc_get_ext_temp(2)*1000);
msgs.bmslv_temp1.ntc4_resistance = mcb_bmslv_temp1_ntc4_resistance_encode(ntc_get_ext_temp(3)*1000);
msgs.bmslv_temp1.ntc5_resistance = mcb_bmslv_temp1_ntc5_resistance_encode(ntc_get_ext_temp(4)*1000);
msgs.bmslv_temp1.ntc6_resistance = mcb_bmslv_temp1_ntc6_resistance_encode(ntc_get_ext_temp(5)*1000);

tx_header.DLC = mcb_bmslv_temp1_pack(buffer, &msgs.bmslv_temp1, MCB_BMSLV_TEMP1_LENGTH);
break;
case MCB_BMSLV_TEMP2_FRAME_ID:
msgs.bmslv_temp2.ntc1_resistance = mcb_bmslv_temp2_ntc1_resistance_encode(ntc_get_ext_resistance(6));
msgs.bmslv_temp2.ntc2_resistance = mcb_bmslv_temp2_ntc2_resistance_encode(ntc_get_int_resistance(0));
msgs.bmslv_temp2.ntc3_resistance = mcb_bmslv_temp2_ntc3_resistance_encode(ntc_get_int_resistance(1));
msgs.bmslv_temp2.ntc4_resistance = mcb_bmslv_temp2_ntc4_resistance_encode(ntc_get_int_resistance(2));
msgs.bmslv_temp2.ntc5_resistance = mcb_bmslv_temp2_ntc5_resistance_encode(ntc_get_int_resistance(3));
msgs.bmslv_temp2.ntc6_resistance = mcb_bmslv_temp2_ntc6_resistance_encode(ntc_get_int_resistance(4));
msgs.bmslv_temp2.ntc1_resistance = mcb_bmslv_temp2_ntc1_resistance_encode(ntc_get_ext_temp(6)*1000);
msgs.bmslv_temp2.ntc2_resistance = mcb_bmslv_temp2_ntc2_resistance_encode(ntc_get_int_temp(0)*1000);
msgs.bmslv_temp2.ntc3_resistance = mcb_bmslv_temp2_ntc3_resistance_encode(ntc_get_int_temp(1)*1000);
msgs.bmslv_temp2.ntc4_resistance = mcb_bmslv_temp2_ntc4_resistance_encode(ntc_get_int_temp(2)*1000);
msgs.bmslv_temp2.ntc5_resistance = mcb_bmslv_temp2_ntc5_resistance_encode(ntc_get_int_temp(3)*1000);
msgs.bmslv_temp2.ntc6_resistance = mcb_bmslv_temp2_ntc6_resistance_encode(ntc_get_int_temp(4)*1000);

tx_header.DLC = mcb_bmslv_temp2_pack(buffer, &msgs.bmslv_temp2, MCB_BMSLV_TEMP2_LENGTH);
break;
Expand Down Expand Up @@ -166,13 +166,13 @@ void can_send_msg(uint32_t id) {
tx_header.DLC = mcb_bmslv_status_pack(buffer, &msgs.bmslv_status, MCB_BMSLV_STATUS_LENGTH);
break;
default:
break;
return;
}
can_send(&hcan1, buffer, &tx_header);
}

void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) {
logger_log(LOGGER_ERROR, "sborato");
// logger_log(LOGGER_ERROR, "sborato");
}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
Expand Down
16 changes: 12 additions & 4 deletions Core/Src/data_reading_timebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ STMLIBS_StatusTypeDef data_reading_l9963e_cb(void) {

cells = L9963E_utils_get_cells(&cells_n);

for(uint8_t i=0; i<cells_n; ++i) {
for(uint8_t i=2; i<5; ++i) {
if(cells[i] > 4.2 / 0.000089)
error_set_overvoltage(i);
else
Expand All @@ -37,14 +37,22 @@ STMLIBS_StatusTypeDef data_reading_l9963e_cb(void) {
for(uint8_t i=0; i<NTC_INT_ADC_N; ++i) {
if(ntc_get_int_temp(i) > 80)
error_set_overtemp(i);
else
else if(ntc_get_int_temp(i) < -20)
error_set_undertemp(i);
else{
error_reset_overtemp(i);
error_reset_undertemp(i);
}
}
for(uint8_t i=0; i<NTC_EXT_ADC_N; ++i) {
if(ntc_get_ext_temp(i) > 80)
error_set_overtemp(NTC_EXT_ADC_N+i);
else
else if(ntc_get_ext_temp(i) < -20)
error_set_undertemp(NTC_EXT_ADC_N+i);
else {
error_reset_overtemp(NTC_EXT_ADC_N+i);
error_reset_undertemp(NTC_EXT_ADC_N+i);
}
}
}

Expand All @@ -56,7 +64,7 @@ void data_reading_timebase_init(void) {

TIMEBASE_init(&data_reading_timebase_handle, &htim6, 1000);

TIMEBASE_add_interval(&data_reading_timebase_handle, 1000, &interval);
TIMEBASE_add_interval(&data_reading_timebase_handle, 10000, &interval);
TIMEBASE_register_callback(&data_reading_timebase_handle, interval, data_reading_l9963e_cb);
}

Expand Down
21 changes: 21 additions & 0 deletions Core/Src/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ void overvoltage_exp_cb(uint8_t error_index, uint8_t instance_index);
void overvoltage_toggle_cb(uint8_t error_index, uint8_t instance_index);
void overtemp_exp_cb(uint8_t error_index, uint8_t instance_index);
void overtemp_toggle_cb(uint8_t error_index, uint8_t instance_index);
void undertemp_exp_cb(uint8_t error_index, uint8_t instance_index);
void undertemp_toggle_cb(uint8_t error_index, uint8_t instance_index);

ERROR_UTILS_HandleTypeDef error_h;
ERROR_UTILS_ErrorInstanceTypeDef undervoltage_instances[7];
ERROR_UTILS_ErrorInstanceTypeDef overvoltage_instances[7];
ERROR_UTILS_ErrorInstanceTypeDef overtemp_instances[12];
ERROR_UTILS_ErrorInstanceTypeDef undertemp_instances[12];

ERROR_UTILS_ErrorTypeDef errors[] = {
[ERRORS_UNDERVOLTAGE] = {.expiry_callback = undervoltage_exp_cb, .expiry_delay_ms = 500, .instances = undervoltage_instances, .instances_length = 7, .toggle_callback = undervoltage_toggle_cb},
[ERRORS_OVERVOLTAGE] = {.expiry_callback = overvoltage_exp_cb, .expiry_delay_ms = 500, .instances = overvoltage_instances, .instances_length = 7, .toggle_callback = overvoltage_toggle_cb},
[ERRORS_OVERTEMPERATURE] = {.expiry_callback = overtemp_exp_cb, .expiry_delay_ms = 1000, .instances = overtemp_instances, .instances_length = 12, .toggle_callback = overtemp_toggle_cb},
[ERRORS_UNDERTEMPERATURE] = {.expiry_callback = undertemp_exp_cb, .expiry_delay_ms = 1000, .instances = undertemp_instances, .instances_length = 12, .toggle_callback = undertemp_toggle_cb},
};
ERROR_UTILS_ConfigTypeDef config = {.errors_array = errors, .errors_length = ERRORS_NUM};

Expand Down Expand Up @@ -54,6 +58,14 @@ uint8_t error_get_overtemp(uint8_t index) {
return ERROR_UTILS_is_set(&error_h, ERRORS_OVERTEMPERATURE, index);
}

void error_set_undertemp(uint8_t index) {
ERROR_UTILS_error_set(&error_h, ERRORS_UNDERTEMPERATURE, index);
}

uint8_t error_get_undertemp(uint8_t index) {
return ERROR_UTILS_is_set(&error_h, ERRORS_UNDERTEMPERATURE, index);
}

void error_reset_undervoltage(uint8_t index) {
ERROR_UTILS_error_reset(&error_h, ERRORS_UNDERVOLTAGE, index);
}
Expand All @@ -66,6 +78,10 @@ void error_reset_overtemp(uint8_t index) {
ERROR_UTILS_error_reset(&error_h, ERRORS_OVERTEMPERATURE, index);
}

void error_reset_undertemp(uint8_t index) {
ERROR_UTILS_error_reset(&error_h, ERRORS_UNDERTEMPERATURE, index);
}

void global_exp_cb(uint8_t error_index, uint8_t instance_index) {
can_send_msg(MCB_BMSLV_STATUS_FRAME_ID);
HAL_GPIO_WritePin(LV_CMD_GPIO_OUT_GPIO_Port, LV_CMD_GPIO_OUT_Pin, GPIO_PIN_RESET);
Expand All @@ -89,6 +105,11 @@ void overtemp_exp_cb(uint8_t error_index, uint8_t instance_index) {
}
void overtemp_toggle_cb(uint8_t error_index, uint8_t instance_index) {
}
void undertemp_exp_cb(uint8_t error_index, uint8_t instance_index) {
logger_log(LOGGER_ERROR, "undertemp on temp %d", instance_index);
}
void undertemp_toggle_cb(uint8_t error_index, uint8_t instance_index) {
}


void error_timerElapsed_irq(TIM_HandleTypeDef *htim) {
Expand Down
4 changes: 4 additions & 0 deletions Core/Src/lem.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ void lem_new_value(uint16_t val) {
}

current = LEM_EXP_SMOOTH_ALPHA*val + (1-LEM_EXP_SMOOTH_ALPHA)*current;

if(lem_get_current_ampere() > 50) {
HAL_GPIO_WritePin(LV_CMD_GPIO_OUT_GPIO_Port, LV_CMD_GPIO_OUT_Pin, GPIO_PIN_RESET);
}
}

void lem_adc_callback(ADC_HandleTypeDef *hadc) {
Expand Down
2 changes: 1 addition & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int main(void)
data_reading_timebase_routine();
can_send_timebase_routine();

logger_routine();
// logger_routine();

// HAL_Delay(500);

Expand Down
1 change: 0 additions & 1 deletion STM32Make.make
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))

# list of ASM program objects
# list of ASM program objects
UPPER_CASE_ASM_SOURCES = $(filter %.S,$(ASM_SOURCES))
LOWER_CASE_ASM_SOURCES = $(filter %.s,$(ASM_SOURCES))
Expand Down

0 comments on commit 406b8c0

Please sign in to comment.