-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcurrent_viewer.py
More file actions
512 lines (397 loc) · 20.5 KB
/
current_viewer.py
File metadata and controls
512 lines (397 loc) · 20.5 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python
# Copyright (c) Marius Gheorghescu. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
import sys
import time
import serial
import logging
from logging.handlers import RotatingFileHandler
import argparse
import platform
import collections
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import mplcursors
import matplotlib.animation as animation
from matplotlib.dates import num2date, MinuteLocator, SecondLocator, DateFormatter
from matplotlib.widgets import Button
from datetime import datetime, timedelta
from threading import Thread
from os import path
version = '1.0.7'
port = ''
baud = 115200
logfile = 'current_viewer.log'
refresh_interval = 66 # 66ms = 15fps
# controls the window size (and memory usage). 100k samples = 3 minutes
buffer_max_samples = 100000
# controls how many samples to display in the chart (and CPU usage). Ie 4k display should be ok with 2k samples
chart_max_samples = 2048
# how many samples to average (median)
max_supersampling = 16;
# set to true to compute median instead of average (less noise, more CPU)
median_filter = 0;
#
save_file = None;
save_format = None;
connected_device = "CurrentRanger"
class CRPlot:
def __init__(self, sample_buffer = 100):
self.port = '/dev/ttyACM0'
self.baud = 9600
self.thread = None
self.stream_data = True
self.pause_chart = False
self.sample_count = 0
self.animation_index = 0
self.max_samples = sample_buffer
self.data = collections.deque(maxlen=sample_buffer)
self.timestamps = collections.deque(maxlen=sample_buffer)
self.dataStartTS = None
self.serialConnection = None
self.framerate = 30
def serialStart(self, port, speed = 115200):
self.port = port
self.baud = speed
logging.info("Trying to connect to port='{}' baud='{}'".format(port, speed))
try:
self.serialConnection = serial.Serial(self.port, self.baud, timeout=5)
logging.info("Connected to {} at baud {}".format(port, speed))
except serial.SerialException as e:
logging.error("Error connecting to serial port: {}".format(e))
return False
except:
logging.error("Error connecting to serial port, unexpected exception:{}".format(sys.exc_info()))
return False
if self.thread == None:
self.thread = Thread(target=self.serialStream)
self.thread.start()
print('Initializing data capture:', end='')
wait_timeout = 100
while wait_timeout > 0 and self.sample_count == 0:
print('.', end='', flush=True)
time.sleep(0.01)
wait_timeout -= 1
if (self.sample_count == 0):
logging.error("Error: No data samples received. Aborting")
return False
print("OK\n")
return True
def pauseRefresh(self, state):
logging.debug("pause {}".format(state))
self.pause_chart = not self.pause_chart
if self.pause_chart:
self.ax.set_title('<Paused>', color="yellow")
self.bpause.label.set_text('Resume')
else:
self.ax.set_title(f"Streaming: {connected_device}", color="white")
self.bpause.label.set_text('Pause')
def saveAnimation(self, state):
logging.debug("save {}".format(state))
self.bsave.label.set_text('Saving...')
plt.gcf().canvas.draw()
filename = None
while True:
filename = 'current' + str(self.animation_index) + '.gif'
self.animation_index += 1
if not path.exists(filename):
break
logging.info("Animation saved to '{}'".format(filename))
self.anim.save(filename, writer='imagemagick', fps=self.framerate)
self.bsave.label.set_text('GIF')
def chartSetup(self, refresh_interval=100):
plt.style.use('dark_background')
fig = plt.figure(num=f"Current Viewer {version}", figsize=(10, 6))
self.ax = plt.axes()
ax = self.ax
ax.set_title(f"Streaming: {connected_device}", color="white")
fig.text (0.2, 0.88, f"CurrentViewer {version}", color="yellow", verticalalignment='bottom', horizontalalignment='center', fontsize=9, alpha=0.7)
fig.text (0.89, 0.0, f"github.com/MGX3D/CurrentViewer", color="white", verticalalignment='bottom', horizontalalignment='center', fontsize=9, alpha=0.5)
ax.set_ylabel("Current draw (Amps)")
ax.set_yscale("log", nonpositive='clip')
ax.set_ylim(1e-10, 1e1)
plt.yticks([1.0e-9, 1.0e-8, 1.0e-7, 1.0e-6, 1.0e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0], ['1nA', '10nA', '100nA', '1\u00B5A', '10\u00B5A', '100\u00B5A', '1mA', '10mA', '100mA', '1A'], rotation=0)
ax.grid(axis="y", which="both", color="yellow", alpha=.3, linewidth=.5)
ax.set_xlabel("Time")
plt.xticks(rotation=20)
ax.set_xlim(datetime.now(), datetime.now() + timedelta(seconds=10))
ax.grid(axis="x", color="green", alpha=.4, linewidth=2, linestyle=":")
#ax.xaxis.set_major_locator(SecondLocator())
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
def on_xlims_change(event_ax):
logging.debug("Interactive zoom: {} .. {}".format(num2date(event_ax.get_xlim()[0]), num2date(event_ax.get_xlim()[1])))
chart_len = num2date(event_ax.get_xlim()[1]) - num2date(event_ax.get_xlim()[0])
if chart_len.total_seconds() < 5:
self.ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S.%f'))
else:
self.ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
self.ax.xaxis.set_minor_formatter(DateFormatter('%H:%M:%S.%f'))
ax.callbacks.connect('xlim_changed', on_xlims_change)
lines = ax.plot([], [], label="Current")[0]
lastText = ax.text(0.50, 0.95, '', transform=ax.transAxes)
statusText = ax.text(0.50, 0.50, '', transform=ax.transAxes)
self.anim = animation.FuncAnimation(fig, self.getSerialData, fargs=(lines, plt.legend(), lastText), interval=refresh_interval)
plt.legend(loc="upper right", framealpha=0.5)
apause = plt.axes([0.91, 0.15, 0.08, 0.07])
self.bpause = Button(apause, label='Pause', color='0.2', hovercolor='0.1')
self.bpause.on_clicked(self.pauseRefresh)
self.bpause.label.set_color('yellow')
aanimation = plt.axes([0.91, 0.25, 0.08, 0.07])
self.bsave = Button(aanimation, 'GIF', color='0.2', hovercolor='0.1')
self.bsave.on_clicked(self.saveAnimation)
self.bsave.label.set_color('yellow')
crs = mplcursors.cursor(ax, hover=True)
@crs.connect("add")
def _(sel):
sel.annotation.arrow_patch.set(arrowstyle="simple", fc="yellow", alpha=.4)
sel.annotation.set_text(self.textAmp(sel.target[1]))
self.framerate = 1000/refresh_interval
plt.gcf().autofmt_xdate()
plt.show()
def serialStream(self):
# set data streaming mode on CR (assuming it was off)
self.serialConnection.write(b'u')
self.serialConnection.reset_input_buffer()
self.sample_count = 0
line_count = 0
error_count = 0
self.dataStartTS = datetime.now()
# data timeout threshold (seconds) - bails out of no samples received
data_timeout_ths = 0.5
line = None
device_data = bytearray()
logging.info("Starting USB streaming loop")
while (self.stream_data):
try:
# get the timestamp before the data string, likely to align better with the actual reading
ts = datetime.now()
chunk_len = device_data.find(b"\n")
if chunk_len >= 0:
line = device_data[:chunk_len]
device_data = device_data[chunk_len+1:]
else:
line = None
while line == None and self.stream_data:
chunk_len = max(1, min(4096, self.serialConnection.in_waiting))
chunk = self.serialConnection.read(chunk_len)
chunk_len = chunk.find(b"\n")
if chunk_len >= 0:
line = device_data + chunk[:chunk_len]
device_data[0:] = chunk[chunk_len+1:]
else:
device_data.extend(chunk)
if line == None:
continue
line = line.decode(encoding="ascii", errors="strict")
if (line.startswith("USB_LOGGING")):
if (line.startswith("USB_LOGGING_DISABLED")):
# must have been left open by a different process/instance
logging.info("CR USB Logging was disabled. Re-enabling")
self.serialConnection.write(b'u')
self.serialConnection.flush()
continue
data = float(line)
self.sample_count += 1
line_count += 1
if save_file:
if save_format == 'CSV':
save_file.write(f"{ts},{data}\n")
elif save_format == 'JSON':
save_file.write("{}{{\"time\":\"{}\",\"amps\":\"{}\"}}".format(',\n' if self.sample_count>1 else '', ts, data))
if data < 0.0:
# this happens too often (negative values)
self.timestamps.append(np.datetime64(ts))
self.data.append(1.0e-11)
logging.warning("Unexpected value='{}'".format(line.strip()))
else:
self.timestamps.append(np.datetime64(ts))
self.data.append(data)
logging.debug(f"#{self.sample_count}:{ts}: {data}")
if (self.sample_count % 1000 == 0):
logging.debug("{}: '{}' -> {}".format(ts.strftime("%H:%M:%S.%f"), line.rstrip(), data))
dt = datetime.now() - self.dataStartTS
logging.info("Received {} samples in {:.0f}ms ({:.2f} samples/second)".format(self.sample_count, 1000*dt.total_seconds(), self.sample_count/dt.total_seconds()))
print("Received {} samples in {:.0f}ms ({:.2f} samples/second)".format(self.sample_count, 1000*dt.total_seconds(), self.sample_count/dt.total_seconds()))
except KeyboardInterrupt:
logging.info('Terminated by user')
break
except ValueError:
logging.error("Invalid data format: '{}': {}".format(line, sys.exc_info()))
error_count += 1
last_sample = (np.datetime64(datetime.now()) - (self.timestamps[-1] if self.sample_count else np.datetime64(datetime.now())))/np.timedelta64(1, 's')
if (error_count > 100) and last_sample > data_timeout_ths:
logging.error("Aborting. Error rate is too high {} errors, last valid sample received {} seconds ago".format(error_count, last_sample))
self.stream_data = False
break
pass
except serial.SerialException as e:
logging.error('Serial read error: {}: {}'.format(e.strerror, sys.exc_info()))
self.stream_data = False
break
self.stream_data = False
# stop streaming so the device shuts down if in auto mode
logging.info('Telling CR to stop USB streaming')
try:
# this will throw if the device has failed.disconnected already
self.serialConnection.write(b'u')
except:
logging.warning('Was not able to clean disconnect from the device')
logging.info('Serial streaming terminated')
def textAmp(self, amp):
if (abs(amp) > 1.0):
return "{:.3f} A".format(amp)
if (abs(amp) > 0.001):
return "{:.2f} mA".format(amp*1000)
if (abs(amp) > 0.000001):
return "{:.1f} \u00B5A".format(amp*1000*1000)
return "{:.1f} nA".format(amp*1000*1000*1000)
def getSerialData(self, frame, lines, legend, lastText):
if (self.pause_chart or len(self.data) < 2):
lastText.set_text('')
return
if not self.stream_data:
self.ax.set_title('<Disconnected>', color="red")
lastText.set_text('')
return
dt = datetime.now() - self.dataStartTS
# capped at buffer_max_samples
sample_set_size = len(self.data)
timestamps = []
samples = [] #np.arange(chart_max_samples, dtype="float64")
subsamples = max(1, min(max_supersampling, int(sample_set_size/chart_max_samples)))
# Sub-sampling for longer window views without the redraw perf impact
for i in range(0, chart_max_samples):
sample_index = int(sample_set_size*i/chart_max_samples)
timestamps.append(self.timestamps[sample_index])
supersample = np.array([self.data[i] for i in range(sample_index, sample_index+subsamples)])
samples.append(np.median(supersample) if median_filter else np.average(supersample))
self.ax.set_xlim(timestamps[0], timestamps[-1])
# some machines max out at 100fps, so this should react in 0.5-5 seconds to actual speed
sps_samples = min(512, sample_set_size);
dt_sps = (np.datetime64(datetime.now()) - self.timestamps[-sps_samples])/np.timedelta64(1, 's');
# if more than 1 second since last sample, automatically set SPS to 0 so we don't have until it slowly decays to 0
sps = sps_samples/dt_sps if ((np.datetime64(datetime.now()) - self.timestamps[-1])/np.timedelta64(1, 's')) < 1 else 0.0
lastText.set_text('{:.1f} SPS'.format(sps))
if sps > 500:
lastText.set_color("white")
elif sps > 100:
lastText.set_color("yellow")
else:
lastText.set_color("red")
logging.debug("Drawing chart: range {}@{} .. {}@{}".format(samples[0], timestamps[0], samples[-1], timestamps[-1]))
lines.set_data(timestamps, samples)
self.ax.legend(labels=['Last: {}\nAvg: {}'.format( self.textAmp(samples[-1]), self.textAmp(sum(samples)/len(samples)))])
def isStreaming(self) -> bool:
return self.stream_data
def close(self):
self.stream_data = False
if self.thread != None:
self.thread.join()
if self.serialConnection != None:
self.serialConnection.close()
logging.info("Connection closed.")
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s -p <port> [OPTION]",
description="CurrentRanger R3 Viewer"
)
parser.add_argument("--version", action="version", version = f"{parser.prog} version {version}")
parser.add_argument("-p", "--port", nargs=1, required=True, help="Set the serial port (backed by USB or BlueTooth) to connect to (example: /dev/ttyACM0 or COM3)")
parser.add_argument("-s", "--baud", metavar='<n>', type=int, nargs=1, help=f"Set the serial baud rate (default: {baud})")
parser.add_argument("-o", "--out", metavar='<file>', nargs=1, help=f"Save the output samples to <file> in the format set by --format")
parser.add_argument("--format", metavar='<fmt>', nargs=1, help=f"Set the output format to one of: CSV, JSON")
parser.add_argument("--gui", dest="gui", action="store_true", default=True, help="Display the GUI / Interactive chart (default: ON)")
parser.add_argument("-g", "--no-gui", dest="gui", action="store_false", help="Do not display the GUI / Interactive Chart. Useful for automation")
parser.add_argument("-b", "--buffer", metavar='<samples>', type=int, nargs=1, help=f"Set the chart buffer size (window size) in # of samples (default: {buffer_max_samples})")
parser.add_argument("-m", "--max-chart", metavar='<samples>', type=int, nargs=1, help=f"Set the chart max # samples displayed (default: {chart_max_samples})")
parser.add_argument("-r", "--refresh", metavar='<ms>', type=int, nargs=1, help=f"Set the live chart refresh interval in milliseconds (default: {refresh_interval})")
parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase logging verbosity (can be specified multiple times)")
parser.add_argument("-c", "--console", default=False, action="store_true", help="Show the debug messages on the console")
parser.add_argument("-n", "--no-log", default=False, action="store_true", help=f"Disable debug logging (enabled by default)")
parser.add_argument("--log-size", metavar='<Mb>', type=float, nargs=1, help=f"Set the log maximum size in megabytes (default: 1Mb)")
parser.add_argument("-l", "--log-file", nargs=1, help=f"Set the debug log file name (default:{logfile})")
parser.set_defaults(gui=True)
return parser
def main():
print("CurrentViewer v" + version)
log_size = 1024*1024;
parser = init_argparse()
args = parser.parse_args()
if args.log_file:
global logfile
logfile = args.log_file[0]
if args.log_size:
log_size = 1024*1024*args.log_size[0]
if args.refresh:
global refresh_interval
refresh_interval = args.refresh[0]
if args.baud:
global baud
baud = args.baud[0]
if args.max_chart and args.max_chart[0] > 10:
global chart_max_samples
chart_max_samples = args.max_chart[0]
if args.buffer:
global buffer_max_samples
buffer_max_samples = args.buffer[0]
if buffer_max_samples < chart_max_samples:
print("Command line error: Buffer size cannot be smaller than the chart sample size", file=sys.stderr)
return -1
logging_level = logging.DEBUG if args.verbose>2 else (logging.INFO if args.verbose>1 else (logging.WARNING if args.verbose>0 else logging.ERROR))
# disable matplotlib logging for fonts, seems to be quite noisy
logging.getLogger('matplotlib.font_manager').disabled = True
if args.console or not args.no_log:
logging.getLogger().setLevel(logging.DEBUG)
if not args.no_log:
file_logger = RotatingFileHandler(logfile, maxBytes=log_size, backupCount=1)
file_logger.setLevel(logging.DEBUG)
file_logger.setFormatter(logging.Formatter('%(levelname)s:%(asctime)s:%(threadName)s:%(message)s'))
logging.getLogger().addHandler(file_logger)
if args.console:
print("Setting console logging")
console_logger = logging.StreamHandler()
console_logger.setLevel(logging_level)
console_logger.setFormatter(logging.Formatter('%(levelname)s:%(message)s'))
logging.getLogger().addHandler(console_logger)
global save_file
global save_format
if args.format:
save_format = args.format[0].upper()
if not save_format in ["CSV", "JSON"]:
print(f"Unknown format {save_format}", file=sys.stderr)
return -2
if args.out:
output_file_name = args.out[0]
save_file = open(output_file_name, "w+")
if not save_format:
save_format = 'CSV' if output_file_name.upper().endswith('.CSV') else 'JSON'
logging.info(f"Save format automatically set to {save_format} for {args.out[0]}")
if save_format == 'CSV':
save_file.write("Timestamp, Amps\n")
elif save_format == 'JSON':
save_file.write("{\n\"data\":[\n")
logging.info("CurrentViewer v{}. System: {}, Platform: {}, Machine: {}, Python: {}".format(version, platform.system(), platform.platform(), platform.machine(), platform.python_version()))
csp = CRPlot(sample_buffer=buffer_max_samples)
if csp.serialStart(port=args.port[0], speed=baud):
if args.gui:
print("Starting live chart...")
csp.chartSetup(refresh_interval=refresh_interval)
else:
print("Running with no GUI (press Ctrl-C to stop)...")
try:
while csp.isStreaming():
time.sleep(0.01)
except KeyboardInterrupt:
logging.info('Terminated')
csp.close()
print("Done.")
else:
print("Fatal: Could not connect to USB/BT COM port {}. Check the logs for more information".format(args.port[0]), file=sys.stderr)
csp.close()
if save_file:
if save_format == 'JSON':
save_file.write("\n]\n}\n")
save_file.close()
if __name__ == '__main__':
main()