-
Notifications
You must be signed in to change notification settings - Fork 6
/
calibrate_voltage.py
executable file
·65 lines (59 loc) · 2.25 KB
/
calibrate_voltage.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
#!/usr/bin/python3
"""
This script is to perform measures on the obtained value of "read_voltage()" after a certain
time, according to 'period' and 'width' parameters.
"""
import numpy
from silicontoaster import SiliconToaster
import datetime
from time import sleep
import quicklog
toaster = SiliconToaster("/dev/ttyUSB3")
toaster.on_off(True)
avg_count = 100
log = quicklog.Log(f"calibration_voltage.log")
already_done = []
for record in quicklog.read_log(f"calibration_voltage.log"):
already_done.append((float(record["width"]), float(record["period"])))
for period in list(range(100, 1600, 50))[::-1]:
for width in range(1, 25):
print(f"Measuring for period {period}, width {width}...")
if (width, period) in already_done:
print(f"Already done, skipping.")
continue
toaster.set_pwm_settings(period=period, width=width)
# The array containing the read values.
voltages = []
# For tracking time.
last_print_time = start = datetime.datetime.now()
# To compute the standard deviation
stddev = 0.0
# We prevent the measurement to last indefinitely
while (now := datetime.datetime.now()) - start < datetime.timedelta(minutes=2):
# Read the voltage and append it
voltages.append(toaster.read_voltage())
# We keep only the last values
voltages = voltages[-avg_count:]
if len(voltages) == avg_count:
stddev = numpy.std(voltages)
if stddev < 1.5:
# The standard deviation of the last measurements is low:
# We consider that the reading of the voltage is stable enough
break
if last_print_time - now > datetime.timedelta(seconds=20):
print(stddev)
last_print_time = now
# Each measurement is delayed
sleep(0.05)
record = {
"width": width,
"voltage": numpy.average(voltages),
"period": period,
"std": stddev,
"time": int((now - start).total_seconds()),
}
print(record)
log.append(record)
log.flush()
if numpy.average(voltages) > 950:
break