Exluding ZMOD
Dependencies: iAQ_Core Adafruit_SGP30_mbed mbed BME680
main.cpp@2:ef98576cd67b, 2019-07-12 (annotated)
- Committer:
- mehrnaz
- Date:
- Fri Jul 12 15:58:11 2019 +0000
- Revision:
- 2:ef98576cd67b
- Parent:
- 1:08bbc3ecd836
- Child:
- 3:3d51f8870e91
handheld csv 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 | 2:ef98576cd67b | 5 | // In this version of the program developed for the Breath project, flow and CO2 |
mehrnaz | 2:ef98576cd67b | 6 | // are measured in a separate .h file called: "flow.h" which is included in the |
mehrnaz | 2:ef98576cd67b | 7 | // main code. Data is streamed using a serial connection (TTL cable or Bluetooth) |
mehrnaz | 2:ef98576cd67b | 8 | // without any intruption because of loop calculations. |
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 | 2:ef98576cd67b | 12 | // Generated by: Mehrnaz Javadipour |
mehrnaz | 2:ef98576cd67b | 13 | ////////////////////////// |
christodoulos | 0:d034cdad5b6d | 14 | |
mehrnaz | 2:ef98576cd67b | 15 | Serial ttl(PA_2,PA_3); //TTL cable TX,RX |
mehrnaz | 2:ef98576cd67b | 16 | DigitalOut sol(PC_5); //Solenoid: Digital Output |
mehrnaz | 2:ef98576cd67b | 17 | |
mehrnaz | 2:ef98576cd67b | 18 | int main() |
christodoulos | 0:d034cdad5b6d | 19 | { |
mehrnaz | 2:ef98576cd67b | 20 | ttl.baud(9600); //baudrate for the serial connection |
mehrnaz | 2:ef98576cd67b | 21 | flow(); //calling flow from flow.h |
mehrnaz | 2:ef98576cd67b | 22 | carbon(); //calling CO2 from flow.h |
mehrnaz | 2:ef98576cd67b | 23 | int bf=0; |
mehrnaz | 2:ef98576cd67b | 24 | int i=0; |
mehrnaz | 2:ef98576cd67b | 25 | float bfArray[4]; |
mehrnaz | 2:ef98576cd67b | 26 | float sf=0; |
mehrnaz | 2:ef98576cd67b | 27 | float fv=0; |
mehrnaz | 2:ef98576cd67b | 28 | int myArray[9]; |
mehrnaz | 2:ef98576cd67b | 29 | int measurement_started=0; |
mehrnaz | 2:ef98576cd67b | 30 | unsigned int sum=0; |
mehrnaz | 2:ef98576cd67b | 31 | long double var=0.0; |
mehrnaz | 2:ef98576cd67b | 32 | float sigma=0.0; |
mehrnaz | 2:ef98576cd67b | 33 | int difSum=0; |
mehrnaz | 2:ef98576cd67b | 34 | unsigned int sum2=0; |
mehrnaz | 2:ef98576cd67b | 35 | long double var2=0.0; |
mehrnaz | 2:ef98576cd67b | 36 | float sigma2=0.0; |
mehrnaz | 2:ef98576cd67b | 37 | int difSum2=0; |
mehrnaz | 2:ef98576cd67b | 38 | int avg2=0; |
mehrnaz | 2:ef98576cd67b | 39 | int avg=0; |
mehrnaz | 2:ef98576cd67b | 40 | int m=0; |
mehrnaz | 2:ef98576cd67b | 41 | int flags=0; |
mehrnaz | 2:ef98576cd67b | 42 | int flage=0; |
mehrnaz | 2:ef98576cd67b | 43 | int solstart=0; |
mehrnaz | 2:ef98576cd67b | 44 | int solend=0; |
mehrnaz | 2:ef98576cd67b | 45 | int fin=0; |
mehrnaz | 2:ef98576cd67b | 46 | |
mehrnaz | 2:ef98576cd67b | 47 | //float CO_2; |
mehrnaz | 2:ef98576cd67b | 48 | |
mehrnaz | 2:ef98576cd67b | 49 | while(1) |
mehrnaz | 2:ef98576cd67b | 50 | { |
mehrnaz | 2:ef98576cd67b | 51 | //CO_2=(float)carbon()/10000; |
mehrnaz | 2:ef98576cd67b | 52 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 53 | if (bf==0) |
mehrnaz | 2:ef98576cd67b | 54 | { |
mehrnaz | 2:ef98576cd67b | 55 | for(i=0; i<4; i++) |
mehrnaz | 2:ef98576cd67b | 56 | { |
mehrnaz | 2:ef98576cd67b | 57 | bfArray[i]=flow(); |
mehrnaz | 2:ef98576cd67b | 58 | sf+=bfArray[i]; |
mehrnaz | 2:ef98576cd67b | 59 | } |
mehrnaz | 2:ef98576cd67b | 60 | fv=sf/4; |
mehrnaz | 2:ef98576cd67b | 61 | fv=fv+0.2; |
mehrnaz | 2:ef98576cd67b | 62 | //ttl.printf("set\n"); |
mehrnaz | 2:ef98576cd67b | 63 | bf=1; |
mehrnaz | 2:ef98576cd67b | 64 | } |
mehrnaz | 2:ef98576cd67b | 65 | |
mehrnaz | 2:ef98576cd67b | 66 | if ((flow()>fv) and (measurement_started ==0)) |
mehrnaz | 2:ef98576cd67b | 67 | { |
mehrnaz | 2:ef98576cd67b | 68 | // ttl.printf("ready to detect plateau \n"); |
mehrnaz | 2:ef98576cd67b | 69 | measurement_started = 1; |
mehrnaz | 2:ef98576cd67b | 70 | } |
mehrnaz | 2:ef98576cd67b | 71 | |
mehrnaz | 2:ef98576cd67b | 72 | if ((measurement_started == 1) and (solstart==0)) |
mehrnaz | 2:ef98576cd67b | 73 | { |
mehrnaz | 2:ef98576cd67b | 74 | //ttl.printf("starting...\n"); |
mehrnaz | 2:ef98576cd67b | 75 | |
mehrnaz | 2:ef98576cd67b | 76 | for(m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 77 | { |
mehrnaz | 2:ef98576cd67b | 78 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 79 | myArray[m]=carbon(); |
mehrnaz | 2:ef98576cd67b | 80 | } |
mehrnaz | 2:ef98576cd67b | 81 | while(flags==0) |
mehrnaz | 2:ef98576cd67b | 82 | { |
mehrnaz | 2:ef98576cd67b | 83 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 84 | { |
mehrnaz | 2:ef98576cd67b | 85 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 86 | sum+=myArray[m]; |
mehrnaz | 2:ef98576cd67b | 87 | } |
mehrnaz | 2:ef98576cd67b | 88 | avg=sum/9; |
mehrnaz | 2:ef98576cd67b | 89 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 90 | { |
mehrnaz | 2:ef98576cd67b | 91 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 92 | difSum+=(myArray[m]-avg)*(myArray[m]-avg); //Find sum of difference between value X and mean |
mehrnaz | 2:ef98576cd67b | 93 | } |
mehrnaz | 2:ef98576cd67b | 94 | var=difSum/9; |
mehrnaz | 2:ef98576cd67b | 95 | sigma=sqrt(var); |
mehrnaz | 2:ef98576cd67b | 96 | if (sigma<0.02) |
mehrnaz | 2:ef98576cd67b | 97 | { |
mehrnaz | 2:ef98576cd67b | 98 | //ttl.printf("sol didnt turn on\n"); |
mehrnaz | 2:ef98576cd67b | 99 | for(int m=0;m<8;m++) |
mehrnaz | 2:ef98576cd67b | 100 | { |
mehrnaz | 2:ef98576cd67b | 101 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 102 | myArray[m]=myArray[m+1]; //Shift all carbon to left by 1 |
mehrnaz | 2:ef98576cd67b | 103 | } |
mehrnaz | 2:ef98576cd67b | 104 | myArray[8]=carbon(); |
mehrnaz | 2:ef98576cd67b | 105 | }else |
mehrnaz | 2:ef98576cd67b | 106 | { |
mehrnaz | 2:ef98576cd67b | 107 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 108 | // ttl.printf("sol on\n"); |
mehrnaz | 2:ef98576cd67b | 109 | sol=1; |
mehrnaz | 2:ef98576cd67b | 110 | flags=1; |
mehrnaz | 2:ef98576cd67b | 111 | } |
mehrnaz | 2:ef98576cd67b | 112 | } |
mehrnaz | 2:ef98576cd67b | 113 | solend=1; |
mehrnaz | 2:ef98576cd67b | 114 | solstart =1; |
mehrnaz | 2:ef98576cd67b | 115 | } |
mehrnaz | 2:ef98576cd67b | 116 | if ((measurement_started == 1) and (solend==1)) |
mehrnaz | 2:ef98576cd67b | 117 | { |
mehrnaz | 2:ef98576cd67b | 118 | // ttl.printf("ending...\n"); |
mehrnaz | 2:ef98576cd67b | 119 | |
mehrnaz | 2:ef98576cd67b | 120 | for(m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 121 | { |
mehrnaz | 2:ef98576cd67b | 122 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 123 | myArray[m]=carbon(); |
mehrnaz | 2:ef98576cd67b | 124 | } |
mehrnaz | 2:ef98576cd67b | 125 | while(flage==0) |
mehrnaz | 2:ef98576cd67b | 126 | { |
mehrnaz | 2:ef98576cd67b | 127 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 128 | { |
mehrnaz | 2:ef98576cd67b | 129 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 130 | sum2+=myArray[m]; |
mehrnaz | 2:ef98576cd67b | 131 | } |
mehrnaz | 2:ef98576cd67b | 132 | avg2=sum2/9; |
mehrnaz | 2:ef98576cd67b | 133 | for(int m=0;m<9;m++) |
mehrnaz | 2:ef98576cd67b | 134 | { |
mehrnaz | 2:ef98576cd67b | 135 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 136 | difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); //Find sum of difference between value X and mean |
mehrnaz | 2:ef98576cd67b | 137 | } |
mehrnaz | 2:ef98576cd67b | 138 | var2=difSum2/9; |
mehrnaz | 2:ef98576cd67b | 139 | sigma2=sqrt(var2); |
mehrnaz | 2:ef98576cd67b | 140 | if (sigma2<0.05) |
mehrnaz | 2:ef98576cd67b | 141 | { |
mehrnaz | 2:ef98576cd67b | 142 | // ttl.printf("sol didnt turn on\n"); |
mehrnaz | 2:ef98576cd67b | 143 | for(int m=0;m<8;m++) |
mehrnaz | 2:ef98576cd67b | 144 | { |
mehrnaz | 2:ef98576cd67b | 145 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 146 | myArray[m]=myArray[m+1]; //Shift all carbon to left by 1 |
mehrnaz | 2:ef98576cd67b | 147 | } |
mehrnaz | 2:ef98576cd67b | 148 | myArray[8]=carbon(); |
mehrnaz | 2:ef98576cd67b | 149 | }else |
mehrnaz | 2:ef98576cd67b | 150 | { |
mehrnaz | 2:ef98576cd67b | 151 | ttl.printf("%f,%f\n",flow(), carbon()); |
mehrnaz | 2:ef98576cd67b | 152 | // ttl.printf("sol off\n"); |
mehrnaz | 2:ef98576cd67b | 153 | sol=0; |
mehrnaz | 2:ef98576cd67b | 154 | flage=1; |
mehrnaz | 2:ef98576cd67b | 155 | } |
mehrnaz | 2:ef98576cd67b | 156 | } |
mehrnaz | 2:ef98576cd67b | 157 | solend =0; |
mehrnaz | 2:ef98576cd67b | 158 | bf=0; |
mehrnaz | 2:ef98576cd67b | 159 | fin=1; |
mehrnaz | 2:ef98576cd67b | 160 | } |
mehrnaz | 2:ef98576cd67b | 161 | if((carbon()<10000) and (fin ==1)) |
mehrnaz | 2:ef98576cd67b | 162 | { |
mehrnaz | 2:ef98576cd67b | 163 | |
mehrnaz | 2:ef98576cd67b | 164 | //ttl.printf("ready\n"); |
mehrnaz | 2:ef98576cd67b | 165 | measurement_started =0; |
mehrnaz | 2:ef98576cd67b | 166 | solstart=0; |
mehrnaz | 2:ef98576cd67b | 167 | sum=0; |
mehrnaz | 2:ef98576cd67b | 168 | var=0.0; |
mehrnaz | 2:ef98576cd67b | 169 | sigma=0.0; |
mehrnaz | 2:ef98576cd67b | 170 | difSum=0; |
mehrnaz | 2:ef98576cd67b | 171 | sum2=0; |
mehrnaz | 2:ef98576cd67b | 172 | var2=0.0; |
mehrnaz | 2:ef98576cd67b | 173 | sigma2=0.0; |
mehrnaz | 2:ef98576cd67b | 174 | difSum2=0; |
mehrnaz | 2:ef98576cd67b | 175 | avg2=0; |
mehrnaz | 2:ef98576cd67b | 176 | avg=0; |
mehrnaz | 2:ef98576cd67b | 177 | flags=0; |
mehrnaz | 2:ef98576cd67b | 178 | flage=0; |
mehrnaz | 2:ef98576cd67b | 179 | fin=0; |
mehrnaz | 2:ef98576cd67b | 180 | } |
mehrnaz | 2:ef98576cd67b | 181 | } |
mehrnaz | 2:ef98576cd67b | 182 | } |