-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXively_writeYUN7_BPM.ino
344 lines (270 loc) · 9.55 KB
/
Xively_writeYUN7_BPM.ino
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <ads1292r.h>
#include <SPI.h>
#include <Process.h>
#include <FIR.h>
#define BPM_WINDOW 10
#define FILTERTAPS 5
#define APIKEY "QRLlAi3IqpTr4DKIoI7dZrVPA8H9QIDU5hMZItzBMnN1bj4S" // your pachube api key
#define FEEDID 1853201959 // your feed ID
// set up net client info:
const unsigned long postingInterval = 1000; //delay between updates to xively.com
unsigned long lastRequest = 0; // when you last made a request
String dataString = "";
long int bpm = 0;
ads1292r ADS1292;
FIR fir;
volatile char DataPacketHeader[5];
volatile char DataPacketFooter[2];
volatile int datalen = 135;
unsigned long time;
volatile byte SPI_RX_Buff[150] ;
//volatile static int SPI_RX_Buff_Count = 0;
volatile char *SPI_RX_Buff_Ptr;
volatile int Responsebyte = false;
volatile unsigned int pckt =0 , buff=0,t=0 , j1=0,j2=0;
volatile unsigned long int EEG_Ch1_Data[150],EEG_Ch2_Data[150];
volatile unsigned char datac[150];
unsigned long ueegtemp = 0,Pkt_Counter=0;
signed long seegtemp=0, value;
volatile int i;
signed long adcval_prev;
float feegtemp ,output;
void setup()
{
Bridge.begin();
// initalize the data ready andchip select pins:
pinMode(ADS1292_DRDY_PIN, INPUT); //6
pinMode(ADS1292_CS_PIN, OUTPUT); //7
pinMode(ADS1292_START_PIN, OUTPUT); //5
pinMode(ADS1292_PWDN_PIN, OUTPUT); //4
// Serial.begin(9600); // Baudrate for serial communica
//initalize ADS1292 slave
ADS1292.ads1292_Init();
//ADS1292.ads1292_Reset();
Serial.begin(9600);
float coef[FILTERTAPS] = { 0.021, 0.096, 0.146, 0.096, 0.021};
fir.setCoefficients(coef);
//declare gain coefficient to scale the output back to normal
float gain = 1; // set to 1 and input unity to see what this needs to be
fir.setGain(gain);
Serial.println("Xively client");
lastRequest = millis();
}
void loop()
{
long now = millis();
//Do update and send every postingInterval
if (now - lastRequest >= postingInterval) {
updateData();
sendData();
lastRequest = now;
}
}
//It averages the bpm over a length of BPM_WINDOW to avoid sudden changes
int average_BPM( int bpm)
{
static int bpm_sample_count;
static int bpm_store[BPM_WINDOW];
int bpm_sum =0, bpm_avg=0;
if (bpm_sample_count < BPM_WINDOW)
bpm_store[bpm_sample_count++] = bpm;
else
{
for(i=0 ; i<BPM_WINDOW-1 ; i++)
bpm_store[i] = bpm_store[i+1] ;
bpm_store[BPM_WINDOW-1] = bpm;
}
Serial.println("\nbpm_store\t");
for(i=0 ; i<BPM_WINDOW ; i++)
{
Serial.print(bpm_store[i]);
Serial.print("\t");
}
Serial.print("\nbpm_sample_count\t");
Serial.println(bpm_sample_count);
for(i=0 ; i<BPM_WINDOW ; i++)
bpm_sum += bpm_store[i];
Serial.print("\nbpm_sum\t");
Serial.println(bpm_sum);
bpm_avg = bpm_sum/(bpm_sample_count);
Serial.print("\nbpm_avg\t");
Serial.println(bpm_avg);
return (bpm_avg);
}
void updateData() {
signed long adcval, val, val_high = 0, val_low =0;
long int t1, t2;
int flag = 0;
adcval = ReadADC();
adcval_prev = adcval;
val_high = val_low = adcval;
// caluculate the higs and lows for 2 sec ie 375 samples
for(int i=0; i<250; i++)
{
adcval = ReadADC();
val = adcval;
if(val > val_high)
val_high = val;
if(val < val_low)
val_low = val;
Serial.print(val);
Serial.print("\t");
}
Serial.println("val_low");
Serial.println(val_low);
Serial.println("val_high");
Serial.println(val_high);
Serial.println("val_high - val_low");
Serial.println(val_high - val_low);
// caluculate the timeperiod between 2 peaks ie >90% of signal for next 2 sec
for(int i=0; i<250; i++)
{
adcval = ReadADC();
val = adcval;
//int calval = map(val, val_low, val_high, 0, 100000);
// calval = map(val, val_low, val_high, 0, 10000)
//int calval = map(val, 0, val_high, 0, 100000);
Serial.print(val);
Serial.print("\t");
if (val < val_low*0.70)
{
if (flag == 0)
{
t1 = millis();
flag = 1;
Serial.println("\npeak1 time");
Serial.println(t1);
Serial.println("peak1 value");
Serial.println(val);
//delay(100); //QRS duration is 200ms
}
else if (flag == 1)
{
t2 = millis();
Serial.println("\npeak2 time");
Serial.println(t2);
Serial.println("peak2 value");
Serial.println(val);
flag = 2;
Serial.println("timeperiod"); //caluculating timeperiod of beats
Serial.println(t2 - t1);
Serial.print("BPM is ---------------->");
bpm = (60000)/(t2-t1); //Caluculating the BPM from timeperiod.
Serial.println(bpm);
//delay(100); //QRS duration is 200ms
bpm = average_BPM(bpm);
}
Serial.print("peakfound value");
Serial.println(val);
while ((adcval = ReadADC()) < val_low*0.70) // wait for the peak to end
{
delay (1);
}
}
}
delay(250);
dataString = "BPM,";
dataString += bpm;
// delay(1000);
}
//IIR filter to tune out the dc component.
float Filter(float fInput) {
static float fLast = 0.0f;
int a = -1;
float fResult = fInput+a*fLast;
fLast = fInput;
return 0.5*fResult;
}
signed long ReadADC(void)
{
volatile int SPI_RX_Buff_Count = 0;
while(1)
{
//volatile int SPI_RX_Buff_Count = 0;
if((digitalRead(ADS1292_DRDY_PIN)) == LOW)
{
SPI_RX_Buff_Ptr = ADS1292.ads1292_Read_Data();
Responsebyte = true;
//Serial.print("DRDY low: ");
}
if(Responsebyte == true)
{
for(i = 0; i < 9; i++)
{
SPI_RX_Buff[SPI_RX_Buff_Count++] = *(SPI_RX_Buff_Ptr + i);
}
Responsebyte = false;
}
if(SPI_RX_Buff_Count >= 9)
{
pckt = 0; j1=0; j2=0;
for(i=3;i<9;i+=9)
{
EEG_Ch2_Data[j2++]= (unsigned char)SPI_RX_Buff[i+3];
EEG_Ch2_Data[j2++]= (unsigned char)SPI_RX_Buff[i+4];
EEG_Ch2_Data[j2++]= (unsigned char)SPI_RX_Buff[i+5];
}
for(t=0; t< 1 ; t++)
{
buff = 0;
Pkt_Counter++; //if(Pkt_Counter > 0) Pkt_Counter = 0x00;
//datac[buff++] = 0xA0; // sync0
// datac[buff++] = 36; //sync1
datac[buff++] = (unsigned char)(Pkt_Counter >> 24);
datac[buff++] = (unsigned char)(Pkt_Counter >> 16);
datac[buff++] = (unsigned char)(Pkt_Counter >> 8);
datac[buff++] = (unsigned char)(Pkt_Counter );
ueegtemp = (unsigned long) ((EEG_Ch2_Data[pckt]<<16)|(EEG_Ch2_Data[pckt+1]<<8)|EEG_Ch2_Data[pckt+2]);
//Serial.print(ueegtemp,HEX);
//Serial.print(" ");
ueegtemp = (unsigned long) (ueegtemp<<8);
seegtemp = (signed long) (ueegtemp);
seegtemp = (signed long) (seegtemp>>8);
feegtemp = (float)((seegtemp)*50);
output = fir.process(feegtemp); //FIR filtering for smoothening the ECG
output = Filter(output); //IIR filter to filter out dc
seegtemp = (signed long)((output));
//seegtemp = abs(seegtemp);
return(seegtemp);
//delay(100);
}
SPI_RX_Buff_Count = 0;
}
}
}
//To send the BPM to Xiveley
void sendData() {
// form the string for the API header parameter:
String apiString = "X-ApiKey: ";
apiString += APIKEY;
// form the string for the URL parameter:
String url = "https://api.xively.com/v2/feeds/";
url += FEEDID;
url += ".csv";
// Send the HTTP PUT request
// Is better to declare the Process here, so when the
// sendData function finishes the resources are immediately
// released. Declaring it global works too, BTW.
Process xively;
Serial.print("\n\nSending data... ");
xively.begin("curl");
xively.addParameter("-k");
xively.addParameter("--request");
xively.addParameter("PUT");
xively.addParameter("--data");
xively.addParameter(dataString);
xively.addParameter("--header");
xively.addParameter(apiString);
xively.addParameter(url);
Serial.println(dataString);
Serial.println(apiString);
Serial.println(url);
xively.run();
Serial.println("done!");
// If there's incoming data from the net connection,
// send it out the Serial:
while (xively.available()>0) {
char c = xively.read();
Serial.write(c);
}
}