-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery_demo.py
132 lines (98 loc) · 5.1 KB
/
battery_demo.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 4 10:16:14 2020
GUI for battery demonstartion using real-time data from OPC UA
@author: hube
"""
# %% Import libraries
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from time import sleep
from matplotlib.widgets import Slider, Button
from opcuaclient_subscription import opcua, toggle
style.use('fivethirtyeight')
# %% Establish OPC UA connection
if __name__ == "__main__":
opcua = opcua(user='BenjaminHuberEMPA', password='BenjaminHuberEMPA2020')
try:
opcua.connect()
df_nodes = pd.DataFrame({'node':["ns=2;s=Gateway.PLC1.65NT-03032-D001.PLC1.MET51.strMET51Read.strWetterstation.strStation1.lrLufttemperatur",
"ns=2;s=Gateway.PLC1.65NT-03032-D001.PLC1.MET51.strMET51Read.strWetterstation.strStation1.lrGlobalstrahlung"]})
opcua.subscribe(df_Read=df_nodes)
fig, ax = plt.subplots(2, 1)
fig.set_size_inches (8.27, 5.5)
fig.subplots_adjust(bottom=0.05, top=0.96, left=0.045, right=0.99, hspace=0.13)
def animate(i):
df = opcua.handler.df_Read
df_temp = df.where(df['node'] == df_nodes.iloc[0, 0]).dropna()
df_temp['timestamp'] = pd.to_datetime(df_temp['timestamp'])
df_temp = df_temp.set_index('timestamp')
df_temp.rename(columns={'value':'temp_amb'}, inplace=True)
df_plot = df_temp[['temp_amb']].resample('1S').first()
df_temp = df.where(df['node'] == df_nodes.iloc[1, 0]).dropna()
df_temp['timestamp'] = pd.to_datetime(df_temp['timestamp'])
df_temp = df_temp.set_index('timestamp')
df_temp.rename(columns={'value':'irrad'}, inplace=True)
df_plot = pd.concat([df_plot, df_temp[['irrad']].resample('1S').first()], axis=1)
try:
df_plot['temp_amb'].interpolate(method='linear', limit_directions='both', inplace=True)
df_plot['irrad'].interpolate(method='linear', limit_directions='both', inplace=True)
except:
print("Not interpolated")
ax[0].clear()
ax[1].clear()
ax[0].plot(df_plot["temp_amb"], label='$T_{\mathrm{amb}}$ [°C]')
ax[0].set_xlim(df_plot.index.min(), df_plot.index.max())
ax[0].set_ylabel('$T_{\mathrm{amb}}$ [°C]', fontsize=11.)
ax[0].tick_params(axis='both', which='major', labelsize=11, length=0, labelbottom=False)
ax[0].legend(fontsize=11., loc='upper left')
ax[1].plot(df_plot["irrad"], color='orange', label=r'$\dot{Q}_{\mathrm{solar}}$ $\left[\frac{\mathrm{W}}{\mathrm{m}^2}\right]$')
ax[1].set_xlim(df_plot.index.min(), df_plot.index.max())
ax[1].set_ylabel(r'$\dot{Q}_{\mathrm{solar}}$ $\left[\frac{\mathrm{W}}{\mathrm{m}^2}\right]$', fontsize=11.)
ax[1].tick_params(axis='both', which='major', labelsize=11, length=0, labelbottom=True)
ax[1].legend(fontsize=11., loc='upper left')
fig.align_ylabels()
plt.tight_layout()
ani = animation.FuncAnimation(fig, animate, interval=1000)
figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
plt.show()
# %% Create GUI for battery handling
# fig, ax = plt.subplots()
# plt.subplots_adjust(top=0.985,
# bottom=0.198,
# left=0.047,
# right=0.991,
# hspace=0.2,
# wspace=0.2)
# P_0 = 0
# dP_batt = 1
# t = com['DFAB/371/temperature'].index
# T = gaussian_filter(com['DFAB/371/temperature'].values, sigma=sigma_0)
# l0 = plt.plot(t, com['DFAB/371/temperature'].values, label="$T_{\mathrm{raw}}$")
# l, = plt.plot(t, T, lw=2, label="$T_{\mathrm{Gauss-filtered}}$")
# ax.set_xlabel("Timestamp")
# ax.set_ylabel("Temperature [°C]")
# ax.legend(loc="upper right")
# ax.margins(x=0)
# axcolor = 'lightgoldenrodyellow'
# axSigma = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
# sSigma = Slider(axSigma, '$\sigma$', -100.0, 100.0, valinit=sigma_0, valstep=delta_sigma)
# def update(val):
# sigma = sSigma.val
# l.set_ydata(gaussian_filter(com['DFAB/371/temperature'].values, sigma=sigma))
# fig.canvas.draw_idle()
# sSigma.on_changed(update)
# resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
# button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
# def reset(event):
# sSigma.reset()
# button.on_clicked(reset)
# figManager = plt.get_current_fig_manager()
# figManager.window.showMaximized()
# plt.show()
finally:
opcua.disconnect()
print("OPC UA disconnected")