123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- import pandas as pd
- import numpy as np
- import datetime
- import bisect
- import matplotlib.pyplot as plt
- import BatParam
- class BatUniform:
- def __init__(self,sn,celltype,df_bms,df_volt,df_temp,df_accum): #参数初始化
- self.sn=sn
- self.celltype=celltype
- CellVoltNums=int(df_volt.loc[5,'单体电池总数'])
- if CellVoltNums==110:
- self.celltype=99
- else:
- self.celltype==100
- self.param=BatParam.BatParam(self.celltype)
- self.df_volt=df_volt
- self.df_temp=df_temp
- self.packcrnt=(df_volt['可充电储能装置电流(A)'].astype('float'))*self.param.PackCrntDec
- self.packvolt=df_volt['可充电储能装置电压(V)'].astype('float')
- self.bms_soc=df_bms['SOC']
- # self.bms_soh=df_volt['SOH[%]']
- self.bmstime= pd.to_datetime(df_volt['上报时间'], format='%Y-%m-%d %H:%M:%S')
- self.param.CellVoltNums=CellVoltNums
- self.param.CellTempNums=int(df_temp.loc[5,'可充电储能温度探针个数'])
- def batuniform(self):
- if self.celltype==1 or self.celltype==2 or self.celltype==3 or self.celltype==4 or self.celltype==100:
- df_res=self._ncm_uniform()
- return df_res
-
- elif self.celltype==99:
- df_res=self._lfp_uniform()
- return df_res
-
- else:
- return pd.DataFrame()
-
- #定义滑动滤波函数........................................................................................................................................
- def _np_move_avg(self,a, n, mode="same"):
- return (np.convolve(a, np.ones((n,)) / n, mode=mode))
-
- #寻找当前行数据的最小温度值................................................................................................................................
- def _celltemp_weight(self,num):
- celltemp = []
- for j in range(1, self.param.CellTempNums+1):
- s = str(j)
- celltemp.append(self.df_temp.loc[num, s+'.0'])
- celltemp.remove(min(celltemp))
- self.celltemp=celltemp
- if self.celltype==99:
- if min(celltemp)>=20:
- self.tempweight=1
- self.StandardStandingTime=1800
- elif min(celltemp)>=10:
- self.tempweight=0.6
- self.StandardStandingTime=3600
- elif min(celltemp)>=5:
- self.tempweight=0.
- self.StandardStandingTime=3600
- else:
- self.tempweight=0.1
- self.StandardStandingTime=7200
- else:
- if min(celltemp)>=20:
- self.tempweight=1
- self.StandardStandingTime=1800
- elif min(celltemp)>=10:
- self.tempweight=0.8
- self.StandardStandingTime=2400
- elif min(celltemp)>=5:
- self.tempweight=0.6
- self.StandardStandingTime=3600
- else:
- self.tempweight=0.2
- self.StandardStandingTime=7200
-
- #获取当前行所有电压数据............................................................................................................................
- def _cellvolt_get(self,num):
- cellvolt=[]
- for j in range(1, self.param.CellVoltNums+1):
- s = str(j)
- cellvolt.append(self.df_volt.loc[num,s+'.0'])
- return(cellvolt)
- #寻找DVDQ的峰值点,并返回..........................................................................................................................
- def _dvdq_peak(self, time, soc, cellvolt, packcrnt):
- cellvolt = self._np_move_avg(cellvolt, 3, mode="same")
- Soc = 0
- Ah = 0
- Volt = cellvolt[0]
- DV_Volt = []
- DQ_Ah = []
- DVDQ = []
- time1 = []
- soc1 = []
- soc2 = []
- xvolt=[]
- for m in range(1, len(time)):
- Step = (time[m] - time[m - 1]).total_seconds()
- Soc = Soc - packcrnt[m] * Step * 100 / (3600 * self.param.Capacity)
- Ah = Ah - packcrnt[m] * Step / 3600
- if (cellvolt[m]-Volt)>0.0019 and Ah>0:
- DQ_Ah.append(Ah)
- DV_Volt.append(cellvolt[m]-Volt)
- DVDQ.append((DV_Volt[-1])/DQ_Ah[-1])
- xvolt.append(cellvolt[m])
- Volt=cellvolt[m]
- Ah = 0
- soc1.append(Soc)
- time1.append(time[m])
- soc2.append(soc[m])
- #切片,去除前后10min的数据
- df_Data1 = pd.DataFrame({'time': time1,
- 'SOC': soc2,
- 'DVDQ': DVDQ,
- 'AhSoc': soc1,
- 'DQ_Ah':DQ_Ah,
- 'DV_Volt':DV_Volt,
- 'XVOLT':xvolt})
- start_time=df_Data1.loc[0,'time']
- start_time=start_time+datetime.timedelta(seconds=900)
- end_time=df_Data1.loc[len(time1)-1,'time']
- end_time=end_time-datetime.timedelta(seconds=1200)
- if soc2[0]<36:
- df_Data1=df_Data1[(df_Data1['SOC']>40) & (df_Data1['SOC']<80)]
- else:
- df_Data1=df_Data1[(df_Data1['time']>start_time) & (df_Data1['SOC']<80)]
- df_Data1=df_Data1[(df_Data1['XVOLT']>self.param.PeakVoltLowLmt) & (df_Data1['XVOLT']<self.param.PeakVoltUpLmt)]
- # print(packcrnt[int(len(time)/2)], min(self.celltemp))
- # ax1 = plt.subplot(3, 1, 1)
- # plt.plot(df_Data1['XVOLT'],df_Data1['DVDQ'],'g*-')
- # plt.xlabel('Volt/V')
- # plt.ylabel('DV/DQ')
- # plt.legend()
- # ax1 = plt.subplot(3, 1, 2)
- # plt.plot(df_Data1['SOC'],df_Data1['XVOLT'],'y*-')
- # plt.xlabel('SOC/%')
- # plt.ylabel('Volt/V')
- # plt.legend()
- # ax1 = plt.subplot(3, 1, 3)
- # plt.plot(df_Data1['SOC'], df_Data1['DVDQ'], 'r*-')
- # plt.xlabel('SOC/%')
- # plt.ylabel('DV/DQ')
- # plt.legend()
- # plt.show()
- if len(df_Data1)>2: #寻找峰值点,且峰值点个数>2
- PeakIndex = df_Data1['DVDQ'].idxmax()
- df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
- if len(df_Data2) > 2 and df_Data1.loc[PeakIndex,'XVOLT']<self.param.PeakVoltUpLmt-0.019:
- return df_Data1['AhSoc'][PeakIndex]
- else:
- df_Data1 = df_Data1.drop([PeakIndex])
- PeakIndex = df_Data1['DVDQ'].idxmax()
- df_Data2 = df_Data1[(df_Data1['SOC'] > (df_Data1['SOC'][PeakIndex] - 0.5)) & (df_Data1['SOC'] < (df_Data1['SOC'][PeakIndex] + 0.5))]
- if len(df_Data2) > 2 and df_Data1.loc[PeakIndex,'XVOLT']<self.param.PeakVoltUpLmt-0.019:
- return df_Data1['AhSoc'][PeakIndex]
- else:
- return 0
- else:
- return 0
-
- #三元电池一致性计算.................................................................................................................................
- def _ncm_uniform(self):
- column_name=['time','sn','cellsoc_diff','cellvolt_diff','cellmin_num','cellmax_num']
- df_res=pd.DataFrame(columns=column_name)
- standingtime=0
- for i in range(1,len(self.df_volt)-2):
- if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1: #电流为0
- delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
- standingtime=standingtime+delttime
- self._celltemp_weight(i) #获取不同温度对应的静置时间
- if standingtime>self.StandardStandingTime: #静置时间满足要求
- if abs(self.packcrnt[i+1]) >= 0.1:
- standingtime=0
- cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
- cellvolt_last=self._cellvolt_get(i-1)
- cellvolt_min=min(cellvolt_now)
- cellvolt_max=max(cellvolt_now)
- cellvolt_maxlast=max(cellvolt_last)
- if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and abs(cellvolt_maxlast-cellvolt_max)<0.003:
- cellmin_num=cellvolt_now.index(cellvolt_min)+1
- cellmax_num=cellvolt_now.index(cellvolt_max)+1
- cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
- cellsoc_diff=cellsoc_max-cellsoc_min
- cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
- cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
- df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
- elif standingtime>3600*12:
- standingtime=0
- cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
- cellvolt_last=self._cellvolt_get(i-1)
- cellvolt_min=min(cellvolt_now)
- cellvolt_max=max(cellvolt_now)
- cellvolt_maxlast=max(cellvolt_last)
- if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and abs(cellvolt_maxlast-cellvolt_max)<0.003:
- cellmin_num=cellvolt_now.index(cellvolt_min)+1
- cellmax_num=cellvolt_now.index(cellvolt_max)+1
- cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
- cellsoc_diff=cellsoc_max-cellsoc_min
- cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
- cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
- df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
- elif i>=len(self.df_volt)-4:
- standingtime=0
- cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
- cellvolt_last=self._cellvolt_get(i-1)
- cellvolt_min=min(cellvolt_now)
- cellvolt_max=max(cellvolt_now)
- cellvolt_maxlast=max(cellvolt_last)
- if 3<cellvolt_min<4.5 and 3<cellvolt_max<4.5 and abs(cellvolt_maxlast-cellvolt_max)<0.003:
- cellmin_num=cellvolt_now.index(cellvolt_min)+1
- cellmax_num=cellvolt_now.index(cellvolt_max)+1
- cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
- cellsoc_diff=cellsoc_max-cellsoc_min
- cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
- cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
- df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
- break
- else:
- continue
- else:
- continue
- else:
- standingtime=0
- continue
- if df_res.empty: #返回计算结果
- return pd.DataFrame()
- else:
- return df_res
- #磷酸铁锂电池一致性计算.........................................................................................................................
- def _lfp_uniform(self):
- column_name=['time','sn','cellsoc_diff','cellvolt_diff','cellmin_num','cellmax_num']
- df_res=pd.DataFrame(columns=column_name)
- standingtime=0
- chrg_start=[]
- chrg_end=[]
- charging=0
- for i in range(3,len(self.df_volt)-3):
- #静置电压法计算电芯一致性
- if abs(self.packcrnt[i]) < 0.1 and abs(self.packcrnt[i-1]) < 0.1: #电流为0
- delttime=(self.bmstime[i]-self.bmstime[i-1]).total_seconds()
- standingtime=standingtime+delttime
- self._celltemp_weight(i) #获取不同温度对应的静置时间
- if standingtime>self.StandardStandingTime: #静置时间满足要求
- cellvolt_now=self._cellvolt_get(i) #获取当前行电压数据
- cellvolt_last=self._cellvolt_get(i-1)
- cellvolt_min=min(cellvolt_now)
- cellvolt_max=max(cellvolt_now)
- cellvolt_maxlast=max(cellvolt_last)
- if abs(self.packcrnt[i+1]) >= 0.1 and abs(cellvolt_maxlast-cellvolt_max)<0.003:
- standingtime=0
- if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.02:
- cellmin_num=cellvolt_now.index(cellvolt_min)+1
- cellmax_num=cellvolt_now.index(cellvolt_max)+1
- cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
- cellsoc_diff=cellsoc_max-cellsoc_min
- cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
- cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
- df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
- elif i>=len(self.df_volt)-4:
- standingtime=0
- if 2 < cellvolt_max < self.param.OcvInflexionBelow-0.02:
- cellmin_num=cellvolt_now.index(cellvolt_min)+1
- cellmax_num=cellvolt_now.index(cellvolt_max)+1
- cellsoc_min=np.interp(cellvolt_min,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellsoc_max=np.interp(cellvolt_max,self.param.LookTab_OCV,self.param.LookTab_SOC)
- cellvolt_diff=(cellvolt_max-cellvolt_min)*1000
- cellsoc_diff=cellsoc_max-cellsoc_min
- cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
- cellvolt_diff=eval(format(cellvolt_diff,'.0f'))
- df_res.loc[len(df_res)]=[self.bmstime[i], self.sn, cellsoc_diff, cellvolt_diff, cellmin_num, cellmax_num]
- else:
- pass
- else:
- pass
- else:
- standingtime=0
- pass
- #获取DVDQ算法所需数据——开始............................................................................................................
- if charging==0: #判断充电开始
- if self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]<=-1 and float(self.bms_soc[i].strip('%'))<40: #充电开始
- charging=1
- if len(chrg_start)>len(chrg_end):
- chrg_start[-1]=i
- else:
- chrg_start.append(i)
- else:
- pass
- else: #充电中
- if (self.bmstime[i+1]-self.bmstime[i]).total_seconds()>180 or (self.packcrnt[i]>self.param.Capacity/3 and self.packcrnt[i+1]>self.param.Capacity/3): #如果充电过程中时间间隔>180s,则舍弃该次充电
- chrg_start.remove(chrg_start[-1])
- charging=0
- continue
- elif self.packcrnt[i]<=-1 and self.packcrnt[i+1]<=-1 and self.packcrnt[i+2]>-1: #判断电流波动时刻
- cellvolt_now=self._cellvolt_get(i+1)
- if max(cellvolt_now)>self.param.CellFullChrgVolt: #电压>满充电压
- chrg_end.append(i+1)
- charging=0
- continue
- else:
- pass
- elif self.packcrnt[i+1]>-0.1 and self.packcrnt[i+2]>-0.1: #判断充电结束
- charging=0
- if len(chrg_start)>len(chrg_end):
- if float(self.bms_soc[i].strip('%'))>90:
- chrg_end.append(i)
- else:
- chrg_start.remove(chrg_start[-1])
- continue
- else:
- continue
- elif i==len(self.packcrnt)-4 and self.packcrnt[i+1]<-1 and self.packcrnt[i+2]<-1:
- charging=0
- if len(chrg_start)>len(chrg_end):
- if float(self.bms_soc[i].strip('%'))>90: #soc>90
- chrg_end.append(i)
- continue
- else:
- chrg_start.remove(chrg_start[-1])
- continue
- else:
- continue
- else:
- continue
- # if chrg_end: #DVDQ方法计算soc差
- # peaksoc_list=[]
- # for i in range(len(chrg_end)):
- # peaksoc_list = []
- # self._celltemp_weight(chrg_start[i])
- # if min(self.celltemp)>10:
- # for j in range(1, self.param.CellVoltNums + 1):
- # s = str(j)
- # cellvolt = self.df_volt[s+'.0']
- # cellvolt = list(cellvolt[chrg_start[i]:chrg_end[i]])
- # time = list(self.bmstime[chrg_start[i]:chrg_end[i]])
- # packcrnt = list(self.packcrnt[chrg_start[i]:chrg_end[i]])
- # soc = list(self.bms_soc[chrg_start[i]:chrg_end[i]])
- # peaksoc = self._dvdq_peak(time, soc, cellvolt, packcrnt)
- # if peaksoc>1:
- # peaksoc_list.append(peaksoc) #计算到达峰值点的累计Soc
- # else:
- # pass
- # if len(peaksoc_list)>self.param.CellVoltNums/2:
- # peaksoc_max=max(peaksoc_list)
- # peaksoc_min=min(peaksoc_list)
- # peaksoc_maxnum=peaksoc_list.index(peaksoc_min)+1
- # peaksoc_minnum=peaksoc_list.index(peaksoc_max)+1
- # cellsoc_diff=peaksoc_max-peaksoc_min
- # cellsoc_diff=eval(format(cellsoc_diff,'.1f'))
- # df_res.loc[len(df_res)]=[self.bmstime[chrg_start[i]], self.sn, cellsoc_diff, 0, peaksoc_minnum, peaksoc_maxnum]
- # else:
- # pass
- # # plt.show()
- # else:
- # pass
- if df_res.empty:
- return pd.DataFrame()
- else:
- df_res.sort_values(by='time', ascending=True, inplace=True)
- return df_res
|