-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest.ino
363 lines (276 loc) · 10.1 KB
/
latest.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
363
#include "I2Cdev.h"
#include "MPU6050.h"
#include "HX711.h"
#include <EEPROM.h>
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro;
HX711 scale(A1, A0); // parameter "gain" is ommited; the default value 128 is used by the library
int16_t ax, ay, az;
int16_t gx, gy, gz;
// accelerometer values
int t=20,s=0,v=0;
int rest = 0;
int accel_reading;
int accel_corrected;
int accel_offset = 200;
float accel_angle;
float accel_scale = 1; // set to 0.01
// gyro values
int gyro_offset = 151; // 151
int gyro_corrected;
int gyro_reading;
float gyro_rate;
float gyro_scale = 0.02; // 0.02 by default - tweak as required
float gyro_angle;
float loop_time = 0.05; // 50ms loop
float angle = 0.00; // value to hold final calculated gyro angle
// time stamp variables
int last_update;
int cycle_time;
long last_cycle = 0;
#define OUTPUT_READABLE_ACCELGYRO
#define LED_PIN 13
bool blinkState = false;
int i=0,low_counter=0,peak_counter=0,steps=0;
void ledSteps(){
pinMode(8,OUTPUT); //The following Pins are for LED outputs of the SHOE.
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(5,INPUT);
if(steps>5)digitalWrite(8,HIGH); //For Demonstration purposes we light up each light after 5 steps.
if(steps>10)digitalWrite(9,HIGH);
if(steps>15)digitalWrite(10,HIGH);
if(steps>20)digitalWrite(11,HIGH);
if(steps>25)digitalWrite(12,HIGH);
}
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(38400);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
float aax= (((ax +1300)/16000.0)*9.81 -2.71); // Scaling X axis acceleration for use in our Application
#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
// Serial.print("a/g:\t");
// Serial.print(aax); Serial.println("");
// Serial.print(ax); Serial.print("\t");
// Serial.print(ay); Serial.print("\t");
// Serial.print(az); Serial.print("\t");
// Serial.print(gx); Serial.print("\t");
// Serial.print(gy); Serial.print("\t");
// Serial.println(gz);
#endif
#ifdef OUTPUT_BINARY_ACCELGYRO
Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
#endif
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
//delay(1000);
/*Here is the foot-step detecting algorithm
* Analyzed the variation between Foot step and time while walking
* Lower counter is a counter where it stores how many lower accelerations occur
* Peak counter stores peak accelerations only if there are more than 10 lower counters
* if program gets 10 lower accelerations and more than 7 peak acceleration, it shows that step is taken,according to our analyzation
*/
if(aax<-1)low_counter++;
if(low_counter>10){
if(aax>1)peak_counter++;
if(peak_counter>7){
steps++;
Serial.print("Current Steps : \t");
Serial.println(steps);
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
delay(50);
digitalWrite(5,LOW);
peak_counter=0;
low_counter=0;
}
}
ledSteps(); //This function which is defined above uses the number of steps variable and Light up LEDs accordingly
//Serial.println(low_counter);
//Serial.println(peak_counter);
//Serial.println("");
//EEPROM.write(i,y);
i++;
if(digitalRead(5)){
Serial.println("Entering Calibrating MODE ");
calibrate();
}
delay(t);//each loop is delayed by 20 ms
}
void get_angle(){
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// accelerometer_X_Axis angle calc
accel_reading = ax;
accel_corrected = accel_reading - accel_offset;
accel_corrected = map(accel_corrected, -16800, 16800, -90, 90);
accel_corrected = constrain(accel_corrected, -90, 90);
accel_angle = (float)(accel_corrected * accel_scale)-10;
// Serial.print(accel_angle);
// Serial.print("\t");
// gyro_Y_Axis angle calc
gyro_reading = gy;
gyro_corrected = (float)((gyro_reading/131) - gyro_offset); // 131 is sensivity of gyro from data sheet
gyro_rate = (gyro_corrected * gyro_scale) * -loop_time; // loop_time = 0.05 ie 50ms
gyro_angle = angle + gyro_rate;
/*
// print values to serial monitor for checking
Serial.print(gyro_reading);
Serial.print("\t");
Serial.print(gyro_corrected);
Serial.print("\t");
Serial.print(gyro_angle);
Serial.print("\t");
Serial.println(" ");
*/
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
//timestamp
time_stamp();
}
void time_stamp(){
while ((millis() - last_cycle) < 50){
delay(1);
}
// once loop cycle reaches 50ms, reset timer value and continue
cycle_time = millis() - last_cycle;
last_cycle = millis();
}
void calibrate(){
int i;
float weight=0,temp_weight=0,foot_len=0;
for(i=0;i<10;i++){
digitalWrite(8,HIGH); //For Demonstration purposes we light up each light after 5 steps.
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(8,LOW); //For Demonstration purposes we light up each light after 5 steps.
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
delay(100);
}
for(i=0;i<3;i++){
scale.power_up();
/*
Serial.print("one reading:\t");
Serial.print("\t| average:\t");
Serial.println(, 1);
*/
//Serial.print(scale.get_units(), 1);
temp_weight = scale.get_units(10);
Serial.print("Weight Samples \t");
Serial.println(temp_weight);
if(temp_weight<0) temp_weight = -1.0*temp_weight;
weight = weight +temp_weight;
scale.power_down();
delay(500);// put the ADC in sleep mode
}
Serial.println("Weight");
Serial.println(weight/3);
int cnt=0;
while(cnt<10){
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
float aax= (((ax +1300)/16000.0)*9.81 -2.71); // Scaling X axis acceleration for use in our Application
get_angle(); //update the angle
v=v+aax*t;
s=v+(0.5)*aax*t*t;
if(aax>-1.5 && aax<1.5){
rest++;
}
//Serial.println(rest);
if(rest>20 && (aax<-1.5 || aax>1.5)){
//Serial.println(s);
if(s<0)s=-1*s;
foot_len = s + foot_len;
v = 0;
s = 0;
rest=0;
cnt++;
if(cnt>0)digitalWrite(8,HIGH);
if(cnt>2)digitalWrite(9,HIGH);
if(cnt>4)digitalWrite(10,HIGH);
if(cnt>6)digitalWrite(11,HIGH);
if(cnt>8)digitalWrite(12,HIGH);
}
}
Serial.print("Foot Length :\t");
Serial.println(foot_len/10);
float final_height,final_weight,BMI;
final_height = ((foot_len/10)/1280)*1.76;
final_weight = (weight/3)/55 *75;
BMI = final_weight/final_height*final_height;
Serial.print("Person's Height :\t");
Serial.println(final_height);
Serial.print("Person's Weight :\t");
Serial.println(final_weight);
Serial.print("Person's BMI :\t");
Serial.println(BMI);
digitalWrite(8,LOW); //For Demonstration purposes we light up each light after 5 steps.
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
EEPROM.write(0, BMI);
EEPROM.write(1, foot_len/100);
}