-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcalvfineall.py
114 lines (100 loc) · 3.85 KB
/
calvfineall.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
#!/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
numcells = config['battery']['numcells']
voltages = []
avv = []
summaryfile = SafeConfigParser()
def tail(file, n=1, bs=1024):
f = open(file,'rb')
f.seek(-1,2)
l = 1-f.read(1).decode('utf-8').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)
x = f.read(block).decode('utf-8')
l += x.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 avv():
vstr=""
v=[0.0 for i in range(numcells)]
endlog=tail(config['files']['logfile'],11)
for j in range(numcells):
for i in range(10):
x=float(endlog[i][j*6+15:j*6+20])
v[j]=v[j]+x
for i in range(len(v)):
v[i]=v[i]/10
vstr=vstr+str(i+1) + "=" +str(round(v[i],3)).ljust(5,'0') + ", "
print(vstr)
print(config['calibrate']['delta'])
return v
def getv():
global voltages
try:
summaryfile.read(config['files']['summaryfile'])
except IOError:
pass
voltages=literal_eval(summaryfile.get('current','maxvoltages'))
vprint = ''
for i in range(numcells+1):
vprint = vprint + str(i+1) + '=' + str(voltages[i]).ljust(5,'0') + ', '
print(vprint)
print(config['calibrate']['delta'])
def main():
print('Usage: Pressing just the [Enter] key will display the latest voltage readings, type ^C to exit')
print(' Press any other key to change the voltage calibration of all the voltage readings')
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 calibration is accurate make sure the input voltages are not varying')
avvolts=[]
while True:
try:
loadconfig()
avvolts=avv()
# time.sleep(60.0)
what = input(">")
if len(what)>0:
realvolts = eval(input("All Cell readings "))
for i in range(numcells):
config['calibrate']['delta'][i] = round(avvolts[i]-realvolts+config['calibrate']['delta'][i],3)
print(config['calibrate']['delta'])
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','delta',str(config['calibrate']['delta']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break
if __name__ == "__main__":
main()