![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Breath
Fork of Breath by
windSensor.cpp@1:86d9b36d9132, 2016-12-01 (annotated)
- Committer:
- jamesmsong
- Date:
- Thu Dec 01 03:04:38 2016 +0000
- Revision:
- 1:86d9b36d9132
- Parent:
- 0:397523d4133e
Breath
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
otis22894 | 0:397523d4133e | 1 | #include "windSensor.h" |
jamesmsong | 1:86d9b36d9132 | 2 | #include "MPL3115.h" |
otis22894 | 0:397523d4133e | 3 | |
jamesmsong | 1:86d9b36d9132 | 4 | windSensor :: windSensor(PinName p1, NeoStrip *_strip, PinName p2, PinName p3, int addr) |
jamesmsong | 1:86d9b36d9132 | 5 | : sensor(p1), strip(_strip), barometer(p2, p3, addr) |
otis22894 | 0:397523d4133e | 6 | { |
otis22894 | 0:397523d4133e | 7 | thresh = sensor.read()*1.4; |
otis22894 | 0:397523d4133e | 8 | isBreathing = false; |
otis22894 | 0:397523d4133e | 9 | breathCount = 0; |
jamesmsong | 1:86d9b36d9132 | 10 | spike = false; |
jamesmsong | 1:86d9b36d9132 | 11 | barometer.config(); |
jamesmsong | 1:86d9b36d9132 | 12 | //strip->setBrightness(0.1); |
jamesmsong | 1:86d9b36d9132 | 13 | |
otis22894 | 0:397523d4133e | 14 | } |
otis22894 | 0:397523d4133e | 15 | |
otis22894 | 0:397523d4133e | 16 | void windSensor::startReading() |
otis22894 | 0:397523d4133e | 17 | { |
otis22894 | 0:397523d4133e | 18 | breathReader.attach(this, &windSensor::sample, .002f); |
otis22894 | 0:397523d4133e | 19 | //volatile float a = sensor; |
otis22894 | 0:397523d4133e | 20 | //printf("\nwindsensor reads: %f", a); |
otis22894 | 0:397523d4133e | 21 | //printf("\nthresh is: %f", thresh); |
otis22894 | 0:397523d4133e | 22 | } |
otis22894 | 0:397523d4133e | 23 | |
otis22894 | 0:397523d4133e | 24 | void windSensor::stopReading() |
otis22894 | 0:397523d4133e | 25 | { |
otis22894 | 0:397523d4133e | 26 | breathReader.detach(); |
otis22894 | 0:397523d4133e | 27 | __enable_irq(); |
otis22894 | 0:397523d4133e | 28 | } |
otis22894 | 0:397523d4133e | 29 | |
otis22894 | 0:397523d4133e | 30 | void windSensor::reset() |
otis22894 | 0:397523d4133e | 31 | { |
otis22894 | 0:397523d4133e | 32 | //thresh = sensor*1.2; //set thresh to an appropraite percent above the baseline reading |
otis22894 | 0:397523d4133e | 33 | //Breath = false; // true victims breath crosses thresh |
otis22894 | 0:397523d4133e | 34 | isBreathing = false; //true if we get 3 'Breaths' in the calling programs time window-->means victim is breathing |
otis22894 | 0:397523d4133e | 35 | breathCount = 0; |
otis22894 | 0:397523d4133e | 36 | } |
otis22894 | 0:397523d4133e | 37 | |
otis22894 | 0:397523d4133e | 38 | //call this member function to see if victim is breathing |
otis22894 | 0:397523d4133e | 39 | bool windSensor::breathDetected() |
otis22894 | 0:397523d4133e | 40 | { |
otis22894 | 0:397523d4133e | 41 | return isBreathing; //only return positive if we get the critical number of Breaths in the given time window |
otis22894 | 0:397523d4133e | 42 | } |
otis22894 | 0:397523d4133e | 43 | |
jamesmsong | 1:86d9b36d9132 | 44 | //call this member function to see if there is a pressure spike |
jamesmsong | 1:86d9b36d9132 | 45 | bool windSensor::pressureSpike() |
jamesmsong | 1:86d9b36d9132 | 46 | { |
jamesmsong | 1:86d9b36d9132 | 47 | return spike; //return true if spike occured during give_breath. |
jamesmsong | 1:86d9b36d9132 | 48 | } |
otis22894 | 0:397523d4133e | 49 | |
otis22894 | 0:397523d4133e | 50 | // THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE. |
otis22894 | 0:397523d4133e | 51 | // Timer 2 makes sure that we take a reading every 2 miliseconds |
otis22894 | 0:397523d4133e | 52 | void windSensor:: sample() // triggered when Timer2 counts to 124 |
otis22894 | 0:397523d4133e | 53 | { |
otis22894 | 0:397523d4133e | 54 | __disable_irq(); |
otis22894 | 0:397523d4133e | 55 | float readVal = sensor.read(); // sample |
otis22894 | 0:397523d4133e | 56 | |
otis22894 | 0:397523d4133e | 57 | |
otis22894 | 0:397523d4133e | 58 | //check to see if victim is breathing at all above natural variation in sensor noise |
otis22894 | 0:397523d4133e | 59 | if(readVal >= thresh) { |
otis22894 | 0:397523d4133e | 60 | breathCount++; //increment global counter |
otis22894 | 0:397523d4133e | 61 | } |
otis22894 | 0:397523d4133e | 62 | if(breathCount >= 3) { //should set this higher if want more certainty in victim breathing |
otis22894 | 0:397523d4133e | 63 | isBreathing = true; |
otis22894 | 0:397523d4133e | 64 | } |
otis22894 | 0:397523d4133e | 65 | __enable_irq(); // enable interrupts again |
otis22894 | 0:397523d4133e | 66 | //return; |
otis22894 | 0:397523d4133e | 67 | } |
otis22894 | 0:397523d4133e | 68 | |
otis22894 | 0:397523d4133e | 69 | template<typename T> |
otis22894 | 0:397523d4133e | 70 | void pop_front(std::vector<T>& vec) |
otis22894 | 0:397523d4133e | 71 | { |
otis22894 | 0:397523d4133e | 72 | //assert(!vec.empty()); |
otis22894 | 0:397523d4133e | 73 | vec.erase(vec.begin()); |
otis22894 | 0:397523d4133e | 74 | } |
otis22894 | 0:397523d4133e | 75 | |
otis22894 | 0:397523d4133e | 76 | float windSensor::give_breath(void) |
otis22894 | 0:397523d4133e | 77 | { |
jamesmsong | 1:86d9b36d9132 | 78 | spike = false; |
jamesmsong | 1:86d9b36d9132 | 79 | //strip->initialize(); |
otis22894 | 0:397523d4133e | 80 | c.clear(); |
otis22894 | 0:397523d4133e | 81 | t.reset(); |
otis22894 | 0:397523d4133e | 82 | q.reset(); |
jamesmsong | 1:86d9b36d9132 | 83 | float base = 0.0; |
otis22894 | 0:397523d4133e | 84 | float d = 0.0; //return time for fractional breathes |
otis22894 | 0:397523d4133e | 85 | float e = 0.0; |
otis22894 | 0:397523d4133e | 86 | int i; |
otis22894 | 0:397523d4133e | 87 | float thresh; |
jamesmsong | 1:86d9b36d9132 | 88 | t.start(); |
jamesmsong | 1:86d9b36d9132 | 89 | for(i = 0; i<10; i++) { |
jamesmsong | 1:86d9b36d9132 | 90 | float tempPress = barometer.getPressure(); |
jamesmsong | 1:86d9b36d9132 | 91 | base = base + tempPress; |
otis22894 | 0:397523d4133e | 92 | //printf("%12.5f\n",val); |
otis22894 | 0:397523d4133e | 93 | } |
jamesmsong | 1:86d9b36d9132 | 94 | t.stop(); |
jamesmsong | 1:86d9b36d9132 | 95 | printf("%f\n", t.read()); |
jamesmsong | 1:86d9b36d9132 | 96 | t.reset(); |
jamesmsong | 1:86d9b36d9132 | 97 | base = base/10.0; //get first pressure baseline reading |
jamesmsong | 1:86d9b36d9132 | 98 | printf("Baseline: %f\n", base); |
otis22894 | 0:397523d4133e | 99 | //thresh = a*3.0; //theshold to start breathe timer (ideal multiplier is between 2-3)*/ |
otis22894 | 0:397523d4133e | 100 | thresh = 0.63; |
otis22894 | 0:397523d4133e | 101 | //printf("thresh:%f\n",thresh); |
otis22894 | 0:397523d4133e | 102 | t.start(); |
otis22894 | 0:397523d4133e | 103 | while(1) { |
otis22894 | 0:397523d4133e | 104 | if(t.read() <= 3.0) { |
otis22894 | 0:397523d4133e | 105 | wait(0.1); //necessary to give sensor time to be polled |
otis22894 | 0:397523d4133e | 106 | float val = sensor.read(); |
jamesmsong | 1:86d9b36d9132 | 107 | val = 0.7; //testing, delete after |
otis22894 | 0:397523d4133e | 108 | //printf("%15.5f\n",val); |
otis22894 | 0:397523d4133e | 109 | if(val > thresh) { |
otis22894 | 0:397523d4133e | 110 | //printf("Made it here\n"); |
otis22894 | 0:397523d4133e | 111 | q.start(); |
otis22894 | 0:397523d4133e | 112 | for(i = 0; i<10; i++) { |
otis22894 | 0:397523d4133e | 113 | c.push_back(val); |
otis22894 | 0:397523d4133e | 114 | } |
otis22894 | 0:397523d4133e | 115 | for(i=0; i<10; i++) { |
otis22894 | 0:397523d4133e | 116 | e = e + c[i]; |
otis22894 | 0:397523d4133e | 117 | } |
otis22894 | 0:397523d4133e | 118 | e = e/10.0; //provides slope of 10 most recent samples |
jamesmsong | 1:86d9b36d9132 | 119 | e = 1; //erase |
otis22894 | 0:397523d4133e | 120 | float oldTimerVal = 0; |
otis22894 | 0:397523d4133e | 121 | float timerVal = q.read(); |
jamesmsong | 1:86d9b36d9132 | 122 | printf("Start Breath"); |
otis22894 | 0:397523d4133e | 123 | while( (e > thresh) && (timerVal < 1.0)) { |
otis22894 | 0:397523d4133e | 124 | |
otis22894 | 0:397523d4133e | 125 | pop_front(c);//remove the oldest element |
otis22894 | 0:397523d4133e | 126 | c.push_back(sensor.read()); |
otis22894 | 0:397523d4133e | 127 | for(i=0; i<10; i++) { |
otis22894 | 0:397523d4133e | 128 | e = e + c[i]; |
otis22894 | 0:397523d4133e | 129 | } |
otis22894 | 0:397523d4133e | 130 | e = e/10.0; //provides slope of 10 most recent samples |
jamesmsong | 1:86d9b36d9132 | 131 | e = 1; //erase later |
otis22894 | 0:397523d4133e | 132 | timerVal = q.read(); |
otis22894 | 0:397523d4133e | 133 | if(timerVal >= oldTimerVal + 0.25){ |
jamesmsong | 1:86d9b36d9132 | 134 | //strip->progress(timerVal); |
otis22894 | 0:397523d4133e | 135 | oldTimerVal = timerVal; |
otis22894 | 0:397523d4133e | 136 | } |
jamesmsong | 1:86d9b36d9132 | 137 | float pressure = barometer.getPressure(); |
jamesmsong | 1:86d9b36d9132 | 138 | printf("Pressure: %f\n", pressure); |
jamesmsong | 1:86d9b36d9132 | 139 | if(pressure < .96*base){ |
jamesmsong | 1:86d9b36d9132 | 140 | spike = true; |
jamesmsong | 1:86d9b36d9132 | 141 | printf("SPIKE"); |
jamesmsong | 1:86d9b36d9132 | 142 | } |
otis22894 | 0:397523d4133e | 143 | } |
otis22894 | 0:397523d4133e | 144 | d = q.read(); |
otis22894 | 0:397523d4133e | 145 | if(q.read() > 1.0) { //breathe was greater than thresh and lasted more than 1 sec |
otis22894 | 0:397523d4133e | 146 | t.stop(); q.stop(); |
jamesmsong | 1:86d9b36d9132 | 147 | //strip->progress(1); |
otis22894 | 0:397523d4133e | 148 | return 1.0; //full breath |
otis22894 | 0:397523d4133e | 149 | } else { |
otis22894 | 0:397523d4133e | 150 | t.stop(); q.stop(); |
otis22894 | 0:397523d4133e | 151 | return d; //partial breath |
otis22894 | 0:397523d4133e | 152 | } |
otis22894 | 0:397523d4133e | 153 | } |
otis22894 | 0:397523d4133e | 154 | } else { |
otis22894 | 0:397523d4133e | 155 | t.stop(); q.stop(); |
otis22894 | 0:397523d4133e | 156 | return 0.0; //no breath in 3 seconds |
otis22894 | 0:397523d4133e | 157 | } |
otis22894 | 0:397523d4133e | 158 | } |
otis22894 | 0:397523d4133e | 159 | |
otis22894 | 0:397523d4133e | 160 | } |
otis22894 | 0:397523d4133e | 161 | |
otis22894 | 0:397523d4133e | 162 |