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