v1

Dependencies:   LSM9DS1_Library_cal mbed

Fork of LSM9DS1_Demo_wCal by jim hamblen

Committer:
FrederickLemuel
Date:
Tue Mar 07 22:07:02 2017 +0000
Revision:
1:60606d35ed62
Parent:
0:e693d5bf0a25
Project_v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:e693d5bf0a25 1 #include "mbed.h"
4180_1 0:e693d5bf0a25 2 #include "LSM9DS1.h"
FrederickLemuel 1:60606d35ed62 3 #include <string>
FrederickLemuel 1:60606d35ed62 4 #include <sstream>
FrederickLemuel 1:60606d35ed62 5
FrederickLemuel 1:60606d35ed62 6 Serial pc(USBTX, USBRX);
FrederickLemuel 1:60606d35ed62 7 Serial dev(p9,p10);
FrederickLemuel 1:60606d35ed62 8 Timer t1,t2;
4180_1 0:e693d5bf0a25 9
FrederickLemuel 1:60606d35ed62 10 using namespace std;
4180_1 0:e693d5bf0a25 11
FrederickLemuel 1:60606d35ed62 12 char data;
FrederickLemuel 1:60606d35ed62 13 char goal_val;
FrederickLemuel 1:60606d35ed62 14 char h[10],g[10],w[10];
FrederickLemuel 1:60606d35ed62 15 int total_walktime=0;
FrederickLemuel 1:60606d35ed62 16 float height,weight,goal,goal_f,stride_len,distance_covered,distance_left,calories;
4180_1 0:e693d5bf0a25 17
4180_1 0:e693d5bf0a25 18 int main()
4180_1 0:e693d5bf0a25 19 {
FrederickLemuel 1:60606d35ed62 20
4180_1 0:e693d5bf0a25 21 LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
FrederickLemuel 1:60606d35ed62 22
FrederickLemuel 1:60606d35ed62 23 float curr_value=0.0,last_value=0.0;
FrederickLemuel 1:60606d35ed62 24 int step=0;
FrederickLemuel 1:60606d35ed62 25 char data;
FrederickLemuel 1:60606d35ed62 26 char goal_val;
FrederickLemuel 1:60606d35ed62 27 char h[10],g[10],w[10];
FrederickLemuel 1:60606d35ed62 28 int total_walktime=0;
FrederickLemuel 1:60606d35ed62 29 float height,weight,goal,goal_f,stride_len,distance_covered,distance_left,calories;
FrederickLemuel 1:60606d35ed62 30 bool flag=0,flag2=0;
FrederickLemuel 1:60606d35ed62 31
FrederickLemuel 1:60606d35ed62 32 //Initialize and Calibrate IMU
FrederickLemuel 1:60606d35ed62 33 IMU.begin();
4180_1 0:e693d5bf0a25 34 if (!IMU.begin()) {
4180_1 0:e693d5bf0a25 35 pc.printf("Failed to communicate with LSM9DS1.\n");
4180_1 0:e693d5bf0a25 36 }
4180_1 0:e693d5bf0a25 37 IMU.calibrate(1);
FrederickLemuel 1:60606d35ed62 38
FrederickLemuel 1:60606d35ed62 39 //Set baud rate for serial communication to 9600
FrederickLemuel 1:60606d35ed62 40 pc.baud(9600);
FrederickLemuel 1:60606d35ed62 41 dev.baud(9600);
FrederickLemuel 1:60606d35ed62 42
FrederickLemuel 1:60606d35ed62 43 //Get user input data over bluetooth
FrederickLemuel 1:60606d35ed62 44 dev.printf("Enter weight in pounds: ");
FrederickLemuel 1:60606d35ed62 45 dev.gets(w,5);
FrederickLemuel 1:60606d35ed62 46 stringstream str(w);
FrederickLemuel 1:60606d35ed62 47 str>>weight;
FrederickLemuel 1:60606d35ed62 48 pc.printf("\nWeight = %.3f lbs",weight);
FrederickLemuel 1:60606d35ed62 49
FrederickLemuel 1:60606d35ed62 50 dev.printf("Enter Height in Inches: ");
FrederickLemuel 1:60606d35ed62 51 dev.gets(h,5);
FrederickLemuel 1:60606d35ed62 52 stringstream str1(h);
FrederickLemuel 1:60606d35ed62 53 str1>>height;
FrederickLemuel 1:60606d35ed62 54 pc.printf("\nHeight = %.3f in",height);
FrederickLemuel 1:60606d35ed62 55 stride_len=height*0.413;
FrederickLemuel 1:60606d35ed62 56 pc.printf("\nStride_length = %f in", stride_len);
FrederickLemuel 1:60606d35ed62 57
FrederickLemuel 1:60606d35ed62 58 dev.printf("\nEnter Daily Goal in Miles: ");
FrederickLemuel 1:60606d35ed62 59 dev.gets(g,5);
FrederickLemuel 1:60606d35ed62 60 stringstream str2(g);
FrederickLemuel 1:60606d35ed62 61 str2>>goal;
FrederickLemuel 1:60606d35ed62 62 pc.printf("\nGoal = %f mi",goal);
FrederickLemuel 1:60606d35ed62 63
FrederickLemuel 1:60606d35ed62 64 while(1)
FrederickLemuel 1:60606d35ed62 65 {
FrederickLemuel 1:60606d35ed62 66 //Read accelerometer values from IMU
4180_1 0:e693d5bf0a25 67 while(!IMU.accelAvailable());
4180_1 0:e693d5bf0a25 68 IMU.readAccel();
FrederickLemuel 1:60606d35ed62 69 wait(.25);
FrederickLemuel 1:60606d35ed62 70
FrederickLemuel 1:60606d35ed62 71 curr_value=IMU.calcAccel(IMU.ax);
FrederickLemuel 1:60606d35ed62 72
FrederickLemuel 1:60606d35ed62 73 //Look for a sharp change in accelerometer output to detect a step
FrederickLemuel 1:60606d35ed62 74 if(abs(curr_value-last_value)>0.1)
FrederickLemuel 1:60606d35ed62 75 {
FrederickLemuel 1:60606d35ed62 76 step++;
FrederickLemuel 1:60606d35ed62 77 //Timer t1 keeps track of the total time of movement
FrederickLemuel 1:60606d35ed62 78 //Timer t2 keeps track of the duration for which the user is stationary
FrederickLemuel 1:60606d35ed62 79 if(flag==0)
FrederickLemuel 1:60606d35ed62 80 {
FrederickLemuel 1:60606d35ed62 81 t1.start();
FrederickLemuel 1:60606d35ed62 82 flag=1;
FrederickLemuel 1:60606d35ed62 83 if(flag2==1)
FrederickLemuel 1:60606d35ed62 84 {
FrederickLemuel 1:60606d35ed62 85 flag2=0;
FrederickLemuel 1:60606d35ed62 86 t2.stop();
FrederickLemuel 1:60606d35ed62 87 t2.reset();
FrederickLemuel 1:60606d35ed62 88 }
FrederickLemuel 1:60606d35ed62 89 }
FrederickLemuel 1:60606d35ed62 90 total_walktime = t1.read();
FrederickLemuel 1:60606d35ed62 91 }
FrederickLemuel 1:60606d35ed62 92 else
FrederickLemuel 1:60606d35ed62 93 {
FrederickLemuel 1:60606d35ed62 94 if(flag==1)
FrederickLemuel 1:60606d35ed62 95 {
FrederickLemuel 1:60606d35ed62 96 if(flag2==0)
FrederickLemuel 1:60606d35ed62 97 {
FrederickLemuel 1:60606d35ed62 98 t2.start();
FrederickLemuel 1:60606d35ed62 99 flag2=1;
FrederickLemuel 1:60606d35ed62 100 }
FrederickLemuel 1:60606d35ed62 101 if(t2.read()>=3)
FrederickLemuel 1:60606d35ed62 102 {
FrederickLemuel 1:60606d35ed62 103 flag=0;
FrederickLemuel 1:60606d35ed62 104 flag2=0;
FrederickLemuel 1:60606d35ed62 105 t2.stop();
FrederickLemuel 1:60606d35ed62 106 t2.reset();
FrederickLemuel 1:60606d35ed62 107 t1.stop();
FrederickLemuel 1:60606d35ed62 108 }
FrederickLemuel 1:60606d35ed62 109 }
FrederickLemuel 1:60606d35ed62 110 }
FrederickLemuel 1:60606d35ed62 111
FrederickLemuel 1:60606d35ed62 112 last_value = curr_value;
FrederickLemuel 1:60606d35ed62 113
FrederickLemuel 1:60606d35ed62 114 distance_covered = step*stride_len/63360;
FrederickLemuel 1:60606d35ed62 115 distance_left = goal-distance_covered;
FrederickLemuel 1:60606d35ed62 116 calories = 0.63*distance_covered*weight;
FrederickLemuel 1:60606d35ed62 117
FrederickLemuel 1:60606d35ed62 118 pc.printf("\nSteps = %d",step);
FrederickLemuel 1:60606d35ed62 119 pc.printf("\nDistance Covered = %.3f miles",distance_covered);
FrederickLemuel 1:60606d35ed62 120 pc.printf("\nDistance left to reach your goal = %.3f miles", distance_left);
FrederickLemuel 1:60606d35ed62 121 pc.printf("\nCalories burnt = %.3f cal",calories);
FrederickLemuel 1:60606d35ed62 122 pc.printf("\nTotal Walk Time = %d s\n",total_walktime);
4180_1 0:e693d5bf0a25 123 }
4180_1 0:e693d5bf0a25 124 }
4180_1 0:e693d5bf0a25 125