![](/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:
- 12:8eb2c1cccee6
- Parent:
- 11:7f2414ecb7ee
- Child:
- 13:e5b22bfbe67b
diff -r 7f2414ecb7ee -r 8eb2c1cccee6 CAR.cpp --- a/CAR.cpp Tue May 06 09:11:01 2014 +0000 +++ b/CAR.cpp Tue May 06 09:49:08 2014 +0000 @@ -2,7 +2,8 @@ //Define semaphores to manage accessing the viriables Semaphore CAR_MAIL_SEM(1); -Semaphore INPUT_SEM(3); +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 @@ -37,28 +38,34 @@ /* Read the accelerometer and brake value every 100 mSec and calculate the speed. Also save them in variabls.The speed is saved in an array of three element so the past three values of speed is saved. */ -void CAR::Accelero_Brake_Read(void const *args){ +void CAR::Car_Simulation(void const *args){ while (true) { INPUT_SEM.wait(); Accelerometer_Value = Accelerometer.read(); Brake_Value = Brake.read(); EngineStat = EngineSwitch; - Speed[Counter] = Accelerometer_Value * MaxSpeed * (1 - Brake_Value) * EngineStat ; + Speed_SEM.wait(); + Speed[Counter] = Get_Speed(); + Speed_SEM.release(); Counter++; if(Counter > 2) Counter = 0; + INPUT_SEM.release(); Thread::wait(100); } } +uint8_t CAR::Get_Speed(){ + return (Accelerometer_Value * MaxSpeed * (1 - Brake_Value) * EngineStat) ; +} // Average filter the speed every 200 mSec void CAR::Average_Speed_Measure(void const *args) { while (true) { - INPUT_SEM.wait(); + Speed_SEM.wait(); AVR_SPEED_SEM.wait(); Average_Speed = ( Speed[0] + Speed[1] + Speed[2] )/3 ; - INPUT_SEM.release(); AVR_SPEED_SEM.release(); + Speed_SEM.release(); Thread::wait(200); } }