CalDist.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from math import radians, cos, sin, asin, sqrt
  2. import pandas as pd
  3. import numpy as np
  4. from datetime import datetime
  5. from datetime import timedelta
  6. from GpsRank import *
  7. from ProcessDfBms import *
  8. from ProcessDfGps import *
  9. from LIB.BACKEND import DBManager
  10. #####################################配置环境分割线#################################################
  11. def GetDistInfo(input_sn,input_starttime,input_endtime):
  12. #####################################配置参数分割线#################################################
  13. dbManager = DBManager.DBManager()
  14. data_raw = dbManager.get_data(sn=input_sn, start_time=input_starttime,
  15. end_time=input_endtime)
  16. #拆包预处理
  17. df_bms_raw=data_raw['bms']
  18. df_gps_raw=data_raw['gps']
  19. df_bms=preprocess_Df_Bms(df_bms_raw)
  20. df_gps=preprocess_Df_Gps(df_gps_raw)
  21. #####################################数据预处理分割线#################################################
  22. # mode: 0:正常取数; 1:7255 取数
  23. if input_sn[0:2] == 'UD' or input_sn[0:2] == 'MG':
  24. mode = 1
  25. else:
  26. mode = 0
  27. #获取状态表,mode默认为0,mode=1放电时电流为负,mode=0充电时电流为正
  28. df_bms_drive_timetable=get_bms_drive_timetable(df_bms,mode)
  29. df_gps_drive_cycle_accum=pd.DataFrame()
  30. if len(df_bms_drive_timetable)>0:
  31. for index in range(len(df_bms_drive_timetable)):
  32. #筛选drivecycle数据
  33. drive_start_time=df_bms_drive_timetable.loc[index,'drive_start_time']#开始时间
  34. drive_end_time=df_bms_drive_timetable.loc[index,'drive_end_time']#结束时间
  35. time_condition=(df_gps['time']>drive_start_time)&(df_gps['time']<drive_end_time)#时间判断条件
  36. df_gps_drive_cycle=df_gps.loc[time_condition,:].copy()
  37. df_gps_drive_cycle=df_gps_drive_cycle.reset_index(drop=True)#重置index
  38. #计算drivecycle GPS累计里程,存入表格
  39. condition_a=df_gps_drive_cycle['deltatime']>60*3
  40. condition_b=(df_gps_drive_cycle['deltatime']>90*1)&(df_gps_drive_cycle['distance']>1000)
  41. drive_cycle_dist_array=df_gps_drive_cycle.loc[~(condition_a|condition_b),'distance'].values
  42. drive_cycle_dist_array=drive_cycle_dist_array[np.where((drive_cycle_dist_array>=1)&(drive_cycle_dist_array<1000))[0]]
  43. gps_dist=drive_cycle_dist_array.sum()
  44. df_bms_drive_timetable.loc[index,'gps_dist']=gps_dist#得到GPS路径
  45. #计算头-尾的空缺时间段对应的预估SOC
  46. if len(df_gps_drive_cycle)>2:
  47. gps_starttime=df_gps_drive_cycle.loc[1,'time']#gps开始时间
  48. gps_endtime=df_gps_drive_cycle.loc[df_gps_drive_cycle.index[-1],'time']#gps结束时间
  49. #从drive_start_time到gps开始时间,使用SOC计算的里程
  50. #gps结束时间到drive_end_time,使用SOC计算的里程
  51. unrecorded_odo_head=cal_deltasoc(df_bms,drive_start_time,gps_starttime)
  52. unrecorded_odo_tail=cal_deltasoc(df_bms,gps_endtime,drive_end_time)
  53. else:
  54. #计算数据丢失行unrecordeodo
  55. unrecorded_odo_head=cal_deltasoc(df_bms,drive_start_time,drive_end_time)
  56. unrecorded_odo_tail=0
  57. #计算中间的预估SOC
  58. predict_dist=cal_unrecorded_gps(df_gps_drive_cycle,df_bms)
  59. #计算总的预估SOC
  60. totaldist=predict_dist+unrecorded_odo_head+ unrecorded_odo_tail#得到GPS路径
  61. df_bms_drive_timetable.loc[index,'predict_dist']=totaldist
  62. else :
  63. pass
  64. #####################################统计行驶里程End#################################################
  65. #打印输出结果#
  66. index_list=list(range(len(df_bms_drive_timetable)))
  67. dist_gps=0
  68. dist_predict=0
  69. day_start_time=''#当日开始时间
  70. day_end_time=''#当日结束时间
  71. day_start_soc=0#当日开始soc
  72. day_end_soc=0#当日结束soc
  73. day_min_soc=101#当日最低soc
  74. drive_accum_soc=0#累计使用SOC
  75. if len(df_bms_drive_timetable)>0:
  76. #开始行
  77. day_start_soc=df_bms_drive_timetable.loc[1,'drive_start_soc']#开始soc
  78. day_start_time=df_bms_drive_timetable.loc[1,'drive_start_time']#开始时间
  79. #结束行
  80. day_end_time=df_bms_drive_timetable.loc[len(df_bms_drive_timetable)-1,'drive_end_time']#结束时间
  81. day_end_soc=df_bms_drive_timetable.loc[len(df_bms_drive_timetable)-1,'drive_end_soc']#结束soc
  82. for index in index_list:
  83. '''汇总里程'''
  84. dist_gps+=df_bms_drive_timetable.loc[index,'gps_dist']/1000#计算GPS里程
  85. dist_predict+=df_bms_drive_timetable.loc[index,'predict_dist']#计算预估里程
  86. drive_start_soc=df_bms_drive_timetable.loc[index,'drive_start_soc']#驾驶周期开始的soc
  87. drive_end_soc=df_bms_drive_timetable.loc[index,'drive_end_soc']#驾驶周期结束的soc
  88. day_min_soc=min(day_min_soc,drive_start_soc,drive_end_soc)
  89. delta_soc=drive_start_soc-drive_end_soc#驾驶周期SOC变化量
  90. drive_accum_soc+=abs(delta_soc)#所有drive cycle累计消耗的soc
  91. # gps_score=get_df_gps_score(input_starttime,input_endtime,df_gps)
  92. # gps_score=round(gps_score,1)
  93. #计算总里程
  94. dist_gps=round(dist_gps,3)
  95. dist_predict=round(dist_predict,3)
  96. dist_all=round(dist_gps+dist_predict,3)
  97. #输出统计结果
  98. # print ('为您查询到,从'+input_starttime+'到'+input_endtime+'时间段内:')
  99. # print('SOC变化量:'+str(df_bms['bmspacksoc'].max()-df_bms['bmspacksoc'].min())+' %')
  100. # print('行驶总里程:'+str(dist_all)+' km')
  101. return {'SN':input_sn,'range':dist_all,'accum_soc':drive_accum_soc,'day_start_soc':day_start_soc,
  102. 'day_end_soc':day_end_soc,'day_start_time':day_start_time,'day_end_time':day_end_time,
  103. 'day_min_soc':day_min_soc}
  104. # print('其中GPS信号在线时里程:'+str(dist_gps)+' km')
  105. # print('其中GPS信号掉线时预估里程:'+str(dist_predict)+' km')
  106. # print('GPS信号质量评分为:'+str(gps_score),'分\n')
  107. #####################################打印结果End#################################################