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