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

CAR.cpp

Committer:
muaiyd
Date:
2014-04-09
Revision:
8:6e55db96c11c
Parent:
7:a92da438d06c
Child:
9:d86a6b8cdfa4

File content as of revision 8:6e55db96c11c:

#include "CAR.h"
Semaphore CAR_MAIL_SEM(1);
Semaphore INPUT_SEM(2);
Semaphore SWITCH_SEM(3);
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;
bool LSide_Indicator_Value;
bool RSide_Indicator_Value;
 
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) {
        INPUT_SEM.wait();
        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;
        INPUT_SEM.release();
        Thread::wait(100);
    }
}

void CAR::Average_Speed_Measure(void const *args) {
    while (true) {
        INPUT_SEM.wait();
        Average_Speed = ( Speed[0] + Speed[1] + Speed[2] )/3 ;
        INPUT_SEM.release();
        Thread::wait(200);
    }
}

void CAR::Average_Speed_Show(void const *args){
    while(1){
        INPUT_SEM.wait();
        SpeedShow_Servo = 1.0 - (float)Average_Speed / (float)MaxSpeed ;
        INPUT_SEM.release();
        Thread::wait(1000);
    }
}
void CAR::OverSpeed(void const *args){
    while(true){
    INPUT_SEM.wait();
    if(Average_Speed > 70)
            IsOverSpeed = 1;
            
        else
            IsOverSpeed = 0;
    INPUT_SEM.release();        
    Thread::wait(2000);
    }       
}

void CAR::Odo_Show_Indicator_Switch_Read(void const *args){
    while(true){
        LCD.locate(0,0);
        INPUT_SEM.wait();
        Odometer_Value = GET_ODO() + Average_Speed / 7200.0 ;
        SAVE_ODO(Odometer_Value);
        LCD.printf("AvrgSpd %3D MPH",Average_Speed);
        LCD.locate(1,0);
        LCD.printf("OdoVlu %4.2f M",GET_ODO());
        INPUT_SEM.release();
        SWITCH_SEM.wait();
        LSide_Indicator_Value = LSide_Indicator_Switch;
        RSide_Indicator_Value = RSide_Indicator_Switch;
        SWITCH_SEM.release();
        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++;
        CAR_MAIL_SEM.release();
        Thread::wait(5000);              
    }
}
void CAR::DUMP_CAR_VALUES_En (void const *args) {
    while (true) {
        CAR_MAIL_SEM.wait();
        while (Element_Counter_W > Element_Counter_R)
        DUMP_CAR_VALUES();
        CAR_MAIL_SEM.release();
        Thread::wait(20000);
    }
}
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++;        
    }
}
void CAR::Side_Light_Flash(void const *args){
    while(true){
        SWITCH_SEM.wait();
        if((LSide_Indicator_Value == 1) && (RSide_Indicator_Value == 1) ){
            L_Side_Indicator = L_Side_Indicator ^ 1;
            R_Side_Indicator = L_Side_Indicator;
        }
        else if(RSide_Indicator_Value == 1){
            R_Side_Indicator = R_Side_Indicator ^ 1;
            L_Side_Indicator = 0;
        }
        else if(LSide_Indicator_Value == 1){
            L_Side_Indicator = L_Side_Indicator ^ 1;
            R_Side_Indicator = 0;
        }
        else{
            L_Side_Indicator = 0;
            R_Side_Indicator = 0 ;
        }
        SWITCH_SEM.release();
        Thread::wait(1000);
    }
}
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;
        }
        Thread::wait(1000);
    }
}