Dezibot 4
Loading...
Searching...
No Matches
FrequencyFindAFriend.ino
Go to the documentation of this file.
1#include <Dezibot.h>
2#include <arduinoFFT.h>
3#include <rom/ets_sys.h>
4const uint16_t samples = 256; //This value MUST ALWAYS be a power of 2
5const int centeredThreshold = 50 ;
6//const float signalFrequency = 1000;
7const float samplingFrequency = 4000;
8float vReal[4][samples];
9float vImag[4][samples];
10#define SCL_INDEX 0x00
11#define SCL_TIME 0x01
12#define SCL_FREQUENCY 0x02
13#define SCL_PLOT 0x03
14
15ArduinoFFT<float> FFT = ArduinoFFT<float>(vReal[0], vImag[0], samples, samplingFrequency); /* Create FFT object */
16
17Dezibot dezibot = Dezibot();
18void setup() {
19 dezibot.begin();
20 Serial.begin(115200);
21 //dezibot.infraredLight.front.turnOn();
22 //dezibot.infraredLight.bottom.turnOn();
23}
24
25void loop() {
26 portDISABLE_INTERRUPTS();
27 for(int i = 0; i < samples; i++){
28 vReal[0][i] = dezibot.lightDetection.getValue(IR_FRONT);
29 vImag[0][i] = 0.0;
30 vReal[1][i] = dezibot.lightDetection.getValue(IR_LEFT);
31 vImag[1][i] = 0.0;
32 vReal[2][i] = dezibot.lightDetection.getValue(IR_RIGHT);
33 vImag[2][i] = 0.0;
34 vReal[3][i] = dezibot.lightDetection.getValue(IR_BACK);
35 vImag[3][i] = 0.0;
36 ets_delay_us(125);
37 }
38
39 portENABLE_INTERRUPTS();
40 //PrintVector(vReal, (samples>>1), 0);
41
42 //PrintVector(vReal, (samples>>1), 0);
43 float frequency[4];
44 float magnitude[4];
45 for(int index = 0; index <4; index++){
46 FFT.setArrays(vReal[index], vImag[index]);
47 FFT.windowing(FFTWindow::Rectangle, FFTDirection::Forward); /* Weigh data */
48 FFT.compute(FFTDirection::Forward); /* Compute FFT */
49 FFT.complexToMagnitude(); /* Compute magnitudes */
50 FFT.majorPeak(&frequency[index],&magnitude[index]);
51 if(abs(frequency[index]-1147)>10){
52 magnitude[index] = 0;
53 }
54 Serial.print(index);
55 Serial.print(":");
56 Serial.print(frequency[index]);
57 Serial.print(",");
58 Serial.print(index+4);
59 Serial.print(":");
60 Serial.print(magnitude[index]);
61 if(index < 3){
62 Serial.print(",");
63 }
64 Serial.println();
65 }
66
67//================================================
68float leftValue = magnitude[1];
69float rightValue = magnitude[2];
70switch(brightest(magnitude)){
71 case IR_FRONT:
72 //correct Stearing to be centered
73 if( abs(leftValue-rightValue)
74 < centeredThreshold){
75 dezibot.motion.move();
76 }else{
77 if (leftValue > rightValue){
78 dezibot.motion.rotateAntiClockwise();
79 } else{
80 dezibot.motion.rotateClockwise();
81 }
82 }
83 dezibot.multiColorLight.setTopLeds(BLUE);
84 break;
85 case IR_LEFT:
86 dezibot.motion.rotateAntiClockwise();
87 dezibot.multiColorLight.setTopLeds(RED);
88 break;
89 case IR_RIGHT:
90 dezibot.motion.rotateClockwise();
91 dezibot.multiColorLight.setTopLeds(GREEN);
92 break;
93 case IR_BACK:
94 if(leftValue > rightValue){
95 dezibot.motion.rotateAntiClockwise();
96 } else {
97 dezibot.motion.rotateClockwise();
98 }
99 dezibot.multiColorLight.setTopLeds(YELLOW);
100 break;
101 }
102
103
104
105
106
107
108}
109
110photoTransistors brightest(float *magnitudes){
111 int pos;
112 float maxMagnitude = 0;
113 for(int index = 0; index <4; index++){
114 if (magnitudes[index] > maxMagnitude){
115 pos = index;
116 maxMagnitude = magnitudes[index];
117 }
118 }
119 switch (pos) {
120 case 0:
121 return IR_FRONT;
122 case 1:
123 return IR_LEFT;
124 case 2:
125 return IR_RIGHT;
126 case 3:
127 return IR_BACK;
128 }
129}
130
131void PrintVector(float *vData, uint16_t bufferSize, uint8_t scaleType)
132{
133 for (uint16_t i = 0; i < bufferSize; i++)
134 {
135 float abscissa;
136 /* Print abscissa value */
137 switch (scaleType)
138 {
139 case SCL_INDEX:
140 abscissa = (i * 1.0);
141 break;
142 case SCL_TIME:
143 abscissa = ((i * 1.0) / samplingFrequency);
144 break;
145 case SCL_FREQUENCY:
146 abscissa = ((i * 1.0 * samplingFrequency) / samples);
147 break;
148 }
149 Serial.print(abscissa, 6);
150 if(scaleType==SCL_FREQUENCY)
151 Serial.print("Hz");
152 Serial.print(" ");
153 Serial.println(vData[i], 4);
154 }
155 Serial.println();
156}