![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Calculate time delay with Cross Correlation
medeltest.cpp@6:73617748bb3a, 2017-05-11 (annotated)
- Committer:
- KlaraBengtsson
- Date:
- Thu May 11 12:31:53 2017 +0000
- Revision:
- 6:73617748bb3a
- Parent:
- 5:3912347f8b4b
- Child:
- 7:99d5e92152e0
textAvgTimedelay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reritter | 1:e3d3d330c84d | 1 | #include "mbed.h" |
reritter | 1:e3d3d330c84d | 2 | #include <iostream> /* cout */ |
reritter | 1:e3d3d330c84d | 3 | //#include <stdio.h> /* printf */ |
reritter | 1:e3d3d330c84d | 4 | #include <math.h> /* sin */ |
reritter | 1:e3d3d330c84d | 5 | #include <vector> |
reritter | 1:e3d3d330c84d | 6 | #include <stdlib.h> /* abs */ |
reritter | 1:e3d3d330c84d | 7 | #include <stdio.h> |
reritter | 1:e3d3d330c84d | 8 | #include <AnalogIn.h> |
reritter | 1:e3d3d330c84d | 9 | #include <stdint.h> |
reritter | 1:e3d3d330c84d | 10 | #include <DHT.h> |
reritter | 1:e3d3d330c84d | 11 | #include<sstream> |
reritter | 1:e3d3d330c84d | 12 | |
reritter | 1:e3d3d330c84d | 13 | //using namespace std; |
reritter | 1:e3d3d330c84d | 14 | |
reritter | 1:e3d3d330c84d | 15 | /* DEBUG FUNCTION |
reritter | 1:e3d3d330c84d | 16 | // ersätter Debug(xyz) med xyz , där xyz är din kod |
reritter | 1:e3d3d330c84d | 17 | //För att aktivera: |
reritter | 1:e3d3d330c84d | 18 | #define Debug(xyz) xyz |
reritter | 1:e3d3d330c84d | 19 | |
reritter | 1:e3d3d330c84d | 20 | //För att "stänga av": |
reritter | 1:e3d3d330c84d | 21 | #define Debug(xyz) |
reritter | 1:e3d3d330c84d | 22 | |
reritter | 1:e3d3d330c84d | 23 | //I din kod, skriv din debug kod liknande så här: |
reritter | 1:e3d3d330c84d | 24 | Debug( std::cout << "My text: " << myVariable << std::endl; ); |
reritter | 1:e3d3d330c84d | 25 | |
reritter | 1:e3d3d330c84d | 26 | */ |
reritter | 1:e3d3d330c84d | 27 | |
reritter | 1:e3d3d330c84d | 28 | #define Debug(x) x |
reritter | 1:e3d3d330c84d | 29 | #define DebugPrintState(y) y |
reritter | 1:e3d3d330c84d | 30 | #define DebugArcSin(z) z |
KlaraBengtsson | 5:3912347f8b4b | 31 | #define DebugPrintDelay(q) q |
reritter | 1:e3d3d330c84d | 32 | |
reritter | 1:e3d3d330c84d | 33 | |
reritter | 1:e3d3d330c84d | 34 | //----------VARIABLES HERE |
reritter | 1:e3d3d330c84d | 35 | int dataLength = 1000; |
reritter | 1:e3d3d330c84d | 36 | int captureLength = 50; |
reritter | 1:e3d3d330c84d | 37 | double temp = 22; |
reritter | 1:e3d3d330c84d | 38 | double hum = 10; |
KlaraBengtsson | 6:73617748bb3a | 39 | double micDist = 0.15; //meters |
reritter | 1:e3d3d330c84d | 40 | double threshold_1 = 0; //value when going to active mode channel 1 //old hardcoded value = 330 |
reritter | 1:e3d3d330c84d | 41 | double threshold_2 = 0; //value when going to active mode channel 2 //old hardcoded value = 200 |
reritter | 1:e3d3d330c84d | 42 | double threshold_adjust = 15; //used to adjust threshold, + for less sensitivity, - for increased sensitivity |
reritter | 1:e3d3d330c84d | 43 | bool calibratedStatus = false; //flag to make sure Nuclueo only calibrated once for background noise |
reritter | 1:e3d3d330c84d | 44 | bool checkTemp = false; //flag - true to checktemp, false to use predefined values |
reritter | 1:e3d3d330c84d | 45 | int positionOfMaxVal_1; |
reritter | 1:e3d3d330c84d | 46 | int positionOfMaxVal_2; |
reritter | 1:e3d3d330c84d | 47 | const double PI = 3.14159265358979323846; |
KlaraBengtsson | 5:3912347f8b4b | 48 | double timedelay = 0; |
reritter | 1:e3d3d330c84d | 49 | |
reritter | 1:e3d3d330c84d | 50 | // State machine |
reritter | 1:e3d3d330c84d | 51 | int STATE; |
reritter | 1:e3d3d330c84d | 52 | //const int NONE = -1; |
reritter | 1:e3d3d330c84d | 53 | const int IDLE = 0; |
reritter | 1:e3d3d330c84d | 54 | const int CALIBRATE = 1; |
reritter | 1:e3d3d330c84d | 55 | const int TESTNEW = 2; |
reritter | 1:e3d3d330c84d | 56 | const int CALC = 3; |
reritter | 1:e3d3d330c84d | 57 | const int CALC_ERROR = 4; |
reritter | 1:e3d3d330c84d | 58 | const int SEND = 5; |
reritter | 1:e3d3d330c84d | 59 | //const int WAIT = 9; |
reritter | 1:e3d3d330c84d | 60 | |
reritter | 1:e3d3d330c84d | 61 | //dataLength behövs kanske inte, vector klassen kan växa med behov |
reritter | 1:e3d3d330c84d | 62 | std::vector<double> channel_1(dataLength); |
reritter | 1:e3d3d330c84d | 63 | std::vector<double> channel_2(dataLength); |
reritter | 1:e3d3d330c84d | 64 | std::vector<int> timestamps_1(dataLength); |
reritter | 1:e3d3d330c84d | 65 | std::vector<int> timestamps_2(dataLength); |
reritter | 1:e3d3d330c84d | 66 | std::vector<double> capture_1(captureLength); |
reritter | 1:e3d3d330c84d | 67 | std::vector<double> capture_2(captureLength); |
reritter | 1:e3d3d330c84d | 68 | std::vector<double> capturestamps_1(captureLength); |
reritter | 1:e3d3d330c84d | 69 | std::vector<double> capturestamps_2(captureLength); |
reritter | 1:e3d3d330c84d | 70 | |
reritter | 1:e3d3d330c84d | 71 | int positiontest = 0; |
reritter | 1:e3d3d330c84d | 72 | int test = 9; |
reritter | 1:e3d3d330c84d | 73 | std::vector<double> delaytest(test); |
reritter | 1:e3d3d330c84d | 74 | |
reritter | 1:e3d3d330c84d | 75 | |
reritter | 1:e3d3d330c84d | 76 | AnalogIn mic1(A0); |
reritter | 1:e3d3d330c84d | 77 | AnalogIn mic2(A1); |
reritter | 1:e3d3d330c84d | 78 | AnalogIn mic3(A2); |
reritter | 1:e3d3d330c84d | 79 | DHT sensor(A3, DHT11); |
reritter | 1:e3d3d330c84d | 80 | |
reritter | 1:e3d3d330c84d | 81 | //TIMER |
reritter | 1:e3d3d330c84d | 82 | Timer t; |
reritter | 1:e3d3d330c84d | 83 | |
reritter | 1:e3d3d330c84d | 84 | //led can be used for status |
reritter | 1:e3d3d330c84d | 85 | DigitalOut led1(LED1); |
reritter | 1:e3d3d330c84d | 86 | |
reritter | 1:e3d3d330c84d | 87 | |
reritter | 1:e3d3d330c84d | 88 | //----------FUNCTIONS HERE |
reritter | 1:e3d3d330c84d | 89 | //Calculating distance between sound and camera |
reritter | 1:e3d3d330c84d | 90 | double calcDist(double t, double v) |
reritter | 1:e3d3d330c84d | 91 | { |
reritter | 1:e3d3d330c84d | 92 | double s = t*v; |
reritter | 1:e3d3d330c84d | 93 | return s; |
reritter | 1:e3d3d330c84d | 94 | } |
reritter | 1:e3d3d330c84d | 95 | |
reritter | 1:e3d3d330c84d | 96 | //Calculating angle in radians, D distance between mic1 and mic2 |
reritter | 1:e3d3d330c84d | 97 | double calcAng(double s, double D) |
reritter | 1:e3d3d330c84d | 98 | { |
reritter | 1:e3d3d330c84d | 99 | return asin(s/D) + PI/2; |
reritter | 1:e3d3d330c84d | 100 | } |
reritter | 1:e3d3d330c84d | 101 | |
reritter | 1:e3d3d330c84d | 102 | //Assuming the input value is temp as a number in degrees celcius and humidity as procent |
reritter | 1:e3d3d330c84d | 103 | double calcSoundSpeed(double temp, double hum) |
reritter | 1:e3d3d330c84d | 104 | { |
reritter | 1:e3d3d330c84d | 105 | //Calculations were done in Matlab |
reritter | 1:e3d3d330c84d | 106 | double speed = 331.1190 + 0.6016*temp + 0.0126*hum; |
reritter | 1:e3d3d330c84d | 107 | return speed; |
reritter | 1:e3d3d330c84d | 108 | } |
reritter | 1:e3d3d330c84d | 109 | |
reritter | 1:e3d3d330c84d | 110 | //translate angle to number for camera |
reritter | 1:e3d3d330c84d | 111 | string convertAngToCamNbr(double ang) |
reritter | 1:e3d3d330c84d | 112 | { |
reritter | 1:e3d3d330c84d | 113 | ang = ang*(180 / PI) + 45; //radianer till grader |
reritter | 1:e3d3d330c84d | 114 | double angValues = 270; |
reritter | 1:e3d3d330c84d | 115 | int stepValues = 50000; |
reritter | 1:e3d3d330c84d | 116 | string tiltNumber = " 18000"; //hårdkodat Camera Pan värde |
reritter | 1:e3d3d330c84d | 117 | |
reritter | 1:e3d3d330c84d | 118 | double oneAng = stepValues/angValues; |
reritter | 1:e3d3d330c84d | 119 | double cameraAngNumber = ang*oneAng; |
reritter | 1:e3d3d330c84d | 120 | int panInt = (int)(cameraAngNumber); //double to int |
reritter | 1:e3d3d330c84d | 121 | //int to string |
reritter | 1:e3d3d330c84d | 122 | string panNumber; |
reritter | 1:e3d3d330c84d | 123 | ostringstream convert; |
reritter | 1:e3d3d330c84d | 124 | convert << panInt; |
reritter | 1:e3d3d330c84d | 125 | panNumber = convert.str(); |
reritter | 1:e3d3d330c84d | 126 | |
reritter | 1:e3d3d330c84d | 127 | string send = panNumber + tiltNumber; |
reritter | 1:e3d3d330c84d | 128 | return send; |
reritter | 1:e3d3d330c84d | 129 | } |
reritter | 1:e3d3d330c84d | 130 | |
reritter | 1:e3d3d330c84d | 131 | |
reritter | 1:e3d3d330c84d | 132 | //calc time delay by finding peak values in 2 vectors |
reritter | 1:e3d3d330c84d | 133 | //channel = 1 or 2 |
reritter | 1:e3d3d330c84d | 134 | int FindPeak(int channel) |
reritter | 1:e3d3d330c84d | 135 | { |
reritter | 1:e3d3d330c84d | 136 | std::vector<double> channel_curr(captureLength); //temporary vector with channel voltage values |
reritter | 1:e3d3d330c84d | 137 | |
reritter | 1:e3d3d330c84d | 138 | //if channel 1 then set current channel to channel 1 |
reritter | 1:e3d3d330c84d | 139 | if (channel == 1) { |
reritter | 1:e3d3d330c84d | 140 | channel_curr = capture_1; |
reritter | 1:e3d3d330c84d | 141 | } else channel_curr = capture_2; |
reritter | 1:e3d3d330c84d | 142 | |
reritter | 1:e3d3d330c84d | 143 | //reset max value & sum value |
reritter | 1:e3d3d330c84d | 144 | double valueMax = 0; |
reritter | 1:e3d3d330c84d | 145 | |
reritter | 1:e3d3d330c84d | 146 | //reset array position |
reritter | 1:e3d3d330c84d | 147 | int positionOfMaxVal = 0; |
reritter | 1:e3d3d330c84d | 148 | |
reritter | 1:e3d3d330c84d | 149 | //find largest value & mark that position in vectors |
reritter | 1:e3d3d330c84d | 150 | for (int position = 0; position < channel_curr.size(); position++) { |
reritter | 1:e3d3d330c84d | 151 | double val = abs(channel_curr[position]); |
reritter | 1:e3d3d330c84d | 152 | if (val > valueMax ) { |
reritter | 1:e3d3d330c84d | 153 | valueMax = val; |
reritter | 1:e3d3d330c84d | 154 | positionOfMaxVal = position; |
reritter | 1:e3d3d330c84d | 155 | } |
reritter | 1:e3d3d330c84d | 156 | } |
reritter | 1:e3d3d330c84d | 157 | return positionOfMaxVal; |
reritter | 1:e3d3d330c84d | 158 | } |
reritter | 1:e3d3d330c84d | 159 | |
reritter | 2:4fdc0a17f6fa | 160 | /*Crosscorrelation code |
reritter | 2:4fdc0a17f6fa | 161 | /*TDEVector* TDE::CrossCorrelation() |
reritter | 2:4fdc0a17f6fa | 162 | { |
reritter | 2:4fdc0a17f6fa | 163 | TDEVector* res = new TDEVector( |
reritter | 2:4fdc0a17f6fa | 164 | 2 * m_maxDelay + 1, {0, CalcZero}); |
reritter | 2:4fdc0a17f6fa | 165 | |
reritter | 2:4fdc0a17f6fa | 166 | for (DelayType = -m_maxDelay; delay <= m_maxDelay; delay++) |
reritter | 2:4fdc0a17f6fa | 167 | { |
reritter | 2:4fdc0a17f6fa | 168 | CalcType sum = 0; |
reritter | 2:4fdc0a17f6fa | 169 | for (size_t pos = 0; pos < m_datalength; pos++) |
reritter | 2:4fdc0a17f6fa | 170 | { |
reritter | 2:4fdc0a17f6fa | 171 | sum += m_channel0[pos] |
reritter | 2:4fdc0a17f6fa | 172 | * m_channel1[pos + delay + m_maxDelay]; |
reritter | 2:4fdc0a17f6fa | 173 | } |
reritter | 2:4fdc0a17f6fa | 174 | res->at(delay + m_maxDelay).delay = delay; |
reritter | 2:4fdc0a17f6fa | 175 | res->at(delay + m_maxDelay).value = sum; |
reritter | 2:4fdc0a17f6fa | 176 | } |
reritter | 2:4fdc0a17f6fa | 177 | return res; |
reritter | 2:4fdc0a17f6fa | 178 | }*/ |
reritter | 2:4fdc0a17f6fa | 179 | |
reritter | 2:4fdc0a17f6fa | 180 | |
reritter | 2:4fdc0a17f6fa | 181 | |
reritter | 2:4fdc0a17f6fa | 182 | |
reritter | 1:e3d3d330c84d | 183 | double FindTimeDelay(int positionOfMaxVal_1, int positionOfMaxVal_2) |
reritter | 1:e3d3d330c84d | 184 | { |
reritter | 1:e3d3d330c84d | 185 | double timemax_1 = capturestamps_1[positionOfMaxVal_1]; |
reritter | 1:e3d3d330c84d | 186 | double timemax_2 = capturestamps_2[positionOfMaxVal_2]; |
reritter | 1:e3d3d330c84d | 187 | double delay = timemax_1 - timemax_2; |
reritter | 1:e3d3d330c84d | 188 | return delay; //if negative near microphone 1, if positive near micropnone 2 |
reritter | 1:e3d3d330c84d | 189 | } |
reritter | 1:e3d3d330c84d | 190 | |
reritter | 1:e3d3d330c84d | 191 | |
reritter | 1:e3d3d330c84d | 192 | //get voltage value which represents audio amplitude from microphone |
reritter | 1:e3d3d330c84d | 193 | double getAudioValue(AnalogIn micX) |
reritter | 1:e3d3d330c84d | 194 | { |
reritter | 1:e3d3d330c84d | 195 | return 1000*micX.read(); |
reritter | 1:e3d3d330c84d | 196 | } |
reritter | 1:e3d3d330c84d | 197 | |
reritter | 1:e3d3d330c84d | 198 | |
reritter | 1:e3d3d330c84d | 199 | bool overThreshold(double micValue_1, double micValue_2) |
reritter | 1:e3d3d330c84d | 200 | { |
reritter | 1:e3d3d330c84d | 201 | if ((micValue_1 > threshold_1) || (micValue_2 > threshold_2)) { |
reritter | 1:e3d3d330c84d | 202 | return true; |
reritter | 1:e3d3d330c84d | 203 | } else return false; |
reritter | 1:e3d3d330c84d | 204 | } |
reritter | 1:e3d3d330c84d | 205 | |
reritter | 1:e3d3d330c84d | 206 | //true if voltage value in microphone is above the current threshold value |
reritter | 1:e3d3d330c84d | 207 | bool calibrateThreshold(double micValue, double currentThreshold) |
reritter | 1:e3d3d330c84d | 208 | { |
reritter | 1:e3d3d330c84d | 209 | if ( micValue > currentThreshold ) { |
reritter | 1:e3d3d330c84d | 210 | return true; |
reritter | 1:e3d3d330c84d | 211 | } else return false; |
reritter | 1:e3d3d330c84d | 212 | } |
reritter | 1:e3d3d330c84d | 213 | |
reritter | 1:e3d3d330c84d | 214 | |
reritter | 1:e3d3d330c84d | 215 | // main() runs in its own thread in the OS |
reritter | 1:e3d3d330c84d | 216 | int main() |
reritter | 1:e3d3d330c84d | 217 | { |
reritter | 1:e3d3d330c84d | 218 | for(int i = 0; i < test; i++) { |
reritter | 1:e3d3d330c84d | 219 | delaytest[i] = -420 + i*105; |
reritter | 1:e3d3d330c84d | 220 | } |
reritter | 1:e3d3d330c84d | 221 | t.start(); // start timer |
reritter | 1:e3d3d330c84d | 222 | |
reritter | 1:e3d3d330c84d | 223 | //while (true) { |
reritter | 1:e3d3d330c84d | 224 | led1 = !led1; |
reritter | 1:e3d3d330c84d | 225 | wait(0.5); |
reritter | 1:e3d3d330c84d | 226 | |
reritter | 1:e3d3d330c84d | 227 | |
reritter | 1:e3d3d330c84d | 228 | //STATE MACHINE |
reritter | 1:e3d3d330c84d | 229 | STATE = IDLE; |
reritter | 1:e3d3d330c84d | 230 | //int counter = 0; |
reritter | 1:e3d3d330c84d | 231 | while (true) { |
reritter | 1:e3d3d330c84d | 232 | switch (STATE) { |
reritter | 1:e3d3d330c84d | 233 | case IDLE: //always start here |
reritter | 1:e3d3d330c84d | 234 | DebugPrintState( std::cout << "Nucleo state is IDLE: " << std::endl; ); |
reritter | 1:e3d3d330c84d | 235 | Debug( wait(0.5); ); |
reritter | 1:e3d3d330c84d | 236 | if (!calibratedStatus) STATE = CALIBRATE; |
reritter | 1:e3d3d330c84d | 237 | else STATE = TESTNEW; |
reritter | 1:e3d3d330c84d | 238 | break; |
reritter | 1:e3d3d330c84d | 239 | |
reritter | 1:e3d3d330c84d | 240 | case CALIBRATE: |
reritter | 1:e3d3d330c84d | 241 | DebugPrintState( std::cout << "Nucleo state is CALIBRATE: " << std::endl; ); |
reritter | 1:e3d3d330c84d | 242 | Debug( wait(1); ); |
reritter | 1:e3d3d330c84d | 243 | //listen for X seconds to background noise, to set accurate threshold value |
reritter | 1:e3d3d330c84d | 244 | // This should be done only once when rebooting Nucleo |
reritter | 1:e3d3d330c84d | 245 | int startTime = t.read_us(); |
reritter | 1:e3d3d330c84d | 246 | int offsetTime = 3000; //microseconds |
reritter | 1:e3d3d330c84d | 247 | int blinkTime = 500; //microseconds |
reritter | 1:e3d3d330c84d | 248 | while (t.read_us() < (startTime + offsetTime) ) { |
reritter | 1:e3d3d330c84d | 249 | double micValue_1 = getAudioValue(mic1); |
reritter | 1:e3d3d330c84d | 250 | if ( calibrateThreshold(micValue_1, threshold_1) ) { |
reritter | 1:e3d3d330c84d | 251 | threshold_1 = micValue_1; //threshold value updated |
reritter | 1:e3d3d330c84d | 252 | } |
reritter | 1:e3d3d330c84d | 253 | double micValue_2 = getAudioValue(mic2); |
reritter | 1:e3d3d330c84d | 254 | if ( calibrateThreshold(micValue_2, threshold_2) ) { |
reritter | 1:e3d3d330c84d | 255 | threshold_2 = micValue_2; //threshold value updated |
reritter | 1:e3d3d330c84d | 256 | } |
reritter | 1:e3d3d330c84d | 257 | //make LED blink every 500 ms |
reritter | 1:e3d3d330c84d | 258 | if ( t.read_us() > (startTime + blinkTime) ) { |
reritter | 1:e3d3d330c84d | 259 | led1 = !led1; |
reritter | 1:e3d3d330c84d | 260 | blinkTime = blinkTime + 500; |
reritter | 1:e3d3d330c84d | 261 | } |
reritter | 1:e3d3d330c84d | 262 | } |
reritter | 1:e3d3d330c84d | 263 | threshold_1 = threshold_2 + threshold_adjust; |
reritter | 1:e3d3d330c84d | 264 | threshold_2 = threshold_2 + threshold_adjust; |
reritter | 1:e3d3d330c84d | 265 | |
reritter | 1:e3d3d330c84d | 266 | //Calibrate temp and hum |
reritter | 1:e3d3d330c84d | 267 | if(checkTemp){ |
reritter | 1:e3d3d330c84d | 268 | bool done = false; |
reritter | 1:e3d3d330c84d | 269 | while(!done) { |
reritter | 1:e3d3d330c84d | 270 | if(sensor.readData() == 0) { |
reritter | 1:e3d3d330c84d | 271 | temp = sensor.ReadTemperature(CELCIUS); |
reritter | 1:e3d3d330c84d | 272 | hum = sensor.ReadHumidity(); |
reritter | 1:e3d3d330c84d | 273 | DebugPrintState(std::cout << "Temp: " << temp << "Degrees Celcius" <<std::endl; ); |
reritter | 1:e3d3d330c84d | 274 | DebugPrintState(std::cout << "Hum: " << temp << "%" <<std::endl; ); |
reritter | 1:e3d3d330c84d | 275 | done = true; |
reritter | 1:e3d3d330c84d | 276 | } |
reritter | 1:e3d3d330c84d | 277 | } |
reritter | 1:e3d3d330c84d | 278 | } |
reritter | 1:e3d3d330c84d | 279 | |
reritter | 1:e3d3d330c84d | 280 | calibratedStatus = true; |
reritter | 1:e3d3d330c84d | 281 | STATE = TESTNEW; //next state |
reritter | 1:e3d3d330c84d | 282 | break; |
reritter | 1:e3d3d330c84d | 283 | |
reritter | 1:e3d3d330c84d | 284 | case TESTNEW: |
reritter | 1:e3d3d330c84d | 285 | DebugPrintState( std::cout << "Nucleo state is TESTNEW: " << std::endl; ); |
KlaraBengtsson | 5:3912347f8b4b | 286 | |
KlaraBengtsson | 5:3912347f8b4b | 287 | double avgTimedelay = 0; |
KlaraBengtsson | 5:3912347f8b4b | 288 | int counter = 0; |
KlaraBengtsson | 5:3912347f8b4b | 289 | while(counter<1000){ |
KlaraBengtsson | 5:3912347f8b4b | 290 | int i = 0; |
KlaraBengtsson | 5:3912347f8b4b | 291 | bool quit = false; |
KlaraBengtsson | 5:3912347f8b4b | 292 | while(!quit) { |
KlaraBengtsson | 5:3912347f8b4b | 293 | channel_1[i] = getAudioValue(mic1); |
KlaraBengtsson | 5:3912347f8b4b | 294 | timestamps_1[i] = t.read_us(); |
KlaraBengtsson | 5:3912347f8b4b | 295 | channel_2[i] = getAudioValue(mic2); |
KlaraBengtsson | 5:3912347f8b4b | 296 | timestamps_2[i] = t.read_us(); |
KlaraBengtsson | 5:3912347f8b4b | 297 | if(overThreshold(channel_1[i], channel_2[i]) == true) { |
KlaraBengtsson | 5:3912347f8b4b | 298 | capture_1[0] = channel_1[i]; |
KlaraBengtsson | 5:3912347f8b4b | 299 | capturestamps_1[0] = timestamps_1[i]; |
KlaraBengtsson | 5:3912347f8b4b | 300 | capture_2[0] = channel_2[i]; |
KlaraBengtsson | 5:3912347f8b4b | 301 | capturestamps_2[0] = timestamps_2[i]; |
KlaraBengtsson | 5:3912347f8b4b | 302 | for(int i = 1; i < captureLength; i++) { |
KlaraBengtsson | 5:3912347f8b4b | 303 | capture_1[i] = getAudioValue(mic1); |
KlaraBengtsson | 5:3912347f8b4b | 304 | capturestamps_1[i] = t.read_us(); |
KlaraBengtsson | 5:3912347f8b4b | 305 | capture_2[i] = getAudioValue(mic2); |
KlaraBengtsson | 5:3912347f8b4b | 306 | capturestamps_2[i] = t.read_us(); |
KlaraBengtsson | 5:3912347f8b4b | 307 | } |
KlaraBengtsson | 5:3912347f8b4b | 308 | quit = true; |
reritter | 1:e3d3d330c84d | 309 | } |
KlaraBengtsson | 5:3912347f8b4b | 310 | if(i < dataLength) { |
KlaraBengtsson | 5:3912347f8b4b | 311 | i++; |
KlaraBengtsson | 5:3912347f8b4b | 312 | } else { |
KlaraBengtsson | 5:3912347f8b4b | 313 | i = 0; |
KlaraBengtsson | 5:3912347f8b4b | 314 | } |
reritter | 1:e3d3d330c84d | 315 | } |
KlaraBengtsson | 5:3912347f8b4b | 316 | |
KlaraBengtsson | 5:3912347f8b4b | 317 | |
KlaraBengtsson | 5:3912347f8b4b | 318 | int positionOfMaxVal_1 = FindPeak(1); |
KlaraBengtsson | 5:3912347f8b4b | 319 | int positionOfMaxVal_2 = FindPeak(2); |
KlaraBengtsson | 5:3912347f8b4b | 320 | avgTimedelay = avgTimedelay + FindTimeDelay(positionOfMaxVal_1, positionOfMaxVal_2); //microseceonds |
KlaraBengtsson | 5:3912347f8b4b | 321 | counter++; |
reritter | 1:e3d3d330c84d | 322 | } |
KlaraBengtsson | 5:3912347f8b4b | 323 | timedelay = avgTimedelay*0.001; |
KlaraBengtsson | 6:73617748bb3a | 324 | DebugPrintDelay(std::cout<<"avgTimedelay is: "<<timedelay<<std::endl;); |
reritter | 1:e3d3d330c84d | 325 | STATE = CALC; |
reritter | 1:e3d3d330c84d | 326 | break; |
reritter | 1:e3d3d330c84d | 327 | |
reritter | 1:e3d3d330c84d | 328 | |
reritter | 1:e3d3d330c84d | 329 | case CALC: |
reritter | 1:e3d3d330c84d | 330 | DebugPrintState( std::cout << "Nucleo state is CALC: " << std::endl; ); |
reritter | 1:e3d3d330c84d | 331 | //Debug( wait(0.5); ); |
KlaraBengtsson | 6:73617748bb3a | 332 | |
KlaraBengtsson | 5:3912347f8b4b | 333 | //int positionOfMaxVal_1 = FindPeak(1); |
KlaraBengtsson | 5:3912347f8b4b | 334 | //int positionOfMaxVal_2 = FindPeak(2); |
reritter | 1:e3d3d330c84d | 335 | //run functions |
KlaraBengtsson | 5:3912347f8b4b | 336 | //double timedelay = FindTimeDelay(positionOfMaxVal_1, positionOfMaxVal_2); //microseceonds |
reritter | 1:e3d3d330c84d | 337 | if(abs(timedelay) > micDist/calcSoundSpeed(temp, hum)){ |
reritter | 1:e3d3d330c84d | 338 | STATE = CALC_ERROR; |
reritter | 1:e3d3d330c84d | 339 | break; |
reritter | 1:e3d3d330c84d | 340 | } |
reritter | 1:e3d3d330c84d | 341 | double speed = calcSoundSpeed(temp, hum); //meters per second |
reritter | 1:e3d3d330c84d | 342 | double distance = calcDist(timedelay/1000000, speed); //input converted to meters |
reritter | 1:e3d3d330c84d | 343 | double angle = calcAng((double)distance, micDist); //0,15m = 15cm = 150mm, double type cast because of asin function in angle calculation |
reritter | 1:e3d3d330c84d | 344 | //go to state SEND if no calc_error |
reritter | 1:e3d3d330c84d | 345 | |
reritter | 1:e3d3d330c84d | 346 | Debug( |
reritter | 1:e3d3d330c84d | 347 | std::cout << "max position for channel 1: " << positionOfMaxVal_1+1 << std::endl; |
reritter | 1:e3d3d330c84d | 348 | std::cout << "max position for channel 2: " << positionOfMaxVal_2+1 << std::endl; |
reritter | 1:e3d3d330c84d | 349 | std::cout << "run FindPeak, delay is: " << timedelay << "microseconds" << std::endl; |
reritter | 1:e3d3d330c84d | 350 | std::cout << "run calcDist, delta s is: " << distance << " millimeters" << std::endl; |
reritter | 1:e3d3d330c84d | 351 | std::cout << "run calcAngle, angle is: " << angle << " radians" << std::endl; |
reritter | 1:e3d3d330c84d | 352 | std::cout << "run calcAngle, angle is: " << angle*(180 / PI) << " degrees" << std::endl; |
reritter | 1:e3d3d330c84d | 353 | std::cout << "run convertAngToCamNbr, coordinates: "<< convertAngToCamNbr(angle)<<std::endl; //return "panNumber tiltNumber"; |
reritter | 1:e3d3d330c84d | 354 | ); |
reritter | 1:e3d3d330c84d | 355 | if (angle > (3 * PI )/2 || angle < 0 ) { //vinkel larger than 270 eller minde än noll |
reritter | 1:e3d3d330c84d | 356 | STATE = CALC_ERROR; |
reritter | 1:e3d3d330c84d | 357 | } else { |
reritter | 1:e3d3d330c84d | 358 | STATE = SEND; |
reritter | 1:e3d3d330c84d | 359 | } |
reritter | 1:e3d3d330c84d | 360 | break; |
reritter | 1:e3d3d330c84d | 361 | |
reritter | 1:e3d3d330c84d | 362 | case CALC_ERROR: |
reritter | 1:e3d3d330c84d | 363 | DebugPrintState( std::cout << "Nucleo state is CALC_ERROR: " << std::endl; ); |
reritter | 1:e3d3d330c84d | 364 | Debug( wait(0.5); ); |
reritter | 1:e3d3d330c84d | 365 | //error message |
reritter | 1:e3d3d330c84d | 366 | std::cout << "Error. angle not within limits 0 -270 degrees" << std::endl; |
reritter | 1:e3d3d330c84d | 367 | //nollställ vektorer, , stoppa klockan , osv |
reritter | 1:e3d3d330c84d | 368 | STATE = TESTNEW; |
reritter | 1:e3d3d330c84d | 369 | break; |
reritter | 1:e3d3d330c84d | 370 | |
reritter | 1:e3d3d330c84d | 371 | case SEND: |
reritter | 1:e3d3d330c84d | 372 | DebugPrintState( std::cout << "Nucleo state is SEND: " << std::endl; ); |
reritter | 1:e3d3d330c84d | 373 | Debug( wait(0.5); ); |
reritter | 1:e3d3d330c84d | 374 | // send coordinates to serial port to camera |
reritter | 1:e3d3d330c84d | 375 | std::cout<<convertAngToCamNbr(angle)<<std::endl; //return "panNumber tiltNumber"; |
reritter | 1:e3d3d330c84d | 376 | Debug( wait(0.5); ); |
reritter | 1:e3d3d330c84d | 377 | STATE = IDLE; |
reritter | 1:e3d3d330c84d | 378 | wait(5); |
reritter | 1:e3d3d330c84d | 379 | break; |
reritter | 1:e3d3d330c84d | 380 | } |
reritter | 1:e3d3d330c84d | 381 | } |
reritter | 1:e3d3d330c84d | 382 | } |
reritter | 1:e3d3d330c84d | 383 |