-
Notifications
You must be signed in to change notification settings - Fork 1
/
battery_capacity.py
88 lines (70 loc) · 2.52 KB
/
battery_capacity.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
#!/usr/bin/env python
import sys
import csv
import matplotlib.pyplot
from pprint import pprint
# Constants
DATADIR = "data/"
def parse_index(btype):
index = []
bdir = DATADIR + btype + "/"
with open(bdir + "index.csv", "rb") as f:
reader = csv.reader(f)
try:
for i, row in enumerate(reader):
if i > 0:
expcap = 0
if row[4] != "":
expcap = int(row[4])
index.append({
"show": bool(int(row[0])),
"brand": row[1],
"model": row[2],
"voltage": float(row[3]),
"exp_capacity": expcap,
"current": int(row[5]),
"type": row[6],
"cutoff": float(row[7]),
"file": row[8],
"comment": row[9]
})
except csv.Error as err:
sys.exit("File %s, line %d: %s" % (filename, reader.line_num, err))
return index
def parse_battery(filename, current, cutoff = 0.8):
mah = []
volts = []
with open(filename, "rb") as f:
reader = csv.reader(f)
try:
for i, row in enumerate(reader):
voltage = float(row[2])
mah.append(current * (float(i) / 3600))
volts.append(voltage)
if (voltage < cutoff):
break
except csv.Error as err:
sys.exit("File %s, line %d: %s" % (filename, reader.line_num, err))
return { "mah": mah, "volts": volts }
def get_batteries(btype):
index = parse_index(btype)
bdir = DATADIR + btype + "/"
batteries = []
for battery in index:
if battery["show"]:
batteries.append({ "name": battery["brand"] + " " + battery["model"],
"data": parse_battery(bdir + battery["file"], battery["current"], battery["cutoff"]) })
return batteries
if __name__ == "__main__":
batteries = get_batteries("9V")
# Prepare the plot.
matplotlib.pyplot.style.use("ggplot")
for batt in batteries:
matplotlib.pyplot.plot(batt["data"]["mah"], batt["data"]["volts"], label = batt["name"])
# Setup the plot.
matplotlib.pyplot.title("Battery Discharge")
matplotlib.pyplot.legend(loc = "upper right")
matplotlib.pyplot.xlabel("Capacity (mAh)")
matplotlib.pyplot.ylabel("Voltage (V)")
matplotlib.pyplot.grid(True)
matplotlib.pyplot.show()