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

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Revision:
2:13a9394ba2e0
Parent:
1:cdf851858518
Child:
3:8192bbde17b3
--- a/main.cpp	Tue Mar 29 19:25:06 2016 +0000
+++ b/main.cpp	Tue Mar 29 21:30:00 2016 +0000
@@ -4,6 +4,12 @@
 #define TRUE 1
 #define FALSE 0
 
+#define HIGH 1
+#define LOW 0
+
+// ============================================================================
+// MBED Pin Assignments
+// ============================================================================
 AnalogIn Brake(p19);
 AnalogIn Accelerometer(p20);
 
@@ -17,7 +23,116 @@
 
 PwmOut LeftLightInd(LED3);
 PwmOut RightLightInd(LED4);
-PwmOut OverSpeedInd(p21);
+PwmOut OverSpeedInd(p22);
+
+PwmOut AvSpeedWiper(p21);
+
+// ============================================================================
+// Global Data Structure Declerations
+// ============================================================================
+typedef struct
+{
+    bool EngineState;
+    float RawAccelerometer;
+    float RawBraking;
+} CarRawParams;
+
+Mutex rawParamsMutex;
+CarRawParams rawParams;
+
+
+typedef struct
+{
+    float rawAcceleration;
+    float rawSpeed;
+} CarProcessedParams;
+
+Mutex processedParamsMutex;
+CarProcessedParams processedParams;
+
+
+typedef struct
+{
+    float AverageSpeed;
+} CarFilteredParams;
+
+Mutex filteredParamsMutex;
+CarFilteredParams filteredParams;
+
+
+// ============================================================================
+// Car Simulation
+// ============================================================================
+
+void CarSimulator(void const *arg)
+{
+    rawParamsMutex.lock();
+    float currentAccelerometer = rawParams.RawAccelerometer;
+    float currentBrake = rawParams.RawBraking;
+    bool currentEngineState = rawParams.EngineState;
+    rawParamsMutex.unlock();
+    
+    processedParamsMutex.lock();
+    float currentRawSpeed = processedParams.rawSpeed;
+    processedParamsMutex.unlock();
+    
+    if(currentEngineState)
+    {
+        if(currentRawSpeed >= 100)
+        {
+            processedParamsMutex.lock();
+            processedParams.rawSpeed = 100;
+            processedParamsMutex.unlock();
+        }
+        else
+        {
+            // Car Sim Model
+        }
+    }
+}
+
+
+// ============================================================================
+// Control System Tasks
+// ============================================================================
+
+void Task1_ReadRawData(void const *arg)
+{
+      rawParamsMutex.lock();
+      rawParams.RawBraking = Brake.read();
+      rawParams.RawAccelerometer = Accelerometer.read();
+      rawParamsMutex.unlock();
+}
+
+
+
+void Task2_ReadEngineState(void const *arg)
+{
+    rawParamsMutex.lock();
+    bool currentEngineState = rawParams.EngineState;
+    rawParamsMutex.unlock();
+    
+    if(currentEngineState)
+    {
+        EngineStateInd = HIGH;
+    }
+    else
+    {
+        EngineStateInd = LOW;
+    }
+}
+        
+
+
+void Task3_UpdateRCWiper(void const *arg)
+{
+    filteredParamsMutex.lock();
+    float currentAverageSpeed = filteredParams.AverageSpeed;
+    filteredParamsMutex.unlock();
+    
+    AvSpeedWiper.pulsewidth_ms(2*currentAverageSpeed);
+}
+
 
 
 void Task8_ReadSideLight(void const *arg)
@@ -32,39 +147,73 @@
     }  
 }
 
+
+
 void Task9_ReadIndicatorLights(void const *arg)
 {
     // Left Indicator Only
     if(LeftIndicator && !RightIndicator)
     {
-        LeftIndicator.period(1.0);
-        LeftIndicator.pulsewidth(0.5);
+        LeftLightInd.period(1.0);
+        LeftLightInd.pulsewidth(0.5);
+        
+        RightLightInd.period(1.0);
+        RightLightInd.pulsewidth(0.0);
     }
     
     // Right Indicator Only
     else if(!LeftIndicator && RightIndicator)
     {
-        RightIndicator.period(1.0);
-        RightIndicator.pulsewidth(0.5);
+        LeftLightInd.period(1.0);
+        LeftLightInd.pulsewidth(0.0);
+        
+        RightLightInd.period(1.0);
+        RightLightInd.pulsewidth(0.5);
     }
     
     // Left and Right Indicators
     else if(LeftIndicator && RightIndicator)
     {
-        LeftIndicator.period(0.5);
-        RightIndicator.period(0.5);
+        LeftLightInd.period(0.5);
+        RightLightInd.period(0.5);
         
-        LeftIndicator.pulsewidth(0.25);
-        RightIndicator.pulsewidth(0.25);
+        LeftLightInd.pulsewidth(0.25);
+        RightLightInd.pulsewidth(0.25);
+    }
+    
+    else
+    {
+        LeftLightInd.period(1.0);
+        LeftLightInd.pulsewidth(0.0);
+        
+        RightLightInd.period(1.0);
+        RightLightInd.pulsewidth(0.0);
     }
 }
         
+// ============================================================================
+// Entry Point Thread
+// ============================================================================
 
 int main()
 {
  
- RtosTimer Task8(Task8_ReadSideLight,osTimerPeriodic); 
- RtosTimer Task9(Task9_ReadIndicatorLights,osTimerPeriodic);
+ AvSpeedWiper.period_ms(20);
+ 
+ RtosTimer CarSim(CarSimulator,osTimerPeriodic); Thread::wait(2);
+ 
+ RtosTimer Task1(Task1_ReadRawData,osTimerPeriodic); Thread::wait(2);
+ RtosTimer Task2(Task2_ReadEngineState,osTimerPeriodic); Thread::wait(2);
+ RtosTimer Task3(Task3_UpdateRCWiper,osTimerPeriodic); Thread::wait(2);
+ RtosTimer Task8(Task8_ReadSideLight,osTimerPeriodic); Thread::wait(2);
+ RtosTimer Task9(Task9_ReadIndicatorLights,osTimerPeriodic); Thread::wait(2);
+ 
+ CarSim.start(50);
+ 
+ Task1.start(100);
+ Task2.start(500);
+ Task3.start(1000);
+ 
  
  Task8.start(1000);
  Task9.start(2000);