fix: 获取电量错误

This commit is contained in:
2026-08-13 11:20:29 +08:00
parent 06994c5905
commit 1fee464924
3 changed files with 13 additions and 12 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
id: t11
name: t11
version: 2.16.3
version: 2.16.4
author: t11
icon: ''
desc: t11
+11 -10
View File
@@ -5,10 +5,11 @@
提供电压、电流监测和充电状态检测
"""
import config
import os
import subprocess
from logger_manager import logger_manager
from maix import time as maix_time
_INA226_PRESENT = None
@@ -85,8 +86,8 @@ def get_bus_voltage():
def get_current():
"""
读取电流(单位:mA
正数表示电,负数表示放电
当前电源板实测:正数表示电,负数表示充电。
INA226 电流计算公式:
Current = (Current Register Value) × Current_LSB
Current_LSB = 0.001 × CALIBRATION_VALUE / 4096
@@ -96,13 +97,13 @@ def get_current():
return 0.0
raw = read_register(config.REG_CURRENT)
# INA226 电流寄存器是16位有符号整数
# 最高位是符号位0=正(充电),1=负(放电)
# 最高位是符号位;电流方向含义取决于电源板的采样电阻接线方向。
# 计算 Current_LSB(根据 CALIBRATION_VALUE
current_lsb = 0.001 * config.CALIBRATION_VALUE / 4096 # 单位:A
# 处理有符号数:如果最高位为1,转换为负数
if raw & 0x8000: # 最高位为1,表示负数(放电)
if raw & 0x8000:
signed_raw = raw - 0x10000 # 转换为有符号整数
else: # 最高位为0,表示正数(充电)
else:
signed_raw = raw
# 转换为毫安
current_ma = signed_raw * current_lsb * 1000
@@ -119,17 +120,17 @@ def get_current():
def is_charging(threshold_ma=10.0):
"""
检测是否在充电(通过电流方向判断)
Args:
threshold_ma: 电流阈值(毫安),超过此值认为在充电,默认10mA
Returns:
True: 正在充电
False: 未充电或读取失败
"""
try:
current = get_current()
is_charge = current > threshold_ma
is_charge = current < -abs(float(threshold_ma))
return is_charge
except Exception as e:
logger = logger_manager.logger
@@ -159,7 +160,7 @@ def voltage_to_percent(voltage):
return 0
if v <= 0:
return 0
return int(int(_BATTERY_MONITOR.get_soc(v) * 10) / 10) # 截断而不是四舍五入
return int(int(_BATTERY_MONITOR.get_soc(v) * 10) / 10) # 截断而不是四舍五入
class BatteryMonitor:
+1 -1
View File
@@ -4,6 +4,6 @@
应用版本号
每次 OTA 更新时,只需要更新这个文件中的版本号
"""
VERSION = '2.16.3'
VERSION = '2.16.4'