From bf8c495ce7395055b8432342d5d62e59ed7fa147 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=BD=AD=E7=A3=8A?= <1624952103@qq.com> Date: Tue, 25 Jun 2024 00:09:48 +0000 Subject: [PATCH] Merge #372 into sdk602patch0521 from refs/changes/372/head MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Code review from 彭磊 * refs/changes/372/head: (7 commits squashed) - 添加stop cywee 接口 - Merge branch 'sdk602patch0521' of https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230 into remotes/origin/sdk602patch0521 - Merge branch 'sdk602patch0521' of https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230 into remotes/origin/sdk602patch0521 - Merge branch 'sdk602patch0521' of https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230 into remotes/origin/sdk602patch0521 - Merge branch 'sdk602patch0521' of https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230 into remotes/origin/sdk602patch0521 - Merge branch 'sdk602patch0521' of https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230 into remotes/origin/sdk602patch0521 - 优化电池充电 Signed-off-by: aliyun6865282430 <1624952103@qq.com> Reviewed-by: snickersun@163.com Merged-by: snickersun@163.com CR-link: https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230/change/372 --- .../battery/service/battery_driver.cpp | 109 +++++++++++++----- .../drivers/peripheral/utils/custom_utils.c | 2 +- 2 files changed, 83 insertions(+), 28 deletions(-) diff --git a/code/sdk/application/battery/service/battery_driver.cpp b/code/sdk/application/battery/service/battery_driver.cpp index 9cd72db2d..3f458c56a 100644 --- a/code/sdk/application/battery/service/battery_driver.cpp +++ b/code/sdk/application/battery/service/battery_driver.cpp @@ -68,6 +68,7 @@ typedef enum { #include "debug_print.h" +#include "custom_utils.h" #define HAL_ADC_SAMPLE_NUMBER_CONFIG_0 10 /* Channel with buffer manual sample average number. */ #define HAL_ADC_SAMPLE_NUMBER_CONFIG_1 10 /* Channel without buffer manual sample average number. */ @@ -228,31 +229,31 @@ float battery_ntc_map[][2]={ float battery_percent_map[][2]={ - {4.136,100}, - {4.086,95}, - {4.031,90}, - {3.977,85}, - {3.936,80}, - {3.883,75}, - {3.836,70}, - {3.804,65}, - {3.771,60}, - {3.74,55}, - {3.713,50}, - {3.693,45}, - {3.672,40}, - {3.654,35}, - {3.639,30}, - {3.62,25}, - {3.596,20}, - {3.571,15}, - {3.534,10}, - {3.487,5}, - {3.38,0} + {4.3,100}, + {4.264,95}, + {4.228,90}, + {4.192,85}, + {4.156,80}, + {4.12,75}, + {4.084,70}, + {4.048,65}, + {4.012,60}, + {3.976,55}, + {3.94,50}, + {3.904,45}, + {3.868,40}, + {3.832,35}, + {3.796,30}, + {3.76,25}, + {3.724,20}, + {3.688,15}, + {3.652,10}, + {3.41,5}, + {3.3,0} }; -#define batteryservice_VOLTAGE_MAX (4.136f) -#define batteryservice_VOLTAGE_MIN (3.38f) +#define batteryservice_VOLTAGE_MAX (4.3f) +#define batteryservice_VOLTAGE_MIN (3.3f) static uint32_t g_adc_sample_times = 0; static uint32_t g_adc_sample_discard = 0; @@ -262,8 +263,14 @@ static uint32_t g_adc_sample_stick = 0; #define MINIMUM_VOLTAGE_RANGE 2 #define MAXIMUM_VOLTAGE_RANGE 17 - - +static int history_percent = 0; +static int sum_percent = 0; +static int percent_num=0; +static int history_data[5]={0}; +static int left = 0; +static int right = -1; +static uint32_t history_time = 0; +static uint32_t sample_time = 0; static battery_info_t current_battery_info={0}; @@ -385,6 +392,7 @@ static float adc_trans_systick_to_battery_ntc(int sticks) */ static int battery_get_power_percentage(float voltage) { + uint32_t current_time = 0; int percent = 0; int map_len = sizeof(battery_percent_map)/sizeof(battery_percent_map[0]); float temp_value = 0.0f; @@ -399,15 +407,62 @@ static int battery_get_power_percentage(float voltage) temp_value = battery_percent_map[i][1] - (battery_percent_map[i][1] - battery_percent_map[i+1][1] ) * r_div ; percent = (int)(temp_value + 0.5); - + + if(get_charge_stat()) + { + if(history_percent > percent) + { + percent = history_percent; + break; + } + } + else + { + if(history_percent < percent && history_percent != 0) + { + percent = history_percent; + break; + } + + } + + history_percent = percent; // printf("batterymap[%d]=%f,batterymap[%d]=%f, r_div = %f , temp_value = %f ,percent = %d\n", // i,battery_percent_map[i][0],i+1,battery_percent_map[i+1][0], // r_div,temp_value,percent // ); break; } - } + } + + uapi_get_timestampe_ss(¤t_time); + if(history_time == 0) + history_time = current_time; + sample_time = current_time - history_time; + history_time = current_time; + + if(sample_time >= 10) + { + sample_time = 0; + if(percent_num == 5) + { + sum_percent -= history_data[left]; + history_data[left] = 0; + left++; + left = left % 5; + percent_num--; + } + + percent_num++; + right++; + right = right % 5; + history_data[right] = percent; + sum_percent += history_data[right]; + } + + percent = sum_percent / percent_num; + if (voltage <= batteryservice_VOLTAGE_MIN) percent = 0; diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.c b/code/sdk/drivers/peripheral/utils/custom_utils.c index 3b9ae3f1a..84cdd162b 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils.c @@ -415,7 +415,7 @@ errcode_t uapi_get_timestampe_ss(uint32_t *tm) } *tm = get_current_timestamp(); - printf("[timestamp:%u]\n", *tm); + //printf("[timestamp:%u]\n", *tm); return ERRCODE_SUCC; } -- 2.17.1