Code for mbed running GPS and IMU to calculate speed and notify if too high or acceleration too high. By Ibrahim Khan and Saad Khan

Dependencies:   4DGL-uLCD-SE GPS LSM9DS1_Library_cal mbed wave_player SDFileSystem

By Ibrahim Khan and Saad Khan. Part of ECE4180 final project.

Files at this revision

API Documentation at this revision

Comitter:
gotmilk
Date:
Sun Dec 11 03:50:17 2016 +0000
Parent:
1:40becbadfee3
Commit message:
one mbed - working version alhamdulillah

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 40becbadfee3 -r 95d21fb43f6f SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Sun Dec 11 03:50:17 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#8db0d3b02cec
diff -r 40becbadfee3 -r 95d21fb43f6f main.cpp
--- a/main.cpp	Fri Dec 09 07:41:08 2016 +0000
+++ b/main.cpp	Sun Dec 11 03:50:17 2016 +0000
@@ -3,16 +3,19 @@
 #include "GPS.h"
 #include "uLCD_4DGL.h"
 #include "LSM9DS1.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
 
 #define M_PI  3.141592653589793238462
 
 Serial pc(USBTX, USBRX);
 GPS gps(p13, p14);
-DigitalOut led2(LED2);
+DigitalOut led2(LED1);
 uLCD_4DGL uLCD(p28,p27,p30);
 Timer t;
-DigitalOut fast(p21);
-DigitalOut turn(p22);
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
 
 
 float oldlat, oldlong, oldtime;
@@ -43,9 +46,9 @@
 
 void checkgps()
 {
-    uLCD.cls();
     uLCD.locate(0,1);
-    if(!gps.sample()) {
+    if(gps.sample()) {
+        time_t seconds = time(NULL);
         float newtime = t.read();
         float newlat = gps.latitude;
         float newlong = gps.longitude;
@@ -53,31 +56,36 @@
         float timediff = newtime - oldtime;
         float speed = dist/timediff; //meters per second
         float speed_mph = (speed * 2.23);
-        led2 = 1;
         uLCD.printf("Longitude: %f degrees\n\rLatitude: %f degrees\n\r", newlong, newlat);
         uLCD.printf("Distance: %f\n\r", dist);
         uLCD.printf("D-time: %f\n\r", timediff);
         uLCD.printf("Speed in M/ph: %f\n\r", speed_mph);
         if (speed_mph > 50 && speed_mph < 100) {
-            fast = 1;
-            wait(0.25);
+            FILE *wave_file;
+            wave_file=fopen("/sd/fast.wav","r");
+            waver.play(wave_file);
+            fclose(wave_file);
+            FILE *fp = fopen("/sd/speedlog.txt", "w");
+            if(fp == NULL) {
+                uLCD.printf("Can't open file");
+            }
+            fprintf(fp, "Driver drove above limit on %s\n", ctime(&seconds));
+            fclose(fp);
         }
-        fast = 0;
         oldlat = newlat;
         oldlong = newlong;
         oldtime = newtime;
     } else {
         uLCD.printf("Oh Dear! No lock :(\n\r");
-        led2 = 0;
     }
 }
 
 int main()
 {
+    time_t seconds = time(NULL);
+    uLCD.cls();
     uLCD.printf("start main\r\n");
     LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
-    fast = 0;
-    turn = 0;
     IMU.begin();
     if (!IMU.begin()) {
         uLCD.printf("Failed to communicate with LSM9DS1.\n");
@@ -90,7 +98,7 @@
     oldtime = t.read();
     oldlat = gps.latitude;
     oldlong = gps.longitude;
-    checkgps();
+    //checkgps();
     while(1) {
         newtime = t.read();
         if ((newtime-oldtime)>300) {
@@ -102,21 +110,27 @@
             IMU.readAccel();
             display++;
             if (display%100==0) {
-                uLCD.locate(0,9);
+                uLCD.locate(0,10);
                 accel = IMU.calcAccel(IMU.ay);
                 uLCD.printf("Y-acceleration in g's: %2f\n", accel);
                 if (abs(accel) > .6 && abs(accel) < 1) {
                     counter++;
+                } else {
+                    counter = 0;
                 }
-                else {
-                    counter = 0;
-                    }
                 if (counter >= 2) {
-                    turn = 1;
+                    FILE *wave_file;
+                    wave_file=fopen("/sd/turn.wav","r");
+                    waver.play(wave_file);
+                    fclose(wave_file);
+                    FILE *fp = fopen("/sd/speedlog.txt", "w");
+                    if(fp == NULL) {
+                        uLCD.printf("Can't open file");
+                    }
+                    fprintf(fp, "Driver turned above g-limit on on %s\n", ctime(&seconds));
+                    fclose(fp);
                     counter = 0;
-                    wait(0.25);
                 }
-                turn = 0;
             }
         }
     }
diff -r 40becbadfee3 -r 95d21fb43f6f wave_player.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Sun Dec 11 03:50:17 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad