-
Notifications
You must be signed in to change notification settings - Fork 20
/
getdata.py
211 lines (189 loc) · 8.89 KB
/
getdata.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
# *****BatteryMonitor Getdata from battery cells getdata.py*****
# Copyright (C) 2014 Simon Richard Matthews
# Project loaction https://github.com/simat/BatteryMonitor
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#from test import config
import re
from config import config
numcells = config['battery']['numcells']
import time
#from x import Raw
class Readings:
""" get and manipulates readings from the real world"""
def __init__(self):
self.measured = config['calibrate']['measured']
self.displayed = config['calibrate']['displayed']
self.ratio = [ 1.0 for i in range(len(self.measured))]
for i in range(1, len(self.measured)):
self.ratio[i] = self.measured[i]/self.displayed[i]
self.calvolts = [ 0.0 for i in range(len(self.measured))]
# calvolts = [measureddelta[i] - displayeddelta[i] for i in range(numcells+1)]
self.voltsav = [ 3.25 for i in range(len(self.measured)+1)]
self.voltsav[0] = 0.0
self.rawvolts = [ 0.0 for i in range(len(self.measured))]
self.batvolts = [ i*3.25 for i in range(len(self.measured))]
self.uncalvolts = [ i*3.25 for i in range(len(self.measured))]
self.batvoltsav = [ i*3.25 for i in range(len(self.measured))]
self.balflg = [ 0.0 for i in range(numcells)]
self.baltime = [ 0.0 for i in range(numcells)]
self.temp = [ 0.0 for i in range(len(config['TemperatureInputs']))]
self.numiins = len(config['CurrentInputs'])
self.current = [ 0.0 for i in range(self.numiins)]
self.currentav = [ 0.0 for i in range(self.numiins)]
self.ibatminute = 0.0 #sum of last minutes battery current readings
self.ibatnuminmin = 0 # numvber of samples in last minute
self.kWhin = [ 0.0 for i in range(self.numiins)]
self.kWhout = [ 0.0 for i in range(self.numiins)]
self.chargestates = [ b'00' for i in range(len(config['Status']))]
self.pwravailable = 0.0 # amount of excess solar power available
self.minmaxdemandpwr = [0.0,0.0] # min and max amount of excess power
self.batcapresidual = config['battery']['capacity']*\
(1-(int(time.strftime("%Y", time.localtime()))\
-config['battery']['yearinstalled'])\
*(config['battery']['lossperyear']/100)) # residual battery capacity factor
# rawcurrent = 0.0
# batcurrent = 0.0
# batcurrentav = 0.0
# rawincurrent = 0.0
# incurrent = 0.0
# incurrentav = 0.0
self.soc = 0.0
self.socadj = 0.0
self.batah = 0.0
self.batahadj = 0.0
self.inah = 0.0
self.inahtot = 0.0
self.ah = 0.0
self.prevbatvoltage = 0.0
self.mincellv=100.0
self.maxcellv=0.0
self.lastmaxv = 0.0 # previous sample maximum cell voltage
self.lastminv = 100.0 # previous sample minimum cell voltage
self.pwrbat = 0.0 # battery power units kW
self.pwrbattot = 0.0 # total battery power units kWh
self.pwrin = 0.0 # gross power in units kW
self.pwrintot = 0.0 # total gross power in units kWh
self.iall=""
self.vdelta=""
self.vcells=""
self.soctxt=""
self.socadjtxt=""
self.sampleshr=3600/config['sampling']['sampletime']
self.interfacesinuse=[]# numder of sample / hour
batpwr1hrav = 0.0
for i in config['Interfaces']:
interface=re.match(r'\w*',config['Interfaces'][i]).group()
snstring=re.compile(r'[(].*[^)]')
sn=snstring.search(config['Interfaces'][i])
if sn!=None:
sn=sn.group()
exec("import " + interface)
if sn==None:
# exec('self.'+i +'='+interface+'.Rawdat(self.interfacesinuse)')
exec('self.{}={}.Rawdat({})'.format(i,interface,self.interfacesinuse))
else:
# exec('self.'+i+'='+interface+".Rawdat('"+str(sn[1:])+"self.interfacesinuse')")
exec('self.{}={}.Rawdat("{}",{})'.format(i,interface,sn[1:],self.interfacesinuse))
self.vin = []
for i in sorted(config['VoltageInputs']):
self.vin = self.vin + [config['VoltageInputs'][i]]
# self.vin = self.vin + [compile('self.'+config['VoltageInputs'][i], '<string>', 'eval')]
self.iin = []
for i in sorted(config['CurrentInputs']):
self.iin = self.iin + [config['CurrentInputs'][i]]
# self.iin = self.iin + [compile(config['CurrentInputs'][i], '<string>', 'eval')]
self.tin = [] # temperatures
for i in sorted(config['TemperatureInputs']):
self.tin = self.tin + [config['TemperatureInputs'][i]]
self.balf = [] # balance flags
for i in sorted(config['BalanceFlags']):
self.balf = self.balf + [config['BalanceFlags'][i]]
self.chgstat = [] # balance flags
for i in sorted(config['Status']):
self.chgstat = self.chgstat + [config['Status'][i]]
self.sampletime = time.time()
self.getvi()
self.batvoltsav = self.batvolts
self.batcurrentav = self.current[-3]
self.incurrentav = self.current[-2]
self.batpwr1hrav = self.batvoltsav[-1]*self.batcurrentav/1000
for i in range(0,self.numiins):
self.currentav[i] = self.current[i]
# (self.batvoltsav, self.current)
def getvi(self):
""" Get raw data """
self.oldsampletime=self.sampletime
sleeptime = max(config['sampling']['sampletime'] - (time.time()-self.oldsampletime), 0.0)
# sleeptime
time.sleep(sleeptime)
self.sampletime = time.time()
# get data from Interfaces
for i in config['Interfaces']:
exec('self.'+i+".getdata()")
for i in range(len(self.iin)):
self.current[i] = eval(self.iin[i]) \
*config['calibrate']['currentgain'][i] \
-config['calibrate']['currentoffset'][i]
# self.batvolts[0] = self.rawdata.rawv[0]
# self.uncalvolts[0] = self.rawdata.rawv[0]
# self.batvolts[0] = 0.0
# self.uncalvolts[0] = 0.0
for i in range(len(self.vin)):
self.uncalvolts[i+1] = eval(self.vin[i]) \
*config['calibrate']['batvgain'] # A/D to battery volts
self.batvolts[i+1] = self.uncalvolts[i+1]*self.ratio[i] # calibrate values
for i in range(len(self.tin)): # get temperatures
self.temp[i] = eval(self.tin[i])
for i in range(numcells): # get balance flags
self.balflg[i] = eval(self.balf[i])
for i in range(len(self.chgstat)): # get PIP charge states
self.chargestates[i]=eval(self.chgstat[i])
def getraw(self):
""" gets battery data, do averaging, voltage results in volts, current in amps"""
self.getvi()
samplesav = config['sampling']['samplesav']
self.deltatime=(self.sampletime-self.oldsampletime)/3600
self.batah = self.currentav[-3]*self.deltatime
self.batahadj = (self.currentav[-3]+config['battery']['ahloss'])*self.deltatime
self.inah = self.currentav[-2]*self.deltatime
for i in range(1,len(self.measured)):
self.batvoltsav[i] = (self.batvoltsav[i]*(samplesav-1) \
+ self.batvolts[i])/samplesav
batvoltsav = self.batvoltsav[config['battery']['numcells']+1]
self.pwrin = self.inah*batvoltsav/1000 # gross input energy
self.pwrbat = self.batah*batvoltsav/1000 # battery energy in/out
self.batpwr1hrav = self.batpwr1hrav \
+1.2*(self.currentav[-3]*batvoltsav/1000-self.batpwr1hrav)/self.sampleshr # caculate battery power 1hr running av in kW
self.ibatminute = self.ibatminute+self.currentav[-3]
self.ibatnuminmin += 1
for i in range(0,self.numiins):
self.currentav[i] = (self.currentav[i]*(samplesav-1)+self.current[i])/samplesav # running av current
if self.currentav[i] < 0:
self.kWhin[i] = self.kWhin[i]+self.currentav[i]*self.deltatime*batvoltsav/1000
else:
self.kWhout[i] = self.kWhout[i]+self.currentav[i]*self.deltatime*batvoltsav/1000
for i in range(numcells+1):
if self.balflg[i-1]:
self.baltime[i-1]= self.baltime[i-1]+self.deltatime # update time balancers are on
self.voltsav[0]=round(self.batvolts[0],3)
self.lastmincellv=self.mincellv
self.lastmaxcellv=self.maxcellv
self.mincellv=100.0
self.maxcellv=0.0
for i in range(numcells,0,-1): # caculate cell voltages
self.voltsav[i]=round((self.batvoltsav[i]-self.batvoltsav[i-1]-config['calibrate']['delta'][i-1]),3)
self.mincellv = min(self.voltsav[i],self.mincellv)
self.maxcellv = max(self.voltsav[i],self.maxcellv)
for i in range(numcells+1,len(self.measured)): # do other voltages
self.voltsav[i]=round(self.batvoltsav[i],2)