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