Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: iAQ_Core Adafruit_SGP30_mbed mbed BME680
Revision 3:3d51f8870e91, committed 2019-07-25
- Comitter:
- mehrnaz
- Date:
- Thu Jul 25 11:17:46 2019 +0000
- Parent:
- 2:ef98576cd67b
- Child:
- 4:54dc2a95c130
- Commit message:
- final version of Handheld used for KST
Changed in this revision
--- a/SoftSerial.lib Fri Jul 12 15:58:11 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/Sissors/code/SoftSerial/#a0029614de72
--- a/flow.h Fri Jul 12 15:58:11 2019 +0000 +++ b/flow.h Thu Jul 25 11:17:46 2019 +0000 @@ -1,9 +1,21 @@ #include "mbed.h" +// First the inputs and pins are defined + AnalogIn flowIn(PA_0); -Serial co2(PC_10,PC_11); //USED IN MAIN +Serial co2(PC_10,PC_11); //Serial ttl(A1,A0); +AnalogIn sensor1(PC_0); +AnalogIn sensor2(PC_1); +AnalogIn sensor3(PC_2); +AnalogIn sensor4(PC_3); +AnalogIn sensor5(PA_4); +AnalogIn sensor6(PA_5); +AnalogIn sensor7(PA_6); +AnalogIn sensor8(PA_7); +AnalogIn temp(PA_1); +///////////////////// FLOW LOOP ///////////////////////// float finalflow; float flowVal1; float flowVal2; @@ -13,8 +25,6 @@ float bpArray[10]; float fp;float sp; float FPressure; -int value; -// float flow() { while(1) @@ -22,7 +32,8 @@ wait(0.01); flowVal1=3.3*flowIn; //Logic level 3.3 flowVal2 = 1.5*flowVal1; //5v - P1 =(125*flowVal2)-62.5; + P1 =(125*flowVal2)-62.5; //Pressure + //making the value of pressure positive inside the SQRT function: if(flag==0) { finalflow=0; @@ -39,13 +50,13 @@ { FPressure=P1-fp; finalflow=(0.24*sqrt(FPressure)); //flow in litter per min - //ttl.printf("Flow: %f\n",finalflow); return finalflow; } } } - +///////////////////// CO2 LOOP ///////////////////////// +int value; float carbon() { bool allow = false; @@ -56,6 +67,8 @@ while(1) { c = co2.getc(); + //based on the user manual PDF for the CO2 sensor, the value starts with "Z" + //and we need to extract the right number of CO2 value if(c=='Z') { allow = true; } @@ -66,9 +79,8 @@ count++; } - if(count>=6) { //NOT SURE IF 5 OR 6 + if(count>=6) { value = ((co2_measure[0]-'0')*100000+co2_measure[1]-'0')*10000+(co2_measure[2]-'0')*1000+(co2_measure[3]-'0')*100; - //ttl.printf("CO2: %d\n",value); float CAR; CAR=(float)value/10000; count=0; @@ -79,3 +91,90 @@ } } +///////////////////// TEMPERATURE LOOP ///////////////////////// + +float t2Cel; + +float getTemp() +{ + while(1) { + float B = 3478; //Define thermistor constant + float rRef=10e3; // Define reference resistance + float r1=10e3; // Define thermistor resistance at 25 C + float t1=25+273; // Define thermistor initial temperature s 25C in Kelvin + float x = temp.read(); //Measure input voltage at pin A0 in bits + float v = 3.3*x; //Convert bits into voltage + float r2 = (3.3*rRef/v)-rRef; //Convert voltage into thermistor resistance + float t2 = (B*t1)/(B-t1*log(r1/r2)); //Convert thermistor resistance into temperature in Kelvin (log means natural logarithm ln) + t2Cel = t2-273; //Convert temperature from Kelvin to Celcius + return t2Cel; +// printf("Temp: %f\n", t2Cel); + } +} + +///////////////////// 8-CHANNEL SENSOR LOOP ///////////////////////// + +float s1() +{ + float sen1; + while(1){ + sen1=sensor1*3.3; + return sen1; + } +} +float s2() +{ + float sen2; + while(1){ + sen2=sensor2*3.3; + return sen2; + } +} +float s3() +{ + float sen3; + while(1){ + sen3=sensor3*3.3; + return sen3; + } +} +float s4() +{ + float sen4; + while(1){ + sen4=sensor4*3.3; + return sen4; + } +} +float s5() +{ + float sen5; + while(1){ + sen5=sensor5*3.3; + return sen5; + } +} +float s6() +{ + float sen6; + while(1){ + sen6=sensor6*3.3; + return sen6; + } +} +float s7() +{ + float sen7; + while(1){ + sen7=sensor7*3.3; + return sen7; + } +} +float s8() +{ + float sen8; + while(1){ + sen8=sensor8*3.3; + return sen8; + } +} \ No newline at end of file
--- a/main.cpp Fri Jul 12 15:58:11 2019 +0000 +++ b/main.cpp Thu Jul 25 11:17:46 2019 +0000 @@ -2,55 +2,84 @@ #include "flow.h" ///////////////////////// -// In this version of the program developed for the Breath project, flow and CO2 +// In this version of the program developed for the Breath project, flow and CO2, as well as 8 channel sensors, // are measured in a separate .h file called: "flow.h" which is included in the -// main code. Data is streamed using a serial connection (TTL cable or Bluetooth) -// without any intruption because of loop calculations. +// main code. So 10 sets of data is streamed using a serial connection (TTL cable or Bluetooth) +// without any intruption. // This version is especially suitable to be used for KST. // Also, a solenoid would be turned on and off based on calculating the standard deviation in CO2 profile. // +// START POINT: calculates SD for 9 samples of CO2, if it's grater than 0.02 it enables the solenoid. +// END POINT: calculates SD for 9 samples of CO2, if it's grater thatn 0.05 it disables the solenoid. +// +// You can easily change the threshold of Standard deviation to detect plateau +// // Generated by: Mehrnaz Javadipour ////////////////////////// -Serial ttl(PA_2,PA_3); //TTL cable TX,RX +Serial ttl(PC_12,PD_2); //TTL cable TX,RX DigitalOut sol(PC_5); //Solenoid: Digital Output +PwmOut led(PB_6); + + int main() { ttl.baud(9600); //baudrate for the serial connection flow(); //calling flow from flow.h carbon(); //calling CO2 from flow.h - int bf=0; + s1(); //calling 8 channels from flow.h + s2(); + s3(); + s4(); + s5(); + s6(); + s7(); + s8(); + getTemp(); //calling Temperature from flow.h + + ////////////////////////////// + // I defined a flag for each section of specific functions, so by enabling the + // flag the section starts and by disabling the flag it finishes the section. + // at the end of the program, I reset the flags so it would be ready for the next loop. + ///////////////////////////// + + int bf=0; //FLAG for detecting base flow int i=0; - float bfArray[4]; - float sf=0; - float fv=0; - int myArray[9]; - int measurement_started=0; - unsigned int sum=0; - long double var=0.0; - float sigma=0.0; - int difSum=0; - unsigned int sum2=0; + float bfArray[4]; //sampling flow for finding the average base flow + float sf=0; //sum of flow samples for calculating base flow + float fv=0; //final value of base flow + + int measurement_started=0; //FLAG for starting calculations after detecting breath + + int solstart=0; //FLAG for starting calculations for detecting plateau + int m=0; + int myArray[9]; //sampling 9 values of CO2 + unsigned int sum=0; //sum of 9 samples of CO2 + int avg=0; //average of 9 samples of CO2 + int difSum=0; //used for the Standard deviation algorithm + long double var=0.0; //used for the Standard deviation algorithm + float sigma=0.0; //final value for standar deviation + int flags=0; //FLAG for keep taking samples from CO2 profile when it's too early to detect plateau + + int solend=0; //FLAG for ending calculations for detecting plateau + unsigned int sum2=0; //same as before; used for finding standard deviation long double var2=0.0; float sigma2=0.0; int difSum2=0; - int avg2=0; - int avg=0; - int m=0; - int flags=0; - int flage=0; - int solstart=0; - int solend=0; + int avg2=0; + int flage=0; //FLAG for keep taking samples from CO2 profile when it's too early to finish plateau + int fin=0; - //float CO_2; + while(1) { - //CO_2=(float)carbon()/10000; - ttl.printf("%f,%f\n",flow(), carbon()); - if (bf==0) + led=1.00f; //an LED is fully turned on at the beginning, the brightness will be reduced when the plateau is detected. + 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()); + + if (bf==0) //finding base flow before breath { for(i=0; i<4; i++) { @@ -63,105 +92,116 @@ bf=1; } + //Starts calculations when it detects breathing into the device: + if ((flow()>fv) and (measurement_started ==0)) { - // ttl.printf("ready to detect plateau \n"); measurement_started = 1; } + //Starts detecting plateau: + if ((measurement_started == 1) and (solstart==0)) { - //ttl.printf("starting...\n"); - + //Takes 9 samples of CO2: + //I have also included printing the values inside the loops so we don't loose any data during calculatins. for(m=0;m<9;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); + 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()); myArray[m]=carbon(); } while(flags==0) { + //While "flags" is enabled, keeps calculating the standard deviation. for(int m=0;m<9;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); + 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()); sum+=myArray[m]; } avg=sum/9; for(int m=0;m<9;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); + 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()); difSum+=(myArray[m]-avg)*(myArray[m]-avg); //Find sum of difference between value X and mean } var=difSum/9; sigma=sqrt(var); if (sigma<0.02) { - //ttl.printf("sol didnt turn on\n"); + + //if SD is less than 0.02 it means that it is too early to start the plateau + //So we shift all but the first sample and define the new set of arrays: + for(int m=0;m<8;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); - myArray[m]=myArray[m+1]; //Shift all carbon to left by 1 + 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()); + myArray[m]=myArray[m+1]; //Shift all CO2 values to the left by 1 value } - myArray[8]=carbon(); - }else + myArray[8]=carbon(); //assign a new value for the 9th sample + } + //The new set of arrays are now generated and is sent back to be used for preveious SD calculations. + //If sigma for the new set is still small, a newer set will be generated and replaced + //Otherwise, it's accepted and will turn on the solenoid: + else { - ttl.printf("%f,%f\n",flow(), carbon()); - // ttl.printf("sol on\n"); - sol=1; - flags=1; + 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()); + sol=1; //Solenoid is ON + led=0.50f; //The brightness is reduced to half during the plateau + flags=1; //breakes the while loop } } - solend=1; + solend=1; //prepares the next section for finishing the plateau solstart =1; } if ((measurement_started == 1) and (solend==1)) { - // ttl.printf("ending...\n"); + // same process happens for finishing the plateau: for(m=0;m<9;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); + 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()); myArray[m]=carbon(); } while(flage==0) { for(int m=0;m<9;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); + 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()); sum2+=myArray[m]; } avg2=sum2/9; for(int m=0;m<9;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); - difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); //Find sum of difference between value X and mean + 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()); + difSum2+=(myArray[m]-avg2)*(myArray[m]-avg2); } var2=difSum2/9; sigma2=sqrt(var2); if (sigma2<0.05) { - // ttl.printf("sol didnt turn on\n"); + // here we defined the end threshold to be 0.05, it can be changed later based on experiment results for(int m=0;m<8;m++) { - ttl.printf("%f,%f\n",flow(), carbon()); - myArray[m]=myArray[m+1]; //Shift all carbon to left by 1 + 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()); + myArray[m]=myArray[m+1]; } myArray[8]=carbon(); }else { - ttl.printf("%f,%f\n",flow(), carbon()); - // ttl.printf("sol off\n"); - sol=0; - flage=1; + 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()); + sol=0; //Solenoid is OFF + flage=1; //breakes the loop } } - solend =0; - bf=0; - fin=1; + solend =0; //end of this section + led=1.00f; //LED is back to full brightness + bf=0; //reset the detecting base flow flag + fin=1; //enables the next section flag } if((carbon()<10000) and (fin ==1)) { - - //ttl.printf("ready\n"); + //User has to wait for the CO2 level to drop less than 1% before testing again. + //Once it is less than 1%, all the flags and parameters used in calculations are reset measurement_started =0; solstart=0; sum=0;