优化电压计算存储 3205/1 3205/head
author1624952103 <pl@DESKTOP-EN9LKR5.localdomain>
Sat, 7 Feb 2026 09:22:25 +0000 (17:22 +0800)
committer1624952103 <pl@DESKTOP-EN9LKR5.localdomain>
Sat, 7 Feb 2026 09:22:25 +0000 (17:22 +0800)
code/sdk/zhihui/services/battery/service/battery_driver.cpp
code/sdk/zhihui/utils/custom_utils.c

index 324d0de..7081bf7 100644 (file)
@@ -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(&current_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);
     }  
 }
 
index 66792e5..a26d9d9 100644 (file)
@@ -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