![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All tasks complete
Dependencies: mbed MCP23017 mbed-rtos WattBob_TextLCD
assignment3tasks.cpp@7:87f7ba90fdce, 2019-03-26 (annotated)
- Committer:
- aingks
- Date:
- Tue Mar 26 16:48:30 2019 +0000
- Revision:
- 7:87f7ba90fdce
- Parent:
- 6:abbd51d9b045
- Child:
- 8:3f3f2c2e2046
LED disp module added ;
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 | 7:87f7ba90fdce | 111 | //speed.rawspeed = (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 | 7:87f7ba90fdce | 139 | } |
aingks | 7:87f7ba90fdce | 140 | |
aingks | 7:87f7ba90fdce | 141 | |
aingks | 7:87f7ba90fdce | 142 | void task6speedmonitor() |
aingks | 7:87f7ba90fdce | 143 | { |
aingks | 7:87f7ba90fdce | 144 | float delay = 1000 / TASKFREQ6; |
aingks | 7:87f7ba90fdce | 145 | while(true){ |
aingks | 7:87f7ba90fdce | 146 | |
aingks | 7:87f7ba90fdce | 147 | carstateXS.lock(); //lock carstate |
aingks | 7:87f7ba90fdce | 148 | speedXS.lock(); //lock speed |
aingks | 7:87f7ba90fdce | 149 | |
aingks | 7:87f7ba90fdce | 150 | |
aingks | 7:87f7ba90fdce | 151 | info.odometer += (speed.rawspeed / TASKFREQ6); |
aingks | 7:87f7ba90fdce | 152 | uint8_t temp_enginestate = info.ENGINESTATE; |
aingks | 7:87f7ba90fdce | 153 | |
aingks | 7:87f7ba90fdce | 154 | carstateXS.unlock(); //unlock carstate |
aingks | 7:87f7ba90fdce | 155 | |
aingks | 7:87f7ba90fdce | 156 | float temp_accel = 0.0; |
aingks | 7:87f7ba90fdce | 157 | if(temp_enginestate != 0) |
aingks | 7:87f7ba90fdce | 158 | temp_accel = speed.accelvalue; |
aingks | 7:87f7ba90fdce | 159 | |
aingks | 7:87f7ba90fdce | 160 | //update speed |
aingks | 7:87f7ba90fdce | 161 | speed.rawspeed += (temp_accel - speed.brakevalue) * MAXSPEED; |
aingks | 7:87f7ba90fdce | 162 | |
aingks | 7:87f7ba90fdce | 163 | if(speed.rawspeed < 0) |
aingks | 7:87f7ba90fdce | 164 | speed.rawspeed = 0; |
aingks | 7:87f7ba90fdce | 165 | if(speed.rawspeed > MAXSPEED) |
aingks | 7:87f7ba90fdce | 166 | speed.rawspeed = MAXSPEED; |
aingks | 7:87f7ba90fdce | 167 | |
aingks | 7:87f7ba90fdce | 168 | // Saving the new speed |
aingks | 7:87f7ba90fdce | 169 | for (int i = 0; i < SAMPLESIZE - 1; i++) |
aingks | 7:87f7ba90fdce | 170 | |
aingks | 7:87f7ba90fdce | 171 | speed.array[i]= speed.array[i+1]; |
aingks | 7:87f7ba90fdce | 172 | speed.array[4]= speed.rawspeed; |
aingks | 7:87f7ba90fdce | 173 | |
aingks | 7:87f7ba90fdce | 174 | float temp = speed.rawspeed; |
aingks | 7:87f7ba90fdce | 175 | |
aingks | 7:87f7ba90fdce | 176 | // UNLOCK Speed |
aingks | 7:87f7ba90fdce | 177 | speedXS.unlock(); |
aingks | 7:87f7ba90fdce | 178 | |
aingks | 7:87f7ba90fdce | 179 | // Update Overspeed display |
aingks | 7:87f7ba90fdce | 180 | |
aingks | 7:87f7ba90fdce | 181 | if (temp > SPEEDLIMIT) |
aingks | 7:87f7ba90fdce | 182 | overspeedind = 1; |
aingks | 7:87f7ba90fdce | 183 | else |
aingks | 7:87f7ba90fdce | 184 | overspeedind = 0; |
aingks | 7:87f7ba90fdce | 185 | |
aingks | 7:87f7ba90fdce | 186 | Thread::wait((int)delay); |
aingks | 7:87f7ba90fdce | 187 | } |
aingks | 7:87f7ba90fdce | 188 | } |
aingks | 7:87f7ba90fdce | 189 | |
aingks | 7:87f7ba90fdce | 190 | void task7displayvalues() |
aingks | 7:87f7ba90fdce | 191 | { |
aingks | 7:87f7ba90fdce | 192 | float delay = 1000 / TASKFREQ7; |
aingks | 7:87f7ba90fdce | 193 | while(true){ |
aingks | 7:87f7ba90fdce | 194 | display->cls(); |
aingks | 7:87f7ba90fdce | 195 | |
aingks | 7:87f7ba90fdce | 196 | //LOCK data |
aingks | 7:87f7ba90fdce | 197 | carstateXS.lock(); |
aingks | 7:87f7ba90fdce | 198 | //LOCK Speed |
aingks | 7:87f7ba90fdce | 199 | speedXS.lock(); |
aingks | 7:87f7ba90fdce | 200 | |
aingks | 7:87f7ba90fdce | 201 | display->locate(1,0); |
aingks | 7:87f7ba90fdce | 202 | display->printf("S_AVER: %.2f",speed.average); |
aingks | 7:87f7ba90fdce | 203 | display->locate(0,0); |
aingks | 7:87f7ba90fdce | 204 | display->printf("ODOM: %d",(int) info.odometer); |
aingks | 7:87f7ba90fdce | 205 | |
aingks | 7:87f7ba90fdce | 206 | //UNLOCK data |
aingks | 7:87f7ba90fdce | 207 | carstateXS.unlock(); |
aingks | 7:87f7ba90fdce | 208 | //UNLOCK Speed |
aingks | 7:87f7ba90fdce | 209 | speedXS.unlock(); |
aingks | 7:87f7ba90fdce | 210 | |
aingks | 7:87f7ba90fdce | 211 | Thread::wait((int)delay); |
aingks | 7:87f7ba90fdce | 212 | |
aingks | 7:87f7ba90fdce | 213 | |
aingks | 7:87f7ba90fdce | 214 | } |
aingks | 0:0943f040009c | 215 | } |