-
Notifications
You must be signed in to change notification settings - Fork 0
/
finite_state_machine.cpp
664 lines (541 loc) · 14.9 KB
/
finite_state_machine.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
/*
* File: main.cpp
* Author: hans
*
* Created on 4 de Junho de 2015, 20:25
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <signal.h>
#include <sstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <string.h>
#include "math.h"
#include <wiringPi.h>
#include "robotAPI/Pins.h"
#include "robotAPI/Sonar.h"
#include "robotAPI/Motor.h"
#include "robotAPI/Encoder.h"
#include "robotAPI/KBAsync.h"
#include "robotAPI/Odometry.h"
#define nl "\n\r"
#define pi 3.1415
//#define DEBUG
using namespace std;
using namespace cv;
bool running;
bool hasCamera = true;
float distancia = 0;
float fdist = 20;
typedef enum {AVALIA, VERMELHO, VERDE, NENHUM} TRobotState;
typedef enum {AVANCA, ESTOURA, CONFERE, PERDEU} TAniquilaState;
typedef struct SRetornoPython {
int identificador;
float angulo;
}TRetornoPython;
Sonar sonar;
Encoder encoderL, encoderR;
Motor motorL, motorR;
float posInter[3];
//Detect ^C
void handleCTRLC(int s);
void setupCTRLCHandle();
string takePicture(VideoCapture *cap);
TRobotState updateStateRobot (int identificador);
TAniquilaState updateStateAniquila (TAniquilaState aniquilaState, VideoCapture *cap);
void getDataImg (TRetornoPython *retornoPython, VideoCapture *cap);
float to_positive_angle(float angle);
float menorAngulo(float target, float source);
void girarGraus(int grau);
void andarFrente(float distance);
void diferencial(float goal[3]);
//Funcoes para testes e debug
#ifdef DEBUG
TAniquilaState testeStateAniquile(VideoCapture *cap);
#endif
int procImagem(char tipo, VideoCapture *cap){
string ss;
ss = "kkkkkkkk";
ss = takePicture(cap);
int iLowH;
int iHighH;
int iLowS;
int iHighS;
int iLowV;
int iHighV;
Mat imgOriginal(320,240, -1);
imgOriginal = imread(ss);
if(!imgOriginal.data) {
cout << "picture nao abriu!" <<endl;
exit(0);
}
//namedWindow("Control", WINDOW_AUTOSIZE); //create a window called "Control"
if(tipo == 'G'){
iLowH = 47; // Green
iHighH = 92;
iLowS = 82;
iHighS = 255;
iLowV = 0;
iHighV = 255;
}else if(tipo == 'R'){
iLowH = 170; //Vermelho
iHighH = 179;
iLowS = 150;
iHighS = 255;
iLowV = 60;
iHighV = 255;
}
int i, j, limiarDePixels = 1000;
int countEsquerda = 0, countDireita = 0;
int retorno=0;
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
//imshow("Thresholded Image", imgThresholded); //show the thresholded image
//imshow("Original", imgOriginal); //show the original image
for(i=0; i< imgThresholded.rows; i++){
for(j=0; j<imgThresholded.cols/2; j++){
//cout << imgThresholded.at<uchar>(i,j) << endl;
if((int)imgThresholded.at<uchar>(i,j) > 200)
countEsquerda += 1;
}
}
for(i=0; i <imgThresholded.rows ; i++){
for(j=imgThresholded.cols/2; j<imgThresholded.cols; j++){
//cout << imgThresholded.at<uchar>(i,j) << endl;
if((int)imgThresholded.at<uchar>(i,j) > 200){
countDireita += 1;
}
}
}
cout << "Esquerda: " << countEsquerda << " Direita: " << countDireita << endl;
if(countEsquerda - countDireita > limiarDePixels){
//va para esquerda
//cout << "Esquerda" << endl;
retorno = 1;
}
else if(countDireita - countEsquerda > limiarDePixels){
//va para direita
//cout << "Direita" << endl;
retorno = 2;
}
else if(abs(countDireita - countEsquerda) < limiarDePixels && (countDireita > 3500 || countEsquerda > 3500)) {
//va para frente
//cout << "Frente" << endl;
retorno = 0;
}
else{
//cout << "Nao existe" << endl;
retorno = -1;
}
return retorno;
}
int *controlProcImage(VideoCapture *cap){
int *retorno;
retorno = (int*)malloc(2*sizeof(int));
int retornoVerde = procImagem('G',cap);
int retornoVermelho = procImagem('R',cap);
if(retornoVerde == 0){
retorno[0] = 1;
retorno[1] = 0;
}
else if(retornoVerde == 2 ){
retorno[0] = 1; //direita
retorno[1] = -8;
}
else if(retornoVerde == 1){
retorno[0] = 1; //esquerda
retorno[1] = 8;
}
else{
if(retornoVermelho == 2){
retorno[0] = 2;
retorno[1] = 0;
}
else{
retorno[0] = 0;
retorno[1] = 0;
}
}
return retorno;
}
int main(int argc, char** argv) {
int *retornoProcIm;
retornoProcIm = (int*)malloc(2*sizeof(int));
TRobotState currentState = AVALIA;
TAniquilaState aniquilaState = CONFERE;
running = true;
KBAsync kb;
String path;
int key;
int contador_voltas = 0;
int contador_voltas2 = 0;
//float angulo;
//int identificador;
ifstream myfile;
TRetornoPython dadosImagem;
setupCTRLCHandle();
if (PIN_MODE==PIN_BCM) {
cout << "Pins in BCM mode." << nl;
if (wiringPiSetupGpio()<0) {
cout << "Could not setup GPIO pins" << nl;
return -1;
}
} else {
cout << "Pins in wiringPi mode." << nl;
wiringPiSetup();
}
encoderL.setup(ENCODER_LEFT, LEFT_SIDE);
encoderR.setup(ENCODER_RIGHT, RIGHT_SIDE);
sonar.setup(SONAR_TRIGGER, SONAR_ECHO);
motorL.setup(MOTOR_LEFT_A, MOTOR_LEFT_B, MOTOR_LEFT_E, &encoderL);
motorR.setup(MOTOR_RIGHT_A, MOTOR_RIGHT_B, MOTOR_RIGHT_E, &encoderR);
motorL.setK(1);
motorR.setK(1);
VideoCapture cap(0); // open the video camera no. 0
if (cap.isOpened()) {
hasCamera = true;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Camera frame size : " << dWidth << " x " << dHeight << nl;
} else {
cout << "Cannot open the video cam" << nl;
hasCamera = false;
}
//Caso nao esteja em modo debug, executa o codigo correto
#ifndef DEBUG
while ((key=kb.getKey())!='q') {
//cout << "\r" << flush << "Distance:" << fdist << "cm" << nl;
switch (currentState) {
case AVALIA: //tira foto do ambiente ao redor e
cout << "AVALIA" << nl; //define proxima acao
retornoProcIm = controlProcImage(&cap);
cout << "Retorno Proc Im ID: " << retornoProcIm[0] << "Retorno Proc Im ANGLE: " << retornoProcIm[1] << nl;
currentState = updateStateRobot (retornoProcIm[0]); //a partir do angulo, define a próxima acao
break;
case NENHUM: // balao a vista.
cout << "NENHUM" << nl;
girarGraus(45);
cout << "GIROU 45 GRAUS" << nl;
if(contador_voltas2 > 7){
cout << "Saindo do lugar\ndistancia do sonar: " << sonar.measureDistance() << nl;
if(sonar.measureDistance() > 35){
posInter[0] = 40;
posInter[1] = 0;
posInter[2] = 0;
diferencial(posInter);
contador_voltas2=0;
}
}
currentState = AVALIA;
break;
case VERDE: //apenas um balao verde, ataca.
cout << "\nVISUALIZOU O VERDE\n" << nl;
while (aniquilaState != PERDEU){
cout << "\nANIQUILA STATE\n" << nl;
aniquilaState = updateStateAniquila (aniquilaState,&cap);
}
currentState = AVALIA; //nao ve mais verde, retorna para procurar
aniquilaState = CONFERE; //reinicia a maquina de estados
break;
case VERMELHO: //so baloes vermelhos, gira.
cout << "VERMELHO" << nl;
girarGraus(45);
if(contador_voltas > 7){
cout << "Saindo do lugar\ndistancia do sonar: " << sonar.measureDistance() << nl;
if(sonar.measureDistance() > 35){
posInter[0] = 45;
posInter[1] = 0;
posInter[2] = 0;
diferencial(posInter);
contador_voltas=0;
}
}
contador_voltas++;
currentState = AVALIA;
break;
}
motorL.controlSpeed();
motorR.controlSpeed();
delayMicroseconds(100000);
}
motorL.stop();
motorR.stop();
cout << "\r\nExiting...\r\n";
#endif
//Para testes e debug
#ifdef DEBUG
TAniquilaState estado;
estado = testeStateAniquile(&cap);
if (estado == CONFERE) cout << "\r\nConfere\r\n";
else cout << "\r\nOutro\r\n";
#endif
return 0;
}
//Funcoes para debug e testes
#ifdef DEBUG
TAniquilaState testeStateAniquile(VideoCapture *cap){
TRetornoPython pyData;
int *retornoProcIm;
retornoProcIm = controlProcImage();
TAniquilaState state = CONFERE;
//getDataImg(&pyData,cap);
cout << pyData.angulo;
cout << "\r\n";
cout << pyData.identificador;
cout << "\r\n";
float distance = sonar.measureDistance();
cout << distance << nl;
return state;
}
#endif
float to_positive_angle(float angle)
{
angle = fmod(angle, 2*pi);
while(angle < 0)
angle = angle + 2*pi;
return angle;
}
float menorAngulo(float target, float source)
{
float a = to_positive_angle(target) - to_positive_angle(source);
if (a > pi)
a = a - 2*pi;
else if (a < -pi)
a = a + 2*pi;
return a;
}
void diferencial(float goal[3])
{
KBAsync kb;
int state = 0;
float pos[3] = {0,0,0};
//float goal[3] = {-150,-40,4.71};
cout << "x: " << goal[0] << "y: " << goal[1] << nl;
float k_rho = 0.2;
float k_alpha = 1.7;
float k_beta = -0.8;
int key;
float l = 7.25;
float rLeft = 3.30;
float rRight = 3.30;
float k = 1.3;
float velLeft = 10;
float velRigth = 10;
float minPower = 10;
Encoder encoderL, encoderR;
Motor motorL, motorR;
encoderL.setup(ENCODER_LEFT, LEFT_SIDE);
encoderR.setup(ENCODER_RIGHT, RIGHT_SIDE);
motorL.setup(MOTOR_LEFT_A, MOTOR_LEFT_B, MOTOR_LEFT_E, &encoderL);
motorR.setup(MOTOR_RIGHT_A, MOTOR_RIGHT_B, MOTOR_RIGHT_E, &encoderR);
Odometry odometry(l, rLeft, rRight, &encoderL, &encoderR);
motorL.setK(k);
motorR.setK(k);
motorL.setMinPower(minPower);
motorR.setMinPower(minPower);
int contador = 0;
while ((key=kb.getKey())!='q') {
if(contador <10){
k_rho=0.1;
}
else{
k_rho=0.18;
}
contador++;
delayMicroseconds(100000);
float dx = goal[0] - pos[0];
float dy = goal[1] - pos[1];
float theta = pos[2];
float dtheta = goal[2] - pos[2];
float rho = sqrt(dx*dx + dy*dy);
float atg = odometry.to180range(atan2(dy,dx));
float alpha = odometry.to180range(menorAngulo(atg,theta));
float beta = odometry.to180range(goal[2]-pos[2] - alpha);
float v = k_rho*rho;
float omega = k_alpha*alpha + k_beta*beta;
float wR = 2*v + l*omega;
float wL = wR - 2 *l*omega;
float velLeft = wL/rLeft;
float velRight = wR/rRight;
if (rho<15 || state>0){
if (abs(dtheta) > 0.15 && state != 2){
state = 1;
if(dtheta>=0){
motorL.setPower(-(3.8)*minPower);
motorR.setPower((3.8)*minPower);
}
else{
motorL.setPower((3.8)*minPower);
motorR.setPower(-(3.8)*minPower);
}
}
else{
velRight = 0;
velLeft = 0;
state = 2;
motorL.setTargetSpeed(velLeft);
motorR.setTargetSpeed(velRight);
break;
}
}
//cout << "Iteracao: " << contador << " rho: " << rho << " dtheta: "<<dtheta<< nl;
//cout <<"State: "<< state << " VelLeft: " << velLeft << " VelRight: " << velRight << nl;
odometry.updatePosition(pos);
motorL.setTargetSpeed(velLeft);
motorR.setTargetSpeed(velRight);
motorL.controlSpeed();
motorR.controlSpeed();
//cout << " x: " << pos[0] << " y: " << pos[1] << " theta: " << pos[2] << nl;//*180/M_PI << nl;
}
motorL.stop();
motorR.stop();
//std::cout << nl << "Exiting..." << nl;
}
void girarGraus(int grau)
{
float pos[3];
float radiano = grau*pi/180.0;
pos[0] = 0;
pos[1] = 0;
pos[2] = radiano;
cout << "vai girar" << nl;
diferencial(pos);
}
void getDataImg (TRetornoPython *retornoPython, VideoCapture *cap){
/*
ifstream myfile;
std::string filename = "/home/pi/crobot1/ImagensRobotica-2.py";
std::string cmd = "python ";
cmd += filename;
takePicture(cap); //tira foto
system (cmd.c_str()); //executa python para analisar a imagem
myfile.open ("angulos.txt"); //abre arquivo gerado pelo py
if (myfile.is_open()){ //se arquivo aberto sem problemas
myfile >> retornoPython->angulo; //le o valor do angulo
myfile >> retornoPython->identificador ; //indica qual a cor encontrada
//que sera usada para definir proxima acao
} //
myfile.close(); //fecha arquivo
*/
cout << nl << "Angulo: " << nl;
cin >> retornoPython->angulo;
cout << nl << "ID: " << nl;
cin >> retornoPython->identificador;
}
#define ANGULO_MINIMO 7
TAniquilaState updateStateAniquila (TAniquilaState aniquilaState,VideoCapture *cap){
TRetornoPython pyData;
TAniquilaState newState;
newState = aniquilaState;
int *retornoProcIm;
retornoProcIm = (int*)malloc(2*sizeof(int));
float filterWeight = 0.6;
//cout << "\r" << flush << "Distance:" << fdist << "cm" << nl;
switch (newState){
case CONFERE:
cout << "CONFERINDO" << nl;
//getDataImg(&pyData,cap);
retornoProcIm = controlProcImage(cap);
cout << "Retorno Proc Im ID: " << retornoProcIm[0] << "Retorno Proc Im ANGLE: " << retornoProcIm[1] << endl;
if (abs(retornoProcIm[1]) < ANGULO_MINIMO && retornoProcIm[0] /*pyData.identificador*/ == 1){
distancia = sonar.measureDistance();
fdist = filterWeight*fdist + (1-filterWeight)*distancia;
cout << "distancia sonar: " << fdist << nl;
if(fdist > 20){
newState = AVANCA;
}
else {
newState = ESTOURA;
}
}
else{
if (abs(retornoProcIm[1]) >= ANGULO_MINIMO) {
girarGraus(retornoProcIm[1]);
}
newState = PERDEU;
}
break;
case AVANCA:
//avanca rapido ~10cm
posInter[0] = 35;
posInter[1] = 0;
posInter[2] = 0;
diferencial(posInter);
newState = CONFERE;
cout << "AVANCA" << nl;
break;
case ESTOURA:
//avanca lentamente ~1cm
posInter[0] = 25;
posInter[1] = 0;
posInter[2] = 0;
diferencial(posInter);
newState = CONFERE;
cout << "ESTOURA" << nl;
break;
case PERDEU:
newState = PERDEU;
cout << "PERDEU" << nl;
break;
}
return newState;
}
TRobotState updateStateRobot (int identificador){
TRobotState newState;
switch (identificador){
case 1: //apenas um balao verde
newState = VERDE;
break;
case 2: //apenas vermelhos
newState = VERMELHO;
break;
default:
newState = NENHUM;
break;
}
return newState;
}
#define OPENCV_BUFFER_SIZE 5
string takePicture(VideoCapture *cap) {
string retorno;
stringstream ss;
if (hasCamera) {
static int pictureCount = 1;
//cout << "\rTaking picture " << pictureCount << "...";
ss << pictureCount << ".png";
Mat frame;
bool bSuccess = true;
//Clear the video buffer in order to get the a recent frame
//only required for exporadic captures
for (int i=0; i<OPENCV_BUFFER_SIZE && bSuccess; i++)
bSuccess = cap->read(frame);
// read a new frame from video
bSuccess = cap->read(frame);
//now "frame" stores our image, writ it to the file.
if (bSuccess && imwrite(ss.str(), frame)){
cout << "ok." << nl;
//cout << "path: " << ss.str() << nl;
pictureCount++;
} else
cout << "error." << nl;
}
retorno = ss.str();
return retorno;
}
//Detect ^C
void handleCTRLC(int s) {
running=false;
}
void setupCTRLCHandle() {
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = handleCTRLC;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
}