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