Files
banban/ql-application/threadx/interface/custom_app/battery.c
T

264 lines
9.3 KiB
C

#include <stdarg.h>
#include "ql_type.h"
#include "ql_data_call.h"
#include "ql_rtos.h"
#include "ql_application.h"
#include <stdio.h>
#include "sockets.h"
#include "ql_uart.h"
#include "ql_func.h"
#include "netdb.h"
#include "ql_sim.h"
#include "ql_dev.h"
#include "ql_wtd.h"
#include "ql_fs.h"
#include "ql_fota.h"
#include "ql_sim.h"
#include "ql_ping.h"
#include "ql_nw.h"
#include "ql_atcmd.h"
#include "app_inc.h"
#include "ql_uart.h"
#include "ql_application.h"
#include "ql_log.h"
#include "hal_api.h"
// 电量阈值定义
#define BAT_VOLTAGE_100 4200
#define BAT_VOLTAGE_90 4125
#define BAT_VOLTAGE_80 4050
#define BAT_VOLTAGE_70 3950
#define BAT_VOLTAGE_60 3850
#define BAT_VOLTAGE_50 3775
#define BAT_VOLTAGE_40 3700
#define BAT_VOLTAGE_30 3650
#define BAT_VOLTAGE_20 3600
#define BAT_VOLTAGE_10 3550
#define BAT_VOLTAGE_MIN 3500
// ========== 新增:10分钟电压历史记录用于判断充放电状态 ==========
#define VOLTAGE_HISTORY_SIZE 60 // 10分钟 = 60个10秒采样点
#define VOLTAGE_RISE_THRESHOLD 15 // 10分钟内上升超过15mV判定为充电
#define VOLTAGE_DROP_THRESHOLD 15 // 10分钟内下降超过15mV判定为放电
#define VOLTAGE_STABLE_RANGE 10 // 变化在±10mV内判定为稳定
unsigned short g_vbat_vol = 0;
unsigned short vbat_sample[6] = {0}; // 修改:样本数改为6
unsigned char battery_first_update_flag = 0;
static unsigned short g_vbat_vol_last = 0;
static bat_level_t g_bat_level_last = BAT_LEVEL_100;
// ========== 新增:电压历史记录环形缓冲区 ==========
static unsigned short g_voltage_history[VOLTAGE_HISTORY_SIZE] = {0};
static unsigned char g_history_index = 0;
static unsigned char g_history_count = 0; // 已记录的有效点数
static int g_charging_state = 0; // 当前判定的充电状态:1-充电, 0-未充电
static unsigned char g_charging_stable_cnt = 0; // 状态稳定计数
bat_level_t calc_battery_level(uint16_t voltage_mv)
{
if (voltage_mv >= BAT_VOLTAGE_90) return BAT_LEVEL_100;
else if (voltage_mv >= BAT_VOLTAGE_80) return BAT_LEVEL_90;
else if (voltage_mv >= BAT_VOLTAGE_70) return BAT_LEVEL_80;
else if (voltage_mv >= BAT_VOLTAGE_60) return BAT_LEVEL_70;
else if (voltage_mv >= BAT_VOLTAGE_50) return BAT_LEVEL_60;
else if (voltage_mv >= BAT_VOLTAGE_40) return BAT_LEVEL_50;
else if (voltage_mv >= BAT_VOLTAGE_30) return BAT_LEVEL_40;
else if (voltage_mv >= BAT_VOLTAGE_20) return BAT_LEVEL_30;
else if (voltage_mv >= BAT_VOLTAGE_10) return BAT_LEVEL_20;
else if (voltage_mv >= BAT_VOLTAGE_MIN) return BAT_LEVEL_10;
else return BAT_LEVEL_0;
}
/**
* @brief 基于10分钟电压历史判断充放电状态
* @return 1-充电中, 0-未充电/放电
*/
static int detect_charging_by_trend(void)
{
if (g_history_count < VOLTAGE_HISTORY_SIZE) {
printf("[BAT] history not full (%d/%d), keep state:%d\r\n",
g_history_count, VOLTAGE_HISTORY_SIZE, g_charging_state);
return g_charging_state;
}
unsigned char old_index = (g_history_index + 1) % VOLTAGE_HISTORY_SIZE;
unsigned short vbat_now = g_voltage_history[(g_history_index + VOLTAGE_HISTORY_SIZE - 1) % VOLTAGE_HISTORY_SIZE];
unsigned short vbat_10min_ago = g_voltage_history[old_index];
short delta = (short)vbat_now - (short)vbat_10min_ago;
printf("[BAT] trend: now=%d, 10min_ago=%d, delta=%d\r\n",
vbat_now, vbat_10min_ago, delta);
int new_state = g_charging_state;
if (delta > VOLTAGE_RISE_THRESHOLD) {
new_state = 1;
printf("[BAT] trend: CHARGING detected (rise %dmV)\r\n", delta);
}
else if (delta < -VOLTAGE_DROP_THRESHOLD) {
new_state = 0;
printf("[BAT] trend: DISCHARGING detected (drop %dmV)\r\n", -delta);
}
else {
printf("[BAT] trend: STABLE (delta %dmV), keep state:%d\r\n", delta, g_charging_state);
}
if (new_state == g_charging_state) {
g_charging_stable_cnt = 0;
} else {
g_charging_stable_cnt++;
if (g_charging_stable_cnt < 3) {
printf("[BAT] state change pending: %d -> %d (%d/3)\r\n",
g_charging_state, new_state, g_charging_stable_cnt);
return g_charging_state;
}
g_charging_stable_cnt = 0;
printf("[BAT] state changed: %d -> %d (confirmed)\r\n", g_charging_state, new_state);
}
return new_state;
}
static void update_voltage_history(unsigned short voltage)
{
g_voltage_history[g_history_index] = voltage;
g_history_index = (g_history_index + 1) % VOLTAGE_HISTORY_SIZE;
if (g_history_count < VOLTAGE_HISTORY_SIZE) {
g_history_count++;
}
}
static int is_charging(void)
{
// 如有硬件检测引脚,可优先使用
return detect_charging_by_trend();
}
static bat_level_t limit_level_jump(bat_level_t raw_level, bat_level_t last_level, int charging)
{
bat_level_t result = last_level;
if (charging) {
if (raw_level > last_level) {
if (raw_level - last_level == 1) {
result = raw_level;
printf("[BAT] level up (charging): %d -> %d\r\n", last_level, raw_level);
} else if (raw_level - last_level > 1) {
result = (bat_level_t)(last_level + 1);
printf("[BAT] level up limited (charging): %d -> %d (raw:%d)\r\n",
last_level, result, raw_level);
}
}
} else {
if (raw_level < last_level) {
if (last_level - raw_level == 1) {
result = raw_level;
printf("[BAT] level down (discharging): %d -> %d\r\n", last_level, raw_level);
} else if (last_level - raw_level > 1) {
result = (bat_level_t)(last_level - 1);
printf("[BAT] level down limited (discharging): %d -> %d (raw:%d)\r\n",
last_level, result, raw_level);
}
}
}
return result;
}
/* 修改后的 battery_check:分阶段采样,采样窗口为6次 */
void battery_check(void)
{
static unsigned int call_count = 0; // 总调用计数
static unsigned char sample_idx = 0; // 当前已采集样本数 (0~5)
static unsigned short sample_buf[6] = {0}; // 本地缓冲(也可用全局 vbat_sample,此处直接用局部)
call_count++;
/* 判断本次是否执行ADC采样 */
unsigned char do_sample = 0;
if (call_count <= 24) { // 开机2分钟内 (24次*5s=120s)
do_sample = 1;
} else {
// 2分钟后,每36次调用采样一次 (36*5s=180s=3分钟)
if ((call_count - 25) % 36 == 0) {
do_sample = 1;
}
}
if (!do_sample) {
return; // 本次不采样,直接返回
}
/* ----- 执行一次ADC采样 ----- */
u16 vbat_vol = 0;
ql_adc_onff(1);
ql_rtos_task_sleep_ms(5);
ql_adc_onff(0);
vbat_vol = ql_get_battery_vol();
sample_buf[sample_idx] = vbat_vol;
sample_idx++;
/* 当收集满6个样本时进行平均处理 */
if (sample_idx == 6) {
unsigned short max = sample_buf[0], min = sample_buf[0];
unsigned short j = 0, vol_all = 0;
for (j = 1; j < 6; j++) {
if (max < sample_buf[j]) max = sample_buf[j];
if (min > sample_buf[j]) min = sample_buf[j];
}
for (j = 0; j < 6; j++) {
vol_all += sample_buf[j];
}
vol_all = vol_all - min - max;
vbat_vol = vol_all / 4; // 6个样本去掉最大最小,剩余4个平均
/* 更新电压历史(用于充电趋势判断) */
update_voltage_history(vbat_vol);
/* 判断充电状态 */
int charging = is_charging();
g_charging_state = charging;
/* 非充电状态下,电压不允许上升 */
if (!charging) {
if (g_vbat_vol_last > 0 && vbat_vol > g_vbat_vol_last) {
printf("[BAT] voltage rise blocked: %d -> %d (not charging)\r\n",
g_vbat_vol_last, vbat_vol);
vbat_vol = g_vbat_vol_last;
}
}
g_vbat_vol = vbat_vol;
g_vbat_vol_last = vbat_vol;
printf("vbat_vol:%d, charging:%d\r\n", g_vbat_vol, charging);
/* 计算电量等级并限制跳变 */
bat_level_t raw_level = calc_battery_level(g_vbat_vol);
bat_level_t level = limit_level_jump(raw_level, g_bat_level_last, charging);
g_bat_level_last = level;
printf("[BAT] raw_level:%d, limited_level:%d, charging:%d\r\n",
raw_level, level, charging);
if (battery_first_update_flag == 0) {
battery_first_update_flag = 1;
}
/* 低电量处理 */
if (level == BAT_LEVEL_0) {
printf("poweroff now\r\n");
app_play_tips_file(15);
ql_rtos_task_sleep_s(5);
ql_power_down(1);
} else if (level <= BAT_LEVEL_10) {
app_play_tips_file(14);
}
sample_idx = 0; // 重置样本索引,准备下一轮采集
}
/* 若未满6个,则本次只存储,不进行平均,直接返回 */
}