-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
893 lines (777 loc) · 36 KB
/
main.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
# -*- coding: utf-8 -*-
#attention, vérifier le port COM de connexion à l'imprimante ainsi que le baudrate, et les changer dans l'instanciation de la classe communication_serie si nécessaire
#(partie Functions printer (PAr123) ligne 243)
import sys
import os
import visa
import Oscilloscope_control as ui_osc #fichier où est créé l'ui
import time
from Utils import ui_test, dictionnary_operation as dic
from Acq_script import nucleo_acquisition
from Acq_script import test_acquisition
from PyQt5 import QtWidgets, QtCore
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QThread, QPoint
import PyQt5
import cv2
import numpy as np
import communication_serie #bibliothèque écrite par le PAr123
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self)
self.ui = ui_osc.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn_disconnect.setEnabled(False)
self.ui.btn_ld_osc.setEnabled(False)
self.ui.btn_apply.setEnabled(False)
self.ui.actionDisconnect.setEnabled(False)
self.ui.actionLoad_from_oscilloscope.setEnabled(False)
self.ui.lbl_cmd.setEnabled(False)
self.ui.cmd_line.setEnabled(False)
self.ui.btn_send_cmd.setEnabled(False)
# =============================================================================
# Variables
# =============================================================================
self.oscilloscope = None
self.communication_printer = None # objet qui va gérer la communication avec l'imprimante (instanciation de la classe communication_serie
# =============================================================================
# other initialization
# =============================================================================
self.update_res()
# =============================================================================
# Connection of menu to actions
# =============================================================================
self.ui.actionConnect.triggered.connect(self.instrument_connect)
self.ui.actionDisconnect.triggered.connect(self.instrument_disconnect)
self.ui.actionLoad_from_file_2.triggered.connect(self.load_file)
self.ui.actionLoad_from_oscilloscope.triggered.connect(
self.read_instrument)
self.ui.actionSave.triggered.connect(self.save)
self.ui.actionQuit.triggered.connect(self.close)
# =============================================================================
# Help Menu connection
# =============================================================================
self.ui.actionAcquisition.triggered.connect(
lambda: os.startfile("Help/Acquisition.pdf"))
self.ui.actionAlias.triggered.connect(
lambda: os.startfile("Help/Alias.pdf"))
self.ui.actionCalibrate.triggered.connect(
lambda: os.startfile("Help/Calibrate.pdf"))
self.ui.actionCursor.triggered.connect(
lambda: os.startfile("Help/Cursor.pdf"))
self.ui.actionDiagnostic.triggered.connect(
lambda: os.startfile("Help/Diagnostic.pdf"))
self.ui.actionDisplay.triggered.connect(
lambda: os.startfile("Help/Display.pdf"))
self.ui.actionFile_System.triggered.connect(
lambda: os.startfile("Help/File_System.pdf"))
self.ui.actionHardcopy.triggered.connect(
lambda: os.startfile("Help/Hardcopy.pdf"))
self.ui.actionHistogram.triggered.connect(
lambda: os.startfile("Help/Histogram.pdf"))
self.ui.actionHorizontal.triggered.connect(
lambda: os.startfile("Help/Horizontal.pdf"))
self.ui.actionMath.triggered.connect(
lambda: os.startfile("Help/Math.pdf"))
self.ui.actionMeasurement.triggered.connect(
lambda: os.startfile("Help/Measurement.pdf"))
self.ui.actionMiscellaneous.triggered.connect(
lambda: os.startfile("Help/Miscellaneous.pdf"))
self.ui.actionSave_and_Recall.triggered.connect(
lambda: os.startfile("Help/Save_and_Reccal.pdf"))
self.ui.actionStatus_and_Error.triggered.connect(
lambda: os.startfile("Help/Status_and_Error.pdf"))
self.ui.actionTrigger.triggered.connect(
lambda: os.startfile("Help/Trigger.pdf"))
self.ui.actionVertical.triggered.connect(
lambda: os.startfile("Help/Vertical.pdf"))
self.ui.actionWaveform_Transfert.triggered.connect(
lambda: os.startfile("Help/Waveform_Transfert.pdf"))
self.ui.actionZoom.triggered.connect(
lambda: os.startfile("Help/Zoom.pdf"))
# =============================================================================
# Connection of button actions
# =============================================================================
self.ui.btn_connect.clicked.connect(self.instrument_connect)
self.ui.btn_disconnect.clicked.connect(self.instrument_disconnect)
self.ui.btn_ld_file.clicked.connect(self.load_file)
self.ui.btn_ld_osc.clicked.connect(self.read_instrument)
self.ui.btn_save.clicked.connect(self.save)
self.ui.btn_apply.clicked.connect(self.apply_changes)
self.ui.btn_send_cmd.clicked.connect(self.send_cmd)
self.ui.btn_acq.clicked.connect(self.start_acquisition)
# =============================================================================
# Other connection
# =============================================================================
self.ui.hor_sca.currentIndexChanged.connect(self.update_res)
self.ui.channel1.toggled.connect(self.update_res)
self.ui.channel2.toggled.connect(self.update_res)
self.ui.channel3.toggled.connect(self.update_res)
self.ui.channel4.toggled.connect(self.update_res)
self.ui.acq_grp.toggled.connect(self.active_acq)
self.ui.check_data_start.stateChanged.connect(
lambda: self.ui.data_start.setEnabled(self.ui.check_data_start.isChecked()))
self.ui.check_data_stop.stateChanged.connect(
lambda: self.ui.data_stop.setEnabled(self.ui.check_data_stop.isChecked()))
self.ui.hor_res.currentIndexChanged.connect(self.data_stop_update)
self.ui.acq_script.currentIndexChanged.connect(self.acq_script_param)
# =============================================================================
# Run video
# =============================================================================
self.playCamera = True
self.cameraScreenShot = QPixmap()
self.cameraScreenShotRef = QPixmap()
self.initialiseCamera()
self.display_width = self.ui.CameraDisplay.size().width()
self.display_height = self.ui.CameraDisplay.size().height()
# Draw on image
self.drawing = False
self.globalDrawing = True
# Draw Rect
self.resetRectangle()
self.isPlacingCorner = False
self.placingCorner = None
# Calibrate
self.calibrated = False
self.isPlacingCalibrate = False
self.cornerCalibrateNumber = 0
# Homography
self.homography = None
self.ptsDst = np.array([
[0,0],
[self.display_width-1,0],
[self.display_width-1,self.display_height-1],
[0,self.display_height-1]
])
# Resolution of the Attack
self.ui.AttackResolution.setValue(1)
self.ui.AttackResolution.setMinimum(0.1)
self.ui.AttackResolution.setSingleStep(0.1)
self.ui.AttackResolution.setDecimals(1)
self.ui.StartAttack.setEnabled(False)
# Printer caracteristics (mm)
self.printerHeight = 200
self.printerWidth = 200
# create the video capture thread
self.thread = VideoThread(0)
# connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
# start the thread
self.thread.start()
self.corners = {
'tl': QPoint(0, 0),
'tr': QPoint(0, 0),
'bl': QPoint(0, 0),
'br': QPoint(0, 0)
}
self.calibrationCorners = {
'tl': QPoint(-1, -1),
'tr': QPoint(-1, -1),
'bl': QPoint(-1, -1),
'br': QPoint(-1, -1)
}
try:
with open('calibration.dat', 'r') as f:
ptsSrc = eval(f.read())
print(ptsSrc)
ptsSrc = np.array(ptsSrc)
self.homography, status = cv2.findHomography(ptsSrc,self.ptsDst)
self.calibrated = True
self.ui.StartAttack.setEnabled(True)
except:
print('Aucune calibration chargée')
self.cornerNames = ['tl','tr','br','bl']
try:
with open('position.dat', 'r') as f:
self.corners = eval(f.read())
print(self.corners)
self.updateRectLabels()
except:
print('Aucune calibration chargée')
self.originHeight = 1
self.originWidth = 1
# =============================================================================
# Circuit Recognition Connections (PAr122)
# =============================================================================
self.ui.CameraSelect.currentIndexChanged.connect(self.changeCam)
self.ui.toogleCamera.clicked.connect(self.toogleCamera)
self.ui.placeCornerTL.clicked.connect(
lambda: self.placeCorner('tl'))
self.ui.placeCornerTR.clicked.connect(
lambda: self.placeCorner('tr'))
self.ui.placeCornerBL.clicked.connect(
lambda: self.placeCorner('bl'))
self.ui.resetRectangle.clicked.connect(self.resetRectangle)
self.ui.DrawRectangle.clicked.connect(lambda : self.drawRectangle(self.corners))
self.ui.CalibrateCam.clicked.connect(self.calibrateCam)
self.ui.StartAttack.clicked.connect(self.startAttack)
# =============================================================================
# Printer connexion (PAr123) : ui settings
# =============================================================================
self.ui.printer_connexion.clicked.connect(self.connexion_printer)
self.ui.printer_disconnexion.clicked.connect(self.disconnexion_printer)
self.ui.goto_coord.clicked.connect(self.goto)
self.ui.send_gcode.clicked.connect(self.send_gcode)
self.ui.ApproachButton.clicked.connect(self.approach)
self.ui.recup_X.setValue(0.0)
self.ui.recup_X.setMinimum(-100.0)
self.ui.recup_X.setMaximum(100.0)
self.ui.recup_X.setSingleStep(0.1)
self.ui.recup_X.setDecimals(1)
self.ui.recup_Y.setValue(0.0)
self.ui.recup_Y.setMinimum(-100.0)
self.ui.recup_Y.setMaximum(100.0)
self.ui.recup_Y.setSingleStep(0.1)
self.ui.recup_Y.setDecimals(1)
self.ui.recup_Z.setValue(0.0)
self.ui.recup_Z.setMinimum(-100.0)
self.ui.recup_Z.setMaximum(100.0)
self.ui.recup_Z.setSingleStep(0.1)
self.ui.recup_Z.setDecimals(1)
# =============================================================================
# Functions printer (PAr123)
# =============================================================================
def connexion_printer(self):
if self.communication_printer==None:
self.communication_printer=communication_serie.communication_serie('COM4',250000)
time.sleep(1)
self.communication_printer.send('G28')
def disconnexion_printer(self):
if self.communication_printer!=None:
self.communication_printer.disconnect()
self.communication_printer=None
def goto(self):
self.communication_printer.send('G91')
time.sleep(0.1)
self.communication_printer.sendxyz(str(self.ui.recup_X.value()),str(self.ui.recup_Y.value()),str(self.ui.recup_Z.value()))
time.sleep(0.1)
self.communication_printer.send('G90')
def send_gcode(self):
self.communication_printer.send(self.ui.recup_gcode.text())
# =============================================================================
# Functions
# =============================================================================
def send_cmd(self):
cmd = self.ui.cmd_line.text()
if cmd[len(cmd) - 1] == '?':
self.ui.log.append(self.oscilloscope.query(cmd) + "\n")
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
else:
self.oscilloscope.write(cmd)
def instrument_connect(self):
rm = visa.ResourceManager()
try:
self.oscilloscope = rm.open_resource(self.ui.osc_name.text())
self.ui.btn_disconnect.setEnabled(True)
self.ui.btn_ld_osc.setEnabled(True)
self.ui.btn_apply.setEnabled(True)
self.ui.actionDisconnect.setEnabled(True)
self.ui.actionLoad_from_oscilloscope.setEnabled(True)
self.ui.lbl_cmd.setEnabled(True)
self.ui.cmd_line.setEnabled(True)
self.ui.btn_send_cmd.setEnabled(True)
self.ui.groupBox.setEnabled(True)
self.ui.data_stop.setText(self.oscilloscope.query("DATa:STOP?"))
self.ui.log.append('connected to ' + self.ui.osc_name.text())
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
return 1
except:
self.ui.log.append('oscilloscope connection problem')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
return 0
def instrument_disconnect(self):
try:
self.oscilloscope.close()
self.oscilloscope = None
self.ui.btn_disconnect.setEnabled(False)
self.ui.btn_ld_osc.setEnabled(False)
self.ui.btn_apply.setEnabled(False)
self.ui.actionDisconnect.setEnabled(False)
self.ui.actionLoad_from_oscilloscope.setEnabled(False)
self.ui.lbl_cmd.setEnabled(False)
self.ui.cmd_line.setEnabled(False)
self.ui.btn_send_cmd.setEnabled(False)
self.ui.groupBox.setEnabled(False)
self.ui.log.append('disconnected from' + self.ui.osc_name.text())
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
except:
self.ui.log.append('oscilloscope disconnection problem')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
def load_file(self):
if self.ui.check_vert_app.isChecked():
self.load_vertical()
if self.ui.check_hor_app.isChecked():
self.load_horizontal()
if self.ui.check_trig_app.isChecked():
self.load_trigger()
def read_instrument(self):
self.ui.log.append('read_instrument : not implemented')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
def save(self):
if self.ui.check_vert_app.isChecked():
self.save_vertical()
if self.ui.check_hor_app.isChecked():
self.save_horizontal()
if self.ui.check_trig_app.isChecked():
self.save_trigger()
def close(self):
QtCore.QCoreApplication.instance().quit()
def apply_changes(self):
self.ui.log.append('apply change to instrument')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
if self.ui.check_vert_app.isChecked():
self.apply_vertical()
if self.ui.check_hor_app.isChecked():
self.apply_horizontal()
if self.ui.check_trig_app.isChecked():
self.apply_trigger()
if self.ui.check_wfm_app.isChecked():
self.apply_wfm()
def update_res(self):
self.ui.hor_res.clear()
index = self.ui.hor_sca.currentIndex()
f = open('dictionnaries/resolutions.txt', 'r')
for i in range(0, index + 1):
s = (f.readline()).rstrip()
f.close()
del f
del index
tmp = s.split(';')
del s
act_ch = self.activated_channel()
if act_ch > 0:
for i in range(0, len(tmp) - (self.activated_channel() - 1)):
self.ui.hor_res.addItem(tmp[i])
def data_stop_update(self):
if (not(self.ui.data_stop.isEnabled()) and type(self.oscilloscope) != type(None)):
self.ui.data_stop.setText(
self.oscilloscope.query("HORizontal:RECOrdlength?"))
def activated_channel(self):
res = self.ui.channel1.isChecked() + self.ui.channel2.isChecked() + \
self.ui.channel3.isChecked()
if (res < 3):
res = res + self.ui.channel4.isChecked()
return res
def apply_vertical(self):
self.ui.log.append('apply vertical settings')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
# Channel 1
if self.ui.channel1.isChecked():
self.oscilloscope.write("SELECT:CH1 ON")
self.oscilloscope.write("CH1:SCA " + self.ui.ch1_sca.currentText())
self.oscilloscope.write("CH1:POS " + self.ui.ch1_pos.text())
else:
self.oscilloscope.write("SELECT:CH1 OFF")
# Channel 2
if self.ui.channel2.isChecked():
self.oscilloscope.write("SELECT:CH2 ON")
self.oscilloscope.write("CH2:SCA " + self.ui.ch2_sca.currentText())
self.oscilloscope.write("CH2:POS " + self.ui.ch2_pos.text())
else:
self.oscilloscope.write("SELECT:CH2 OFF")
# Channel 3
if self.ui.channel3.isChecked():
self.oscilloscope.write("SELECT:CH3 ON")
self.oscilloscope.write("CH3:SCA " + self.ui.ch3_sca.currentText())
self.oscilloscope.write("CH3:POS " + self.ui.ch3_pos.text())
else:
self.oscilloscope.write("SELECT:CH3 OFF")
# Channel 4
if self.ui.channel4.isChecked():
self.oscilloscope.write("SELECT:CH4 ON")
self.oscilloscope.write("CH4:SCA " + self.ui.ch4_sca.currentText())
self.oscilloscope.write("CH4:POS " + self.ui.ch4_pos.text())
else:
self.oscilloscope.write("SELECT:CH4 OFF")
def apply_horizontal(self):
self.ui.log.append('apply horizontal settings')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
self.oscilloscope.write("HOR:POS " + self.ui.hor_pos.text())
self.oscilloscope.write("HOR:MAI:SCA " + self.ui.hor_sca.currentText())
if (len(self.ui.hor_res.currentText()) > 0):
self.oscilloscope.write("HOR:RESO " + str(10 * float(
self.ui.hor_res.currentText()) * float(self.ui.hor_sca.currentText())))
def apply_trigger(self):
self.ui.log.append('apply trigger settings')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
self.oscilloscope.write(
"TRIGger:A:EDGE:SOU " + self.ui.trig_sou.currentText())
self.oscilloscope.write(
"TRIGger:A:EDGE:COUP " + self.ui.trig_cou.currentText())
self.oscilloscope.write(
"TRIGger:A:EDGE:SLO " + self.ui.trig_slo.currentText())
self.oscilloscope.write("TRIGger:A:LEVel " + self.ui.trig_lvl.text())
def apply_wfm(self):
self.ui.log.append('apply Waveform parameter settings')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
self.oscilloscope.write(
"DATa:SOUrce " + self.ui.data_sou.currentText())
self.oscilloscope.write(
"DATa:ENCdg " + self.ui.data_encoding.currentText())
self.oscilloscope.write("DATa:STARt " + self.ui.data_start.text())
self.oscilloscope.write("DATa:STOP " + self.ui.data_stop.text())
def save_vertical(self):
f = open('dictionnaries/vertical_parameters.txt', 'w')
if self.ui.channel1.isChecked():
f.write("channel1;1\n")
else:
f.write("channel1;0\n")
f.write("ch1_pos;" + self.ui.ch1_pos.text() + "\n")
f.write("ch1_sca;" + str(self.ui.ch1_sca.currentIndex()) + "\n")
if self.ui.channel2.isChecked():
f.write("channel2;1\n")
else:
f.write("channel2;0\n")
f.write("ch2_pos;" + self.ui.ch2_pos.text() + "\n")
f.write("ch2_sca;" + str(self.ui.ch2_sca.currentIndex()) + "\n")
if self.ui.channel3.isChecked():
f.write("channel3;1\n")
else:
f.write("channel3;0\n")
f.write("ch3_pos;" + self.ui.ch3_pos.text() + "\n")
f.write("ch3_sca;" + str(self.ui.ch3_sca.currentIndex()) + "\n")
if self.ui.channel4.isChecked():
f.write("channel4;1\n")
else:
f.write("channel4;0\n")
f.write("ch4_pos;" + self.ui.ch4_pos.text() + "\n")
f.write("ch4_sca;" + str(self.ui.ch4_sca.currentIndex()) + "\n")
f.close()
def save_horizontal(self):
f = open('dictionnaries/horizontal_parameters.txt', 'w')
f.write("hor_pos;" + self.ui.hor_pos.text() + "\n")
f.write("hor_sca;" + str(self.ui.hor_sca.currentIndex()) + "\n")
f.write("hor_res;" + str(self.ui.hor_res.currentIndex()) + "\n")
f.close()
def save_trigger(self):
f = open('dictionnaries/trigger_parameters.txt', 'w')
f.write("trig_sou;" + str(self.ui.trig_sou.currentIndex()) + "\n")
f.write("trig_cou;" + str(self.ui.trig_cou.currentIndex()) + "\n")
f.write("trig_slo;" + str(self.ui.trig_slo.currentIndex()) + "\n")
f.write("trig_lvl;" + self.ui.trig_lvl.text() + "\n")
f.close()
def load_vertical(self):
f = open('dictionnaries/vertical_parameters.txt', 'r')
s = f.readlines()
f.close()
# Channel 1
t = (s[0].rstrip()).split(';')
eval("self.ui." + t[0] + ".setChecked(int(" + t[1] + "))")
t = (s[1].rstrip()).split(';')
eval("self.ui." + t[0] + ".setText(str(" + t[1] + "))")
t = (s[2].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
# Channel 2
t = (s[3].rstrip()).split(';')
eval("self.ui." + t[0] + ".setChecked(int(" + t[1] + "))")
t = (s[4].rstrip()).split(';')
eval("self.ui." + t[0] + ".setText(str(" + t[1] + "))")
t = (s[5].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
# Channel 3
t = (s[6].rstrip()).split(';')
eval("self.ui." + t[0] + ".setChecked(int(" + t[1] + "))")
t = (s[7].rstrip()).split(';')
eval("self.ui." + t[0] + ".setText(str(" + t[1] + "))")
t = (s[8].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
# Channel 4
t = (s[9].rstrip()).split(';')
eval("self.ui." + t[0] + ".setChecked(int(" + t[1] + "))")
t = (s[10].rstrip()).split(';')
eval("self.ui." + t[0] + ".setText(str(" + t[1] + "))")
t = (s[11].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
def load_horizontal(self):
f = open('dictionnaries/horizontal_parameters.txt', 'r')
s = f.readlines()
f.close()
t = (s[0].rstrip()).split(';')
eval("self.ui." + t[0] + ".setText(str(" + t[1] + "))")
t = (s[1].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
t = (s[2].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
def load_trigger(self):
f = open('dictionnaries/trigger_parameters.txt', 'r')
s = f.readlines()
f.close()
t = (s[0].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
t = (s[1].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
t = (s[2].rstrip()).split(';')
eval("self.ui." + t[0] + ".setCurrentIndex(int(" + t[1] + "))")
t = (s[3].rstrip()).split(';')
eval("self.ui." + t[0] + ".setText(str(" + t[1] + "))")
def active_acq(self):
self.ui.acq_script.clear()
self.ui.parameter_edit.clear()
if self.ui.acq_grp.isEnabled():
for file in os.listdir("Acq_script/"):
if file.endswith(".py"):
self.ui.acq_script.addItem(file[0:len(file) - 3])
def acq_script_param(self):
if len(self.ui.acq_script.currentText()) > 0:
param_file = open(
"Acq_script/" + self.ui.acq_script.currentText() + ".dat", "r")
l = param_file.readlines()
param_file.close()
self.ui.parameter_edit.clear()
for i in range(0, len(l)):
self.ui.parameter_edit.append(l[i].rstrip())
def start_acquisition(self):
if type(self.oscilloscope) != type(None):
self.ui.log.append('start acquisition')
with open("log.txt", "w") as log:
log.write(self.ui.log.toPlainText())
with open("Acq_script/" + self.ui.acq_script.currentText() + ".dat", "w") as p:
p.write(self.ui.parameter_edit.toPlainText())
# =============================================================================
# self.acquisition= eval(self.ui.acq_script.currentText() + ".External()")
# self.acquisition.countChanged.connect(self.onCountChanged)
# self.acquisition.start(self)
# =============================================================================
eval(self.ui.acq_script.currentText() + ".run(self)")
self.ui.log.append('Acquisition done')
def onCountChanged(self, val):
self.ui.progressBar.setValue(val)
def initialiseCamera(self):
availableCameras = []
for i in range(5):
cap = cv2.VideoCapture(i, cv2.CAP_DSHOW)
if cap != None and cap.isOpened():
availableCameras.append(i)
cap.release()
self.ui.CameraSelect.clear()
for camera in availableCameras:
self.ui.CameraSelect.addItem(str(camera))
@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
self.originHeight, self.originWidth, z = cv_img.shape # Used for scaled images
if self.calibrated:
hm_img = cv2.warpPerspective(cv_img, self.homography, (self.display_width,self.display_height))
qt_img = self.convert_cv_qt(hm_img)
else:
qt_img = self.convert_cv_qt(cv_img)
self.cameraScreenShot = qt_img
self.cameraScreenShotRef = qt_img.copy()
self.ui.CameraDisplay.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(
rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(
self.display_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
def changeCam(self):
""" Reboot the thread with the right cam index"""
cam = self.ui.CameraSelect.currentIndex()
self.thread.stop()
self.thread = VideoThread(cam)
self.thread.change_pixmap_signal.connect(self.update_image)
self.thread.start()
if not self.playCamera:
self.toogleCamera()
def closeEvent(self, event):
""" Method called when the window is closed """
self.thread.stop()
self.disconnexion_printer()
event.accept()
def toogleCamera(self, pause = False):
if self.playCamera or pause:
self.thread.pause()
self.playCamera = False
else:
self.thread.resume()
self.playCamera = True
text = ""
if self.playCamera:
text = 'Take Picture'
else:
text = 'Restore Camera'
self.ui.toogleCamera.setText(text)
def mousePressEvent(self, event):
""" Place a corner of a rectangle """
if event.button() == Qt.LeftButton:
self.point = event.pos()
self.point = self.ui.CameraDisplay.mapFrom(
self.ui.centralwidget, event.pos())
# offset of 21 because the Widget centralWidget is not at 0,0 of the main window
self.point.setY(self.point.y() - 21)
print(self.point)
if self.isPlacingCorner: # Define processor location
self.corners[self.placingCorner] = self.point
self.updateRectLabels()
self.isPlacingCorner = False
print(self.corners)
with open('position.dat', 'w') as f:
f.write(str(self.corners))
if self.isPlacingCalibrate: # Calibrate Camera
self.calibrationCorners[self.cornerNames[self.cornerCalibrateNumber]] = self.point
self.cornerCalibrateNumber += 1
if self.cornerCalibrateNumber == 4:
#self.drawRectangle(self.calibrationCorners, Qt.yellow)
self.isPlacingCalibrate = False
self.ui.CalibrateCam.setText("calibrate")
self.calibrated = True
self.ui.StartAttack.setEnabled(True)
# Homography's parameters
ptsSrc = []
for key, item in self.calibrationCorners.items():
ptsSrc.append(
[int(item.x()*self.originHeight/self.display_height),
int(item.y()*self.originWidth/self.display_width)])
point = ptsSrc.pop(2)
ptsSrc.append(point)
# Export
with open('calibration.dat', 'w') as f:
f.write(str(ptsSrc))
ptsSrc = np.array(ptsSrc)
self.homography, status = cv2.findHomography(ptsSrc,self.ptsDst)
self.toogleCamera()
def placeCorner(self, corner):
self.isPlacingCorner = True
self.placingCorner = corner
def updateRectLabels(self):
self.ui.cornerTLX.setText(str(self.corners['tl'].x()))
self.ui.cornerTLY.setText(str(self.corners['tl'].y()))
self.ui.cornerTRX.setText(str(self.corners['tr'].x()))
self.ui.cornerTRY.setText(str(self.corners['tr'].y()))
self.ui.cornerBLX.setText(str(self.corners['bl'].x()))
self.ui.cornerBLY.setText(str(self.corners['bl'].y()))
def resetRectangle(self):
self.corners = {
'tl': QPoint(-1, -1),
'tr': QPoint(-1, -1),
'bl': QPoint(-1, -1),
'br': QPoint(-1, -1)
}
self.updateRectLabels()
self.ui.CameraDisplay.setPixmap(self.cameraScreenShotRef)
self.cameraScreenShot = self.cameraScreenShotRef.copy()
def drawRectangle(self, rectangle, color = Qt.red):
painter = QPainter(self.cameraScreenShot)
painter.setPen(QPen(color, 2,
Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
newX = rectangle['bl'].x() - rectangle['tl'].x() + \
rectangle['tr'].x()
newY = rectangle['tr'].y() - rectangle['tl'].y() + \
rectangle['bl'].y()
rectangle['br'] = QPoint(newX, newY)
with open('position.dat', 'w') as f:
f.write(str(rectangle))
painter.drawLine(rectangle['tl'], rectangle['tr'])
painter.drawLine(rectangle['tr'], rectangle['br'])
painter.drawLine(rectangle['br'], rectangle['bl'])
painter.drawLine(rectangle['tl'], rectangle['bl'])
print(rectangle)
self.ui.CameraDisplay.setPixmap(self.cameraScreenShot)
def calibrateCam(self):
print("Go to (0,0)")
self.toogleCamera(True)
self.ui.CameraDisplay.setPixmap(self.cameraScreenShotRef)
self.cameraScreenShot = self.cameraScreenShotRef.copy()
self.calibrated = False
self.calibrationCorners = {
'tl': QPoint(-1, -1),
'tr': QPoint(-1, -1),
'bl': QPoint(-1, -1),
'br': QPoint(-1, -1)
}
self.isPlacingCalibrate = True
self.cornerCalibrateNumber = 0
self.ui.CalibrateCam.setText("Calibrating...")
def startAttack(self):
print('start')
# Importing data for attack
resolution = self.ui.AttackResolution.value()
try:
with open('position.dat', 'r') as f:
position = eval(f.read())
# Changing scale (verify if same coordinate system)
for key, item in position.items():
position[key] = (round(item.x() * self.printerWidth / self.display_width,1), round(self.printerHeight - item.y() * self.printerHeight / self.display_height,1))
print(position)
# attack matrice
taille_x=int(abs(position['tr'][0]-position['tl'][0])/resolution)
taille_y=int(abs(position['tl'][1]-position['bl'][1])/resolution)
x=[round(position['tl'][0]+i*resolution,1) for i in range(taille_x)]
y=[round(position['tl'][1]-i*resolution,1) for i in range(taille_y)]
for i in x:
self.communication_printer.send('G0 X'+str(i)+' Y'+str(round(position['tl'][1],1)))
time.sleep(1)
for j in y:
self.communication_printer.send('G0 Y'+str(j))
time.sleep(1)
except:
print('Donnée(s) manquantes')
def approach(self):
try:
with open('position.dat', 'r') as f:
position = eval(f.read())
# Changing scale (verify if same coordinate system)
position['tl'] = (round(position['tl'].x() * self.printerWidth / self.display_width,1), round(self.printerHeight - position['tl'].y() * self.printerHeight / self.display_height,1))
self.communication_printer.sendxyz(str(position['tl'][0]),str(position['tl'][1]),str(20))
except:
print('Donnée(s) manquantes')
class VideoThread(QThread):
""" Thread displaying opencv VideoCapture (allow the interaction with the window while displaying) """
def __init__(self, cam):
super().__init__()
self.running = True
self.cam = cam
self.play = True
change_pixmap_signal = pyqtSignal(np.ndarray)
def run(self):
# capture from web cam
cap = cv2.VideoCapture(self.cam, cv2.CAP_DSHOW)
while self.running:
while self.play:
ret, cv_img = cap.read()
if ret:
# Send the image thru a slot to MyWindow.update_image
self.change_pixmap_signal.emit(cv_img)
def stop(self):
"""Sets run flag to False and waits for thread to finish"""
self.play = False
self.running = False
self.wait()
def pause(self):
self.play = False
def resume(self):
self.play = True
if __name__ == '__main__':
test = 0
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
if test:
ui_test.vertival_test(window, 1)
time.sleep(1)
ui_test.vertival_test(window, 2)
time.sleep(1)
ui_test.vertival_test(window, 3)
time.sleep(1)
ui_test.vertival_test(window, 4)
time.sleep(1)
ui_test.Trigger_test(window)
time.sleep(1)
ui_test.horizontal_test(window)
sys.exit(app.exec_())