![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All tasks complete
Dependencies: mbed MCP23017 mbed-rtos WattBob_TextLCD
assignment3tasks.cpp@6:abbd51d9b045, 2019-03-26 (annotated)
- Committer:
- aingks
- Date:
- Tue Mar 26 16:20:28 2019 +0000
- Revision:
- 6:abbd51d9b045
- Parent:
- 5:f5dda79b93cb
- Child:
- 7:87f7ba90fdce
task 5 complete ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aingks | 0:0943f040009c | 1 | #include "header.h" |
aingks | 3:f88d667629e6 | 2 | |
aingks | 2:9d6d07c9cc9c | 3 | |
aingks | 4:27340b291c09 | 4 | //definitions of tasks |
aingks | 4:27340b291c09 | 5 | //lock and unlock variables in struct when performing calculations |
aingks | 0:0943f040009c | 6 | |
aingks | 0:0943f040009c | 7 | //pedals |
aingks | 0:0943f040009c | 8 | AnalogIn brake_pedal(pin_analog_1); |
aingks | 0:0943f040009c | 9 | AnalogIn accel_pedal(pin_analog_2); |
aingks | 0:0943f040009c | 10 | |
aingks | 0:0943f040009c | 11 | |
aingks | 0:0943f040009c | 12 | //switches |
aingks | 0:0943f040009c | 13 | DigitalIn ngene(pin_digital_1); |
aingks | 1:c4f62ef9b5b7 | 14 | DigitalIn leftsw(pin_digital_2); |
aingks | 1:c4f62ef9b5b7 | 15 | DigitalIn rightsw(pin_digital_3); |
aingks | 1:c4f62ef9b5b7 | 16 | DigitalIn sidesw(pin_digital_4); |
aingks | 0:0943f040009c | 17 | |
aingks | 0:0943f040009c | 18 | |
aingks | 0:0943f040009c | 19 | //indicators |
aingks | 0:0943f040009c | 20 | DigitalOut ngeneind(pin_LED1); |
aingks | 4:27340b291c09 | 21 | PwmOut leftind(pin_LED2); //was digitalout |
aingks | 4:27340b291c09 | 22 | PwmOut rightind(pin_LED3);//was digitalout |
aingks | 0:0943f040009c | 23 | DigitalOut sideind(pin_LED4); |
aingks | 0:0943f040009c | 24 | |
aingks | 0:0943f040009c | 25 | |
aingks | 0:0943f040009c | 26 | //RED box lights |
aingks | 0:0943f040009c | 27 | DigitalOut brakeind(pin_LED5); |
aingks | 0:0943f040009c | 28 | DigitalOut overspeedind(pin_LED6); |
aingks | 0:0943f040009c | 29 | |
aingks | 4:27340b291c09 | 30 | |
aingks | 4:27340b291c09 | 31 | |
aingks | 4:27340b291c09 | 32 | // |
aingks | 4:27340b291c09 | 33 | extern SDATA speed; |
aingks | 4:27340b291c09 | 34 | extern CARINFO info; |
aingks | 4:27340b291c09 | 35 | extern SIMU_DATA sim; |
aingks | 4:27340b291c09 | 36 | |
aingks | 4:27340b291c09 | 37 | // LCD |
aingks | 4:27340b291c09 | 38 | |
aingks | 4:27340b291c09 | 39 | extern WattBob_TextLCD *display; |
aingks | 4:27340b291c09 | 40 | |
aingks | 4:27340b291c09 | 41 | // MUTEX |
aingks | 4:27340b291c09 | 42 | |
aingks | 4:27340b291c09 | 43 | extern Mutex simuXS; |
aingks | 4:27340b291c09 | 44 | extern Mutex speedXS; |
aingks | 4:27340b291c09 | 45 | extern Mutex carstateXS; |
aingks | 4:27340b291c09 | 46 | // |
aingks | 2:9d6d07c9cc9c | 47 | |
aingks | 0:0943f040009c | 48 | |
aingks | 0:0943f040009c | 49 | void task1readbrake() |
aingks | 6:abbd51d9b045 | 50 | { |
aingks | 6:abbd51d9b045 | 51 | float delay = 1000 / TASKFREQ1; |
aingks | 6:abbd51d9b045 | 52 | while(true){ |
aingks | 6:abbd51d9b045 | 53 | |
aingks | 4:27340b291c09 | 54 | speedXS.lock();//lock |
aingks | 6:abbd51d9b045 | 55 | |
aingks | 1:c4f62ef9b5b7 | 56 | speed.brakevalue = brake_pedal.read(); |
aingks | 6:abbd51d9b045 | 57 | |
aingks | 4:27340b291c09 | 58 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 59 | |
aingks | 6:abbd51d9b045 | 60 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 61 | |
aingks | 6:abbd51d9b045 | 62 | } |
aingks | 0:0943f040009c | 63 | } |
aingks | 0:0943f040009c | 64 | |
aingks | 0:0943f040009c | 65 | void task2readaccel() |
aingks | 0:0943f040009c | 66 | { |
aingks | 6:abbd51d9b045 | 67 | float delay = 1000 / TASKFREQ2; |
aingks | 6:abbd51d9b045 | 68 | while(true){ |
aingks | 6:abbd51d9b045 | 69 | |
aingks | 4:27340b291c09 | 70 | speedXS.lock();//lock |
aingks | 6:abbd51d9b045 | 71 | |
aingks | 1:c4f62ef9b5b7 | 72 | speed.accelvalue = accel_pedal.read(); |
aingks | 6:abbd51d9b045 | 73 | |
aingks | 4:27340b291c09 | 74 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 75 | |
aingks | 6:abbd51d9b045 | 76 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 77 | |
aingks | 6:abbd51d9b045 | 78 | } |
aingks | 6:abbd51d9b045 | 79 | |
aingks | 0:0943f040009c | 80 | } |
aingks | 0:0943f040009c | 81 | |
aingks | 0:0943f040009c | 82 | void task3enginestate() |
aingks | 6:abbd51d9b045 | 83 | { |
aingks | 6:abbd51d9b045 | 84 | float delay = 1000 / TASKFREQ3; |
aingks | 6:abbd51d9b045 | 85 | while(true){ |
aingks | 1:c4f62ef9b5b7 | 86 | //lock |
aingks | 4:27340b291c09 | 87 | carstateXS.lock(); |
aingks | 1:c4f62ef9b5b7 | 88 | uint8_t engineonoff = ngene.read(); |
aingks | 2:9d6d07c9cc9c | 89 | info.ENGINESTATE = engineonoff; |
aingks | 4:27340b291c09 | 90 | carstateXS.unlock(); |
aingks | 1:c4f62ef9b5b7 | 91 | //unlock |
aingks | 1:c4f62ef9b5b7 | 92 | |
aingks | 1:c4f62ef9b5b7 | 93 | if(engineonoff == 1){ |
aingks | 1:c4f62ef9b5b7 | 94 | ngeneind = 1; |
aingks | 1:c4f62ef9b5b7 | 95 | } |
aingks | 1:c4f62ef9b5b7 | 96 | else{ |
aingks | 1:c4f62ef9b5b7 | 97 | ngeneind = 0; |
aingks | 1:c4f62ef9b5b7 | 98 | } |
aingks | 6:abbd51d9b045 | 99 | |
aingks | 6:abbd51d9b045 | 100 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 101 | } |
aingks | 0:0943f040009c | 102 | } |
aingks | 0:0943f040009c | 103 | |
aingks | 0:0943f040009c | 104 | void task4average() |
aingks | 0:0943f040009c | 105 | { |
aingks | 6:abbd51d9b045 | 106 | float delay = 1000 / TASKFREQ4; |
aingks | 6:abbd51d9b045 | 107 | while(true){ |
aingks | 6:abbd51d9b045 | 108 | speedXS.lock();//lock |
aingks | 0:0943f040009c | 109 | |
aingks | 6:abbd51d9b045 | 110 | for(int i = 0; i < SAMPLESIZE ; i++) |
aingks | 5:f5dda79b93cb | 111 | //speed.raw = (speed.accelvalue - speed.brakevalue)*MAXSPEED; |
aingks | 5:f5dda79b93cb | 112 | speed.average += speed.array[i]; |
aingks | 6:abbd51d9b045 | 113 | speed.average = (speed.average / SAMPLESIZE); |
aingks | 6:abbd51d9b045 | 114 | |
aingks | 6:abbd51d9b045 | 115 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 116 | |
aingks | 6:abbd51d9b045 | 117 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 118 | } |
aingks | 6:abbd51d9b045 | 119 | } |
aingks | 6:abbd51d9b045 | 120 | |
aingks | 6:abbd51d9b045 | 121 | void task5brakeLED() |
aingks | 6:abbd51d9b045 | 122 | { |
aingks | 6:abbd51d9b045 | 123 | float delay = 1000 / TASKFREQ5; |
aingks | 6:abbd51d9b045 | 124 | while(true){ |
aingks | 5:f5dda79b93cb | 125 | |
aingks | 6:abbd51d9b045 | 126 | //lock |
aingks | 6:abbd51d9b045 | 127 | speedXS.lock(); |
aingks | 6:abbd51d9b045 | 128 | float braketest = speed.brakevalue; |
aingks | 6:abbd51d9b045 | 129 | |
aingks | 6:abbd51d9b045 | 130 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 131 | |
aingks | 6:abbd51d9b045 | 132 | if( braketest > 0.2) // speed increase |
aingks | 6:abbd51d9b045 | 133 | brakeind = 1; |
aingks | 6:abbd51d9b045 | 134 | else |
aingks | 6:abbd51d9b045 | 135 | brakeind = 0; |
aingks | 5:f5dda79b93cb | 136 | |
aingks | 6:abbd51d9b045 | 137 | Thread::wait((int)delay); |
aingks | 6:abbd51d9b045 | 138 | } |
aingks | 0:0943f040009c | 139 | } |