LFPLeakCurrent20210812.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # 获取数据
  2. from LIB.BACKEND import DBManager
  3. import os
  4. import pandas as pd
  5. import numpy as np
  6. import bisect
  7. import datetime
  8. # import matplotlib.pyplot as plt
  9. #参数输入
  10. Capacity = 53.6
  11. PackFullChrgVolt=69.99
  12. CellFullChrgVolt=3.37
  13. CellVoltNums=20
  14. CellTempNums=4
  15. FullChrgSoc=98
  16. CellVoltPort=[3.357,3.358,3.359,3.36,3.361]
  17. PeakSoc=57
  18. # #40Ah-OCV
  19. # LookTab_SOC = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
  20. # LookTab_OCV = [3.3159, 3.4502, 3.4904, 3.5277, 3.5590, 3.5888, 3.6146, 3.6312, 3.6467, 3.6642, 3.6865, 3.7171, 3.7617,
  21. # 3.8031, 3.8440, 3.8888, 3.9376, 3.9891, 4.0451, 4.1068, 4.1830]
  22. #55Ah-OCV
  23. LookTab_SOC = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
  24. LookTab_OCV = [3.1820, 3.2250, 3.2730, 3.2840, 3.2860, 3.2920, 3.3210, 3.3260, 3.3270, 3.3270, 3.3640]
  25. # 获取数据时间段
  26. def cal_LFPLeakCurrent(sn, end_time, start_time):
  27. end_time = end_time
  28. strat_time = start_time
  29. sn = sn
  30. st = strat_time
  31. et = end_time
  32. dbManager = DBManager.DBManager()
  33. df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])
  34. df_bms = df_data['bms']
  35. #寻找电压最大值
  36. packcrnt=df_bms['总电流[A]']
  37. SOC=df_bms['SOC[%]']
  38. bmsstat=df_bms['充电状态']
  39. time= pd.to_datetime(df_bms['时间戳'], format='%Y-%m-%d %H:%M:%S')
  40. #第一步:筛选充电数据
  41. ChgStart=[]
  42. ChgEnd=[]
  43. for i in range(3, len(time) - 3):
  44. if i==3 and bmsstat[i]==2 and bmsstat[i+1]==2 and bmsstat[i+2]==2:
  45. ChgStart.append(i)
  46. elif bmsstat[i-2]!=2 and bmsstat[i-1]!=2 and bmsstat[i]==2:
  47. ChgStart.append(i)
  48. elif bmsstat[i-1]==2 and bmsstat[i]!=2 and bmsstat[i+1]!=2:
  49. ChgEnd.append(i)
  50. elif i == (len(time) - 4) and bmsstat[len(bmsstat)-1] == 2 and bmsstat[len(bmsstat)-2] == 2:
  51. ChgEnd.append(len(time)-1)
  52. #第二步:筛选充电起始Soc<45%,且单体最小电压>3.37V的数据
  53. ChgStartValid=[]
  54. ChgEndValid=[]
  55. if ChgStart:
  56. for i in range(len(ChgEnd)):
  57. #寻找最小电压值
  58. cellvolt = []
  59. for j in range(1, CellVoltNums+1):
  60. s = str(j)
  61. volt = df_bms['单体电压' + s]/1000
  62. cellvolt.append(max(volt[ChgStart[i]:ChgEnd[i]]))
  63. if min(cellvolt)>CellFullChrgVolt and SOC[ChgStart[i]]<40 and (ChgEnd[i]-ChgStart[i])>10:
  64. if ((time[ChgEnd[i]]-time[ChgStart[i]]).total_seconds())/(ChgEnd[i]-ChgStart[i])<30:
  65. ChgStartValid.append(ChgStart[i])
  66. ChgEndValid.append(ChgEnd[i])
  67. #第三步:计算充电每个单体到达3.368V的Ah差
  68. #定义寻找电压3.368V的数据点
  69. def data_search(data1,data2,data3,data4):
  70. Soc=0
  71. for m in range(1,len(data1)):
  72. t=(data2[m]-data2[m-1]).total_seconds()
  73. Soc=Soc-data3[m]*t/(3600*Capacity)
  74. if data1[m]>data4:
  75. DetaT=(data2[m]-data2[0]).total_seconds()
  76. return Soc,m
  77. break
  78. if ChgStartValid:
  79. df_DetaTime=pd.DataFrame()
  80. df_DetaTime1=pd.DataFrame()
  81. df_detatime=pd.DataFrame()
  82. for i in range(len(ChgStartValid)):
  83. DetaSoc1=[]
  84. DetaSoc2 = []
  85. DetaSoc=[]
  86. a=list(range(5))
  87. b=list(range(5))
  88. #计算1-10号电芯到达特定电压值得时间和SOC
  89. for j in range(1, CellVoltNums-9):
  90. s = str(j)
  91. cellvolt = df_bms['单体电压' + s]/1000
  92. cellvolt=list(cellvolt[ChgStartValid[i]:ChgEndValid[i]])
  93. Time=list(time[ChgStartValid[i]:ChgEndValid[i]])
  94. Packcrnt=list(packcrnt[ChgStartValid[i]:ChgEndValid[i]])
  95. for k in range(len(CellVoltPort)):
  96. a[k],b[k]=data_search(cellvolt,Time,Packcrnt,CellVoltPort[k])
  97. DetaSoc1.append(np.mean(a)) #计算到达3.368V的时长
  98. # DetaT.append((Time[b]-Time[0]).total_seconds())
  99. #计算1-10号电芯到达特定电压值的平均Soc
  100. Socmean1=(sum(DetaSoc1)-max(DetaSoc1)-min(DetaSoc1))/(len(DetaSoc1)-2)
  101. # Tmean=np.mean(DetaT)
  102. ##计算11-20号电芯到达特定电压值得时间和SOC
  103. for j in range(11, CellVoltNums+1):
  104. s = str(j)
  105. cellvolt = df_bms['单体电压' + s]/1000
  106. cellvolt=list(cellvolt[ChgStartValid[i]:ChgEndValid[i]])
  107. Time=list(time[ChgStartValid[i]:ChgEndValid[i]])
  108. Packcrnt=list(packcrnt[ChgStartValid[i]:ChgEndValid[i]])
  109. for k in range(len(CellVoltPort)):
  110. a[k],b[k]=data_search(cellvolt,Time,Packcrnt,CellVoltPort[k])
  111. DetaSoc2.append(np.mean(a)) #计算到达3.368V的时长
  112. #计算11-20号电芯到达特定电压值的平均Soc
  113. Socmean2=(sum(DetaSoc2)-max(DetaSoc2)-min(DetaSoc2))/(len(DetaSoc2)-2)
  114. #计算每个电芯的Soc差
  115. DetaSoc3=DetaSoc1+DetaSoc2
  116. for j in range(len(DetaSoc3)):
  117. if j<10:
  118. Socmean=Socmean1
  119. else:
  120. Socmean=Socmean2
  121. DetaSoc.append(DetaSoc3[j]-Socmean)
  122. # DetaSoc.append((DetaT[j]-Tmean)*9.5/(Capacity*3600))
  123. df_DetaTime[time[ChgStartValid[i]]]=DetaSoc
  124. #漏电流计算
  125. column=[]
  126. time1=[]
  127. sn1=[]
  128. for index, row in df_DetaTime.iteritems():
  129. column.append(index) #提取列名称
  130. for i in range(1,len(column)):#计算漏电流值
  131. df_DetaTime1[column[i]] = df_DetaTime.apply(lambda x: (x[column[i-1]] - x[column[i]])*1000*Capacity*3600/((column[i]-column[i-1]).total_seconds()), axis=1)
  132. time1.append(column[i])
  133. sn1.append(sn)
  134. df_detatime['time']=time1
  135. df_detatime['sn']=sn1
  136. for i in range(CellVoltNums):
  137. cell=[]
  138. for j in range(1,len(column)):
  139. cell.append(df_DetaTime1[column[j]][i])
  140. df_detatime['cell'+str(i+1)]=cell
  141. return df_detatime
  142. return pd.DataFrame()