From db744ef1d1196512e7c5be7ab5a98cd22c6ba851 Mon Sep 17 00:00:00 2001 From: 1624952103 Date: Sat, 7 Feb 2026 17:22:25 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96=E7=94=B5=E5=8E=8B=E8=AE=A1?= =?utf8?q?=E7=AE=97=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../battery/service/battery_driver.cpp | 57 +++++++++++++------ code/sdk/zhihui/utils/custom_utils.c | 3 +- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/code/sdk/zhihui/services/battery/service/battery_driver.cpp b/code/sdk/zhihui/services/battery/service/battery_driver.cpp index 324d0dedd..7081bf72a 100644 --- a/code/sdk/zhihui/services/battery/service/battery_driver.cpp +++ b/code/sdk/zhihui/services/battery/service/battery_driver.cpp @@ -565,7 +565,7 @@ void battery_restore_vol(void) batteryservice_get_voltage_by_channel(&cur_vol,ADC_CHANNEL_BAT_VOLTAGE); - if(bat_vol.percent > 0 && bat_vol.percent<=100 && (fabsf(cur_vol - bat_vol.avg_vol) < 0.15)) + if(bat_vol.percent > 0 && bat_vol.percent<=100 && (fabsf(cur_vol - bat_vol.avg_vol) < 0.25)) history_percent = bat_vol.percent; // for(int i = 0; i < AVG_VOLTAGE_COUNT;i++) // { @@ -841,6 +841,7 @@ static void get_avg_voltage(float *vol) uint32_t current_time = 0; float max_vol = 0; float min_vol = 0; + float sum_vol = 0; get_timestampe_ss(¤t_time); if(history_time == 0) history_time = current_time; @@ -871,27 +872,49 @@ static void get_avg_voltage(float *vol) { if(bat_vol.vol_num > 2) { - min_vol = bat_vol.history_data[0]; - max_vol = bat_vol.history_data[0]; - for (int i = 0;i < bat_vol.vol_num; i++) + + float valid_data[AVG_VOLTAGE_COUNT] = {0}; + int valid_idx = 0; + for (int i = 0; i < AVG_VOLTAGE_COUNT; i++) { + if (bat_vol.history_data[i] >= 3.25 && bat_vol.history_data[i] <= 4.45) + { + valid_data[valid_idx++] = bat_vol.history_data[i]; + sum_vol+=bat_vol.history_data[i]; + PRINT("333history_data[%d] : %.3f\n",i,bat_vol.history_data[i]); + } + } - PRINT("333history_data[%d] : %.3f\n",i,bat_vol.history_data[i]); - if (bat_vol.history_data[i] < min_vol) - min_vol = bat_vol.history_data[i]; - if (bat_vol.history_data[i] > max_vol) - max_vol = bat_vol.history_data[i]; + if (valid_idx > 2) + { + min_vol = valid_data[0]; + max_vol = valid_data[0]; + for (int i = 1; i < valid_idx; i++) + { + if (valid_data[i] < min_vol) min_vol = valid_data[i]; + if (valid_data[i] > max_vol) max_vol = valid_data[i]; + } + // 去掉最大/最小值计算平均值 + bat_vol.avg_vol = (sum_vol - max_vol - min_vol) / (valid_idx - 2); } - - - bat_vol.avg_vol = (bat_vol.sum_vol - max_vol - min_vol) / (bat_vol.vol_num - 2); - printf("avg_vol : %.3f\n",bat_vol.avg_vol); - // *vol = (bat_vol.sum_vol - max_vol - min_vol) / (bat_vol.vol_num - 2); - // printf("avg_vol : %.3f\n",*vol); + else + { + bat_vol.avg_vol = sum_vol / valid_idx; + } + + if(sum_vol != bat_vol.sum_vol) + { + printf("sum_vol:%.3f != bat_vol.sum_vol:%.3f\n",sum_vol,bat_vol.sum_vol); + // bat_vol.sum_vol = sum_vol; + memset_s(&bat_vol,sizeof(BAT_VOL_T),0,sizeof(BAT_VOL_T)); + bat_vol.right = -1; + } + } - else + else { bat_vol.avg_vol = bat_vol.sum_vol / bat_vol.vol_num; - // *vol = bat_vol.sum_vol / bat_vol.vol_num; + } + printf("avg_vol : %.3f\n",bat_vol.avg_vol); } } diff --git a/code/sdk/zhihui/utils/custom_utils.c b/code/sdk/zhihui/utils/custom_utils.c index 66792e5f8..a26d9d908 100644 --- a/code/sdk/zhihui/utils/custom_utils.c +++ b/code/sdk/zhihui/utils/custom_utils.c @@ -436,7 +436,8 @@ void shutdown_reboot_prejobs_task(void) //保存电量 #ifdef SUPPORT_BATTERY_SERVICE PRINT("shutdown_reboot_prejobs_task: Calling send_battery_svr_cmd_msg()\r\n"); - send_battery_svr_cmd_msg(BATTERY_CMD_SAVE_PERCENT); + // send_battery_svr_cmd_msg(BATTERY_CMD_SAVE_PERCENT); + battery_save_vol(); PRINT("shutdown_reboot_prejobs_task: send_battery_svr_cmd_msg() completed\r\n"); #endif -- 2.17.1