NCMSoh_4842.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = 41
  10. PackFullChrgVolt=69.99
  11. CellFullChrgVolt=3.5
  12. CellVoltNums=14
  13. CellTempNums=4
  14. FullChrgSoc=98
  15. PeakSoc=57
  16. # #40Ah-OCV
  17. LookTab_SOC = [0, 3.534883489, 8.358178409, 13.18141871, 18.00471528, 22.82796155, 27.65123833, 32.47444668, 37.29772717, 42.12099502, 46.94423182, 51.76744813, 56.59070685, 61.4139927, 66.23719857, 71.0604667, 75.88373853, 80.70702266, 85.5302705, 90.35352009, 95.17676458, 100]
  18. LookTab_OCV = [3.3159, 3.4384, 3.4774, 3.5156, 3.5478, 3.5748, 3.6058, 3.6238, 3.638, 3.6535, 3.6715, 3.6951, 3.7279, 3.7757, 3.8126, 3.8529, 3.8969, 3.9446, 3.9946, 4.0491, 4.109, 4.183]
  19. # #55Ah-OCV
  20. # LookTab_SOC = [0.00, 2.40, 6.38, 10.37, 14.35, 18.33, 22.32, 26.30, 30.28, 35.26, 40.24, 45.22, 50.20, 54.19, 58.17, 60.16, 65.14, 70.12, 75.10, 80.08, 84.06, 88.05, 92.03, 96.02, 100.00]
  21. # LookTab_OCV = [2.7151, 3.0298, 3.1935, 3.2009, 3.2167, 3.2393, 3.2561, 3.2703, 3.2843, 3.2871, 3.2874, 3.2868, 3.2896, 3.2917, 3.2967, 3.3128, 3.3283, 3.3286, 3.3287, 3.3288, 3.3289, 3.3296, 3.3302, 3.3314, 3.3429]
  22. #参数初始化
  23. Soh3=[]
  24. Time3=[]
  25. Bms_Soh3=[]
  26. Soh_Err3=[]
  27. sn_list=[]
  28. #获取数据时间段
  29. def cal_soh(sn, end_time, start_time):
  30. end_time = end_time
  31. strat_time = start_time
  32. SNnum=str(sn)
  33. sn = sn
  34. st = strat_time
  35. et = end_time
  36. dbManager = DBManager.DBManager()
  37. df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])
  38. data = df_data['bms']
  39. # print(data)
  40. packcrnt=data['总电流[A]']
  41. packvolt=data['总电压[V]']
  42. SOC=data['SOC[%]']
  43. SOH=data['SOH[%]']
  44. bmsstat=data['充电状态']
  45. time= pd.to_datetime(data['时间戳'], format='%Y-%m-%d %H:%M:%S')
  46. #第一步:筛选充电数据
  47. if len(packcrnt)>100:
  48. ChgStart=[]
  49. ChgEnd=[]
  50. for i in range(3, len(time) - 3):
  51. if i==3 and bmsstat[i]==2 and bmsstat[i+1]==2 and bmsstat[i+2]==2:
  52. ChgStart.append(i)
  53. elif bmsstat[i-2]!=2 and bmsstat[i-1]!=2 and bmsstat[i]==2:
  54. ChgStart.append(i)
  55. elif bmsstat[i-1]==2 and bmsstat[i]!=2 and bmsstat[i+1]!=2:
  56. ChgEnd.append(i-1)
  57. elif i == (len(time) - 4) and bmsstat[len(bmsstat)-1] == 2 and bmsstat[len(bmsstat)-2] == 2:
  58. ChgEnd.append(len(time)-2)
  59. #第二步:筛选充电起始Soc<45% & SOC>85%,电芯温度>5℃
  60. ChgStartValid1=[]
  61. ChgEndValid1=[]
  62. ChgStartValid2=[]
  63. ChgEndValid2=[]
  64. StandingNum=[]
  65. for i in range(min(len(ChgStart),len(ChgEnd))):
  66. #获取最小温度值
  67. celltemp = []
  68. for j in range(1, CellTempNums+1):
  69. s = str(j)
  70. temp = data['单体温度' + s]
  71. celltemp.append(temp[ChgEnd[i]])
  72. #去除电流0点
  73. for k in range(ChgStart[i],ChgEnd[i]):
  74. if packcrnt[k]<-0.5 and packcrnt[k+1]>-0.5 and packcrnt[k+2]>-0.5 and packcrnt[k+3]>-0.5:
  75. ChgEnd[i]=k
  76. #计算最大packvolt
  77. if len(packvolt[ChgStart[i]:ChgEnd[i]])>0:
  78. packvoltMAX=max(packvolt[ChgStart[i]:ChgEnd[i]])
  79. #筛选满足2点法计算的数据
  80. StandingTime=0
  81. StandingTime1=0
  82. StandingTime2=0
  83. if SOC[ChgEnd[i]]>85 and SOC[ChgStart[i]]<45 and min(celltemp)>5:
  84. for m in range(min(len(packcrnt)-ChgEnd[i]-2,ChgStart[i]-2)):
  85. if abs(packcrnt[ChgStart[i] - m - 1]) < 0.1:
  86. StandingTime = StandingTime + (time[ChgStart[i] - m] - time[ChgStart[i] - m - 1]).total_seconds()
  87. if abs(packcrnt[ChgEnd[i] + m + 1]) < 0.1:
  88. StandingTime1 = StandingTime1 + (time[ChgEnd[i] + m + 1] - time[ChgEnd[i] + m]).total_seconds()
  89. if StandingTime > 900 and StandingTime1>900 and ((time[ChgEnd[i]]-time[ChgStart[i]]).total_seconds())/(ChgEnd[i]-ChgStart[i])<60: #筛选静置时间>15min且慢充过程丢失数据少
  90. ChgStartValid1.append(ChgStart[i])
  91. ChgEndValid1.append(ChgEnd[i])
  92. StandingNum.append(m)
  93. break
  94. if abs(packcrnt[ChgStart[i] - m - 2])>0.5 and abs(packcrnt[ChgEnd[i] + m + 2])>0.5:
  95. break
  96. # 计算soh
  97. Soh1=[]
  98. Soh2=[]
  99. Time1=[]
  100. Bms_Soh1=[]
  101. Soh_Err1=[]
  102. sn_list1=[]
  103. #两点法计算Soh
  104. if len(ChgStartValid1)>0:
  105. for i in range(len(ChgStartValid1)):
  106. #计算Ah
  107. Ah=0
  108. for j in range(ChgStartValid1[i],ChgEndValid1[i]):
  109. Step=(time[j+1]-time[j]).total_seconds()
  110. Ah=Ah-packcrnt[j+1]*Step/3600
  111. #计算每个电芯的Soh
  112. for j in range(1, CellVoltNums+1):
  113. s = str(j)
  114. cellvolt = data['单体电压' + s]/1000
  115. OCVStart=cellvolt[ChgStartValid1[i]-2]
  116. OCVEnd=cellvolt[ChgEndValid1[i]+StandingNum[i]]
  117. #soh
  118. Ocv_Soc1=np.interp(OCVStart,LookTab_OCV,LookTab_SOC)
  119. Ocv_Soc2=np.interp(OCVEnd,LookTab_OCV,LookTab_SOC)
  120. Soh2.append(Ah*100/((Ocv_Soc2-Ocv_Soc1)*0.01*Capacity))
  121. Soh1.append(np.mean(Soh2))
  122. Bms_Soh1.append(SOH[ChgStartValid1[i]])
  123. Soh_Err1.append(Bms_Soh1[-1]-Soh1[-1])
  124. Time1.append(time[ChgStartValid1[i]])
  125. sn_list1.append(SNnum)
  126. # Soh3.append(np.mean(Soh1))
  127. # Bms_Soh3.append(np.mean(Bms_Soh1))
  128. # Soh_Err3.append(np.mean(Soh_Err1))
  129. # Time3.append(time[ChgStartValid1[-1]])
  130. # sn_list.append(SNnum)
  131. #第四步:将数据存入Excel
  132. result_soh2={'时间': Time1,
  133. 'SN号': sn_list1,
  134. 'BMS_SOH': Bms_Soh1,
  135. 'SOH': Soh1,
  136. 'SOH误差': Soh_Err1}
  137. Result_Soh2=pd.DataFrame(result_soh2)
  138. # Result_Soh2.to_csv('BMS_SOH_'+SNnum+'.csv',encoding='GB18030')
  139. return Result_Soh2
  140. return pd.DataFrame()
  141. # result_soh1={'时间': Time3,
  142. # 'SN号':sn_list,
  143. # 'BMS_SOH': Bms_Soh3,
  144. # 'SOH': Soh3,
  145. # 'SOH误差': Soh_Err3}
  146. # Result_Soh1=pd.DataFrame(result_soh1)
  147. # print(Result_Soh1)
  148. # Result_Soh1.to_csv('BMS_SOH_'+'6040'+'.csv',encoding='GB18030')