![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
Diff: CAR.cpp
- Revision:
- 13:e5b22bfbe67b
- Parent:
- 12:8eb2c1cccee6
--- 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