瀏覽代碼

更改项:
1.cbmsbatdiag诊断函数修正log中错误;
2.主函数修改项:修正Ram中数值,避免重复计算。

shangguanlie23 3 年之前
父節點
當前提交
273ff7fce8
共有 3 個文件被更改,包括 61 次插入124 次删除
  1. 42 108
      LIB/MIDDLE/SaftyCenter/diagfault/CBMSBatDiag.py
  2. 19 16
      LIB/MIDDLE/SaftyCenter/diagfault/main.py
  3. 二進制
      df_file.xlsx

+ 42 - 108
LIB/MIDDLE/SaftyCenter/diagfault/CBMSBatDiag.py

@@ -45,7 +45,7 @@ class BatDiag:
 
     #获取当前行所有电压数据............................................................................................
     def _cellvolt_get(self,num): 
-        cellvolt = np.array(self.df_bms.loc[num,self.cellvolt_name]/1000)
+        cellvolt = list(self.df_bms.loc[num,self.cellvolt_name]/1000)
         return cellvolt
 
     #..........................................三元电池诊断功能..................................................................
@@ -92,7 +92,7 @@ class BatDiag:
                 else:   #ram当前故障中有该故障,则判断是否退出该故障
                     if celltempmax0<self.param.CellTempHighLv1-5 and celltempmax1<self.param.CellTempHighLv1-5:    #二级高温恢复
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==4].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==4].index, 'end_time'] = time
                     else:
                         pass
             
@@ -110,7 +110,7 @@ class BatDiag:
                 else:   #ram当前故障中有该故障,则判断是否退出该故障
                     if celltempmax0>self.param.CellTempLowLv1+2 and celltempmax1>self.param.CellTempLowLv1+2:    #二级高温恢复
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==6].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==6].index, 'end_time'] = time
                     else:
                         pass
               
@@ -128,7 +128,7 @@ class BatDiag:
                 else:   #ram当前故障中有该故障,则判断是否退出该故障
                     if (celltempmax0-celltempmin0)<self.param.CellTempDiffLv1-2 and (celltempmax1-celltempmax0)>self.param.CellTempDiffLv1-2:  #二级温差恢复
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==8].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==8].index, 'end_time'] = time
                     else:
                         pass
 
@@ -155,7 +155,7 @@ class BatDiag:
                         else:   #ram当前故障中有该故障,则判断是否退出该故障
                             if celltemp_rate<self.param.CellTempRate-1: #温升故障恢复
                                 time=self.bmstime[i]
-                                self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==9].index, 'end_time'] = time
+                                self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==9].index, 'end_time'] = time
                     else:
                         pass
                 else:
@@ -192,7 +192,7 @@ class BatDiag:
                 else:   #ram当前故障中有该故障,则判断是否退出该故障
                     if cellvoltmax0<self.param.CellOvLv1-0.05 and cellvoltmax1<self.param.CellOvLv1-0.05:   #二级过压故障恢复
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==12].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==12].index, 'end_time'] = time
                     else:
                         pass
               
@@ -211,7 +211,7 @@ class BatDiag:
                 else:
                     if cellvoltmin0>self.param.CellUvLv1+0.1 and cellvoltmin1>self.param.CellUvLv1+0.1:
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==14].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==14].index, 'end_time'] = time
                     else:
                         pass
              
@@ -229,7 +229,7 @@ class BatDiag:
                 else:
                     if (cellvoltmax0-cellvoltmin0)<self.param.CellVoltDiffLv1-0.05 and (cellvoltmax1-cellvoltmin1)>self.param.CellVoltDiffLv1-0.05: #二级欠压恢复
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==16].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==16].index, 'end_time'] = time
                     else:
                         pass
             else:
@@ -249,7 +249,7 @@ class BatDiag:
             else:
                 if self.packvolt[i-1]<self.param.PackVoltOvLv1-0.05*self.param.CellVoltNums and self.packvolt[i]<self.param.PackVoltOvLv1-0.05*self.param.CellVoltNums: #电池包过压二级恢复
                     time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==18].index, 'end_time'] = time
+                    self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==18].index, 'end_time'] = time
                 else:
                     pass
           
@@ -267,7 +267,7 @@ class BatDiag:
             else:
                 if self.packvolt[i-1]>self.param.PackVoltUvLv1+0.1*self.param.CellVoltNums and self.packvolt[i]>self.param.PackVoltUvLv1+0.1*self.param.CellVoltNums:   #电池包二级欠压恢复
                     time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==20].index, 'end_time'] = time
+                    self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==20].index, 'end_time'] = time
                 else:
                     pass
             
@@ -294,7 +294,7 @@ class BatDiag:
             else:
                 if self.packcrnt[i-1]<self.param.PackDisOc-10 and self.packcrnt[i]<self.param.PackDisOc-10:
                     time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==22].index, 'end_time'] = time
+                    self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==22].index, 'end_time'] = time
                 else:
                     pass
             
@@ -311,7 +311,7 @@ class BatDiag:
             else:
                 if self.packcrnt[i-1]>self.param.PackChgOc+10 and self.packcrnt[i]>self.param.PackChgOc+10:
                     time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==22].index, 'end_time'] = time
+                    self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==22].index, 'end_time'] = time
                 else:
                     pass
 
@@ -337,7 +337,7 @@ class BatDiag:
                 else:
                     if abs(bmssoc_now-bmssoc_st)>self.param.SocClamp:   #SOC卡滞故障退出
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==27].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==27].index, 'end_time'] = time
                     else:
                         pass
                 bmssoc_st=bmssoc_now
@@ -362,7 +362,7 @@ class BatDiag:
                 else:   
                     if abs(bmssoc_now-bmssoc_st)<self.param.SocJump:    #SOC跳变故障退出
                         time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==28].index, 'end_time'] = time
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==28].index, 'end_time'] = time
                     else:
                         pass
             else:
@@ -371,7 +371,7 @@ class BatDiag:
             #SOC过低故障报警............................................................................................................
             if not 26 in list(self.df_diag_ram['code']):  #当前故障中没有该故障,则判断是否发生该故障
                 if self.bms_soc[i-1]<self.param.SocLow and self.bms_soc[i]<self.param.SocLow:   #SOC过低故障进入
-                    time=self.bmstime[0]
+                    time=self.bmstime[i]
                     code=26
                     faultlv=1
                     faultinfo='电池包电量过低'
@@ -382,28 +382,28 @@ class BatDiag:
             else:   
                 if self.bms_soc[i-1]>self.param.SocLow and self.bms_soc[i]>self.param.SocLow:   #SOC过低故障退出
                     time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==26].index, 'end_time'] = time
+                    self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==26].index, 'end_time'] = time
                 else:
                     pass
 
-            #BMS故障报警........................................................................................................
-            if not 1 in list(self.df_diag_ram['code']):  #当前故障中没有该故障,则判断是否发生该故障
-                if self.bmsfault1[i-1] is None or self.bmsfault1[i] is None:
-                    self.bmsfault1[i-1]=0
-                    self.bmsfault1[i]=0
-                if self.bmsfault1[i-1]>0 or self.bmsfault1[i]>0:   #BMS故障进入
-                    time=self.bmstime[0]
-                    code=1
-                    faultlv=2
-                    faultinfo='BMS故障报警:{}'.format(self.bmsfault1[i-1])
-                    faultadvice='技术介入诊断'
-                    self.df_diag_ram.loc[len(self.df_diag_ram)]=[time, end_time, self.sn, code, faultlv, faultinfo, faultadvice]
-                else:
-                    pass
-            else:
-                if self.bmsfault1[i-1]==0 and self.bmsfault1[i]==0:   #BMS故恢复
-                    time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==1].index, 'end_time'] = time
+            # #BMS故障报警........................................................................................................
+            # if not 1 in list(self.df_diag_ram['code']):  #当前故障中没有该故障,则判断是否发生该故障
+            #     if self.bmsfault1[i-1] is None or self.bmsfault1[i] is None:
+            #         self.bmsfault1[i-1]=0
+            #         self.bmsfault1[i]=0
+            #     if self.bmsfault1[i-1]>0 or self.bmsfault1[i]>0:   #BMS故障进入
+            #         time=self.bmstime[0]
+            #         code=1
+            #         faultlv=2
+            #         faultinfo='BMS故障报警:{}'.format(self.bmsfault1[i-1])
+            #         faultadvice='技术介入诊断'
+            #         self.df_diag_ram.loc[len(self.df_diag_ram)]=[time, end_time, self.sn, code, faultlv, faultinfo, faultadvice]
+            #     else:
+            #         pass
+            # else:
+            #     if self.bmsfault1[i-1]==0 and self.bmsfault1[i]==0:   #BMS故恢复
+            #         time=self.bmstime[i]
+            #         self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==1].index, 'end_time'] = time
         
         
         #SOC一致性故障报警..........................................................................................................
@@ -414,15 +414,15 @@ class BatDiag:
                     time=self.bmstime[0]
                     code=25
                     faultlv=1
-                    faultinfo='电芯{}和{}SOC差过大:{}'.format(self.df_uniform.loc[0,'cellmin_num'],self.df_uniform.loc[0,'cellmax_num']),cellsoc_diff
+                    faultinfo='电芯{}和{}SOC差过大:{}'.format(self.df_uniform.loc[0,'cellmin_num'],self.df_uniform.loc[0,'cellmax_num'],cellsoc_diff)
                     faultadvice='技术介入诊断'
                     self.df_diag_ram.loc[len(self.df_diag_ram)]=[time, end_time, self.sn, code, faultlv, faultinfo, faultadvice]
                 else:
                     pass
             else:
                 if cellsoc_diff<self.param.SocDiff: #SOC一致性差故障恢复
-                    time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==25].index, 'end_time'] = time
+                    time=self.bmstime[0]
+                    self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==25].index, 'end_time'] = time
         else:
             cellsoc_diff=3
 
@@ -447,8 +447,8 @@ class BatDiag:
                         pass
                 else:
                     if soh>self.param.SohLow+2:   #soh过低故障恢复
-                        time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==23].index, 'end_time'] = time
+                        time=self.bmstime[0]
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==23].index, 'end_time'] = time
                     else:
                         pass
 
@@ -464,8 +464,8 @@ class BatDiag:
                         pass
                 else:
                     if cellsoh_diff<self.param.SohDiff-2:
-                        time=self.bmstime[i]
-                        self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==24].index, 'end_time'] = time
+                        time=self.bmstime[0]
+                        self.df_diag_ram.loc[self.df_diag_ram[self.df_diag_ram['code']==24].index, 'end_time'] = time
                     else:
                         pass
             else:
@@ -488,70 +488,4 @@ class BatDiag:
         if not df_res.empty:
             return df_res
         else:
-            return pd.DataFrame()
-
-#............................................................内短路故障诊断.............................................................................
-class ShortDiag():
-    def __init__(self,sn,celltype,df_short):  #参数初始化
-
-        self.sn=sn
-        self.celltype=celltype
-        self.param=BatParam.BatParam(celltype)
-        self.df_short=df_short
-
-    def shortdiag(self):
-        if len(self.df_short)>1:
-            df_res=self._short_diag()
-            return df_res
-        else:
-            return pd.DataFrame()
-
-    #内短路故障检测...................................................................................................................................
-    def _short_diag(self):
-        column_name=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice']
-        df_res=pd.DataFrame(columns=column_name)
-        time=datetime.datetime.now()
-        end_time=datetime.datetime.strftime('0000-00-00 00:00:00')
-        end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d %H:%M:%S')
-
-        # if self.df_diag.empty:
-        #     health_state=100
-        # else:
-        #     health_state=self.df_diag.loc[0,'health_state']
-
-        for i in range(self.param.CellVoltNums):
-            #将字符串分割为多列
-            short_current=self.df_short['short_current']
-            short_current=short_current.str.replace("[", '')
-            short_current=short_current.str.replace("]", '')
-            self.df_short['cellshort'+str(i+1)]=short_current.map(lambda x:x.split(',')[i])
-            self.df_short['cellshort'+str(i+1)]=self.df_short['cellshort'+str(i+1)].map(lambda x:eval(x))
-
-            #漏电流故障判断
-            cellshort=np.array(self.df_short['cellshort'+str(i+1)])
-            shortlv3=np.sum(cellshort>self.param.LeakCurrentLv3)
-            shortlv2=np.sum(cellshort>self.param.LeakCurrentLv2)-shortlv3
-            shortlv1=np.sum(cellshort>self.param.LeakCurrentLv1)-shortlv2-shortlv3
-            shortlv=shortlv3*3 + shortlv2*2 + shortlv1
-
-            if not 31 in list(self.df_diag_ram['code']):  #当前故障中没有该故障,则判断是否发生该故障
-                if shortlv>=5:
-                    time=self.bmstime[0]
-                    code=31
-                    faultlv=3
-                    faultinfo='电芯{}发生严重内短路'.format(i+1)
-                    faultadvice='禁止充放电,检修电池'
-                    self.df_diag_ram.loc[len(self.df_diag_ram)]=[time, end_time, self.sn, code, faultlv, faultinfo, faultadvice]
-                else:
-                    pass
-            else:
-                if shortlv<3:
-                    time=self.bmstime[i]
-                    self.df_diag_ram[self.df_diag_ram[self.df_diag_ram['code']==31].index, 'end_time'] = time
-        
-        if not df_res.empty:
-            return df_res
-        else:
-            return pd.DataFrame()
-
-
+            return pd.DataFrame()

+ 19 - 16
LIB/MIDDLE/SaftyCenter/diagfault/main.py

@@ -68,6 +68,7 @@ def diag_cal():
 
         #电池诊断................................................................................................................................................................
         CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
+        df_Diag_Ram = df_Diag_Ram.drop_duplicates(subset=['product_id','code','start_time','Batpos','info'],keep='first')#sn之外的故障
         df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
         df_Diag_Ram_sn_else = pd.concat([df_Diag_Ram,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['product_id','code','start_time','Batpos','info'],keep=False)#sn之外的故障
         CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
@@ -77,7 +78,7 @@ def diag_cal():
             df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障   
             BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算
             df_Diag_Batdiag_update=BatDiag.diag() 
-            df_Diag_Cal_Update_add = pd.concat([CellFltInfo,df_Diag_Batdiag_update_xq,df_Diag_Batdiag_update])#重新计算的该SN下的故障
+            df_Diag_Cal_Update_add = pd.concat([df_Diag_Batdiag_update_xq,df_Diag_Batdiag_update])#重新计算的该SN下的故障
             df_Diag_Cal_Update_temp = df_Diag_Cal_Update_add.drop_duplicates(subset=['product_id','start_time','end_time','code','info'], keep='first', inplace=False, ignore_index=False)#去除相同故障
             df_Diag_cal_early_unfix = pd.DataFrame()
             df_Diag_Cal_finish = pd.DataFrame()
@@ -105,18 +106,20 @@ def diag_cal():
                 if not df_Diag_cal_early_fix.empty:
                     df_Diag_cal_early_fix['Batpos'] = 1
             df_Diag_Ram_Update = pd.concat([df_Diag_cal_early_unfix,df_Diag_cal_early_fix,df_Diag_Cal_finish])
+            df_Diag_Ram_Update['start_time'] = pd.to_dateime(df_Diag_Ram_Update['start_time'])
             df_Diag_Ram_Update.sort_values(by = ['start_time'], axis = 0, ascending=True,inplace=True)#该sn下当次诊断的故障状态
             df_Diag_Ram_add = pd.concat([df_Diag_Ram_Update,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
             df_Diag_Ram_Update_old = pd.concat([df_Diag_Ram_Update,df_Diag_Ram_add,df_Diag_Ram_add]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
             df_Diag_Ram_Update_change = pd.concat([df_Diag_Ram_Update_old,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['start_time','code','Batpos'],keep=False)#此次判断中新增故障
-            df_Diag_Ram = pd.concat([df_Diag_Ram_sn_else,df_Diag_Cal_new])
+            df_Diag_Ram_sav = df_Diag_Ram_Update.loc[df_Diag_Ram_Update['Batpos'] == 0]
+            df_Diag_Ram = pd.concat([df_Diag_Ram_sn_else,df_Diag_Ram_sav])
 
-        if (len(df_Diag_Ram_add) > 0) | (len(df_Diag_Ram_Update_change) > 0):#历史及现有故障
-            df_Diag_Ram.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',index=False,encoding='GB18030')
+        if len(df_Diag_Ram_sav) > 0:#历史及现有故障
+            df_Diag_Ram.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\result.csv',index=False,encoding='GB18030')
         if len(df_Diag_Ram_add) > 0:#新增故障
-            df_Diag_Ram_add.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result_add.csv',index=False,encoding='GB18030')
+            df_Diag_Ram_add.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\result_add.csv',index=False,encoding='GB18030')
         if len(df_Diag_Ram_Update_change) > 0:#更改故障
-            df_Diag_Ram_Update_change.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problemm\result_change.csv',index=False,encoding='GB18030')
+            df_Diag_Ram_Update_change.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\result_change.csv',index=False,encoding='GB18030')
         end=time.time()
         print(end-start)
 
@@ -124,7 +127,7 @@ def diag_cal():
 if __name__ == "__main__":
     global SNnums
     
-    excelpath=r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\sn-20210903.xlsx'
+    excelpath=r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\sn-20210903.xlsx'
     SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
     SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
     SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
@@ -145,15 +148,15 @@ if __name__ == "__main__":
     mylog.logcfg()
     #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
     host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
-    port=3306
-    db='safety_platform'
-    user='qx_read'
-    password='Qx@123456'
-    mode=2
-    tablename2='all_fault_info'
-    DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
-    with DBRead as DBRead:
-        df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory='骑享',sn='',timename='',st='',sp='')
+    port=3306
+    db='safety_platform'
+    user='qx_read'
+    password='Qx@123456'
+    mode=2
+    tablename2='all_fault_info'
+    DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
+    with DBRead as DBRead:
+        df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory='骑享',sn='',timename='',st='',sp='')
     # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
 
     #定时任务.......................................................................................................................................................................

二進制
df_file.xlsx