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:
Mon Apr 07 15:19:37 2014 +0000
Parent:
0:68ce46607848
Child:
2:707ab9d39a64
Child:
4:952a2d814fb1
Child:
6:5037779f6a55
Commit message:
Finish the semefor with mail

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	Wed Apr 02 09:13:57 2014 +0000
+++ b/CAR.cpp	Mon Apr 07 15:19:37 2014 +0000
@@ -1,8 +1,119 @@
 #include "CAR.h"
+Semaphore CAR_MAIL_SEM(1);
+uint32_t Element_Counter_W = 0;
+uint32_t Element_Counter_R = 0;
+typedef struct {
+            uint8_t Mail_Average_Speed; 
+            float   Mail_Accelerometer_Value;
+            float   Mail_Brake_Value;
+            int     Counter;
+        } CAR_MAIL;
+static Mail<CAR_MAIL, 100> mail_box;
 
-void CAR::led2_thread(void const *args) {
+ 
+CAR::CAR(){
+    Port.write_bit(1,BL_BIT); 
+     
+      
+}
+void CAR::SAVE_ODO(float value) {
+    LPC_RTC->GPREG0 = *((uint32_t*)&value);
+}
+ 
+float CAR::GET_ODO() {
+    return *((float*)&(LPC_RTC->GPREG0));
+}
+void CAR::Accelero_Brake_Read(void const *args){
     while (true) {
-        led2 = !led2;
+        Accelerometer_Value = Accelerometer.read();
+        Brake_Value =  Brake.read();
+        EngineStat = EngineSwitch;
+        Speed[Counter] = Accelerometer_Value * MaxSpeed * (1 - Brake_Value) * EngineStat ;
+        Counter++;
+        if(Counter > 2) Counter = 0;
+        Thread::wait(100);
+    }
+}
+
+void CAR::Average_Speed_Measure(void const *args) {
+    while (true) {
+        Average_Speed = ( Speed[0] + Speed[1] + Speed[2] )/3 ;
+        Thread::wait(200);
+    }
+}
+
+void CAR::Average_Speed_Show(void const *args){
+    while(1){
+        SpeedShow = 1.0 - (float)Average_Speed / (float)MaxSpeed ;
+        SpeedShow_Servo = SpeedShow ;
         Thread::wait(1000);
     }
+}
+void CAR::OverSpeed(void const *args){
+    while(true){
+    if(Average_Speed > 70)
+            IsOverSpeed = 1;
+            
+        else
+            IsOverSpeed = 0;
+            
+        Thread::wait(2000);
+    }       
+}
+
+void CAR::Odometer_Measure(void const *args){
+    while(true){
+        Odometer_Value = GET_ODO();
+        Odometer_Value = Odometer_Value + Average_Speed / 7200.0 ;
+        SAVE_ODO(Odometer_Value);
+        Thread::wait(500);
+    }
+}
+void CAR::Odometer_Show(void const *args){
+    while(true){
+        LCD.locate(0,0);
+        LCD.printf("AvrgSpd %3D MPH",Average_Speed);
+        LCD.locate(1,0);
+        LCD.printf("OdoVlu %4.2f M",Odometer_Value);
+        Thread::wait(500);
+    }
+}
+void CAR::SEND_CAR_VALUES (void const *args) {
+    
+    while (true) {
+        CAR_MAIL *mail = mail_box.alloc();
+        CAR_MAIL_SEM.wait();
+        mail->Mail_Average_Speed = Average_Speed; 
+        mail->Mail_Accelerometer_Value = Accelerometer_Value;
+        mail->Mail_Brake_Value = Brake_Value;
+        mail->Counter = Element_Counter_W ;
+        mail_box.put(mail);
+        printf("\n %i £ \n\r",Element_Counter_W);
+        Element_Counter_W++;
+        Thread::wait(1000); 
+        CAR_MAIL_SEM.release();             
+    }
+}
+void CAR::DUMP_CAR_VALUES_En (void const *args) {
+    while (true) {
+        L1 = L1 ^ 1;
+        CAR_MAIL_SEM.wait();
+        while (Element_Counter_W > Element_Counter_R)
+            DUMP_CAR_VALUES();
+        CAR_MAIL_SEM.release();
+        Thread::wait(5000);
+    }
+}
+void CAR::DUMP_CAR_VALUES () {
+    printf("Writing \n\r");
+    osEvent evt = mail_box.get(10);
+    CAR_MAIL *mail = (CAR_MAIL*)evt.value.p;
+    if (evt.status == osEventMail) {
+            printf("\nAverage_Speed: %i MPH\n\r"   , mail->Mail_Average_Speed);
+            printf("Accelerometer_Value: %.3f %\n\r"     , mail->Mail_Accelerometer_Value);
+            printf("Brake_Value: %.3f %\n\r", mail->Mail_Brake_Value);
+            printf("\nCounter: %i \n\r"   , mail->Counter);
+            mail_box.free(mail);
+            Element_Counter_R++;        
+    }
 }
\ No newline at end of file
--- a/CAR.h	Wed Apr 02 09:13:57 2014 +0000
+++ b/CAR.h	Mon Apr 07 15:19:37 2014 +0000
@@ -5,7 +5,18 @@
 
 class CAR {
     public:
-        static void led2_thread(void const *args);
+        CAR();
+        static void SAVE_ODO(float value);
+        static float GET_ODO();
+        static void Accelero_Brake_Read(void const *args);
+        static void Average_Speed_Measure(void const *args);
+        static void Average_Speed_Show(void const *args);
+        static void OverSpeed(void const *args);
+        static void Odometer_Measure(void const *args);
+        static void Odometer_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();
     private:
         
 };
--- a/CommonVariable.cpp	Wed Apr 02 09:13:57 2014 +0000
+++ b/CommonVariable.cpp	Mon Apr 07 15:19:37 2014 +0000
@@ -1,1 +1,14 @@
 #include "CommonVariable.h"
+
+const uint8_t MaxSpeed  = 140;
+
+bool EngineStat = 0;
+bool IsOverSpeed = 0;
+float Accelerometer_Value = 0;
+float Brake_Value = 0;
+float SpeedShow = 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	Wed Apr 02 09:13:57 2014 +0000
+++ b/CommonVariable.h	Mon Apr 07 15:19:37 2014 +0000
@@ -3,11 +3,31 @@
 #include "MCP23017.h"
 #include "WattBob_TextLCD.h"
 #include "mbed.h"
+#include "Servo.h"
 
+static Servo SpeedShow_Servo(p21);
 static MCP23017 Port(p9,p10,0x40) ;      // 16-bit object with I2C Chip MCP23017 
 static WattBob_TextLCD LCD(&Port);       // A 2*16 chacater LCD object
+static BusOut OverSpeedLED(LED1 , LED2 , LED3 , LED4);
+static DigitalIn EngineSwitch(p5);
+static DigitalOut L1(p27);
+static DigitalOut L2(p28);
+static DigitalOut L3(p29);
+static DigitalOut L4(p30);
+static AnalogIn Accelerometer(p19);
+static AnalogIn Brake(p20);
 
-static DigitalOut led2(LED2);
-static DigitalOut led1(LED1);
+extern const uint8_t MaxSpeed;
+
+extern bool EngineStat;
+extern bool IsOverSpeed;
+extern float Accelerometer_Value;
+extern float Brake_Value;
+extern float SpeedShow;
+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	Wed Apr 02 09:13:57 2014 +0000
+++ b/main.cpp	Mon Apr 07 15:19:37 2014 +0000
@@ -1,13 +1,25 @@
 #include "CAR.h"
- 
+
 int main() {
     Port.write_bit(1,BL_BIT);
     CAR CAR1;
-    void const *args;
-    Thread thread(CAR1.led2_thread);
-    
-    while (true) {
-        led1 = !led1;
-        Thread::wait(100);
+    Thread Accelero_Brake_Thread(CAR1.Accelero_Brake_Read);
+    Thread Average_Speed_Measure_Thread(CAR1.Average_Speed_Measure);
+    Thread Average_Speed_Show_Thread(CAR1.Average_Speed_Show);
+    Thread OverSpeed_Thread(CAR1.OverSpeed);
+    Thread Odometer_Measure_Thread(CAR1.Odometer_Measure);
+    Thread Odometer_Show_Thread(CAR1.Odometer_Show);
+    Thread SEND_CAR_VALUES_Thread(CAR1.SEND_CAR_VALUES);
+    Thread DUMP_CAR_VALUES_Thread(CAR1.DUMP_CAR_VALUES_En);
+    while(1){
+        if(IsOverSpeed){
+            OverSpeedLED = 0x6;
+            wait(0.2);
+            OverSpeedLED = 0x9;
+            wait(0.2) ;   
+        }
+        else{
+            OverSpeedLED = 0;
+        }     
     }
 }