-
Notifications
You must be signed in to change notification settings - Fork 1
/
cert3d.py
1520 lines (1404 loc) · 56 KB
/
cert3d.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Cert3D.py is a GUI for showing and interpreting results.
"""
import statistics
import time
from threading import Thread
import random
import struct
import os
import dpi
from dpi import asize
import serial
import serial.tools.list_ports
import wx
import wx.richtext
from AnalysisWindowBase import AnalysisWindowBase
from Data import *
from ScopePanel import *
vid = 0x0483
pid = 0x5740
# signals as processed from file
signal_names = [
"X_STEP",
"X_DIR",
"Y_STEP",
"Y_DIR",
"Z_STEP",
"Z_DIR",
"E_STEP",
"E_DIR",
]
verbose = True
# text added to a test when it's modified
modified_suffix = " (modified)"
# exit_children = False
# flag to clear the log file
# clear_log_file = False
# flag to log to file
# log_to_file = True
# flag for open log file (not thread safe but probably fine)
# log_file_open = False
# thread for monitoring the port
# port_thread = None
# c3d_port = None
# data rate in Mbps
# c3d_data_rate = 0.0
# bytes received since last data rate check
# c3d_data_rate_bytes = 0
# time of last data rate check
# c3d_data_rate_time = 0
# frequency of data rate calculations
# c3d_data_rate_frequency = 4.0
# main window
# c3d_gui_window = None
# default file name
c3d_log_filename = "c3d_data.bin"
class PrinterGeometry:
"""A PrinterGeometry object holds information about printer geometry."""
def __init__(self, output=""):
# steps per mm
self.steps_per_mm = {c: 1 for c in "XYZE"}
# steps per mm
self.steps_per_mm = {c: 1 for c in "XYZE"}
def parse_output(self, output):
pass
def human_file_size(byte_count):
"""Return a human-readable string for the given number of bytes."""
if byte_count < 1000:
return "%d bytes" % byte_count
elif byte_count < 995000:
return "%.3g kB" % (byte_count / 1e3)
elif byte_count < 995000000:
return "%.3g MB" % (byte_count / 1e6)
else:
return "%.2g GB" % (byte_count / 1e9)
def derivate_data_triangle_pulses(
data: PlotData,
idle_corrections=False,
max_pulse_duration=0.001,
min_pulse_duration=0.001,
):
"""Return the derivative of the given data."""
"""
points = [(data.points[0][0], 0)]
for p1, p2 in zip(data.points[:-1], data.points[1:]):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
if dx == 0:
dx = 1
dy = 0
points.append((p2[0], dy / dx))
"""
# find defintion for each pulse in the form of
# (peak_time, half_duration)
# not the peak amplitude is equal to 1 / half_duration by definition
# for example (5, 2) is a triangular pulse with the points
# (5 - 2, 0), (5, 1 / 2), (6 + 2, 0)
pulses = []
points = data.points
# add another point at the end thg
points.append(points[-1])
for p1, p2, p3 in zip(points[:-2], points[1:-1], points[2:]):
if p2[1] == p1[1]:
continue
half_duration = max_pulse_duration / data.seconds_per_tick
pulses.append((p2[0], (p2[1] - p1[1]) / half_duration, half_duration))
# create data points
x_points = set()
for (center_time, peak, half_duration) in pulses:
x_points.add(center_time - half_duration)
x_points.add(center_time)
x_points.add(center_time + half_duration)
# add first and last point
x_points.add(points[0][0])
x_points.add(points[-1][0])
# sort points and keep without bounds
x_points = sorted(x_points)
x_points = [
x for x in x_points if x >= points[0][0] and x <= points[-1][0]
]
# sort pulses by end time
pulses = sorted(pulses, key=lambda p: p[0] + p[2])
# now create velocity for each point
last_index = 0
first_index = 0
new_points = []
for x in x_points:
# increment first index if necessary
while (
first_index < len(pulses)
and pulses[first_index][0] + pulses[first_index][2] <= x
):
first_index += 1
# increment end index if necessary
while (
last_index < len(pulses)
and pulses[last_index][0] - pulses[last_index][2] < x
):
last_index += 1
# now add up pulses to get this time
y = 0.0
for index in range(first_index, last_index):
center_time, peak, half_duration = pulses[index]
alpha = abs(x - center_time) / half_duration
assert alpha <= 1.0
y += peak * (1.0 - alpha)
new_points.append((x, y))
new_data = PlotData()
new_data.start_time = data.start_time
new_data.seconds_per_tick = data.seconds_per_tick
new_data.points = new_points
return new_data
def derivate_data_squared_signal(data: PlotData):
"""Return the exact derivative of the given data."""
points = []
for p1, p2 in zip(data.points[:-1], data.points[1:]):
dx = (p2[0] - p1[0]) * data.seconds_per_tick
dy = p2[1] - p1[1]
if dx == 0:
dx = 1
dy = 0
points.append((p1[0], dy / dx))
points.append((p2[0], dy / dx))
new_data = PlotData()
new_data.start_time = data.start_time
new_data.seconds_per_tick = data.seconds_per_tick
new_data.points = points
return new_data
def derivate_data(data: PlotData, idle_corrections=False):
"""Return the derivative of the given data."""
points = [(data.points[0][0], 0)]
for p1, p2 in zip(data.points[:-1], data.points[1:]):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
if dx == 0:
dx = 1
dy = 0
points.append((p2[0], dy / dx))
# Note: the data above is taken by averaging over the distance between
# active edges. When this is a large value, such as when the stepper is at
# rest and begins to move, it can produce misleading data. To avoid this,
# we reduce the duration in cases where the stepper motion is very slow.
if idle_corrections:
durations = [y[0] - x[0] for x, y in zip(points[:-1], points[1:])]
durations.sort()
cutoff = durations[len(durations) // 2] * 5
# cutoff = max(x for x in durations if x <= cutoff)
i = 0
while i < len(points) - 1:
i += 1
duration = points[i][0] - points[i - 1][0]
if duration <= 2 * cutoff:
continue
if points[i][1] == points[i - 1][1]:
continue
# print(i, duration, cutoff, duration / cutoff)
# add ramp down
if points[i - 1][1] == 0:
points.insert(i, (points[i][0] - cutoff, 0.0))
i += 1
# add ramp up
if points[i][1] == 0:
points.insert(i, (points[i - 1][0] + cutoff, 0.0))
i += 1
new_data = PlotData()
new_data.start_time = data.start_time
new_data.seconds_per_tick = data.seconds_per_tick
new_data.points = points
return new_data
def decode_stepper(
step_data: BilevelData,
dir_data: BilevelData,
correct_idle_time=False,
idle_time=0.020,
):
"""Given the STEP and DIR channels, return step position as a PlotData."""
# ensure the channels match in start and duration
assert step_data.start_time == dir_data.start_time
assert step_data.get_length() == dir_data.get_length()
assert step_data.seconds_per_tick == dir_data.seconds_per_tick
# hold step vs position
points = []
points.append((0, 0))
steps = 0
# loop through until we reach the end of either stream
try:
# read first dir tick
dir_it = iter(dir_data.edges)
dir_tick = next(dir_it)
# value of the DIR channel before dir_tick
dir_is_low = dir_data.start_high
# value of STEP channel
step_is_low = not step_data.start_high
for step_tick in step_data.edges[1:-1]:
step_is_low = not step_is_low
# advance dir until we're past the step tick
while dir_tick < step_tick:
dir_tick = next(dir_it)
dir_is_low = not dir_is_low
# if STEP transitioned from low to high, increase or decrease pos
if not step_is_low:
if dir_is_low:
steps -= 1
else:
steps += 1
points.append((step_tick, steps))
except StopIteration:
pass
# add last value
points.append((step_data.edges[-1], steps))
if correct_idle_time:
# Note: the data above is taken by averaging over the distance between
# active edges. When this is a large value, such as when the stepper
# is at rest and begins to move, it can produce misleading data. To
# avoid this, we reduce the duration in cases where the stepper motion
# is very slow.
durations = [y[0] - x[0] for x, y in zip(points[:-1], points[1:])]
durations.sort()
cutoff = idle_time / step_data.seconds_per_tick
# cutoff = min(x for x in durations if x <= cutoff)
i = 0
while i < len(points) - 1:
i += 1
duration = points[i][0] - points[i - 1][0]
if duration <= cutoff:
continue
points.insert(i, (points[i][0] - cutoff, points[i - 1][1]))
i += 1
# create PlotData object
pos = PlotData()
pos.seconds_per_tick = step_data.seconds_per_tick
pos.points = points
return pos
def find_c3d_ports():
"""Find and return possible COM ports attached to a Cert3D board."""
c3d_ports = []
description = "USB Serial Device (COM"
for port in serial.tools.list_ports.comports():
if port.description.startswith(
description
) and port.serial_number.startswith("C3D"):
c3d_ports.append(port)
return c3d_ports
all_colors = [wx.RED, wx.GREEN, wx.YELLOW, wx.Colour(255, 0, 255), wx.CYAN]
class AnalysisWindow(AnalysisWindowBase):
def __init__(self, parent):
super(AnalysisWindow, self).__init__(parent)
# set icon
self.SetIcon(wx.Icon("c3d_icon.ico"))
# ratio of the entire display to take up for the initial window
ratio = 0.6
# reduce window size to maintain 16:9 ratio
rect = wx.Display().GetClientArea()
scaler = min(x / y * ratio for x, y in zip(rect[2:], [16, 9]))
window_size = [round(16 * scaler), round(9 * scaler)]
self.SetSize(window_size)
self.Centre()
# create slave thread to monitor c3d port
self.c3d_port_thread = C3DPortMonitor()
# adjust signal name width
self.scope_panel.adjust_channel_name_size()
# zoom to all
self.scope_panel.zoom_to_all()
# set scaling of status bar
self.status_bar.SetStatusWidths([-1, asize(180), asize(180)])
# printer port monitor
self.printer_port_thread = PrinterPortMonitor(
self.rich_text_serial_log,
self.gauge_test_progress,
self.c3d_port_thread,
)
# set font for text control
self.rich_text_serial_log.SetFont(self.text_ctrl_test_code.GetFont())
# hold list of tests
self.tests = {}
# load tests
self.load_tests()
# update stuff in the combo box
self.update_test_combo_box()
# if test is selected, update it
self.event_combo_selection_made(None)
def event_close(self, event):
print("Handling EVT_CLOSE event")
# hide window immediately
print("Hiding window")
self.Hide()
# save tests
self.save_tests()
# signal child thread to exit
# join child thread
#print("Joining C3D thread")
#self.c3d_port_thread.exit_and_join()
#print("Joining printer port thread")
#self.printer_port_thread.exit_and_join()
print("Closing window")
# destory this window
self.Destroy()
# process this event
event.Skip()
def event_menu_file_exit(self, _event):
self.Close()
def event_menu_file_open(self, _event):
with wx.FileDialog(
self,
"Open data file",
defaultDir=os.path.curdir,
wildcard="BIN files (*.bin)|*.bin|All files (*.*)|*.*",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST,
) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return
# Proceed loading the file chosen by the user
self.interpret_data_file(fileDialog.GetPath())
def event_button_start_stream_click(self, _event):
self.c3d_port_thread.log_to_file = True
self.c3d_port_thread.discard_data = False
self.c3d_port_thread.open_port_automatically = True
self.c3d_port_thread.send_command(b"start")
def event_button_stop_stream_click(self, _event):
self.c3d_port_thread.log_to_file = False
self.c3d_port_thread.discard_data = True
self.c3d_port_thread.open_port_automatically = True
self.c3d_port_thread.send_command(b"stop")
def event_button_zoom_all_click(self, event):
self.scope_panel.zoom_to_all()
def event_button_debug_click(self, event):
self.c3d_port_thread.send_command(b"debug")
def event_button_reset_click(self, event):
self.c3d_port_thread.send_command(b"reset")
def event_button_status_click(self, event):
self.c3d_port_thread.send_command(b"status")
def event_button_clear_log_click(self, event):
global clear_log_file
clear_log_file = True
def interpret_data_file(self, filename):
data = interpret_data(filename)
if data is None:
print("ERROR: no data in file")
return
# print([x.get_length() for x in data])
# replace signal data with data from file
self.scope_panel.clear()
for index, this_data in enumerate(data):
if index < len(signal_names):
name = signal_names[index]
else:
name = this_data.name
channel = ScopeChannel(
signal=Signal(name=name, data=this_data)
)
self.scope_panel.add_channel(channel)
# create step channels if possible
postprocess_signals(self.scope_panel)
# find simplified representations
for channel in self.scope_panel.channels:
for signal in channel.signals:
signal.create_simplified_data_sets()
self.scope_panel.zoom_to_all()
self.scope_panel.Refresh()
def event_button_debug_2_click(self, event):
self.interpret_data_file("c3d_data - Copy.bin")
def event_button_interpret_click(self, _event):
# stop streaming and close file
self.event_button_stop_stream_click(_event)
# wait until file is closed
while self.c3d_port_thread.log_file:
time.sleep(0.010)
self.interpret_data_file(self.c3d_port_thread.log_filename)
# trim it
self.event_button_trim_click(_event)
self.notebook.SetSelection(0)
def event_timer_update_ui(self, _event):
# noinspection PyUnusedLocal
c3d_status = "C3D disconnected"
try:
port = self.c3d_port_thread.serial_port.port
rate = "%.3f Mbps" % self.c3d_port_thread.get_data_rate_mbps()
c3d_status = "C3D on %s (%s)" % (port, rate)
except AttributeError:
# this is triggered when the serial port is closed
pass
# update status bar
self.status_bar.SetStatusText(c3d_status, 2)
# if port_name != self.static_text_c3d_board_status.GetLabel():
# self.static_text_c3d_board_status.SetLabel(port_name)
# if data_rate != self.static_text_c3d_board_data_rate.GetLabel():
# self.static_text_c3d_board_data_rate.SetLabel(data_rate)
# if data_size != self.static_text_c3d_board_data_size.GetLabel():
# self.static_text_c3d_board_data_size.SetLabel(data_size)
# update Printer port
# noinspection PyUnusedLocal
printer_status = "Printer disconnected"
try:
port = self.printer_port_thread.serial_port.port
printer_status = "Printer on %s" % port
except AttributeError:
# this is triggered when the serial port is closed
pass
self.status_bar.SetStatusText(printer_status, 1)
# if port_name != self.static_text_printer_board_connection.GetLabel():
# self.static_text_printer_board_connection.SetLabel(port_name)
# if we just finished a test, open it
if (
self.printer_port_thread.test_just_finished
and not self.c3d_port_thread.log_file
):
self.printer_port_thread.test_just_finished = False
if self.checkbox_view_when_complete.IsChecked():
self.event_button_view_results_click(None)
def event_button_trim_click(self, _event):
self.scope_panel.trim_signals()
self.scope_panel.zoom_to_all()
self.Refresh()
def event_button_open_test_window_click(self, event):
return
# if not self.test_window:
# self.test_window = TestWindow(self, self.c3d_port_thread)
# self.test_window.Show()
# self.test_window.SetFocus()
def save_tests(self):
"""Save tests to disk."""
filename = os.path.join(os.environ["LOCALAPPDATA"], "cert3d")
os.makedirs(filename, exist_ok=True)
old_filename = os.path.join(filename, "tests_old.py")
filename = os.path.join(filename, "tests.py")
# rename file if it exists
if os.path.isfile(filename):
if os.path.isfile(old_filename):
os.remove(old_filename)
os.rename(filename, old_filename)
with open(filename, "w") as f:
for name in sorted(self.tests.keys()):
f.write(
"tests['%s'] = '%s'\n"
% (name, self.tests[name].replace("\n", "\\n"))
)
print("Saved %d tests to %s." % (len(self.tests), filename))
def load_tests(self):
"""Load tests from disk and populate the combo."""
# erase tests
self.tests = {}
filename = os.path.join(os.environ["LOCALAPPDATA"], "cert3d")
os.makedirs(filename, exist_ok=True)
filename = os.path.join(filename, "tests.py")
if not os.path.isfile(filename):
print("Could not find test definition file.")
return
# populate tests
test_lines = open(filename, "r").readlines()
try:
for line in test_lines:
if not line:
continue
exec(line, {"tests": self.tests})
except (SyntaxError, NameError):
print("ERROR: could not read tests")
self.tests = {}
print("We found %d tests." % len(self.tests))
def update_test_combo_box(self):
"""Update combobox with current tests."""
# alias to get shorter name
combo = self.combo_box_test_choice
# clear all entries
combo.Dismiss()
combo.Clear()
# add entries
combo.Append(sorted(self.tests.keys()))
# if nothing is selected, select the first test
if combo.GetCount():
combo.SetSelection(0)
def event_button_create_update_click(self, event):
name = self.combo_box_test_choice.GetValue()
# if it's modified, erase the *
if name.endswith(modified_suffix):
name = name[: -len(modified_suffix)]
self.combo_box_test_choice.SetValue(name)
# name cannot be empty
if not name:
return
creating = name not in self.tests
if name in self.tests:
print("Overwriting test %s" % name)
else:
print("Creating new test %s" % name)
self.tests[name] = self.text_ctrl_test_code.GetValue()
if creating:
self.update_test_combo_box()
combo = self.combo_box_test_choice
combo.SetSelection(combo.GetItems().index(name))
# save changes
self.save_tests()
def event_button_delete_click(self, event):
name = self.combo_box_test_choice.GetValue()
if name.endswith(modified_suffix):
name = name[: -len(modified_suffix)]
if name in self.tests:
print("Deleting test %s" % name)
matching_test = (
self.text_ctrl_test_code.GetValue() == self.tests[name]
)
del self.tests[name]
self.update_test_combo_box()
if matching_test:
self.event_combo_selection_made(event)
def event_combo_on_text_enter(self, event):
self.event_button_create_update_click(event)
def event_combo_selection_made(self, event):
index = self.combo_box_test_choice.GetSelection()
if index == -1:
self.text_ctrl_test_code.SetValue("")
return
name = self.combo_box_test_choice.GetItems()[index]
assert name in self.tests
self.text_ctrl_test_code.SetValue(self.tests[name])
self.text_ctrl_test_code.SetInsertionPointEnd()
print("Test %s selected" % name)
def event_static_text_gcode_reference_click(self, _event):
wx.BeginBusyCursor()
import webbrowser
webbrowser.open(self.static_text_gcode_reference.GetLabel())
wx.EndBusyCursor()
def event_static_text_reprap_reference_click(self, _event):
wx.BeginBusyCursor()
import webbrowser
webbrowser.open(self.static_text_reprap_link.GetLabel())
wx.EndBusyCursor()
def event_button_run_test_click(self, event):
self.run_test()
def event_button_send_click(self, _event):
if not self.text_ctrl_message.GetValue():
return
if not self.printer_port_thread.serial_port:
self.rich_text_serial_log.SetDefaultStyle(
wx.TextAttr(wx.Colour(wx.RED))
)
self.rich_text_serial_log.AppendText("Printer not connected\n")
return
command = self.text_ctrl_message.GetValue() + "\n"
self.printer_port_thread.send_command(command)
self.text_ctrl_message.Clear()
def event_text_ctrl_message_enter(self, event):
self.event_button_send_click(event)
def run_test(self):
self.printer_port_thread.run_test(self.text_ctrl_test_code.GetValue())
def event_text_ctrl_test_code_char(self, event):
# Ctrl+A pressed
if event.GetUnicodeKey() == 1:
self.text_ctrl_test_code.SetSelection(0, -1)
return
event.Skip()
def event_text_ctrl_test_code_on_text(self, event):
value = self.combo_box_test_choice.GetValue()
if value in self.tests:
if self.text_ctrl_test_code.GetValue() != self.tests[value]:
self.combo_box_test_choice.SetValue(value + modified_suffix)
def event_button_view_results_click(self, _event):
viewer = self
# get steps per mm for each channel
steps_per_mm = self.rich_text_serial_log.GetValue()
steps_per_mm = steps_per_mm.split("\n")
# get lines with M92
steps_per_mm = [x for x in steps_per_mm if "M92" in x]
# get text after M92
steps_per_mm = steps_per_mm[-1]
steps_per_mm = steps_per_mm[steps_per_mm.index("M92") + 3 :]
# remove comments
steps_per_mm = steps_per_mm[: (steps_per_mm + ";").index(";")]
steps_per_mm = [x for x in steps_per_mm.split(" ") if x]
steps_per_mm = {
x[0]: float(x[1:]) for x in steps_per_mm if x[0] in "XYZE"
}
viewer.event_button_interpret_click(None)
viewer.event_button_trim_click(None)
# scale POS/VEL/ACC channels by steps/mm
for channel in viewer.scope_panel.channels:
for signal in channel.signals:
data = signal.get_master_data()
if not isinstance(data, PlotData):
continue
if signal.name[0] in steps_per_mm:
scale = 1.0 / steps_per_mm[signal.name[0]]
data.points = [(x, y * scale) for x, y in data.points]
signal.create_simplified_data_sets()
viewer.event_button_zoom_all_click(None)
# set active tab to viewer
self.notebook.SetSelection(0)
# viewer.SetFocus()
class InfoHeader:
"""An InfoHeader contains information about sets of Packets."""
def __init__(self, file):
# True if the information is complete
self.valid = False
# frequency of the system processor
self.system_clock = None
# number of signal channels
self.signal_count = None
# frequency for each signal channel
self.signal_frequencies = []
# timer period in ticks for each signal channel
self.signal_overflow_ticks = []
# system ticks per ADC reading (1 sample on all channels)
self.ticks_per_adc_reading = None
# number of ADC channels
self.adc_count = None
# low/high value for ADC channels
self.adc_ranges = None
# try to parse from reading file
self.read_from_file(file)
def read_from_file(self, file):
"""Populate information from the given file object."""
try:
print("Reading header")
# read info block
# char[9] start_string
start_string = file.read(9).decode("utf-8", "ignore")
print("- start_string: %s" % start_string)
assert start_string == "InfoStart"
# uint8_t header_packet_version
header_packet_version = struct.unpack("B", file.read(1))[0]
print("- header_packet_version: %d" % header_packet_version)
assert header_packet_version == 1
# uint8_t streaming_packet_version
streaming_packet_version = struct.unpack("B", file.read(1))[0]
print("- streaming_packet_version: %d" % streaming_packet_version)
assert streaming_packet_version == 1
# uint32_t system_clock
self.system_clock = struct.unpack("L", file.read(4))[0]
print("- Clock speed: %d" % self.system_clock)
# uint8_t signal_channel_count
self.signal_count = struct.unpack("B", file.read(1))[0]
print("- Signal count: %d" % self.signal_count)
# uint8_t adc_channel_count
self.adc_count = struct.unpack("B", file.read(1))[0]
print("- ADC count: %d" % self.adc_count)
# uint32_t ticks_per_adc_reading
self.ticks_per_adc_reading = struct.unpack("L", file.read(4))[0]
print("- Ticks per ADC reading: %d" % self.ticks_per_adc_reading)
self.signal_frequencies = []
self.signal_overflow_ticks = []
for i in range(self.signal_count):
# uint32_t signal_clock
clock = struct.unpack("L", file.read(4))[0]
# uint32_t update_ticks
overflow = struct.unpack("L", file.read(4))[0]
self.signal_frequencies.append(clock)
self.signal_overflow_ticks.append(overflow)
print(
" - Signal channel %d: clock=%d Hz, overflow=%d ticks"
% (i + 1, clock, overflow)
)
# float zero_value
# float high_value
self.adc_ranges = [
(
struct.unpack("f", file.read(4))[0],
struct.unpack("f", file.read(4))[0],
)
for _ in range(self.adc_count)
]
# char[8] end_string
stop = file.read(8).decode("utf-8", "ignore")
print("- end_string: %s" % start_string)
assert stop == "InfoStop"
print(" - Success!")
self.valid = True
except AssertionError:
print("ERROR: file is empty or has invalid header")
self.valid = False
def is_valid(self):
"""Return True if the info is valid."""
return self.valid
class Packet:
"""A Packet is an output from a single process."""
def __init__(self, file):
# uint8_t packet_number
self.packet_number = struct.unpack("B", file.read(1))[0]
# uint8_t channel_mask
channel_mask = struct.unpack("B", file.read(1))[0]
channel_edges = []
for index in range(8):
if channel_mask & (1 << index):
# uint8_t edge_count
count = struct.unpack("B", file.read(1))[0]
# uint16_t timer_edge
edges = struct.unpack("%dH" % count, file.read(2 * count))
channel_edges.append(edges)
else:
channel_edges.append(tuple())
self.channel_edges = channel_edges
# adc_sample_count
adc_count = struct.unpack("B", file.read(1))[0]
adc_values = []
for _ in range(adc_count):
# uint16_t adc_value
adc_values.append(struct.unpack("%dH" % 14, file.read(2 * 14)))
self.adc_values = adc_values
def packets_to_signals(packets, header: InfoHeader):
"""Process packets and return signals."""
# get system ticks for each timer overflow
ticks_per_overflow = [
1.0 * header.signal_frequencies[i] / header.signal_overflow_ticks[i]
for i in range(header.signal_count)
]
assert len(set(ticks_per_overflow)) == 1
# get system ticks per packet
system_ticks_per_packet = (
header.system_clock
* header.signal_overflow_ticks[0]
// header.signal_frequencies[0]
)
assert system_ticks_per_packet == int(system_ticks_per_packet)
print("system_ticks_per_packet =", system_ticks_per_packet)
system_ticks_per_packet = int(system_ticks_per_packet)
print("- Converting readings to per-signal lists")
# add first data point
edges = [[0] for _ in range(header.signal_count)]
# convert edges for each channel to another format
# signal_ticks[0] = [(cycle1, value1), (cycle2, value2), etc...]
signal_ticks = [[] for _ in range(header.signal_count)]
for packet_index, packet in enumerate(packets):
# process each channel
for channel_index, cycle in enumerate(packet.channel_edges):
if cycle:
signal_ticks[channel_index].extend(
[(packet_index, delta) for delta in cycle]
)
if cycle != tuple(sorted(cycle)):
print("BAD DATA on channel %d, packet %d: %s"
% (channel_index, packet_index, cycle))
# now process each channel
print("- Processing channels")
for channel_index, ticks in enumerate(signal_ticks):
# store range of deltas
# delta_values = []
# get timer ticks per packet
ticks_per_packet = (
system_ticks_per_packet
* header.signal_frequencies[channel_index]
// header.system_clock
)
# expected_offset = -ticks_per_packet // 8 - ticks_per_packet
# # get overflow
# overflow = ticks_per_packet
# assert overflow == header.signal_overflow_ticks[channel_index]
edges[channel_index].extend(x * ticks_per_packet + y
for x, y in signal_ticks[channel_index])
# for (cycle, delta) in signal_ticks[channel_index]:
# expected = cycle * ticks_per_packet + expected_offset
# delta_from_expected = (delta - expected) % overflow
# delta_values.append(delta_from_expected)
# this_tick = expected + delta_from_expected
# edges[channel_index].append(this_tick)
# if delta_values:
# print(
# "expected=%d, overflow=%d, delta min=%d, max=%d"
# % (
# expected_offset,
# overflow,
# min(delta_values) + expected_offset,
# max(delta_values) + expected_offset,
# )
# )
# adjust edges so no edge is negative:
# if False:
# for i in range(1, len(edges[channel_index])):
# while edges[channel_index][i] < edges[channel_index][i - 1]:
# print("WARNING: adjusted edge to make it non-negative")
# print(" possibly something is out of sync")
# print(" restarting may fix")
# edges[channel_index][i] += overflow
# add last data point to finish the signal
edges[channel_index].append((len(packets) + 1) * ticks_per_packet)
# ensure time is monotonic increasing
# for channel_index, these_edges in enumerate(edges):
# for i in range(1, len(these_edges)):
# assert these_edges[i] >= these_edges[i - 1]
# now unpack adc values
adc_values = []
for packet in packets:
adc_values.extend(packet.adc_values)
print("- Done!")
print("- Edges in each channel:", [len(x) - 2 for x in edges])
print("- ADC readings:", len(adc_values))
# print([x[:10] for x in edges])
# print("Edges in each channel:", [len(x) - 2 for x in edges])
# process each signal into a BilevelData object
signals = []
for i in range(header.signal_count):
data = BilevelData()
data.start_time = 0.0
data.start_high = False
data.seconds_per_tick = 1.0 / header.signal_frequencies[i]
if edges[i][:2] == [0, 0]:
del edges[i][0]
data.start_high = True
data.edges = edges[i]
signals.append(data)
if adc_values:
# transpose lists
adc_values = list(map(list, zip(*adc_values)))
for c, values in enumerate(adc_values):
low, high = header.adc_ranges[c]
data = PlotData()
data.start_time = 0.0
data.name = "ADC%d" % (c + 1)
data.seconds_per_tick = (
header.ticks_per_adc_reading / header.system_clock
)
data.points = [
(i, low + y * (high - low) / 4095.0)
for i, y in enumerate(values)
]
signals.append(data)
return signals
def create_xy_vel(scope_panel: ScopePanel):
"""Calculate and return the XY_VEL signal from X_VEL and Y_VEL."""
x_vel = None
y_vel = None
for channel in scope_panel.channels:
for signal in channel.signals:
if signal.name == "X_VEL":
x_vel = signal.get_master_data()
if signal.name == "Y_VEL":
y_vel = signal.get_master_data()
assert x_vel and y_vel
# get all x points
x_values = [x[0] for x in x_vel.points]
x_values.extend(x[0] for x in y_vel.points)
x_values = sorted(set(x_values))
# go through all points and find y values
x_vel_it = iter(x_vel.points)
y_vel_it = iter(y_vel.points)
x_vel_point = next(x_vel_it)
y_vel_point = next(y_vel_it)
# hold new points
points = []
for x in x_values:
# increase x_vel point until we're at/past this point
while x_vel_point[0] < x:
x_vel_point = next(x_vel_it)
while y_vel_point[0] < x:
y_vel_point = next(y_vel_it)
vel = math.sqrt(x_vel_point[1] ** 2 + y_vel_point[1] ** 2)
if points:
points.append((points[-1][0], vel))
points.append((x, vel))
data = PlotData()
data.name = "XY_VEL"
data.start_time = x_vel.start_time
data.seconds_per_tick = x_vel.seconds_per_tick
data.points = points
signal = Signal(name="XY_VEL", color=wx.WHITE, data=data)
return signal
def postprocess_signals(scope_panel: ScopePanel):
"""Postprocess step position from the DIR and STEP channels."""
all_signals = dict()
for channel in scope_panel.channels:
for signal in channel.signals:
all_signals[signal.name] = signal.get_master_data()
# find all channels with both a *_STEP and *_DIR signal
names = [x[:-5] for x in all_signals.keys() if x.endswith("_STEP")]
names = [x for x in names if x + "_DIR" in all_signals.keys()]
# postprocess each one
new_signals = []
for name in names:
step_data = all_signals[name + "_STEP"]
dir_data = all_signals[name + "_DIR"]
pos_data = decode_stepper(step_data, dir_data)
# scale from counts to units
# pos_data.points = [(x, y / 80.0) for (x, y) in pos_data.points]
vel_data = derivate_data_squared_signal(pos_data)