sample_code/soc_pkg/代码分离/battery_logic.py
2025-04-01 08:52:50 +08:00

30 lines
932 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
# 已知总电压与电量百分比SoC的对应关系
total_voltages = [12.6, 12.3, 12.0, 11.7, 11.4, 11.1, 10.8, 10.5, 10.2, 9.9, 9.0]
socs = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
def get_soc_from_total_voltage(total_voltage):
"""
根据总电压计算电量百分比SoC
:param total_voltage: 总电压值
:return: 电量百分比0% 到 100%
"""
soc = np.interp(total_voltage, total_voltages[::-1], socs[::-1])
return max(0, min(100, soc)) # 确保 SoC 在 0% 到 100% 之间
def get_battery_color(soc):
"""
根据电量百分比返回电池颜色
:param soc: 电量百分比
:return: QColor 对象表示的颜色
"""
if soc > 80:
return (0, 255, 0) # 绿色
elif soc > 50:
return (255, 255, 0) # 黄色
elif soc > 20:
return (255, 165, 0) # 橙色
else:
return (255, 0, 0) # 红色