-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsoc_predict.py
90 lines (74 loc) · 2.47 KB
/
soc_predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# predict soc from last 15 minutes of BMS data
import xgboost as xgb
import numpy as np
from influxdb import DataFrameClient, InfluxDBClient
import pandas as pd
host = '10.0.1.7'
port = 8086
dbname = 'sensors'
client = InfluxDBClient(host, port, database=dbname)
metrics_list = ['esphome-jbd-bms_state_of_charge',
'esphome-jbd-bms_average_cell_voltage',
'soc',
# 'esphome-jbd-bms_delta_cell_voltage',
'esphome-jbd-bms_current',
'esphome-jbd-bms_temperature_2',
# 'esphome-jbd-bms_cell_voltage_1',
# 'esphome-jbd-bms_cell_voltage_2',
# 'esphome-jbd-bms_cell_voltage_3',
# 'esphome-jbd-bms_cell_voltage_4',
# 'esphome-jbd-bms_cell_voltage_5',
# 'esphome-jbd-bms_cell_voltage_6',
# 'esphome-jbd-bms_cell_voltage_7',
# 'esphome-jbd-bms_cell_voltage_8'
]
for i, m in enumerate(metrics_list):
query= '''
select value from
"''' + m + '''"
where time > now() - 16m;
'''
try:
name = m.split('-')[2]
except:
name = m
result = client.query(query)
dat = result.raw['series'][0]['values']
col = result.raw['series'][0]['columns']
df = pd.DataFrame(dat, columns=col)
df = df.rename(columns={'value': name})
df.time = pd.to_datetime(df.time, format='ISO8601')
if i == 0:
rdf = df
else:
rdf = pd.merge_asof(rdf, df, on='time', direction='nearest')
rdf = rdf.set_index('time')
df2 = rdf.resample('10s').mean()
# # Predict
model = xgb.XGBRegressor()
model.load_model('soc_model.json')
df2['soc_d'] = df2.soc.shift(90)
df2['roll_v_10'] = df2.bms_average_cell_voltage.rolling(10).mean()
df2['roll_c_10'] = df2.bms_current.rolling(10).mean()
df2['roll_cs_90'] = df2.bms_current.rolling(90).sum()
df2['roll_vs_90'] = df2.bms_average_cell_voltage.rolling(90).sum()
df2['roll_v_90'] = df2.bms_average_cell_voltage.rolling(90).mean()
df2['roll_c_90'] = df2.bms_current.rolling(90).mean()
df2['temp_roll_90'] = df2.bms_temperature_2.rolling(90).mean()
df2 = df2.tail(1) # recent sample
X = df2[[
'bms_current',
'bms_average_cell_voltage',
'soc_d',
'roll_c_10',
'roll_c_90',
'roll_v_10',
'roll_v_90',
'roll_cs_90',
'bms_temperature_2',
'temp_roll_90'
]]
# Predict SOC value
pred = model.predict(X)
pred = pred.clip(0, 100.0)
print(pred[0])