-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmainwindow.cpp
839 lines (738 loc) · 31.3 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
frameGrabber = new FrameGrabber();
measureTool = new MeasureTool();
pyClassification = new PyClassification();
fdTester = new FDTester();
triggerForm = new TriggerForm(this);
//modbusTest = new ModbusTest(this);
//mqttTest = new MqttTest(this);
//opcuaTest = new OpcUaTest(this);
//rfidTest = new RFIDtest(this);
initialSetup();
}
MainWindow::~MainWindow()
{
if (isOpcUaConnected)
{
visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 0, QOpcUa::UInt16);
diconnectToOPCUA();
}
delete pyClassification;
delete fdTester;
delete frameGrabber;
delete measureTool;
//delete calibratorForm;
delete triggerForm;
//delete modbusClient;
delete ui;
}
void MainWindow::initialSetup()
{
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Vision Inspection System started.");
ui->labelShowFrame->setScaledContents(true);
ui->scrollArea->setBackgroundRole(QPalette::Dark);
ui->scrollArea->setWidget(ui->labelShowFrame);
ui->scrollArea->setVisible(true);
opcuaProvider = new QOpcUaProvider(this);
receiveReadCaliConf();
bgImg.load("../resource/logo.png");
ui->labelShowFrame->setPixmap(bgImg);
ui->labelShowPos->setAlignment(Qt::AlignCenter);
ui->labelShowRes->setAlignment(Qt::AlignCenter);
ui->labelShowScale->setAlignment(Qt::AlignCenter);
ui->labelShowRGB->setAlignment(Qt::AlignCenter);
ui->pushButtonConnect->setEnabled(false);
ui->pushButtonDisconnect->setEnabled(false);
ui->pushButtonCapture->setEnabled(false);
ui->pushButtonStream->setEnabled(false);
ui->pushButtonStop->setEnabled(false);
ui->pushButtonSaveCapture->setEnabled(false);
ui->pushButtonScanDevices->setEnabled(true);
// Always dispaly the latest message in msg log in bottom
connect(ui->listWidgetMessageLog->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
ui->listWidgetMessageLog, SLOT(scrollToBottom()));
connect(this, SIGNAL(sendConnect()), frameGrabber, SLOT(receiveConnectCamera()));
connect(this, SIGNAL(sendDisconnect()), frameGrabber, SLOT(receiveDisconnectCamera()));
connect(this, SIGNAL(sendCaptureMode()), frameGrabber, SLOT(receiveStartCaptureMode()));
connect(this, SIGNAL(sendStreamMode()), frameGrabber, SLOT(receiveStartStreamMode()));
connect(this, SIGNAL(sendStopGrabbing()), frameGrabber, SLOT(receiveStopGrabbing()));
connect(frameGrabber, SIGNAL(sendCaptureFrame(cv::Mat)), this, SLOT(receiveRawFrame(cv::Mat)));
connect(ui->labelShowFrame, SIGNAL(sendMousePosition(QPoint&)), this, SLOT(receiveShowMousePosition(QPoint&)));
connect(this, SIGNAL(sendFrameToMeasurement(cv::Mat)), measureTool, SLOT(receiveFrame(cv::Mat)));
connect(this, SIGNAL(sendCalibrationPara(double, int)), measureTool, SLOT(receiveCalibrationPara(double, int)));
connect(measureTool, SIGNAL(sendFrameToShow(cv::Mat)), this, SLOT(receiveRawFrame(cv::Mat)));
connect(measureTool, SIGNAL(sendMeasurement(double, bool)), this, SLOT(receiveMeasurement(double, bool)));
connect(this, SIGNAL(sendStatusToWriteResult()), this, SLOT(receiveStatusToWriteResult()));
connect(this, SIGNAL(sendResultToCheck()), this, SLOT(receiveResultToCheck()));
emit sendCalibrationPara(currentPPMM, 3);
ui->comboBoxMatchMethod->addItems({"Machine Learning", "Image Processing"});
connectToOPCUA();
}
void MainWindow::receiveReadCaliConf()
{
QFile caliConfFile("../conf/calibration.conf");
if (!caliConfFile.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this,"File Read Error", "Configuration file can't open", QMessageBox::Yes);
}
else
{
QTextStream in(&caliConfFile);
QString line;
line = in.readLine();
currentPPMM = line.toDouble();
caliConfFile.close();
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Loaded ruler configuration: " + line + " pixel/mm");
}
}
void MainWindow::receiveMeasurement(double length, bool color)
{
ui->labelShowMeasurement->setText(QString::number(length, 'f', 2) + " mm");
// for PoC
isColorOK = color;
currentLength = length;
emit sendResultToCheck();
}
void MainWindow::on_pushButtonConnect_clicked()
{
emit sendConnect();
usleep(1000); // 1ms
if (frameGrabber->cameraConnected)
{
//visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 1, QOpcUa::UInt16);
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Camera is open.");
ui->pushButtonConnect->setEnabled(false);
ui->pushButtonConnect->setStyleSheet("background-color: rgb(100, 255, 100);"); // Make this button green, it's open.
ui->pushButtonDisconnect->setEnabled(true);
ui->pushButtonCapture->setEnabled(true);
ui->pushButtonStream->setEnabled(true);
}
else
{
ui->listWidgetMessageLog->addItem("[Error] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Fail to connect camera.");
QMessageBox::information(this, "Connetion Failure", "Please Check Camera and Debug.");
}
}
void MainWindow::on_pushButtonDisconnect_clicked()
{
// @ZC TODO: add one function to set disconnect status
if (grabMode == 'N') // in camera close mode
{
emit sendDisconnect();
usleep(1000);
if (!frameGrabber->cameraConnected)
{
//visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 0, QOpcUa::UInt16);
ui->pushButtonDisconnect->setEnabled(false);
ui->pushButtonConnect->setEnabled(true);
ui->pushButtonConnect->setStyleSheet("background-color: rgb(225, 225, 225);"); // Make it gray color
ui->pushButtonCapture->setEnabled(false);
ui->pushButtonStream->setEnabled(false);
ui->pushButtonSaveCapture->setEnabled(false);
ui->pushButtonStop->setEnabled(false);
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Camera is close.");
}
else
{
ui->listWidgetMessageLog->addItem("[Error] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Fail to disconnect camera.");
QMessageBox::information(this, "Disconnetion Failure", "Please Debug.");
}
}
else if (grabMode == 'S') // in camera streaming mode
{
QMessageBox::StandardButton disconnectReply;
disconnectReply = QMessageBox::question(this, "Disconnect",
"Camera is still in streaming mode. Are you sure to disconnect camera?",
QMessageBox::Yes|QMessageBox::No);
if (disconnectReply == QMessageBox::Yes)
{
emit sendDisconnect();
usleep(1000);
if (!frameGrabber->cameraConnected)
{
on_pushButtonStop_clicked();
//visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 0, QOpcUa::UInt16);
ui->pushButtonCapture->setEnabled(false);
ui->pushButtonStream->setEnabled(false);
ui->pushButtonStream->setStyleSheet("background-color: rgb(225, 225, 225);");
ui->pushButtonDisconnect->setEnabled(false);
ui->pushButtonSaveCapture->setEnabled(false);
ui->pushButtonConnect->setEnabled(true);
ui->pushButtonConnect->setStyleSheet("background-color: rgb(225, 225, 225);");
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Camera is close.");
}
}
}
else if (grabMode == 'C') // in camera capture mode
{
emit sendDisconnect();
usleep(1000);
if (!frameGrabber->cameraConnected)
{
//visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 0, QOpcUa::UInt16);
ui->pushButtonCapture->setEnabled(false);
ui->pushButtonStream->setEnabled(false);
ui->pushButtonDisconnect->setEnabled(false);
ui->pushButtonSaveCapture->setEnabled(false);
ui->pushButtonConnect->setEnabled(true);
ui->pushButtonConnect->setStyleSheet("background-color: rgb(225, 225, 225);");
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Camera is close.");
}
}
ui->labelShowFrame->setMouseTracking(false);
ui->labelShowFrame->setPixmap(bgImg);
ui->labelShowRes->setText("");
ui->labelShowPos->setText("");
ui->labelShowRGB->setText("");
ui->labelShowScale->setText("");
}
void MainWindow::on_pushButtonCapture_clicked()
{
emit sendCaptureMode();
grabMode = 'C';
ui->pushButtonStream->setEnabled(true);
ui->pushButtonStop->setEnabled(false);
ui->pushButtonSaveCapture->setEnabled(true);
ui->labelShowFrame->setMouseTracking(true);
ui->labelShowRes->setText("2448x2048"); // hard code need to be changed
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Capture one image.");
}
void MainWindow::on_pushButtonSaveCapture_clicked()
{
QString fileSavePathQStr = defaultSavePath + "/" + QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss") + ".jpg";
// convert QString to char
QByteArray ba = fileSavePathQStr.toLatin1();
const char *fileSavePathChar = ba.data();
if (cv::imwrite(fileSavePathChar, cvRawFrameCopy))
{
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " One image saved.");
}
}
void MainWindow::on_pushButtonStream_clicked()
{
emit sendStreamMode();
grabMode = 'S';
ui->pushButtonStream->setEnabled(false);
ui->pushButtonStream->setStyleSheet("background-color: rgb(100, 255, 100);"); // Make it green
ui->pushButtonCapture->setEnabled(false);
ui->pushButtonStop->setStyleSheet("background-color: rgb(255, 0, 0); color: rgb(255, 0, 0);"); // Make it red
ui->pushButtonStop->setEnabled(true);
ui->pushButtonSaveCapture->setEnabled(false);
streamTrigger = new QTimer(); // use timer to loop streaming
streamTrigger->setInterval(1);
connect(streamTrigger, SIGNAL(timeout()), frameGrabber, SLOT(receiveSendFrame()));
connect(frameGrabber, SIGNAL(sendFrame(cv::Mat)), this, SLOT(receiveRawFrame(cv::Mat)));
streamTrigger->start(); // loop start
ui->labelShowFrame->setMouseTracking(true);
ui->labelShowRes->setText("2448x2048"); // hard code, need to be changed
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Start streaming mode.");
}
void MainWindow::on_pushButtonStop_clicked()
{
if (grabMode == 'S')
{
streamTrigger->stop();
delete streamTrigger;
ui->labelShowFrame->setMouseTracking(false);
emit sendStopGrabbing();
usleep(1000);
if (!frameGrabber->startGrabbing)
{
grabMode = 'N';
ui->pushButtonCapture->setEnabled(true);
ui->pushButtonStream->setEnabled(true);
ui->pushButtonStream->setStyleSheet("background-color: rgb(225, 225, 225);");
ui->pushButtonStop->setStyleSheet("background-color: rgb(225 225, 225);");
ui->pushButtonStop->setEnabled(false);
ui->labelShowRes->setText("");
ui->labelShowPos->setText("");
ui->labelShowRGB->setText("");
ui->labelShowScale->setText("");
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Quit streaming mode.");
}
}
}
void MainWindow::on_pushButtonScanDevices_clicked()
{
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Scanning devices...");
QString deviceName = frameGrabber->scanDevices();
if (deviceName.contains("Basler", Qt::CaseSensitive))
{
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Basler Camera found.");
if (!frameGrabber->cameraConnected)
{
ui->pushButtonConnect->setEnabled(true);
}
}
else
{
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " No camera found, please check connection.");
ui->pushButtonConnect->setEnabled(false);
}
ui->comboBoxDevices->clear();
ui->comboBoxDevices->addItem(deviceName);
}
void MainWindow::receiveRawFrame(cv::Mat cvRawFrame)
{
cvRawFrameCopy = cvRawFrame.clone();
displayFrame();
}
void MainWindow::displayFrame()
{
cv::Mat tempFrame;
if (autoCalibration)
{
tempFrame = frameToCali.clone();
}
else if (autoMeasure)
{
tempFrame = frameToMeasure.clone();
}
else
{
tempFrame = cvRawFrameCopy.clone();
}
cv::resize(tempFrame, cvRGBFrame, cv::Size(), scaleFactor, scaleFactor);
cv::cvtColor(cvRGBFrame, cvRGBFrame, cv::COLOR_BGR2RGB);
qDisplayedFrame = QImage((uchar*)cvRGBFrame.data, cvRGBFrame.cols, cvRGBFrame.rows, cvRGBFrame.step, QImage::Format_RGB888);
ui->labelShowFrame->setPixmap(QPixmap::fromImage(qDisplayedFrame));
}
void MainWindow::connectToOPCUA()
{
const static QUrl opcuaServer(QLatin1String("opc.tcp://172.19.80.34:4840"));
// default plugin is open62541
//qDebug() << "Available OPCUA plugins:" << opcuaProvider->availableBackends();
opcuaClient = opcuaProvider->createClient(opcuaProvider->availableBackends()[0]);
if (!opcuaClient)
{
qDebug() << "Fail to create OPCUA client.";
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")
+ "Fail to create OPCUA client.");
return;
}
connect(opcuaClient, &QOpcUaClient::connected, this, &MainWindow::opcuaConnected);
connect(opcuaClient, &QOpcUaClient::disconnected, this, &MainWindow::opcuaDisconnected);
connect(opcuaClient, &QOpcUaClient::errorChanged, this, &MainWindow::opcuaClientError);
connect(opcuaClient, &QOpcUaClient::stateChanged, this, &MainWindow::opcuaClientState);
opcuaClient->connectToEndpoint(opcuaServer); // connect action
}
void MainWindow::diconnectToOPCUA()
{
if (isOpcUaConnected)
{
opcuaClient->disconnectFromEndpoint();
}
}
void MainWindow::on_actionChangeSavePath_triggered()
{
defaultSavePath = QFileDialog::getExistingDirectory();
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Image save path changed to: " + defaultSavePath);
}
void MainWindow::on_actionZoomIn_triggered()
{
if (scaleFactor >= 1.0)
{
scaleFactor += 1.0;
if (scaleFactor > 5.0) scaleFactor = 5.0;
}
else if (scaleFactor < 1.0)
{
scaleFactor *= 1.3333;
if (scaleFactor > 0.35 && scaleFactor < 0.5) scaleFactor = 0.4;
if (scaleFactor > 0.85 && scaleFactor < 1.1) scaleFactor = 1.0;
}
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Scaling factor: " + QString::number(scaleFactor));
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
if (grabMode == 'C') displayFrame();
}
void MainWindow::on_actionZoomToFit_triggered()
{
scaleFactor = 0.4;
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Scaling factor: " + QString::number(scaleFactor));
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
if (grabMode == 'C') displayFrame();
}
void MainWindow::on_actionZoomOut_triggered()
{
if (scaleFactor > 1.0)
{
scaleFactor -= 1.0;
}
else if (scaleFactor <= 1.0)
{
scaleFactor *= 0.6667;
if (scaleFactor >= 0.35 && scaleFactor < 0.5) scaleFactor = 0.4;
if (scaleFactor < 0.35) scaleFactor = 0.4;
}
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Scaling factor: " + QString::number(scaleFactor));
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
if (grabMode == 'C') displayFrame();
}
void MainWindow::on_actionZoomToRaw_triggered()
{
scaleFactor = 1.0;
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Scaling factor: " + QString::number(scaleFactor));
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
if (grabMode == 'C') displayFrame();
}
void MainWindow::receiveShowMousePosition(QPoint &pos)
{
ui->labelShowPos->setText(QString::number(pos.x()) + ", " + QString::number((pos.y())) );
}
void MainWindow::receiveFrameRequest()
{
emit sendFrameToCalibrator(cvRawFrameCopy);
}
void MainWindow::on_actionOpenImage_triggered()
{
grabMode = 'C';
QString openFilePath = QFileDialog::getOpenFileName(this, "Open an image", "../images/");
QByteArray ba = openFilePath.toLatin1();
const char *imagePath = ba.data();
if (openFilePath.isEmpty()) imagePath = "../resource/logo.png";
cv::Mat openImage = cv::imread(imagePath, 1);
receiveRawFrame(openImage);
ui->labelShowRes->setText("2448x2048"); // hard code
ui->labelShowFrame->setMouseTracking(true);
ui->labelShowScale->setText(QString::number(scaleFactor*100, 'f', 0)+"%");
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Opened an image.");
}
void MainWindow::on_actionAutoRulerStart_triggered()
{
autoMeasure = true;
frameToMeasure = cvRawFrameCopy.clone();
/*
MeasureTool *measureTool = new MeasureTool(frameToMeasure, currentPPMM);
delete measureTool;
cv::resize(frameToMeasure, cvRGBFrame, cv::Size(), scaleFactor, scaleFactor);
cv::cvtColor(cvRGBFrame, cvRGBFrame, cv::COLOR_BGR2RGB);
qDisplayedFrame = QImage((uchar*)cvRGBFrame.data, cvRGBFrame.cols, cvRGBFrame.rows, cvRGBFrame.step, QImage::Format_RGB888);
ui->labelShowFrame->setPixmap(QPixmap::fromImage(qDisplayedFrame));
*/
}
void MainWindow::on_actionAutoRulerStop_triggered()
{
autoMeasure = false;
displayFrame();
}
void MainWindow::on_actionManualRulerStart_triggered()
{
ui->labelShowFrame->startManualRuler(currentPPMM);
}
void MainWindow::on_actionManualRulerStop_triggered()
{
ui->labelShowFrame->stopManualRuler();
}
void MainWindow::on_actionCameraSetting_triggered()
{
SettingDialog *settingDialog = new SettingDialog(this);
int r = settingDialog->exec();
if (r == QDialog::Accepted)
{
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " Camera Parameters Updated.");
if (ui->pushButtonDisconnect->isEnabled())
{
if (!ui->pushButtonStream->isEnabled())
{
on_pushButtonStop_clicked();
on_pushButtonDisconnect_clicked();
on_pushButtonConnect_clicked();
on_pushButtonStream_clicked();
}
else
{
on_pushButtonDisconnect_clicked();
on_pushButtonConnect_clicked();
on_pushButtonCapture_clicked();
}
}
}
delete settingDialog;
}
void MainWindow::on_pushButtonMatch_clicked()
{
matchMethod = ui->comboBoxMatchMethod->currentText();
frameToTest = cvRawFrameCopy.clone();
if (matchMethod == "Machine Learning")
{
QString matchResult = pyClassification->process(frameToTest);
ui->labelMatchResult->setStyleSheet("color: blue; font: 20pt; background-color: white;");
ui->labelMatchResult->setText(matchResult);
// for PoC
colorStatus = 8;
emit sendResultToCheck();
}
else if (matchMethod == "Image Processing")
{
QElapsedTimer timer;
timer.start();
QMap<QString, double> testDists = fdTester->getTestDistance(frameToTest);
QMapIterator<QString, double> i(testDists);
QString bestMatchName = "None";
double minDist = 9999.99;
qDebug() << "Total time:" << timer.elapsed() << "ms";
qDebug() << "------------------------------------------------";
while (i.hasNext())
{
i.next();
if (i.value() <= minDist)
{
minDist = i.value();
bestMatchName = i.key();
}/*
if (minDist > 0.999)
{
bestMatchName = "None";
}*/
ui->labelMatchResult->setStyleSheet("color: blue; font: 20pt; background-color: white;");
ui->labelMatchResult->setText(bestMatchName);
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
+ " " + i.key() + ": " + QString::number(i.value()));
}
}
}
void MainWindow::on_actionCalibration_triggered()
{
calibratorForm = new CalibratorForm(this);
calibratorForm->show();
connect(calibratorForm, SIGNAL(sendFrameRequest()), this, SLOT(receiveFrameRequest()));
connect(this, SIGNAL(sendFrameToCalibrator(cv::Mat)), calibratorForm, SLOT(receiveFrame(cv::Mat)));
connect(calibratorForm, SIGNAL(sendFrameToShow(cv::Mat)), this, SLOT(receiveRawFrame(cv::Mat)));
connect(calibratorForm, SIGNAL(sendUpdateConfig()), this, SLOT(receiveReadCaliConf()));
connect(calibratorForm, SIGNAL(sendCaliCommand(QString)), this->ui->labelShowFrame, SLOT(receiveCaliCommand(QString)));
connect(this->ui->labelShowFrame, SIGNAL(sendMousePressedPosition(QPoint&)), calibratorForm, SLOT(receiveMousePressedPosition(QPoint&)));
}
void MainWindow::on_pushButtonMeasure_clicked()
{
emit sendFrameToMeasurement(cvRawFrameCopy);
}
void MainWindow::on_actionTrigger_triggered()
{
triggerForm->show();
connect(triggerForm, SIGNAL(sendTrigger()), this, SLOT(receiveTrigger()));
}
void MainWindow::receiveTrigger()
{
objectPresentNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 1, QOpcUa::UInt16);
qDebug() << "Part Present by trigger.";
ui->lineEditVisionResult->clear();
ui->labelShowPoCRes->clear();
// NOT GOOD, main thread will stop
usleep(2000000); // 2s
on_pushButtonCapture_clicked();
on_pushButtonMeasure_clicked();
}
void MainWindow::on_actionModbus_triggered()
{
//modbusTest->show();
}
void MainWindow::on_actionMQTT_triggered()
{
//mqttTest->show();
}
void MainWindow::on_actionRFID_triggered()
{
//rfidTest->show();
}
void MainWindow::on_actionOPC_UA_triggered()
{
//opcuaTest->show();
}
void MainWindow::receiveResultToCheck()
{
if (currentLength != 0.0)
{
if (!isColorOK)
{
visionResult = 10; // color wrong
ui->lineEditVisionResult->setText(QString::number(visionResult));
isResultReady = true;
emit sendStatusToWriteResult();
}
else if (isColorOK)
{
if (abs(currentLength-standardLengh) < 3.0)
{
visionResult = 8; // ok
ui->lineEditVisionResult->setText(QString::number(visionResult));
isResultReady = true;
emit sendStatusToWriteResult();
}
else
{
visionResult = 9; // size wrong
ui->lineEditVisionResult->setText(QString::number(visionResult));
isResultReady = true;
emit sendStatusToWriteResult();
}
}
currentLength = 0.0;
isColorOK = true;
}
}
void MainWindow::receiveStatusToWriteResult()
{
if (isMachineReady && isResultReady)
{
if (visionResult == 8)
{
ui->labelShowPoCRes->setText("Pass");
}
else if (visionResult == 9)
{
ui->labelShowPoCRes->setText("Size Fail");
}
else if (visionResult == 10)
{
ui->labelShowPoCRes->setText("Color Fail");
}
visionResultNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, visionResult, QOpcUa::UInt16);
isResultReady = false;
}
}
void MainWindow::opcuaConnected()
{
visionStatusNodeW = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.vision.VISION_STATUS"); // uint 16
// temp using
visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 1, QOpcUa::UInt16);
visionStatusNodeW->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters(100));
connect(visionStatusNodeW, &QOpcUaNode::attributeUpdated, this, [this](QOpcUa::NodeAttribute attr, const QVariant &value)
{
Q_UNUSED(attr);
if (value.toInt() != 1)
{
visionStatusNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 1, QOpcUa::UInt16);
}
});
visionResultNodeW = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.vision.RESULT"); // uint 16
objectPresentNodeW = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.objectPresent"); // uint 16
resultReadNodeRW = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.vision.RESULT_READ"); // uint 16
resultReadNodeRW->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters(100));
connect(resultReadNodeRW, &QOpcUaNode::attributeUpdated, this, [this](QOpcUa::NodeAttribute attr, const QVariant &value)
{
Q_UNUSED(attr);
qDebug() << "Read result-read status node:" << value.toInt();
if (value.toInt() == 1)
{
visionResultNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 0, QOpcUa::UInt16);
resultReadNodeRW->writeAttribute(QOpcUa::NodeAttribute::Value, 0, QOpcUa::UInt16);
}
});
machinePLCReadyNodeRW = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.vision.MACHINE_READY"); // unit 16
machinePLCReadyNodeRW->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters(100));
connect(machinePLCReadyNodeRW, &QOpcUaNode::attributeUpdated, this, [this](QOpcUa::NodeAttribute attr, const QVariant &value)
{
Q_UNUSED(attr);
qDebug() << "Read PLC-Machine-Ready status node:" << value.toInt();
if (value.toInt() == 0)
{
isMachineReady = false;
}
else if (value.toInt() == 1)
{
isMachineReady = true;
emit sendStatusToWriteResult();
}
});
jobModelNodeR = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.job.job_Model"); // string
jobModelNodeR->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters(100));
connect(jobModelNodeR, &QOpcUaNode::attributeUpdated, this, [this](QOpcUa::NodeAttribute attr, const QVariant &value)
{
Q_UNUSED(attr);
qDebug() << "Read job-model infor node:" << value.toString();
standardModel = value.toString();
});
jobLengthNodeR = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.job.job_Length"); // int 16
jobLengthNodeR->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters(100));
connect(jobLengthNodeR, &QOpcUaNode::attributeUpdated, this, [this](QOpcUa::NodeAttribute attr, const QVariant &value)
{
Q_UNUSED(attr);
qDebug() << "Read job-length node:" << value.toInt();
standardLengh = double(value.toInt());
});
jobColorNodeR = opcuaClient->node("ns=2;s=|var|CPS-PCS341MB-DS1.Application.GVL.OPC_Machine_A0001.job.job_Color"); // string
jobColorNodeR->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters(100));
connect(jobColorNodeR, &QOpcUaNode::attributeUpdated, this, [this](QOpcUa::NodeAttribute attr, const QVariant &value)
{
Q_UNUSED(attr);
qDebug() << "Read job-color status node:" << value.toString();
standardColor = value.toString();
});
}
void MainWindow::opcuaDisconnected()
{
isOpcUaConnected = false;
opcuaClient->deleteLater();
}
void MainWindow::opcuaClientError(QOpcUaClient::ClientError error)
{
qDebug() << "OPCUA Client Error:" << error;
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")
+ "OPCUA Client Error: " + error);
}
void MainWindow::opcuaClientState(QOpcUaClient::ClientState state)
{
if (state == QOpcUaClient::ClientState::Connected)
{
qDebug() << "Connected to OPCUA server.";
isOpcUaConnected = true;
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")
+ "Connected to OPCUA server.");
}
else if (state == QOpcUaClient::ClientState::Connecting)
{
qDebug() << "Trying to connect OPCUA server now.";
isOpcUaConnected = false;
}
else if (state == QOpcUaClient::ClientState::Disconnected)
{
qDebug() << "Disconnected to OPCUA server.";
isOpcUaConnected = false;
ui->listWidgetMessageLog->addItem("[Info] " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ")
+ "Disconnected to OPCUA server.");
}
}
void MainWindow::on_pushButtonVisionResultReady_clicked()
{
visionResult = ui->lineEditVisionResult->text().toInt();
// temp using
colorStatus = visionResult;
isResultReady = true;
emit sendStatusToWriteResult();
qDebug() << "visionResult:" << visionResult;
}
void MainWindow::on_pushButtonPartPresent_clicked()
{
objectPresentNodeW->writeAttribute(QOpcUa::NodeAttribute::Value, 1, QOpcUa::UInt16);
qDebug() << "Part Present Manually.";
}