![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Copy_Assignment3
Dependencies: mbed MCP23017 WattBob_TextLCD mbed-rtos
source/my_tasks.cpp@0:8940db3353d7, 2018-03-28 (annotated)
- Committer:
- aoc2
- Date:
- Wed Mar 28 18:51:55 2018 +0000
- Revision:
- 0:8940db3353d7
Copy of Assig 3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aoc2 | 0:8940db3353d7 | 1 | #include "my_tasks.h" |
aoc2 | 0:8940db3353d7 | 2 | #include "my_structures.h" |
aoc2 | 0:8940db3353d7 | 3 | #include "my_tools.h" |
aoc2 | 0:8940db3353d7 | 4 | |
aoc2 | 0:8940db3353d7 | 5 | #ifndef __CAR_STATS__ |
aoc2 | 0:8940db3353d7 | 6 | #define __CAT_STATS__ |
aoc2 | 0:8940db3353d7 | 7 | |
aoc2 | 0:8940db3353d7 | 8 | #define AIR_DENSITY 1.225 // kg/m3 |
aoc2 | 0:8940db3353d7 | 9 | |
aoc2 | 0:8940db3353d7 | 10 | // TESLA MODEL X |
aoc2 | 0:8940db3353d7 | 11 | #define DRAG_COEF 0.24 |
aoc2 | 0:8940db3353d7 | 12 | #define REF_AREA 0.62 // m² |
aoc2 | 0:8940db3353d7 | 13 | |
aoc2 | 0:8940db3353d7 | 14 | #endif |
aoc2 | 0:8940db3353d7 | 15 | |
aoc2 | 0:8940db3353d7 | 16 | using namespace std; |
aoc2 | 0:8940db3353d7 | 17 | |
aoc2 | 0:8940db3353d7 | 18 | // TASKS at 10Hz and 5Hz |
aoc2 | 0:8940db3353d7 | 19 | void timer1(void *arg) |
aoc2 | 0:8940db3353d7 | 20 | { |
aoc2 | 0:8940db3353d7 | 21 | static int counterTimer1 = 0; |
aoc2 | 0:8940db3353d7 | 22 | |
aoc2 | 0:8940db3353d7 | 23 | counterTimer1++; |
aoc2 | 0:8940db3353d7 | 24 | task1_readAccelBrake(arg); |
aoc2 | 0:8940db3353d7 | 25 | |
aoc2 | 0:8940db3353d7 | 26 | if (counterTimer1 % 2 == 0) |
aoc2 | 0:8940db3353d7 | 27 | { |
aoc2 | 0:8940db3353d7 | 28 | task3_filterSpeed(arg); |
aoc2 | 0:8940db3353d7 | 29 | } |
aoc2 | 0:8940db3353d7 | 30 | } |
aoc2 | 0:8940db3353d7 | 31 | |
aoc2 | 0:8940db3353d7 | 32 | // TASKS at 2Hz, 1 Hz and 0.5 Hz |
aoc2 | 0:8940db3353d7 | 33 | void timer2(void *arg) |
aoc2 | 0:8940db3353d7 | 34 | { |
aoc2 | 0:8940db3353d7 | 35 | static int counterTimer2 = 0; |
aoc2 | 0:8940db3353d7 | 36 | |
aoc2 | 0:8940db3353d7 | 37 | counterTimer2++; |
aoc2 | 0:8940db3353d7 | 38 | task2_readEngineState(arg); |
aoc2 | 0:8940db3353d7 | 39 | task4_showUseOfBrake(arg); |
aoc2 | 0:8940db3353d7 | 40 | task6_writeLCD(arg); |
aoc2 | 0:8940db3353d7 | 41 | |
aoc2 | 0:8940db3353d7 | 42 | if (counterTimer2 % 2 == 0) |
aoc2 | 0:8940db3353d7 | 43 | { |
aoc2 | 0:8940db3353d7 | 44 | task5_monitorSpeed(arg); |
aoc2 | 0:8940db3353d7 | 45 | task9_readSideLight(arg); |
aoc2 | 0:8940db3353d7 | 46 | } |
aoc2 | 0:8940db3353d7 | 47 | if (counterTimer2 % 4 == 0) |
aoc2 | 0:8940db3353d7 | 48 | { |
aoc2 | 0:8940db3353d7 | 49 | task10_turnIndic(arg); |
aoc2 | 0:8940db3353d7 | 50 | } |
aoc2 | 0:8940db3353d7 | 51 | } |
aoc2 | 0:8940db3353d7 | 52 | |
aoc2 | 0:8940db3353d7 | 53 | // TASKS at 0.2 Hz and 0.05 Hz |
aoc2 | 0:8940db3353d7 | 54 | void timer3(void *arg) |
aoc2 | 0:8940db3353d7 | 55 | { |
aoc2 | 0:8940db3353d7 | 56 | static int counterTimer3 = 0; |
aoc2 | 0:8940db3353d7 | 57 | |
aoc2 | 0:8940db3353d7 | 58 | counterTimer3++; |
aoc2 | 0:8940db3353d7 | 59 | task7_sendToMailQueue(arg); |
aoc2 | 0:8940db3353d7 | 60 | |
aoc2 | 0:8940db3353d7 | 61 | if (counterTimer3 % 4 == 0) |
aoc2 | 0:8940db3353d7 | 62 | { |
aoc2 | 0:8940db3353d7 | 63 | task8_dumpMailQueue(arg); |
aoc2 | 0:8940db3353d7 | 64 | } |
aoc2 | 0:8940db3353d7 | 65 | } |
aoc2 | 0:8940db3353d7 | 66 | |
aoc2 | 0:8940db3353d7 | 67 | |
aoc2 | 0:8940db3353d7 | 68 | |
aoc2 | 0:8940db3353d7 | 69 | |
aoc2 | 0:8940db3353d7 | 70 | |
aoc2 | 0:8940db3353d7 | 71 | |
aoc2 | 0:8940db3353d7 | 72 | |
aoc2 | 0:8940db3353d7 | 73 | |
aoc2 | 0:8940db3353d7 | 74 | |
aoc2 | 0:8940db3353d7 | 75 | // CAR SIMULATOR |
aoc2 | 0:8940db3353d7 | 76 | void task0_carSim(void *arg) |
aoc2 | 0:8940db3353d7 | 77 | { |
aoc2 | 0:8940db3353d7 | 78 | const float dt = 0.05; // 20Hz |
aoc2 | 0:8940db3353d7 | 79 | carStructure *myCar = (carStructure *) arg; |
aoc2 | 0:8940db3353d7 | 80 | |
aoc2 | 0:8940db3353d7 | 81 | // Block access on RAWValues structure |
aoc2 | 0:8940db3353d7 | 82 | myCar->p_rawMutex->lock(); |
aoc2 | 0:8940db3353d7 | 83 | engineRAWValues &p_rawValues = *(myCar->p_rawValues); |
aoc2 | 0:8940db3353d7 | 84 | |
aoc2 | 0:8940db3353d7 | 85 | // Copy the values |
aoc2 | 0:8940db3353d7 | 86 | int engine = p_rawValues.engineState; |
aoc2 | 0:8940db3353d7 | 87 | float accel = p_rawValues.acceleratorValue; |
aoc2 | 0:8940db3353d7 | 88 | float brake = p_rawValues.brakeValue; |
aoc2 | 0:8940db3353d7 | 89 | |
aoc2 | 0:8940db3353d7 | 90 | // Release the mutex once we copied the values |
aoc2 | 0:8940db3353d7 | 91 | myCar->p_rawMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 92 | |
aoc2 | 0:8940db3353d7 | 93 | |
aoc2 | 0:8940db3353d7 | 94 | |
aoc2 | 0:8940db3353d7 | 95 | // Add it the vector saving the speed values |
aoc2 | 0:8940db3353d7 | 96 | myCar->p_statMutex->lock(); |
aoc2 | 0:8940db3353d7 | 97 | // Get last speed value stored |
aoc2 | 0:8940db3353d7 | 98 | float lastSpeed =(*(myCar->p_carStats->p_speedVector))[SIZE_SPEED_VECTOR-1]; |
aoc2 | 0:8940db3353d7 | 99 | myCar->p_statMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 100 | |
aoc2 | 0:8940db3353d7 | 101 | // ---------------------------------------------------------------------- // |
aoc2 | 0:8940db3353d7 | 102 | // Acceleration Force |
aoc2 | 0:8940db3353d7 | 103 | float accelForce = MAX_SPEED * (accel - brake) * engine; |
aoc2 | 0:8940db3353d7 | 104 | |
aoc2 | 0:8940db3353d7 | 105 | // ---------------------------------------------------------------------- // |
aoc2 | 0:8940db3353d7 | 106 | /* |
aoc2 | 0:8940db3353d7 | 107 | * DRAG FORCE -> F = 0.5 C ρ A V² |
aoc2 | 0:8940db3353d7 | 108 | * |
aoc2 | 0:8940db3353d7 | 109 | * A = Reference area, m2. |
aoc2 | 0:8940db3353d7 | 110 | * C = Drag coefficient, unitless. |
aoc2 | 0:8940db3353d7 | 111 | * F = Drag force, N. |
aoc2 | 0:8940db3353d7 | 112 | * V = Velocity, m/s. |
aoc2 | 0:8940db3353d7 | 113 | * ρ = Density of fluid (liquid or gas), kg/m3. |
aoc2 | 0:8940db3353d7 | 114 | */ |
aoc2 | 0:8940db3353d7 | 115 | float dragForce = 0.5*DRAG_COEF |
aoc2 | 0:8940db3353d7 | 116 | *lastSpeed*lastSpeed |
aoc2 | 0:8940db3353d7 | 117 | *REF_AREA |
aoc2 | 0:8940db3353d7 | 118 | *AIR_DENSITY; |
aoc2 | 0:8940db3353d7 | 119 | if (lastSpeed == 0) dragForce = 0; |
aoc2 | 0:8940db3353d7 | 120 | |
aoc2 | 0:8940db3353d7 | 121 | // ---------------------------------------------------------------------- // |
aoc2 | 0:8940db3353d7 | 122 | // Compute speed of car on Euler Scheme |
aoc2 | 0:8940db3353d7 | 123 | float newSpeed = lastSpeed*0.98 |
aoc2 | 0:8940db3353d7 | 124 | + dt*(accelForce - dragForce); |
aoc2 | 0:8940db3353d7 | 125 | if (newSpeed < 0) newSpeed = 0; |
aoc2 | 0:8940db3353d7 | 126 | |
aoc2 | 0:8940db3353d7 | 127 | // ---------------------------------------------------------------------- // |
aoc2 | 0:8940db3353d7 | 128 | // Lock Mutex |
aoc2 | 0:8940db3353d7 | 129 | myCar->p_statMutex->lock(); |
aoc2 | 0:8940db3353d7 | 130 | // Delete first element of vector |
aoc2 | 0:8940db3353d7 | 131 | deleteFirstValueVec(*(myCar->p_carStats->p_speedVector)); |
aoc2 | 0:8940db3353d7 | 132 | // Add new speed value |
aoc2 | 0:8940db3353d7 | 133 | myCar->p_carStats->p_speedVector->push_back(newSpeed); // m/s |
aoc2 | 0:8940db3353d7 | 134 | |
aoc2 | 0:8940db3353d7 | 135 | // ---------------------------------------------------------------------- // |
aoc2 | 0:8940db3353d7 | 136 | // Euler Scheme for traveled distance |
aoc2 | 0:8940db3353d7 | 137 | float distance = myCar->p_carStats->distance + dt*newSpeed; //m |
aoc2 | 0:8940db3353d7 | 138 | myCar->p_carStats->distance = distance; |
aoc2 | 0:8940db3353d7 | 139 | // Unlock Mutex |
aoc2 | 0:8940db3353d7 | 140 | myCar->p_statMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 141 | |
aoc2 | 0:8940db3353d7 | 142 | // ---------------------------------------------------------------------- // |
aoc2 | 0:8940db3353d7 | 143 | myCar->p_PC->printf("Accel: %.2f | Brake: %.2f\r\n", accel, brake); |
aoc2 | 0:8940db3353d7 | 144 | myCar->p_PC->printf("New Speed: %.1f km/h\r\n", newSpeed*3.6); |
aoc2 | 0:8940db3353d7 | 145 | myCar->p_PC->printf("Traveled Distance: %.3f km\r\n", distance/1000); |
aoc2 | 0:8940db3353d7 | 146 | } |
aoc2 | 0:8940db3353d7 | 147 | |
aoc2 | 0:8940db3353d7 | 148 | // TASK 1 |
aoc2 | 0:8940db3353d7 | 149 | void task1_readAccelBrake(void *arg) |
aoc2 | 0:8940db3353d7 | 150 | { |
aoc2 | 0:8940db3353d7 | 151 | carStructure &myCar = *((carStructure *) arg); |
aoc2 | 0:8940db3353d7 | 152 | |
aoc2 | 0:8940db3353d7 | 153 | // Block access on RAWValues structure |
aoc2 | 0:8940db3353d7 | 154 | (myCar.p_rawMutex)->lock(); |
aoc2 | 0:8940db3353d7 | 155 | engineRAWValues &p_rawValues = *(myCar.p_rawValues); |
aoc2 | 0:8940db3353d7 | 156 | |
aoc2 | 0:8940db3353d7 | 157 | // Read Accelerator and Brake Values |
aoc2 | 0:8940db3353d7 | 158 | p_rawValues.acceleratorValue = readAnalogInput(*(myCar.p_accelerator)); |
aoc2 | 0:8940db3353d7 | 159 | p_rawValues.brakeValue = readAnalogInput(*(myCar.p_brake)); |
aoc2 | 0:8940db3353d7 | 160 | |
aoc2 | 0:8940db3353d7 | 161 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 162 | (myCar.p_rawMutex)->unlock(); |
aoc2 | 0:8940db3353d7 | 163 | } |
aoc2 | 0:8940db3353d7 | 164 | |
aoc2 | 0:8940db3353d7 | 165 | // TASK 2 |
aoc2 | 0:8940db3353d7 | 166 | void task2_readEngineState(void *arg) |
aoc2 | 0:8940db3353d7 | 167 | { |
aoc2 | 0:8940db3353d7 | 168 | carStructure *myCar = (carStructure *) arg; |
aoc2 | 0:8940db3353d7 | 169 | |
aoc2 | 0:8940db3353d7 | 170 | // Block access on RAWValues structure |
aoc2 | 0:8940db3353d7 | 171 | myCar->p_rawMutex->lock(); |
aoc2 | 0:8940db3353d7 | 172 | engineRAWValues &p_rawValues = *(myCar->p_rawValues); |
aoc2 | 0:8940db3353d7 | 173 | |
aoc2 | 0:8940db3353d7 | 174 | // Read Engine State |
aoc2 | 0:8940db3353d7 | 175 | int state = myCar->p_engineSwitch->read(); |
aoc2 | 0:8940db3353d7 | 176 | p_rawValues.engineState = state; |
aoc2 | 0:8940db3353d7 | 177 | if (state) |
aoc2 | 0:8940db3353d7 | 178 | { |
aoc2 | 0:8940db3353d7 | 179 | *(myCar->p_engineLight) = ON; |
aoc2 | 0:8940db3353d7 | 180 | } |
aoc2 | 0:8940db3353d7 | 181 | else |
aoc2 | 0:8940db3353d7 | 182 | { |
aoc2 | 0:8940db3353d7 | 183 | *(myCar->p_engineLight) = OFF; |
aoc2 | 0:8940db3353d7 | 184 | } |
aoc2 | 0:8940db3353d7 | 185 | |
aoc2 | 0:8940db3353d7 | 186 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 187 | myCar->p_rawMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 188 | } |
aoc2 | 0:8940db3353d7 | 189 | |
aoc2 | 0:8940db3353d7 | 190 | // TASK 3 |
aoc2 | 0:8940db3353d7 | 191 | void task3_filterSpeed(void *arg) |
aoc2 | 0:8940db3353d7 | 192 | { |
aoc2 | 0:8940db3353d7 | 193 | carStructure &myCar = *((carStructure *) arg); |
aoc2 | 0:8940db3353d7 | 194 | |
aoc2 | 0:8940db3353d7 | 195 | // Block access on carStat |
aoc2 | 0:8940db3353d7 | 196 | myCar.p_statMutex->lock(); |
aoc2 | 0:8940db3353d7 | 197 | |
aoc2 | 0:8940db3353d7 | 198 | // Copy pointers to values we want to manipulate |
aoc2 | 0:8940db3353d7 | 199 | carStatistics &carStat = *(myCar.p_carStats); |
aoc2 | 0:8940db3353d7 | 200 | vector<float> &speedVec = *(carStat.p_speedVector); |
aoc2 | 0:8940db3353d7 | 201 | |
aoc2 | 0:8940db3353d7 | 202 | // Compute average speed on sizeOfAvg last readings |
aoc2 | 0:8940db3353d7 | 203 | float sum = 0; |
aoc2 | 0:8940db3353d7 | 204 | for (int i = 1; i < carStat.sizeOfAvg + 1; i++) |
aoc2 | 0:8940db3353d7 | 205 | { |
aoc2 | 0:8940db3353d7 | 206 | sum += speedVec[SIZE_SPEED_VECTOR - i]; |
aoc2 | 0:8940db3353d7 | 207 | } |
aoc2 | 0:8940db3353d7 | 208 | // Compute the average speed |
aoc2 | 0:8940db3353d7 | 209 | carStat.averageSpeed = sum / carStat.sizeOfAvg; |
aoc2 | 0:8940db3353d7 | 210 | |
aoc2 | 0:8940db3353d7 | 211 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 212 | myCar.p_statMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 213 | } |
aoc2 | 0:8940db3353d7 | 214 | |
aoc2 | 0:8940db3353d7 | 215 | // TASK 4 |
aoc2 | 0:8940db3353d7 | 216 | void task4_showUseOfBrake(void *arg) |
aoc2 | 0:8940db3353d7 | 217 | { |
aoc2 | 0:8940db3353d7 | 218 | carStructure &myCar = *((carStructure *) arg); |
aoc2 | 0:8940db3353d7 | 219 | |
aoc2 | 0:8940db3353d7 | 220 | // Block access on RAWValues structure |
aoc2 | 0:8940db3353d7 | 221 | myCar.p_rawMutex->lock(); |
aoc2 | 0:8940db3353d7 | 222 | engineRAWValues &p_rawValues = *(myCar.p_rawValues); |
aoc2 | 0:8940db3353d7 | 223 | |
aoc2 | 0:8940db3353d7 | 224 | // Read Brake State |
aoc2 | 0:8940db3353d7 | 225 | float brake = p_rawValues.brakeValue; |
aoc2 | 0:8940db3353d7 | 226 | |
aoc2 | 0:8940db3353d7 | 227 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 228 | myCar.p_rawMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 229 | |
aoc2 | 0:8940db3353d7 | 230 | // Deal with the copied value |
aoc2 | 0:8940db3353d7 | 231 | monitorValue(brake, *(myCar.p_brakeLight), 0.05); |
aoc2 | 0:8940db3353d7 | 232 | } |
aoc2 | 0:8940db3353d7 | 233 | |
aoc2 | 0:8940db3353d7 | 234 | // TASK 5 |
aoc2 | 0:8940db3353d7 | 235 | void task5_monitorSpeed(void *arg) |
aoc2 | 0:8940db3353d7 | 236 | { |
aoc2 | 0:8940db3353d7 | 237 | carStructure &myCar = *((carStructure *) arg); |
aoc2 | 0:8940db3353d7 | 238 | |
aoc2 | 0:8940db3353d7 | 239 | // Block access on carStat |
aoc2 | 0:8940db3353d7 | 240 | myCar.p_statMutex->lock(); |
aoc2 | 0:8940db3353d7 | 241 | // Get an easier access to the vector storing speed values |
aoc2 | 0:8940db3353d7 | 242 | vector<float> &speedVec = *(myCar.p_carStats->p_speedVector); |
aoc2 | 0:8940db3353d7 | 243 | float lastSpeed = speedVec[SIZE_SPEED_VECTOR - 1]; |
aoc2 | 0:8940db3353d7 | 244 | |
aoc2 | 0:8940db3353d7 | 245 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 246 | myCar.p_statMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 247 | |
aoc2 | 0:8940db3353d7 | 248 | // Deal with the copied value |
aoc2 | 0:8940db3353d7 | 249 | monitorValue(lastSpeed, *(myCar.p_overspeedLight), 141.0/3.6); |
aoc2 | 0:8940db3353d7 | 250 | } |
aoc2 | 0:8940db3353d7 | 251 | |
aoc2 | 0:8940db3353d7 | 252 | // TASK 6 |
aoc2 | 0:8940db3353d7 | 253 | void task6_writeLCD(void *arg) |
aoc2 | 0:8940db3353d7 | 254 | { |
aoc2 | 0:8940db3353d7 | 255 | carStructure &myCar = *((carStructure *) arg); |
aoc2 | 0:8940db3353d7 | 256 | |
aoc2 | 0:8940db3353d7 | 257 | // Block access on carStat |
aoc2 | 0:8940db3353d7 | 258 | myCar.p_statMutex->lock(); |
aoc2 | 0:8940db3353d7 | 259 | |
aoc2 | 0:8940db3353d7 | 260 | carStatistics &carStat = *(myCar.p_carStats); |
aoc2 | 0:8940db3353d7 | 261 | |
aoc2 | 0:8940db3353d7 | 262 | // Copy what we want |
aoc2 | 0:8940db3353d7 | 263 | float avgSpeed = carStat.averageSpeed; |
aoc2 | 0:8940db3353d7 | 264 | float distance = carStat.distance; |
aoc2 | 0:8940db3353d7 | 265 | |
aoc2 | 0:8940db3353d7 | 266 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 267 | myCar.p_statMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 268 | |
aoc2 | 0:8940db3353d7 | 269 | writeOnLCD(*(myCar.p_lcd), distance, avgSpeed); |
aoc2 | 0:8940db3353d7 | 270 | } |
aoc2 | 0:8940db3353d7 | 271 | |
aoc2 | 0:8940db3353d7 | 272 | // TASK 7 |
aoc2 | 0:8940db3353d7 | 273 | void task7_sendToMailQueue(void *arg) |
aoc2 | 0:8940db3353d7 | 274 | { |
aoc2 | 0:8940db3353d7 | 275 | carStructure *myCar = (carStructure *) arg; |
aoc2 | 0:8940db3353d7 | 276 | |
aoc2 | 0:8940db3353d7 | 277 | // Block access on carStat |
aoc2 | 0:8940db3353d7 | 278 | myCar->p_statMutex->lock(); |
aoc2 | 0:8940db3353d7 | 279 | carStatistics &carStat = *(myCar->p_carStats); |
aoc2 | 0:8940db3353d7 | 280 | // Copy what we want |
aoc2 | 0:8940db3353d7 | 281 | float avgSpeed = carStat.averageSpeed; |
aoc2 | 0:8940db3353d7 | 282 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 283 | myCar->p_statMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 284 | |
aoc2 | 0:8940db3353d7 | 285 | // Block access on RAWValues structure |
aoc2 | 0:8940db3353d7 | 286 | myCar->p_rawMutex->lock(); |
aoc2 | 0:8940db3353d7 | 287 | engineRAWValues &p_rawValues = *(myCar->p_rawValues); |
aoc2 | 0:8940db3353d7 | 288 | // Read Brake State |
aoc2 | 0:8940db3353d7 | 289 | float brake = p_rawValues.brakeValue; |
aoc2 | 0:8940db3353d7 | 290 | float accel = p_rawValues.acceleratorValue; |
aoc2 | 0:8940db3353d7 | 291 | // Release the mutex |
aoc2 | 0:8940db3353d7 | 292 | myCar->p_rawMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 293 | |
aoc2 | 0:8940db3353d7 | 294 | // New Queue Object |
aoc2 | 0:8940db3353d7 | 295 | mailStruct *mail = myCar->p_mailQueue->alloc(); |
aoc2 | 0:8940db3353d7 | 296 | // Copy values |
aoc2 | 0:8940db3353d7 | 297 | mail->accelerator = accel; |
aoc2 | 0:8940db3353d7 | 298 | mail->brake = brake; |
aoc2 | 0:8940db3353d7 | 299 | mail->avgSpeed = avgSpeed; |
aoc2 | 0:8940db3353d7 | 300 | // Add to queue |
aoc2 | 0:8940db3353d7 | 301 | myCar->p_mailQueue->put(mail); |
aoc2 | 0:8940db3353d7 | 302 | myCar->p_PC->printf("\r\nPUT in Queue. In: %i\r\n", myCar->nbElementInQueue); |
aoc2 | 0:8940db3353d7 | 303 | |
aoc2 | 0:8940db3353d7 | 304 | // Lock Counter |
aoc2 | 0:8940db3353d7 | 305 | myCar->p_mailMutex->lock(); |
aoc2 | 0:8940db3353d7 | 306 | myCar->nbElementInQueue += 1; |
aoc2 | 0:8940db3353d7 | 307 | // Unlock queue |
aoc2 | 0:8940db3353d7 | 308 | myCar->p_mailMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 309 | } |
aoc2 | 0:8940db3353d7 | 310 | |
aoc2 | 0:8940db3353d7 | 311 | // TASK 8 |
aoc2 | 0:8940db3353d7 | 312 | void task8_dumpMailQueue(void *arg) |
aoc2 | 0:8940db3353d7 | 313 | { |
aoc2 | 0:8940db3353d7 | 314 | carStructure *myCar = (carStructure *) arg; |
aoc2 | 0:8940db3353d7 | 315 | |
aoc2 | 0:8940db3353d7 | 316 | // Lock Mail |
aoc2 | 0:8940db3353d7 | 317 | myCar->p_mailMutex->lock(); |
aoc2 | 0:8940db3353d7 | 318 | int nbOfElement = myCar->nbElementInQueue; |
aoc2 | 0:8940db3353d7 | 319 | // Unlock Counter |
aoc2 | 0:8940db3353d7 | 320 | myCar->p_mailMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 321 | |
aoc2 | 0:8940db3353d7 | 322 | myCar->p_PC->printf("\r\nDumping Queue. In: %i\r\n", nbOfElement); |
aoc2 | 0:8940db3353d7 | 323 | // Go through every element of the queue |
aoc2 | 0:8940db3353d7 | 324 | for (int i = 0; i < nbOfElement ; i++) |
aoc2 | 0:8940db3353d7 | 325 | { |
aoc2 | 0:8940db3353d7 | 326 | // Get Them |
aoc2 | 0:8940db3353d7 | 327 | osEvent evt = myCar->p_mailQueue->get(); |
aoc2 | 0:8940db3353d7 | 328 | if (evt.status == osEventMail) |
aoc2 | 0:8940db3353d7 | 329 | { |
aoc2 | 0:8940db3353d7 | 330 | mailStruct *mail = (mailStruct *)evt.value.p; |
aoc2 | 0:8940db3353d7 | 331 | // Send them to the PC |
aoc2 | 0:8940db3353d7 | 332 | myCar->p_PC->printf("\tElement %i \r\n\t Accel: %.2f\r\n\t", |
aoc2 | 0:8940db3353d7 | 333 | i+1, mail->accelerator); |
aoc2 | 0:8940db3353d7 | 334 | myCar->p_PC->printf(" Brake: %.2f\r\n\t Speed: %.2f\r\n", |
aoc2 | 0:8940db3353d7 | 335 | mail->brake, (mail->avgSpeed)*3.6); |
aoc2 | 0:8940db3353d7 | 336 | |
aoc2 | 0:8940db3353d7 | 337 | myCar->p_mailQueue->free(mail); |
aoc2 | 0:8940db3353d7 | 338 | } |
aoc2 | 0:8940db3353d7 | 339 | } |
aoc2 | 0:8940db3353d7 | 340 | |
aoc2 | 0:8940db3353d7 | 341 | // Lock Counter |
aoc2 | 0:8940db3353d7 | 342 | myCar->p_mailMutex->lock(); |
aoc2 | 0:8940db3353d7 | 343 | // Reset the counter of element in the queue |
aoc2 | 0:8940db3353d7 | 344 | myCar->nbElementInQueue = 0; |
aoc2 | 0:8940db3353d7 | 345 | // Unlock Mail |
aoc2 | 0:8940db3353d7 | 346 | myCar->p_mailMutex->unlock(); |
aoc2 | 0:8940db3353d7 | 347 | } |
aoc2 | 0:8940db3353d7 | 348 | |
aoc2 | 0:8940db3353d7 | 349 | // TASK 9 |
aoc2 | 0:8940db3353d7 | 350 | void task9_readSideLight(void *arg) |
aoc2 | 0:8940db3353d7 | 351 | { |
aoc2 | 0:8940db3353d7 | 352 | carStructure *myCar = (carStructure *) arg; |
aoc2 | 0:8940db3353d7 | 353 | |
aoc2 | 0:8940db3353d7 | 354 | if (myCar->p_sideLightSwitch->read()) |
aoc2 | 0:8940db3353d7 | 355 | { |
aoc2 | 0:8940db3353d7 | 356 | *(myCar->p_sideLightIndicator) = ON; |
aoc2 | 0:8940db3353d7 | 357 | } |
aoc2 | 0:8940db3353d7 | 358 | else |
aoc2 | 0:8940db3353d7 | 359 | { |
aoc2 | 0:8940db3353d7 | 360 | *(myCar->p_sideLightIndicator) = OFF; |
aoc2 | 0:8940db3353d7 | 361 | } |
aoc2 | 0:8940db3353d7 | 362 | } |
aoc2 | 0:8940db3353d7 | 363 | |
aoc2 | 0:8940db3353d7 | 364 | // TASK 10 |
aoc2 | 0:8940db3353d7 | 365 | void task10_turnIndic(void *arg) |
aoc2 | 0:8940db3353d7 | 366 | { |
aoc2 | 0:8940db3353d7 | 367 | carStructure *myCar = (carStructure *) arg; |
aoc2 | 0:8940db3353d7 | 368 | |
aoc2 | 0:8940db3353d7 | 369 | static bool right = false; |
aoc2 | 0:8940db3353d7 | 370 | static bool left = false; |
aoc2 | 0:8940db3353d7 | 371 | |
aoc2 | 0:8940db3353d7 | 372 | // We check if the pins have changed |
aoc2 | 0:8940db3353d7 | 373 | if ( right != myCar->p_rightSwitch->read() |
aoc2 | 0:8940db3353d7 | 374 | || left != myCar->p_leftSwitch->read() ) |
aoc2 | 0:8940db3353d7 | 375 | { |
aoc2 | 0:8940db3353d7 | 376 | // If the state of the switch has changed, we update the PWM frequency |
aoc2 | 0:8940db3353d7 | 377 | // of the LEDs |
aoc2 | 0:8940db3353d7 | 378 | right = myCar->p_rightSwitch->read(); |
aoc2 | 0:8940db3353d7 | 379 | left = myCar->p_leftSwitch->read(); |
aoc2 | 0:8940db3353d7 | 380 | updateLEDs(myCar, right, left); |
aoc2 | 0:8940db3353d7 | 381 | } |
aoc2 | 0:8940db3353d7 | 382 | } |
aoc2 | 0:8940db3353d7 | 383 |