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