![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All tasks complete
Dependencies: mbed MCP23017 mbed-rtos WattBob_TextLCD
assignment3tasks.cpp@9:2fd97246b8f0, 2019-03-27 (annotated)
- Committer:
- aingks
- Date:
- Wed Mar 27 12:28:10 2019 +0000
- Revision:
- 9:2fd97246b8f0
- Parent:
- 8:3f3f2c2e2046
- Child:
- 10:2b262d810c67
added indicator check module, not compiling ;
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 | 8:3f3f2c2e2046 | 84 | float delay = 125; |
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 | 8:3f3f2c2e2046 | 100 | Thread::wait(125); |
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 | 8:3f3f2c2e2046 | 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 | 8:3f3f2c2e2046 | 113 | } |
aingks | 6:abbd51d9b045 | 114 | speed.average = (speed.average / SAMPLESIZE); |
aingks | 6:abbd51d9b045 | 115 | |
aingks | 6:abbd51d9b045 | 116 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 117 | |
aingks | 6:abbd51d9b045 | 118 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 119 | } |
aingks | 6:abbd51d9b045 | 120 | } |
aingks | 6:abbd51d9b045 | 121 | |
aingks | 6:abbd51d9b045 | 122 | void task5brakeLED() |
aingks | 6:abbd51d9b045 | 123 | { |
aingks | 6:abbd51d9b045 | 124 | float delay = 1000 / TASKFREQ5; |
aingks | 6:abbd51d9b045 | 125 | while(true){ |
aingks | 5:f5dda79b93cb | 126 | |
aingks | 6:abbd51d9b045 | 127 | //lock |
aingks | 6:abbd51d9b045 | 128 | speedXS.lock(); |
aingks | 6:abbd51d9b045 | 129 | float braketest = speed.brakevalue; |
aingks | 6:abbd51d9b045 | 130 | |
aingks | 6:abbd51d9b045 | 131 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 132 | |
aingks | 6:abbd51d9b045 | 133 | if( braketest > 0.2) // speed increase |
aingks | 6:abbd51d9b045 | 134 | brakeind = 1; |
aingks | 6:abbd51d9b045 | 135 | else |
aingks | 6:abbd51d9b045 | 136 | brakeind = 0; |
aingks | 5:f5dda79b93cb | 137 | |
aingks | 6:abbd51d9b045 | 138 | Thread::wait((int)delay); |
aingks | 6:abbd51d9b045 | 139 | } |
aingks | 7:87f7ba90fdce | 140 | } |
aingks | 7:87f7ba90fdce | 141 | |
aingks | 7:87f7ba90fdce | 142 | |
aingks | 7:87f7ba90fdce | 143 | void task6speedmonitor() |
aingks | 7:87f7ba90fdce | 144 | { |
aingks | 7:87f7ba90fdce | 145 | float delay = 1000 / TASKFREQ6; |
aingks | 7:87f7ba90fdce | 146 | while(true){ |
aingks | 7:87f7ba90fdce | 147 | |
aingks | 7:87f7ba90fdce | 148 | carstateXS.lock(); //lock carstate |
aingks | 7:87f7ba90fdce | 149 | speedXS.lock(); //lock speed |
aingks | 7:87f7ba90fdce | 150 | |
aingks | 7:87f7ba90fdce | 151 | |
aingks | 7:87f7ba90fdce | 152 | info.odometer += (speed.rawspeed / TASKFREQ6); |
aingks | 7:87f7ba90fdce | 153 | uint8_t temp_enginestate = info.ENGINESTATE; |
aingks | 7:87f7ba90fdce | 154 | |
aingks | 7:87f7ba90fdce | 155 | carstateXS.unlock(); //unlock carstate |
aingks | 7:87f7ba90fdce | 156 | |
aingks | 7:87f7ba90fdce | 157 | float temp_accel = 0.0; |
aingks | 7:87f7ba90fdce | 158 | if(temp_enginestate != 0) |
aingks | 7:87f7ba90fdce | 159 | temp_accel = speed.accelvalue; |
aingks | 7:87f7ba90fdce | 160 | |
aingks | 7:87f7ba90fdce | 161 | //update speed |
aingks | 7:87f7ba90fdce | 162 | speed.rawspeed += (temp_accel - speed.brakevalue) * MAXSPEED; |
aingks | 7:87f7ba90fdce | 163 | |
aingks | 7:87f7ba90fdce | 164 | if(speed.rawspeed < 0) |
aingks | 7:87f7ba90fdce | 165 | speed.rawspeed = 0; |
aingks | 7:87f7ba90fdce | 166 | if(speed.rawspeed > MAXSPEED) |
aingks | 7:87f7ba90fdce | 167 | speed.rawspeed = MAXSPEED; |
aingks | 7:87f7ba90fdce | 168 | |
aingks | 7:87f7ba90fdce | 169 | // Saving the new speed |
aingks | 7:87f7ba90fdce | 170 | for (int i = 0; i < SAMPLESIZE - 1; i++) |
aingks | 7:87f7ba90fdce | 171 | |
aingks | 7:87f7ba90fdce | 172 | speed.array[i]= speed.array[i+1]; |
aingks | 7:87f7ba90fdce | 173 | speed.array[4]= speed.rawspeed; |
aingks | 7:87f7ba90fdce | 174 | |
aingks | 7:87f7ba90fdce | 175 | float temp = speed.rawspeed; |
aingks | 7:87f7ba90fdce | 176 | |
aingks | 7:87f7ba90fdce | 177 | // UNLOCK Speed |
aingks | 7:87f7ba90fdce | 178 | speedXS.unlock(); |
aingks | 7:87f7ba90fdce | 179 | |
aingks | 7:87f7ba90fdce | 180 | // Update Overspeed display |
aingks | 7:87f7ba90fdce | 181 | |
aingks | 7:87f7ba90fdce | 182 | if (temp > SPEEDLIMIT) |
aingks | 7:87f7ba90fdce | 183 | overspeedind = 1; |
aingks | 7:87f7ba90fdce | 184 | else |
aingks | 7:87f7ba90fdce | 185 | overspeedind = 0; |
aingks | 7:87f7ba90fdce | 186 | |
aingks | 7:87f7ba90fdce | 187 | Thread::wait((int)delay); |
aingks | 7:87f7ba90fdce | 188 | } |
aingks | 7:87f7ba90fdce | 189 | } |
aingks | 7:87f7ba90fdce | 190 | |
aingks | 7:87f7ba90fdce | 191 | void task7displayvalues() |
aingks | 7:87f7ba90fdce | 192 | { |
aingks | 7:87f7ba90fdce | 193 | float delay = 1000 / TASKFREQ7; |
aingks | 7:87f7ba90fdce | 194 | while(true){ |
aingks | 8:3f3f2c2e2046 | 195 | |
aingks | 8:3f3f2c2e2046 | 196 | display->cls(); |
aingks | 7:87f7ba90fdce | 197 | |
aingks | 7:87f7ba90fdce | 198 | //LOCK data |
aingks | 7:87f7ba90fdce | 199 | carstateXS.lock(); |
aingks | 7:87f7ba90fdce | 200 | //LOCK Speed |
aingks | 7:87f7ba90fdce | 201 | speedXS.lock(); |
aingks | 7:87f7ba90fdce | 202 | |
aingks | 7:87f7ba90fdce | 203 | display->locate(1,0); |
aingks | 8:3f3f2c2e2046 | 204 | display->printf("AvgS: %.2f",speed.average); |
aingks | 7:87f7ba90fdce | 205 | display->locate(0,0); |
aingks | 7:87f7ba90fdce | 206 | display->printf("ODOM: %d",(int) info.odometer); |
aingks | 7:87f7ba90fdce | 207 | |
aingks | 7:87f7ba90fdce | 208 | //UNLOCK data |
aingks | 7:87f7ba90fdce | 209 | carstateXS.unlock(); |
aingks | 7:87f7ba90fdce | 210 | //UNLOCK Speed |
aingks | 7:87f7ba90fdce | 211 | speedXS.unlock(); |
aingks | 7:87f7ba90fdce | 212 | |
aingks | 8:3f3f2c2e2046 | 213 | |
aingks | 8:3f3f2c2e2046 | 214 | |
aingks | 8:3f3f2c2e2046 | 215 | |
aingks | 8:3f3f2c2e2046 | 216 | Thread::wait(500); |
aingks | 8:3f3f2c2e2046 | 217 | |
aingks | 8:3f3f2c2e2046 | 218 | |
aingks | 8:3f3f2c2e2046 | 219 | } |
aingks | 8:3f3f2c2e2046 | 220 | } |
aingks | 8:3f3f2c2e2046 | 221 | |
aingks | 8:3f3f2c2e2046 | 222 | void task8sidelights() |
aingks | 8:3f3f2c2e2046 | 223 | { |
aingks | 8:3f3f2c2e2046 | 224 | float delay = 1000 / TASKFREQ8; |
aingks | 8:3f3f2c2e2046 | 225 | while(true){ |
aingks | 8:3f3f2c2e2046 | 226 | |
aingks | 8:3f3f2c2e2046 | 227 | // LOCK Car data |
aingks | 8:3f3f2c2e2046 | 228 | carstateXS.lock(); |
aingks | 8:3f3f2c2e2046 | 229 | |
aingks | 8:3f3f2c2e2046 | 230 | info.sideind = sidesw.read(); |
aingks | 8:3f3f2c2e2046 | 231 | uint8_t temp = info.sideind; |
aingks | 8:3f3f2c2e2046 | 232 | |
aingks | 8:3f3f2c2e2046 | 233 | //UNLOCK Car data |
aingks | 8:3f3f2c2e2046 | 234 | carstateXS.unlock(); |
aingks | 8:3f3f2c2e2046 | 235 | |
aingks | 8:3f3f2c2e2046 | 236 | if (temp == 1) |
aingks | 8:3f3f2c2e2046 | 237 | sideind = 1; |
aingks | 8:3f3f2c2e2046 | 238 | else |
aingks | 8:3f3f2c2e2046 | 239 | sideind = 0; |
aingks | 8:3f3f2c2e2046 | 240 | |
aingks | 7:87f7ba90fdce | 241 | Thread::wait((int)delay); |
aingks | 7:87f7ba90fdce | 242 | |
aingks | 7:87f7ba90fdce | 243 | |
aingks | 8:3f3f2c2e2046 | 244 | |
aingks | 9:2fd97246b8f0 | 245 | |
aingks | 7:87f7ba90fdce | 246 | } |
aingks | 9:2fd97246b8f0 | 247 | } |
aingks | 9:2fd97246b8f0 | 248 | |
aingks | 9:2fd97246b8f0 | 249 | void task9indLED() |
aingks | 9:2fd97246b8f0 | 250 | { |
aingks | 9:2fd97246b8f0 | 251 | float delay = 1000 / TASKFREQ9; |
aingks | 9:2fd97246b8f0 | 252 | while(true){ |
aingks | 9:2fd97246b8f0 | 253 | uint8_t turnleft = leftsw.read(); |
aingks | 9:2fd97246b8f0 | 254 | uint8_t turnright = rightsw.read(); |
aingks | 9:2fd97246b8f0 | 255 | |
aingks | 9:2fd97246b8f0 | 256 | carstateXS.lock(); |
aingks | 9:2fd97246b8f0 | 257 | |
aingks | 9:2fd97246b8f0 | 258 | |
aingks | 9:2fd97246b8f0 | 259 | |
aingks | 9:2fd97246b8f0 | 260 | (turnleft = 1)? info.leftind = 1 :info.leftind = 0; |
aingks | 9:2fd97246b8f0 | 261 | (turnright = 1)? info.rightind = 1 :info.rightind = 0; |
aingks | 9:2fd97246b8f0 | 262 | |
aingks | 9:2fd97246b8f0 | 263 | /* if(turnleft = 1){ |
aingks | 9:2fd97246b8f0 | 264 | info.leftind = 1; |
aingks | 9:2fd97246b8f0 | 265 | } |
aingks | 9:2fd97246b8f0 | 266 | else{ |
aingks | 9:2fd97246b8f0 | 267 | info.leftind = 0; |
aingks | 9:2fd97246b8f0 | 268 | } |
aingks | 9:2fd97246b8f0 | 269 | |
aingks | 9:2fd97246b8f0 | 270 | if(turnleft = 1){ |
aingks | 9:2fd97246b8f0 | 271 | info.rightind = 1; |
aingks | 9:2fd97246b8f0 | 272 | } |
aingks | 9:2fd97246b8f0 | 273 | else{ |
aingks | 9:2fd97246b8f0 | 274 | info.rightind = 0; |
aingks | 9:2fd97246b8f0 | 275 | } |
aingks | 9:2fd97246b8f0 | 276 | */ |
aingks | 9:2fd97246b8f0 | 277 | |
aingks | 9:2fd97246b8f0 | 278 | carstateXS.unlock(); |
aingks | 9:2fd97246b8f0 | 279 | |
aingks | 9:2fd97246b8f0 | 280 | //hazard and other lights |
aingks | 9:2fd97246b8f0 | 281 | |
aingks | 9:2fd97246b8f0 | 282 | float left_period = 1.0; |
aingks | 9:2fd97246b8f0 | 283 | float right_period = 1.0; |
aingks | 9:2fd97246b8f0 | 284 | float left_pulswidth = 0.0; |
aingks | 9:2fd97246b8f0 | 285 | float right_pulswidth = 0.0; |
aingks | 9:2fd97246b8f0 | 286 | |
aingks | 9:2fd97246b8f0 | 287 | if (turnleft != 0) |
aingks | 9:2fd97246b8f0 | 288 | { |
aingks | 9:2fd97246b8f0 | 289 | left_period = (1/TASKFREQ9); |
aingks | 9:2fd97246b8f0 | 290 | left_pulswidth = (1/(2*TASKFREQ9)); |
aingks | 9:2fd97246b8f0 | 291 | } |
aingks | 9:2fd97246b8f0 | 292 | if (turnright!= 0) |
aingks | 9:2fd97246b8f0 | 293 | { |
aingks | 9:2fd97246b8f0 | 294 | right_period = (1/TASKFREQ9); |
aingks | 9:2fd97246b8f0 | 295 | right_pulswidth = (1/TASKFREQ9); |
aingks | 9:2fd97246b8f0 | 296 | } |
aingks | 9:2fd97246b8f0 | 297 | if (turnleft != 0 && turnright != 0) |
aingks | 9:2fd97246b8f0 | 298 | { |
aingks | 9:2fd97246b8f0 | 299 | left_period = left_period / 2; |
aingks | 9:2fd97246b8f0 | 300 | left_pulswidth = left_pulswidth / 2; |
aingks | 9:2fd97246b8f0 | 301 | right_period = right_period / 2; |
aingks | 9:2fd97246b8f0 | 302 | right_pulswidth = right_pulswidth / 2; |
aingks | 9:2fd97246b8f0 | 303 | } |
aingks | 9:2fd97246b8f0 | 304 | |
aingks | 9:2fd97246b8f0 | 305 | leftind.period(left_period); |
aingks | 9:2fd97246b8f0 | 306 | leftind.pulsewidth(left_pulswidth); |
aingks | 9:2fd97246b8f0 | 307 | rightind.period(right_period); |
aingks | 9:2fd97246b8f0 | 308 | rightind.pulsewidth(right_pulswidth); |
aingks | 9:2fd97246b8f0 | 309 | |
aingks | 9:2fd97246b8f0 | 310 | float delay = 1000 / TASKFREQ9S2; |
aingks | 9:2fd97246b8f0 | 311 | Thread::wait((int)delay); |
aingks | 9:2fd97246b8f0 | 312 | |
aingks | 9:2fd97246b8f0 | 313 | |
aingks | 9:2fd97246b8f0 | 314 | |
aingks | 9:2fd97246b8f0 | 315 | } |
aingks | 9:2fd97246b8f0 | 316 | |
aingks | 9:2fd97246b8f0 | 317 | |
aingks | 9:2fd97246b8f0 | 318 | |
aingks | 9:2fd97246b8f0 | 319 | |
aingks | 9:2fd97246b8f0 | 320 | } |