NCMSoh 20210716.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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=17
  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. now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  30. now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S')
  31. start_time=now_time-datetime.timedelta(days=31)
  32. end_time=str(now_time)
  33. strat_time=str(start_time)
  34. #输入一个含有‘SN号’的xlsx
  35. SNdata = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='6040骑享')
  36. SNnums=SNdata['SN号']
  37. for k in range(len(SNnums)):
  38. SNnum=str(SNnums[k])
  39. sn = SNnum
  40. st = strat_time
  41. et = end_time
  42. dbManager = DBManager.DBManager()
  43. df_data = dbManager.get_data(sn=sn, start_time=st, end_time=et, data_groups=['bms'])
  44. data = df_data['bms']
  45. # print(data)
  46. packcrnt=data['总电流[A]']
  47. packvolt=data['总电压[V]']
  48. SOC=data['SOC[%]']
  49. SOH=data['SOH[%]']
  50. bmsstat=data['充电状态']
  51. time= pd.to_datetime(data['时间戳'], format='%Y-%m-%d %H:%M:%S')
  52. #第一步:筛选充电数据
  53. if len(packcrnt)>100:
  54. ChgStart=[]
  55. ChgEnd=[]
  56. for i in range(3, len(time) - 3):
  57. if i==3 and bmsstat[i]==2 and bmsstat[i+1]==2 and bmsstat[i+2]==2:
  58. ChgStart.append(i)
  59. elif bmsstat[i-2]!=2 and bmsstat[i-1]!=2 and bmsstat[i]==2:
  60. ChgStart.append(i)
  61. elif bmsstat[i-1]==2 and bmsstat[i]!=2 and bmsstat[i+1]!=2:
  62. ChgEnd.append(i-1)
  63. elif i == (len(time) - 4) and bmsstat[len(bmsstat)-1] == 2 and bmsstat[len(bmsstat)-2] == 2:
  64. ChgEnd.append(len(time)-2)
  65. #第二步:筛选充电起始Soc<45% & SOC>85%,电芯温度>5℃
  66. ChgStartValid1=[]
  67. ChgEndValid1=[]
  68. ChgStartValid2=[]
  69. ChgEndValid2=[]
  70. StandingNum=[]
  71. for i in range(min(len(ChgStart),len(ChgEnd))):
  72. #获取最小温度值
  73. celltemp = []
  74. for j in range(1, CellTempNums+1):
  75. s = str(j)
  76. temp = data['单体温度' + s]
  77. celltemp.append(temp[ChgEnd[i]])
  78. #去除电流0点
  79. for k in range(ChgStart[i],ChgEnd[i]):
  80. if packcrnt[k]<-0.5 and packcrnt[k+1]>-0.5 and packcrnt[k+2]>-0.5 and packcrnt[k+3]>-0.5:
  81. ChgEnd[i]=k
  82. #计算最大packvolt
  83. if len(packvolt[ChgStart[i]:ChgEnd[i]])>0:
  84. packvoltMAX=max(packvolt[ChgStart[i]:ChgEnd[i]])
  85. #筛选满足2点法计算的数据
  86. StandingTime=0
  87. StandingTime1=0
  88. StandingTime2=0
  89. if SOC[ChgEnd[i]]>85 and SOC[ChgStart[i]]<45 and min(celltemp)>5:
  90. for m in range(min(len(packcrnt)-ChgEnd[i]-2,ChgStart[i]-2)):
  91. if abs(packcrnt[ChgStart[i] - m - 1]) < 0.1:
  92. StandingTime = StandingTime + (time[ChgStart[i] - m] - time[ChgStart[i] - m - 1]).total_seconds()
  93. if abs(packcrnt[ChgEnd[i] + m + 1]) < 0.1:
  94. StandingTime1 = StandingTime1 + (time[ChgEnd[i] + m + 1] - time[ChgEnd[i] + m]).total_seconds()
  95. if StandingTime > 900 and StandingTime1>900 and ((time[ChgEnd[i]]-time[ChgStart[i]]).total_seconds())/(ChgEnd[i]-ChgStart[i])<60: #筛选静置时间>15min且慢充过程丢失数据少
  96. ChgStartValid1.append(ChgStart[i])
  97. ChgEndValid1.append(ChgEnd[i])
  98. StandingNum.append(m)
  99. break
  100. if abs(packcrnt[ChgStart[i] - m - 2])>0.5 and abs(packcrnt[ChgEnd[i] + m + 2])>0.5:
  101. break
  102. # 计算soh
  103. Soh1=[]
  104. Soh2=[]
  105. Time1=[]
  106. Bms_Soh1=[]
  107. Soh_Err1=[]
  108. sn_list1=[]
  109. #两点法计算Soh
  110. if len(ChgStartValid1)>0:
  111. for i in range(len(ChgStartValid1)):
  112. #计算Ah
  113. Ah=0
  114. for j in range(ChgStartValid1[i],ChgEndValid1[i]):
  115. Step=(time[j+1]-time[j]).total_seconds()
  116. Ah=Ah-packcrnt[j+1]*Step/3600
  117. #计算每个电芯的Soh
  118. for j in range(1, CellVoltNums+1):
  119. s = str(j)
  120. cellvolt = data['单体电压' + s]/1000
  121. OCVStart=cellvolt[ChgStartValid1[i]-2]
  122. OCVEnd=cellvolt[ChgEndValid1[i]+StandingNum[i]]
  123. #soh
  124. Ocv_Soc1=np.interp(OCVStart,LookTab_OCV,LookTab_SOC)
  125. Ocv_Soc2=np.interp(OCVEnd,LookTab_OCV,LookTab_SOC)
  126. Soh2.append(Ah*100/((Ocv_Soc2-Ocv_Soc1)*0.01*Capacity))
  127. Soh1.append(np.mean(Soh2))
  128. Bms_Soh1.append(SOH[ChgStartValid1[i]])
  129. Soh_Err1.append(Bms_Soh1[-1]-Soh1[-1])
  130. Time1.append(time[ChgStartValid1[i]])
  131. sn_list1.append(SNnum)
  132. # Soh3.append(np.mean(Soh1))
  133. # Bms_Soh3.append(np.mean(Bms_Soh1))
  134. # Soh_Err3.append(np.mean(Soh_Err1))
  135. # Time3.append(time[ChgStartValid1[-1]])
  136. # sn_list.append(SNnum)
  137. #第四步:将数据存入Excel
  138. result_soh2={'时间': Time1,
  139. 'SN号': sn_list1,
  140. 'BMS_SOH': Bms_Soh1,
  141. 'SOH': Soh1,
  142. 'SOH误差': Soh_Err1}
  143. Result_Soh2=pd.DataFrame(result_soh2)
  144. Result_Soh2.to_csv('BMS_SOH_'+SNnum+'.csv',encoding='GB18030')
  145. # result_soh1={'时间': Time3,
  146. # 'SN号':sn_list,
  147. # 'BMS_SOH': Bms_Soh3,
  148. # 'SOH': Soh3,
  149. # 'SOH误差': Soh_Err3}
  150. # Result_Soh1=pd.DataFrame(result_soh1)
  151. # print(Result_Soh1)
  152. # Result_Soh1.to_csv('BMS_SOH_'+'6040'+'.csv',encoding='GB18030')