CBMSBatUniform.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import pandas as pd
  2. import numpy as np
  3. import datetime
  4. import bisect
  5. import matplotlib.pyplot as plt
  6. from LIB.MIDDLE.CellStateEstimation.Common import BatParam
  7. class BatUniform:
  8. def __init__(self,sn,celltype,df_bms): #参数初始化
  9. self.sn=sn
  10. self.celltype=celltype
  11. self.param=BatParam.BatParam(celltype)
  12. self.df_bms=df_bms
  13. self.packcrnt=df_bms['总电流[A]']*self.param.PackCrntDec
  14. self.packvolt=df_bms['总电压[V]']
  15. self.bms_soc=df_bms['SOC[%]']
  16. self.bmstime= pd.to_datetime(df_bms['时间戳'], format='%Y-%m-%d %H:%M:%S')
  17. def batuniform(self):
  18. if self.celltype==1 or self.celltype==2 or self.celltype==3 or self.celltype==4:
  19. df_res=self._ncm_uniform()
  20. return df_res
  21. elif self.celltype==99:
  22. df_res=self._lfp_uniform()
  23. return df_res
  24. else:
  25. return pd.DataFrame()
  26. #定义滑动滤波函数........................................................................................................................................
  27. def _np_move_avg(self,a, n, mode="same"):
  28. return (np.convolve(a, np.ones((n,)) / n, mode=mode))
  29. #寻找当前行数据的最小温度值................................................................................................................................
  30. def _celltemp_weight(self,num):
  31. celltemp = []
  32. for j in range(1, self.param.CellTempNums+1):
  33. s = str(j)
  34. celltemp.append(self.df_bms.loc[num,'单体温度' + s])
  35. celltemp.remove(min(celltemp))
  36. self.celltemp=celltemp
  37. if self.celltype==99:
  38. if min(celltemp)>=20:
  39. self.tempweight=1
  40. self.StandardStandingTime=2400
  41. elif min(celltemp)>=10:
  42. self.tempweight=0.6
  43. self.StandardStandingTime=3600
  44. elif min(celltemp)>=5:
  45. self.tempweight=0.
  46. self.StandardStandingTime=3600
  47. else:
  48. self.tempweight=0.1
  49. self.StandardStandingTime=7200
  50. else:
  51. if min(celltemp)>=20:
  52. self.tempweight=1
  53. self.StandardStandingTime=1800
  54. elif min(celltemp)>=10:
  55. self.tempweight=0.8
  56. self.StandardStandingTime=2400
  57. elif min(celltemp)>=5:
  58. self.tempweight=0.6
  59. self.StandardStandingTime=3600
  60. else:
  61. self.tempweight=0.2
  62. self.StandardStandingTime=7200
  63. #获取当前行所有电压数据............................................................................................................................
  64. def _cellvolt_get(self,num):
  65. cellvolt=[]
  66. for j in range(1, self.param.CellVoltNums+1):
  67. s = str(j)
  68. cellvolt.append(self.df_bms.loc[num,'单体电压' + s]/1000)
  69. return(cellvolt)
  70. #寻找DVDQ的峰值点,并返回..........................................................................................................................
  71. def _dvdq_peak(self, time, soc, cellvolt, packcrnt):
  72. cellvolt = self._np_move_avg(cellvolt, 3, mode="same")
  73. Soc = 0
  74. Ah = 0
  75. Volt = cellvolt[0]
  76. DV_Volt = []
  77. DQ_Ah = []
  78. DVDQ = []
  79. time1 = []
  80. soc1 = []
  81. soc2 = []
  82. xvolt=[]
  83. for m in range(1, len(time)):
  84. Step = (time[m] - time[m - 1]).total_seconds()
  85. Soc = Soc - packcrnt[m] * Step * 100 / (3600 * self.param.Capacity)
  86. Ah = Ah - packcrnt[m] * Step / 3600
  87. if (cellvolt[m]-Volt)>0.0019 and Ah>0:
  88. DQ_Ah.append(Ah)
  89. DV_Volt.append(cellvolt[m]-Volt)
  90. DVDQ.append((DV_Volt[-1])/DQ_Ah[-1])
  91. xvolt.append(cellvolt[m])
  92. Volt=cellvolt[m]
  93. Ah = 0
  94. soc1.append(Soc)
  95. time1.append(time[m])
  96. soc2.append(soc[m])
  97. #切片,去除前后10min的数据
  98. df_Data1 = pd.DataFrame({'time': time1,
  99. 'SOC': soc2,
  100. 'DVDQ': DVDQ,
  101. 'AhSoc': soc1,
  102. 'DQ_Ah':DQ_Ah,
  103. 'DV_Volt':DV_Volt,
  104. 'XVOLT':xvolt})
  105. start_time=df_Data1.loc[0,'time']
  106. start_time=start_time+datetime.timedelta(seconds=900)
  107. end_time=df_Data1.loc[len(time1)-1,'time']
  108. end_time=end_time-datetime.timedelta(seconds=1200)
  109. if soc2[0]<36:
  110. df_Data1=df_Data1[(df_Data1['SOC']>40) & (df_Data1['SOC']<80)]
  111. else:
  112. df_Data1=df_Data1[(df_Data1['time']>start_time) & (df_Data1['SOC']<80)]
  113. df_Data1=df_Data1[(df_Data1['XVOLT']>self.param.PeakVoltLowLmt) & (df_Data1['XVOLT']<self.param.PeakVoltUpLmt)]
  114. # print(packcrnt[int(len(time)/2)], min(self.celltemp))
  115. # ax1 = plt.subplot(3, 1, 1)
  116. # plt.plot(df_Data1['XVOLT'],df_Data1['DVDQ'],'g*-')
  117. # plt.xlabel('Volt/V')
  118. # plt.ylabel('DV/DQ')
  119. # plt.legend()
  120. # ax1 = plt.subplot(3, 1, 2)
  121. # plt.plot(df_Data1['SOC'],df_Data1['XVOLT'],'y*-')
  122. # plt.xlabel('SOC/%')
  123. # plt.ylabel('Volt/V')
  124. # plt.legend()
  125. # ax1 = plt.subplot(3, 1, 3)
  126. # plt.plot(df_Data1['SOC'], df_Data1['DVDQ'], 'r*-')
  127. # plt.xlabel('SOC/%')
  128. # plt.ylabel('DV/DQ')
  129. # plt.legend()
  130. # plt.show()
  131. if len(df_Data1)>2: #寻找峰值点,且峰值点个数>2
  132. PeakIndex = df_Data1['DVDQ'].idxmax()
  133. df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
  134. if len(df_Data2) > 2 and df_Data1.loc[PeakIndex,'XVOLT']<self.param.PeakVoltUpLmt-0.019:
  135. return df_Data1['AhSoc'][PeakIndex]
  136. else:
  137. df_Data1 = df_Data1.drop([PeakIndex])
  138. PeakIndex = df_Data1['DVDQ'].idxmax()
  139. df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
  140. if len(df_Data2) > 2 and df_Data1.loc[PeakIndex,'XVOLT']<self.param.PeakVoltUpLmt-0.019:
  141. return df_Data1['AhSoc'][PeakIndex]
  142. else:
  143. return 0
  144. else:
  145. return 0
  146. #三元电池一致性计算.................................................................................................................................
  147. def _ncm_uniform(self):
  148. column_name=['time','sn','cellsoc_diff','cellvolt_diff','cellmin_num','cellmax_num']
  149. df_res=pd.DataFrame(columns=column_name)
  150. standingtime=0
  151. for i in range(1,len(self.df_bms)-2):
  152. if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1 and abs(self.packcrnt[i+1]) < 0.1: #电流为0
  153. delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
  154. standingtime=standingtime+delttime
  155. self._celltemp_weight(i) #获取不同温度对应的静置时间
  156. if standingtime>self.StandardStandingTime: #静置时间满足要求
  157. if abs(self.packcrnt[i+2]) >= 0.1:
  158. standingtime=0
  159. cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
  160. cellvolt_min=min(cellvolt_now)
  161. cellvolt_max=max(cellvolt_now)
  162. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5:
  163. cellmin_num=cellvolt_now.index(cellvolt_min)+1
  164. cellmax_num=cellvolt_now.index(cellvolt_max)+1
  165. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  166. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  167. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  168. cellsoc_diff=cellsoc_max-cellsoc_min
  169. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  170. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  171. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
  172. elif standingtime>3600*12:
  173. standingtime=0
  174. cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
  175. cellvolt_min=min(cellvolt_now)
  176. cellvolt_max=max(cellvolt_now)
  177. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5:
  178. cellmin_num=cellvolt_now.index(cellvolt_min)+1
  179. cellmax_num=cellvolt_now.index(cellvolt_max)+1
  180. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  181. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  182. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  183. cellsoc_diff=cellsoc_max-cellsoc_min
  184. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  185. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  186. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
  187. elif i>=len(self.df_bms)-4:
  188. standingtime=0
  189. cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
  190. cellvolt_min=min(cellvolt_now)
  191. cellvolt_max=max(cellvolt_now)
  192. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5:
  193. cellmin_num=cellvolt_now.index(cellvolt_min)+1
  194. cellmax_num=cellvolt_now.index(cellvolt_max)+1
  195. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  196. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  197. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  198. cellsoc_diff=cellsoc_max-cellsoc_min
  199. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  200. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  201. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
  202. break
  203. else:
  204. continue
  205. else:
  206. continue
  207. else:
  208. standingtime=0
  209. continue
  210. if df_res.empty: #返回计算结果
  211. return pd.DataFrame()
  212. else:
  213. return df_res
  214. #磷酸铁锂电池一致性计算.........................................................................................................................
  215. def _lfp_uniform(self):
  216. column_name=['time','sn','cellsoc_diff','cellvolt_diff','cellmin_num','cellmax_num']
  217. df_res=pd.DataFrame(columns=column_name)
  218. standingtime=0
  219. chrg_start=[]
  220. chrg_end=[]
  221. charging=0
  222. for i in range(3,len(self.df_bms)-3):
  223. #静置电压法计算电芯一致性
  224. if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1 and abs(self.packcrnt[i+1]) < 0.1: #电流为0
  225. delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
  226. standingtime=standingtime+delttime
  227. self._celltemp_weight(i) #获取不同温度对应的静置时间
  228. if standingtime>self.StandardStandingTime: #静置时间满足要求
  229. cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
  230. cellvolt_min=min(cellvolt_now)
  231. cellvolt_max=max(cellvolt_now)
  232. if abs(self.packcrnt[i+2]) >= 0.1:
  233. standingtime=0
  234. if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.002:
  235. cellmin_num=cellvolt_now.index(cellvolt_min)+1
  236. cellmax_num=cellvolt_now.index(cellvolt_max)+1
  237. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  238. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  239. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  240. cellsoc_diff=cellsoc_max-cellsoc_min
  241. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  242. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  243. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
  244. elif i>=len(self.df_bms)-4:
  245. standingtime=0
  246. if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.002:
  247. cellmin_num=cellvolt_now.index(cellvolt_min)+1
  248. cellmax_num=cellvolt_now.index(cellvolt_max)+1
  249. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  250. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  251. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  252. cellsoc_diff=cellsoc_max-cellsoc_min
  253. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  254. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  255. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
  256. else:
  257. pass
  258. else:
  259. pass
  260. else:
  261. standingtime=0
  262. pass
  263. #获取DVDQ算法所需数据——开始............................................................................................................
  264. if charging==0: #判断充电开始
  265. if self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]<=-1 and self.bms_soc[i]<40: #充电开始
  266. charging=1
  267. if len(chrg_start)>len(chrg_end):
  268. chrg_start[-1]=i
  269. else:
  270. chrg_start.append(i)
  271. else:
  272. pass
  273. else: #充电中
  274. if (self.bmstime[i+1]-self.bmstime[i]).total_seconds()>180: #如果充电过程中时间间隔>180s,则舍弃该次充电
  275. chrg_start.remove(chrg_start[-1])
  276. charging=0
  277. continue
  278. elif self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]>-1: #判断电流波动时刻
  279. cellvolt_now=self._cellvolt_get(i+1)
  280. if max(cellvolt_now)>self.param.CellFullChrgVolt: #电压>满充电压
  281. chrg_end.append(i+1)
  282. charging=0
  283. continue
  284. else:
  285. pass
  286. elif self.packcrnt[i+1]>-0.1 and self.packcrnt[i+2]>-0.1: #判断充电结束
  287. charging=0
  288. if len(chrg_start)>len(chrg_end):
  289. if self.bms_soc[i]>90:
  290. chrg_end.append(i)
  291. else:
  292. chrg_start.remove(chrg_start[-1])
  293. continue
  294. else:
  295. continue
  296. elif i==len(self.packcrnt)-4 and self.packcrnt[i+1]<-1 and self.packcrnt[i+2]<-1:
  297. charging=0
  298. if len(chrg_start)>len(chrg_end):
  299. if self.bms_soc[i]>90: #soc>90
  300. chrg_end.append(i)
  301. continue
  302. else:
  303. chrg_start.remove(chrg_start[-1])
  304. continue
  305. else:
  306. continue
  307. else:
  308. continue
  309. if chrg_end: #DVDQ方法计算soc差
  310. peaksoc_list=[]
  311. for i in range(len(chrg_end)):
  312. peaksoc_list = []
  313. self._celltemp_weight(chrg_start[i])
  314. if min(self.celltemp)>10:
  315. for j in range(1, self.param.CellVoltNums + 1):
  316. s = str(j)
  317. cellvolt = self.df_bms['单体电压' + s]/1000
  318. cellvolt = list(cellvolt[chrg_start[i]:chrg_end[i]])
  319. time = list(self.bmstime[chrg_start[i]:chrg_end[i]])
  320. packcrnt = list(self.packcrnt[chrg_start[i]:chrg_end[i]])
  321. soc = list(self.bms_soc[chrg_start[i]:chrg_end[i]])
  322. peaksoc = self._dvdq_peak(time, soc, cellvolt, packcrnt)
  323. if peaksoc>1:
  324. peaksoc_list.append(peaksoc) #计算到达峰值点的累计Soc
  325. else:
  326. pass
  327. if len(peaksoc_list)>self.param.CellVoltNums/2:
  328. peaksoc_max=max(peaksoc_list)
  329. peaksoc_min=min(peaksoc_list)
  330. peaksoc_maxnum=peaksoc_list.index(peaksoc_min)+1
  331. peaksoc_minnum=peaksoc_list.index(peaksoc_max)+1
  332. cellsoc_diff=peaksoc_max-peaksoc_min
  333. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  334. df_res.loc[len(df_res)]=[self.bmstime[chrg_start[i]], self.sn, cellsoc_diff, 0, peaksoc_minnum, peaksoc_maxnum]
  335. else:
  336. pass
  337. plt.show()
  338. else:
  339. pass
  340. if df_res.empty:
  341. return pd.DataFrame()
  342. else:
  343. df_res.sort_values(by='time', ascending=True, inplace=True)
  344. return df_res