rest_stscs.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from ctypes import Structure
  2. from re import M
  3. import pandas as pd
  4. import numpy as np
  5. import datetime
  6. import time, datetime
  7. import matplotlib.pyplot as plt
  8. from pylab import*
  9. from scipy.signal import savgol_filter
  10. from scipy import interpolate
  11. import os
  12. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import BatParam
  13. class cell_statistic:
  14. def __init__(self,sn,celltype,df_bms): #参数初始化
  15. self.sn=sn
  16. self.celltype=celltype
  17. self.param=BatParam.BatParam(celltype)#鹏飞param中为BatParam,学琦为BatteryInfo
  18. self.df_bms=pd.DataFrame(df_bms)
  19. self.packcrnt=df_bms['总电流[A]']*self.param.PackCrntDec
  20. self.packvolt=df_bms['总电压[V]']
  21. self.bms_soc=df_bms['SOC[%]']
  22. self.bmstime= pd.to_datetime(df_bms['时间戳'], format='%Y-%m-%d %H:%M:%S')
  23. self.LookTab_OCV = self.param.LookTab_OCV
  24. self.LookTab_SOC = self.param.LookTab_SOC
  25. self.packcrnt_flg = self.param.PackCrntDec#判断充放电电流方向,一般放电为正,充电为负
  26. self.cellvolt_list=['单体电压'+str(x) for x in range(1,self.param.CellVoltNums+1)]
  27. self.celltemp_name=['单体温度'+str(x) for x in range(1,self.param.CellTempNums+1)]
  28. self.bmssta = df_bms['充电状态']
  29. #定义加权滤波函数..................................................................................................
  30. def moving_average(interval, windowsize):
  31. window = np.ones(int(windowsize)) / float(windowsize)
  32. re = np.convolve(interval, window, 'same')
  33. return re
  34. #.............................................充电行为统计............................................................................
  35. def rest_sta(self):
  36. start_time = time.time()
  37. df_rest_soc = pd.DataFrame(columns = ['sn', 'time_st', 'time_end', 'soc_st', 'soc_sp', 'delta_soc', 'tempure', 'meancrnt'])
  38. data_temp = self.df_bms
  39. data = data_temp.dropna(axis = 0, how = 'any', subset = ['总电流[A]', 'SOC[%]'])#删除电流与SOC中的nan值
  40. #----------------------------------------区分充放电数据----------------------------------------------------------------------
  41. df_soc = data['SOC[%]']
  42. # df_soc[df_soc > 98] = 100#满SOC
  43. #-------------------------------------------------------利用电流连续为0判断电池静置---------------------------------------------------------
  44. rest_crnt = data.loc[data['总电流[A]'] == 0]#电流为0,判断电池静置
  45. if not rest_crnt.empty:
  46. rest_crnt_data = rest_crnt.reset_index(drop=True)
  47. temp_rest_time = rest_crnt_data['时间戳']
  48. rest_time = pd.to_datetime(temp_rest_time)
  49. delta_time = (np.diff(rest_time)/pd.Timedelta(1, 'min'))#计算时间差的分钟数
  50. pos = np.where(delta_time > 60)#静置数据分段,大于60min时,认为是两个静置过程
  51. splice_num = []
  52. if len(pos[0]) >= 1:
  53. pos_ful_tem = np.insert(pos, 0, 0)
  54. pos_len = len(pos_ful_tem)
  55. data_len = len(rest_time)
  56. pos_ful = np.insert(pos_ful_tem, pos_len, data_len-1)
  57. for item in range(0,len(pos_ful)-1):
  58. splice_num.extend(item*np.ones(pos_ful[item +1]-pos_ful[item]))
  59. splice_num = np.insert(splice_num, 0, 0)
  60. else:
  61. splice_num = np.zeros(len(temp_rest_time))
  62. pos_ful = np.array([0])
  63. rest_crnt_data['rest_rest'] = splice_num
  64. rest_splice_num = np.unique(rest_crnt_data['rest_rest'])#判断有几段充电数据
  65. if len(rest_splice_num) > 0:
  66. for item_rest in rest_splice_num:
  67. celltemp_name = self.celltemp_name#电芯温度数量
  68. df_rest_splice_data = rest_crnt_data.loc[rest_crnt_data['rest_rest'] == item_rest]
  69. df_time_temp = pd.to_datetime(df_rest_splice_data['时间戳'])
  70. df_soc_temp = df_rest_splice_data['SOC[%]']
  71. delta_soc = abs(round((df_soc_temp.iloc[-1] - df_soc_temp.iloc[0]), 3))
  72. delta_time = (df_time_temp.iloc[-1] - df_time_temp.iloc[0])/pd.Timedelta(1, 'hours')
  73. if (delta_soc < 2) & (delta_time > 1):
  74. chrg_rate = round(delta_soc/(100*delta_time), 2)
  75. df_rest_soc_temp = pd.DataFrame({"sn":[self.sn], "time_st":[df_time_temp.iloc[0]], "time_end":[df_time_temp.iloc[-1]],
  76. "soc_st":[df_soc_temp.iloc[0]], "soc_sp":[df_soc_temp.iloc[-1]], "delta_soc":[delta_soc],
  77. "tempure":[str(list(df_rest_splice_data[celltemp_name].iloc[0]))], "meancrnt":[chrg_rate]})
  78. df_rest_soc = df_rest_soc.append(df_rest_soc_temp)
  79. df_rest_soc.reset_index(drop = True, inplace = True)
  80. # df_rest_soc.sort_values(by = ['splicestrt_time'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  81. if not df_rest_soc.empty:
  82. return df_rest_soc
  83. else:
  84. return pd.DataFrame()