summaryrefslogtreecommitdiffstats
path: root/firmware/dumpcurves.py
blob: 954494ee4d8d3331c0d8fc275447311fc4398294 (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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
#
# Xytronic LF-1600
# Debug interface tool
#
# Copyright (c) 2015-2016 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import sys
import serial


if len(sys.argv) != 3 or "-h" in sys.argv or "--help" in sys.argv:
	print("Usage: dumpcurves.py /dev/ttyUSB0 output.csv")
	print("")
	print("/dev/ttyUSB0 is the serial device where the data is read from.")
	print("output.csv is the output file.")
	sys.exit(1)

inDev = sys.argv[1]
outFile = sys.argv[2]

s = serial.Serial(port = inDev, baudrate = 9600, bytesize = 8,
		  parity = serial.PARITY_NONE, stopbits = 2)

outFd = open(outFile, "wb")
outFd.write(b"time (ticks);current real r (A);current used r (A);current y (A);"
	    b"temp r (*C);temp y1 (*C);temp y2 (A);"
	    b"measured current (ADC);filtered current (ADC);"
	    b"measured temp (ADC);"
	    b"boost mode;"
	    b"calib current percent\r\n")

def reset():
	global outLines
	global prevTimeStamp
	global val_currentRealR
	global val_currentUsedR
	global val_currentY
	global val_tempR
	global val_tempY1
	global val_tempY2
	global val_measCurr
	global val_filtCurr
	global val_measTemp
	global val_boostMode;
	global val_calCurrPercent

	print("RESET")
	outLines = []
	prevTimeStamp = None
	val_currentRealR = 0.0
	val_currentUsedR = 0.0
	val_currentY = 0.0
	val_tempR = 0.0
	val_tempY1 = 0.0
	val_tempY2 = 0.0
	val_measCurr = 0
	val_filtCurr = 0
	val_measTemp = 0
	val_boostMode = 0
	val_calCurrPercent = 0

def putLine(timeStamp):
	csvLine = "%d;%f;%f;%f;%f;%f;%f;%d;%d;%d;%d;%d\r\n" % (
		timeStamp,
		val_currentRealR,
		val_currentUsedR,
		val_currentY,
		val_tempR,
		val_tempY1,
		val_tempY2,
		val_measCurr,
		val_filtCurr,
		val_measTemp,
		val_boostMode,
		val_calCurrPercent,
	)
	outLines.append(csvLine)

def parseInt(valStr, valIdent):
	try:
		val = int(valStr, 10)
	except ValueError:
		print("Invalid %s" % valIdent)
		return 0
	return val

def parseFixpt(valStr, valIdent):
	val = parseInt(valStr, valIdent)
	val = float(val) / (1 << 6)
	return val

reset()
try:
	while 1:
		line = s.readline()
		line = line.decode("utf-8", "ignore").strip()
		if not line:
			continue

		if line == "st":
			reset()
			continue

		idx = line.find(':')
		if idx < 0:
			print("Time stamp not found")
			continue
		try:
			timeStamp = int(line[ : idx], 16)
		except ValueError:
			print("Invalid time stamp format")
			continue
		line = line[idx + 1 : ].strip()

		elems = line.split()
		if len(elems) != 2:
			print("Unknown format: %s" % line)
			continue
		if elems[0] == "cr1":
			val_currentRealR = parseFixpt(elems[1], "cr1")
		elif elems[0] == "cr2":
			val_currentUsedR = parseFixpt(elems[1], "cr2")
		elif elems[0] == "cy":
			val_currentY = parseFixpt(elems[1], "cy")
		elif elems[0] == "tr":
			val_tempR = parseFixpt(elems[1], "tr")
		elif elems[0] == "ty1":
			val_tempY1 = parseFixpt(elems[1], "ty1")
		elif elems[0] == "ty2":
			val_tempY2 = parseFixpt(elems[1], "ty2")
		elif elems[0] == "tb":
			val_boostMode = parseInt(elems[1], "tb")
		elif elems[0] == "mc":
			val_measCurr = parseInt(elems[1], "mc")
		elif elems[0] == "fc":
			val_filtCurr = parseInt(elems[1], "fc")
		elif elems[0] == "mt":
			val_measTemp = parseInt(elems[1], "mt")
		elif elems[0] == "cc":
			val_calCurrPercent = parseInt(elems[1], "cc")
		else:
			print("Unknown elem: %s" % elems[0])
			continue

		# Fill the missing lines with copies of the previous.
		if 0:
			if prevTimeStamp is not None:
				t = (prevTimeStamp + 1) & 0xFFFF
				while t != timeStamp:
					putLine(t)
					t = (t + 1) & 0xFFFF
			prevTimeStamp = timeStamp
		# Put the current line into the CSV.
		putLine(timeStamp)

		print("temp-r: %.02f   current-y: %.02f   current-r: %.02f" % (
			val_tempR,
			val_currentY,
			val_currentUsedR,
		))
except KeyboardInterrupt as e:
	pass

outFd.write("".join(outLines).encode("utf-8"))
outFd.close()

sys.exit(0)
bues.ch cgit interface