MotoTrak

Dependencies:   TextLCD mbed

Files at this revision

API Documentation at this revision

Comitter:
justedwa
Date:
Mon Apr 30 18:18:19 2018 +0000
Parent:
0:454412fa8fee
Commit message:
Final

Changed in this revision

DHT11.lib Show annotated file Show diff for this revision Revisions of this file
FXOS8700CQ.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
parse.cpp Show annotated file Show diff for this revision Revisions of this file
parse.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT11.lib	Mon Apr 30 18:18:19 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/fossum_13/code/DHT11/#5da6f6de3e42
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700CQ.lib	Mon Apr 30 18:18:19 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/trm/code/FXOS8700CQ/#e2fe752b881e
--- a/main.cpp	Tue Apr 24 21:17:05 2018 +0000
+++ b/main.cpp	Mon Apr 30 18:18:19 2018 +0000
@@ -3,8 +3,11 @@
 #include "parse.h"
 #include "sense.h"
 #include "TextLCD.h"
-
-TextLCD lcd(A0,A1,A2,A3,A4,A5,TextLCD::LCD16x2);
+#include "FXOS8700CQ.h"
+#include <math.h>
+#include "Dht11.h"
+FXOS8700CQ fxos(PTE25,PTE24,FXOS8700CQ_SLAVE_ADDR1);
+TextLCD lcd(PTC5,PTC7,D13,D12,D11,D10,TextLCD::LCD16x2);
 Serial pc(USBTX, USBRX);
 InterruptIn gearUp(PTA4);
 InterruptIn gearDown(PTC6);
@@ -12,7 +15,11 @@
 Serial blue(PTC15,PTC14);
 Ticker display;
 Ticker Wifi;
-char rx[50];
+Ticker lean;
+Ticker BLE;
+Ticker th;
+Dht11 sense(D3);
+char rx[40];
 Serial esp8266(PTC4,PTC3);
 //DigitalOut myled(PTB22);
 //DigitalOut myled3(PTB21);
@@ -23,33 +30,36 @@
 void gDown();
 void updateDisplay();
 void updateWifi();
-
+void updateangle();
+void onBluetoothReceived();
+void flushBuff();
+void updateTempH();
 
 void onBluetoothReceived(void) {
-    if(y == 1000){
-        y = 0;
-        while(blue.readable()){
-            //myled = !myled;
+        if(blue.readable()){
+            //myled = !myled
             //pc.printf("Interrupt!\r\n");
-            blue.gets(rx,50);
-            //pc.puts(rx);
+            if(blue.readable()) blue.gets(rx,39);
+            flushBuff();
+            //pc.puts("Passed\r\n");
             //pc.puts("\n");
-            if(rx[0] == '[' && strlen(rx) > 24){
+            //pc.puts(rx);
+            //if(rx[0] == '['){
                 //pc.puts(rx);
-            }
-            else
-               rx[0] = '\0';
+            //}
+            //else
+               //rx[0] = '\0';
         }
-    }
-    else
-        y++;
 }
 
 int main() 
 {   
     initializeStruct(currInfo);
-    Wifi.attach(&updateWifi,5);
-    display.attach(&updateDisplay,.5);
+    th.attach(&updateTempH,5);
+    lean.attach(&updateangle,.5);
+    Wifi.attach(&updateWifi,1);
+    BLE.attach(&onBluetoothReceived,.5);
+    display.attach(&updateDisplay,.1);
     SetGear(currInfo,Nswitch);
     gearUp.rise(&gUp);
     gearDown.rise(&gDown);
@@ -71,16 +81,20 @@
 }
 
 void gUp(){
+    currInfo->crash = 1;
+    pc.printf("CRASH %d",currInfo->crash);
     if(currInfo->gear == 6)
         return;
-    if(currInfo->gear == 0){
+    else if(currInfo->gear == 0){
         currInfo->gear = 2;
         return;
     }
-    currInfo->gear++;    
+    else
+        currInfo->gear++;    
 }
 
 void gDown(){
+    currInfo->crash = 0;
     if(currInfo->gear == 0)
         currInfo->gear++;
     else if(currInfo->gear == 1)
@@ -92,11 +106,49 @@
 void updateDisplay(){
     lcd.cls();
     lcd.locate(0,0);
-    lcd.printf("Angle: %.2f",currInfo->x);
+    lcd.printf("Angle: %d",currInfo->lean);
     lcd.locate(0,1);
     lcd.printf("m/s:%.2f G:%d",currInfo->speed,currInfo->gear);
 }
 void updateWifi(){     //[,WIFI,hr,temp,humidity,x,y,speed,lean,accel,gear,]
-       esp8266.printf("[,WIFI,%d,%d,%d,%f,%f,%f,%d,%d,%d,]",currInfo->hr,currInfo->temp,currInfo->humidity,currInfo->x,currInfo->y,currInfo->speed,currInfo->lean,currInfo->accel,currInfo->gear);
-       //pc.printf("[,WIFI,%d,%d,%d,%f,%f,%f,%d,%d,%d,]",currInfo->hr,currInfo->temp,currInfo->humidity,currInfo->x,currInfo->y,currInfo->speed,currInfo->lean,currInfo->accel,currInfo->gear);
+       esp8266.printf("[,WIFI,%d,%d,%d,%f,%f,%f,%d,%d,%d,%d,]",currInfo->hr,currInfo->temp,currInfo->humidity,currInfo->x,currInfo->y,currInfo->speed,currInfo->lean,currInfo->accel,currInfo->gear,currInfo->crash);
+       //pc.printf("[,WIFI,%d,%d,%d,%f,%f,%f,%d,%d,%d,%d,]\r\n",currInfo->hr,currInfo->temp,currInfo->humidity,currInfo->x,currInfo->y,currInfo->speed,currInfo->lean,currInfo->accel,currInfo->gear,currInfo->crash);
 }
+
+float prevx = 0, prevy = 0, prevz = 0;
+void updateangle(){
+    SRAWDATA accdata,magdata;
+    float ax,ay,az,mx,my,mz,axf,ayf,xh,yh;
+    double xangle,yangle,zangle,heading;
+    fxos.enable();
+    fxos.get_data(&accdata,&magdata);
+    ax = accdata.x;
+    ay = accdata.y;
+    az = accdata.z;
+    //pc.printf("%f %f %f\r\n",ax,ay,az);
+    xangle = atan2(ax, (sqrt(ay*ay + az*az)));
+    xangle *= 180; xangle /= 3.141592;
+    if(prevx - ax > 6144){
+        currInfo->crash = 1;
+    }
+    currInfo->lean = (int)xangle;
+    currInfo->accel = (float)((ax/4096)*9.8);
+    //pc.printf("%d",currInfo->lean);
+    prevx = ax;
+    prevy = ay;
+    prevz = az;
+}
+void flushBuff(){
+    char temp = 0;
+    while(blue.readable()){
+        temp = blue.getc();
+    }
+    return;
+}
+void updateTempH(){
+    sense.read();
+    currInfo->temp = sense.getFahrenheit();
+    currInfo->temp = currInfo->temp;
+    currInfo->humidity = sense.getHumidity();
+    currInfo->humidity = currInfo->humidity;
+}
\ No newline at end of file
--- a/parse.cpp	Tue Apr 24 21:17:05 2018 +0000
+++ b/parse.cpp	Mon Apr 30 18:18:19 2018 +0000
@@ -69,4 +69,5 @@
     a->speed = 0;
     a->humidity = 0;
     a->hr = 0;
+    a->crash = 0;
 }    
\ No newline at end of file
--- a/parse.h	Tue Apr 24 21:17:05 2018 +0000
+++ b/parse.h	Mon Apr 30 18:18:19 2018 +0000
@@ -10,6 +10,7 @@
     int temp;
     int humidity;
     int gear;
+    int crash;
 };
 int parse(char*,info*);
 void initializeStruct(info*);