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) # 红色