Просмотр исходного кода

小电流短路保护处理&_process_unheath 返回的结果或起来

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 лет назад
Родитель
Сommit
66e5ad1ced

+ 31 - 15
Application/app/sox/health.c

@@ -48,6 +48,7 @@ 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 void _aux_lock_timer_handler(shark_timer_t *t);
+static void _aux_unlock_timer_handler(shark_timer_t *t);
 static bms_health_t _health;
 static debounce_timer_t _load_detect_timer;
 static debounce_timer_t _charger_detect_timer;
@@ -118,7 +119,7 @@ static void check_ml5238_state(int event){
 static void debug_health(void){
 	uint32_t *value = (uint32_t *)&_health;
 	if (*value != 0){
-		health_error("health value = 0x%x\n", *value);
+		//health_error("health value = 0x%x\n", *value);
 	}
 }
 
@@ -276,32 +277,47 @@ static int _is_low_temp(int8_t *temps){
 
 static uint8_t small_power_detect_count = 0;
 static shark_timer_t _aux_lock_timer = {.handler = _aux_lock_timer_handler};
+static shark_timer_t _aux_unlock_timer = {.handler = _aux_unlock_timer_handler};
+
 static void _aux_lock_timer_handler(shark_timer_t *t){
-	//有可能这个时候,用户发了关小电的指令
-	if (bms_state_get()->user_request & USER_REQUEST_SMALLCURRENT_ON){
-		AUX_VOL_OPEN(1);
-		if (++small_power_detect_count >= 10){
-			delay_us(1000);
-			//端口电压小于阈值,判断为小电流短路
-			if (get_small_current_voltage() < SMALL_CURRENT_MIN_VOLTAGE){//real short
-				bms_health()->small_current_short = 1;
-				AUX_VOL_OPEN(0);
-				small_power_detect_count = 0;
-				shark_timer_post( &_aux_lock_timer, 30 * 1000); //30s后再次尝试打开
-			}
+	AUX_VOL_OPEN(1);
+	shark_timer_post( &_aux_unlock_timer, 200);
+	health_debug("open aux[re-enable], %lld\n", shark_get_mseconds());
+	if (++small_power_detect_count >= 10){
+		delay_us(1000);
+		//端口电压小于阈值,判断为小电流短路
+		if (get_small_current_voltage() < SMALL_CURRENT_MIN_VOLTAGE){//real short
+			bms_health()->small_current_short = 1;
+			AUX_VOL_OPEN(0);
+			small_power_detect_count = 0;
+			shark_timer_post( &_aux_lock_timer, 30 * 1000); //30s后再次尝试打开
+			shark_timer_cancel(&_aux_unlock_timer);
+			health_debug("set aux short current, and retry after 30s\n");
 		}
 	}
 }
 
+static void _aux_unlock_timer_handler(shark_timer_t *t){
+	if (!io_state()->aux_lock_detect){
+		health_debug("unlock aux detect\n");
+		small_power_detect_count = 0;
+		bms_health()->small_current_short = 0;
+		AUX_VOL_OPEN(1);
+	}
+}
+
+
 void health_process_aux_lock(void){
 	if (io_state()->aux_lock_detect) {
 		if (AUX_VOL_IS_OPEN()){
 			AUX_VOL_OPEN(0);
+			health_debug("close aux[locked], %lld\n", shark_get_mseconds());
 			shark_timer_post( &_aux_lock_timer, 1);
+			shark_timer_cancel(&_aux_unlock_timer);
 		}
 	}else {
-		if ((AUX_VOL_IS_OPEN())){
-			small_power_detect_count = 0;
+		if (AUX_VOL_IS_OPEN()) {
+			shark_timer_post( &_aux_unlock_timer, 500);
 			shark_timer_cancel(&_aux_lock_timer);
 		}
 	}

+ 4 - 2
Application/app/sox/iostate.c

@@ -28,7 +28,7 @@ static io_timer_t dcdc_pwr_io = {._timer.handler = io_timer_handler, .debounce_t
 
 static void debug_io(void){
 	uint16_t *io = (uint16_t *)&_io_state;
-	io_debug("io state = 0x%x\n", *io);
+	io_debug("io state = 0x%x, %lld\n", *io, shark_get_mseconds());
 }
 
 void io_state_init(void){
@@ -139,7 +139,9 @@ static void io_timer_handler(shark_timer_t *t){
 
 
 static void small_current_irq_timer_handler(shark_timer_t *t){
-	_io_state.aux_lock_detect = 1;
+	aux_short_io.value = _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
+	aux_short_io.detect_cnt = 0;
+	debug_io();
 	health_process_aux_lock();
 }
 static shark_timer_t _small_current_irq_timer = {.handler = small_current_irq_timer_handler};

+ 6 - 6
Application/app/sox/measure_task.c

@@ -42,7 +42,7 @@ static struct means_task _pack_current_task;
 static u32 pack_current_task_handler(void);
 static void init_pack_current_task(void){
 	_pack_current_task._task.handler = pack_current_task_handler;
-	_pack_current_task.delay = 1000;
+	_pack_current_task.delay = 30;
 	_pack_current_task.index = 0;
 	_measure_value.load_current = get_pack_current();
 	shark_task_add(&_pack_current_task._task);
@@ -51,7 +51,7 @@ static void init_pack_current_task(void){
 static u32 pack_current_task_handler(void){
 	_measure_value.load_current = get_pack_current();
 	_current_notify();//通知bms state 有新的电流数据
-	measure_debug("load current = %f\n", _measure_value.load_current);
+	//measure_debug("load current = %f\n", _measure_value.load_current);
 	return _pack_current_task.delay;
 }
 
@@ -62,7 +62,7 @@ static struct means_task _cell_task;
 static u32 cell_task_handler(void);
 static void init_cell_task(void){
 	_cell_task._task.handler = cell_task_handler;
-	_cell_task.delay = 1000;
+	_cell_task.delay = 30;
 	_cell_task.index = 0;
 	for (int i = 0; i < CELLS_NUM; i++){
 		_measure_value.cell_vol[i] = get_cell_voltage(i);
@@ -75,7 +75,7 @@ static u32 cell_task_handler(void){
 		return _cell_task.delay;
 	}
 	_measure_value.cell_vol[_cell_task.index] = get_cell_voltage(_cell_task.index);
-	measure_debug("cell[%d]=%f\n", _cell_task.index, _measure_value.cell_vol[_cell_task.index]/1000.0f);
+	//measure_debug("cell[%d]=%f\n", _cell_task.index, _measure_value.cell_vol[_cell_task.index]/1000.0f);
 	_cell_task.index = (_cell_task.index + 1) % CELLS_NUM;
 	_voltage_notify();//通知bms state 有新的电压数据
 	return _cell_task.delay;
@@ -88,7 +88,7 @@ static struct means_task _temp_task;
 static u32 temp_task_handler(void);
 static void init_temp_task(void){
 	_temp_task._task.handler = temp_task_handler;
-	_temp_task.delay = 5 * 1000;
+	_temp_task.delay = 2 * 1000;
 	_temp_task.index = 0;
 	for (int i = 0; i < PACK_TEMPS_NUM; i++){
 		_measure_value.pack_temp[i] = get_pack_temperature(i);
@@ -97,7 +97,7 @@ static void init_temp_task(void){
 }
 static u32 temp_task_handler(void){
 	_measure_value.pack_temp[_temp_task.index] = get_pack_temperature(_temp_task.index);
-	measure_debug("pack temp[%d]=%d\n", _temp_task.index, _measure_value.pack_temp[_temp_task.index]);
+	//measure_debug("pack temp[%d]=%d\n", _temp_task.index, _measure_value.pack_temp[_temp_task.index]);
 	_temp_task.index = (_temp_task.index + 1) % (PACK_TEMPS_NUM);
 	_temperature_notify();//通知bms state 有新的温度数据
 	return _temp_task.delay;

+ 2 - 1
Application/app/sox/soc.c

@@ -70,7 +70,7 @@ static __inline__ float _delta_time(void){
 
 void soc_update_by_ocv(void){
 	if (_soc.flags & SOC_FLAG_CALIBRATED){
-		if (bms_health()->powerdown_lower_voltage){
+		if (!chargering && bms_health()->powerdown_lower_voltage){
 			_soc.coulomb_min = _soc.coulomb_now; //已经校准过了,而且电池进入powerdown,最小容量修正为当前容量
 			_soc.capacity = 0;
 			soc_warning("current coulomb %f\n", _soc.coulomb_now);
@@ -165,6 +165,7 @@ void soc_update(void){
 			soc_warning("calibrate OK, charging coulomb: %f\n", _soc.charger_coulomb);
 		}
 	}
+	_soc.energy = bms_state_get()->pack_voltage/1000.f * (_soc.coulomb_now - _soc.coulomb_min) * _soc.capacity/100.0f;
 	if (old_cap != _soc.capacity) {
 		nv_save_soc();
 	}

+ 9 - 7
Application/app/sox/state.c

@@ -36,6 +36,7 @@ void bms_state_init(void){
 	io_state_init();
 	health_init();
 	soc_init();
+	_bms_state.user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_ON;
 	shark_task_add(&_bms_main_task);
 }
 
@@ -106,27 +107,28 @@ static void charger_open(int open) {
 #define Health_aux_Fault 4
 
 static s32 _process_unheath(void){
+	u32 unhealth = Health_Success;
 	if (bms_health()->load_current_short) {//短路检测后,关闭充放电mos
 		discharger_open(0);
 		charger_open(0); //disable charger mosfet
 		AUX_VOL_OPEN(0);
 		_bms_state.charging = 0;
-		return (Health_Discharger_Failt | Health_charger_Fault);
+		unhealth = (Health_Discharger_Failt | Health_charger_Fault);
 	}
 
 	if (bms_health()->charger_over_current || bms_health()->charger_over_temp || bms_health()->charger_lower_temp){
 		charger_open(0); //disable charger mosfet
 		_bms_state.charging = 0;
-		return Health_charger_Fault;
+		unhealth |= Health_charger_Fault;
 	}
 
 	if (bms_health()->discharger_over_temp || bms_health()->discharger_lower_temp || bms_health()->discharger_lower_voltage){
 		discharger_open(0); //disable charger mosfet
-		return Health_Discharger_Failt;
+		unhealth |= Health_Discharger_Failt;
 	}
 
 	if (io_state()->aux_lock_detect || bms_health()->small_current_short) {
-		return Health_aux_Fault;
+		unhealth |= Health_aux_Fault;
 	}
 	
 	return Health_Success;
@@ -245,7 +247,7 @@ static int _can_close_mos_no_hall(void){
 	return 0;
 }
 
-static void _process_iostate_changed(void){
+static void _process_iostate_changed(s32 unhealth){
 	if (!(io_state()->hall_detect) && shark_uart_timeout() && _can_close_mos_no_hall()){
 		if (ml5238_is_discharging()){
 			discharger_open(0);
@@ -254,7 +256,7 @@ static void _process_iostate_changed(void){
 			AUX_VOL_OPEN(1);
 		}
 	}
-	if (IS_CHARGER_IN()) {
+	if (io_state()->charger_detect && ((unhealth & Health_charger_Fault) == 0)) {
 		if (!ml5238_is_charging()){
 			charger_open(1);
 		}
@@ -266,7 +268,7 @@ static u32 _bms_main_task_handler(void){
 	_process_user_request(unhealth);
 	_process_deepsleep(unhealth);
 	_process_power_down();
-	_process_iostate_changed();
+	_process_iostate_changed(unhealth);
 	return 0;
 }