This is car control simulation by using Mbed controller and real time operating system.

Dependencies:   MCP23017 Servo WattBob_TextLCD mbed-rtos mbed

Fork of Ass3 by Muaiyd Al-Zandi

Files at this revision

API Documentation at this revision

Comitter:
muaiyd
Date:
Wed May 07 10:25:50 2014 +0000
Parent:
12:8eb2c1cccee6
Commit message:
Final Version

Changed in this revision

CAR.cpp Show annotated file Show diff for this revision Revisions of this file
CAR.h Show annotated file Show diff for this revision Revisions of this file
CommonVariable.cpp Show annotated file Show diff for this revision Revisions of this file
CommonVariable.h 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
--- a/CAR.cpp	Tue May 06 09:49:08 2014 +0000
+++ b/CAR.cpp	Wed May 07 10:25:50 2014 +0000
@@ -1,10 +1,13 @@
+/***********************************
+*             CAR.cpp              *
+************************************/
 #include "CAR.h"
 
 //Define semaphores to manage accessing the viriables
 Semaphore CAR_MAIL_SEM(1);
 Semaphore INPUT_SEM(1); 
 Semaphore Speed_SEM(1); 
-Semaphore SWITCH_SEM(2);
+
 Semaphore AVR_SPEED_SEM(2);
 /* Two counter the first is counting the number of element that
     was sended to the MAIL. The another is to count the number of
@@ -14,8 +17,6 @@
 //  Initialise a mail with 100 element
 static Mail<CAR_MAIL, 100> mail_box;
 
-bool LSide_Indicator_Value;
-bool RSide_Indicator_Value;
  // Create the local filesystem under the name "local"
 LocalFileSystem local("local");              
 
@@ -44,17 +45,20 @@
         Accelerometer_Value = Accelerometer.read();
         Brake_Value =  Brake.read();
         EngineStat = EngineSwitch;
+        R_LightSide_SW = R_Light_Switch;
+        L_LightSide_SW = L_Light_Switch;
+        R_Indicator_SW = R_Indicator_Switch;
+        L_Indicator_SW = L_Indicator_Switch;
+        INPUT_SEM.release();
         Speed_SEM.wait();
-        Speed[Counter] = Get_Speed();
+        Speed[Counter] = Measure_Speed();
         Speed_SEM.release();
         Counter++;
         if(Counter > 2) Counter = 0;
-        
-        INPUT_SEM.release();
         Thread::wait(100);
     }
 }
-uint8_t CAR::Get_Speed(){
+uint8_t CAR::Measure_Speed(){
     return (Accelerometer_Value * MaxSpeed * (1 - Brake_Value) * EngineStat) ;
 }
 
@@ -96,7 +100,7 @@
 /*  To task in this function becuase boath are repeted every 0.5 Sec
     1- Update the ODOmeter
     2- Read the indecator swetches */
-void CAR::Odo_Show_Indicator_Switch_Read(void const *args){
+void CAR::Odo_Show(void const *args){
     while(true){
         LCD.locate(0,0);
         AVR_SPEED_SEM.wait();
@@ -107,11 +111,6 @@
         LCD.printf("ODO %4.2f M",GET_ODO());
         AVR_SPEED_SEM.release();
         
-        SWITCH_SEM.wait();
-        LSide_Indicator_Value = LSide_Indicator_Switch;
-        RSide_Indicator_Value = RSide_Indicator_Switch;
-        SWITCH_SEM.release();
-        
         Thread::wait(500);
     }
 }
@@ -130,11 +129,13 @@
         INPUT_SEM.wait();
         mail->Mail_Accelerometer_Value = Accelerometer_Value;
         mail->Mail_Brake_Value = Brake_Value;
+        INPUT_SEM.release();
+        
         mail->Counter = Element_Counter_W ;
         mail_box.put(mail);
         Element_Counter_W++;
+        
         CAR_MAIL_SEM.release();
-        INPUT_SEM.release();
         
         Thread::wait(5000);              
     }
@@ -170,18 +171,18 @@
 // Flashing the indecator light
 void CAR::Side_Light_Flash(void const *args){
     while(true){
-        SWITCH_SEM.wait();
+        INPUT_SEM.wait();
         // If both side are enabled
-        if((LSide_Indicator_Value == 1) && (RSide_Indicator_Value == 1) ){
+        if((L_Indicator_SW == 1) && (R_Indicator_SW == 1) ){
             L_Side_Indicator = L_Side_Indicator ^ 1;
             R_Side_Indicator = L_Side_Indicator;
         }
         //If just one side enabled
-        else if(RSide_Indicator_Value == 1){
+        else if(R_Indicator_SW == 1){
             R_Side_Indicator = R_Side_Indicator ^ 1;
             L_Side_Indicator = 0;
         }
-        else if(LSide_Indicator_Value == 1){
+        else if(L_Indicator_SW == 1){
             L_Side_Indicator = L_Side_Indicator ^ 1;
             R_Side_Indicator = 0;
         }
@@ -190,25 +191,17 @@
             L_Side_Indicator = 0;
             R_Side_Indicator = 0 ;
         }
-        SWITCH_SEM.release();
-        Thread::wait(1000);
+        INPUT_SEM.release();
+        Thread::wait(500);
     }
 }
 // Check the side light swetches and turn on or off the light
 void CAR::Side_Light(void const *args){
     while(true){
-        if(LSide_Light_Switch){
-            L_Side_Light = 1;
-        }
-        else{
-            L_Side_Light = 0;
-        }
-        if(RSide_Light_Switch){
-            R_Side_Light = 1;
-        }
-        else{
-            R_Side_Light = 0;
-        }
+        INPUT_SEM.wait();
+        L_Side_Light = L_LightSide_SW;
+        R_Side_Light = R_LightSide_SW;
+        INPUT_SEM.release();
         Thread::wait(1000);
     }
 }
\ No newline at end of file
--- a/CAR.h	Tue May 06 09:49:08 2014 +0000
+++ b/CAR.h	Wed May 07 10:25:50 2014 +0000
@@ -1,3 +1,6 @@
+/***********************************
+*              CAR.h               *
+************************************/
 #ifndef CAR_H
 #define CAR_H
 #include "rtos.h"
@@ -17,18 +20,16 @@
         static float GET_ODO();
         
         static void Car_Simulation(void const *args);
-        static uint8_t Get_Speed();
-        
+        static uint8_t Measure_Speed();
         static void Average_Speed_Measure(void const *args);
         static void Average_Speed_Show(void const *args);
         static void OverSpeed(void const *args);
-        static void Odo_Show_Indicator_Switch_Read(void const *args);
+        static void Odo_Show(void const *args);
         static void SEND_CAR_VALUES (void const *args);
         static void DUMP_CAR_VALUES_En (void const *args);
         static void DUMP_CAR_VALUES();
         static void Side_Light_Flash(void const *args);
         static void Side_Light(void const *args);
-    private:
         
 };
 
--- a/CommonVariable.cpp	Tue May 06 09:49:08 2014 +0000
+++ b/CommonVariable.cpp	Wed May 07 10:25:50 2014 +0000
@@ -1,13 +1,19 @@
+/***********************************
+*       CommonVariable.cpp         *
+************************************/
 #include "CommonVariable.h"
 // The maximam speed of this simulation is 140 MPH
 const uint8_t MaxSpeed  = 140; 
-
+//Initialize all the common varaible
 bool EngineStat = 0;
 bool IsOverSpeed = 0;
+bool R_LightSide_SW = 0;
+bool L_LightSide_SW = 0;
+bool R_Indicator_SW = 0;
+bool L_Indicator_SW = 0;
 float Accelerometer_Value = 0;
 float Brake_Value = 0;
 uint8_t Speed[3] = {0,0,0};
 uint8_t Average_Speed = 0;
-
 float Odometer_Value = 0;
 uint8_t Counter = 0;
\ No newline at end of file
--- a/CommonVariable.h	Tue May 06 09:49:08 2014 +0000
+++ b/CommonVariable.h	Wed May 07 10:25:50 2014 +0000
@@ -1,3 +1,6 @@
+/***********************************
+*       CommonVariable.h           *
+************************************/
 #ifndef COMMON_VARIABLE_H
 #define COMMON_VARIABLE_H
 #include "MCP23017.h"
@@ -8,29 +11,34 @@
 static Servo SpeedShow_Servo(p21); // Use pin 21 to control the servo
 static MCP23017 Port(p9,p10,0x40) ;      // 16-bit object with I2C Chip MCP23017 
 static WattBob_TextLCD LCD(&Port);       // A 2*16 chacater LCD object
+//Input and Output ports
 static BusOut OverSpeedLED(LED1 , LED2 , LED3 , LED4);
 static DigitalIn EngineSwitch(p5);
 static DigitalOut R_Side_Light(p27);
 static DigitalOut L_Side_Light(p28);
 static DigitalOut R_Side_Indicator(p29);
 static DigitalOut L_Side_Indicator(p30);
-static DigitalIn LSide_Light_Switch(p6);
-static DigitalIn RSide_Light_Switch(p7);
-static DigitalIn LSide_Indicator_Switch(p15);
-static DigitalIn RSide_Indicator_Switch(p16);
+static DigitalIn L_Light_Switch(p6);
+static DigitalIn R_Light_Switch(p7);
+static DigitalIn L_Indicator_Switch(p15);
+static DigitalIn R_Indicator_Switch(p16);
 static AnalogIn Accelerometer(p19);
 static AnalogIn Brake(p20);
 
 extern const uint8_t MaxSpeed;
-
+//Inpout Data Variable
 extern bool EngineStat;
 extern bool IsOverSpeed;
+extern bool R_LightSide_SW;
+extern bool L_LightSide_SW;
+extern bool R_Indicator_SW;
+extern bool L_Indicator_SW;
 extern float Accelerometer_Value;
 extern float Brake_Value;
+
 extern uint8_t Speed[3];
 extern uint8_t Average_Speed;
 extern float Odometer_Value;
 extern uint8_t Counter;
 
-
 #endif
\ No newline at end of file
--- a/main.cpp	Tue May 06 09:49:08 2014 +0000
+++ b/main.cpp	Wed May 07 10:25:50 2014 +0000
@@ -1,9 +1,12 @@
 /*
+By Muaiyd Hameed Mohammed Al-Zandi
+*****************************
+*        MAIN.CPP           *
+*****************************
 Small software to simulate a car and show the Average speed and the odometer
 on the LCD. Also, it saves the average speed and accelerometer and brake value
-in a special queue. This queue is sumped into a file every 20 second.
-Author : Muaiyd Hameed Al-Zandi
-Date   : 09/04/2014 
+in a special queue. This queue is dumped into a file every 20 second.
+ 
 */
 #include "CAR.h"
 
@@ -12,14 +15,12 @@
     CAR car1;
     //Define the multy threat function
     Thread Car_Simulation_Thread(car1.Car_Simulation);
-    
     Thread Average_Speed_Measure_Thread(car1.Average_Speed_Measure);
     Thread Average_Speed_Show_Thread(car1.Average_Speed_Show);
     Thread OverSpeed_Thread(car1.OverSpeed);
-    Thread ODO_And_Indicator_Switch_Read_Thread(car1.Odo_Show_Indicator_Switch_Read);
+    Thread ODO_Thread(car1.Odo_Show);
     Thread SEND_CAR_VALUES_Thread(car1.SEND_CAR_VALUES);
     Thread DUMP_CAR_VALUES_Thread(car1.DUMP_CAR_VALUES_En);
-    
     Thread Side_Light_Flash_Thread(car1.Side_Light_Flash);
     Thread Side_Light_Thread(car1.Side_Light);