heart rate test

Dependencies:   mbed

Committer:
zchen78
Date:
Tue Apr 14 03:08:26 2015 +0000
Revision:
3:fe75390a6f30
Parent:
2:d7d21a7491aa
Child:
4:3edb2dedc40b
revised algorithm to find 2nd max.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zchen78 0:901c98b3591a 1 #include "mbed.h"
zchen78 0:901c98b3591a 2 #include "math.h"
zchen78 0:901c98b3591a 3 Ticker flipper;
zchen78 0:901c98b3591a 4 Ticker flipper2;
zchen78 0:901c98b3591a 5 DigitalOut led2(LED2);
zchen78 0:901c98b3591a 6 AnalogIn sensor(p20);
zchen78 0:901c98b3591a 7 AnalogIn sensor_visible(p19);
zchen78 0:901c98b3591a 8 AnalogIn sensor_invisible(p18);
zchen78 0:901c98b3591a 9
zchen78 0:901c98b3591a 10 //680 nm
zchen78 0:901c98b3591a 11 #define visible 68
zchen78 0:901c98b3591a 12 //940 nm
zchen78 0:901c98b3591a 13 #define invisible 94
zchen78 0:901c98b3591a 14 float data =0;
zchen78 0:901c98b3591a 15 float previous_data =0;
zchen78 0:901c98b3591a 16 int count1=-1;
zchen78 0:901c98b3591a 17 int count2=-1;
zchen78 0:901c98b3591a 18 float max1 =0.0;
zchen78 0:901c98b3591a 19 float max2 =0.0;
zchen78 3:fe75390a6f30 20 float global_max =0.0;
zchen78 0:901c98b3591a 21 int running_count=0;
zchen78 0:901c98b3591a 22 bool foundMax = false;
zchen78 1:b7f42e8e04fc 23 bool firstRun = true;
zchen78 2:d7d21a7491aa 24 #define difference 0.05
zchen78 0:901c98b3591a 25 #define count_period 0.1
zchen78 2:d7d21a7491aa 26 void reset();
zchen78 2:d7d21a7491aa 27 float normalization();
zchen78 0:901c98b3591a 28 void flip() {
zchen78 0:901c98b3591a 29 led2 = !led2;
zchen78 0:901c98b3591a 30 previous_data = data;
zchen78 0:901c98b3591a 31 // printf("Data = %f\t",data);
zchen78 0:901c98b3591a 32 data = sensor.read() * 3.3;
zchen78 0:901c98b3591a 33 running_count++;
zchen78 0:901c98b3591a 34 // printf("data = %f\n",data);
zchen78 2:d7d21a7491aa 35
zchen78 0:901c98b3591a 36 if (data - previous_data > 0){
zchen78 0:901c98b3591a 37 //increasing
zchen78 0:901c98b3591a 38 printf("Increasing \n");
zchen78 0:901c98b3591a 39 //do nothing
zchen78 0:901c98b3591a 40 }
zchen78 0:901c98b3591a 41 else if(data - previous_data < 0 && !foundMax){
zchen78 1:b7f42e8e04fc 42 //check if foundMax
zchen78 2:d7d21a7491aa 43 printf("Decrease\n");
zchen78 1:b7f42e8e04fc 44 if(firstRun){
zchen78 1:b7f42e8e04fc 45 float diff = data - global_max;
zchen78 2:d7d21a7491aa 46 if(diff < difference || diff >-1*difference) {
zchen78 1:b7f42e8e04fc 47 printf("Within the range of first max, confident\n");
zchen78 1:b7f42e8e04fc 48 printf("found max, data= %f\n",data);
zchen78 1:b7f42e8e04fc 49 foundMax = true;
zchen78 1:b7f42e8e04fc 50 count1 = running_count;
zchen78 1:b7f42e8e04fc 51 max1 = data;
zchen78 1:b7f42e8e04fc 52 firstRun = false;
zchen78 1:b7f42e8e04fc 53 }
zchen78 1:b7f42e8e04fc 54 }
zchen78 1:b7f42e8e04fc 55 else{
zchen78 1:b7f42e8e04fc 56 printf("found max, data= %f\n",data);
zchen78 1:b7f42e8e04fc 57 foundMax = true;
zchen78 1:b7f42e8e04fc 58 count1 = running_count;
zchen78 1:b7f42e8e04fc 59 max1 = data;
zchen78 1:b7f42e8e04fc 60 }
zchen78 0:901c98b3591a 61 }
zchen78 0:901c98b3591a 62 else{
zchen78 3:fe75390a6f30 63 if((max1-data < difference || max1-data > -1*difference) && foundMax){
zchen78 3:fe75390a6f30 64 //found second max;
zchen78 3:fe75390a6f30 65 printf("found second max,data = %f \n",data);
zchen78 3:fe75390a6f30 66 count2 = running_count;
zchen78 3:fe75390a6f30 67 float period = (count2 - count1)* count_period;
zchen78 3:fe75390a6f30 68 float BPM = (1/period) * 60;
zchen78 3:fe75390a6f30 69 printf("Period = %f\n",period);
zchen78 3:fe75390a6f30 70 printf("BPM = %f\n",BPM);
zchen78 3:fe75390a6f30 71 exit(1);
zchen78 3:fe75390a6f30 72 printf("\n");
zchen78 3:fe75390a6f30 73 printf("\n");
zchen78 3:fe75390a6f30 74 printf("\n");
zchen78 3:fe75390a6f30 75 }
zchen78 0:901c98b3591a 76 }
zchen78 2:d7d21a7491aa 77
zchen78 2:d7d21a7491aa 78 if(!foundMax && running_count>250){
zchen78 2:d7d21a7491aa 79 reset();
zchen78 2:d7d21a7491aa 80 global_max = normalization();
zchen78 2:d7d21a7491aa 81 printf("new normalization = %f",global_max);
zchen78 2:d7d21a7491aa 82 }
zchen78 2:d7d21a7491aa 83 }
zchen78 2:d7d21a7491aa 84
zchen78 2:d7d21a7491aa 85 void reset(){
zchen78 2:d7d21a7491aa 86 //reset
zchen78 2:d7d21a7491aa 87 printf("reset\n");
zchen78 0:901c98b3591a 88 count1 = 0;
zchen78 1:b7f42e8e04fc 89 count2 = 0;
zchen78 0:901c98b3591a 90 foundMax = false;
zchen78 1:b7f42e8e04fc 91 max1 = 0;
zchen78 1:b7f42e8e04fc 92 running_count = 0;
zchen78 0:901c98b3591a 93 }
zchen78 0:901c98b3591a 94
zchen78 2:d7d21a7491aa 95 float normalization(){
zchen78 2:d7d21a7491aa 96 float max_input = -1.0;
zchen78 2:d7d21a7491aa 97 float data_input;
zchen78 1:b7f42e8e04fc 98 //normalize the data input
zchen78 1:b7f42e8e04fc 99 //find the max in 5 seconds;
zchen78 1:b7f42e8e04fc 100 for (int x=0;x<100;x++){
zchen78 1:b7f42e8e04fc 101 data_input = sensor.read() * 3.3;
zchen78 1:b7f42e8e04fc 102 if(data_input > max_input)max_input = data_input;
zchen78 1:b7f42e8e04fc 103 wait_ms(50);
zchen78 1:b7f42e8e04fc 104 }
zchen78 1:b7f42e8e04fc 105 return max_input;
zchen78 1:b7f42e8e04fc 106 }
zchen78 1:b7f42e8e04fc 107
zchen78 1:b7f42e8e04fc 108
zchen78 0:901c98b3591a 109 void findOxygenSaturation(){
zchen78 0:901c98b3591a 110 float v = log10f(sensor_visible.read()*3.3) * visible;
zchen78 0:901c98b3591a 111 float iv = log10f(sensor_invisible.read()*3.3) * invisible;
zchen78 0:901c98b3591a 112 printf("visible = %f\n",v);
zchen78 0:901c98b3591a 113 printf("invisible = %f\n",iv);
zchen78 0:901c98b3591a 114 float ratio = v/iv;
zchen78 0:901c98b3591a 115 printf("ratio = %f\n",ratio);
zchen78 0:901c98b3591a 116 float spo = -0.3393 * ratio + 1.1595 ;
zchen78 0:901c98b3591a 117 printf("spo = %f\n",spo);
zchen78 0:901c98b3591a 118 }
zchen78 0:901c98b3591a 119
zchen78 0:901c98b3591a 120
zchen78 0:901c98b3591a 121 int main() {
zchen78 0:901c98b3591a 122 led2 = 1;
zchen78 0:901c98b3591a 123 //comment out the below line to test heart rate
zchen78 0:901c98b3591a 124 //the heart rate test subroutine, once it find the heart rate, it will terminate the program.
zchen78 0:901c98b3591a 125 //so make sure you reset the MBED
zchen78 1:b7f42e8e04fc 126 global_max = normalization();
zchen78 1:b7f42e8e04fc 127 printf("normalization = %f",global_max);
zchen78 1:b7f42e8e04fc 128 flipper.attach(&flip, count_period); // the address of the function to be attached (flip) and the interval (1 seconds)
zchen78 0:901c98b3591a 129 //read OxygenSaturation Every two second.
zchen78 0:901c98b3591a 130 //use pin 19 for visible light input
zchen78 0:901c98b3591a 131 //use pin 18 for invisible light input
zchen78 1:b7f42e8e04fc 132 // flipper2.attach(&findOxygenSaturation,2.0);
zchen78 1:b7f42e8e04fc 133
zchen78 0:901c98b3591a 134 while(1) {
zchen78 0:901c98b3591a 135 }
zchen78 0:901c98b3591a 136 }