An mbed health monitor
Dependencies: 4DGL-uLCD-SE GP-20U7 PinDetect RPCInterface mbed
Diff: main.cpp
- Revision:
- 0:0de3867984e5
- Child:
- 1:ad5378dcef79
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Dec 11 22:58:51 2016 +0000 @@ -0,0 +1,94 @@ +#include "mbed.h" +#include "LSM9DS1.h" +#include "GPS.h" +#include "GPSstuff.h" +#include <time.h> +#include <stdio.h> + +Serial pc(USBTX, USBRX); +GPS gps(p13, p14); +GPSstuff object; +LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); + +float a, ax, ay, az; //acceleration +int steps=0, i=0, step_num; +bool flag=0; + +double buffer[4], speed, dist_travel=0, distance, totalDistance=0, threshold=1.1, time_elapsed=0; + +int getsteps(){ + while(!IMU.accelAvailable()); + IMU.readAccel(); + ax=IMU.calcAccel(IMU.ax); + ay=IMU.calcAccel(IMU.ay); + az=IMU.calcAccel(IMU.az); + a=sqrt(pow(ax, 2) + pow(ay, 2) + pow(az, 2)); + pc.printf("a: %d", a); + if (a>=threshold && flag==0){ // if it crosses threshold increment step by one and raise the flag + steps++; flag=1; + pc.printf("steps: %i", steps); + } + + else if (a < threshold && flag==1){ // if flag is raised and threshold is not crossed , put that flag down. + flag=0; + } + return steps; +} + +double getdistance(){ + if (i==0){ + //initial reading made by the GPS + gps.sample(); + buffer[i]= gps.longitude; + buffer[i+1] = gps.latitude; + i=i+2; +// pc.printf("First reading taken \n"); + if (buffer[2]==0 && buffer[3]==0){ + //second reading made by GPS + gps.sample(); + buffer[2]= gps.longitude; + buffer[3] = gps.latitude; + i=2; + } + } + else if (i==2){ + float lat1d = buffer[0]; + float lon1d = buffer[1]; + float lat2d = buffer[2]; + float lon2d = buffer[3]; + distance = object.distanceEarth(lat1d, lon1d, lat2d, lon2d); + +// pc.putc(totalDistance); +// pc.putc(time_passed); + //pc.printf("Distance travelled: %f km", totalDistance); + //pc.printf("\n Distance travelled: %f miles", totalDistance * 0.621371); + + lat1d = buffer[2]; + lon1d = buffer[3]; + i=0; + } + return distance; +} + + +int main() +{ + IMU.begin(); + if (!IMU.begin()) { + pc.printf("Failed to communicate with LSM9DS1.\n"); + } + IMU.calibrate(1); + clock_t start = clock(); + dist_travel=getdistance(); + totalDistance+=dist_travel; + + while(1) { + step_num=getsteps(); + dist_travel=getdistance(); + clock_t end = clock(); + time_elapsed=end-start; + totalDistance+=dist_travel; + pc.printf("Distance travelled: %f km\n\rTime elapsed: %f s", totalDistance, time_elapsed/1000); + clock_t start = clock(); + } +} \ No newline at end of file