forked from UnifiedEngineering/T-962-improvements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial-control.py
executable file
·255 lines (197 loc) · 6.56 KB
/
serial-control.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Log the temperatures reported by the oven in a live plot and
# in a CSV file.
#
# Requires
# python 2.7
# - pyserial (python-serial in ubuntu, pip install pyserial)
# - matplotlib (python-matplotlib in ubuntu, pip install matplotlib)
#
import csv
import datetime
import serial
import sys
from time import time
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
# settings
#
FIELD_NAMES = 'Time,Temp0,Temp1,Temp2,Temp3,Set,Actual,Heat,Fan,ColdJ,Mode'
TTYs = ('COM1', 'COM2', 'COM3', 'COM4', 'COM5',
'/dev/ttyUSB0', '/dev/ttyUSB1', '/dev/ttyUSB2',
'/dev/tty.usbserial',
'/dev/tty.PL2303-00002014', '/dev/tty.PL2303-00001014')
BAUD_RATE = 115200
logdir = 'logs/'
DEBUG = open('debug_output', 'w+')
MAX_X = 470
MAX_Y_temperature = 300
MAX_Y_pwm = 260
#
# end of settings
def timestamp(dt=None):
if dt is None:
dt = datetime.datetime.now()
return dt.strftime('%Y-%m-%d-%H%M%S')
def logname(filetype, profile, timestamp):
return '%s%s-%s.%s' % (
logdir,
timestamp,
profile.replace(' ', '_').replace('/', '_'),
filetype
)
def get_tty():
for devname in TTYs:
try:
port = serial.Serial(devname, baudrate=BAUD_RATE)
print 'Using serial port %s' % port.name
return port
except:
pass
return None
class Line(object):
def __init__(self, axis, key, label=None):
self.xvalues = []
self.yvalues = []
self._key = key
self._line, = axis.plot(self.xvalues, self.yvalues, label=label or key)
def add(self, log):
self.xvalues.append(log['Time'])
self.yvalues.append(log[self._key])
self.update()
def update(self):
self._line.set_data(self.xvalues, self.yvalues)
def clear(self):
self.xvalues = []
self.yvalues = []
self.update()
class Log(object):
profile = ''
def __init__(self):
self.init_plot()
self.clear_logs()
def clear_logs(self):
self.raw_log = []
map(Line.clear, self.lines)
self.mode = ''
def init_plot(self):
plt.ion()
gs = gridspec.GridSpec(2, 1, height_ratios=(4, 1))
fig = plt.figure(figsize=(7, 5))
axis_upper = fig.add_subplot(gs[0])
axis_lower = fig.add_subplot(gs[1], sharex=axis_upper)
plt.subplots_adjust(hspace=0.05, top=0.95, bottom=0.10, left=0.10, right=0.95)
# setup axis for upper graph (temperature values)
axis_upper.set_ylabel(u'Temperature [°C]')
axis_upper.set_xlim(-5, MAX_X)
axis_upper.set_ylim(-5, MAX_Y_temperature)
plt.setp( axis_upper.get_xticklabels(), visible=False)
# setup axis for lower graph (PWM values)
axis_lower.set_xlim(-5, MAX_X)
axis_lower.set_ylim(-5, MAX_Y_pwm)
axis_lower.set_ylabel('PWM value')
axis_lower.set_xlabel('Time [s]')
# select values to be plotted
self.lines = [
Line(axis_upper, 'Actual'),
Line(axis_upper, 'Temp0'),
Line(axis_upper, 'Temp1'),
Line(axis_upper, 'Set', u'Setpoint'),
Line(axis_upper, 'ColdJ', u'Coldjunction'),
# Line(axis_upper, 'Temp2'),
# Line(axis_upper, 'Temp3'),
Line(axis_lower, 'Fan'),
Line(axis_lower, 'Heat', 'Heater')
]
axis_upper.legend()
axis_lower.legend()
plt.draw()
plt.pause(0.1)
self.axis_upper = axis_upper
self.axis_lower = axis_lower
def save_logfiles(self):
if len(self.raw_log) < 100:
print 'Not saving log (too short)'
return
now = timestamp()
print 'Saved log in %s ' % logname('csv', self.profile, now)
plt.savefig(logname('png', self.profile, now))
plt.savefig(logname('pdf', self.profile, now))
with open(logname('csv', self.profile, now), 'w+') as csvout:
writer = csv.DictWriter(csvout, FIELD_NAMES.split(','))
writer.writeheader()
for l in self.raw_log:
writer.writerow(l)
def parse(self, line):
values = map(str.strip, line.split(','))
# Convert all values to float, except the mode
values = map(float, values[0:-1]) + [values[-1], ]
fields = FIELD_NAMES.split(',')
if len(values) != len(fields):
raise ValueError('Expected %d fields, found %d' % (len(fields), len(values)))
return dict(zip(fields, values))
def process_log(self, logline):
print >>DEBUG, logline
# ignore 'comments'
if logline.startswith('#'):
return
# parse Profile name
if logline.startswith('Starting reflow with profile: '):
self.profile = logline[30:].strip()
return
if logline.startswith('Selected profile'):
self.profile = logline[20:].strip()
return
try:
log = self.parse(logline)
print >>DEBUG, log
except ValueError, e:
if len(logline) > 0:
print >>DEBUG, '!!', logline
return
if 'Mode' in log:
# clean up log before starting reflow
if self.mode == 'STANDBY' and log['Mode'] in ('BAKE', 'REFLOW'):
self.clear_logs()
# save png graph an csv file when bake or reflow ends.
if self.mode in ('BAKE', 'REFLOW') and log['Mode'] == 'STANDBY':
self.save_logfiles()
self.mode = log['Mode']
if log['Mode'] == 'BAKE':
self.profile = 'bake'
self.axis_upper.set_title('Profile: %s Mode: %s ' % (self.profile, self.mode))
if 'Time' in log and log['Time'] != 0.0:
if 'Actual' not in log:
return
# update all lines
map(lambda x: x.add(log), self.lines)
self.raw_log.append(log)
# update view
plt.draw()
plt.pause(0.001)
def logging_only():
log = Log()
port = get_tty()
if not port:
return
try:
while True:
serial_line = port.readline().strip()
log.process_log(serial_line)
except Exception:
sys.exit(0)
finally:
port.close()
if __name__ == '__main__':
if len(sys.argv) > 1:
try:
port = serial.Serial(sys.argv[1], baudrate=BAUD_RATE)
TTYs = [sys.argv[1]]
except:
pass
print 'Logging reflow sessions...'
logging_only()