-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
409 lines (366 loc) · 15.2 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPen>
#include <QFont>
//-------------------------------UART COMMUNICATION--------------------------------------------------
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <wiringPi.h> //Used for GPIO
#include <sys/time.h>
int version = 3;
int uart0_filestream = -1;
unsigned char accumulatedData[256];
int commandLenght = 0;
// Variable that stores the next plate to send
unsigned char plateToSend[]={'?','?','?','?','?','?','?','?','?','?'};
// Variable that stores the last valid plate
unsigned char lastValidPlate[]={'?','?','?','?','?','?','?','?','?','?'};
// Variable that stores the last plate sent in UART
unsigned char lastSentPlate[]={'?','?','?','?','?','?','?','?','?','?'};
int plateCooldown = 30;
int validationFactor = 1;
int validationCounter = 0;
unsigned char address = 0;
long int msLastSecond = 0;
long int previousPlatesTimes[30];
unsigned char previousPlates[30][10];
int previousPlateCounter = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//-------------------------------UART COMMUNICATION--------------------------------------------------
uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B19200 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
plateToSend[0] = 0;
wiringPiSetupPhys();
pinMode(31, INPUT);
pinMode(33, INPUT);
pinMode(35, INPUT);
pinMode(37, INPUT);
struct timeval tp;
gettimeofday(&tp, NULL);
msLastSecond = tp.tv_sec * 1000 + tp.tv_usec / 1000;
//---------------------------------------------------------------------------
ui->setupUi(this);
qPlayer = new QPlayer();
QObject::connect( qPlayer,
SIGNAL(processedImage(QImage)),
this,
SLOT(updatePlayerUI(QImage)),
Qt::DirectConnection);
qPlayer->loadCamera(0);
memset(&lprResult, 0, sizeof(CARPLATE_DATA));
if(qPlayer->isCameraOpened())
{
for(int i = 0; i < THREAD_COUNT; i++){
qDetector[i] = new QDetector(qPlayer);
QObject::connect( qDetector[i],
SIGNAL(sendLPRResult(CARPLATE_DATA)),
this,
SLOT(setLPRResult(CARPLATE_DATA)),
Qt::DirectConnection);
}
}
}
MainWindow::~MainWindow()
{
if(qPlayer)
delete qPlayer;
for(int i = 0; i < THREAD_COUNT; i++){
if(qDetector[i])
{
qDetector[i]->stop_detection();
delete qDetector[i];
}
}
delete ui;
}
void MainWindow::updatePlayerUI(QImage qImg)
{
//-------------------------------UART COMMUNICATION----------------------------------------------------
address = digitalRead(31);
address += digitalRead(33)*2;
address += digitalRead(35)*4;
address += digitalRead(37)*8;
//printf("ADDRESS: %i\n", address);
if (uart0_filestream != -1)
{
// Read up to 255 characters from the port if they are there
unsigned char rx_buffer[256];
int rx_length = read(uart0_filestream, (void*)rx_buffer, 255); //Filestream, buffer to store in, number of bytes to read (max)
if (rx_length < 0)
{
//An error occured (will occur if there are no bytes)
}
else if (rx_length == 0)
{
//No data waiting
}
else
{
//Bytes received
rx_buffer[rx_length] = '\0';
//printf("%i bytes read : %s\n", rx_length, rx_buffer);
for(int i=0;i<rx_length;i++){
accumulatedData[commandLenght] = rx_buffer[i];
commandLenght += 1;
if(commandLenght > 256){
commandLenght = 256;
}
if(rx_buffer[i]==0x7D){
//printf("\nFINAL ENCONTRADO\n");
unsigned char command[commandLenght];
for(int i=0;i<commandLenght;i++){
command[i] = accumulatedData[i];
}
if(command[1]==0x5B && command[2] == address && command[commandLenght-2] == calculateBCC(&command[0], commandLenght-2)){
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
if(command[0] == 0x03){
*p_tx_buffer++ = 0x04;
*p_tx_buffer++ = 0x5D;
*p_tx_buffer++ = 0x5B;
*p_tx_buffer++ = address;
*p_tx_buffer++ = calculateBCC(&tx_buffer[0], 4);
*p_tx_buffer++ = 0x7D;
}else if(command[0] == 0x05){
if(plateToSend[0] != 63){
printf("PLATE SENT");
printf("\n");
char length = 0;
for(int i=0;i<10;i++){
if(plateToSend[i] != 63){
length++;
}else{
break;
}
}
printf("%i largo\n", length);
// Construct message to respond with plate number
*p_tx_buffer++ = 0x06;
*p_tx_buffer++ = 0x5D;
*p_tx_buffer++ = 0x5B;
*p_tx_buffer++ = address;
*p_tx_buffer++ = length;
for(int i = 0; i<length;i++){
*p_tx_buffer++ = plateToSend[i];
}
*p_tx_buffer++ = calculateBCC(&tx_buffer[0], length+5);
*p_tx_buffer++ = 0x7D;
cleanPlatesAfterSend();
}else{
*p_tx_buffer++ = 0x0B;
*p_tx_buffer++ = 0x5D;
*p_tx_buffer++ = 0x5B;
*p_tx_buffer++ = address;
*p_tx_buffer++ = version;
*p_tx_buffer++ = calculateBCC(&tx_buffer[0], 5);
*p_tx_buffer++ = 0x7D;
}
}else if(command[0] == 0x1A){
plateCooldown = command[3];
previousPlateCounter = 0;
printf("PLATE COOLDOWN CHANGED: %i\n", plateCooldown);
*p_tx_buffer++ = 0x0E;
*p_tx_buffer++ = 0x5D;
*p_tx_buffer++ = 0x00;
*p_tx_buffer++ = 0x58;
*p_tx_buffer++ = calculateBCC(&tx_buffer[0], 4);
*p_tx_buffer++ = 0x7D;
}else if(command[0] == 0x09){
validationFactor = command[3];
printf("VALIDATION FACTOR CHANGED: %i\n", validationFactor);
*p_tx_buffer++ = 0x0E;
*p_tx_buffer++ = 0x5D;
*p_tx_buffer++ = 0x00;
*p_tx_buffer++ = 0x58;
*p_tx_buffer++ = calculateBCC(&tx_buffer[0], 4);
*p_tx_buffer++ = 0x7D;
}
if (uart0_filestream != -1)
{
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (count < 0)
{
printf("UART TX error\n");
}
printf("LENGTH SENT: %i\n", tx_buffer[5]);
}
//printf("BCC: %i\n", calculateBCC(&tx_buffer[0], 4));
//printf("ADDRESS: %i\n", address);
//printf("COMMAND: %i\n", command[2]);
}
//printf("%i bytes read : %s\n", commandLenght, command);
commandLenght = 0;
}
}
}
}
//------------------------------------------------------------------------------------------
m_Mutex.lock();
QFont m_serifFont("MS Serif", 25, QFont::Bold);
QPainter qPainter(&qImg);
qPainter.setFont(m_serifFont);
for(int i = 0; i < lprResult.nPlate; i++){
#if 1
QPen m_penPlate(Qt::red);
m_penPlate.setWidth(3);
QPen m_penText(Qt::blue);
qPainter.setPen(m_penPlate);
qPainter.drawRect(lprResult.pPlate[i].rtPlate.left, lprResult.pPlate[i].rtPlate.top,
lprResult.pPlate[i].rtPlate.right - lprResult.pPlate[i].rtPlate.left, lprResult.pPlate[i].rtPlate.bottom - lprResult.pPlate[i].rtPlate.top);
qPainter.setPen(m_penText);
QString strCarNumber(pPlate[i].szLicense);
//-------------------------------UART COMMUNICATION--------------------------------------------------
unsigned char tempPlate[]={'?','?','?','?','?','?','?','?','?','?'};
memcpy(tempPlate, strCarNumber.toStdString().c_str(), strCarNumber.size());
validatePlate(&tempPlate[0], lprResult.pPlate[i].nTrust);
//----------------------------------------------------------------------------------------------------
QString strConf;
strConf.sprintf("-[Conf:%.2f]", lprResult.pPlate[i].nTrust);
strCarNumber += strConf + QString("%");
QRectF rt(lprResult.pPlate[i].rtPlate.left, lprResult.pPlate[i].rtPlate.bottom, 500, 35);
qPainter.fillRect(rt, Qt::yellow);
qPainter.drawText(lprResult.pPlate[i].rtPlate.left, lprResult.pPlate[i].rtPlate.bottom + 30, strCarNumber);
#endif
}
ui->videoView->setPixmap(QPixmap::fromImage(qImg).scaled(ui->videoView->size(),
Qt::KeepAspectRatio, Qt::FastTransformation));
m_Mutex.unlock();
}
void MainWindow::setLPRResult(CARPLATE_DATA lResult)
{
memcpy(&lprResult, &lResult, sizeof(CARPLATE_DATA));
}
void MainWindow::on_openButton_pressed()
{
bool suc = qPlayer->loadCamera(0);
if(qPlayer->isCameraOpened() && suc)
{
for(int i = 0; i < THREAD_COUNT; i++){
qDetector[i] = new QDetector(qPlayer);
QObject::connect( qDetector[i],
SIGNAL(sendLPRResult(CARPLATE_DATA)),
this,
SLOT(setLPRResult(CARPLATE_DATA)),
Qt::DirectConnection);
}
}
}
void MainWindow::on_closeButton_pressed()
{
// if(qPlayer)
// delete qPlayer;
// for(int i = 0; i < THREAD_COUNT; i++){
// if(qDetector[i])
// delete qDetector[i];
// }
QApplication::quit();
}
//-------------------------------UART COMMUNICATION--------------------------------------------------
unsigned char MainWindow::calculateBCC(unsigned char* pointer, int lenght){
unsigned char bcc = 0;
for(int i=0;i<lenght;i++){
if(i==0){
bcc = *pointer;
}else{
bcc = bcc^*pointer;
}
pointer++;
}
return bcc;
}
void MainWindow::validatePlate(unsigned char* newPlatePointer, float conf){
if(conf<90){
//printf("Plate ignored %s, confidence: %f\n", newPlatePointer, conf);
return;
}else{
//printf("\n");
//printf("PLACA QUE SUPUESTAMENTE ESTA BIEN: %s\n", newPlatePointer);
//printf("\n");
}
unsigned char newPlate[10];
for(int i=0; i<10; i++){
newPlate[i] = *newPlatePointer;
newPlatePointer++;
}
if(validationCounter >= validationFactor-1){
bool sendPlate = true;
struct timeval tp;
gettimeofday(&tp, NULL);
long int msNow = tp.tv_sec * 1000 + tp.tv_usec / 1000;
for(int i=0; i<30; i++){
bool plateFound = true;
for(int j=0; j<10; j++){
if(previousPlates[i][j] != newPlate[j]){
plateFound = false;
}
}
if(plateFound && previousPlatesTimes[i] > msNow - (plateCooldown*1000)){
sendPlate = false;
//printf("COOLDOWN NOT COMPLETED\n");
break;
}
}
if(sendPlate){
previousPlateCounter++;
if(previousPlateCounter >= 30){
previousPlateCounter = 0;
}
for(int k=0; k<10; k++){
plateToSend[k] = newPlate[k];
previousPlates[previousPlateCounter][k] = newPlate[k];
}
previousPlatesTimes[previousPlateCounter] = msNow;
validationCounter = 0;
lastValidPlate[0] = 0;
//printf("--------SENDING PLATE----------\n");
//printf("PLATE: %s\n", plateToSend);
//printf("-------------------------------\n");
}
}else{
if(lastValidPlate[0] == 63){
for(int k=0; k<10; k++){
lastValidPlate[k] = newPlate[k];
}
}else{
bool samePlate = true;
for(int k=0; k<10; k++){
if(lastValidPlate[k] != newPlate[k]){
samePlate = false;
break;
}
}
if(samePlate){
//AQUIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
validationCounter++;
}else{
lastValidPlate[0] = 63;
validationCounter = 0;
}
}
}
}
void MainWindow::cleanPlatesAfterSend(){
for(int i=0; i<10; i++){
lastSentPlate[i] = plateToSend[i];
plateToSend[i] = '?';
}
}
//---------------------------------------------------------------------------------------