-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathina169_measurement.py
executable file
·62 lines (48 loc) · 2.25 KB
/
ina169_measurement.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
#!/usr/bin/env python3
import argparse
import board
import busio
import datetime
import json
import psutil
import subprocess
import time
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
parser = argparse.ArgumentParser(description='Measure current with load on INA169.')
parser.add_argument('-d', '--data-rate', metavar='N', dest='data_rate', default=60,
help='number of measurements to record per second')
parser.add_argument('input_file', type=str, nargs=1,
help='input file for commands to run, as JSON')
def init_adc():
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c, data_rate=860, mode=ADS.Mode.SINGLE)
return AnalogIn(ads, ADS.P1)
def main_loop(adc, tests, data_rate, out_file='currents.log'):
log = open(out_file, 'w')
log.write('time,cpu_percent_0,cpu_percent_1,cpu_percent_2,cpu_percent_3,' +
'virtual_memory,adc_voltage\n')
log.flush()
for test in tests:
test_loads = []
for command in test['commands']:
test_loads.append(subprocess.Popen(command.split(' ')))
for i in range(test['run_time'] * data_rate):
log.write('{},{},{},{},{},{},{}\n'.format(str(datetime.datetime.now()),
psutil.cpu_times_percent(interval=None, percpu=True)[0].idle,
psutil.cpu_times_percent(interval=None, percpu=True)[1].idle,
psutil.cpu_times_percent(interval=None, percpu=True)[2].idle,
psutil.cpu_times_percent(interval=None, percpu=True)[3].idle,
psutil.virtual_memory().percent,
adc.voltage))
log.flush()
time.sleep(1 / data_rate)
for test_load in test_loads:
if test_load is not None and test_load.poll() is None:
test_load.kill()
if __name__ == '__main__':
args = parser.parse_args()
with open(args.input_file[0]) as f:
tests = json.load(f)['runs']
adc = init_adc()
main_loop(adc, tests, int(args.data_rate))