#define AVG_VOLTAGE_COUNT 7
/** 滑动平均:仅接受该区间内的采样;历史槽中若出现非 0 且越界视为异常并清空环形缓冲 */
#define BAT_AVG_SAMPLE_VOL_MIN (3.25f)
-#define BAT_AVG_SAMPLE_VOL_MAX (4.45f)
+#define BAT_AVG_SAMPLE_VOL_MAX (4.55f)
#pragma pack(1)
typedef struct
for(int i = 0; i < bat_vol.vol_num;i++)
{
printf("history_data[%d]:%.3f\n",i,bat_vol.history_data[i]);
- if(bat_vol.history_data[i] < 3.3 || bat_vol.history_data[i] > 4.45)
+ if(bat_vol.history_data[i] < BAT_AVG_SAMPLE_VOL_MIN || bat_vol.history_data[i] > BAT_AVG_SAMPLE_VOL_MAX)
bat_vol.history_data[i] = 0;
}
if(bat_vol.vol_num < 0 || bat_vol.vol_num > AVG_VOLTAGE_COUNT ||
bat_vol.right < -1 || bat_vol.right >= AVG_VOLTAGE_COUNT ||
bat_vol.left < -1 || bat_vol.left >= AVG_VOLTAGE_COUNT ||
- bat_vol.sum_vol < bat_vol.vol_num * 3.25 || bat_vol.sum_vol > (bat_vol.vol_num * 4.45))
+ bat_vol.sum_vol < bat_vol.vol_num * BAT_AVG_SAMPLE_VOL_MIN || bat_vol.sum_vol > (bat_vol.vol_num * BAT_AVG_SAMPLE_VOL_MAX))
{
memset_s(&bat_vol,sizeof(BAT_VOL_T),0,sizeof(BAT_VOL_T));
bat_vol.right = -1;
// for(int i = 0; i < AVG_VOLTAGE_COUNT;i++)
// {
// printf("history_data[%d]:%.3f\n",i,bat_vol.history_data[i]);
- // if(bat_vol.history_data[i] < 3.3 || bat_vol.history_data[i] > 4.45)
+ // if(bat_vol.history_data[i] < BAT_AVG_SAMPLE_VOL_MIN || bat_vol.history_data[i] > BAT_AVG_SAMPLE_VOL_MAX)
// {
// bat_vol.history_data[i] = 0;
// }
return percent;
}
+#ifdef ENABLE_HP4570
+
+static bool hp4570_check_charge_done(float voltage, uint8_t *percent)
+{
+ static float s_hp4570_chg_done_last_v = -1.0f;
+ static uint8_t s_hp4570_chg_done_flat_cnt = 0;
+ const float k_chg_done_floor_v = HP4570_CHG_DONE_MIN_VOLTAGE_V;
+ const float k_flat_eps_v = 0.0015f; /* 噪声内视为“未升” */
+
+ if (!get_charge_stat() || voltage < k_chg_done_floor_v) {
+ s_hp4570_chg_done_last_v = -1.0f;
+ s_hp4570_chg_done_flat_cnt = 0;
+ return false;
+ }
+ if (!hp4570n_read_chrg_stat_chrg_done()) {
+ s_hp4570_chg_done_last_v = -1.0f;
+ s_hp4570_chg_done_flat_cnt = 0;
+ return false;
+ }
+
+ if (s_hp4570_chg_done_last_v < 0.0f) {
+ s_hp4570_chg_done_last_v = voltage;
+ s_hp4570_chg_done_flat_cnt = 0;
+ } else {
+ if (voltage <= s_hp4570_chg_done_last_v + k_flat_eps_v) {
+ if (s_hp4570_chg_done_flat_cnt < 250) {
+ s_hp4570_chg_done_flat_cnt++;
+ }
+ } else {
+ s_hp4570_chg_done_flat_cnt = 0;
+ }
+ s_hp4570_chg_done_last_v = voltage;
+ }
+
+ if (s_hp4570_chg_done_flat_cnt >= HP4570_CHG_DONE_FLAT_STREAK && history_percent < 100) {
+ history_percent++;
+ *io_percent = history_percent;
+ printf("[电池计算] HP4570 CHG_DONE+>=%.1fV 连续电压不升 cnt=%u(>=%u) -> 累加 %% hist=%d\n",
+ HP4570_CHG_DONE_MIN_VOLTAGE_V,
+ (unsigned int)s_hp4570_chg_done_flat_cnt, (unsigned int)HP4570_CHG_DONE_FLAT_STREAK, history_percent);
+ if (history_percent >= 100) {
+ s_hp4570_chg_done_flat_cnt = 0;
+ s_hp4570_chg_done_last_v = -1.0f;
+ }
+ return true;
+ }
+ return false;
+}
+#endif
+
/**
* 输入电池电压值
* 根据 battery_percent_map 计算并返回 电池电量百分比
}
#ifdef ENABLE_HP4570
- /* CHG_DONE 且电压≥HP4570_CHG_DONE_MIN_VOLTAGE_V:连续 HP4570_CHG_DONE_FLAT_STREAK 次采样电压不升,再每次仅 +1% 直至 100 */
- {
- static float s_hp4570_chg_done_last_v = -1.0f;
- static uint8_t s_hp4570_chg_done_flat_cnt = 0;
- const float k_chg_done_floor_v = HP4570_CHG_DONE_MIN_VOLTAGE_V;
- const float k_flat_eps_v = 0.0015f; /* 噪声内视为“未升” */
-
- if (!get_charge_stat() || voltage < k_chg_done_floor_v) {
- s_hp4570_chg_done_last_v = -1.0f;
- s_hp4570_chg_done_flat_cnt = 0;
- } else if (hp4570n_read_chrg_stat_chrg_done()) {
- if (s_hp4570_chg_done_last_v < 0.0f) {
- s_hp4570_chg_done_last_v = voltage;
- s_hp4570_chg_done_flat_cnt = 0;
- } else {
- if (voltage <= s_hp4570_chg_done_last_v + k_flat_eps_v) {
- if (s_hp4570_chg_done_flat_cnt < 250) {
- s_hp4570_chg_done_flat_cnt++;
- }
- } else {
- s_hp4570_chg_done_flat_cnt = 0;
- }
- s_hp4570_chg_done_last_v = voltage;
- }
- if (s_hp4570_chg_done_flat_cnt >= HP4570_CHG_DONE_FLAT_STREAK && history_percent < 100) {
- history_percent++;
- percent = history_percent;
- printf("[电池计算] HP4570 CHG_DONE+>=%.1fV 连续电压不升 cnt=%u(>=%u) -> 累加 %% hist=%d\n",
- HP4570_CHG_DONE_MIN_VOLTAGE_V,
- (unsigned int)s_hp4570_chg_done_flat_cnt, (unsigned int)HP4570_CHG_DONE_FLAT_STREAK, history_percent);
- if (history_percent >= 100) {
- s_hp4570_chg_done_flat_cnt = 0;
- s_hp4570_chg_done_last_v = -1.0f;
- }
- return percent;
- }
- } else {
- s_hp4570_chg_done_last_v = -1.0f;
- s_hp4570_chg_done_flat_cnt = 0;
- }
+ if (hp4570_check_charge_done(voltage, &percent)) {
+ return percent;
}
#endif