|
@@ -0,0 +1,257 @@
|
|
|
|
|
+#include "health.h"
|
|
|
|
|
+#include "bsp/ml5238.h"
|
|
|
|
|
+#include "state.h"
|
|
|
|
|
+#include "iostate.h"
|
|
|
|
|
+#include "measure_task.h"
|
|
|
|
|
+
|
|
|
|
|
+#define MAX_CURRENT_FOR_CHARGER (20*1000) //最大充电电流20A
|
|
|
|
|
+#define MIN_VOLTAGE_FOR_DISCHARGER (39 * 1000) //允许能放电的最小电压
|
|
|
|
|
+#define MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER (40 * 1000) //恢复放电的最小电压
|
|
|
|
|
+#define MIN_VOLTAGE_FOR_POWER_DOWN (32 * 1000)
|
|
|
|
|
+static int8_t charger_normal_low_temp[PACK_TEMPS_NUM] = {0,0,0,0}; //正常的充电最低温度
|
|
|
|
|
+static int8_t charger_normal_high_temp[PACK_TEMPS_NUM] = {50,50,50,55}; //正常的充电最高温度
|
|
|
|
|
+static int8_t charger_lower_low_temp[PACK_TEMPS_NUM] = {-5,-5,-5,0}; //需要停止充电的最低温度
|
|
|
|
|
+static int8_t charger_higher_high_temp[PACK_TEMPS_NUM] = {55,55,55,60}; //需要停止充电的最高温度
|
|
|
|
|
+
|
|
|
|
|
+static int8_t discharger_normal_low_temp[PACK_TEMPS_NUM] = {-10,-10,-10,-5};//正常的放电最低温度
|
|
|
|
|
+static int8_t discharger_normal_high_temp[PACK_TEMPS_NUM] = {50,50,50,55};//正常的放电最高温度
|
|
|
|
|
+static int8_t discharger_lower_low_temp[PACK_TEMPS_NUM] = {-15,-15,-15,-10}; //需要停止放电的最低温度
|
|
|
|
|
+static int8_t discharger_higher_high_temp[PACK_TEMPS_NUM] = {55,55,55,60};//需要停止放电的最高温度
|
|
|
|
|
+
|
|
|
|
|
+/* health 模块,只检测状态,不做任何控制,如果有异常情况,控制中心会统一处理 */
|
|
|
|
|
+static void check_ml5238_state(int event);
|
|
|
|
|
+static void init_detect_timer(void);
|
|
|
|
|
+static void load_delect_handler(shark_timer_t *timer);
|
|
|
|
|
+static void charger_detect_handler(shark_timer_t *timer);
|
|
|
|
|
+
|
|
|
|
|
+static bms_health_t _health;
|
|
|
|
|
+static debounce_timer_t _load_detect_timer;
|
|
|
|
|
+static debounce_timer_t _charger_detect_timer;
|
|
|
|
|
+void health_init(void){
|
|
|
|
|
+ /* 5238如果有异常情况,比如短路,负载移除,通过这个handler上报 */
|
|
|
|
|
+ ml5238_register_notify_handler(check_ml5238_state);
|
|
|
|
|
+ init_detect_timer();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bms_health_t *bms_health(){
|
|
|
|
|
+ return &_health;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void init_detect_timer(void){
|
|
|
|
|
+ _load_detect_timer._timer.handler = load_delect_handler;
|
|
|
|
|
+ _load_detect_timer.max_count = 100;
|
|
|
|
|
+ _load_detect_timer.interval = 10;
|
|
|
|
|
+
|
|
|
|
|
+ _charger_detect_timer._timer.handler = charger_detect_handler;
|
|
|
|
|
+ _charger_detect_timer.max_count = 100;
|
|
|
|
|
+ _charger_detect_timer.interval = 10;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void load_delect_handler(shark_timer_t *timer){
|
|
|
|
|
+ if (ml5238_is_load_disconnect()){
|
|
|
|
|
+ _load_detect_timer.count ++;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ _load_detect_timer.count = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (_load_detect_timer.count >= _load_detect_timer.max_count) {
|
|
|
|
|
+ _health.load_current_short = 0; //负载移除,clear load current short
|
|
|
|
|
+ ml5238_enable_load_detect(0);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void charger_detect_handler(shark_timer_t *timer){
|
|
|
|
|
+ if (!io_state()->charger_detect) {
|
|
|
|
|
+ _charger_detect_timer.count ++;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ _charger_detect_timer.count = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (_charger_detect_timer.count >= _charger_detect_timer.max_count){
|
|
|
|
|
+ _health.charger_over_current = 0;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void check_ml5238_state(int event){
|
|
|
|
|
+ if (event == ML5238_Event_Charger_Over_Current){
|
|
|
|
|
+ shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
|
|
|
|
|
+ }else if (event == ML5238_Event_Short_Current) { //ml5238触发短路保护,充放电mos全部关闭
|
|
|
|
|
+ _health.load_current_short = 1;
|
|
|
|
|
+ ml5238_enable_load_detect(1); //打开负载检测
|
|
|
|
|
+ shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
|
|
|
|
|
+ }else if (event == ML5238_Event_Load_Disconnect) {
|
|
|
|
|
+ shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* 检测电流情况,看是否过流等 */
|
|
|
|
|
+static debounce_t _charger_over_current = {
|
|
|
|
|
+ .count = 0,
|
|
|
|
|
+ .max_count = 10
|
|
|
|
|
+};
|
|
|
|
|
+void check_current_state(void){
|
|
|
|
|
+ //判断是否过流充电,放电过流通过5238来检测
|
|
|
|
|
+ if (bms_state_get()->charging && !_health.charger_over_current) {
|
|
|
|
|
+ float current = measure_value()->load_current;
|
|
|
|
|
+ if (current > MAX_CURRENT_FOR_CHARGER) {
|
|
|
|
|
+ _charger_over_current.count ++;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ _charger_over_current.count = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (_charger_over_current.count >= _charger_over_current.max_count){
|
|
|
|
|
+ _health.charger_over_current = 1;
|
|
|
|
|
+ _charger_over_current.count = 0;
|
|
|
|
|
+ shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* 检测pack电压,cell电压,pack电压过低触发powerdown*/
|
|
|
|
|
+
|
|
|
|
|
+static debounce_t _discharger_lower_voltage = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _discharger_low_normal_voltage = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _power_down_voltage = {.count = 0, .max_count = 10};
|
|
|
|
|
+void check_voltage_state(void) {
|
|
|
|
|
+ if (!_health.discharger_lower_voltage){ //check for low pack voltage for close discharger
|
|
|
|
|
+ if (bms_state_get()->pack_voltage <= MIN_VOLTAGE_FOR_DISCHARGER){
|
|
|
|
|
+ debounce_inc(&_discharger_lower_voltage);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_discharger_lower_voltage);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_discharger_lower_voltage)){
|
|
|
|
|
+ _health.discharger_lower_voltage = 1;
|
|
|
|
|
+ debounce_reset(&_discharger_lower_voltage);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }else { //check for discharger recovery
|
|
|
|
|
+ if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER){
|
|
|
|
|
+ debounce_inc(&_discharger_low_normal_voltage);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_discharger_low_normal_voltage);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_discharger_low_normal_voltage)){
|
|
|
|
|
+ _health.discharger_lower_voltage = 0;
|
|
|
|
|
+ debounce_reset(&_discharger_low_normal_voltage);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ /* check for power down */
|
|
|
|
|
+ if (!io_state()->charger_detect && !bms_state_get()->charging && !_health.powerdown_lower_voltage){
|
|
|
|
|
+ if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_POWER_DOWN){
|
|
|
|
|
+ debounce_inc(&_power_down_voltage);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_power_down_voltage);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_power_down_voltage)){
|
|
|
|
|
+ /*
|
|
|
|
|
+ * no need to clear powerdown(bms is shutdown), when charger insert,
|
|
|
|
|
+ * system will power on with powerdown_lower_voltage cleared
|
|
|
|
|
+ */
|
|
|
|
|
+ _health.powerdown_lower_voltage = 1;
|
|
|
|
|
+ debounce_reset(&_power_down_voltage);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* 检测温度情况,看是否过高温,或者过低温 */
|
|
|
|
|
+
|
|
|
|
|
+static debounce_t _charger_over_temp = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _charger_lower_temp = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _charger_normal_temp = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _discharger_over_temp = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _discharger_lower_temp = {.count = 0, .max_count = 10};
|
|
|
|
|
+static debounce_t _discharger_normal_temp = {.count = 0, .max_count = 10};
|
|
|
|
|
+
|
|
|
|
|
+static int _is_over_temp(int8_t *temps){
|
|
|
|
|
+ for (int i = 0; i < PACK_TEMPS_NUM; i++){
|
|
|
|
|
+ if (measure_value()->pack_temp[i] >= temps[i]){
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int _is_low_temp(int8_t *temps){
|
|
|
|
|
+ for (int i = 0; i < PACK_TEMPS_NUM; i++){
|
|
|
|
|
+ if (measure_value()->pack_temp[i] <= temps[i]){
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void check_temp_state(void){
|
|
|
|
|
+ if (bms_state_get()->charging){
|
|
|
|
|
+ if (!_health.charger_over_temp){
|
|
|
|
|
+ if (_is_over_temp(charger_higher_high_temp)) {//超过允许的最高温度
|
|
|
|
|
+ debounce_inc(&_charger_over_temp);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_charger_over_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_charger_over_temp)){
|
|
|
|
|
+ _health.charger_over_temp = 1;
|
|
|
|
|
+ debounce_reset(&_charger_over_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!_health.charger_lower_temp){
|
|
|
|
|
+ if (_is_low_temp(charger_lower_low_temp)) {//低于允许的最低温度
|
|
|
|
|
+ debounce_inc(&_charger_lower_temp);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_charger_lower_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_charger_lower_temp)) {
|
|
|
|
|
+ _health.charger_lower_temp = 1;
|
|
|
|
|
+ debounce_reset(&_charger_lower_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (_health.charger_over_temp || _health.charger_lower_temp) {
|
|
|
|
|
+ if (!_is_over_temp(charger_normal_high_temp) && !_is_low_temp(charger_normal_low_temp)){
|
|
|
|
|
+ debounce_inc(&_charger_normal_temp);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_charger_normal_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_charger_normal_temp)){
|
|
|
|
|
+ _health.charger_over_temp = 0;
|
|
|
|
|
+ _health.charger_lower_temp = 0;
|
|
|
|
|
+ debounce_reset(&_charger_normal_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }else {
|
|
|
|
|
+ if (!_health.discharger_over_temp){
|
|
|
|
|
+ if (_is_over_temp(discharger_higher_high_temp)) {//超过允许的最高温度
|
|
|
|
|
+ debounce_inc(&_discharger_over_temp);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_discharger_over_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_discharger_over_temp)){
|
|
|
|
|
+ _health.discharger_over_temp = 1;
|
|
|
|
|
+ debounce_reset(&_discharger_over_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!_health.discharger_lower_temp){
|
|
|
|
|
+ if (_is_over_temp(discharger_lower_low_temp)) {//超过允许的最高温度
|
|
|
|
|
+ debounce_inc(&_discharger_lower_temp);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_discharger_lower_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_discharger_lower_temp)) {
|
|
|
|
|
+ _health.discharger_lower_temp = 1;
|
|
|
|
|
+ debounce_reset(&_discharger_lower_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (_health.discharger_over_temp || _health.discharger_lower_temp) {
|
|
|
|
|
+ if (!_is_over_temp(discharger_normal_high_temp) && !_is_low_temp(discharger_normal_low_temp)){
|
|
|
|
|
+ debounce_inc(&_discharger_normal_temp);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ debounce_reset(&_discharger_normal_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (debounce_reach(&_discharger_normal_temp)){
|
|
|
|
|
+ _health.charger_over_temp = 0;
|
|
|
|
|
+ _health.charger_lower_temp = 0;
|
|
|
|
|
+ debounce_reset(&_discharger_normal_temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|