summaryrefslogtreecommitdiff
path: root/bin/msp430-etv
blob: de138cfa571b95984e6ed83c0823a675b45a63f5 (plain)
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
#!/usr/bin/env python3

import matplotlib.pyplot as plt
import numpy as np
import os
import subprocess
import sys

if __name__ == '__main__':
	try:
		duration = int(sys.argv[1])
	except IndexError:
		print('Usage: msp430-etv <duration>')
	except ValueError:
		print('Error: duration "{}" is not a number'.format(sys.argv[1]))

	if not 'LD_LIBRARY_PATH' in os.environ:
		os.environ['LD_LIBRARY_PATH'] = '{}/var/projects/msp430/MSP430Flasher_1.3.7'.format(os.environ['HOME'])

	energytrace_cmd = '{}/var/source/energytrace-util/energytrace'.format(os.environ['HOME'])

	res = subprocess.run([energytrace_cmd, str(duration)], stdout = subprocess.PIPE, universal_newlines = True)

	lines = res.stdout.split('\n')
	data_count = sum(map(lambda x: len(x) > 0 and x[0] != '#', lines))
	data_lines = filter(lambda x: len(x) > 0 and x[0] != '#', lines)
	data = np.empty((data_count, 4))

	for i, line in enumerate(data_lines):
		timestamp, current, voltage, total_energy = map(float, line.split(' '))
		data[i] = [timestamp, current, voltage, total_energy]

	m_duration = data[-1, 0] - data[0, 0]
	m_energy = data[-1, 3] - data[0, 3]
	m_calc_energy = np.sum(data[1:, 1] * data[1:, 2] * (data[1:, 0] - data[:-1, 0]))

	print('{:d} measurements in {:.2f} s = {:.0f} Hz sample rate'.format(
		data_count, m_duration, data_count / m_duration))

	print('Reported energy: E = {:f} J'.format(m_energy))
	print('Calculated energy: U*I*t = {:f} J'.format(m_calc_energy))

	pwrhandle, = plt.plot(data[:, 0], data[:, 1] * data[:, 2], 'b-', label='U*I', markersize=1)
	#energyhandle, = plt.plot(data[1:, 0], (data[1:, 3] - data[:-1, 3]) / (data[1:, 0] - data[:-1, 0]), 'r-', label='E/Δt', markersize=1)
	plt.legend(handles=[pwrhandle])
	plt.xlabel('Time [s]')
	plt.ylabel('Power [W]')
	plt.grid(True)
	plt.show()