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 11:05:32 2014 +0000
Revision:
9:d86a6b8cdfa4
Parent:
8:6e55db96c11c
Child:
10:2522e3878e1c
Optimization and comment;

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