![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
heart rate test
main.cpp@6:95e2e305e802, 2015-04-14 (annotated)
- Committer:
- zchen78
- Date:
- Tue Apr 14 05:16:32 2015 +0000
- Revision:
- 6:95e2e305e802
- Parent:
- 5:6db460f13fcc
tons of hack around
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:6db460f13fcc | 24 | float 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 | 0:901c98b3591a | 35 | if (data - previous_data > 0){ |
zchen78 | 0:901c98b3591a | 36 | //increasing |
zchen78 | 0:901c98b3591a | 37 | printf("Increasing \n"); |
zchen78 | 0:901c98b3591a | 38 | //do nothing |
zchen78 | 0:901c98b3591a | 39 | } |
zchen78 | 0:901c98b3591a | 40 | else if(data - previous_data < 0 && !foundMax){ |
zchen78 | 1:b7f42e8e04fc | 41 | //check if foundMax |
zchen78 | 2:d7d21a7491aa | 42 | printf("Decrease\n"); |
zchen78 | 1:b7f42e8e04fc | 43 | if(firstRun){ |
zchen78 | 1:b7f42e8e04fc | 44 | float diff = data - global_max; |
zchen78 | 4:3edb2dedc40b | 45 | float negative = -1*difference; |
zchen78 | 6:95e2e305e802 | 46 | // printf("diff = %f\n",diff); |
zchen78 | 6:95e2e305e802 | 47 | // printf("difference = %f,\t \t \t negative = %f \n", difference, negative); |
zchen78 | 6:95e2e305e802 | 48 | //printf("data = %f\n",data); |
zchen78 | 4:3edb2dedc40b | 49 | printf("global_max = %f\n",global_max); |
zchen78 | 6:95e2e305e802 | 50 | //printf("------------ End\n"); |
zchen78 | 4:3edb2dedc40b | 51 | if(diff < difference && diff >negative) { |
zchen78 | 1:b7f42e8e04fc | 52 | printf("Within the range of first max, confident\n"); |
zchen78 | 1:b7f42e8e04fc | 53 | printf("found max, data= %f\n",data); |
zchen78 | 1:b7f42e8e04fc | 54 | foundMax = true; |
zchen78 | 1:b7f42e8e04fc | 55 | count1 = running_count; |
zchen78 | 1:b7f42e8e04fc | 56 | max1 = data; |
zchen78 | 1:b7f42e8e04fc | 57 | firstRun = false; |
zchen78 | 1:b7f42e8e04fc | 58 | } |
zchen78 | 1:b7f42e8e04fc | 59 | } |
zchen78 | 1:b7f42e8e04fc | 60 | else{ |
zchen78 | 1:b7f42e8e04fc | 61 | printf("found max, data= %f\n",data); |
zchen78 | 1:b7f42e8e04fc | 62 | foundMax = true; |
zchen78 | 1:b7f42e8e04fc | 63 | count1 = running_count; |
zchen78 | 1:b7f42e8e04fc | 64 | max1 = data; |
zchen78 | 1:b7f42e8e04fc | 65 | } |
zchen78 | 0:901c98b3591a | 66 | } |
zchen78 | 4:3edb2dedc40b | 67 | else if(data - previous_data < 0 && foundMax){ |
zchen78 | 6:95e2e305e802 | 68 | printf("Decrease \t \n"); |
zchen78 | 6:95e2e305e802 | 69 | if(((max1-data) < difference) && ((max1-data) > -1*difference)){ |
zchen78 | 3:fe75390a6f30 | 70 | //found second max; |
zchen78 | 4:3edb2dedc40b | 71 | printf("max1 = %f\n",max1); |
zchen78 | 3:fe75390a6f30 | 72 | printf("found second max,data = %f \n",data); |
zchen78 | 3:fe75390a6f30 | 73 | count2 = running_count; |
zchen78 | 3:fe75390a6f30 | 74 | float period = (count2 - count1)* count_period; |
zchen78 | 6:95e2e305e802 | 75 | float BPM = (1.0/period) * 60.0; |
zchen78 | 3:fe75390a6f30 | 76 | printf("Period = %f\n",period); |
zchen78 | 3:fe75390a6f30 | 77 | printf("BPM = %f\n",BPM); |
zchen78 | 6:95e2e305e802 | 78 | // exit(1); |
zchen78 | 6:95e2e305e802 | 79 | wait(5); |
zchen78 | 6:95e2e305e802 | 80 | reset(); |
zchen78 | 3:fe75390a6f30 | 81 | printf("\n"); |
zchen78 | 3:fe75390a6f30 | 82 | printf("\n"); |
zchen78 | 3:fe75390a6f30 | 83 | printf("\n"); |
zchen78 | 6:95e2e305e802 | 84 | } |
zchen78 | 6:95e2e305e802 | 85 | else { |
zchen78 | 6:95e2e305e802 | 86 | if((running_count - count1)*count_period > 1.5) { |
zchen78 | 6:95e2e305e802 | 87 | printf("Optimization filter by hand \t, reset\n"); |
zchen78 | 6:95e2e305e802 | 88 | reset(); |
zchen78 | 6:95e2e305e802 | 89 | } |
zchen78 | 3:fe75390a6f30 | 90 | } |
zchen78 | 0:901c98b3591a | 91 | } |
zchen78 | 4:3edb2dedc40b | 92 | else{ |
zchen78 | 4:3edb2dedc40b | 93 | } |
zchen78 | 6:95e2e305e802 | 94 | //reset if not found max for 5 seconds. |
zchen78 | 6:95e2e305e802 | 95 | if(!foundMax && running_count*count_period>5){ |
zchen78 | 2:d7d21a7491aa | 96 | reset(); |
zchen78 | 2:d7d21a7491aa | 97 | } |
zchen78 | 2:d7d21a7491aa | 98 | } |
zchen78 | 2:d7d21a7491aa | 99 | |
zchen78 | 2:d7d21a7491aa | 100 | void reset(){ |
zchen78 | 2:d7d21a7491aa | 101 | //reset |
zchen78 | 2:d7d21a7491aa | 102 | printf("reset\n"); |
zchen78 | 6:95e2e305e802 | 103 | global_max = normalization(); |
zchen78 | 6:95e2e305e802 | 104 | printf("new normalization = %f",global_max); |
zchen78 | 0:901c98b3591a | 105 | count1 = 0; |
zchen78 | 1:b7f42e8e04fc | 106 | count2 = 0; |
zchen78 | 0:901c98b3591a | 107 | foundMax = false; |
zchen78 | 6:95e2e305e802 | 108 | max1 = 0.0; |
zchen78 | 1:b7f42e8e04fc | 109 | running_count = 0; |
zchen78 | 6:95e2e305e802 | 110 | firstRun = true; |
zchen78 | 0:901c98b3591a | 111 | } |
zchen78 | 0:901c98b3591a | 112 | |
zchen78 | 2:d7d21a7491aa | 113 | float normalization(){ |
zchen78 | 2:d7d21a7491aa | 114 | float max_input = -1.0; |
zchen78 | 2:d7d21a7491aa | 115 | float data_input; |
zchen78 | 1:b7f42e8e04fc | 116 | //normalize the data input |
zchen78 | 1:b7f42e8e04fc | 117 | //find the max in 5 seconds; |
zchen78 | 6:95e2e305e802 | 118 | for (int x=0;x<50;x++){ |
zchen78 | 1:b7f42e8e04fc | 119 | data_input = sensor.read() * 3.3; |
zchen78 | 1:b7f42e8e04fc | 120 | if(data_input > max_input)max_input = data_input; |
zchen78 | 1:b7f42e8e04fc | 121 | wait_ms(50); |
zchen78 | 1:b7f42e8e04fc | 122 | } |
zchen78 | 6:95e2e305e802 | 123 | difference = max_input * 0.35; |
zchen78 | 1:b7f42e8e04fc | 124 | return max_input; |
zchen78 | 1:b7f42e8e04fc | 125 | } |
zchen78 | 1:b7f42e8e04fc | 126 | |
zchen78 | 1:b7f42e8e04fc | 127 | |
zchen78 | 0:901c98b3591a | 128 | void findOxygenSaturation(){ |
zchen78 | 0:901c98b3591a | 129 | float v = log10f(sensor_visible.read()*3.3) * visible; |
zchen78 | 0:901c98b3591a | 130 | float iv = log10f(sensor_invisible.read()*3.3) * invisible; |
zchen78 | 0:901c98b3591a | 131 | printf("visible = %f\n",v); |
zchen78 | 0:901c98b3591a | 132 | printf("invisible = %f\n",iv); |
zchen78 | 0:901c98b3591a | 133 | float ratio = v/iv; |
zchen78 | 0:901c98b3591a | 134 | printf("ratio = %f\n",ratio); |
zchen78 | 0:901c98b3591a | 135 | float spo = -0.3393 * ratio + 1.1595 ; |
zchen78 | 0:901c98b3591a | 136 | printf("spo = %f\n",spo); |
zchen78 | 0:901c98b3591a | 137 | } |
zchen78 | 0:901c98b3591a | 138 | |
zchen78 | 0:901c98b3591a | 139 | |
zchen78 | 0:901c98b3591a | 140 | int main() { |
zchen78 | 0:901c98b3591a | 141 | led2 = 1; |
zchen78 | 0:901c98b3591a | 142 | //comment out the below line to test heart rate |
zchen78 | 0:901c98b3591a | 143 | //the heart rate test subroutine, once it find the heart rate, it will terminate the program. |
zchen78 | 0:901c98b3591a | 144 | //so make sure you reset the MBED |
zchen78 | 1:b7f42e8e04fc | 145 | global_max = normalization(); |
zchen78 | 1:b7f42e8e04fc | 146 | printf("normalization = %f",global_max); |
zchen78 | 1:b7f42e8e04fc | 147 | flipper.attach(&flip, count_period); // the address of the function to be attached (flip) and the interval (1 seconds) |
zchen78 | 0:901c98b3591a | 148 | //read OxygenSaturation Every two second. |
zchen78 | 0:901c98b3591a | 149 | //use pin 19 for visible light input |
zchen78 | 0:901c98b3591a | 150 | //use pin 18 for invisible light input |
zchen78 | 1:b7f42e8e04fc | 151 | // flipper2.attach(&findOxygenSaturation,2.0); |
zchen78 | 1:b7f42e8e04fc | 152 | |
zchen78 | 0:901c98b3591a | 153 | while(1) { |
zchen78 | 0:901c98b3591a | 154 | } |
zchen78 | 0:901c98b3591a | 155 | } |