forked from lfwag/HeartSpeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeartSpeed.cpp
290 lines (263 loc) · 7.58 KB
/
HeartSpeed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "HeartSpeed.h"
int heartRate[20]={0}; ///<Heart rate total sample
int sample[sampleSize]={0};
int average_switch=1;
int sum_for_collect;
float average_num;
int sum_high=0;
int sum_low=0;
int num_sum_high=0,num_sum_low=0;///<Two types of situations sum
HEARTCBFUNC cb;
/*!
* @brief The constructor
*
* @brief For get pin and rawDataData
*
* @brief Constructor
*/
HeartSpeed::HeartSpeed(uint8_t _pin,uint8_t _rawData)
:pin(_pin), rawData(_rawData)
{
}
/*!
* @brief Sets the callback function
*
* @brief Obtain the callback function name
*
* @return void
*/
void HeartSpeed::setCB(HEARTCBFUNC cb_)
{
cb = cb_;
}
/*!
* @brief Start program
*
* @brief Set the timer, 5 seconds at a time
*
* @return void
*/
void HeartSpeed::begin(void)
{
MsTimer2::set(5,recv,this);
MsTimer2::start();
}
/*!
* @brief Receive serial data and process
*
* @brief Will receive the data of kalman filter, and square wave modulation
*
* @return void
*/
void HeartSpeed::recv(void *param)
{
HeartSpeed *_this = (HeartSpeed *)param;
int value;
int data = analogRead(_this->pin);
if(_this->rawData==1){
Serial.println(data);
cb(_this->rawData,data);
return;
}
if(_this->sampleNum<sampleSize){
sample[_this->sampleNum]=data;
sample[_this->sampleNum] = _this->kalmanUpdate(0,sample[_this->sampleNum]);///<Square wave using Kalman filter
if(average_switch){
if(sample[_this->sampleNum] >= 0){
sum_high += sample[_this->sampleNum];
num_sum_high++;
}else{
sum_low += sample[_this->sampleNum];
num_sum_low++;
}
}
}else{
for(int i=0;i<(sampleSize-1);i++)
{
sample[i]=sample[i+1]; ///<An array of right.
}
sample[sampleSize-1]=data; ///<The last array update.
sample[_this->sampleNum-1] = _this->kalmanUpdate(0,sample[_this->sampleNum-1]);
average_switch = 0;
if((sample[_this->sampleNum-1]>=(sum_low*1.1/num_sum_low))&&(_this->high==1)){
sample[_this->sampleNum-1]=(-15);
}else{
_this->high=0;
_this->low=1;
}
if((sample[_this->sampleNum-1]<=(sum_high*1.5/num_sum_high))&&(_this->low==1)){
sample[_this->sampleNum-1]=(+25);
}
else{
_this->low=0;
_this->high=1;
}
}
if(_this->sampleNum<sampleSize){
_this->sampleNum++;
}else{
_this->sampleNum =sampleSize;
}
value = _this->calculateSpeed();
if(_this->Count==200){ ///<5ms once,200 times about 1s
if(value > 0){
cb(_this->rawData,value);
}
_this->Count=0;
}else{
_this->Count++;
}
}
/*!
* @brief Calculate heart rate value
*
* @brief Calculate the number of times per minute and time,Each time to beat
*
* @return int
*/
int HeartSpeed::calculateSpeed()
{
int ret = -1;
if(heartRateNum<20){
for(int i=1;i<sampleSize;i++)
{
if(sample[i] != sample[i-1]){ ///<Square, adjoining values, represents the waveform changes
if(go1 == 0){
go1 = i;
}else if(go2 == 0){
go2 = i;
}else if(go3 ==0){
go3 = i;
}else if(go4 ==0){
go4 = i;
break;
}
}
}
if((go1==0)||(go2==0)||(go3==0)||(go4==0)){ ///<Becase of the size is 256,A period of more than 256 values,When a group of arrays cannot represent a complete waveform, then the acquisition.
//nothing to do
}else{
heartRate[heartRateNum]=(go4-go1);
sum += heartRate[heartRateNum];
heartRateNum++;
}
}
if(heartRateNum == 20){
for(int i=1;i<sampleSize;i++)
{
if(sample[i] != sample[i-1]){
if(go1 == 0){
go1 = i;
}else if(go2 == 0){
go2 = i;
}else if(go3 ==0){
go3 = i;
}else if(go4 ==0){
go4 = i;
if((go4 - go1)<85){
go4=0;
}else if((go4-go1)>260){
goInit();
}else{
break;
}
}
}
}
if((go1==0)||(go2==0)||(go3==0)||(go4==0)||((go2-go1)<0)||\
((go3-go2)<0)||((go4-go3)<0)){ ///<Filter exceptions beat wave
goInit();
goto exit;
}else{
sum=0;
for(int i=0;i<19;i++){ ///<After 20 times, nine before migration, last update
heartRate[i]=heartRate[i+1];
sum+=heartRate[i];
}
heartRate[19]=(go4-go1);
sum+=heartRate[19];
}
}
if(heartRateNum==20){
float num_min = 1000,num_max = 0;
for(int i=0;i<20;i++){
if(heartRate[i]<num_min){
num_min = heartRate[i];
}
if(heartRate[i]>num_max){
num_max = heartRate[i];
}
}
heart = 60000/(((sum)*5)/(heartRateNum));
//heart = 60000/(((sum-num_max-num_min)*5)/(heartRateNum-2)); ///<Minus the maximum and minimum values
sum=0;
goInit();
ret = (int)heart;
goto exit;
}
goInit();
exit:
return ret;
}
/*!
* @brief Initializes the heart rate period
*
* @brief Make go1,go2,go3,go4 equal 0
*
* @return void
*/
void HeartSpeed::goInit(){
go1=0; ///<First change
go2=0; ///<Second change
go3=0; ///<Third change
go4=0; ///<Fourth change
}
/*!
* @brief Kalman filtering
*
* @brief Remove the varied data, make the waveform more smoothly
*
* @return void
*/
float HeartSpeed::kalmanUpdate(float Accel,float Gyro){
Angle+=(Gyro - Q_bias) * dt; ///<Prior estimates
Pdot[0]=Q_angle - PP[0][1] - PP[1][0]; ///< Pk-A priori estimation error covariance of differential
Pdot[1]=- PP[1][1];
Pdot[2]=- PP[1][1];
Pdot[3]=Q_gyro;
PP[0][0] += Pdot[0] * dt; ///< Pk-A priori estimation error covariance of differential-integral
PP[0][1] += Pdot[1] * dt; ///< =A priori estimation error covariance
PP[1][0] += Pdot[2] * dt;
PP[1][1] += Pdot[3] * dt;
Angle_err = Accel - Angle; ///<zk-Prior estimates
PCt_0 = C_0 * PP[0][0];
PCt_1 = C_0 * PP[1][0];
E = R_angle + C_0 * PCt_0;
K_0 = PCt_0 / E;
K_1 = PCt_1 / E;
t_0 = PCt_0;
t_1 = C_0 * PP[0][1];
PP[0][0] -= K_0 * t_0; ///<Posteriori error covariance
PP[0][1] -= K_0 * t_1;
PP[1][0] -= K_1 * t_0;
PP[1][1] -= K_1 * t_1;
Angle += K_0 * Angle_err; ///<A posteriori estimation
Q_bias += K_1 * Angle_err; ///<A posteriori estimation
Gyro_y = Gyro - Q_bias; ///<Output values (a posteriori) differential = angular velocity
Angle = Angle + (((Accel-Angle)*0.5 + Gyro_y)*0.001);
return Angle;
}
/******************************************************************************
Copyright (C) <2016> <jianghao>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: [email protected]
******************************************************************************/