-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcaligain.py
110 lines (98 loc) · 4.06 KB
/
caligain.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
#!/usr/bin/python
# *****BatteryMonitor store summary data summary.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.
import sys
from os import rename
import time
from shutil import copy as filecopy
from copy import deepcopy
from ast import literal_eval
from configparser import SafeConfigParser
from config import config, loadconfig
numiin = len(config['CurrentInputs'])
numcells = config['battery']['numcells']
summaryfile = SafeConfigParser()
def tail(file, n=1, bs=1024):
f = open(file)
f.seek(-1,2)
l = 1-f.read(1).count('\n') # If file doesn't end in \n, count it anyway.
B = f.tell()
while n >= l and B > 0:
block = min(bs, B)
B -= block
f.seek(B, 0)
l += f.read(block).count('\n')
f.seek(B, 0)
l = min(l,n) # discard first (incomplete) line if l > n
lines = f.readlines()[-l:]
f.close()
return lines
def getavi():
istr=""
avi = [ 0.0 for i in range(numiin)]
endlog=tail(config['files']['logfile'],11)
for line in range(10):
lineargs = str(endlog[line]).split(' ')
for iin in range(numiin):
avi[iin] = avi[iin] + float(lineargs[1+numcells+2+iin])/10
for iin in range(numiin):
istr =istr+str(round(avi[iin],3)) + ", "
print('Average readings for last 100 seconds: ' + istr)
loadconfig()
print('gain from config file: ' + str(config['calibrate']['currentgain']))
return avi
def geti():
try:
summaryfile.read(config['files']['summaryfile'])
except IOError:
pass
iin=literal_eval(summaryfile.get('current','ioutmax'))
iprint = ''
for i in range(numiin):
iprint = iprint + str(i+1) + '=' + str(iin[i]).ljust(5,'0') + ', '
print('Readings from summary: ' + iprint)
def main():
print('Usage: At > prompt enter current input channel number to change then [Enter] key')
print(' Pressing just the [Enter] key will display the latest current readings, type ^C to exit')
print()
print(' Make sure the Battery Monitoring software is running otherwise the summary file will not be current')
print(' This program uses the data from the log file generated by the Battery Monitoring software')
print(' Make sure that the Battery Monitoring software is logging to a real file and not /dev/null')
print(' This program averages the data from the log file for the last 100 seconds.')
print(' To make sure that the calibartion is accurate make sure the current on the input being calibrated is not varying')
while True:
try:
# time.sleep(60.0)
geti()
what = input(">")
if len(what)>0:
avi=getavi()
reali = eval(input("Input " + what + " multimeter reading "))
if reali == 0:
print("Input Error: Zero not valid current input")
else:
what=int(what)
config['calibrate']['currentgain'][what-1] = config['calibrate']['currentgain'][what-1]*reali/avi[what-1]
print('Recalculated gain: ' + str(config['calibrate']['currentgain']))
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','currentgain',str(config['calibrate']['currentgain']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break
if __name__ == "__main__":
main()