#include "bsp/gpio.h" #include "bsp/ml5238.h" #include "app/sox/measure.h" #include "app/sox/measure_task.h" #include "libs/shark_task.h" #include "health.h" #include "state.h" #include "iostate.h" static bms_state_t _bms_state; static void _current_notify(void); static void _voltage_notify(void); static void _temperature_notify(void); void bms_state_init(void){ measure_task_init(_current_notify, _voltage_notify, _temperature_notify); io_state_init(); health_init(); } bms_state_t *bms_state_get(void){ return &_bms_state; } static debounce_t _charging_detect = { .count = 0, .max_count = 10 }; static void check_charging(){ if (bms_health()->charger_over_current || bms_health()->load_current_short) { ml5238_enable_discharger_mosfet(0); ml5238_enable_charger_mosfet(0); //disable charger mosfet _bms_state.charging = 0; return; } if (!_bms_state.charging) { if (measure_value()->load_current >= MIN_START_CHARGER_CURRENT) { _charging_detect.count ++; }else { _charging_detect.count = 0; } if (debounce_reach(&_charging_detect)){ _bms_state.charging = 1; _bms_state.discharging = 0; _charging_detect.count = 0; } }else { if (measure_value()->load_current <= MIN_START_LOADING_CURRENT) { _charging_detect.count ++; }else { _charging_detect.count = 0; } if (debounce_reach(&_charging_detect)){ _bms_state.charging = 0; _bms_state.discharging = 1; _charging_detect.count = 0; } } } static void _current_notify(void){ check_current_state(); //check health of current check_charging(); } /* 充电电流小于一定值,认为充满的时候,需要检查电芯的电压,如果发现有电芯电压过低,需要开启被动均衡 */ static void check_cell_balance(void){ } static void _voltage_notify(void){ uint16_t voltage = 0; for (int i = 0; i < CELLS_NUM; i++){ voltage += measure_value()->cell_vol[i]; } _bms_state.pack_voltage = voltage; check_voltage_state(); //check health of cell voltage check_cell_balance(); } static void _temperature_notify(void){ check_temp_state(); //check health of cell/pcb temperature }