CBMSBatSoh copy.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 BatSoh():
  8. def __init__(self,sn,celltype,df_bms,df_accum,df_soh): #参数初始化
  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.bms_soh=df_bms['SOH[%]']
  17. self.bmsstat=df_bms['充电状态']
  18. self.bmstime= pd.to_datetime(df_bms['时间戳'], format='%Y-%m-%d %H:%M:%S')
  19. self.df_accum=df_accum
  20. self.accumtime=pd.to_datetime(df_accum['时间戳'], format='%Y-%m-%d %H:%M:%S')
  21. self.df_soh=df_soh
  22. self.cellvolt_name=['单体电压'+str(x) for x in range(1,self.param.CellVoltNums+1)]
  23. self.celltemp_name=['单体温度'+str(x) for x in range(1,self.param.CellTempNums+1)]
  24. def batsoh(self):
  25. if self.celltype==1 or self.celltype==2 or self.celltype==3 or self.celltype==4:
  26. df_res=self._ncmsoh_twopoint()
  27. return df_res
  28. elif self.celltype==99:
  29. df_res=self._lfpsoh()
  30. return df_res
  31. else:
  32. return pd.DataFrame()
  33. #定义滑动滤波函数.........................................................................................................................
  34. def _np_move_avg(self,a, n, mode="same"):
  35. return (np.convolve(a, np.ones((n,)) / n, mode=mode))
  36. #筛选充电数据..............................................................................................................................
  37. def _chrgdata(self):
  38. self.ChgStart=[]
  39. self.ChgEnd=[]
  40. if len(self.packvolt)>100:
  41. for i in range(3, len(self.bmstime) - 3):
  42. if i==3 and self.bmsstat[i]==2 and self.bmsstat[i+1]==2 and self.bmsstat[i+2]==2:
  43. self.ChgStart.append(i)
  44. elif self.bmsstat[i-2]!=2 and self.bmsstat[i-1]!=2 and self.bmsstat[i]==2:
  45. self.ChgStart.append(i)
  46. elif self.bmsstat[i-1]==2 and self.bmsstat[i]!=2 and self.bmsstat[i+1]!=2:
  47. self.ChgEnd.append(i-1)
  48. elif i == (len(self.bmstime) - 4) and self.bmsstat[len(self.bmsstat)-1] == 2 and self.bmsstat[len(self.bmsstat)-2] == 2:
  49. self.ChgEnd.append(len(self.bmstime)-2)
  50. #寻找当前行数据的最小温度值.................................................................................................................
  51. def _celltemp_weight(self,num):
  52. celltemp = list(self.df_bms.loc[num,self.celltemp_name])
  53. celltemp.remove(min(celltemp))
  54. self.celltemp=celltemp
  55. if self.celltype>50:
  56. if min(celltemp)>=25:
  57. self.tempweight=1
  58. self.StandardStandingTime=2400
  59. elif min(celltemp)>=15:
  60. self.tempweight=0.6
  61. self.StandardStandingTime=3600
  62. elif min(celltemp)>=5:
  63. self.tempweight=0.3
  64. self.StandardStandingTime=7200
  65. else:
  66. self.tempweight=0.1
  67. self.StandardStandingTime=10800
  68. else:
  69. if min(celltemp)>=25:
  70. self.tempweight=1
  71. self.StandardStandingTime=1800
  72. elif min(celltemp)>=15:
  73. self.tempweight=0.8
  74. self.StandardStandingTime=3600
  75. elif min(celltemp)>=5:
  76. self.tempweight=0.3
  77. self.StandardStandingTime=4800
  78. else:
  79. self.tempweight=0.1
  80. self.StandardStandingTime=7200
  81. #获取SOC差对应的SOH权重值...................................................................................................................
  82. def _deltsoc_weight(self,deltsoc):
  83. if deltsoc>60:
  84. deltsoc_weight=1
  85. elif deltsoc>50:
  86. deltsoc_weight=0.9
  87. elif deltsoc>40:
  88. deltsoc_weight=0.7
  89. elif deltsoc>30:
  90. deltsoc_weight=0
  91. elif deltsoc>20:
  92. deltsoc_weight=0
  93. else:
  94. deltsoc_weight=0
  95. return deltsoc_weight
  96. #获取当前行所有电压数据......................................................................................................................
  97. def _cellvolt_get(self,num):
  98. cellvolt = np.array(self.df_bms.loc[num,self.cellvolt_name])/1000
  99. return cellvolt
  100. #获取单个电压值.................................................................................................
  101. def _singlevolt_get(self,num,series,mode): #mode==1取当前行单体电压值,mode==2取某个单体所有电压值
  102. s=str(series)
  103. if mode==1:
  104. singlevolt=self.df_bms.loc[num,'单体电压' + s]/1000
  105. return singlevolt
  106. else:
  107. singlevolt=self.df_bms['单体电压' + s]/1000
  108. return singlevolt
  109. #dvdq方法计算soh...........................................................................................................................
  110. def _dvdq_soh(self, chrg_st, chrg_end,cellvolt):
  111. Ah = 0 #参数赋初始值
  112. Volt = cellvolt[chrg_st]
  113. DV_Volt=[]
  114. DQ_Ah = []
  115. DVDQ = []
  116. time2 = []
  117. soc2 = []
  118. Ah_tatal=[0]
  119. xvolt=[]
  120. #计算DV和DQ值
  121. for j in range(chrg_st,chrg_end):
  122. Step=(self.bmstime[j+1]-self.bmstime[j]).total_seconds()
  123. Ah=Ah-self.packcrnt[j]*Step/3600
  124. if (cellvolt[j]-Volt)>0.0015 and Ah>0:
  125. Ah_tatal.append(Ah_tatal[-1]+Ah)
  126. DQ_Ah.append(Ah)
  127. DV_Volt.append(cellvolt[j]-Volt)
  128. DVDQ.append((DV_Volt[-1])/DQ_Ah[-1])
  129. xvolt.append(cellvolt[j])
  130. Volt=cellvolt[j]
  131. Ah = 0
  132. time2.append(self.bmstime[j])
  133. soc2.append(self.bms_soc[j])
  134. #切片,去除前后10min的数据
  135. df_Data1 = pd.DataFrame({'time': time2,
  136. 'SOC': soc2,
  137. 'DVDQ': DVDQ,
  138. 'Ah_tatal': Ah_tatal[:-1],
  139. 'DQ_Ah':DQ_Ah,
  140. 'DV_Volt':DV_Volt,
  141. 'XVOLT':xvolt})
  142. start_time=df_Data1.loc[0,'time']
  143. start_time=start_time+datetime.timedelta(seconds=900)
  144. end_time=df_Data1.loc[len(time2)-1,'time']
  145. end_time=end_time-datetime.timedelta(seconds=1200)
  146. if soc2[0]<36:
  147. df_Data1=df_Data1[(df_Data1['SOC']>40) & (df_Data1['SOC']<80)]
  148. else:
  149. df_Data1=df_Data1[(df_Data1['time']>start_time) & (df_Data1['SOC']<80)]
  150. df_Data1=df_Data1[(df_Data1['XVOLT']>self.param.PeakVoltLowLmt) & (df_Data1['XVOLT']<self.param.PeakVoltUpLmt)]
  151. # self._celltemp_weight(int((chrg_st+chrg_end)/2))
  152. # print(self.packcrnt[int((chrg_st+chrg_end)/2)], min(self.celltemp))
  153. # ax1 = plt.subplot(3, 1, 1)
  154. # plt.plot(df_Data1['XVOLT'],df_Data1['DVDQ'],'r*-')
  155. # plt.xlabel('Volt/V')
  156. # plt.ylabel('DV/DQ')
  157. # plt.legend()
  158. # ax1 = plt.subplot(3, 1, 2)
  159. # plt.plot(df_Data1['SOC'],df_Data1['XVOLT'],'y*-')
  160. # plt.xlabel('SOC/%')
  161. # plt.ylabel('Volt/V')
  162. # plt.legend()
  163. # ax1 = plt.subplot(3, 1, 3)
  164. # plt.plot(df_Data1['SOC'], df_Data1['DVDQ'], 'r*-')
  165. # plt.xlabel('SOC/%')
  166. # plt.ylabel('DV/DQ')
  167. # plt.legend()
  168. # plt.show()
  169. #寻找峰值并计算Soh
  170. if len(df_Data1)>2:
  171. PeakIndex=df_Data1['DVDQ'].idxmax()
  172. #筛选峰值点附近±0.5%SOC内的数据
  173. df_Data2=df_Data1[(df_Data1['SOC']>(df_Data1['SOC'][PeakIndex]-0.5)) & (df_Data1['SOC']<(df_Data1['SOC'][PeakIndex]+0.5))]
  174. if len(df_Data2)>1 and df_Data1.loc[PeakIndex,'XVOLT']<self.param.PeakVoltUpLmt-0.015:
  175. Ah_tatal1 = df_Data1['Ah_tatal']
  176. DVDQ = df_Data1['DVDQ']
  177. soc2 = df_Data1['SOC']
  178. xvolt = df_Data1['XVOLT']
  179. if soc2[PeakIndex]>40 and soc2[PeakIndex]<80:
  180. cellsoh_init=(Ah_tatal[-1]-Ah_tatal1[PeakIndex]) * 100 / ((self.param.FullChrgSoc - self.param.PeakSoc) * 0.01 * self.param.Capacity)
  181. if cellsoh_init<95:
  182. cellsoh_init=cellsoh_init*0.3926+58.14
  183. return cellsoh_init
  184. else:
  185. return cellsoh_init
  186. else:
  187. return 0
  188. else:
  189. df_Data1=df_Data1.drop([PeakIndex])
  190. PeakIndex = df_Data1['DVDQ'].idxmax()
  191. df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
  192. if len(df_Data2) > 1 and df_Data1.loc[PeakIndex,'XVOLT']<self.param.PeakVoltUpLmt-0.015:
  193. Ah_tatal1 = df_Data1['Ah_tatal']
  194. DVDQ = df_Data1['DVDQ']
  195. soc2 = df_Data1['SOC']
  196. xvolt = df_Data1['XVOLT']
  197. if soc2[PeakIndex]>40 and soc2[PeakIndex]<80:
  198. cellsoh_init=(Ah_tatal[-1]-Ah_tatal1[PeakIndex]) * 100 / ((self.param.FullChrgSoc - self.param.PeakSoc) * 0.01 * self.param.Capacity)
  199. if cellsoh_init<95:
  200. cellsoh_init=cellsoh_init*0.3926+58.14
  201. return cellsoh_init
  202. else:
  203. return cellsoh_init
  204. else:
  205. return 0
  206. else:
  207. return 0
  208. else:
  209. return 0
  210. #NCM充电数据soh计算.........................................................................................................................
  211. def _ncmsoh_chrg(self):
  212. self._chrgdata()
  213. ChgStartValid=[]
  214. ChgEndValid=[]
  215. tempweightlist=[]
  216. for i in range(min(len(self.ChgStart),len(self.ChgEnd))):
  217. self._celltemp_weight(self.ChgEnd[i]) #获取温度对应的静置时间及权重
  218. for k in range(self.ChgStart[i],self.ChgEnd[i]): #去除电流0点
  219. if self.packcrnt[k]<-0.5 and self.packcrnt[k+1]>-0.5 and self.packcrnt[k+2]>-0.5 and self.packcrnt[k+3]>-0.5:
  220. self.ChgEnd[i]=k
  221. #筛选满足2点法计算的数据
  222. StandingTime=0
  223. StandingTime1=0
  224. if self.bms_soc[self.ChgEnd[i]]>70 and self.bms_soc[self.ChgStart[i]]<50:
  225. for m in range(min(len(self.packcrnt)-self.ChgEnd[i]-2,self.ChgStart[i]-2)):
  226. if abs(self.packcrnt[self.ChgStart[i] - m - 1]) < 0.5:
  227. StandingTime = StandingTime + (self.bmstime[self.ChgStart[i] - m] - self.bmstime[self.ChgStart[i] - m - 1]).total_seconds()
  228. if abs(self.packcrnt[self.ChgEnd[i] + m + 1]) < 0.5:
  229. StandingTime1 = StandingTime1 + (self.bmstime[self.ChgEnd[i] + m + 1] - self.bmstime[self.ChgEnd[i] + m]).total_seconds()
  230. if StandingTime > self.StandardStandingTime and StandingTime1>self.StandardStandingTime and ((self.bmstime[self.ChgEnd[i]]-self.bmstime[self.ChgStart[i]]).total_seconds())/(self.ChgEnd[i]-self.ChgStart[i])<60: #筛选静置时间>15min且慢充过程丢失数据少
  231. if abs(self.packcrnt[self.ChgEnd[i] + m + 2])>0.5 or m==len(self.packcrnt)-self.ChgEnd[i]-3: #如果电流<0.5,继续寻找充电后的静置电压,直到末尾
  232. ChgStartValid.append(self.ChgStart[i])
  233. ChgEndValid.append(self.ChgEnd[i]+m)
  234. tempweightlist.append(self.tempweight)
  235. break
  236. if abs(self.packcrnt[self.ChgStart[i] - m - 2])>0.5 and abs(self.packcrnt[self.ChgEnd[i] + m + 2])>0.5:
  237. break
  238. if len(ChgStartValid)>0: #两点法计算Soh
  239. df_res=pd.DataFrame(columns=('time','sn','soh','soh1'))
  240. soh2=[]
  241. if not self.df_soh.empty: #获取数据库中上次计算的Soh值
  242. soh_init=list(self.df_soh['soh'])[-1]
  243. else:
  244. soh_init=list(self.bms_soh)[-1]
  245. for i in range(len(ChgStartValid)):
  246. Ah=0
  247. for j in range(ChgStartValid[i],ChgEndValid[i]): #计算Ah
  248. Step=(self.bmstime[j+1]-self.bmstime[j]).total_seconds()
  249. Ah=Ah-self.packcrnt[j+1]*Step/3600
  250. for j in range(1, self.param.CellVoltNums+1): #计算每个电芯的Soh
  251. s = str(j)
  252. OCVStart=self.df_bms.loc[ChgStartValid[i]-2,'单体电压' + s]/1000
  253. OCVEnd=self.df_bms.loc[ChgEndValid[i]-1,'单体电压' + s]/1000
  254. #soh
  255. ocv_Soc1=np.interp(OCVStart,self.param.LookTab_OCV,self.param.LookTab_SOC)
  256. ocv_Soc2=np.interp(OCVEnd,self.param.LookTab_OCV,self.param.LookTab_SOC)
  257. soh2.append(Ah*100/((ocv_Soc2-ocv_Soc1)*0.01*self.param.Capacity))
  258. soh1=np.mean(soh2)
  259. delt_ocv_soc=ocv_Soc2-ocv_Soc1
  260. self._deltsoc_weight(delt_ocv_soc)
  261. soh_res=soh_init*(1-self.deltsoc_weight*tempweightlist[i])+soh1*self.deltsoc_weight*tempweightlist[i]
  262. soh_init=soh_res
  263. df_res.loc[i]=[self.bmstime[ChgStartValid[i]],self.sn,soh_res,soh1]
  264. return df_res
  265. return pd.DataFrame()
  266. #两点法计算三元SOH.........................................................................................................................
  267. def _ncmsoh_twopoint(self):
  268. standingpoint_st=[]
  269. standingpoint_sp=[]
  270. tempweightlist=[]
  271. standingtime=0
  272. for i in range(3,len(self.df_bms)-3):
  273. if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1 and abs(self.packcrnt[i+1]) < 0.1: #电流为0
  274. delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
  275. standingtime=standingtime+delttime
  276. self._celltemp_weight(i) #获取不同温度对应的静置时间
  277. if standingtime>self.StandardStandingTime: #静置时间满足要求
  278. cellvolt_now=self._cellvolt_get(i)
  279. cellvolt_min=min(cellvolt_now)
  280. cellvolt_max=max(cellvolt_now)
  281. cellvolt_last=self._cellvolt_get(i-1)
  282. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  283. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and deltvolt<0.003: #前后两次电压波动<3mV
  284. if standingpoint_st:
  285. if len(standingpoint_st)>len(standingpoint_sp): #开始时刻已获取,结束时刻未获取
  286. minocv_socnow=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  287. cellvolt_st=self._cellvolt_get(standingpoint_st[-1]) #获取开始时刻静置后的电压数据
  288. minocv_socst=np.interp(min(cellvolt_st),self.param.LookTab_OCV,self.param.LookTab_SOC)
  289. if abs(minocv_socst-minocv_socnow)>=40: #当前时刻SOC与开始时刻SOC差>=40
  290. if abs(self.packcrnt[i+2])>=0.1: #如果下一时刻电流>=0.5,则压入当前索引
  291. standingpoint_sp.append(i)
  292. standingpoint_st.append(i)
  293. tempweightlist.append(self.tempweight)
  294. standingtime=0
  295. continue
  296. else:
  297. if standingtime>3600 or i==len(self.df_bms)-2: #仍处于静置,但静置时间>1h,则直接获取sp时刻,或者到了数据末尾
  298. standingpoint_sp.append(i)
  299. tempweightlist.append(self.tempweight)
  300. continue
  301. else:
  302. if abs(self.packcrnt[i+2])>=0.1:
  303. standingtime=0
  304. if minocv_socst<50 and minocv_socnow<minocv_socst:
  305. standingpoint_st[-1]=i
  306. continue
  307. elif abs(self.packcrnt[i+2])>=0.1:
  308. standingtime=0
  309. if minocv_socst>=50 and minocv_socnow>minocv_socst:
  310. standingpoint_st[-1]=i
  311. continue
  312. else:
  313. continue
  314. else:
  315. if abs(self.packcrnt[i+2])>=0.1:
  316. standingpoint_st.append(i)
  317. standingtime=0
  318. continue
  319. else:
  320. continue
  321. else:
  322. if abs(self.packcrnt[i+2])>0.1:
  323. standingpoint_st.append(i)
  324. standingtime=0
  325. continue
  326. else:
  327. continue
  328. else:
  329. continue
  330. else:
  331. continue
  332. else:
  333. standingtime=0
  334. continue
  335. #计算SOH......................................................................................................................
  336. if standingpoint_sp:
  337. column_name=['time_st','time_sp','sn','method','bmssoh','packsoh','soh','cellsoh']
  338. df_res=pd.DataFrame(columns=column_name)
  339. for i in range(len(standingpoint_sp)):
  340. cellocv_st=self._cellvolt_get(standingpoint_st[i]) #获取静置点所有电芯的电压
  341. cellocv_sp=self._cellvolt_get(standingpoint_sp[i])
  342. accumtime=self.accumtime.to_list() #累计量的时间列表
  343. timepoint_bms_st=self.bmstime[standingpoint_st[i]] #获取静置点的时间
  344. timepoint_bms_sp=self.bmstime[standingpoint_sp[i]]
  345. timepoint_accum_st=bisect.bisect(accumtime,timepoint_bms_st) #获取最接近静置点时间的累计量时间点
  346. timepoint_accum_sp=bisect.bisect(accumtime,timepoint_bms_sp)
  347. if timepoint_accum_sp>=len(accumtime): #防止指针超出数据范围
  348. timepoint_accum_sp=len(accumtime)-1
  349. ah_packcrnt_dis=0
  350. ah_packcrnt_chg=0
  351. for j in range(standingpoint_st[i]+1,standingpoint_sp[i]): #计算累计Ah
  352. Step=(self.bmstime[j+1]-self.bmstime[j]).total_seconds()
  353. if Step<120:
  354. if self.packcrnt[j+1]>=0:
  355. ah_packcrnt_dis=ah_packcrnt_dis+self.packcrnt[j+1]*Step
  356. else:
  357. ah_packcrnt_chg=ah_packcrnt_chg-self.packcrnt[j+1]*Step
  358. ah_packcrnt_chg=ah_packcrnt_chg/3600
  359. ah_packcrnt_dis=ah_packcrnt_dis/3600
  360. ah_packcrnt=ah_packcrnt_chg-ah_packcrnt_dis #两个静置点的总累计AH,负值代表放电,正值代表充电
  361. ah_accum_dis=self.df_accum.loc[timepoint_accum_sp,'累计放电电量']-self.df_accum.loc[timepoint_accum_st,'累计放电电量'] #两个静置点之间的放电电量
  362. ah_accum_chg=self.df_accum.loc[timepoint_accum_sp,'累计充电电量']-self.df_accum.loc[timepoint_accum_st,'累计充电电量'] #两个静置点之间的充电电量
  363. ah_accum_tatol=ah_accum_chg-ah_accum_dis #两个静置点的总累计AH,负值代表放电,正值代表充电
  364. ah_accum=ah_packcrnt
  365. delt_days=(self.bmstime[standingpoint_sp[i]]-self.bmstime[standingpoint_st[i]]).total_seconds()/(3600*24)
  366. if delt_days<=1: #两次时间间隔对计算结果的影响
  367. soh_weight1=1
  368. elif delt_days<=2:
  369. soh_weight1=0.7
  370. elif delt_days<=3:
  371. soh_weight1=0.4
  372. else:
  373. soh_weight1=0
  374. if ah_packcrnt_dis<self.param.Capacity: #放电ah数对结果的影响
  375. soh_weight1=(1-ah_packcrnt_dis/(self.param.Capacity*1.5))*soh_weight1
  376. else:
  377. soh_weight1=0.1
  378. if self.param.Capacity**0.7*0.4 < abs(ah_accum_tatol) < self.param.Capacity: #累计量的权重
  379. if abs(ah_accum_tatol-ah_packcrnt)<self.param.Capacity/20:
  380. soh_weight1=soh_weight1*1
  381. elif abs(ah_accum_tatol-ah_packcrnt) < self.param.Capacity/10:
  382. soh_weight1=soh_weight1*0.8
  383. else:
  384. soh_weight1=soh_weight1*0.5
  385. else:
  386. if self.param.Capacity*0.7*0.4< abs(ah_packcrnt) <self.param.Capacity:
  387. soh_weight1=soh_weight1*0.3
  388. else:
  389. soh_weight1=0
  390. #计算每个电芯的SOH值
  391. cellsoh=[]
  392. for j in range(self.param.CellVoltNums):
  393. ocv_soc1=np.interp(cellocv_st[j],self.param.LookTab_OCV,self.param.LookTab_SOC)
  394. ocv_soc2=np.interp(cellocv_sp[j],self.param.LookTab_OCV,self.param.LookTab_SOC)
  395. delt_ocv_soc=ocv_soc2-ocv_soc1
  396. delt_ocv_soc_weight=self._deltsoc_weight(abs(delt_ocv_soc))
  397. soh_weight=soh_weight1*tempweightlist[i]*delt_ocv_soc_weight*0.5
  398. cellsoh_init=ah_accum*100/((ocv_soc2-ocv_soc1)*0.01*self.param.Capacity)
  399. if cellsoh_init>55 and cellsoh_init<120 and soh_weight>0.05: #判断soh值的有效区间
  400. if len(df_res)<1:
  401. if not self.df_soh.empty:
  402. cellsoh_last=eval(self.df_soh.loc[len(self.df_soh)-1,'cellsoh'])
  403. if soh_weight>1/abs(cellsoh_init-cellsoh_last[j]):
  404. soh_weight=1/abs(cellsoh_init-cellsoh_last[j])
  405. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last[j]*(1-soh_weight)
  406. else:
  407. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last[j]*(1-soh_weight)
  408. else:
  409. cellsoh_cal=cellsoh_init
  410. else:
  411. cellsoh_last=eval(df_res.loc[len(df_res)-1,'cellsoh'])
  412. if soh_weight>1/abs(cellsoh_init-cellsoh_last[j]):
  413. soh_weight=1/abs(cellsoh_init-cellsoh_last[j])
  414. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last[j]*(1-soh_weight)
  415. else:
  416. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last[j]*(1-soh_weight)
  417. cellsoh_cal=eval(format(cellsoh_cal,'.1f'))
  418. cellsoh.append(cellsoh_cal)
  419. else:
  420. cellsoh=[]
  421. break
  422. #计算电池包SOH
  423. ocv_soc1=np.interp(min(cellocv_st),self.param.LookTab_OCV,self.param.LookTab_SOC)
  424. ocv_soc2=np.interp(max(cellocv_sp),self.param.LookTab_OCV,self.param.LookTab_SOC)
  425. delt_ocv_soc=ocv_soc2-ocv_soc1
  426. delt_ocv_soc_weight=self._deltsoc_weight(abs(delt_ocv_soc))
  427. soh_weight=soh_weight1*tempweightlist[i]*delt_ocv_soc_weight*0.5
  428. packsoh_init=ah_accum*100/((ocv_soc2-ocv_soc1)*0.01*self.param.Capacity)
  429. if packsoh_init>55 and packsoh_init<120 and soh_weight>0.05: #判断soh值的有效区间
  430. if len(df_res)<1:
  431. if not self.df_soh.empty:
  432. packsoh_last=self.df_soh.loc[len(self.df_soh)-1,'packsoh']
  433. if soh_weight>1/abs(packsoh_init-packsoh_last):
  434. soh_weight=1/abs(packsoh_init-packsoh_last)
  435. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  436. else:
  437. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  438. else:
  439. packsoh=packsoh_init
  440. else:
  441. packsoh_last=df_res.loc[len(df_res)-1,'packsoh']
  442. if soh_weight>1/abs(packsoh_init-packsoh_last):
  443. soh_weight=1/abs(packsoh_init-packsoh_last)
  444. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  445. else:
  446. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  447. packsoh=eval(format(packsoh,'.1f'))
  448. else:
  449. packsoh=0
  450. continue
  451. if cellsoh and 55<min(cellsoh)<120 and 50<packsoh<120:
  452. soh=min(cellsoh)
  453. bmssoh=self.bms_soh[standingpoint_sp[i]]
  454. soh_list=[timepoint_bms_st, timepoint_bms_sp, self.sn, 1, bmssoh, packsoh, soh, str(cellsoh)]
  455. df_res.loc[len(df_res)]=soh_list
  456. else:
  457. continue
  458. if df_res.empty:
  459. return pd.DataFrame()
  460. else:
  461. return df_res
  462. return pd.DataFrame()
  463. #两点法和DVDQ法计算磷酸铁锂电池SOH..................................................................................................................
  464. def _lfpsoh(self):
  465. standingpoint_st=[]
  466. standingpoint_sp=[]
  467. tempweightlist1=[]
  468. cellmaxvolt_number1=[]
  469. standingtime=0
  470. chrg_start=[]
  471. chrg_end=[]
  472. tempweightlist2=[]
  473. cellmaxvolt_number2=[]
  474. charging=0
  475. for i in range(3,len(self.df_bms)-3):
  476. #获取两点法法所需数据-开始.................................................................................................................
  477. if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1 and abs(self.packcrnt[i+1]) < 0.1: #判断非平台区静置状态
  478. delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
  479. standingtime=standingtime+delttime
  480. self._celltemp_weight(i) #获取不同温度对应的静置时间
  481. if standingtime>self.StandardStandingTime: #静置时间满足要求
  482. if abs(self.packcrnt[i+2])>=0.1: #下一时刻电流>0.1A
  483. standingtime=0
  484. cellvolt_now=self._cellvolt_get(i)
  485. cellvolt_max=max(cellvolt_now)
  486. cellvolt_min=min(cellvolt_now)
  487. cellvolt_last=self._cellvolt_get(i-1)
  488. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  489. if 2<cellvolt_max<self.param.OcvInflexionBelow-0.002 and 2<cellvolt_min<4.5 and deltvolt<0.003: #当前最大电芯电压<OCV下拐点
  490. if standingpoint_st:
  491. if len(standingpoint_st)>len(standingpoint_sp):
  492. if self.packcrnt[standingpoint_st[-1]]<-1: #判断上一次静置点的是否为满充
  493. standingpoint_sp.append(i)
  494. standingpoint_st.append(i)
  495. tempweightlist1.append(self.tempweight)
  496. else:
  497. standingpoint_st[-1]=i
  498. tempweightlist1[-1]=self.tempweight
  499. else:
  500. standingpoint_st.append(i)
  501. tempweightlist1.append(self.tempweight)
  502. else:
  503. standingpoint_st.append(i)
  504. tempweightlist1.append(self.tempweight)
  505. else:
  506. pass
  507. else:
  508. pass
  509. else:
  510. pass
  511. elif self.packcrnt[i]<=-1 and self.packcrnt[i-1]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]>-1: #判读满充状态
  512. standingtime=0
  513. self._celltemp_weight(i)
  514. cellvolt_now=self._cellvolt_get(i).tolist()
  515. if max(cellvolt_now)>self.param.CellFullChrgVolt:
  516. if standingpoint_st:
  517. if len(standingpoint_st)>len(standingpoint_sp):
  518. if abs(self.packcrnt[standingpoint_st[-1]])<0.5: #判断上一次静置点是否为下拐点
  519. standingpoint_sp.append(i)
  520. standingpoint_st.append(i)
  521. tempweightlist1.append(self.tempweight)
  522. cellmaxvolt_number1.append(cellvolt_now.index(max(cellvolt_now))) #获取最大电压索引
  523. cellmaxvolt_number1.append(cellvolt_now.index(max(cellvolt_now))) #获取最大电压索引
  524. else:
  525. standingpoint_st[-1]=i
  526. tempweightlist1[-1]=self.tempweight
  527. cellmaxvolt_number1[-1]=cellvolt_now.index(max(cellvolt_now))
  528. else:
  529. standingpoint_st.append(i)
  530. tempweightlist1.append(self.tempweight)
  531. cellmaxvolt_number1.append(cellvolt_now.index(max(cellvolt_now)))
  532. else:
  533. pass
  534. else:
  535. standingtime=0
  536. pass
  537. #获取DVDQ算法所需数据——开始.............................................................................................................
  538. if charging==0:
  539. if self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]<=-1 and self.bms_soc[i]<40: #充电开始
  540. self._celltemp_weight(i)
  541. charging=1
  542. if len(chrg_start)>len(chrg_end):
  543. chrg_start[-1]=i
  544. tempweightlist2[-1]=self.tempweight
  545. else:
  546. chrg_start.append(i)
  547. tempweightlist2.append(self.tempweight)
  548. else:
  549. pass
  550. else: #充电中
  551. if (self.bmstime[i+1]-self.bmstime[i]).total_seconds()>180 or (self.packcrnt[i]<-self.param.Capacity/2 and self.packcrnt[i+1]<-self.param.Capacity/2): #如果充电过程中时间间隔>180s,则舍弃该次充电
  552. chrg_start.remove(chrg_start[-1])
  553. tempweightlist2.remove(tempweightlist2[-1])
  554. charging=0
  555. continue
  556. elif self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]>-1: #判断电流波动时刻
  557. cellvolt_now=self._cellvolt_get(i+1).tolist()
  558. if max(cellvolt_now)>self.param.CellFullChrgVolt: #电压>满充电压
  559. chrg_end.append(i+1)
  560. cellmaxvolt_number2.append(cellvolt_now.index(max(cellvolt_now))) #获取最大电压索引
  561. charging=0
  562. continue
  563. else:
  564. pass
  565. elif self.packcrnt[i+1]>-0.1 and self.packcrnt[i+2]>-0.1: #判断充电结束
  566. charging=0
  567. if len(chrg_start)>len(chrg_end):
  568. chrg_start.remove(chrg_start[-1])
  569. tempweightlist2.remove(tempweightlist2[-1])
  570. continue
  571. else:
  572. continue
  573. elif i==len(self.packcrnt)-4 and self.packcrnt[i+1]<-1 and self.packcrnt[i+2]<-1:
  574. charging=0
  575. if len(chrg_start)>len(chrg_end):
  576. cellvolt_now=self._cellvolt_get(i).tolist()
  577. if max(cellvolt_now)>self.param.CellFullChrgVolt: #电压>满充电压
  578. chrg_end.append(i)
  579. cellmaxvolt_number2.append(cellvolt_now.index(max(cellvolt_now))) #获取最大电压索引
  580. continue
  581. else:
  582. chrg_start.remove(chrg_start[-1])
  583. tempweightlist2.remove(tempweightlist2[-1])
  584. continue
  585. else:
  586. continue
  587. else:
  588. continue
  589. #开始计算SOH.............................................................................................................................................
  590. if standingpoint_sp or chrg_end:
  591. column_name=['time_st','time_sp','sn','method','bmssoh','packsoh','soh','cellsoh','weight']
  592. df_res=pd.DataFrame(columns=column_name)
  593. #两点法计算SOH........................................................................................................................................
  594. if standingpoint_sp:
  595. for i in range(len(standingpoint_sp)): #判断为满充点或者下拐点
  596. if self.packcrnt[standingpoint_sp[i]]<=-1: #计算单体电芯soh
  597. cellocv_st=self._cellvolt_get(standingpoint_st[i])
  598. ocv_soc1=np.interp(cellocv_st[cellmaxvolt_number1[i]],self.param.LookTab_OCV,self.param.LookTab_SOC)
  599. ocv_packsoc1=np.interp(min(cellocv_st),self.param.LookTab_OCV,self.param.LookTab_SOC)
  600. ocv_soc2=self.param.FullChrgSoc
  601. ocv_packsoc2=ocv_soc2
  602. else:
  603. cellocv_sp=self._cellvolt_get(standingpoint_sp[i])
  604. ocv_soc1=self.param.FullChrgSoc
  605. ocv_packsoc1=ocv_soc1
  606. ocv_soc2=np.interp(cellocv_sp[cellmaxvolt_number1[i]],self.param.LookTab_OCV,self.param.LookTab_SOC)
  607. ocv_packsoc2=np.interp(min(cellocv_sp),self.param.LookTab_OCV,self.param.LookTab_SOC)
  608. cellocv_sp=self._cellvolt_get(standingpoint_sp[i])
  609. accumtime=self.accumtime.to_list() #累计量的时间列表
  610. timepoint_bms_st=self.bmstime[standingpoint_st[i]] #获取静置点的时间
  611. timepoint_bms_sp=self.bmstime[standingpoint_sp[i]]
  612. timepoint_accum_st=bisect.bisect(accumtime,timepoint_bms_st) #获取最接近静置点时间的累计量时间点
  613. timepoint_accum_sp=bisect.bisect(accumtime,timepoint_bms_sp)
  614. if timepoint_accum_sp>=len(accumtime): #防止指针超出数据范围
  615. timepoint_accum_sp=len(accumtime)-1
  616. ah_packcrnt_dis=0
  617. ah_packcrnt_chg=0
  618. for j in range(standingpoint_st[i]+1,standingpoint_sp[i]+1): #计算累计Ah
  619. Step=(self.bmstime[j+1]-self.bmstime[j]).total_seconds()
  620. if Step<120:
  621. if self.packcrnt[j+1]>=0:
  622. ah_packcrnt_dis=ah_packcrnt_dis+self.packcrnt[j+1]*Step
  623. else:
  624. ah_packcrnt_chg=ah_packcrnt_chg-self.packcrnt[j+1]*Step
  625. ah_packcrnt_chg=ah_packcrnt_chg/3600
  626. ah_packcrnt_dis=ah_packcrnt_dis/3600
  627. ah_packcrnt=ah_packcrnt_chg-ah_packcrnt_dis #两个静置点的总累计AH,负值代表放电,正值代表充电
  628. ah_accum_dis=self.df_accum.loc[timepoint_accum_sp,'累计放电电量']-self.df_accum.loc[timepoint_accum_st,'累计放电电量'] #两个静置点之间的放电电量
  629. ah_accum_chg=self.df_accum.loc[timepoint_accum_sp,'累计充电电量']-self.df_accum.loc[timepoint_accum_st,'累计充电电量'] #两个静置点之间的充电电量
  630. ah_accum_tatol=ah_accum_chg-ah_accum_dis #两个静置点的总累计AH,负值代表放电,正值代表充电
  631. ah_accum=ah_accum_tatol
  632. delt_days=(self.bmstime[standingpoint_sp[i]]-self.bmstime[standingpoint_st[i]]).total_seconds()/(3600*24)
  633. if delt_days<=1: #两次时间间隔对计算结果的影响
  634. soh_weight=1
  635. elif delt_days<=2:
  636. soh_weight=0.7
  637. elif delt_days<=3:
  638. soh_weight=0.4
  639. else:
  640. soh_weight=0
  641. if self.param.Capacity*0.65*0.7 < abs(ah_packcrnt) < self.param.Capacity: #累计量的权重
  642. if abs(ah_accum_tatol-ah_packcrnt)<self.param.Capacity/20:
  643. soh_weight=soh_weight*1
  644. elif abs(ah_accum_tatol-ah_packcrnt)<self.param.Capacity/10:
  645. soh_weight=soh_weight*0.8
  646. else:
  647. soh_weight=soh_weight*0.5
  648. else:
  649. if self.param.Capacity*0.65*0.7 < abs(ah_accum) < self.param.Capacity:
  650. soh_weight=soh_weight*0.3
  651. else:
  652. soh_weight=0
  653. delt_ocv_soc=ocv_soc2-ocv_soc1
  654. delt_ocv_packsoc=ocv_packsoc2-ocv_packsoc1
  655. delt_ocv_soc_weight=self._deltsoc_weight(abs(delt_ocv_soc))
  656. soh_weight=soh_weight*tempweightlist1[i]*delt_ocv_soc_weight*0.5
  657. cellsoh_init=ah_accum*100/(delt_ocv_soc*0.01*self.param.Capacity)
  658. packsoh_init=ah_accum*100/(delt_ocv_packsoc*0.01*self.param.Capacity)
  659. bmssoh=self.bms_soh[standingpoint_sp[i]]
  660. if 55<cellsoh_init<120 and 50<packsoh_init<120: #判断soh值的有效区间
  661. soh_list=[timepoint_bms_st, timepoint_bms_sp, self.sn, 1, bmssoh, packsoh_init, cellsoh_init, str(cellsoh_init), soh_weight]
  662. df_res.loc[len(df_res)]=soh_list
  663. else:
  664. pass
  665. else:
  666. pass
  667. #DVDQ法计算SOH.......................................................................................................................................
  668. if chrg_end:
  669. for i in range(len(chrg_end)):
  670. cellsoh=[]
  671. for j in range(1, self.param.CellVoltNums + 1):
  672. cellvolt1 = self._singlevolt_get(i,j,2) #取单体电压j的所有电压值
  673. cellvolt=self._np_move_avg(cellvolt1, 3, mode="same") #对电压进行滑动平均滤
  674. cellsoh_init=self._dvdq_soh(chrg_start[i],chrg_end[i],cellvolt) #dvdq计算soh
  675. cellsoh.append(cellsoh_init)
  676. soh_weight=tempweightlist2[i]*0.3
  677. cellsoh_init=cellsoh[cellmaxvolt_number2[i]+1]
  678. cellsoh_new=[k for k in cellsoh if 50<k<120]
  679. packsoh_init=min(cellsoh_new)
  680. bmssoh=self.bms_soh[chrg_end[i]]
  681. if 55<cellsoh_init<120: #判断soh值的有效区间
  682. soh_list=[self.bmstime[chrg_start[i]], self.bmstime[chrg_end[i]], self.sn, 2, bmssoh, packsoh_init, cellsoh_init, str(cellsoh_init), soh_weight]
  683. df_res.loc[len(df_res)]=soh_list
  684. else:
  685. pass
  686. #对SOH结果进行滤波处理................................................................................................................................
  687. if df_res.empty:
  688. return pd.DataFrame()
  689. else:
  690. df_res=df_res.sort_values(by='time_st',ascending=True)
  691. df_res.index=range(len(df_res))
  692. for i in range(len(df_res)):
  693. cellsoh_init=df_res.loc[i,'soh']
  694. packsoh_init=df_res.loc[i,'packsoh']
  695. soh_weight=df_res.loc[i,'weight']
  696. if i<1:
  697. if not self.df_soh.empty and 60<self.df_soh.loc[len(self.df_soh)-1,'soh']<115:
  698. cellsoh_last=self.df_soh.loc[len(self.df_soh)-1,'soh']
  699. packsoh_last=self.df_soh.loc[len(self.df_soh)-1,'packsoh']
  700. if soh_weight>1/abs(cellsoh_init-cellsoh_last):
  701. soh_weight=1/abs(cellsoh_init-cellsoh_last)
  702. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last*(1-soh_weight)
  703. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  704. else:
  705. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last*(1-soh_weight)
  706. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  707. else:
  708. cellsoh_cal=cellsoh_init
  709. packsoh=packsoh_init
  710. cellsoh_cal=eval(format(cellsoh_cal,'.1f'))
  711. packsoh=eval(format(packsoh,'.1f'))
  712. if 55<cellsoh_cal<120:
  713. df_res.loc[i,'soh']=cellsoh_cal
  714. df_res.loc[i,'cellsoh']=str(cellsoh_cal)
  715. df_res.loc[i,'packsoh']=packsoh
  716. else:
  717. df_res=df_res.drop(i,axis=0)
  718. else:
  719. cellsoh_last=df_res.loc[i-1,'soh']
  720. packsoh_last=df_res.loc[i-1,'packsoh']
  721. if soh_weight>1/abs(cellsoh_init-cellsoh_last):
  722. soh_weight=1/abs(cellsoh_init-cellsoh_last)
  723. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last*(1-soh_weight)
  724. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  725. else:
  726. cellsoh_cal=cellsoh_init*soh_weight + cellsoh_last*(1-soh_weight)
  727. packsoh=packsoh_init*soh_weight + packsoh_last*(1-soh_weight)
  728. cellsoh_cal=eval(format(cellsoh_cal,'.1f'))
  729. packsoh=eval(format(packsoh,'.1f'))
  730. if 55<cellsoh_cal<120:
  731. df_res.loc[i,'soh']=cellsoh_cal
  732. df_res.loc[i,'cellsoh']=str([cellsoh_cal])
  733. df_res.loc[i,'packsoh']=packsoh
  734. else:
  735. df_res=df_res.drop(i,axis=0)
  736. df_res=df_res.drop('weight',axis=1)
  737. return df_res
  738. return pd.DataFrame()