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:
Wed Apr 09 09:41:30 2014 +0000
Revision:
8:6e55db96c11c
Parent:
7:a92da438d06c
Child:
9:d86a6b8cdfa4
Finished all the tasks except the saving in file

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 7:a92da438d06c 3 Semaphore INPUT_SEM(2);
muaiyd 7:a92da438d06c 4 Semaphore SWITCH_SEM(3);
muaiyd 1:b409ad65466a 5 uint32_t Element_Counter_W = 0;
muaiyd 1:b409ad65466a 6 uint32_t Element_Counter_R = 0;
muaiyd 1:b409ad65466a 7 typedef struct {
muaiyd 1:b409ad65466a 8 uint8_t Mail_Average_Speed;
muaiyd 1:b409ad65466a 9 float Mail_Accelerometer_Value;
muaiyd 1:b409ad65466a 10 float Mail_Brake_Value;
muaiyd 1:b409ad65466a 11 int Counter;
muaiyd 1:b409ad65466a 12 } CAR_MAIL;
muaiyd 1:b409ad65466a 13 static Mail<CAR_MAIL, 100> mail_box;
muaiyd 7:a92da438d06c 14 bool LSide_Indicator_Value;
muaiyd 7:a92da438d06c 15 bool RSide_Indicator_Value;
muaiyd 1:b409ad65466a 16
muaiyd 1:b409ad65466a 17 CAR::CAR(){
muaiyd 1:b409ad65466a 18 Port.write_bit(1,BL_BIT);
muaiyd 1:b409ad65466a 19
muaiyd 1:b409ad65466a 20
muaiyd 1:b409ad65466a 21 }
muaiyd 1:b409ad65466a 22 void CAR::SAVE_ODO(float value) {
muaiyd 1:b409ad65466a 23 LPC_RTC->GPREG0 = *((uint32_t*)&value);
muaiyd 1:b409ad65466a 24 }
muaiyd 1:b409ad65466a 25
muaiyd 1:b409ad65466a 26 float CAR::GET_ODO() {
muaiyd 1:b409ad65466a 27 return *((float*)&(LPC_RTC->GPREG0));
muaiyd 1:b409ad65466a 28 }
muaiyd 1:b409ad65466a 29 void CAR::Accelero_Brake_Read(void const *args){
muaiyd 0:68ce46607848 30 while (true) {
muaiyd 7:a92da438d06c 31 INPUT_SEM.wait();
muaiyd 1:b409ad65466a 32 Accelerometer_Value = Accelerometer.read();
muaiyd 1:b409ad65466a 33 Brake_Value = Brake.read();
muaiyd 1:b409ad65466a 34 EngineStat = EngineSwitch;
muaiyd 1:b409ad65466a 35 Speed[Counter] = Accelerometer_Value * MaxSpeed * (1 - Brake_Value) * EngineStat ;
muaiyd 1:b409ad65466a 36 Counter++;
muaiyd 1:b409ad65466a 37 if(Counter > 2) Counter = 0;
muaiyd 7:a92da438d06c 38 INPUT_SEM.release();
muaiyd 1:b409ad65466a 39 Thread::wait(100);
muaiyd 1:b409ad65466a 40 }
muaiyd 1:b409ad65466a 41 }
muaiyd 1:b409ad65466a 42
muaiyd 1:b409ad65466a 43 void CAR::Average_Speed_Measure(void const *args) {
muaiyd 1:b409ad65466a 44 while (true) {
muaiyd 7:a92da438d06c 45 INPUT_SEM.wait();
muaiyd 1:b409ad65466a 46 Average_Speed = ( Speed[0] + Speed[1] + Speed[2] )/3 ;
muaiyd 7:a92da438d06c 47 INPUT_SEM.release();
muaiyd 1:b409ad65466a 48 Thread::wait(200);
muaiyd 1:b409ad65466a 49 }
muaiyd 1:b409ad65466a 50 }
muaiyd 1:b409ad65466a 51
muaiyd 1:b409ad65466a 52 void CAR::Average_Speed_Show(void const *args){
muaiyd 1:b409ad65466a 53 while(1){
muaiyd 7:a92da438d06c 54 INPUT_SEM.wait();
muaiyd 7:a92da438d06c 55 SpeedShow_Servo = 1.0 - (float)Average_Speed / (float)MaxSpeed ;
muaiyd 7:a92da438d06c 56 INPUT_SEM.release();
muaiyd 0:68ce46607848 57 Thread::wait(1000);
muaiyd 0:68ce46607848 58 }
muaiyd 1:b409ad65466a 59 }
muaiyd 1:b409ad65466a 60 void CAR::OverSpeed(void const *args){
muaiyd 1:b409ad65466a 61 while(true){
muaiyd 7:a92da438d06c 62 INPUT_SEM.wait();
muaiyd 1:b409ad65466a 63 if(Average_Speed > 70)
muaiyd 1:b409ad65466a 64 IsOverSpeed = 1;
muaiyd 1:b409ad65466a 65
muaiyd 1:b409ad65466a 66 else
muaiyd 1:b409ad65466a 67 IsOverSpeed = 0;
muaiyd 7:a92da438d06c 68 INPUT_SEM.release();
muaiyd 7:a92da438d06c 69 Thread::wait(2000);
muaiyd 1:b409ad65466a 70 }
muaiyd 1:b409ad65466a 71 }
muaiyd 1:b409ad65466a 72
muaiyd 7:a92da438d06c 73 void CAR::Odo_Show_Indicator_Switch_Read(void const *args){
muaiyd 1:b409ad65466a 74 while(true){
muaiyd 1:b409ad65466a 75 LCD.locate(0,0);
muaiyd 7:a92da438d06c 76 INPUT_SEM.wait();
muaiyd 7:a92da438d06c 77 Odometer_Value = GET_ODO() + Average_Speed / 7200.0 ;
muaiyd 7:a92da438d06c 78 SAVE_ODO(Odometer_Value);
muaiyd 1:b409ad65466a 79 LCD.printf("AvrgSpd %3D MPH",Average_Speed);
muaiyd 1:b409ad65466a 80 LCD.locate(1,0);
muaiyd 7:a92da438d06c 81 LCD.printf("OdoVlu %4.2f M",GET_ODO());
muaiyd 7:a92da438d06c 82 INPUT_SEM.release();
muaiyd 7:a92da438d06c 83 SWITCH_SEM.wait();
muaiyd 7:a92da438d06c 84 LSide_Indicator_Value = LSide_Indicator_Switch;
muaiyd 7:a92da438d06c 85 RSide_Indicator_Value = RSide_Indicator_Switch;
muaiyd 7:a92da438d06c 86 SWITCH_SEM.release();
muaiyd 1:b409ad65466a 87 Thread::wait(500);
muaiyd 1:b409ad65466a 88 }
muaiyd 1:b409ad65466a 89 }
muaiyd 1:b409ad65466a 90 void CAR::SEND_CAR_VALUES (void const *args) {
muaiyd 1:b409ad65466a 91
muaiyd 1:b409ad65466a 92 while (true) {
muaiyd 1:b409ad65466a 93 CAR_MAIL *mail = mail_box.alloc();
muaiyd 1:b409ad65466a 94 CAR_MAIL_SEM.wait();
muaiyd 1:b409ad65466a 95 mail->Mail_Average_Speed = Average_Speed;
muaiyd 1:b409ad65466a 96 mail->Mail_Accelerometer_Value = Accelerometer_Value;
muaiyd 1:b409ad65466a 97 mail->Mail_Brake_Value = Brake_Value;
muaiyd 1:b409ad65466a 98 mail->Counter = Element_Counter_W ;
muaiyd 1:b409ad65466a 99 mail_box.put(mail);
muaiyd 1:b409ad65466a 100 printf("\n %i £ \n\r",Element_Counter_W);
muaiyd 1:b409ad65466a 101 Element_Counter_W++;
muaiyd 7:a92da438d06c 102 CAR_MAIL_SEM.release();
muaiyd 7:a92da438d06c 103 Thread::wait(5000);
muaiyd 1:b409ad65466a 104 }
muaiyd 1:b409ad65466a 105 }
muaiyd 1:b409ad65466a 106 void CAR::DUMP_CAR_VALUES_En (void const *args) {
muaiyd 1:b409ad65466a 107 while (true) {
muaiyd 1:b409ad65466a 108 CAR_MAIL_SEM.wait();
muaiyd 1:b409ad65466a 109 while (Element_Counter_W > Element_Counter_R)
muaiyd 7:a92da438d06c 110 DUMP_CAR_VALUES();
muaiyd 1:b409ad65466a 111 CAR_MAIL_SEM.release();
muaiyd 6:5037779f6a55 112 Thread::wait(20000);
muaiyd 1:b409ad65466a 113 }
muaiyd 1:b409ad65466a 114 }
muaiyd 1:b409ad65466a 115 void CAR::DUMP_CAR_VALUES () {
muaiyd 1:b409ad65466a 116 printf("Writing \n\r");
muaiyd 1:b409ad65466a 117 osEvent evt = mail_box.get(10);
muaiyd 1:b409ad65466a 118 CAR_MAIL *mail = (CAR_MAIL*)evt.value.p;
muaiyd 1:b409ad65466a 119 if (evt.status == osEventMail) {
muaiyd 1:b409ad65466a 120 printf("\nAverage_Speed: %i MPH\n\r" , mail->Mail_Average_Speed);
muaiyd 1:b409ad65466a 121 printf("Accelerometer_Value: %.3f %\n\r" , mail->Mail_Accelerometer_Value);
muaiyd 1:b409ad65466a 122 printf("Brake_Value: %.3f %\n\r", mail->Mail_Brake_Value);
muaiyd 1:b409ad65466a 123 printf("\nCounter: %i \n\r" , mail->Counter);
muaiyd 1:b409ad65466a 124 mail_box.free(mail);
muaiyd 1:b409ad65466a 125 Element_Counter_R++;
muaiyd 1:b409ad65466a 126 }
muaiyd 7:a92da438d06c 127 }
muaiyd 7:a92da438d06c 128 void CAR::Side_Light_Flash(void const *args){
muaiyd 7:a92da438d06c 129 while(true){
muaiyd 7:a92da438d06c 130 SWITCH_SEM.wait();
muaiyd 8:6e55db96c11c 131 if((LSide_Indicator_Value == 1) && (RSide_Indicator_Value == 1) ){
muaiyd 8:6e55db96c11c 132 L_Side_Indicator = L_Side_Indicator ^ 1;
muaiyd 8:6e55db96c11c 133 R_Side_Indicator = L_Side_Indicator;
muaiyd 8:6e55db96c11c 134 }
muaiyd 8:6e55db96c11c 135 else if(RSide_Indicator_Value == 1){
muaiyd 8:6e55db96c11c 136 R_Side_Indicator = R_Side_Indicator ^ 1;
muaiyd 8:6e55db96c11c 137 L_Side_Indicator = 0;
muaiyd 8:6e55db96c11c 138 }
muaiyd 8:6e55db96c11c 139 else if(LSide_Indicator_Value == 1){
muaiyd 8:6e55db96c11c 140 L_Side_Indicator = L_Side_Indicator ^ 1;
muaiyd 8:6e55db96c11c 141 R_Side_Indicator = 0;
muaiyd 8:6e55db96c11c 142 }
muaiyd 8:6e55db96c11c 143 else{
muaiyd 8:6e55db96c11c 144 L_Side_Indicator = 0;
muaiyd 8:6e55db96c11c 145 R_Side_Indicator = 0 ;
muaiyd 8:6e55db96c11c 146 }
muaiyd 8:6e55db96c11c 147 SWITCH_SEM.release();
muaiyd 8:6e55db96c11c 148 Thread::wait(1000);
muaiyd 8:6e55db96c11c 149 }
muaiyd 8:6e55db96c11c 150 }
muaiyd 8:6e55db96c11c 151 void CAR::Side_Light(void const *args){
muaiyd 8:6e55db96c11c 152 while(true){
muaiyd 8:6e55db96c11c 153 if(LSide_Light_Switch){
muaiyd 8:6e55db96c11c 154 L_Side_Light = 1;
muaiyd 7:a92da438d06c 155 }
muaiyd 7:a92da438d06c 156 else{
muaiyd 7:a92da438d06c 157 L_Side_Light = 0;
muaiyd 7:a92da438d06c 158 }
muaiyd 8:6e55db96c11c 159 if(RSide_Light_Switch){
muaiyd 8:6e55db96c11c 160 R_Side_Light = 1;
muaiyd 7:a92da438d06c 161 }
muaiyd 7:a92da438d06c 162 else{
muaiyd 8:6e55db96c11c 163 R_Side_Light = 0;
muaiyd 7:a92da438d06c 164 }
muaiyd 7:a92da438d06c 165 Thread::wait(1000);
muaiyd 7:a92da438d06c 166 }
muaiyd 0:68ce46607848 167 }