CBMSBatUniform.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import pandas as pd
  2. import numpy as np
  3. import datetime
  4. import BatParam
  5. # import matplotlib.pyplot as plt
  6. class BatUniform():
  7. def __init__(self,sn,celltype,df_bms,df_uniform,df_last3,df_lfp1): #参数初始化
  8. if (not df_lfp1.empty) and celltype>50:
  9. df_bms=pd.concat([df_lfp1, df_bms], ignore_index=True)
  10. df_bms.reset_index(inplace=True,drop=True)
  11. else:
  12. pass
  13. self.sn=sn
  14. self.celltype=celltype
  15. self.param=BatParam.BatParam(celltype)
  16. self.df_bms=df_bms
  17. self.packcrnt=df_bms['PackCrnt']*self.param.PackCrntDec
  18. self.packvolt=df_bms['PackVolt']
  19. self.bms_soc=df_bms['PackSOC']
  20. self.bmstime= pd.to_datetime(df_bms['time'], format='%Y-%m-%d %H:%M:%S')
  21. # df_uniform['time']=pd.to_datetime(df_uniform['time'], format='%Y-%m-%d %H:%M:%S')
  22. self.df_uniform=df_uniform
  23. self.df_last3=df_last3
  24. self.df_lfp1=df_lfp1
  25. self.cellvolt_name=['CellVolt'+str(x) for x in range(1,self.param.CellVoltNums+1)]
  26. self.celltemp_name=['CellTemp'+str(x) for x in range(1,self.param.CellTempNums+1)]
  27. def batuniform(self):
  28. if self.celltype<50:
  29. df_res, df_ram_last3=self._ncm_uniform()
  30. return df_res, df_ram_last3, self.df_lfp1
  31. else:
  32. df_res, df_ram_last3, df_ram_lfp1=self._lfp_uniform()
  33. return df_res, df_ram_last3, df_ram_lfp1
  34. #定义滑动滤波函数........................................................................................................................................
  35. def _np_move_avg(self,a, n, mode="same"):
  36. return (np.convolve(a, np.ones((n,)) / n, mode=mode))
  37. #寻找当前行数据的最小温度值................................................................................................................................
  38. def _celltemp_weight(self,num):
  39. celltemp = list(self.df_bms.loc[num,self.celltemp_name])
  40. celltemp.remove(min(celltemp))
  41. self.celltemp=celltemp
  42. if self.celltype>50:
  43. if min(celltemp)>=25:
  44. self.tempweight=1
  45. self.StandardStandingTime=2400
  46. elif min(celltemp)>=15:
  47. self.tempweight=0.6
  48. self.StandardStandingTime=3600
  49. elif min(celltemp)>=5:
  50. self.tempweight=0.2
  51. self.StandardStandingTime=4800
  52. else:
  53. self.tempweight=0.1
  54. self.StandardStandingTime=7200
  55. else:
  56. if min(celltemp)>=25:
  57. self.tempweight=1
  58. self.StandardStandingTime=1800
  59. elif min(celltemp)>=15:
  60. self.tempweight=0.8
  61. self.StandardStandingTime=2400
  62. elif min(celltemp)>=5:
  63. self.tempweight=0.6
  64. self.StandardStandingTime=3600
  65. else:
  66. self.tempweight=0.2
  67. self.StandardStandingTime=7200
  68. #获取当前行所有电压数据............................................................................................................................
  69. def _cellvolt_get(self,num):
  70. cellvolt = np.array(self.df_bms.loc[num,self.cellvolt_name])
  71. return cellvolt
  72. #获取单个电压值.................................................................................................
  73. def _singlevolt_get(self,num,series,mode): #mode==1取当前行单体电压值,mode==2取某个单体所有电压值
  74. s=str(series)
  75. if mode==1:
  76. singlevolt=self.df_bms.loc[num,'CellVolt' + s]
  77. return singlevolt
  78. else:
  79. singlevolt=self.df_bms['CellVolt' + s]
  80. return singlevolt
  81. #寻找DVDQ的峰值点,并返回..........................................................................................................................
  82. def _dvdq_peak(self, time, soc, cellvolt, packcrnt):
  83. cellvolt = self._np_move_avg(cellvolt, 3, mode="same")
  84. Soc = 0
  85. Ah = 0
  86. Volt = cellvolt[0]
  87. DV_Volt = []
  88. DQ_Ah = []
  89. DVDQ = []
  90. time1 = []
  91. soc1 = []
  92. soc2 = []
  93. xvolt=[]
  94. for m in range(1, len(time)):
  95. Step = (time[m] - time[m - 1]).total_seconds()
  96. Soc = Soc - packcrnt[m] * Step * 100 / (3600 * self.param.Capacity)
  97. Ah = Ah - packcrnt[m] * Step / 3600
  98. if (cellvolt[m]-Volt)>0.0015 and Ah>0:
  99. DQ_Ah.append(Ah)
  100. DV_Volt.append(cellvolt[m]-Volt)
  101. DVDQ.append((DV_Volt[-1])/Ah)
  102. xvolt.append(cellvolt[m])
  103. Volt=cellvolt[m]
  104. Ah = 0
  105. soc1.append(Soc)
  106. time1.append(time[m])
  107. soc2.append(soc[m])
  108. #切片,去除前后10min的数据
  109. df_Data1 = pd.DataFrame({'time': time1,
  110. 'SOC': soc2,
  111. 'DVDQ': DVDQ,
  112. 'AhSoc': soc1,
  113. 'DQ_Ah':DQ_Ah,
  114. 'DV_Volt':DV_Volt,
  115. 'XVOLT':xvolt})
  116. start_time=df_Data1.loc[0,'time']
  117. start_time=start_time+datetime.timedelta(seconds=900)
  118. end_time=df_Data1.loc[len(time1)-1,'time']
  119. end_time=end_time-datetime.timedelta(seconds=1200)
  120. if soc2[0]<36:
  121. df_Data1=df_Data1[(df_Data1['SOC']>40) & (df_Data1['SOC']<80)]
  122. else:
  123. df_Data1=df_Data1[(df_Data1['time']>start_time) & (df_Data1['SOC']<80)]
  124. df_Data1=df_Data1[(df_Data1['XVOLT']>self.param.PeakVoltLowLmt) & (df_Data1['XVOLT']<self.param.PeakVoltUpLmt)]
  125. # print(packcrnt[int(len(time)/2)], min(self.celltemp))
  126. # ax1 = plt.subplot(3, 1, 1)
  127. # plt.plot(df_Data1['SOC'],df_Data1['DQ_Ah'],'g*-')
  128. # plt.xlabel('SOC/%')
  129. # plt.ylabel('DQ_Ah')
  130. # plt.legend()
  131. # ax1 = plt.subplot(3, 1, 2)
  132. # plt.plot(df_Data1['SOC'],df_Data1['XVOLT'],'y*-')
  133. # plt.xlabel('SOC/%')
  134. # plt.ylabel('Volt/V')
  135. # plt.legend()
  136. # ax1 = plt.subplot(3, 1, 3)
  137. # plt.plot(df_Data1['SOC'], df_Data1['DVDQ'], 'r*-')
  138. # plt.xlabel('SOC/%')
  139. # plt.ylabel('DV/DQ')
  140. # plt.legend()
  141. # plt.show()
  142. if len(df_Data1)>2: #寻找峰值点,且峰值点个数>2
  143. PeakIndex = df_Data1['DVDQ'].idxmax()
  144. df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
  145. if len(df_Data2) > 1 and min(df_Data1['SOC'])+0.5<df_Data1['SOC'][PeakIndex]<max(df_Data1['SOC'])-1:
  146. return df_Data1['AhSoc'][PeakIndex]
  147. else:
  148. if min(df_Data1['SOC'])+0.5<df_Data1['SOC'][PeakIndex]<max(df_Data1['SOC'])-1:
  149. df_Data1=df_Data1.drop([PeakIndex])
  150. elif df_Data1['SOC'][PeakIndex]>max(df_Data1['SOC'])-1:
  151. df_Data1=df_Data1[df_Data1['SOC']<(df_Data1['SOC'][PeakIndex]-1)]
  152. else:
  153. df_Data1=df_Data1[df_Data1['SOC']>(df_Data1['SOC'][PeakIndex]+0.5)]
  154. if len(df_Data1)>2:
  155. PeakIndex = df_Data1['DVDQ'].idxmax()
  156. df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
  157. if len(df_Data2) > 1 and min(df_Data1['SOC'])+0.5<df_Data1['SOC'][PeakIndex]<max(df_Data1['SOC'])-1:
  158. return df_Data1['AhSoc'][PeakIndex]
  159. else:
  160. return 0
  161. else:
  162. return 0
  163. else:
  164. return 0
  165. #三元电池一致性计算.................................................................................................................................
  166. def _ncm_uniform(self):
  167. column_name=['time','sn','cellsoc_diff','cellvolt_diff','cellmin_num','cellmax_num','cellvolt_rank']
  168. df_res=pd.DataFrame(columns=column_name)
  169. df_ram_last3=self.df_last3
  170. if df_ram_last3.empty:
  171. standingtime=0
  172. standingtime1=0
  173. standingtime2=0
  174. else:
  175. standingtime=df_ram_last3.loc[0,'standingtime']
  176. standingtime1=df_ram_last3.loc[0,'standingtime1']
  177. standingtime2=df_ram_last3.loc[0,'standingtime2']
  178. if abs(self.packcrnt[0])<0.01 and standingtime2>1:
  179. standingtime2=standingtime2+(self.bmstime[0]-df_ram_last3.loc[0,'time3']).total_seconds()
  180. else:
  181. pass
  182. for i in range(1,len(self.df_bms)-1):
  183. if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1: #电流为0
  184. delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
  185. standingtime2=standingtime2+delttime
  186. self._celltemp_weight(i) #获取不同温度对应的静置时间
  187. if standingtime2>self.StandardStandingTime: #静置时间满足要求
  188. if abs(self.packcrnt[i+1]) >= 0.1:
  189. standingtime2=0
  190. cellvolt_now=self._cellvolt_get(i)
  191. cellvolt_min=min(cellvolt_now)
  192. cellvolt_max=max(cellvolt_now)
  193. cellvolt_last=self._cellvolt_get(i-1)
  194. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  195. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and deltvolt<0.005:
  196. cellvolt_sort=np.argsort(cellvolt_now)
  197. cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  198. cellmin_num=list(cellvolt_now).index(cellvolt_min)+1
  199. cellmax_num=list(cellvolt_now).index(cellvolt_max)+1
  200. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  201. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  202. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  203. cellsoc_diff=cellsoc_max-cellsoc_min
  204. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  205. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  206. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num, str(cellvolt_rank)]
  207. elif standingtime2>3600:
  208. cellvolt_now=self._cellvolt_get(i)
  209. cellvolt_min=min(cellvolt_now)
  210. cellvolt_max=max(cellvolt_now)
  211. cellvolt_last=self._cellvolt_get(i-1)
  212. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  213. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and deltvolt<0.005:
  214. standingtime2=0
  215. cellvolt_sort=np.argsort(cellvolt_now)
  216. cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  217. cellmin_num=list(cellvolt_now).index(cellvolt_min)+1
  218. cellmax_num=list(cellvolt_now).index(cellvolt_max)+1
  219. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  220. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  221. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  222. cellsoc_diff=cellsoc_max-cellsoc_min
  223. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  224. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  225. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num, str(cellvolt_rank)]
  226. elif i>=len(self.df_bms)-2:
  227. cellvolt_now=self._cellvolt_get(i)
  228. cellvolt_min=min(cellvolt_now)
  229. cellvolt_max=max(cellvolt_now)
  230. cellvolt_last=self._cellvolt_get(i-1)
  231. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  232. if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and deltvolt<0.005:
  233. standingtime2=0
  234. cellvolt_sort=np.argsort(cellvolt_now)
  235. cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  236. cellmin_num=list(cellvolt_now).index(cellvolt_min)+1
  237. cellmax_num=list(cellvolt_now).index(cellvolt_max)+1
  238. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  239. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  240. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  241. cellsoc_diff=cellsoc_max-cellsoc_min
  242. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  243. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  244. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num, str(cellvolt_rank)]
  245. break
  246. else:
  247. continue
  248. else:
  249. continue
  250. else:
  251. standingtime2=0
  252. continue
  253. #更新RAM的standingtime
  254. df_ram_last3.loc[0]=[self.sn,self.bmstime[len(self.bmstime)-1],standingtime,standingtime1,standingtime2]
  255. if df_res.empty: #返回计算结果
  256. return pd.DataFrame(), df_ram_last3
  257. else:
  258. return df_res, df_ram_last3
  259. #磷酸铁锂电池一致性计算.........................................................................................................................
  260. def _lfp_uniform(self):
  261. column_name=['time','sn','cellsoc_diff','cellvolt_diff','cellmin_num','cellmax_num','cellvolt_rank']
  262. df_res=pd.DataFrame(columns=column_name)
  263. df_ram_lfp1=pd.DataFrame(columns=self.df_bms.columns.tolist())
  264. chrg_start=[]
  265. chrg_end=[]
  266. charging=0
  267. df_ram_last3=self.df_last3
  268. if df_ram_last3.empty:
  269. standingtime=0
  270. standingtime1=0
  271. standingtime2=0
  272. else:
  273. standingtime=df_ram_last3.loc[0,'standingtime']
  274. standingtime1=df_ram_last3.loc[0,'standingtime1']
  275. standingtime2=df_ram_last3.loc[0,'standingtime2']
  276. if abs(self.packcrnt[0])<0.01 and standingtime2>1:
  277. standingtime2=standingtime2+(self.bmstime[0]-df_ram_last3.loc[0,'time3']).total_seconds()
  278. else:
  279. pass
  280. for i in range(1,len(self.df_bms)-1):
  281. #静置电压法计算电芯一致性
  282. if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1: #电流为0
  283. delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
  284. standingtime2=standingtime2+delttime
  285. self._celltemp_weight(i) #获取不同温度对应的静置时间
  286. if standingtime2>self.StandardStandingTime: #静置时间满足要求
  287. if abs(self.packcrnt[i+1]) >= 0.1:
  288. standingtime2=0
  289. cellvolt_now=self._cellvolt_get(i)
  290. cellvolt_min=min(cellvolt_now)
  291. cellvolt_max=max(cellvolt_now)
  292. cellvolt_last=self._cellvolt_get(i-1)
  293. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  294. if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.002 and 2<cellvolt_min<4.5 and deltvolt<0.005:
  295. cellvolt_sort=np.argsort(cellvolt_now)
  296. cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  297. cellmin_num=list(cellvolt_now).index(cellvolt_min)+1
  298. cellmax_num=list(cellvolt_now).index(cellvolt_max)+1
  299. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  300. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  301. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  302. cellsoc_diff=cellsoc_max-cellsoc_min
  303. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  304. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  305. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num, str(cellvolt_rank)]
  306. # elif 2<cellvolt_max<4.5 and 2<cellvolt_min<4.5 and deltvolt<0.005:
  307. # cellvolt_sort=np.argsort(cellvolt_now)
  308. # cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  309. # if not df_res.empty:
  310. # df_res.loc[len(df_res)]=df_res.loc[len(df_res)-1]
  311. # df_res.loc[len(df_res)-1,'cellvolt_rank']=str(cellvolt_rank)
  312. # df_res.loc[len(df_res)-1,'time']=self.bmstime[i]
  313. # elif not self.df_uniform.empty:
  314. # df_res.loc[len(df_res)]=self.df_uniform.iloc[-1]
  315. # df_res.loc[len(df_res)-1,'cellvolt_rank']=str(cellvolt_rank)
  316. # df_res.loc[len(df_res)-1,'time']=self.bmstime[i]
  317. # else:
  318. # pass
  319. elif standingtime2>3600*6:
  320. cellvolt_now=self._cellvolt_get(i)
  321. cellvolt_min=min(cellvolt_now)
  322. cellvolt_max=max(cellvolt_now)
  323. cellvolt_last=self._cellvolt_get(i-1)
  324. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  325. if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.002 and 2<cellvolt_min<4.5 and deltvolt<0.005:
  326. standingtime2=0
  327. cellvolt_sort=np.argsort(cellvolt_now)
  328. cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  329. cellmin_num=list(cellvolt_now).index(cellvolt_min)+1
  330. cellmax_num=list(cellvolt_now).index(cellvolt_max)+1
  331. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  332. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  333. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  334. cellsoc_diff=cellsoc_max-cellsoc_min
  335. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  336. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  337. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num, str(cellvolt_rank)]
  338. # elif 2<cellvolt_max<4.5 and 2<cellvolt_min<4.5 and deltvolt<0.005:
  339. # cellvolt_sort=np.argsort(cellvolt_now)
  340. # cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  341. # if not df_res.empty:
  342. # df_res.loc[len(df_res)]=df_res.loc[len(df_res)-1]
  343. # df_res.loc[len(df_res)-1,'cellvolt_rank']=str(cellvolt_rank)
  344. # df_res.loc[len(df_res)-1,'time']=self.bmstime[i]
  345. # elif not self.df_uniform.empty:
  346. # df_res.loc[len(df_res)]=self.df_uniform.iloc[-1]
  347. # df_res.loc[len(df_res)-1,'cellvolt_rank']=str(cellvolt_rank)
  348. # df_res.loc[len(df_res)-1,'time']=self.bmstime[i]
  349. # else:
  350. # pass
  351. elif i>=len(self.df_bms)-2:
  352. standingtime2=0
  353. cellvolt_now=self._cellvolt_get(i)
  354. cellvolt_min=min(cellvolt_now)
  355. cellvolt_max=max(cellvolt_now)
  356. cellvolt_last=self._cellvolt_get(i-1)
  357. deltvolt=max(abs(cellvolt_now-cellvolt_last))
  358. if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.002 and 2<cellvolt_min<4.5 and deltvolt<0.003:
  359. cellvolt_sort=np.argsort(cellvolt_now)
  360. cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  361. cellmin_num=list(cellvolt_now).index(cellvolt_min)+1
  362. cellmax_num=list(cellvolt_now).index(cellvolt_max)+1
  363. cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
  364. cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
  365. cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
  366. cellsoc_diff=cellsoc_max-cellsoc_min
  367. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  368. cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
  369. df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num, str(cellvolt_rank)]
  370. # elif 2<cellvolt_max<4.5 and 2<cellvolt_min<4.5 and deltvolt<0.005:
  371. # cellvolt_sort=np.argsort(cellvolt_now)
  372. # cellvolt_rank=list(np.argsort(cellvolt_sort)+1)
  373. # if not df_res.empty:
  374. # df_res.loc[len(df_res)]=df_res.loc[len(df_res)-1]
  375. # df_res.loc[len(df_res)-1,'cellvolt_rank']=str(cellvolt_rank)
  376. # df_res.loc[len(df_res)-1,'time']=self.bmstime[i]
  377. # elif not self.df_uniform.empty:
  378. # df_res.loc[len(df_res)]=self.df_uniform.iloc[-1]
  379. # df_res.loc[len(df_res)-1,'cellvolt_rank']=str(cellvolt_rank)
  380. # df_res.loc[len(df_res)-1,'time']=self.bmstime[i]
  381. # else:
  382. # pass
  383. else:
  384. pass
  385. else:
  386. pass
  387. else:
  388. standingtime2=0
  389. pass
  390. if i==len(self.df_bms)-2 and abs(self.packcrnt[i+1]) < 0.1: #数据中断后仍在静置,将最后一条数据写入RAM
  391. df_ram_lfp1.loc[0]=self.df_bms.iloc[-1]
  392. else:
  393. pass
  394. #获取DVDQ算法所需数据——开始............................................................................................................
  395. if charging==0: #判断充电开始
  396. if self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.bms_soc[i]<40: #充电开始
  397. charging=1
  398. if len(chrg_start)>len(chrg_end):
  399. chrg_start[-1]=i
  400. else:
  401. chrg_start.append(i)
  402. else:
  403. pass
  404. else: #充电中
  405. if (self.bmstime[i+1]-self.bmstime[i]).total_seconds()>180 or (self.packcrnt[i]<-self.param.Capacity and self.packcrnt[i+1]<-self.param.Capacity): #如果充电过程中时间间隔>180s,则舍弃该次充电
  406. chrg_start.remove(chrg_start[-1])
  407. charging=0
  408. continue
  409. elif self.packcrnt[i]<=-1 and self.packcrnt[i-1]<=-1 and self.packcrnt[i+1]>-1: #判断电流波动时刻
  410. cellvolt_now=self._cellvolt_get(i)
  411. if max(cellvolt_now)>self.param.CellFullChrgVolt-0.1: #电压>满充电压
  412. chrg_end.append(i)
  413. charging=0
  414. continue
  415. else:
  416. pass
  417. elif self.packcrnt[i+1]>-0.1 and self.packcrnt[i]>-0.1: #判断充电结束
  418. charging=0
  419. if len(chrg_start)>len(chrg_end):
  420. if self.bms_soc[i]>90:
  421. chrg_end.append(i)
  422. else:
  423. chrg_start.remove(chrg_start[-1])
  424. continue
  425. else:
  426. continue
  427. elif i==len(self.packcrnt)-2 and self.packcrnt[i+1]<-1 and self.packcrnt[i]<-1:
  428. charging=0
  429. if len(chrg_start)>len(chrg_end) and self.bms_soc[i]>90: #soc>90
  430. chrg_end.append(i)
  431. continue
  432. else:
  433. df_ram_lfp1=self.df_bms.iloc[chrg_start[-1]:]
  434. chrg_start.remove(chrg_start[-1])
  435. continue
  436. else:
  437. continue
  438. if chrg_end: #DVDQ方法计算soc差
  439. peaksoc_list=[]
  440. for i in range(len(chrg_end)):
  441. peaksoc_list = []
  442. self._celltemp_weight(chrg_start[i])
  443. if min(self.celltemp)>10:
  444. for j in range(1, self.param.CellVoltNums + 1):
  445. cellvolt = self._singlevolt_get(i,j,2) #取单体电压j的所有电压值
  446. cellvolt = list(cellvolt[chrg_start[i]:chrg_end[i]])
  447. time = list(self.bmstime[chrg_start[i]:chrg_end[i]])
  448. packcrnt = list(self.packcrnt[chrg_start[i]:chrg_end[i]])
  449. soc = list(self.bms_soc[chrg_start[i]:chrg_end[i]])
  450. peaksoc = self._dvdq_peak(time, soc, cellvolt, packcrnt)
  451. if peaksoc>1:
  452. peaksoc_list.append(peaksoc) #计算到达峰值点的累计Soc
  453. else:
  454. pass
  455. if len(peaksoc_list)>self.param.CellVoltNums/2:
  456. peaksoc_max=max(peaksoc_list)
  457. peaksoc_min=min(peaksoc_list)
  458. peaksoc_maxnum=peaksoc_list.index(peaksoc_min)+1
  459. peaksoc_minnum=peaksoc_list.index(peaksoc_max)+1
  460. cellsoc_diff=peaksoc_max-peaksoc_min
  461. cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
  462. if not df_res.empty:
  463. cellvolt_rank=df_res.iloc[-1]['cellvolt_rank']
  464. df_res.loc[len(df_res)]=[self.bmstime[chrg_start[i]], self.sn, cellsoc_diff, 0, peaksoc_minnum, peaksoc_maxnum, cellvolt_rank]
  465. elif not self.df_uniform.empty:
  466. cellvolt_rank=self.df_uniform.iloc[-1]['cellvolt_rank']
  467. df_res.loc[len(df_res)]=[self.bmstime[chrg_start[i]], self.sn, cellsoc_diff, 0, peaksoc_minnum, peaksoc_maxnum, cellvolt_rank]
  468. else:
  469. pass
  470. else:
  471. pass
  472. else:
  473. pass
  474. #更新RAM的standingtime
  475. df_ram_last3.loc[0]=[self.sn,self.bmstime[len(self.bmstime)-1],standingtime,standingtime1,standingtime2]
  476. if df_res.empty:
  477. return pd.DataFrame(), df_ram_last3, df_ram_lfp1
  478. else:
  479. df_res.sort_values(by='time', ascending=True, inplace=True)
  480. return df_res, df_ram_last3, df_ram_lfp1