Frederick Lemuel Ravibabu / Mbed 2 deprecated FitnessTracker

Dependencies:   LSM9DS1_Library_cal mbed

Fork of LSM9DS1_Demo_wCal by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LSM9DS1.h"
00003 #include <string>
00004 #include <sstream>
00005 
00006 Serial  pc(USBTX, USBRX);
00007 Serial  dev(p9,p10);
00008 Timer t1,t2;
00009 
00010 using namespace std;
00011 
00012 char data;
00013 char goal_val;
00014 char h[10],g[10],w[10];
00015 int total_walktime=0;
00016 float height,weight,goal,goal_f,stride_len,distance_covered,distance_left,calories;
00017 
00018 int main()
00019 {
00020     
00021     LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
00022     
00023     float curr_value=0.0,last_value=0.0;
00024     int step=0;
00025     char data;
00026     char goal_val;
00027     char h[10],g[10],w[10];
00028     int total_walktime=0;
00029     float height,weight,goal,goal_f,stride_len,distance_covered,distance_left,calories;
00030     bool flag=0,flag2=0; 
00031     
00032     //Initialize and Calibrate IMU
00033     IMU.begin();  
00034     if (!IMU.begin()) {
00035         pc.printf("Failed to communicate with LSM9DS1.\n");
00036     }
00037     IMU.calibrate(1);
00038     
00039     //Set baud rate for serial communication to 9600 
00040     pc.baud(9600);
00041     dev.baud(9600);
00042     
00043     //Get user input data over bluetooth
00044     dev.printf("Enter weight in pounds: ");   
00045     dev.gets(w,5);         
00046     stringstream str(w);
00047     str>>weight;
00048     pc.printf("\nWeight = %.3f lbs",weight);   
00049     
00050     dev.printf("Enter Height in Inches: ");   
00051     dev.gets(h,5);         
00052     stringstream str1(h);
00053     str1>>height;
00054     pc.printf("\nHeight = %.3f in",height);   
00055     stride_len=height*0.413; 
00056     pc.printf("\nStride_length = %f in", stride_len);
00057     
00058     dev.printf("\nEnter Daily Goal in Miles: ");
00059     dev.gets(g,5);         
00060     stringstream str2(g);
00061     str2>>goal;
00062     pc.printf("\nGoal = %f mi",goal);   
00063        
00064     while(1) 
00065     {   
00066         //Read accelerometer values from IMU
00067         while(!IMU.accelAvailable());
00068         IMU.readAccel();
00069         wait(.25);
00070     
00071         curr_value=IMU.calcAccel(IMU.ax);
00072        
00073        //Look for a sharp change in accelerometer output to detect a step  
00074        if(abs(curr_value-last_value)>0.1)
00075        {
00076             step++;
00077             //Timer t1 keeps track of the total time of movement
00078             //Timer t2 keeps track of the duration for which the user is stationary
00079             if(flag==0)
00080             {
00081                 t1.start();
00082                 flag=1;
00083                 if(flag2==1)
00084                 {
00085                     flag2=0;
00086                     t2.stop();
00087                     t2.reset();
00088                 }
00089             }  
00090             total_walktime = t1.read();
00091         } 
00092         else
00093         {
00094              if(flag==1)
00095              {
00096                       if(flag2==0)
00097                       {
00098                           t2.start();
00099                           flag2=1;
00100                       }
00101                       if(t2.read()>=3)
00102                       {
00103                           flag=0;
00104                           flag2=0;
00105                           t2.stop();
00106                           t2.reset();
00107                           t1.stop();
00108                       }
00109              }  
00110         }         
00111        
00112         last_value = curr_value;
00113         
00114         distance_covered = step*stride_len/63360;
00115         distance_left = goal-distance_covered;
00116         calories = 0.63*distance_covered*weight;
00117         
00118         pc.printf("\nSteps = %d",step);
00119         pc.printf("\nDistance Covered = %.3f miles",distance_covered);
00120         pc.printf("\nDistance left to reach your goal = %.3f miles", distance_left);
00121         pc.printf("\nCalories burnt = %.3f cal",calories);
00122         pc.printf("\nTotal Walk Time = %d s\n",total_walktime);
00123     }
00124 }
00125