-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBody_Fat.ino
362 lines (263 loc) · 8.31 KB
/
Body_Fat.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "Wire.h"
#define button 2
#define SLAVE_ADDR 0x0D
#define ADDR_PTR 0xB0
#define START_FREQ_R1 0x82
#define START_FREQ_R2 0x83
#define START_FREQ_R3 0x84
#define FREG_INCRE_R1 0x85
#define FREG_INCRE_R2 0x86
#define FREG_INCRE_R3 0x87
#define NUM_INCRE_R1 0x88
#define NUM_INCRE_R2 0x89
#define NUM_SCYCLES_R1 0x8A
#define NUM_SCYCLES_R2 0x8B
#define RE_DATA_R1 0x94
#define RE_DATA_R2 0x95
#define IMG_DATA_R1 0x96
#define IMG_DATA_R2 0x97
#define TEMP_R1 0x92
#define TEMP_R2 0x93
#define CTRL_REG 0x80
#define CTRL_REG2 0x81
#define STATUS_REG 0x8F
const float MCLK = 16.776*pow(10,6); // AD5933 Internal Clock Speed 16.776 MHz
const float start_freq = 50*pow(10,3); // Set start freq, < 100Khz
const float incre_freq = 1*pow(10,2); // Set freq increment
const int incre_num = 49; // Set number of increments; < 511
const float Z_Val = 500;
char state;
void setup() {
Wire.begin();
Serial.begin(115200);
pinMode(button, INPUT);
//nop - clear ctrl-reg
writeData(CTRL_REG,0x0);
//reset ctrl register
writeData(CTRL_REG2,0x10);
programReg();
}
void loop(){
//Read state and enter FSM
// if(Serial.available()>0) {
// state = Serial.read();
// //FSM
// switch(state) {
// case 'A': //Program Registers
// programReg();
// break;
//
// case 'B': //Measure Temperature
// measureTemperature();
// break;
//
// case 'C':
// runSweep();
// delay(1000);
// break;
/////Programming Device Registers/////
programReg();
runSweep();
delay(10000);
//}
// Serial.flush();
// }
}
void programReg(){
// Set Range 1, PGA gain 1
writeData(CTRL_REG,0x01);
// Set settling cycles
writeData(NUM_SCYCLES_R1, 0x07);
writeData(NUM_SCYCLES_R2, 0xFF);
// Start frequency of 1kHz
writeData(START_FREQ_R1, getFrequency(start_freq,1));
writeData(START_FREQ_R2, getFrequency(start_freq,2));
writeData(START_FREQ_R3, getFrequency(start_freq,3));
// Increment by 1 kHz
writeData(FREG_INCRE_R1, getFrequency(incre_freq,1));
writeData(FREG_INCRE_R2, getFrequency(incre_freq,2));
writeData(FREG_INCRE_R3, getFrequency(incre_freq,3));
// Points in frequency sweep (100), max 511
writeData(NUM_INCRE_R1, (incre_num & 0x001F00)>>0x08 );
writeData(NUM_INCRE_R2, (incre_num & 0x0000FF));
}
void runSweep() {
short re;
short img;
double freq;
double mag;
double phase;
double Impedance;
double GF;
double FFM;
double Wt,Ht,Sex,TBW,age,ECF;
double BF;
double tot = 0;
double magcount = 0;
double impcount = 0;
double avgmag;
double totimp = 0;
double avgimp;
int i=0;
programReg();
// 1. Standby '10110000' Mask D8-10 of avoid tampering with gains
writeData(CTRL_REG,(readData(CTRL_REG) & 0x07) | 0xB0);
// 2. Initialize sweep
writeData(CTRL_REG,(readData(CTRL_REG) & 0x07) | 0x10);
// 3. Start sweep
writeData(CTRL_REG,(readData(CTRL_REG) & 0x07) | 0x20);
while((readData(STATUS_REG) & 0x07) < 4 ) { // Check that status reg != 4, sweep not complete
delay(100); // delay between measurements
int flag = readData(STATUS_REG)& 2;
if (flag==2) {
byte R1 = readData(RE_DATA_R1);
byte R2 = readData(RE_DATA_R2);
re = (R1 << 8) | R2;
R1 = readData(IMG_DATA_R1);
R2 = readData(IMG_DATA_R2);
img = (R1 << 8) | R2;
freq = start_freq + i*incre_freq;
mag = sqrt(pow(double(re),2)+pow(double(img),2));
tot = tot+mag;
magcount = magcount+1;
//GF = (1/.22)/19.06;
// GF = .000000402889443; //calibrated on 1K
GF = 0.000001306616544; //calibrated on 500 ohm 50khz
// GF = 0.000001374217510; //calibrated on 500 ohm 5khz
//GF = .00000843328818; //calibrated without any more connections
Impedance = 1/(GF*mag);
Impedance= Z_Val*Z_Val/Impedance; //just to fix an error
// if (Impedance < 100){
impcount = impcount+1;
totimp = totimp+Impedance;
// }
// phase = atan(double(img)/double(re));
// phase = (180.0/3.1415926)*phase; //convert phase angle to degrees
// Phase Calibration
// sys_phase = 118;
// phase = phase - sys_phase;
// gain = (1.0/197760)/9786.98;
// impedance = 1/(gain*mag);
Serial.print("Frequency: ");
Serial.print(freq/1000);
Serial.print(",kHz;");
Serial.print(" Magnitude: ");
Serial.print(mag);
Serial.print(",Ohm;");
Serial.print(" Impedance: ");
Serial.print(Impedance,8);
Serial.println(",");
Serial.print(" Reactance: ");
Serial.print(img);
Serial.println(",");
// break; //TODO: for single run, remove after debugging
//Increment frequency
if((readData(STATUS_REG) & 0x07) < 4 ){
writeData(CTRL_REG,(readData(CTRL_REG) & 0x07) | 0x30);
i++;
}
avgmag = tot/magcount;
avgimp = totimp/impcount;
Wt = 78.6; //wight in Kg
Ht = 169; //hight in cm
Sex =0; //0 for male and 1 for female
age=26;
FFM= 17.7868 + 0.000985* (Ht*Ht) + 0.3736 *(Wt)- 0.0238 *(avgimp) - 4.2921* (Sex) - 0.1531 *(age) ; // the hight is 1.68 m
// FFM= 12.6 + 0.22*(Wt) + 0.46*((pow(Ht,2))/avgimp)-(5.7*(Sex)); //calculating the fat free mass
BF = ((Wt-FFM)/Wt)*100;
// TBW = (Wt-(0.73*FFM))/Wt; //calculating the total body water
//if (Sex==0)
//{ TBW = 1.2 + (0.45*(pow(Ht,2))/avgimp) + 0.18*Wt;
//}
//else
//{ TBW = 3.75 + 0.45*(pow(Ht,2))/avgimp + 0.11*Wt; }
TBW = 6.53+0.36740 *(Ht*Ht/avgimp)+0.17531*Wt-0.11*age-2.83*(Sex-1); #Deurenberg, P.; Van der Kooy, K.; Leenen
ECF = 1.03 + 0.189 *(Ht*Ht/avgimp) + 0.052* (Wt) - 0.0002* (Ht*Ht/img); #Lukaski et al. (1988)
}
}
Serial.print(" Avg Impedance: ");
Serial.print(avgimp);
Serial.print(",");
Serial.print(" % Body Fat: ");
Serial.print(BF);
Serial.print(",");
Serial.print(" % Body water: ");
Serial.print(TBW);
Serial.print(",");
Serial.print(" % external Body water: ");
Serial.print(ECF);
Serial.print(",");
//Power down
// writeData(CTRL_REG,0xA0);
writeData(CTRL_REG,(readData(CTRL_REG) & 0x07) | 0xA0);
}
void writeData(int addr, int data) {
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(addr);
Wire.write(data);
Wire.endTransmission();
delay(1);
}
int readData(int addr){
int data;
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(ADDR_PTR);
Wire.write(addr);
Wire.endTransmission();
delay(1);
Wire.requestFrom(SLAVE_ADDR,1);
if (Wire.available() >= 1){
data = Wire.read();
}
else {
data = -1;
}
delay(1);
return data;
}
boolean measureTemperature() {
// Measure temperature '10010000'
writeData(CTRL_REG, 0x90);
//TODO: necessary to write to second control register?
delay(10); // wait for 10 ms
//Check status reg for temp measurement available
int flag = readData(STATUS_REG)& 1;
if (flag == 1) {
// Temperature is available
int temperatureData = readData(TEMP_R1) << 8;
temperatureData |= readData(TEMP_R2);
temperatureData &= 0x3FFF; // remove first two bits
if (temperatureData & 0x2000 == 1) { // negative temperature
temperatureData -= 0x4000;
}
double val = double(temperatureData) / 32;
temperatureData /= 32;
Serial.print("Temperature: ");
Serial.print(val);
//Serial.write(176); //degree sign
Serial.println("C.");
// Power Down '10100000'
writeData(CTRL_REG,0xA0);
return true;
} else {
return false;
}
}
byte getFrequency(float freq, int n){
long val = long((freq/(MCLK/4)) * pow(2,27));
byte code;
switch (n) {
case 1:
code = (val & 0xFF0000) >> 0x10;
break;
case 2:
code = (val & 0x00FF00) >> 0x08;
break;
case 3:
code = (val & 0x0000FF);
break;
default:
code = 0;
}
return code;
}