|
1 |
| -"""This sample program will use the kel103 to test a batteries capacity and |
2 |
| -show this information in matplotlib. This method is an aproximation and its resolution |
3 |
| -can be increase with sampling rate. |
4 | 1 | """
|
5 |
| -import socket |
| 2 | +This sample program will use the kel103 to test a battery's capacity and |
| 3 | +show this information in matplotlib. This method is an aproximation and its |
| 4 | +resolution can be increased with sampling rate. |
| 5 | +""" |
| 6 | + |
6 | 7 | import time
|
7 | 8 | import re
|
8 | 9 | import matplotlib
|
|
16 | 17 | MISSED_LIMIT = 10 # amount of missed samples that is allowed
|
17 | 18 |
|
18 | 19 | # setup the device (the IP of your ethernet/wifi interface, the IP of the Korad device)
|
19 |
| -kel = kel103.kel103("192.168.8.126", "192.168.8.128", 18190) |
| 20 | +host_ip = "192.168.x.x" |
| 21 | +device_ip = "192.168.x.x" |
| 22 | +port = 18190 |
| 23 | +kel = kel103.kel103(host_ip, device_ip, port) |
20 | 24 | kel.checkDevice()
|
21 | 25 |
|
22 | 26 | # a quick battery test
|
23 |
| -kel.setOutput(False) |
| 27 | +kel.setOutput(False) |
24 | 28 | voltage = kel.measureVolt()
|
25 | 29 | kel.setCurrent(dischargeRate)
|
26 | 30 | voltageData = []
|
|
33 | 37 | startTime = time.time()
|
34 | 38 | missedSuccessiveSamples = 0
|
35 | 39 |
|
| 40 | +fileBasename = f"test_{time.time()}" |
| 41 | + |
36 | 42 | while voltage > cutOffVoltage:
|
37 | 43 | try:
|
38 | 44 | # store the time before measuring volt/current
|
39 |
| - current_time = (time.time() - startTime) |
| 45 | + currentTime = (time.time() - startTime) |
40 | 46 | voltage = kel.measureVolt()
|
41 | 47 | current = kel.measureCurrent()
|
42 | 48 | voltageData.append(voltage)
|
43 | 49 | # Only append the timedata when volt/current measurements went fine.
|
44 | 50 | # This is because the voltage or current measurement could fail
|
45 |
| - # and then the x and y-axis would have different dimentions |
46 |
| - timeData.append(current_time) |
| 51 | + # and then the x and y-axis would have different dimensions |
| 52 | + timeData.append(currentTime) |
47 | 53 |
|
48 | 54 | # solve the current stuff as a running accumulation
|
49 |
| - capacity = ((startTime - time.time()) / 60 / 60) * current |
| 55 | + capacity = ((time.time() - startTime) / 3600) * current |
50 | 56 |
|
51 |
| - print("Voltage: " + str(voltage) + " V DC, Capacity: " + str(capacity) + " Ah") |
52 |
| - time.sleep(0.25) |
| 57 | + print(f"Voltage: {voltage:.3f} V DC, Capacity: {capacity:.3f} Ah") |
| 58 | + time.sleep(1.0) |
53 | 59 | missedSuccessiveSamples = 0
|
| 60 | + |
| 61 | + with open(f'{fileBasename}.txt', 'a') as f: |
| 62 | + f.write(f"{currentTime} {voltage}\n") |
54 | 63 | except Exception as e:
|
55 | 64 | print(e)
|
56 | 65 | missedSuccessiveSamples += 1
|
|
65 | 74 | fig, ax = plt.subplots()
|
66 | 75 | ax.plot(timeData, voltageData)
|
67 | 76 |
|
68 |
| -ax.set(xlabel='time (s)', ylabel='voltage (V DC)', |
69 |
| - title='Battery Discharge Test {}A: {:.4f}Ah'.format(dischargeRate, capacity)) |
| 77 | +ax.set(xlabel='time (s)', |
| 78 | + ylabel='voltage (V DC)', |
| 79 | + title=f'Battery Discharge Test {dischargeRate:.3f}A: {capacity:.4f}Ah') |
70 | 80 | ax.grid()
|
71 | 81 |
|
72 |
| -fig.savefig("test_" + str(time.time()) + ".png") |
| 82 | +fig.savefig(f"{fileBasename}.png") |
73 | 83 | plt.show()
|
0 commit comments