Example software of using the mbed-rtos to control a simple vehicle's on board computer

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Revision:
7:f09208f9a4f7
Parent:
6:572b9755f2c1
Child:
8:6fad4bd89240
--- a/main.cpp	Wed Mar 30 04:01:53 2016 +0000
+++ b/main.cpp	Wed Mar 30 09:35:10 2016 +0000
@@ -1,15 +1,35 @@
+/* #####################################################################
+                               main.cpp
+                               ---------
+                
+                     Embedded Software - Assignment 3
+                     --------------------------------
+ 
+ Written by:        Steven Kay
+ 
+ Date:              March 2016
+ 
+ Function:          This is the main runner containing the Cyclic executive
+                    There are 7 defined tasks and several Auxillary components
+                    which are logically ran periodically at their required time
+                    by a Cyclic Executive sequencer.
+                    Ticks, or slots, to this Cyclic Executive are provided by
+                    a ticker ever 25ms, and then using logical expressions, the
+                    correct task is initiated and allocated the required time.
+ 
+ ##################################################################### */
+ 
 #include "mbed.h"
 #include "rtos.h"
 #include "MCP23017.h"
 #include "WattBob_TextLCD.h"
 
+// ============================================================================
+// Define Statements
+// ============================================================================
 #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;
-WattBob_TextLCD *lcd;
-
 #define TRUE 1
 #define FALSE 0
 
@@ -19,29 +39,37 @@
 // ============================================================================
 // MBED Pin Assignments
 // ============================================================================
-AnalogIn Brake(p19);
-AnalogIn Accelerometer(p20);
+
+// System Inputs
+AnalogIn Brake(p19);                        // Brake Pedal
+AnalogIn Accelerometer(p20);                // Accelerator Pedal
 
-DigitalIn EngineState(p18);
-DigitalIn LeftIndicator(p17);
-DigitalIn RightIndicator(p16);
-DigitalIn SideLightIndicator(p15);
+DigitalIn EngineState(p18);                 // Engine State Switch
+DigitalIn LeftIndicator(p17);               // Left Indicator Switch
+DigitalIn RightIndicator(p16);              // Right Indicator Switch
+DigitalIn SideLightIndicator(p15);          // Side Light Indicator
+
 
-DigitalOut EngineStateInd(LED1);
-DigitalOut SideLightInd(LED2);
+// System Outputs
+DigitalOut EngineStateInd(LED1);            // Engine State LED
+DigitalOut SideLightInd(LED2);              // Side Light LED
 
-PwmOut LeftLightInd(LED3);
-PwmOut RightLightInd(LED4);
-PwmOut OverSpeedInd(p22);
+PwmOut LeftLightInd(LED3);                  // Left Indicator LED
+PwmOut RightLightInd(LED4);                 // Right Indicator LED
+PwmOut OverSpeedInd(p22);                   // OverSpeed LED
 
-PwmOut AvSpeedWiper(p21);
+PwmOut AvSpeedWiper(p21);                   // Average Speed Wiper
 
-Serial PCConn(USBTX,USBRX);
+Serial PCConn(USBTX,USBRX);                 // Connection to PC
 
+MCP23017 *par_port;                         // Object pointing to Expander
+WattBob_TextLCD *lcd;                       // LCD Connection
 
 // ============================================================================
 // Global Data Structure Declerations
 // ============================================================================
+
+
 typedef struct
 {
     bool EngineState;
@@ -68,7 +96,6 @@
 typedef struct
 {
     float AverageSpeed;
-    float totalDistance;
 } CarFilteredParams;
 
 Mutex filteredParamsMutex;
@@ -76,21 +103,33 @@
 
 float totalDistance;
 
+typedef struct
+{
+    float currentAverageSpeed;
+    float currentAccelerometer;
+    float currentBraking;
+} PCDump_t;
+
+Mail<PCDump_t,100> Memory_Dump;
+
+Mutex MailMutex;
+int MailQueueCounter;
+
 // ============================================================================
 // Car Simulation
 // ============================================================================
 
 void CarSimulator(void const *arg)
 {
-    PCConn.printf("CarSim\r\n");
+//    PCConn.printf("CarSim\r\n");
     
     float newSpeed;
     
     rawParamsMutex.lock();
     float currentAccelerometer = rawParams.RawAccelerometer;
-    PCConn.printf("A: %f\r\n",currentAccelerometer);
+//    PCConn.printf("A: %f\r\n",currentAccelerometer);
     float currentBrake = rawParams.RawBraking;
-    PCConn.printf("B: %f\r\n",currentBrake);
+//    PCConn.printf("B: %f\r\n",currentBrake);
     bool currentEngineState = rawParams.EngineState;
     rawParamsMutex.unlock();   
 
@@ -117,7 +156,7 @@
 
 void Task1_ReadRawData(void const *arg)
 {
-      PCConn.printf("Task1\r\n");
+//      PCConn.printf("Task1\r\n");
       rawParamsMutex.lock();
       rawParams.RawBraking = Brake.read();
       rawParams.RawAccelerometer = Accelerometer.read();
@@ -128,7 +167,7 @@
 
 void Task2_ReadEngineState(void const *arg)
 {
-    PCConn.printf("Task2\r\n");
+//    PCConn.printf("Task2\r\n");
     bool currentEngineState = EngineState.read();
     rawParamsMutex.lock();
     rawParams.EngineState = currentEngineState;
@@ -148,18 +187,18 @@
         
 void Task3_CalcAvSpeed(void const *arg)
 {
-    PCConn.printf("Task3\r\n");
+//    PCConn.printf("Task3\r\n");
     float speedTotal = 0.0;
     
     for(int num = 0; num < 3; num++)
     {
         speedTotal = speedTotal + speedParams.rawSpeed[num];
-        PCConn.printf("Total: %f\r\n",speedTotal);
+//        PCConn.printf("Total: %f\r\n",speedTotal);
     }
 
     filteredParamsMutex.lock();
     filteredParams.AverageSpeed = (speedTotal/3);
-    PCConn.printf("Av: %f\r\n",filteredParams.AverageSpeed);
+//    PCConn.printf("Av: %f\r\n",filteredParams.AverageSpeed);
     filteredParamsMutex.unlock();
 }
 
@@ -167,7 +206,7 @@
 
 void Task4_UpdateRCWiper(void const *arg)
 {
-    PCConn.printf("Task4\r\n");
+//    PCConn.printf("Task4\r\n");
     filteredParamsMutex.lock();
     float currentAverageSpeed = filteredParams.AverageSpeed;
     filteredParamsMutex.unlock();
@@ -179,7 +218,7 @@
 
 void Task5_OverspeedLED(void const *arg)
 {
-    PCConn.printf("Task5\r\n");
+//    PCConn.printf("Task5\r\n");
     SpeedMutex.lock();
     float currentInstSpeed = speedParams.rawSpeed[speedParams.counter];
     SpeedMutex.unlock();
@@ -212,26 +251,63 @@
 }
 
 
-
 void Task7_SendToMailQueue(void const *arg)
 {
+    filteredParamsMutex.lock();
+    float currentAverageSpeed = filteredParams.AverageSpeed;
+    filteredParamsMutex.unlock();
     
+    rawParamsMutex.lock();
+    float currentAccelerometer = rawParams.RawAccelerometer;
+    float currentBrake = rawParams.RawBraking;
+    rawParamsMutex.unlock();  
     
+    PCDump_t *currentPCDump = Memory_Dump.alloc();
+    currentPCDump -> currentAverageSpeed = currentAverageSpeed;
+    currentPCDump -> currentAccelerometer = currentAccelerometer;
+    currentPCDump -> currentBraking = currentBrake;
+    
+    Memory_Dump.put(currentPCDump);  
+    
+    MailMutex.lock();
+    MailQueueCounter++;
+    MailMutex.unlock();  
 }
 
 
 
 void Task8_DumpSerial(void const *arg)
 {
+    MailMutex.lock();
+    int currentQueueCounter = MailQueueCounter;
+    MailMutex.unlock();
     
+    PCConn.printf("Memory Dump\r\n");
     
+    for(int num = 0; num < currentQueueCounter; num++)
+    {
+        osEvent evt = Memory_Dump.get();
+        if(evt.status == osEventMail)
+        {
+            PCDump_t *currentPCDump = (PCDump_t*)evt.value.p;
+            
+            PCConn.printf("Av Speed: %f\r\nAcceler: %f\r\nBrake: %f\r\n\r\n",  currentPCDump -> currentAverageSpeed,
+                                                                    currentPCDump -> currentAccelerometer,
+                                                                    currentPCDump -> currentBraking);
+            Memory_Dump.free(currentPCDump);
+        }
+    }
+    
+    MailMutex.lock();
+    MailQueueCounter = 0;
+    MailMutex.unlock();
 }
 
 
 
 void Task9_ReadSideLight(void const *arg)
 {
-    PCConn.printf("Task9\r\n");
+//    PCConn.printf("Task9\r\n");
     if(SideLightIndicator)
     {
         SideLightInd = HIGH;
@@ -246,7 +322,7 @@
 
 void Task10_ReadIndicatorLights(void const *arg)
 {
-    PCConn.printf("Task10\r\n");
+//    PCConn.printf("Task10\r\n");
     // Left Indicator Only
     if(LeftIndicator && !RightIndicator)
     {
@@ -306,6 +382,8 @@
     filteredParams.AverageSpeed = 0.0;
     
     totalDistance = 0.0;
+
+    MailQueueCounter = 0;
 }
     
      
@@ -352,66 +430,3 @@
     
     
 }
-
-//
-//
-//Mutex readvalues;
-//Semaphore read_s(1);
-//
-//Mail<value_t, 100> mail_box;
-//
-//
-//float readValue = 1.0;
-//
-//typedef struct
-//{
-//    float AccelerationRaw;
-//    float BrakeRaw;
-//} value_t;
-//
-//
-//void readAnalogPins(void const *args)
-//{
-//    while(1)
-//    {
-//        value_t *ReadValue = mail_box.alloc();
-//        
-//        ReadValue -> AccelerationrRaw = Accelerometer.read();
-//        ReadValue -> BrakeRaw = Brake.read();
-//        
-//        mail_box.put(ReadValue);
-//        Thread::wait(100); 
-//    }
-//    
-//}
-//
-//void sendToPC(void const *args)
-//{
-//    while(1)
-//    {
-//        osEvent evt = mail_box.get();
-//        
-//        if(evt.status == osEventMail)
-//        {
-//            value_t *ReadValue = (value_t*)evt.value.p;
-//            printf("Value: %1.3f\r\n", ReadValue-> vale);
-//            mail_box.free(ReadValue);
-//        }
-//        Thread::wait(5000); 
-//    }
-//    
-//}
-//
-//int main() {
-//    
-//    Thread thread_1(readData);    
-//    Thread thread_2(sendToPC);
-//    
-//    while(1) {
-//        
-//        myled = 1;
-//        wait(0.2);
-//        myled = 0;
-//        wait(0.2);
-//    }
-//}