Aaron Stevenson / Mbed 2 deprecated Assignment3

Dependencies:   mbed MCP23017 mbed-rtos WattBob_TextLCD

Revision:
4:72c487215487
Parent:
3:d888509e77b8
Child:
5:e62e0e3084fc
diff -r d888509e77b8 -r 72c487215487 main.cpp
--- a/main.cpp	Mon Mar 26 14:56:16 2018 +0000
+++ b/main.cpp	Tue Mar 27 10:15:57 2018 +0000
@@ -7,10 +7,10 @@
 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
 
-MCP23017 *par_port;     // pointer to 16-bit parallel I/O object
-WattBob_TextLCD *lcd;   // pointer to 2*16 chacater LCD object
+MCP23017 *par_port;             // pointer to 16-bit parallel I/O object
+WattBob_TextLCD *lcd;           // pointer to 2*16 chacater LCD object
 
-AnalogIn accel(p15);       // Analog
+AnalogIn accel(p15);            // Analog
 AnalogIn brake(p16);
 float speed = 0;
 float aveSpeed;
@@ -19,23 +19,26 @@
 float dif;
 float distance = 0;
 
-Timer tim;
+Timer tim, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;// integrate with wait?
 long int t;
+int r;
 
-DigitalOut sideLights(LED1);// mbed out
+int indicount = 0;
+
+DigitalOut sideLights(LED1);    // mbed out
 DigitalOut lIndicator(LED2);
 DigitalOut rIndicator(LED3);
 DigitalOut engLight(LED4);
 
-DigitalOut brakeLights(p6);// redbox out
+DigitalOut brakeLights(p6);     // redbox out
 DigitalOut fluxCapacitor(p7);
 
-DigitalIn  lightSwitch(p8);// switches
+DigitalIn  lightSwitch(p8);     // switches
 DigitalIn  lIndicate(p9);
 DigitalIn  rIndicate(p10);
 DigitalIn  engine(p5);
 
-Serial pc(USBTX, USBRX);// serial tx, rx
+Serial pc(USBTX, USBRX);        // serial tx, rx
 
 Thread sp;
 Thread task1;
@@ -49,17 +52,92 @@
 Thread task9;
 Thread task10;
 
-/* Mail */
-typedef struct {
+typedef struct {                                    //  mail
     float m_speed;
     float m_acc;
     float m_br;
-    uint32_t counter; /* A counter value               */
+    uint32_t counter; // A counter value
 } mail_t;
 
 Mail<mail_t, 100> mail_box;
 
-void send_thread (void)
+void acceleration()  //run at 20hz                  //  1.  read brake and accelerator       10
+{
+    while (1) {
+        acc = accel.read()*3.3;
+        br = brake.read()*3.3;
+        Thread::wait(500-r);
+    }
+}
+void getSpeed()                                     // 20Hz calculation for speed & distance
+{
+    while (1) {
+        dif = acc - br;
+        t = tim.read();
+        speed = speed + (dif*t*3.6);
+        if (speed < 0) {
+            speed = 0;
+        }
+        distance = speed*t + (0.5*dif*(t*t))/1000;
+
+        Thread::wait(100);
+    }
+}
+void ignition()                                     //  2.   Read engine on/off show LED     2
+{
+    while (1) {
+        if (engine == 1) {
+            engLight = 1;
+        }
+        Thread::wait(500-r);
+    }
+}
+void speedo()                                       //  3.   Average last n speed readings   5
+{
+    while (1) {
+        for (int i = 0; i<3; i++) {
+            aveSpeed = speed + aveSpeed;
+        }
+        speed = speed/4;
+
+        Thread::wait(200);
+    }
+}
+void braking()                                      //  4.   Brake indicated by LED          2
+{
+    while (1) {
+        if ( br>0) {
+            brakeLights = 1;
+        }
+
+        Thread::wait(500);
+    }
+}
+void greatScott()                                   //  5.   if speed > 88 LED on            1
+{
+    while (1) {
+        if (speed > 140) {
+            fluxCapacitor = 1;
+        }
+
+        Thread::wait(1000);
+    }
+}
+void LCD()                                          //  6.   display odometer and speed      2
+{
+    while (1) {
+        t = tim.read();
+        distance = speed * (t/3600);
+
+        lcd->locate(0,0);
+        lcd->printf("KM:%0.1f",distance);
+        lcd->locate(1,0);
+        lcd->printf("KMPH:%0.1f",speed);
+
+        Thread::wait(500);
+    }
+}
+void send_thread (void)                             //  7.  Send speed, acc, brake to MAILq 0.2 
 {
     uint32_t i = 0;
     while (true) {
@@ -74,99 +152,28 @@
         osEvent evt = mail_box.get();
         if (evt.status == osEventMail) {
             mail_t *mail = (mail_t*)evt.value.p;
-            printf("\nSpeed: %.2f \n\r"   , mail->m_speed);
+            printf("\nSpeed: %.1f \n\r"   , mail->m_speed);
             printf("Acceleration: %.2f \n\r"     , mail->m_acc);
-            printf("Braking: %.2f \n\r", mail->m_br);
+            printf("Braking: %.1f \n\r", mail->m_br);
 
             mail_box.free(mail);
         }
         Thread::wait(5000);
     }
 }
-
-
-void acceleration()  //run at 20hz                  //  1  read brake and accelerator       10
-{
-    while (1) {
-        acc = accel.read()*3.3;
-        br = brake.read()*3.3;
-        Thread::wait(500);
-    }
-}
-void getSpeed()                                     // 20Hz
-{
-    while (1) {
-        dif = acc - br;
-        t = tim.read();
-        speed = speed + (dif*t*3.6);
-        if (speed < 0) {
-            speed = 0;
-        }
-        distance = distance + (speed*t + (0.5*dif*(t*t)))/1000;
-        tim.reset();
-
-        Thread::wait(100);
-    }
-}
-void ignition()                                     //  2   Read engine on/off show LED     2
-{
-    while (1) {
-        if (engine == 1) {
-            engLight = 1;
-        }
-
-        Thread::wait(500);
-    }
-}
-void speedo()                                       //  3   Average last n speed readings   2
+void toSerial()                                     //  8.   MAIL q to serial PC            0.05
 {
     while (1) {
-        for (int i = 0; i<3; i++) {
-            aveSpeed = speed + aveSpeed;
-        }
-        speed = speed/4;
+        osEvent evt = mail_box.get();
+        mail_t *mail = (mail_t*)evt.value.p;
+        pc.printf("\nSpeed: %.2f \n\r", mail->m_speed);
+        pc.printf("Acceleration: %.2f \n\r", mail->m_acc);
+        pc.printf("Braking: %.2f \n\r", mail->m_br);
 
-        Thread::wait(200);
-    }
-}
-void braking()                                      //  4   Brake indicated by LED          2
-{
-    while (1) {
-        if ( br>0) {
-            brakeLights = 1;
-        }
-
-        Thread::wait(500);
+        Thread::wait(20000);
     }
 }
-void greatScott()                                   //  5   if speed > 88 LED on            1
-{
-    while (1) {
-        if (speed > 88) {
-            fluxCapacitor = 1;
-        }
-
-        Thread::wait(1000);
-    }
-}
-void LCD()                                          //  6   display odometer and speed      2
-{
-    while (1) {
-        t = tim.read();
-        distance = speed * (t/3600);
-
-        lcd->locate(0,0);
-        lcd->printf("KM:%0.1f",distance);
-        lcd->locate(1,0);
-        lcd->printf("KMPH:%0.1f",speed);
-
-        Thread::wait(500);
-    }
-}
-//    7       Send speed, acc, brake to 100       0.2
-//           element MAIL q MBED RTOS
-//    8       MAIL q to serial PC                 0.05
-void lights()                                       //  9   side light switch, set lights   1
+void lights()                                       //  9.   set side lights                1
 {
     while (1) {
         if (lightSwitch == 1) {
@@ -178,38 +185,28 @@
         Thread::wait(1000);
     }
 }
-void indicators()                                   //    10.     Read indicator switches, flash
+void indicators()                                   //  10.  flash indicators               0.5
 {
     while (1) {
-        if ((lIndicate == 1) && (rIndicate == 1)) {     //      both LED at 2Hz If both switch on
-            lIndicator = !lIndicator;
+        
+        if ((lIndicate == 1) && (rIndicate == 1)) {         //  If both switch on
+            lIndicator = !lIndicator;                       //      both LED at 2Hz 
             rIndicator = !rIndicator;
-            //   Thread::wait(2000);
+            wait(0.5);
         }
-        if ((lIndicate == 1) && (rIndicate == 0)) {     //        if left switch on
-            lIndicator = !lIndicator;               //          left LED at 1Hz
-            // Thread::wait(1000);
+        else if ((lIndicate == 1) && (rIndicate == 0)) {    //  if left switch on
+            lIndicator = !lIndicator;                       //      left LED at 1Hz
+            wait(1);
         }
-        if ((lIndicate == 0) && (rIndicate == 1)) {     //        if right switch on
-            rIndicator = !rIndicator;               //          right LED at 1Hz
-            // Thread::wait(1000);
+        else if ((lIndicate == 0) && (rIndicate == 1)) {    //  if right switch on
+            rIndicator = !rIndicator;                       //      right LED at 1Hz
+            wait(1);
         }
 
         Thread::wait(2000);
     }
 }
-void toSerial()//7
-{
-    while (1) {
-        osEvent evt = mail_box.get();
-        mail_t *mail = (mail_t*)evt.value.p;
-        pc.printf("\nSpeed: %.2f \n\r", mail->m_speed);                      
-        pc.printf("Acceleration: %.2f \n\r", mail->m_acc);                            
-        pc.printf("Braking: %.2f \n\r", mail->m_br);
-        
-        Thread::wait(20000);
-    }
-}
+
 
 int main()
 {