LFPDeltSoc20210804.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # 获取数据
  2. from LIB.BACKEND import DBManager
  3. import os
  4. import pandas as pd
  5. import numpy as np
  6. import datetime
  7. # import matplotlib.pyplot as plt
  8. #参数初始化
  9. Capacity = 53.6
  10. PackFullChrgVolt=69.99
  11. CellFullChrgVolt=3.6
  12. CellVoltNums=20
  13. CellTempNums=4
  14. FullChrgSoc=98
  15. PeakSoc=57
  16. #获取数据时间段
  17. def cal_deltsoc(sn, end_time, start_time):
  18. end_time = end_time
  19. strat_time = start_time
  20. SNnum=str(sn)
  21. sn = sn
  22. st = strat_time
  23. et = end_time
  24. dbManager = DBManager.DBManager()
  25. df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])
  26. df_bms = df_data['bms']
  27. # 计算电芯Soc差
  28. packcrnt = df_bms['总电流[A]']
  29. packvolt = df_bms['总电压[V]']
  30. SOC = df_bms['SOC[%]']
  31. SOH = df_bms['SOH[%]']
  32. bmsstat = (df_bms['充电状态']).astype(int)
  33. time = pd.to_datetime(df_bms['时间戳'], format='%Y-%m-%d %H:%M:%S')
  34. # 筛选充电数据
  35. ChgStart = []
  36. ChgEnd = []
  37. for i in range(3, len(time) - 3):
  38. if i==3 and bmsstat[i]==2 and bmsstat[i+1]==2 and bmsstat[i+2]==2:
  39. ChgStart.append(i)
  40. elif bmsstat[i-2]!=2 and bmsstat[i-1]!=2 and bmsstat[i]==2:
  41. ChgStart.append(i)
  42. elif bmsstat[i-1]==2 and bmsstat[i]!=2 and bmsstat[i+1]!=2:
  43. ChgEnd.append(i)
  44. elif i == (len(time) - 4) and bmsstat[len(bmsstat)-1] == 2 and bmsstat[len(bmsstat)-2] == 2:
  45. ChgEnd.append(len(time)-1)
  46. # 筛选充电起始Soc<46%,电芯温度>15℃的数据
  47. if ChgStart:
  48. ChgStartValid = []
  49. ChgEndValid = []
  50. for i in range(min(len(ChgStart),len(ChgEnd))):
  51. # 获取最小温度值
  52. celltemp = []
  53. for j in range(1, CellTempNums + 1):
  54. s = str(j)
  55. temp = df_bms['单体温度' + s]
  56. celltemp.append(temp[ChgEnd[i]])
  57. if SOC[ChgStart[i]] < 46 and SOC[ChgEnd[i]]>80 and min(celltemp) > 10:
  58. if ((time[ChgEnd[i]]-time[ChgStart[i]]).total_seconds())/(ChgEnd[i]-ChgStart[i])<30:
  59. ChgStartValid.append(ChgStart[i])
  60. ChgEndValid.append(ChgEnd[i])
  61. # 计算充电每个单体到达DVDQ峰值的Ah差
  62. # 定义滑动平均滤波函数
  63. def np_move_avg(a, n, mode="same"):
  64. return (np.convolve(a, np.ones((n,)) / n, mode=mode))
  65. # 定义函数:切片Soc>50且Soc<80,并寻找峰值返回峰值点的时间
  66. def data_search(t, soc, cellvolt1, packcrnt1):
  67. cellvolt2 = np_move_avg(cellvolt1, 5, mode="same")
  68. Soc = 0
  69. Ah = 0
  70. Volt = [cellvolt2[0]]
  71. DV_Volt = []
  72. DQ_Ah = []
  73. DVDQ = []
  74. time1 = []
  75. soc1 = []
  76. soc2 = []
  77. for m in range(1, len(t)):
  78. Step = (t[m] - t[m - 1]).total_seconds()
  79. Soc = Soc - packcrnt1[m] * Step * 100 / (3600 * Capacity)
  80. Ah = Ah - packcrnt1[m] * Step / 3600
  81. if (cellvolt2[m] - Volt[-1]) > 0.9 and Ah>0:
  82. DQ_Ah.append(Ah)
  83. Volt.append(cellvolt2[m])
  84. DV_Volt.append(Volt[-1] - Volt[-2])
  85. DVDQ.append((DV_Volt[-1]) / DQ_Ah[-1])
  86. Ah = 0
  87. time1.append(t[m])
  88. soc1.append(Soc)
  89. soc2.append(soc[m])
  90. df_Data1 = pd.DataFrame({'Time': time1,
  91. 'SOC': soc2,
  92. 'DVDQ': DVDQ,
  93. 'AhSOC': soc1})
  94. df_Data1 = df_Data1[(df_Data1['SOC'] > 50) & (df_Data1['SOC'] < 75)]
  95. # 寻找峰值点,且峰值点个数>3
  96. if len(df_Data1['DVDQ'])>2:
  97. PeakIndex = df_Data1['DVDQ'].idxmax()
  98. df_Data2 = df_Data1[
  99. (df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
  100. if len(df_Data2) > 3:
  101. return df_Data1['AhSOC'][PeakIndex]
  102. else:
  103. df_Data1 = df_Data1.drop([PeakIndex])
  104. PeakIndex = df_Data1['DVDQ'].idxmax()
  105. return df_Data1['AhSOC'][PeakIndex]
  106. # 计算最大最小Soc差
  107. if ChgStartValid:
  108. DetaSoc2 = []
  109. DetaSoc=[]
  110. DetaSoc_SN=[]
  111. DetaSoc_time=[]
  112. for i in range(len(ChgStartValid)):
  113. DetaSoc1 = []
  114. for j in range(1, CellVoltNums + 1):
  115. s = str(j)
  116. cellvolt = df_bms['单体电压' + s]
  117. cellvolt = list(cellvolt[ChgStartValid[i]:ChgEndValid[i]])
  118. Time = list(time[ChgStartValid[i]:ChgEndValid[i]])
  119. Packcrnt = list(packcrnt[ChgStartValid[i]:ChgEndValid[i]])
  120. SOC1 = list(SOC[ChgStartValid[i]:ChgEndValid[i]])
  121. a = data_search(Time, SOC1, cellvolt, Packcrnt)
  122. if a:
  123. DetaSoc1.append(a) # 计算到达峰值点的累计Soc
  124. if DetaSoc1:
  125. DetaSoc2.append(max(DetaSoc1) - min(DetaSoc1))
  126. DetaSocMean = np.mean(DetaSoc2)
  127. DetaSoc.append(DetaSocMean)
  128. DetaSoc_SN.append(SNnum)
  129. DetaSoc_time.append(time[ChgStartValid[-1]])
  130. result_DetaSoc={'time':DetaSoc_time,
  131. 'SN号':DetaSoc_SN,
  132. 'Soc差':DetaSoc}
  133. Result_DetaSoc=pd.DataFrame(result_DetaSoc)
  134. return Result_DetaSoc
  135. return pd.DataFrame()