-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbatteryCurve_USB.py
75 lines (64 loc) · 2.24 KB
/
batteryCurve_USB.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
"""This sample program will use the kel103 to test a batteries capacity and
show this information in matplotlib. This method is an aproximation and its resolution
can be increase with sampling rate.
"""
import socket
import time
import re
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from korad import kel103_usb
#test proporties
cutOffVoltage = 10
dischargeRate = 3.5
MISSED_LIMIT = 10 # amount of missed samples that is allowed
# setup the device (the IP of your ethernet/wifi interface, the IP of the Korad device)
kel = kel103_usb.kel103('COM5', 115200)
kel.checkDevice()
# a quick battery test
kel.setOutput(False)
voltage = kel.measureVolt()
kel.setCurrent(dischargeRate)
voltageData = []
timeData = []
current = 0
capacity = 0
kel.setOutput(True)
# run the test
startTime = time.time()
current_time = startTime
missedSuccessiveSamples = 0
while voltage > cutOffVoltage:
try:
# store the time before measuring volt/current
voltage = kel.measureVolt()
current = kel.measureCurrent()
voltageData.append(voltage)
# Only append the timedata when volt/current measurements went fine.
# This is because the voltage or current measurement could fail
# and then the x and y-axis would have different dimentions
previous_time = current_time
current_time = time.time()
timeData.append(time.time() - startTime)
# solve the current stuff as a running accumulation
capacity += ((previous_time - current_time) / 60 / 60) * current
print("Voltage: " + str(voltage) + " V DC, Capacity: " + str(capacity) + " Ah, Discharge Rate: " + str(dischargeRate) + " A")
time.sleep(0.25)
missedSuccessiveSamples = 0
except Exception as e:
print(e)
missedSuccessiveSamples += 1
if missedSuccessiveSamples >= MISSED_LIMIT:
raise Exception("Too many missed samples!")
# disable the output
kel.setOutput(False)
kel.endComm()
# plot the finished data
fig, ax = plt.subplots()
ax.plot(timeData, voltageData)
ax.set(xlabel='time (s)', ylabel='voltage (V DC)',
title='Battery Discharge Test {}A: {:.4f}Ah'.format(dischargeRate, capacity))
ax.grid()
fig.savefig("test_" + str(time.time()) + ".png")
plt.show()