-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcalvcourse.py
98 lines (89 loc) · 3.71 KB
/
calvcourse.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
#!/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+1)]
endlog=tail(config['files']['logfile'],11)
for j in range(1,numcells+1):
for i in range(10):
x=float(endlog[i][(j-1)*6+15:(j-1)*6+20])+config['calibrate']['delta'][j-1]
v[j]=v[j]+x
for i in range(1,len(v)):
v[i]=v[i]/10+v[i-1]
vstr=vstr+str(i) + "=" +str(round(v[i],3)).ljust(5,'0') + ", "
print(vstr)
print(config['calibrate']['measured'])
print(config['calibrate']['displayed'])
return v
def main():
print('Usage: Pressing just the [Enter] key will display the latest voltage readings, type ^C to exit')
print(' Type in the cell # that you want to calibrate followed by [Enter]')
print(' Then type in the mesured voltage for that cell followed by [Enter]')
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("Cell " + what + " reading "))
what=int(what)
config['calibrate']['measured'][what] = round(realvolts,3)
config['calibrate']['displayed'][what] = round(avvolts[what],3)
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','measured',str(config['calibrate']['measured']))
batconfigdata.set('calibrate','displayed',str(config['calibrate']['displayed']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break