33 lines
945 B
Python
33 lines
945 B
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
import numpy as np
|
||
|
||
# 已知总电压与电量百分比(SoC)的对应关系
|
||
total_voltages = [25.2, 24.6, 24.0, 23.4, 22.8, 22.2, 21.6, 21.0, 20.4, 19.8, 18.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))
|
||
|
||
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) # 红色
|