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

Committer:
muaiyd
Date:
Mon Apr 07 15:47:56 2014 +0000
Revision:
5:c20ab22e8431
Parent:
4:952a2d814fb1
It needs to be global

Who changed what in which revision?

UserRevisionLine numberNew contents of line
muaiyd 0:68ce46607848 1 #include "CAR.h"
muaiyd 1:b409ad65466a 2 Semaphore CAR_MAIL_SEM(1);
muaiyd 4:952a2d814fb1 3
muaiyd 0:68ce46607848 4
muaiyd 1:b409ad65466a 5
muaiyd 1:b409ad65466a 6 CAR::CAR(){
muaiyd 1:b409ad65466a 7 Port.write_bit(1,BL_BIT);
muaiyd 4:952a2d814fb1 8 Element_Counter_W = 0;
muaiyd 4:952a2d814fb1 9 Element_Counter_R = 0;
muaiyd 1:b409ad65466a 10 }
muaiyd 1:b409ad65466a 11 void CAR::SAVE_ODO(float value) {
muaiyd 1:b409ad65466a 12 LPC_RTC->GPREG0 = *((uint32_t*)&value);
muaiyd 1:b409ad65466a 13 }
muaiyd 1:b409ad65466a 14
muaiyd 1:b409ad65466a 15 float CAR::GET_ODO() {
muaiyd 1:b409ad65466a 16 return *((float*)&(LPC_RTC->GPREG0));
muaiyd 1:b409ad65466a 17 }
muaiyd 1:b409ad65466a 18 void CAR::Accelero_Brake_Read(void const *args){
muaiyd 0:68ce46607848 19 while (true) {
muaiyd 1:b409ad65466a 20 Accelerometer_Value = Accelerometer.read();
muaiyd 1:b409ad65466a 21 Brake_Value = Brake.read();
muaiyd 1:b409ad65466a 22 EngineStat = EngineSwitch;
muaiyd 1:b409ad65466a 23 Speed[Counter] = Accelerometer_Value * MaxSpeed * (1 - Brake_Value) * EngineStat ;
muaiyd 1:b409ad65466a 24 Counter++;
muaiyd 1:b409ad65466a 25 if(Counter > 2) Counter = 0;
muaiyd 1:b409ad65466a 26 Thread::wait(100);
muaiyd 1:b409ad65466a 27 }
muaiyd 1:b409ad65466a 28 }
muaiyd 1:b409ad65466a 29
muaiyd 1:b409ad65466a 30 void CAR::Average_Speed_Measure(void const *args) {
muaiyd 1:b409ad65466a 31 while (true) {
muaiyd 1:b409ad65466a 32 Average_Speed = ( Speed[0] + Speed[1] + Speed[2] )/3 ;
muaiyd 1:b409ad65466a 33 Thread::wait(200);
muaiyd 1:b409ad65466a 34 }
muaiyd 1:b409ad65466a 35 }
muaiyd 1:b409ad65466a 36
muaiyd 1:b409ad65466a 37 void CAR::Average_Speed_Show(void const *args){
muaiyd 1:b409ad65466a 38 while(1){
muaiyd 1:b409ad65466a 39 SpeedShow = 1.0 - (float)Average_Speed / (float)MaxSpeed ;
muaiyd 1:b409ad65466a 40 SpeedShow_Servo = SpeedShow ;
muaiyd 0:68ce46607848 41 Thread::wait(1000);
muaiyd 0:68ce46607848 42 }
muaiyd 1:b409ad65466a 43 }
muaiyd 1:b409ad65466a 44 void CAR::OverSpeed(void const *args){
muaiyd 1:b409ad65466a 45 while(true){
muaiyd 1:b409ad65466a 46 if(Average_Speed > 70)
muaiyd 1:b409ad65466a 47 IsOverSpeed = 1;
muaiyd 1:b409ad65466a 48
muaiyd 1:b409ad65466a 49 else
muaiyd 1:b409ad65466a 50 IsOverSpeed = 0;
muaiyd 1:b409ad65466a 51
muaiyd 1:b409ad65466a 52 Thread::wait(2000);
muaiyd 1:b409ad65466a 53 }
muaiyd 1:b409ad65466a 54 }
muaiyd 1:b409ad65466a 55
muaiyd 1:b409ad65466a 56 void CAR::Odometer_Measure(void const *args){
muaiyd 1:b409ad65466a 57 while(true){
muaiyd 1:b409ad65466a 58 Odometer_Value = GET_ODO();
muaiyd 1:b409ad65466a 59 Odometer_Value = Odometer_Value + Average_Speed / 7200.0 ;
muaiyd 1:b409ad65466a 60 SAVE_ODO(Odometer_Value);
muaiyd 1:b409ad65466a 61 Thread::wait(500);
muaiyd 1:b409ad65466a 62 }
muaiyd 1:b409ad65466a 63 }
muaiyd 1:b409ad65466a 64 void CAR::Odometer_Show(void const *args){
muaiyd 1:b409ad65466a 65 while(true){
muaiyd 1:b409ad65466a 66 LCD.locate(0,0);
muaiyd 1:b409ad65466a 67 LCD.printf("AvrgSpd %3D MPH",Average_Speed);
muaiyd 1:b409ad65466a 68 LCD.locate(1,0);
muaiyd 1:b409ad65466a 69 LCD.printf("OdoVlu %4.2f M",Odometer_Value);
muaiyd 1:b409ad65466a 70 Thread::wait(500);
muaiyd 1:b409ad65466a 71 }
muaiyd 1:b409ad65466a 72 }
muaiyd 1:b409ad65466a 73 void CAR::SEND_CAR_VALUES (void const *args) {
muaiyd 1:b409ad65466a 74
muaiyd 1:b409ad65466a 75 while (true) {
muaiyd 1:b409ad65466a 76 CAR_MAIL *mail = mail_box.alloc();
muaiyd 1:b409ad65466a 77 CAR_MAIL_SEM.wait();
muaiyd 1:b409ad65466a 78 mail->Mail_Average_Speed = Average_Speed;
muaiyd 1:b409ad65466a 79 mail->Mail_Accelerometer_Value = Accelerometer_Value;
muaiyd 1:b409ad65466a 80 mail->Mail_Brake_Value = Brake_Value;
muaiyd 5:c20ab22e8431 81 //mail->Counter = Element_Counter_W ;
muaiyd 1:b409ad65466a 82 mail_box.put(mail);
muaiyd 5:c20ab22e8431 83 // printf("\n %i £ \n\r",Element_Counter_W);
muaiyd 5:c20ab22e8431 84 //Element_Counter_W++;
muaiyd 4:952a2d814fb1 85 Thread::wait(5000);
muaiyd 1:b409ad65466a 86 CAR_MAIL_SEM.release();
muaiyd 1:b409ad65466a 87 }
muaiyd 1:b409ad65466a 88 }
muaiyd 1:b409ad65466a 89 void CAR::DUMP_CAR_VALUES_En (void const *args) {
muaiyd 1:b409ad65466a 90 while (true) {
muaiyd 1:b409ad65466a 91 L1 = L1 ^ 1;
muaiyd 1:b409ad65466a 92 CAR_MAIL_SEM.wait();
muaiyd 1:b409ad65466a 93 while (Element_Counter_W > Element_Counter_R)
muaiyd 1:b409ad65466a 94 DUMP_CAR_VALUES();
muaiyd 1:b409ad65466a 95 CAR_MAIL_SEM.release();
muaiyd 4:952a2d814fb1 96 Thread::wait(20000);
muaiyd 1:b409ad65466a 97 }
muaiyd 1:b409ad65466a 98 }
muaiyd 1:b409ad65466a 99 void CAR::DUMP_CAR_VALUES () {
muaiyd 1:b409ad65466a 100 printf("Writing \n\r");
muaiyd 1:b409ad65466a 101 osEvent evt = mail_box.get(10);
muaiyd 1:b409ad65466a 102 CAR_MAIL *mail = (CAR_MAIL*)evt.value.p;
muaiyd 1:b409ad65466a 103 if (evt.status == osEventMail) {
muaiyd 1:b409ad65466a 104 printf("\nAverage_Speed: %i MPH\n\r" , mail->Mail_Average_Speed);
muaiyd 1:b409ad65466a 105 printf("Accelerometer_Value: %.3f %\n\r" , mail->Mail_Accelerometer_Value);
muaiyd 1:b409ad65466a 106 printf("Brake_Value: %.3f %\n\r", mail->Mail_Brake_Value);
muaiyd 1:b409ad65466a 107 printf("\nCounter: %i \n\r" , mail->Counter);
muaiyd 1:b409ad65466a 108 mail_box.free(mail);
muaiyd 1:b409ad65466a 109 Element_Counter_R++;
muaiyd 1:b409ad65466a 110 }
muaiyd 0:68ce46607848 111 }