v1

Dependencies:   LSM9DS1_Library_cal mbed

Fork of LSM9DS1_Demo_wCal by jim hamblen

Revision:
1:60606d35ed62
Parent:
0:e693d5bf0a25
--- a/main.cpp	Wed Feb 03 18:47:07 2016 +0000
+++ b/main.cpp	Tue Mar 07 22:07:02 2017 +0000
@@ -1,79 +1,125 @@
 #include "mbed.h"
 #include "LSM9DS1.h"
-#define PI 3.14159
-// Earth's magnetic field varies by location. Add or subtract
-// a declination to get a more accurate heading. Calculate
-// your's here:
-// http://www.ngdc.noaa.gov/geomag-web/#declination
-#define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
+#include <string>
+#include <sstream>
+
+Serial  pc(USBTX, USBRX);
+Serial  dev(p9,p10);
+Timer t1,t2;
 
-DigitalOut myled(LED1);
-Serial pc(USBTX, USBRX);
-// Calculate pitch, roll, and heading.
-// Pitch/roll calculations taken from this app note:
-// http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
-// Heading calculations taken from this app note:
-// http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
-void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
-{
-    float roll = atan2(ay, az);
-    float pitch = atan2(-ax, sqrt(ay * ay + az * az));
-// touchy trig stuff to use arctan to get compass heading (scale is 0..360)
-    mx = -mx;
-    float heading;
-    if (my == 0.0)
-        heading = (mx < 0.0) ? 180.0 : 0.0;
-    else
-        heading = atan2(mx, my)*360.0/(2.0*PI);
-    //pc.printf("heading atan=%f \n\r",heading);
-    heading -= DECLINATION; //correct for geo location
-    if(heading>180.0) heading = heading - 360.0;
-    else if(heading<-180.0) heading = 360.0 + heading;
-    else if(heading<0.0) heading = 360.0  + heading;
+using namespace std;
 
-
-    // Convert everything from radians to degrees:
-    //heading *= 180.0 / PI;
-    pitch *= 180.0 / PI;
-    roll  *= 180.0 / PI;
-
-    pc.printf("Pitch: %f,    Roll: %f degress\n\r",pitch,roll);
-    pc.printf("Magnetic Heading: %f degress\n\r",heading);
-}
-
-
-
+char data;
+char goal_val;
+char h[10],g[10],w[10];
+int total_walktime=0;
+float height,weight,goal,goal_f,stride_len,distance_covered,distance_left,calories;
 
 int main()
 {
-    //LSM9DS1 lol(p9, p10, 0x6B, 0x1E);
+    
     LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
-    IMU.begin();
+    
+    float curr_value=0.0,last_value=0.0;
+    int step=0;
+    char data;
+    char goal_val;
+    char h[10],g[10],w[10];
+    int total_walktime=0;
+    float height,weight,goal,goal_f,stride_len,distance_covered,distance_left,calories;
+    bool flag=0,flag2=0; 
+    
+    //Initialize and Calibrate IMU
+    IMU.begin();  
     if (!IMU.begin()) {
         pc.printf("Failed to communicate with LSM9DS1.\n");
     }
     IMU.calibrate(1);
-    IMU.calibrateMag(0);
-    while(1) {
-        while(!IMU.tempAvailable());
-        IMU.readTemp();
-        while(!IMU.magAvailable(X_AXIS));
-        IMU.readMag();
+    
+    //Set baud rate for serial communication to 9600 
+    pc.baud(9600);
+    dev.baud(9600);
+    
+    //Get user input data over bluetooth
+    dev.printf("Enter weight in pounds: ");   
+    dev.gets(w,5);         
+    stringstream str(w);
+    str>>weight;
+    pc.printf("\nWeight = %.3f lbs",weight);   
+    
+    dev.printf("Enter Height in Inches: ");   
+    dev.gets(h,5);         
+    stringstream str1(h);
+    str1>>height;
+    pc.printf("\nHeight = %.3f in",height);   
+    stride_len=height*0.413; 
+    pc.printf("\nStride_length = %f in", stride_len);
+    
+    dev.printf("\nEnter Daily Goal in Miles: ");
+    dev.gets(g,5);         
+    stringstream str2(g);
+    str2>>goal;
+    pc.printf("\nGoal = %f mi",goal);   
+       
+    while(1) 
+    {   
+        //Read accelerometer values from IMU
         while(!IMU.accelAvailable());
         IMU.readAccel();
-        while(!IMU.gyroAvailable());
-        IMU.readGyro();
-        pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
-        pc.printf("        X axis    Y axis    Z axis\n\r");
-        pc.printf("gyro:  %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
-        pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
-        pc.printf("mag:   %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
-        printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
-                      IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
-        myled = 1;
-        wait(0.5);
-        myled = 0;
-        wait(0.5);
+        wait(.25);
+    
+        curr_value=IMU.calcAccel(IMU.ax);
+       
+       //Look for a sharp change in accelerometer output to detect a step  
+       if(abs(curr_value-last_value)>0.1)
+       {
+            step++;
+            //Timer t1 keeps track of the total time of movement
+            //Timer t2 keeps track of the duration for which the user is stationary
+            if(flag==0)
+            {
+                t1.start();
+                flag=1;
+                if(flag2==1)
+                {
+                    flag2=0;
+                    t2.stop();
+                    t2.reset();
+                }
+            }  
+            total_walktime = t1.read();
+        } 
+        else
+        {
+             if(flag==1)
+             {
+                      if(flag2==0)
+                      {
+                          t2.start();
+                          flag2=1;
+                      }
+                      if(t2.read()>=3)
+                      {
+                          flag=0;
+                          flag2=0;
+                          t2.stop();
+                          t2.reset();
+                          t1.stop();
+                      }
+             }  
+        }         
+       
+        last_value = curr_value;
+        
+        distance_covered = step*stride_len/63360;
+        distance_left = goal-distance_covered;
+        calories = 0.63*distance_covered*weight;
+        
+        pc.printf("\nSteps = %d",step);
+        pc.printf("\nDistance Covered = %.3f miles",distance_covered);
+        pc.printf("\nDistance left to reach your goal = %.3f miles", distance_left);
+        pc.printf("\nCalories burnt = %.3f cal",calories);
+        pc.printf("\nTotal Walk Time = %d s\n",total_walktime);
     }
 }