![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All tasks complete
Dependencies: mbed MCP23017 mbed-rtos WattBob_TextLCD
assignment3tasks.cpp@18:65c51c80a165, 2019-04-03 (annotated)
- Committer:
- aingks
- Date:
- Wed Apr 03 20:06:28 2019 +0000
- Revision:
- 18:65c51c80a165
- Parent:
- 17:6b4d94f9c77a
clean code; removed alternative code
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 | 13:822b0e56ea68 | 21 | DigitalOut leftind(pin_LED2); //was digitalout |
aingks | 13:822b0e56ea68 | 22 | DigitalOut 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 | 11:d6d1583fc824 | 48 | //serial port |
aingks | 11:d6d1583fc824 | 49 | Serial PC(USBTX, USBRX); |
aingks | 0:0943f040009c | 50 | |
aingks | 0:0943f040009c | 51 | void task1readbrake() |
aingks | 6:abbd51d9b045 | 52 | { |
aingks | 6:abbd51d9b045 | 53 | float delay = 1000 / TASKFREQ1; |
aingks | 6:abbd51d9b045 | 54 | while(true){ |
aingks | 6:abbd51d9b045 | 55 | |
aingks | 4:27340b291c09 | 56 | speedXS.lock();//lock |
aingks | 6:abbd51d9b045 | 57 | |
aingks | 1:c4f62ef9b5b7 | 58 | speed.brakevalue = brake_pedal.read(); |
aingks | 6:abbd51d9b045 | 59 | |
aingks | 4:27340b291c09 | 60 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 61 | |
aingks | 6:abbd51d9b045 | 62 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 63 | |
aingks | 6:abbd51d9b045 | 64 | } |
aingks | 0:0943f040009c | 65 | } |
aingks | 0:0943f040009c | 66 | |
aingks | 0:0943f040009c | 67 | void task2readaccel() |
aingks | 0:0943f040009c | 68 | { |
aingks | 6:abbd51d9b045 | 69 | float delay = 1000 / TASKFREQ2; |
aingks | 6:abbd51d9b045 | 70 | while(true){ |
aingks | 6:abbd51d9b045 | 71 | |
aingks | 4:27340b291c09 | 72 | speedXS.lock();//lock |
aingks | 6:abbd51d9b045 | 73 | |
aingks | 1:c4f62ef9b5b7 | 74 | speed.accelvalue = accel_pedal.read(); |
aingks | 6:abbd51d9b045 | 75 | |
aingks | 4:27340b291c09 | 76 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 77 | |
aingks | 6:abbd51d9b045 | 78 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 79 | |
aingks | 6:abbd51d9b045 | 80 | } |
aingks | 6:abbd51d9b045 | 81 | |
aingks | 0:0943f040009c | 82 | } |
aingks | 0:0943f040009c | 83 | |
aingks | 0:0943f040009c | 84 | void task3enginestate() |
aingks | 6:abbd51d9b045 | 85 | { |
aingks | 8:3f3f2c2e2046 | 86 | float delay = 125; |
aingks | 6:abbd51d9b045 | 87 | while(true){ |
aingks | 1:c4f62ef9b5b7 | 88 | //lock |
aingks | 4:27340b291c09 | 89 | carstateXS.lock(); |
aingks | 1:c4f62ef9b5b7 | 90 | uint8_t engineonoff = ngene.read(); |
aingks | 2:9d6d07c9cc9c | 91 | info.ENGINESTATE = engineonoff; |
aingks | 4:27340b291c09 | 92 | carstateXS.unlock(); |
aingks | 1:c4f62ef9b5b7 | 93 | //unlock |
aingks | 1:c4f62ef9b5b7 | 94 | |
aingks | 1:c4f62ef9b5b7 | 95 | if(engineonoff == 1){ |
aingks | 1:c4f62ef9b5b7 | 96 | ngeneind = 1; |
aingks | 1:c4f62ef9b5b7 | 97 | } |
aingks | 1:c4f62ef9b5b7 | 98 | else{ |
aingks | 1:c4f62ef9b5b7 | 99 | ngeneind = 0; |
aingks | 1:c4f62ef9b5b7 | 100 | } |
aingks | 6:abbd51d9b045 | 101 | |
aingks | 8:3f3f2c2e2046 | 102 | Thread::wait(125); |
aingks | 6:abbd51d9b045 | 103 | } |
aingks | 0:0943f040009c | 104 | } |
aingks | 0:0943f040009c | 105 | |
aingks | 0:0943f040009c | 106 | void task4average() |
aingks | 0:0943f040009c | 107 | { |
aingks | 6:abbd51d9b045 | 108 | float delay = 1000 / TASKFREQ4; |
aingks | 6:abbd51d9b045 | 109 | while(true){ |
aingks | 6:abbd51d9b045 | 110 | speedXS.lock();//lock |
aingks | 0:0943f040009c | 111 | |
aingks | 12:f4052a23ddc7 | 112 | for(int i = 0; i < SAMPLESIZE - 1 ; i++){ |
aingks | 7:87f7ba90fdce | 113 | //speed.rawspeed = (speed.accelvalue - speed.brakevalue)*MAXSPEED; |
aingks | 5:f5dda79b93cb | 114 | speed.average += speed.array[i]; |
aingks | 8:3f3f2c2e2046 | 115 | } |
aingks | 6:abbd51d9b045 | 116 | speed.average = (speed.average / SAMPLESIZE); |
aingks | 6:abbd51d9b045 | 117 | |
aingks | 6:abbd51d9b045 | 118 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 119 | |
aingks | 6:abbd51d9b045 | 120 | Thread::wait( (int) delay); |
aingks | 6:abbd51d9b045 | 121 | } |
aingks | 6:abbd51d9b045 | 122 | } |
aingks | 6:abbd51d9b045 | 123 | |
aingks | 6:abbd51d9b045 | 124 | void task5brakeLED() |
aingks | 6:abbd51d9b045 | 125 | { |
aingks | 6:abbd51d9b045 | 126 | float delay = 1000 / TASKFREQ5; |
aingks | 6:abbd51d9b045 | 127 | while(true){ |
aingks | 5:f5dda79b93cb | 128 | |
aingks | 6:abbd51d9b045 | 129 | //lock |
aingks | 6:abbd51d9b045 | 130 | speedXS.lock(); |
aingks | 6:abbd51d9b045 | 131 | float braketest = speed.brakevalue; |
aingks | 6:abbd51d9b045 | 132 | |
aingks | 6:abbd51d9b045 | 133 | speedXS.unlock();//unlock |
aingks | 6:abbd51d9b045 | 134 | |
aingks | 6:abbd51d9b045 | 135 | if( braketest > 0.2) // speed increase |
aingks | 6:abbd51d9b045 | 136 | brakeind = 1; |
aingks | 6:abbd51d9b045 | 137 | else |
aingks | 6:abbd51d9b045 | 138 | brakeind = 0; |
aingks | 5:f5dda79b93cb | 139 | |
aingks | 6:abbd51d9b045 | 140 | Thread::wait((int)delay); |
aingks | 6:abbd51d9b045 | 141 | } |
aingks | 7:87f7ba90fdce | 142 | } |
aingks | 7:87f7ba90fdce | 143 | |
aingks | 7:87f7ba90fdce | 144 | |
aingks | 7:87f7ba90fdce | 145 | void task6speedmonitor() |
aingks | 7:87f7ba90fdce | 146 | { |
aingks | 7:87f7ba90fdce | 147 | float delay = 1000 / TASKFREQ6; |
aingks | 7:87f7ba90fdce | 148 | while(true){ |
aingks | 7:87f7ba90fdce | 149 | |
aingks | 7:87f7ba90fdce | 150 | carstateXS.lock(); //lock carstate |
aingks | 7:87f7ba90fdce | 151 | speedXS.lock(); //lock speed |
aingks | 7:87f7ba90fdce | 152 | |
aingks | 7:87f7ba90fdce | 153 | |
aingks | 7:87f7ba90fdce | 154 | info.odometer += (speed.rawspeed / TASKFREQ6); |
aingks | 7:87f7ba90fdce | 155 | uint8_t temp_enginestate = info.ENGINESTATE; |
aingks | 7:87f7ba90fdce | 156 | |
aingks | 7:87f7ba90fdce | 157 | carstateXS.unlock(); //unlock carstate |
aingks | 7:87f7ba90fdce | 158 | |
aingks | 7:87f7ba90fdce | 159 | float temp_accel = 0.0; |
aingks | 7:87f7ba90fdce | 160 | if(temp_enginestate != 0) |
aingks | 7:87f7ba90fdce | 161 | temp_accel = speed.accelvalue; |
aingks | 7:87f7ba90fdce | 162 | |
aingks | 7:87f7ba90fdce | 163 | //update speed |
aingks | 7:87f7ba90fdce | 164 | speed.rawspeed += (temp_accel - speed.brakevalue) * MAXSPEED; |
aingks | 7:87f7ba90fdce | 165 | |
aingks | 7:87f7ba90fdce | 166 | if(speed.rawspeed < 0) |
aingks | 12:f4052a23ddc7 | 167 | speed.rawspeed = 0; |
aingks | 7:87f7ba90fdce | 168 | if(speed.rawspeed > MAXSPEED) |
aingks | 12:f4052a23ddc7 | 169 | speed.rawspeed = MAXSPEED; |
aingks | 7:87f7ba90fdce | 170 | |
aingks | 7:87f7ba90fdce | 171 | // Saving the new speed |
aingks | 7:87f7ba90fdce | 172 | for (int i = 0; i < SAMPLESIZE - 1; i++) |
aingks | 12:f4052a23ddc7 | 173 | speed.array[i]= speed.array[i+1]; |
aingks | 12:f4052a23ddc7 | 174 | speed.array[4]= speed.rawspeed; |
aingks | 7:87f7ba90fdce | 175 | |
aingks | 7:87f7ba90fdce | 176 | float temp = speed.rawspeed; |
aingks | 7:87f7ba90fdce | 177 | |
aingks | 7:87f7ba90fdce | 178 | // UNLOCK Speed |
aingks | 7:87f7ba90fdce | 179 | speedXS.unlock(); |
aingks | 7:87f7ba90fdce | 180 | |
aingks | 7:87f7ba90fdce | 181 | // Update Overspeed display |
aingks | 7:87f7ba90fdce | 182 | |
aingks | 12:f4052a23ddc7 | 183 | if (temp > 39.3395) //88 mph to ms |
aingks | 7:87f7ba90fdce | 184 | overspeedind = 1; |
aingks | 7:87f7ba90fdce | 185 | else |
aingks | 7:87f7ba90fdce | 186 | overspeedind = 0; |
aingks | 7:87f7ba90fdce | 187 | |
aingks | 7:87f7ba90fdce | 188 | Thread::wait((int)delay); |
aingks | 7:87f7ba90fdce | 189 | } |
aingks | 7:87f7ba90fdce | 190 | } |
aingks | 7:87f7ba90fdce | 191 | |
aingks | 7:87f7ba90fdce | 192 | void task7displayvalues() |
aingks | 7:87f7ba90fdce | 193 | { |
aingks | 7:87f7ba90fdce | 194 | float delay = 1000 / TASKFREQ7; |
aingks | 12:f4052a23ddc7 | 195 | display->cls(); |
aingks | 7:87f7ba90fdce | 196 | while(true){ |
aingks | 8:3f3f2c2e2046 | 197 | |
aingks | 12:f4052a23ddc7 | 198 | |
aingks | 7:87f7ba90fdce | 199 | |
aingks | 7:87f7ba90fdce | 200 | //LOCK data |
aingks | 7:87f7ba90fdce | 201 | carstateXS.lock(); |
aingks | 7:87f7ba90fdce | 202 | //LOCK Speed |
aingks | 7:87f7ba90fdce | 203 | speedXS.lock(); |
aingks | 7:87f7ba90fdce | 204 | |
aingks | 7:87f7ba90fdce | 205 | display->locate(1,0); |
aingks | 12:f4052a23ddc7 | 206 | display->printf("AvgS: %.2f m/s",speed.average); |
aingks | 7:87f7ba90fdce | 207 | display->locate(0,0); |
aingks | 12:f4052a23ddc7 | 208 | display->printf("ODOM: %d m",(int) info.odometer); |
aingks | 7:87f7ba90fdce | 209 | |
aingks | 7:87f7ba90fdce | 210 | //UNLOCK data |
aingks | 7:87f7ba90fdce | 211 | carstateXS.unlock(); |
aingks | 7:87f7ba90fdce | 212 | //UNLOCK Speed |
aingks | 7:87f7ba90fdce | 213 | speedXS.unlock(); |
aingks | 7:87f7ba90fdce | 214 | |
aingks | 8:3f3f2c2e2046 | 215 | |
aingks | 8:3f3f2c2e2046 | 216 | |
aingks | 8:3f3f2c2e2046 | 217 | |
aingks | 8:3f3f2c2e2046 | 218 | Thread::wait(500); |
aingks | 8:3f3f2c2e2046 | 219 | |
aingks | 8:3f3f2c2e2046 | 220 | |
aingks | 8:3f3f2c2e2046 | 221 | } |
aingks | 8:3f3f2c2e2046 | 222 | } |
aingks | 8:3f3f2c2e2046 | 223 | |
aingks | 8:3f3f2c2e2046 | 224 | void task8sidelights() |
aingks | 8:3f3f2c2e2046 | 225 | { |
aingks | 8:3f3f2c2e2046 | 226 | float delay = 1000 / TASKFREQ8; |
aingks | 8:3f3f2c2e2046 | 227 | while(true){ |
aingks | 8:3f3f2c2e2046 | 228 | |
aingks | 8:3f3f2c2e2046 | 229 | // LOCK Car data |
aingks | 8:3f3f2c2e2046 | 230 | carstateXS.lock(); |
aingks | 8:3f3f2c2e2046 | 231 | |
aingks | 8:3f3f2c2e2046 | 232 | info.sideind = sidesw.read(); |
aingks | 8:3f3f2c2e2046 | 233 | uint8_t temp = info.sideind; |
aingks | 8:3f3f2c2e2046 | 234 | |
aingks | 8:3f3f2c2e2046 | 235 | //UNLOCK Car data |
aingks | 8:3f3f2c2e2046 | 236 | carstateXS.unlock(); |
aingks | 8:3f3f2c2e2046 | 237 | |
aingks | 8:3f3f2c2e2046 | 238 | if (temp == 1) |
aingks | 8:3f3f2c2e2046 | 239 | sideind = 1; |
aingks | 8:3f3f2c2e2046 | 240 | else |
aingks | 8:3f3f2c2e2046 | 241 | sideind = 0; |
aingks | 8:3f3f2c2e2046 | 242 | |
aingks | 7:87f7ba90fdce | 243 | Thread::wait((int)delay); |
aingks | 7:87f7ba90fdce | 244 | |
aingks | 7:87f7ba90fdce | 245 | |
aingks | 8:3f3f2c2e2046 | 246 | |
aingks | 9:2fd97246b8f0 | 247 | |
aingks | 7:87f7ba90fdce | 248 | } |
aingks | 9:2fd97246b8f0 | 249 | } |
aingks | 12:f4052a23ddc7 | 250 | |
aingks | 9:2fd97246b8f0 | 251 | void task9indLED() |
aingks | 9:2fd97246b8f0 | 252 | { |
aingks | 13:822b0e56ea68 | 253 | int count = 0; |
aingks | 9:2fd97246b8f0 | 254 | while(true){ |
aingks | 9:2fd97246b8f0 | 255 | |
aingks | 10:2b262d810c67 | 256 | |
aingks | 9:2fd97246b8f0 | 257 | |
aingks | 13:822b0e56ea68 | 258 | count++; |
aingks | 13:822b0e56ea68 | 259 | leftind = leftsw; |
aingks | 13:822b0e56ea68 | 260 | rightind = rightsw; |
aingks | 18:65c51c80a165 | 261 | (leftsw==1)? info.leftind = 1: info.leftind = 0; |
aingks | 18:65c51c80a165 | 262 | (rightsw==1)? info.rightind = 1: info.rightind = 0; |
aingks | 13:822b0e56ea68 | 263 | |
aingks | 13:822b0e56ea68 | 264 | if (leftsw == 1 && rightsw == 1){ |
aingks | 13:822b0e56ea68 | 265 | carstateXS.lock(); |
aingks | 13:822b0e56ea68 | 266 | leftind = 0; |
aingks | 13:822b0e56ea68 | 267 | rightind = 0; // current issue :just stopping here |
aingks | 13:822b0e56ea68 | 268 | carstateXS.unlock(); |
aingks | 13:822b0e56ea68 | 269 | } |
aingks | 13:822b0e56ea68 | 270 | if (count == 2||count == 4){ // flash indicators every second |
aingks | 13:822b0e56ea68 | 271 | carstateXS.lock(); |
aingks | 13:822b0e56ea68 | 272 | if (leftind == 1 && rightind == 0){ |
aingks | 13:822b0e56ea68 | 273 | leftind = 0; |
aingks | 13:822b0e56ea68 | 274 | } |
aingks | 13:822b0e56ea68 | 275 | else if (rightind == 1 && leftind == 0){ |
aingks | 13:822b0e56ea68 | 276 | rightind = 0; |
aingks | 13:822b0e56ea68 | 277 | } |
aingks | 9:2fd97246b8f0 | 278 | |
aingks | 13:822b0e56ea68 | 279 | else if (leftind == 0 && rightind == 0){ |
aingks | 13:822b0e56ea68 | 280 | } |
aingks | 13:822b0e56ea68 | 281 | carstateXS.unlock(); |
aingks | 13:822b0e56ea68 | 282 | } |
aingks | 13:822b0e56ea68 | 283 | if (count == 4){ // send data every 2 seconds |
aingks | 13:822b0e56ea68 | 284 | count = 0; |
aingks | 13:822b0e56ea68 | 285 | } |
aingks | 13:822b0e56ea68 | 286 | Thread::wait(500); |
aingks | 9:2fd97246b8f0 | 287 | |
aingks | 9:2fd97246b8f0 | 288 | } |
aingks | 9:2fd97246b8f0 | 289 | |
aingks | 13:822b0e56ea68 | 290 | } |
aingks | 9:2fd97246b8f0 | 291 | |
aingks | 9:2fd97246b8f0 | 292 | |
aingks | 10:2b262d810c67 | 293 | void taskXserialdump() |
aingks | 10:2b262d810c67 | 294 | { |
aingks | 10:2b262d810c67 | 295 | while(true) |
aingks | 10:2b262d810c67 | 296 | { |
aingks | 10:2b262d810c67 | 297 | speedXS.lock(); |
aingks | 10:2b262d810c67 | 298 | |
aingks | 10:2b262d810c67 | 299 | PC.printf("SPEED: %.1f\r\n",speed.rawspeed); |
aingks | 17:6b4d94f9c77a | 300 | PC.printf("BRAKE: %.2f\r\n",speed.brakevalue); |
aingks | 17:6b4d94f9c77a | 301 | PC.printf("ACCELEROMETER: %.2f\r\n",speed.accelvalue); |
aingks | 10:2b262d810c67 | 302 | |
aingks | 10:2b262d810c67 | 303 | speedXS.unlock(); |
aingks | 10:2b262d810c67 | 304 | |
aingks | 12:f4052a23ddc7 | 305 | carstateXS.lock(); |
aingks | 10:2b262d810c67 | 306 | |
aingks | 10:2b262d810c67 | 307 | PC.printf("LEFT INDICATOR : %.1f\r\n",info.leftind); |
aingks | 10:2b262d810c67 | 308 | PC.printf("RIGHT INDICATOR : %.1f\r\n",info.rightind); |
aingks | 10:2b262d810c67 | 309 | |
aingks | 12:f4052a23ddc7 | 310 | carstateXS.unlock(); |
aingks | 10:2b262d810c67 | 311 | |
aingks | 10:2b262d810c67 | 312 | float delay = 1000/ TASKFREQX; |
aingks | 10:2b262d810c67 | 313 | Thread:: wait((int)delay); |
aingks | 10:2b262d810c67 | 314 | |
aingks | 10:2b262d810c67 | 315 | } |
aingks | 10:2b262d810c67 | 316 | |
aingks | 10:2b262d810c67 | 317 | |
aingks | 10:2b262d810c67 | 318 | |
aingks | 18:65c51c80a165 | 319 | } |