SGP30 (used in POCBreath_V2_smd_commercial)
Dependencies: Adafruit_SGP30_mbed mbed
main.cpp@3:3d51f8870e91, 2019-07-25 (annotated)
- Committer:
- mehrnaz
- Date:
- Thu Jul 25 11:17:46 2019 +0000
- Revision:
- 3:3d51f8870e91
- Parent:
- 2:ef98576cd67b
- Child:
- 5:2d4c7a0d805d
final version of Handheld used for KST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
christodoulos | 0:d034cdad5b6d | 1 | #include "mbed.h" |
mehrnaz | 2:ef98576cd67b | 2 | #include "flow.h" |
christodoulos | 0:d034cdad5b6d | 3 | |
mehrnaz | 2:ef98576cd67b | 4 | ///////////////////////// |
mehrnaz | 3:3d51f8870e91 | 5 | // In this version of the program developed for the Breath project, flow and CO2, as well as 8 channel sensors, |
mehrnaz | 2:ef98576cd67b | 6 | // are measured in a separate .h file called: "flow.h" which is included in the |
mehrnaz | 3:3d51f8870e91 | 7 | // main code. So 10 sets of data is streamed using a serial connection (TTL cable or Bluetooth) |
mehrnaz | 3:3d51f8870e91 | 8 | // without any intruption. |
mehrnaz | 2:ef98576cd67b | 9 | // This version is especially suitable to be used for KST. |
mehrnaz | 2:ef98576cd67b | 10 | // Also, a solenoid would be turned on and off based on calculating the standard deviation in CO2 profile. |
mehrnaz | 2:ef98576cd67b | 11 | // |
mehrnaz | 3:3d51f8870e91 | 12 | // START POINT: calculates SD for 9 samples of CO2, if it's grater than 0.02 it enables the solenoid. |
mehrnaz | 3:3d51f8870e91 | 13 | // END POINT: calculates SD for 9 samples of CO2, if it's grater thatn 0.05 it disables the solenoid. |
mehrnaz | 3:3d51f8870e91 | 14 | // |
mehrnaz | 3:3d51f8870e91 | 15 | // You can easily change the threshold of Standard deviation to detect plateau |
mehrnaz | 3:3d51f8870e91 | 16 | // |
mehrnaz | 2:ef98576cd67b | 17 | // Generated by: Mehrnaz Javadipour |
mehrnaz | 2:ef98576cd67b | 18 | ////////////////////////// |
christodoulos | 0:d034cdad5b6d | 19 | |
mehrnaz | 3:3d51f8870e91 | 20 | Serial ttl(PC_12,PD_2); //TTL cable TX,RX |
mehrnaz | 2:ef98576cd67b | 21 | DigitalOut sol(PC_5); //Solenoid: Digital Output |
mehrnaz | 3:3d51f8870e91 | 22 | PwmOut led(PB_6); |
mehrnaz | 3:3d51f8870e91 | 23 | |
mehrnaz | 3:3d51f8870e91 | 24 | |
mehrnaz | 2:ef98576cd67b | 25 | |
mehrnaz | 2:ef98576cd67b | 26 | int main() |
christodoulos | 0:d034cdad5b6d | 27 | { |
mehrnaz | 2:ef98576cd67b | 28 | ttl.baud(9600); //baudrate for the serial connection |
mehrnaz | 2:ef98576cd67b | 29 | flow(); //calling flow from flow.h |
mehrnaz | 2:ef98576cd67b | 30 | carbon(); //calling CO2 from flow.h |
mehrnaz | 3:3d51f8870e91 | 31 | s1(); //calling 8 channels from flow.h |
mehrnaz | 3:3d51f8870e91 | 32 | s2(); |
mehrnaz | 3:3d51f8870e91 | 33 | s3(); |
mehrnaz | 3:3d51f8870e91 | 34 | s4(); |
mehrnaz | 3:3d51f8870e91 | 35 | s5(); |
mehrnaz | 3:3d51f8870e91 | 36 | s6(); |
mehrnaz | 3:3d51f8870e91 | 37 | s7(); |
mehrnaz | 3:3d51f8870e91 | 38 | s8(); |
mehrnaz | 3:3d51f8870e91 | 39 | getTemp(); //calling Temperature from flow.h |
mehrnaz | 3:3d51f8870e91 | 40 | |
mehrnaz | 3:3d51f8870e91 | 41 | ////////////////////////////// |
mehrnaz | 3:3d51f8870e91 | 42 | // I defined a flag for each section of specific functions, so by enabling the |
mehrnaz | 3:3d51f8870e91 | 43 | // flag the section starts and by disabling the flag it finishes the section. |
mehrnaz | 3:3d51f8870e91 | 44 | // at the end of the program, I reset the flags so it would be ready for the next loop. |
mehrnaz | 3:3d51f8870e91 | 45 | ///////////////////////////// |
mehrnaz | 3:3d51f8870e91 | 46 | |
mehrnaz | 3:3d51f8870e91 | 47 | int bf=0; //FLAG for detecting base flow |
mehrnaz | 2:ef98576cd67b | 48 | int i=0; |
mehrnaz | 3:3d51f8870e91 | 49 | float bfArray[4]; //sampling flow for finding the average base flow |
mehrnaz | 3:3d51f8870e91 | 50 | float sf=0; //sum of flow samples for calculating base flow |
mehrnaz | 3:3d51f8870e91 | 51 | float fv=0; //final value of base flow |
mehrnaz | 3:3d51f8870e91 | 52 | |
mehrnaz | 3:3d51f8870e91 | 53 | int measurement_started=0; //FLAG for starting calculations after detecting breath |
mehrnaz | 3:3d51f8870e91 | 54 | |
mehrnaz | 3:3d51f8870e91 | 55 | int solstart=0; //FLAG for starting calculations for detecting plateau |
mehrnaz | 3:3d51f8870e91 | 56 | int m=0; |
mehrnaz | 3:3d51f8870e91 | 57 | int myArray[9]; //sampling 9 values of CO2 |
mehrnaz | 3:3d51f8870e91 | 58 | unsigned int sum=0; //sum of 9 samples of CO2 |
mehrnaz | 3:3d51f8870e91 | 59 | int avg=0; //average of 9 samples of CO2 |
mehrnaz | 3:3d51f8870e91 | 60 | int difSum=0; //used for the Standard deviation algorithm |
mehrnaz | 3:3d51f8870e91 | 61 | long double var=0.0; //used for the Standard deviation algorithm |
mehrnaz | 3:3d51f8870e91 | 62 | float sigma=0.0; //final value for standar deviation |
mehrnaz | 3:3d51f8870e91 | 63 | int flags=0; //FLAG for keep taking samples from CO2 profile when it's too early to detect plateau |
mehrnaz | 3:3d51f8870e91 | 64 | |
mehrnaz | 3:3d51f8870e91 | 65 | int solend=0; //FLAG for ending calculations for detecting plateau |
mehrnaz | 3:3d51f8870e91 | 66 | unsigned int sum2=0; //same as before; used for finding standard deviation |
mehrnaz | 2:ef98576cd67b | 67 | long double var2=0.0; |
mehrnaz | 2:ef98576cd67b | 68 | float sigma2=0.0; |
mehrnaz | 2:ef98576cd67b | 69 | int difSum2=0; |
mehrnaz | 3:3d51f8870e91 | 70 | int avg2=0; |
mehrnaz | 3:3d51f8870e91 | 71 | int flage=0; //FLAG for keep taking samples from CO2 profile when it's too early to finish plateau |
mehrnaz | 3:3d51f8870e91 | 72 | |
mehrnaz | 2:ef98576cd67b | 73 | int fin=0; |
mehrnaz | 2:ef98576cd67b | 74 | |
mehrnaz | 3:3d51f8870e91 | 75 | |
mehrnaz | 2:ef98576cd67b | 76 | |
mehrnaz | 2:ef98576cd67b | 77 | while(1) |
mehrnaz | 2:ef98576cd67b | 78 | { |
mehrnaz | 3:3d51f8870e91 | 79 | led=1.00f; //an LED is fully turned on at the beginning, the brightness will be reduced when the plateau is detected. |
mehrnaz | 3:3d51f8870e91 | 80 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 3:3d51f8870e91 | 81 | |
mehrnaz | 3:3d51f8870e91 | 82 | if (bf==0) //finding base flow before breath |
mehrnaz | 2:ef98576cd67b | 83 | { |
mehrnaz | 2:ef98576cd67b | 84 | for(i=0; i<4; i++) |
mehrnaz | 2:ef98576cd67b | 85 | { |
mehrnaz | 2:ef98576cd67b | 86 | bfArray[i]=flow(); |
mehrnaz | 2:ef98576cd67b | 87 | sf+=bfArray[i]; |
mehrnaz | 2:ef98576cd67b | 88 | } |
mehrnaz | 2:ef98576cd67b | 89 | fv=sf/4; |
mehrnaz | 2:ef98576cd67b | 90 | fv=fv+0.2; |
mehrnaz | 2:ef98576cd67b | 91 | //ttl.printf("set\n"); |
mehrnaz | 2:ef98576cd67b | 92 | bf=1; |
mehrnaz | 2:ef98576cd67b | 93 | } |
mehrnaz | 2:ef98576cd67b | 94 | |
mehrnaz | 3:3d51f8870e91 | 95 | //Starts calculations when it detects breathing into the device: |
mehrnaz | 3:3d51f8870e91 | 96 | |
mehrnaz | 2:ef98576cd67b | 97 | if ((flow()>fv) and (measurement_started ==0)) |
mehrnaz | 2:ef98576cd67b | 98 | { |
mehrnaz | 2:ef98576cd67b | 99 | measurement_started = 1; |
mehrnaz | 2:ef98576cd67b | 100 | } |
mehrnaz | 2:ef98576cd67b | 101 | |
mehrnaz | 3:3d51f8870e91 | 102 | //Starts detecting plateau: |
mehrnaz | 3:3d51f8870e91 | 103 | |
mehrnaz | 2:ef98576cd67b | 104 | if ((measurement_started == 1) and (solstart==0)) |
mehrnaz | 2:ef98576cd67b | 105 | { |
mehrnaz | 3:3d51f8870e91 | 106 | //Takes 9 samples of CO2: |
mehrnaz | 3:3d51f8870e91 | 107 | //I have also included printing the values inside the loops so we don't loose any data during calculatins. |
mehrnaz | 2:ef98576cd67b | 108 | for(m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 109 | { |
mehrnaz | 3:3d51f8870e91 | 110 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 2:ef98576cd67b | 111 | myArray[m]=carbon(); |
mehrnaz | 2:ef98576cd67b | 112 | } |
mehrnaz | 2:ef98576cd67b | 113 | while(flags==0) |
mehrnaz | 2:ef98576cd67b | 114 | { |
mehrnaz | 3:3d51f8870e91 | 115 | //While "flags" is enabled, keeps calculating the standard deviation. |
mehrnaz | 2:ef98576cd67b | 116 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 117 | { |
mehrnaz | 3:3d51f8870e91 | 118 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 2:ef98576cd67b | 119 | sum+=myArray[m]; |
mehrnaz | 2:ef98576cd67b | 120 | } |
mehrnaz | 2:ef98576cd67b | 121 | avg=sum/9; |
mehrnaz | 2:ef98576cd67b | 122 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 123 | { |
mehrnaz | 3:3d51f8870e91 | 124 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 2:ef98576cd67b | 125 | difSum+=(myArray[m]-avg)*(myArray[m]-avg); //Find sum of difference between value X and mean |
mehrnaz | 2:ef98576cd67b | 126 | } |
mehrnaz | 2:ef98576cd67b | 127 | var=difSum/9; |
mehrnaz | 2:ef98576cd67b | 128 | sigma=sqrt(var); |
mehrnaz | 2:ef98576cd67b | 129 | if (sigma<0.02) |
mehrnaz | 2:ef98576cd67b | 130 | { |
mehrnaz | 3:3d51f8870e91 | 131 | |
mehrnaz | 3:3d51f8870e91 | 132 | //if SD is less than 0.02 it means that it is too early to start the plateau |
mehrnaz | 3:3d51f8870e91 | 133 | //So we shift all but the first sample and define the new set of arrays: |
mehrnaz | 3:3d51f8870e91 | 134 | |
mehrnaz | 2:ef98576cd67b | 135 | for(int m=0;m<8;m++) |
mehrnaz | 2:ef98576cd67b | 136 | { |
mehrnaz | 3:3d51f8870e91 | 137 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 3:3d51f8870e91 | 138 | myArray[m]=myArray[m+1]; //Shift all CO2 values to the left by 1 value |
mehrnaz | 2:ef98576cd67b | 139 | } |
mehrnaz | 3:3d51f8870e91 | 140 | myArray[8]=carbon(); //assign a new value for the 9th sample |
mehrnaz | 3:3d51f8870e91 | 141 | } |
mehrnaz | 3:3d51f8870e91 | 142 | //The new set of arrays are now generated and is sent back to be used for preveious SD calculations. |
mehrnaz | 3:3d51f8870e91 | 143 | //If sigma for the new set is still small, a newer set will be generated and replaced |
mehrnaz | 3:3d51f8870e91 | 144 | //Otherwise, it's accepted and will turn on the solenoid: |
mehrnaz | 3:3d51f8870e91 | 145 | else |
mehrnaz | 2:ef98576cd67b | 146 | { |
mehrnaz | 3:3d51f8870e91 | 147 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 3:3d51f8870e91 | 148 | sol=1; //Solenoid is ON |
mehrnaz | 3:3d51f8870e91 | 149 | led=0.50f; //The brightness is reduced to half during the plateau |
mehrnaz | 3:3d51f8870e91 | 150 | flags=1; //breakes the while loop |
mehrnaz | 2:ef98576cd67b | 151 | } |
mehrnaz | 2:ef98576cd67b | 152 | } |
mehrnaz | 3:3d51f8870e91 | 153 | solend=1; //prepares the next section for finishing the plateau |
mehrnaz | 2:ef98576cd67b | 154 | solstart =1; |
mehrnaz | 2:ef98576cd67b | 155 | } |
mehrnaz | 2:ef98576cd67b | 156 | if ((measurement_started == 1) and (solend==1)) |
mehrnaz | 2:ef98576cd67b | 157 | { |
mehrnaz | 3:3d51f8870e91 | 158 | // same process happens for finishing the plateau: |
mehrnaz | 2:ef98576cd67b | 159 | |
mehrnaz | 2:ef98576cd67b | 160 | for(m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 161 | { |
mehrnaz | 3:3d51f8870e91 | 162 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 2:ef98576cd67b | 163 | myArray[m]=carbon(); |
mehrnaz | 2:ef98576cd67b | 164 | } |
mehrnaz | 2:ef98576cd67b | 165 | while(flage==0) |
mehrnaz | 2:ef98576cd67b | 166 | { |
mehrnaz | 2:ef98576cd67b | 167 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 168 | { |
mehrnaz | 3:3d51f8870e91 | 169 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 2:ef98576cd67b | 170 | sum2+=myArray[m]; |
mehrnaz | 2:ef98576cd67b | 171 | } |
mehrnaz | 2:ef98576cd67b | 172 | avg2=sum2/9; |
mehrnaz | 2:ef98576cd67b | 173 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 174 | { |
mehrnaz | 3:3d51f8870e91 | 175 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 3:3d51f8870e91 | 176 | difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); |
mehrnaz | 2:ef98576cd67b | 177 | } |
mehrnaz | 2:ef98576cd67b | 178 | var2=difSum2/9; |
mehrnaz | 2:ef98576cd67b | 179 | sigma2=sqrt(var2); |
mehrnaz | 2:ef98576cd67b | 180 | if (sigma2<0.05) |
mehrnaz | 2:ef98576cd67b | 181 | { |
mehrnaz | 3:3d51f8870e91 | 182 | // here we defined the end threshold to be 0.05, it can be changed later based on experiment results |
mehrnaz | 2:ef98576cd67b | 183 | for(int m=0;m<8;m++) |
mehrnaz | 2:ef98576cd67b | 184 | { |
mehrnaz | 3:3d51f8870e91 | 185 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 3:3d51f8870e91 | 186 | myArray[m]=myArray[m+1]; |
mehrnaz | 2:ef98576cd67b | 187 | } |
mehrnaz | 2:ef98576cd67b | 188 | myArray[8]=carbon(); |
mehrnaz | 2:ef98576cd67b | 189 | }else |
mehrnaz | 2:ef98576cd67b | 190 | { |
mehrnaz | 3:3d51f8870e91 | 191 | ttl.printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",getTemp(),flow(), carbon(),s1(),s2(),s3(),s4(),s5(),s6(),s7(),s8()); |
mehrnaz | 3:3d51f8870e91 | 192 | sol=0; //Solenoid is OFF |
mehrnaz | 3:3d51f8870e91 | 193 | flage=1; //breakes the loop |
mehrnaz | 2:ef98576cd67b | 194 | } |
mehrnaz | 2:ef98576cd67b | 195 | } |
mehrnaz | 3:3d51f8870e91 | 196 | solend =0; //end of this section |
mehrnaz | 3:3d51f8870e91 | 197 | led=1.00f; //LED is back to full brightness |
mehrnaz | 3:3d51f8870e91 | 198 | bf=0; //reset the detecting base flow flag |
mehrnaz | 3:3d51f8870e91 | 199 | fin=1; //enables the next section flag |
mehrnaz | 2:ef98576cd67b | 200 | } |
mehrnaz | 2:ef98576cd67b | 201 | if((carbon()<10000) and (fin ==1)) |
mehrnaz | 2:ef98576cd67b | 202 | { |
mehrnaz | 3:3d51f8870e91 | 203 | //User has to wait for the CO2 level to drop less than 1% before testing again. |
mehrnaz | 3:3d51f8870e91 | 204 | //Once it is less than 1%, all the flags and parameters used in calculations are reset |
mehrnaz | 2:ef98576cd67b | 205 | measurement_started =0; |
mehrnaz | 2:ef98576cd67b | 206 | solstart=0; |
mehrnaz | 2:ef98576cd67b | 207 | sum=0; |
mehrnaz | 2:ef98576cd67b | 208 | var=0.0; |
mehrnaz | 2:ef98576cd67b | 209 | sigma=0.0; |
mehrnaz | 2:ef98576cd67b | 210 | difSum=0; |
mehrnaz | 2:ef98576cd67b | 211 | sum2=0; |
mehrnaz | 2:ef98576cd67b | 212 | var2=0.0; |
mehrnaz | 2:ef98576cd67b | 213 | sigma2=0.0; |
mehrnaz | 2:ef98576cd67b | 214 | difSum2=0; |
mehrnaz | 2:ef98576cd67b | 215 | avg2=0; |
mehrnaz | 2:ef98576cd67b | 216 | avg=0; |
mehrnaz | 2:ef98576cd67b | 217 | flags=0; |
mehrnaz | 2:ef98576cd67b | 218 | flage=0; |
mehrnaz | 2:ef98576cd67b | 219 | fin=0; |
mehrnaz | 2:ef98576cd67b | 220 | } |
mehrnaz | 2:ef98576cd67b | 221 | } |
mehrnaz | 2:ef98576cd67b | 222 | } |