-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheWatcher.cpp
156 lines (138 loc) · 3.59 KB
/
TheWatcher.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
/*
Alan Aceves : a325103@uach.mx <?> alan.aceves.chavez@gmail.com
Daniel Rascón : a329625@uach.mx <?>
Brandon Saldivar : bsaldivar205@gmail.com <?>
*/
#include <iostream>
#include <Stepper.h>
#include "arduinoFFT.h" //.1 Standard Arduino FFT library https://github.com/kosme/arduinoFFT
using namespace std;
//ULN20003 Motor Driver Pins
#define IN1 19
#define IN2 18
#define IN3 5
#define IN4 17
///GPIO INPUT
#define right 34
#define left 32
#define front 35
arduinoFFT FFT = arduinoFFT(); //initializing FFT
int i ;
const int stepsPerRevolution = 2048;
int pos = 0;
//initialize the stepper library
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
const int SAMPLE = 1024; // Sampling period 128 bytes = 1024 bits
const int tolerance= 2000; // 1500 to detect low noise :: 2600 to cancel low noise
//Real number
double vRight [SAMPLE];
double vLeft [SAMPLE]; // use this variables for the microphones comparator
double vFront [SAMPLE];
//Imaginary number
double iRight [SAMPLE];
double iLeft [SAMPLE]; // only usingned on the Fast Fourier Transform
double iFront [SAMPLE];
////Data capture in the Array
void readMicrophones (){
for (int i = 0 ; i < SAMPLE ; i++){
vRight[i] = analogRead(right);
iRight[i] = 0;
vLeft[i] = analogRead(left);
iLeft[i] = 0 ;
vFront[i] = analogRead(front);
iFront[i] = 0 ;
}
}
/// Motor function to move Front
void mover_Norte() {
cout << "Moviendo a Norte" << endl; //.3
if (pos != 0){
switch(pos){
case 1:
myStepper.step(-512);
break;
case -1:
myStepper.step(512);
break;
}
pos = 0;
}
}
/// Motor function to move Left
void mover_Este() {
cout << "Moviendo a Este" << endl; //.3
if (pos != -1){
switch(pos){
case 0:
myStepper.step(-512);
break;
case 1:
myStepper.step(-1024);
break;
}
pos = -1;
}
}
/// Motor function to move Right
void mover_Oeste() {
cout << "Moviendo a Oeste" << endl; //.3
if (pos != 1){
switch(pos){
case 0:
myStepper.step(512);
break;
case -1:
myStepper.step(1024);
break;
}
pos = 1;
}
}
void setup() {
myStepper.setSpeed(15);
Serial.begin(115200);
}
void loop() {
//// Reading Signals
readMicrophones();
////.1 Fast Fourier Transform Right
fourierRight();
////.1 Fast Fourier Transform Left
fourierLeft();
////.1 Fast Fourier Transform Front
fourierFront();
//// MASTER COMPARATOR
//.3 For better performance remove all the couts
for (i = 0 ; i < 512 ; i++){
if ( vRight [i] > tolerance && i > 1 ){
mover_Oeste();
//cout << vRight[i] << " Right" << endl; //.3
}
if ( vLeft [i] > tolerance && i > 1 ){
mover_Este();
// cout << vLeft[i] << " Left" << endl; //.3
}
if ( vFront [i] > tolerance && i > 1 ){
mover_Norte();
//cout << vFront[i] << " Front" << endl; //.3
}
}
}
//.1
void fourierRight(){
FFT.Windowing (vRight, SAMPLE, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute (vRight, iRight, SAMPLE, FFT_FORWARD);
FFT.ComplexToMagnitude (vRight, iRight, SAMPLE);
}
//.1
void fourierLeft(){
FFT.Windowing (vLeft, SAMPLE, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute (vLeft, iLeft, SAMPLE, FFT_FORWARD);
FFT.ComplexToMagnitude (vLeft, iLeft, SAMPLE);
}
//.1
void fourierFront(){
FFT.Windowing (vFront, SAMPLE, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute (vFront, iFront, SAMPLE, FFT_FORWARD);
FFT.ComplexToMagnitude (vFront, iFront, SAMPLE);
}