A system to help you ride your bike better than you do right now.

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed LSM9DS1_Library_cal

Revision:
7:0d5396762696
Parent:
6:45a5043acc7e
Child:
8:8c073046821d
--- a/main.cpp	Mon Dec 05 01:13:48 2016 +0000
+++ b/main.cpp	Mon Dec 05 01:59:24 2016 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include "uLCD_4DGL.h"
+#include "LSM9DS1.h"
 #include <fstream>
 #include <iomanip>
 using namespace std;
@@ -14,10 +15,12 @@
 
 int lightState;
 
-DigitalOut leftBlinker(LED1);
-DigitalOut rightBlinker(LED1);
-DigitalOut brakeLight(LED1);
+DigitalOut leftBlinker(p22);
+DigitalOut rightBlinker(p23);
+DigitalOut brakeLight(p21);
 Timer blinkT;
+AnalogIn flex(p15);
+LSM9DS1 imu(p9, p10, 0xD6, 0x3C);
 
 SDFileSystem sd(p11, p12, p13, p14, "sd");  //mosi -> DI, miso <- DO, slck -> sclck, CS -> CS
 uLCD_4DGL lcd(p28, p27, p29);
@@ -43,7 +46,13 @@
     recall_trips();
     wait(10);
     lcd.cls();
-    
+    /*
+    imu.begin();
+    if (!imu.begin()) {
+        lcd.printf("Failed to communicate with LSM9DS1.\n");
+    }
+    imu.calibrate();
+    */
     // normal operation loop here
     int going = 1; // cyclist is moving
     set_time(START_S);  // Set RTC time
@@ -58,7 +67,7 @@
         seconds = time(NULL) - START_S; // return the seconds passed since start
         
         if (hallT.read() > 6.0 && !stopped) {
-            speed = 0.0;
+            speed = 0;
             stopped = 1;
         }
 
@@ -66,12 +75,15 @@
         maxSpeed = (speed > maxSpeed) ? speed : maxSpeed;
         
         lcd.locate(0, 1);
-        lcd.printf("Distance : %3.1f\n\n", miles);
-        lcd.printf("Top speed : %2.1f\n\n", speed);
-        lcd.printf("Time : %1.1f\n\n", (float)seconds / 3600);
+        lcd.color(GREEN);
+        lcd.printf("Distance : %3.1f mi\n\n", miles);
+        lcd.color(BLUE);
+        lcd.printf("Speed : %2.1f mph\n\n", speed);
+        lcd.color(LGREY);
+        lcd.printf("Time : %3.1f min\n\n", (float)seconds / 60);
         
         // light states code
-        lightState = get_state();
+        // lightState = get_state();
         
         if (lightState == STOP) {
             brakeLight = 1;
@@ -109,7 +121,7 @@
     
     float f_miles;
     float f_maxSpeed;
-    float f_hours;
+    float f_minutes;
     ifstream file;
     
     // read out the most recent trip
@@ -119,12 +131,12 @@
         lcd.locate(0, 1);
         lcd.printf("Could not open file\n");
     } else {
-        file >> f_miles >> f_maxSpeed >> f_hours;
+        file >> f_miles >> f_maxSpeed >> f_minutes;
         lcd.locate(0, 1);
         lcd.printf("Your last trip\n\n");
-        lcd.printf("Distance : %3.1f\n\n", f_miles);
-        lcd.printf("Top speed : %2.1f\n\n", f_maxSpeed);
-        lcd.printf("Time : %1.1f\n\n", f_hours);
+        lcd.printf("Distance : %3.1f mi\n\n", f_miles);
+        lcd.printf("Top speed : %2.1f mph\n\n", f_maxSpeed);
+        lcd.printf("Time : %3.1f min\n\n", f_minutes);
     }
     
     file.close();
@@ -134,15 +146,16 @@
     
     file.open("/sd/records/best-of.txt");
 
+    lcd.color(WHITE);
     if(!file.is_open()) {
         lcd.printf("Could not open file\n");
     } else {
         // show the best trip
-        file >> f_miles >> f_maxSpeed >> f_hours;
+        file >> f_miles >> f_maxSpeed >> f_minutes;
         lcd.printf("Your best trip\n\n");
-        lcd.printf("Distance : %3.1f\n\n", f_miles);
-        lcd.printf("Top speed : %2.1f\n\n", f_maxSpeed);
-        lcd.printf("Time : %1.1f\n\n", f_hours);
+        lcd.printf("Distance : %3.1f mi\n\n", f_miles);
+        lcd.printf("Top speed : %2.1f mph\n\n", f_maxSpeed);
+        lcd.printf("Time : %3.1f min\n\n", f_minutes);
     }
 
     file.close();
@@ -153,23 +166,24 @@
     // store the most recent trip completed
     // determine whether this trip was a record, and indicate if so
     
-    float hours;
+    float minutes;
     fstream file;
     
     file.open("/sd/records/recent.txt");
     
+    lcd.color(WHITE);
     if (!file.is_open()) {
         lcd.locate(0, 1);
         lcd.printf("Could not open file\n");
     } else {
-        hours = (float)seconds / 3600;
+        minutes = (float)seconds / 60;
         lcd.locate(0, 1);
         lcd.printf("This trip\n\n");
-        lcd.printf("Distance : %3.1f\n\n", miles);
-        lcd.printf("Top speed : %2.1f\n\n", maxSpeed);
-        lcd.printf("Time : %1.1f\n\n", hours);
+        lcd.printf("Distance : %3.1f mi\n\n", miles);
+        lcd.printf("Top speed : %2.1f mph\n\n", maxSpeed);
+        lcd.printf("Time : %3.1f min\n\n", minutes);
         // overwrite most recent
-        file << fixed << setprecision(1) << miles << " " << maxSpeed << " " << hours << endl;
+        file << fixed << setprecision(1) << miles << " " << maxSpeed << " " << minutes << endl;
     }
     
     file.close();
@@ -186,10 +200,20 @@
     }
     
     file.close();
+    wait(0.5);
 }
 
 int get_state(void) {
-    //
+    imu.readAccel();
+    float f = flex.read();
+    int x = imu.ax, y = imu.ay, z = imu.az;
+    if (z > 0 && z > abs(y) && z > abs(x) && y < 0 && f >= 0.92)
+        return STOP;
+    if (x < 0 && abs(x) > abs(z) && abs(x) > abs(y) && f <= 0.87)
+        return LEFT;
+    if (y > 0 && y > abs(z) && y > abs(x) && f >= 0.87)
+        return RIGHT;
+    return GO;
 }
 
 void pass(void) {