Tapton School Project to monitor and log SEN students physiological condition using GSR (conductivity) & Heart Rate and displaying output and logging the data to an SD card
DFRobot_Heartrate.cpp@0:0e26bf847b57, 2018-02-09 (annotated)
- Committer:
- raheel123
- Date:
- Fri Feb 09 08:34:23 2018 +0000
- Revision:
- 0:0e26bf847b57
Tapton school SENtinel project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
raheel123 | 0:0e26bf847b57 | 1 | #include "DFRobot_Heartrate.h" |
raheel123 | 0:0e26bf847b57 | 2 | |
raheel123 | 0:0e26bf847b57 | 3 | #define debug_ 1 |
raheel123 | 0:0e26bf847b57 | 4 | |
raheel123 | 0:0e26bf847b57 | 5 | |
raheel123 | 0:0e26bf847b57 | 6 | DigitalIn hrme (p21); |
raheel123 | 0:0e26bf847b57 | 7 | Serial pcu (USBTX, USBRX); // pc's usb tx, rx |
raheel123 | 0:0e26bf847b57 | 8 | |
raheel123 | 0:0e26bf847b57 | 9 | unsigned int value[SAMPLE_NUMBER]={0}; ///< Initialize sampling point value |
raheel123 | 0:0e26bf847b57 | 10 | unsigned int valueCount_ = 255; |
raheel123 | 0:0e26bf847b57 | 11 | /*! |
raheel123 | 0:0e26bf847b57 | 12 | * @brief Get a Sample values |
raheel123 | 0:0e26bf847b57 | 13 | * |
raheel123 | 0:0e26bf847b57 | 14 | * @brief Get a Sample values |
raheel123 | 0:0e26bf847b57 | 15 | * |
raheel123 | 0:0e26bf847b57 | 16 | * @return unsigned int |
raheel123 | 0:0e26bf847b57 | 17 | */ |
raheel123 | 0:0e26bf847b57 | 18 | unsigned int DFRobot_Heartrate::getValue(unsigned int pin) |
raheel123 | 0:0e26bf847b57 | 19 | { |
raheel123 | 0:0e26bf847b57 | 20 | valueCount_++; |
raheel123 | 0:0e26bf847b57 | 21 | if(valueCount_ >= SAMPLE_NUMBER){ |
raheel123 | 0:0e26bf847b57 | 22 | pcu.printf("Resetting counter"); |
raheel123 | 0:0e26bf847b57 | 23 | valueCount_ = 0; |
raheel123 | 0:0e26bf847b57 | 24 | } |
raheel123 | 0:0e26bf847b57 | 25 | value[valueCount_] = hrme; |
raheel123 | 0:0e26bf847b57 | 26 | return(value[valueCount_]); |
raheel123 | 0:0e26bf847b57 | 27 | |
raheel123 | 0:0e26bf847b57 | 28 | } |
raheel123 | 0:0e26bf847b57 | 29 | |
raheel123 | 0:0e26bf847b57 | 30 | /*! |
raheel123 | 0:0e26bf847b57 | 31 | * @brief Get a sample |
raheel123 | 0:0e26bf847b57 | 32 | * |
raheel123 | 0:0e26bf847b57 | 33 | * @brief Get the current location |
raheel123 | 0:0e26bf847b57 | 34 | * |
raheel123 | 0:0e26bf847b57 | 35 | * @return unsigned int |
raheel123 | 0:0e26bf847b57 | 36 | */ |
raheel123 | 0:0e26bf847b57 | 37 | unsigned int DFRobot_Heartrate::getCnt(void) |
raheel123 | 0:0e26bf847b57 | 38 | { |
raheel123 | 0:0e26bf847b57 | 39 | return(valueCount_); |
raheel123 | 0:0e26bf847b57 | 40 | } |
raheel123 | 0:0e26bf847b57 | 41 | |
raheel123 | 0:0e26bf847b57 | 42 | /*! |
raheel123 | 0:0e26bf847b57 | 43 | * @brief Points to a valid node of the wave rise |
raheel123 | 0:0e26bf847b57 | 44 | * |
raheel123 | 0:0e26bf847b57 | 45 | * @brief When the sampling point value increased 4 times in a row, considered to be a heartbeat |
raheel123 | 0:0e26bf847b57 | 46 | * |
raheel123 | 0:0e26bf847b57 | 47 | * @return char |
raheel123 | 0:0e26bf847b57 | 48 | */ |
raheel123 | 0:0e26bf847b57 | 49 | |
raheel123 | 0:0e26bf847b57 | 50 | char DFRobot_Heartrate::maxNumber(unsigned int count) |
raheel123 | 0:0e26bf847b57 | 51 | { |
raheel123 | 0:0e26bf847b57 | 52 | unsigned int temp1,temp2; |
raheel123 | 0:0e26bf847b57 | 53 | for(int i=0;i<4;i++){ |
raheel123 | 0:0e26bf847b57 | 54 | if(count<i){ |
raheel123 | 0:0e26bf847b57 | 55 | temp1 = SAMPLE_NUMBER+count-i; |
raheel123 | 0:0e26bf847b57 | 56 | }else{ |
raheel123 | 0:0e26bf847b57 | 57 | temp1 = count-i; |
raheel123 | 0:0e26bf847b57 | 58 | } |
raheel123 | 0:0e26bf847b57 | 59 | if(count<i+1){ |
raheel123 | 0:0e26bf847b57 | 60 | temp2 = SAMPLE_NUMBER+(count-1)-i; |
raheel123 | 0:0e26bf847b57 | 61 | }else{ |
raheel123 | 0:0e26bf847b57 | 62 | temp2 = (count-1)-i; |
raheel123 | 0:0e26bf847b57 | 63 | } |
raheel123 | 0:0e26bf847b57 | 64 | if(value[temp1]<=value[temp2])return(0); |
raheel123 | 0:0e26bf847b57 | 65 | } |
raheel123 | 0:0e26bf847b57 | 66 | if(valueFlag){ |
raheel123 | 0:0e26bf847b57 | 67 | valueFlag=1; |
raheel123 | 0:0e26bf847b57 | 68 | return(0); |
raheel123 | 0:0e26bf847b57 | 69 | }else{ |
raheel123 | 0:0e26bf847b57 | 70 | valueFlag=1; |
raheel123 | 0:0e26bf847b57 | 71 | return(1); |
raheel123 | 0:0e26bf847b57 | 72 | } |
raheel123 | 0:0e26bf847b57 | 73 | } |
raheel123 | 0:0e26bf847b57 | 74 | |
raheel123 | 0:0e26bf847b57 | 75 | /*! |
raheel123 | 0:0e26bf847b57 | 76 | * @brief Waveform down processing |
raheel123 | 0:0e26bf847b57 | 77 | * |
raheel123 | 0:0e26bf847b57 | 78 | * @brief When the sample values decreased four times in a row, clear the heartbeat |
raheel123 | 0:0e26bf847b57 | 79 | * |
raheel123 | 0:0e26bf847b57 | 80 | * @return void |
raheel123 | 0:0e26bf847b57 | 81 | */ |
raheel123 | 0:0e26bf847b57 | 82 | |
raheel123 | 0:0e26bf847b57 | 83 | void DFRobot_Heartrate::minNumber(unsigned int count) |
raheel123 | 0:0e26bf847b57 | 84 | { |
raheel123 | 0:0e26bf847b57 | 85 | unsigned int temp1,temp2; |
raheel123 | 0:0e26bf847b57 | 86 | for(int i=0;i<4;i++){ |
raheel123 | 0:0e26bf847b57 | 87 | if(count<i){ |
raheel123 | 0:0e26bf847b57 | 88 | temp1 = SAMPLE_NUMBER+count-i; |
raheel123 | 0:0e26bf847b57 | 89 | }else{ |
raheel123 | 0:0e26bf847b57 | 90 | temp1 = count-i; |
raheel123 | 0:0e26bf847b57 | 91 | } |
raheel123 | 0:0e26bf847b57 | 92 | if(count<i+1){ |
raheel123 | 0:0e26bf847b57 | 93 | temp2 = SAMPLE_NUMBER+(count-1)-i; |
raheel123 | 0:0e26bf847b57 | 94 | }else{ |
raheel123 | 0:0e26bf847b57 | 95 | temp2 = (count-1)-i; |
raheel123 | 0:0e26bf847b57 | 96 | } |
raheel123 | 0:0e26bf847b57 | 97 | if(value[temp1]>=value[temp2])return; |
raheel123 | 0:0e26bf847b57 | 98 | } |
raheel123 | 0:0e26bf847b57 | 99 | valueFlag = 0; |
raheel123 | 0:0e26bf847b57 | 100 | |
raheel123 | 0:0e26bf847b57 | 101 | } |
raheel123 | 0:0e26bf847b57 | 102 | |
raheel123 | 0:0e26bf847b57 | 103 | |
raheel123 | 0:0e26bf847b57 | 104 | /*! |
raheel123 | 0:0e26bf847b57 | 105 | * @brief Get heart rate value |
raheel123 | 0:0e26bf847b57 | 106 | * |
raheel123 | 0:0e26bf847b57 | 107 | * @brief Sample ten times in a row beating heart rate value is computed |
raheel123 | 0:0e26bf847b57 | 108 | * |
raheel123 | 0:0e26bf847b57 | 109 | * @return unsigned int |
raheel123 | 0:0e26bf847b57 | 110 | */ |
raheel123 | 0:0e26bf847b57 | 111 | |
raheel123 | 0:0e26bf847b57 | 112 | unsigned int DFRobot_Heartrate::analogGetRate(void) |
raheel123 | 0:0e26bf847b57 | 113 | { |
raheel123 | 0:0e26bf847b57 | 114 | |
raheel123 | 0:0e26bf847b57 | 115 | static unsigned int timeFlag; |
raheel123 | 0:0e26bf847b57 | 116 | static unsigned long sampleTime[10]; |
raheel123 | 0:0e26bf847b57 | 117 | unsigned long valueTime_; |
raheel123 | 0:0e26bf847b57 | 118 | minNumber(valueCount_); |
raheel123 | 0:0e26bf847b57 | 119 | if(maxNumber(valueCount_)){ |
raheel123 | 0:0e26bf847b57 | 120 | nowTim = clock_ms(); |
raheel123 | 0:0e26bf847b57 | 121 | unsigned int difTime = nowTim - lastTim; |
raheel123 | 0:0e26bf847b57 | 122 | lastTim = nowTim; |
raheel123 | 0:0e26bf847b57 | 123 | |
raheel123 | 0:0e26bf847b57 | 124 | if(difTime>300 || difTime<2000){ |
raheel123 | 0:0e26bf847b57 | 125 | sampleTime[timeFlag++] = difTime; |
raheel123 | 0:0e26bf847b57 | 126 | if(timeFlag > 9)timeFlag=0; |
raheel123 | 0:0e26bf847b57 | 127 | } |
raheel123 | 0:0e26bf847b57 | 128 | if(0 == sampleTime[9]){ |
raheel123 | 0:0e26bf847b57 | 129 | pcu.printf("Wait for valid data !"); |
raheel123 | 0:0e26bf847b57 | 130 | return(0); |
raheel123 | 0:0e26bf847b57 | 131 | } |
raheel123 | 0:0e26bf847b57 | 132 | |
raheel123 | 0:0e26bf847b57 | 133 | unsigned int Arrange[10]={0}; |
raheel123 | 0:0e26bf847b57 | 134 | for(int i=0;i<10;i++){ |
raheel123 | 0:0e26bf847b57 | 135 | Arrange[i] = sampleTime[i]; |
raheel123 | 0:0e26bf847b57 | 136 | } |
raheel123 | 0:0e26bf847b57 | 137 | unsigned int Arrange_=0; |
raheel123 | 0:0e26bf847b57 | 138 | for(int i=9;i>0;i--){ |
raheel123 | 0:0e26bf847b57 | 139 | for(int j=0;j<i;j++){ |
raheel123 | 0:0e26bf847b57 | 140 | if(Arrange[j] > Arrange[j+1]){ |
raheel123 | 0:0e26bf847b57 | 141 | Arrange_ = Arrange[j]; |
raheel123 | 0:0e26bf847b57 | 142 | Arrange[j] = Arrange[j+1]; |
raheel123 | 0:0e26bf847b57 | 143 | Arrange[j+1] = Arrange_; |
raheel123 | 0:0e26bf847b57 | 144 | } |
raheel123 | 0:0e26bf847b57 | 145 | } |
raheel123 | 0:0e26bf847b57 | 146 | } |
raheel123 | 0:0e26bf847b57 | 147 | if((Arrange[7]-Arrange[3])>150){ |
raheel123 | 0:0e26bf847b57 | 148 | pcu.printf("Wait for valid data !"); |
raheel123 | 0:0e26bf847b57 | 149 | return(0); |
raheel123 | 0:0e26bf847b57 | 150 | } |
raheel123 | 0:0e26bf847b57 | 151 | |
raheel123 | 0:0e26bf847b57 | 152 | Arrange_ = 0; |
raheel123 | 0:0e26bf847b57 | 153 | for(int i=3;i<=7;i++){ |
raheel123 | 0:0e26bf847b57 | 154 | Arrange_ += Arrange[i]; |
raheel123 | 0:0e26bf847b57 | 155 | } |
raheel123 | 0:0e26bf847b57 | 156 | |
raheel123 | 0:0e26bf847b57 | 157 | valueTime_ = 300000/Arrange_;///<60*1000*(7-2) |
raheel123 | 0:0e26bf847b57 | 158 | return((unsigned int)valueTime_); |
raheel123 | 0:0e26bf847b57 | 159 | } |
raheel123 | 0:0e26bf847b57 | 160 | return(0); |
raheel123 | 0:0e26bf847b57 | 161 | } |
raheel123 | 0:0e26bf847b57 | 162 | |
raheel123 | 0:0e26bf847b57 | 163 | /*! |
raheel123 | 0:0e26bf847b57 | 164 | * @brief Get heart rate value |
raheel123 | 0:0e26bf847b57 | 165 | * |
raheel123 | 0:0e26bf847b57 | 166 | * @brief Sample ten times in a row beating heart rate value is computed |
raheel123 | 0:0e26bf847b57 | 167 | * |
raheel123 | 0:0e26bf847b57 | 168 | * @return unsigned int |
raheel123 | 0:0e26bf847b57 | 169 | */ |
raheel123 | 0:0e26bf847b57 | 170 | |
raheel123 | 0:0e26bf847b57 | 171 | unsigned int DFRobot_Heartrate::digitalGetRate(void) |
raheel123 | 0:0e26bf847b57 | 172 | { |
raheel123 | 0:0e26bf847b57 | 173 | |
raheel123 | 0:0e26bf847b57 | 174 | static unsigned int timeFlag; |
raheel123 | 0:0e26bf847b57 | 175 | static unsigned long sampleTime[10]; |
raheel123 | 0:0e26bf847b57 | 176 | unsigned long valueTime_; |
raheel123 | 0:0e26bf847b57 | 177 | unsigned int count_; |
raheel123 | 0:0e26bf847b57 | 178 | |
raheel123 | 0:0e26bf847b57 | 179 | if(valueCount_){ |
raheel123 | 0:0e26bf847b57 | 180 | count_ = valueCount_-1; |
raheel123 | 0:0e26bf847b57 | 181 | }else{ |
raheel123 | 0:0e26bf847b57 | 182 | count_ = SAMPLE_NUMBER-1; |
raheel123 | 0:0e26bf847b57 | 183 | } |
raheel123 | 0:0e26bf847b57 | 184 | if((value[valueCount_]==1)){ |
raheel123 | 0:0e26bf847b57 | 185 | |
raheel123 | 0:0e26bf847b57 | 186 | nowTim = clock_ms(); |
raheel123 | 0:0e26bf847b57 | 187 | unsigned int difTime = nowTim - lastTim; |
raheel123 | 0:0e26bf847b57 | 188 | lastTim = nowTim; |
raheel123 | 0:0e26bf847b57 | 189 | pcu.printf("dif time %d, valueCount_ %d, value[valueCount_] %d\n\r ", difTime, valueCount_, value[valueCount_]); |
raheel123 | 0:0e26bf847b57 | 190 | |
raheel123 | 0:0e26bf847b57 | 191 | if(difTime>300 || difTime<2000){ |
raheel123 | 0:0e26bf847b57 | 192 | sampleTime[timeFlag++] = difTime; |
raheel123 | 0:0e26bf847b57 | 193 | if(timeFlag > 9)timeFlag=0; |
raheel123 | 0:0e26bf847b57 | 194 | } |
raheel123 | 0:0e26bf847b57 | 195 | if(0 == sampleTime[9]){ |
raheel123 | 0:0e26bf847b57 | 196 | pcu.printf("Wait for valid data !"); |
raheel123 | 0:0e26bf847b57 | 197 | return(0); |
raheel123 | 0:0e26bf847b57 | 198 | } |
raheel123 | 0:0e26bf847b57 | 199 | |
raheel123 | 0:0e26bf847b57 | 200 | unsigned int Arrange[10]={0}; |
raheel123 | 0:0e26bf847b57 | 201 | for(int i=0;i<10;i++){ |
raheel123 | 0:0e26bf847b57 | 202 | Arrange[i] = sampleTime[i]; |
raheel123 | 0:0e26bf847b57 | 203 | } |
raheel123 | 0:0e26bf847b57 | 204 | unsigned int Arrange_=0; |
raheel123 | 0:0e26bf847b57 | 205 | for(int i=9;i>0;i--){ |
raheel123 | 0:0e26bf847b57 | 206 | for(int j=0;j<i;j++){ |
raheel123 | 0:0e26bf847b57 | 207 | if(Arrange[j] > Arrange[j+1]){ |
raheel123 | 0:0e26bf847b57 | 208 | Arrange_ = Arrange[j]; |
raheel123 | 0:0e26bf847b57 | 209 | Arrange[j] = Arrange[j+1]; |
raheel123 | 0:0e26bf847b57 | 210 | Arrange[j+1] = Arrange_; |
raheel123 | 0:0e26bf847b57 | 211 | } |
raheel123 | 0:0e26bf847b57 | 212 | } |
raheel123 | 0:0e26bf847b57 | 213 | } |
raheel123 | 0:0e26bf847b57 | 214 | if((Arrange[7]-Arrange[3])>120){ |
raheel123 | 0:0e26bf847b57 | 215 | pcu.printf("Wait for valid data !"); |
raheel123 | 0:0e26bf847b57 | 216 | return(0); |
raheel123 | 0:0e26bf847b57 | 217 | } |
raheel123 | 0:0e26bf847b57 | 218 | |
raheel123 | 0:0e26bf847b57 | 219 | Arrange_ = 0; |
raheel123 | 0:0e26bf847b57 | 220 | for(int i=3;i<=7;i++){ |
raheel123 | 0:0e26bf847b57 | 221 | Arrange_ += Arrange[i]; |
raheel123 | 0:0e26bf847b57 | 222 | } |
raheel123 | 0:0e26bf847b57 | 223 | |
raheel123 | 0:0e26bf847b57 | 224 | valueTime_ = 300000/Arrange_;///<60*1000*(7-2) |
raheel123 | 0:0e26bf847b57 | 225 | return((unsigned int)valueTime_); |
raheel123 | 0:0e26bf847b57 | 226 | } |
raheel123 | 0:0e26bf847b57 | 227 | return(0); |
raheel123 | 0:0e26bf847b57 | 228 | |
raheel123 | 0:0e26bf847b57 | 229 | } |
raheel123 | 0:0e26bf847b57 | 230 | |
raheel123 | 0:0e26bf847b57 | 231 | /*! |
raheel123 | 0:0e26bf847b57 | 232 | * @brief Get heart rate value |
raheel123 | 0:0e26bf847b57 | 233 | * |
raheel123 | 0:0e26bf847b57 | 234 | * @brief Sample ten times in a row beating heart rate value is computed |
raheel123 | 0:0e26bf847b57 | 235 | * |
raheel123 | 0:0e26bf847b57 | 236 | * @return unsigned int |
raheel123 | 0:0e26bf847b57 | 237 | */ |
raheel123 | 0:0e26bf847b57 | 238 | unsigned int DFRobot_Heartrate::getRate(void) |
raheel123 | 0:0e26bf847b57 | 239 | { |
raheel123 | 0:0e26bf847b57 | 240 | if(mode_ == DIGITAL_MODE){ |
raheel123 | 0:0e26bf847b57 | 241 | return(digitalGetRate()); |
raheel123 | 0:0e26bf847b57 | 242 | }else{ |
raheel123 | 0:0e26bf847b57 | 243 | return(analogGetRate()); |
raheel123 | 0:0e26bf847b57 | 244 | } |
raheel123 | 0:0e26bf847b57 | 245 | } |
raheel123 | 0:0e26bf847b57 | 246 | |
raheel123 | 0:0e26bf847b57 | 247 | |
raheel123 | 0:0e26bf847b57 | 248 | |
raheel123 | 0:0e26bf847b57 | 249 | /****************************************************************************** |
raheel123 | 0:0e26bf847b57 | 250 | Copyright (C) <2015> <linfeng> |
raheel123 | 0:0e26bf847b57 | 251 | This program is free software: you can redistribute it and/or modify |
raheel123 | 0:0e26bf847b57 | 252 | it under the terms of the GNU General Public License as published by |
raheel123 | 0:0e26bf847b57 | 253 | the Free Software Foundation, either version 3 of the License, or |
raheel123 | 0:0e26bf847b57 | 254 | (at your option) any later version. |
raheel123 | 0:0e26bf847b57 | 255 | This program is distributed in the hope that it will be useful, |
raheel123 | 0:0e26bf847b57 | 256 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
raheel123 | 0:0e26bf847b57 | 257 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
raheel123 | 0:0e26bf847b57 | 258 | GNU General Public License for more details. |
raheel123 | 0:0e26bf847b57 | 259 | You should have received a copy of the GNU General Public License |
raheel123 | 0:0e26bf847b57 | 260 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
raheel123 | 0:0e26bf847b57 | 261 | Contact: Musk.lin@dfrobot.com |
raheel123 | 0:0e26bf847b57 | 262 | ******************************************************************************/ |
raheel123 | 0:0e26bf847b57 | 263 | |
raheel123 | 0:0e26bf847b57 | 264 | |
raheel123 | 0:0e26bf847b57 | 265 | |
raheel123 | 0:0e26bf847b57 | 266 |