Example software of using the mbed-rtos to control a simple vehicle's on board computer
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
main.cpp@7:f09208f9a4f7, 2016-03-30 (annotated)
- Committer:
- sk398
- Date:
- Wed Mar 30 09:35:10 2016 +0000
- Revision:
- 7:f09208f9a4f7
- Parent:
- 6:572b9755f2c1
- Child:
- 8:6fad4bd89240
All functions operating without issue. System fully tested.; ; Commenting to be done
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 7:f09208f9a4f7 | 1 | /* ##################################################################### |
sk398 | 7:f09208f9a4f7 | 2 | main.cpp |
sk398 | 7:f09208f9a4f7 | 3 | --------- |
sk398 | 7:f09208f9a4f7 | 4 | |
sk398 | 7:f09208f9a4f7 | 5 | Embedded Software - Assignment 3 |
sk398 | 7:f09208f9a4f7 | 6 | -------------------------------- |
sk398 | 7:f09208f9a4f7 | 7 | |
sk398 | 7:f09208f9a4f7 | 8 | Written by: Steven Kay |
sk398 | 7:f09208f9a4f7 | 9 | |
sk398 | 7:f09208f9a4f7 | 10 | Date: March 2016 |
sk398 | 7:f09208f9a4f7 | 11 | |
sk398 | 7:f09208f9a4f7 | 12 | Function: This is the main runner containing the Cyclic executive |
sk398 | 7:f09208f9a4f7 | 13 | There are 7 defined tasks and several Auxillary components |
sk398 | 7:f09208f9a4f7 | 14 | which are logically ran periodically at their required time |
sk398 | 7:f09208f9a4f7 | 15 | by a Cyclic Executive sequencer. |
sk398 | 7:f09208f9a4f7 | 16 | Ticks, or slots, to this Cyclic Executive are provided by |
sk398 | 7:f09208f9a4f7 | 17 | a ticker ever 25ms, and then using logical expressions, the |
sk398 | 7:f09208f9a4f7 | 18 | correct task is initiated and allocated the required time. |
sk398 | 7:f09208f9a4f7 | 19 | |
sk398 | 7:f09208f9a4f7 | 20 | ##################################################################### */ |
sk398 | 7:f09208f9a4f7 | 21 | |
sk398 | 0:f7d6ed1dfe3e | 22 | #include "mbed.h" |
sk398 | 0:f7d6ed1dfe3e | 23 | #include "rtos.h" |
sk398 | 4:b4bcb329a930 | 24 | #include "MCP23017.h" |
sk398 | 4:b4bcb329a930 | 25 | #include "WattBob_TextLCD.h" |
sk398 | 4:b4bcb329a930 | 26 | |
sk398 | 7:f09208f9a4f7 | 27 | // ============================================================================ |
sk398 | 7:f09208f9a4f7 | 28 | // Define Statements |
sk398 | 7:f09208f9a4f7 | 29 | // ============================================================================ |
sk398 | 4:b4bcb329a930 | 30 | #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) |
sk398 | 4:b4bcb329a930 | 31 | #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) |
sk398 | 4:b4bcb329a930 | 32 | |
sk398 | 1:cdf851858518 | 33 | #define TRUE 1 |
sk398 | 1:cdf851858518 | 34 | #define FALSE 0 |
sk398 | 1:cdf851858518 | 35 | |
sk398 | 2:13a9394ba2e0 | 36 | #define HIGH 1 |
sk398 | 2:13a9394ba2e0 | 37 | #define LOW 0 |
sk398 | 2:13a9394ba2e0 | 38 | |
sk398 | 2:13a9394ba2e0 | 39 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 40 | // MBED Pin Assignments |
sk398 | 2:13a9394ba2e0 | 41 | // ============================================================================ |
sk398 | 7:f09208f9a4f7 | 42 | |
sk398 | 7:f09208f9a4f7 | 43 | // System Inputs |
sk398 | 7:f09208f9a4f7 | 44 | AnalogIn Brake(p19); // Brake Pedal |
sk398 | 7:f09208f9a4f7 | 45 | AnalogIn Accelerometer(p20); // Accelerator Pedal |
sk398 | 0:f7d6ed1dfe3e | 46 | |
sk398 | 7:f09208f9a4f7 | 47 | DigitalIn EngineState(p18); // Engine State Switch |
sk398 | 7:f09208f9a4f7 | 48 | DigitalIn LeftIndicator(p17); // Left Indicator Switch |
sk398 | 7:f09208f9a4f7 | 49 | DigitalIn RightIndicator(p16); // Right Indicator Switch |
sk398 | 7:f09208f9a4f7 | 50 | DigitalIn SideLightIndicator(p15); // Side Light Indicator |
sk398 | 7:f09208f9a4f7 | 51 | |
sk398 | 1:cdf851858518 | 52 | |
sk398 | 7:f09208f9a4f7 | 53 | // System Outputs |
sk398 | 7:f09208f9a4f7 | 54 | DigitalOut EngineStateInd(LED1); // Engine State LED |
sk398 | 7:f09208f9a4f7 | 55 | DigitalOut SideLightInd(LED2); // Side Light LED |
sk398 | 1:cdf851858518 | 56 | |
sk398 | 7:f09208f9a4f7 | 57 | PwmOut LeftLightInd(LED3); // Left Indicator LED |
sk398 | 7:f09208f9a4f7 | 58 | PwmOut RightLightInd(LED4); // Right Indicator LED |
sk398 | 7:f09208f9a4f7 | 59 | PwmOut OverSpeedInd(p22); // OverSpeed LED |
sk398 | 2:13a9394ba2e0 | 60 | |
sk398 | 7:f09208f9a4f7 | 61 | PwmOut AvSpeedWiper(p21); // Average Speed Wiper |
sk398 | 2:13a9394ba2e0 | 62 | |
sk398 | 7:f09208f9a4f7 | 63 | Serial PCConn(USBTX,USBRX); // Connection to PC |
sk398 | 4:b4bcb329a930 | 64 | |
sk398 | 7:f09208f9a4f7 | 65 | MCP23017 *par_port; // Object pointing to Expander |
sk398 | 7:f09208f9a4f7 | 66 | WattBob_TextLCD *lcd; // LCD Connection |
sk398 | 4:b4bcb329a930 | 67 | |
sk398 | 2:13a9394ba2e0 | 68 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 69 | // Global Data Structure Declerations |
sk398 | 2:13a9394ba2e0 | 70 | // ============================================================================ |
sk398 | 7:f09208f9a4f7 | 71 | |
sk398 | 7:f09208f9a4f7 | 72 | |
sk398 | 2:13a9394ba2e0 | 73 | typedef struct |
sk398 | 2:13a9394ba2e0 | 74 | { |
sk398 | 2:13a9394ba2e0 | 75 | bool EngineState; |
sk398 | 2:13a9394ba2e0 | 76 | float RawAccelerometer; |
sk398 | 2:13a9394ba2e0 | 77 | float RawBraking; |
sk398 | 2:13a9394ba2e0 | 78 | } CarRawParams; |
sk398 | 2:13a9394ba2e0 | 79 | |
sk398 | 2:13a9394ba2e0 | 80 | Mutex rawParamsMutex; |
sk398 | 2:13a9394ba2e0 | 81 | CarRawParams rawParams; |
sk398 | 2:13a9394ba2e0 | 82 | |
sk398 | 2:13a9394ba2e0 | 83 | |
sk398 | 2:13a9394ba2e0 | 84 | typedef struct |
sk398 | 2:13a9394ba2e0 | 85 | { |
sk398 | 5:4f2a072ed289 | 86 | float rawSpeed[3]; |
sk398 | 5:4f2a072ed289 | 87 | int counter; |
sk398 | 5:4f2a072ed289 | 88 | } CarSpeed; |
sk398 | 2:13a9394ba2e0 | 89 | |
sk398 | 4:b4bcb329a930 | 90 | |
sk398 | 5:4f2a072ed289 | 91 | float lastSpeed; |
sk398 | 5:4f2a072ed289 | 92 | Mutex SpeedMutex; |
sk398 | 5:4f2a072ed289 | 93 | CarSpeed speedParams; |
sk398 | 2:13a9394ba2e0 | 94 | |
sk398 | 2:13a9394ba2e0 | 95 | |
sk398 | 2:13a9394ba2e0 | 96 | typedef struct |
sk398 | 2:13a9394ba2e0 | 97 | { |
sk398 | 2:13a9394ba2e0 | 98 | float AverageSpeed; |
sk398 | 2:13a9394ba2e0 | 99 | } CarFilteredParams; |
sk398 | 2:13a9394ba2e0 | 100 | |
sk398 | 2:13a9394ba2e0 | 101 | Mutex filteredParamsMutex; |
sk398 | 2:13a9394ba2e0 | 102 | CarFilteredParams filteredParams; |
sk398 | 2:13a9394ba2e0 | 103 | |
sk398 | 5:4f2a072ed289 | 104 | float totalDistance; |
sk398 | 4:b4bcb329a930 | 105 | |
sk398 | 7:f09208f9a4f7 | 106 | typedef struct |
sk398 | 7:f09208f9a4f7 | 107 | { |
sk398 | 7:f09208f9a4f7 | 108 | float currentAverageSpeed; |
sk398 | 7:f09208f9a4f7 | 109 | float currentAccelerometer; |
sk398 | 7:f09208f9a4f7 | 110 | float currentBraking; |
sk398 | 7:f09208f9a4f7 | 111 | } PCDump_t; |
sk398 | 7:f09208f9a4f7 | 112 | |
sk398 | 7:f09208f9a4f7 | 113 | Mail<PCDump_t,100> Memory_Dump; |
sk398 | 7:f09208f9a4f7 | 114 | |
sk398 | 7:f09208f9a4f7 | 115 | Mutex MailMutex; |
sk398 | 7:f09208f9a4f7 | 116 | int MailQueueCounter; |
sk398 | 7:f09208f9a4f7 | 117 | |
sk398 | 2:13a9394ba2e0 | 118 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 119 | // Car Simulation |
sk398 | 2:13a9394ba2e0 | 120 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 121 | |
sk398 | 2:13a9394ba2e0 | 122 | void CarSimulator(void const *arg) |
sk398 | 2:13a9394ba2e0 | 123 | { |
sk398 | 7:f09208f9a4f7 | 124 | // PCConn.printf("CarSim\r\n"); |
sk398 | 5:4f2a072ed289 | 125 | |
sk398 | 5:4f2a072ed289 | 126 | float newSpeed; |
sk398 | 5:4f2a072ed289 | 127 | |
sk398 | 2:13a9394ba2e0 | 128 | rawParamsMutex.lock(); |
sk398 | 2:13a9394ba2e0 | 129 | float currentAccelerometer = rawParams.RawAccelerometer; |
sk398 | 7:f09208f9a4f7 | 130 | // PCConn.printf("A: %f\r\n",currentAccelerometer); |
sk398 | 2:13a9394ba2e0 | 131 | float currentBrake = rawParams.RawBraking; |
sk398 | 7:f09208f9a4f7 | 132 | // PCConn.printf("B: %f\r\n",currentBrake); |
sk398 | 2:13a9394ba2e0 | 133 | bool currentEngineState = rawParams.EngineState; |
sk398 | 5:4f2a072ed289 | 134 | rawParamsMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 135 | |
sk398 | 5:4f2a072ed289 | 136 | #define MAX_SPEED 100 |
sk398 | 5:4f2a072ed289 | 137 | newSpeed = currentAccelerometer*MAX_SPEED*(1-currentBrake)*currentEngineState; |
sk398 | 2:13a9394ba2e0 | 138 | |
sk398 | 5:4f2a072ed289 | 139 | SpeedMutex.lock(); |
sk398 | 5:4f2a072ed289 | 140 | if(speedParams.counter > 2) |
sk398 | 2:13a9394ba2e0 | 141 | { |
sk398 | 5:4f2a072ed289 | 142 | speedParams.counter = 0; |
sk398 | 4:b4bcb329a930 | 143 | } |
sk398 | 4:b4bcb329a930 | 144 | |
sk398 | 5:4f2a072ed289 | 145 | speedParams.rawSpeed[speedParams.counter] = newSpeed; |
sk398 | 5:4f2a072ed289 | 146 | SpeedMutex.unlock(); |
sk398 | 6:572b9755f2c1 | 147 | |
sk398 | 5:4f2a072ed289 | 148 | speedParams.counter = speedParams.counter++; |
sk398 | 6:572b9755f2c1 | 149 | |
sk398 | 2:13a9394ba2e0 | 150 | } |
sk398 | 2:13a9394ba2e0 | 151 | |
sk398 | 2:13a9394ba2e0 | 152 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 153 | // Control System Tasks |
sk398 | 2:13a9394ba2e0 | 154 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 155 | |
sk398 | 5:4f2a072ed289 | 156 | |
sk398 | 2:13a9394ba2e0 | 157 | void Task1_ReadRawData(void const *arg) |
sk398 | 2:13a9394ba2e0 | 158 | { |
sk398 | 7:f09208f9a4f7 | 159 | // PCConn.printf("Task1\r\n"); |
sk398 | 2:13a9394ba2e0 | 160 | rawParamsMutex.lock(); |
sk398 | 2:13a9394ba2e0 | 161 | rawParams.RawBraking = Brake.read(); |
sk398 | 2:13a9394ba2e0 | 162 | rawParams.RawAccelerometer = Accelerometer.read(); |
sk398 | 2:13a9394ba2e0 | 163 | rawParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 164 | } |
sk398 | 2:13a9394ba2e0 | 165 | |
sk398 | 2:13a9394ba2e0 | 166 | |
sk398 | 2:13a9394ba2e0 | 167 | |
sk398 | 2:13a9394ba2e0 | 168 | void Task2_ReadEngineState(void const *arg) |
sk398 | 2:13a9394ba2e0 | 169 | { |
sk398 | 7:f09208f9a4f7 | 170 | // PCConn.printf("Task2\r\n"); |
sk398 | 4:b4bcb329a930 | 171 | bool currentEngineState = EngineState.read(); |
sk398 | 2:13a9394ba2e0 | 172 | rawParamsMutex.lock(); |
sk398 | 4:b4bcb329a930 | 173 | rawParams.EngineState = currentEngineState; |
sk398 | 2:13a9394ba2e0 | 174 | rawParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 175 | |
sk398 | 2:13a9394ba2e0 | 176 | if(currentEngineState) |
sk398 | 2:13a9394ba2e0 | 177 | { |
sk398 | 2:13a9394ba2e0 | 178 | EngineStateInd = HIGH; |
sk398 | 2:13a9394ba2e0 | 179 | } |
sk398 | 2:13a9394ba2e0 | 180 | else |
sk398 | 2:13a9394ba2e0 | 181 | { |
sk398 | 2:13a9394ba2e0 | 182 | EngineStateInd = LOW; |
sk398 | 2:13a9394ba2e0 | 183 | } |
sk398 | 2:13a9394ba2e0 | 184 | } |
sk398 | 5:4f2a072ed289 | 185 | |
sk398 | 5:4f2a072ed289 | 186 | |
sk398 | 2:13a9394ba2e0 | 187 | |
sk398 | 4:b4bcb329a930 | 188 | void Task3_CalcAvSpeed(void const *arg) |
sk398 | 4:b4bcb329a930 | 189 | { |
sk398 | 7:f09208f9a4f7 | 190 | // PCConn.printf("Task3\r\n"); |
sk398 | 4:b4bcb329a930 | 191 | float speedTotal = 0.0; |
sk398 | 5:4f2a072ed289 | 192 | |
sk398 | 4:b4bcb329a930 | 193 | for(int num = 0; num < 3; num++) |
sk398 | 4:b4bcb329a930 | 194 | { |
sk398 | 5:4f2a072ed289 | 195 | speedTotal = speedTotal + speedParams.rawSpeed[num]; |
sk398 | 7:f09208f9a4f7 | 196 | // PCConn.printf("Total: %f\r\n",speedTotal); |
sk398 | 4:b4bcb329a930 | 197 | } |
sk398 | 2:13a9394ba2e0 | 198 | |
sk398 | 4:b4bcb329a930 | 199 | filteredParamsMutex.lock(); |
sk398 | 4:b4bcb329a930 | 200 | filteredParams.AverageSpeed = (speedTotal/3); |
sk398 | 7:f09208f9a4f7 | 201 | // PCConn.printf("Av: %f\r\n",filteredParams.AverageSpeed); |
sk398 | 4:b4bcb329a930 | 202 | filteredParamsMutex.unlock(); |
sk398 | 4:b4bcb329a930 | 203 | } |
sk398 | 4:b4bcb329a930 | 204 | |
sk398 | 5:4f2a072ed289 | 205 | |
sk398 | 5:4f2a072ed289 | 206 | |
sk398 | 4:b4bcb329a930 | 207 | void Task4_UpdateRCWiper(void const *arg) |
sk398 | 2:13a9394ba2e0 | 208 | { |
sk398 | 7:f09208f9a4f7 | 209 | // PCConn.printf("Task4\r\n"); |
sk398 | 2:13a9394ba2e0 | 210 | filteredParamsMutex.lock(); |
sk398 | 2:13a9394ba2e0 | 211 | float currentAverageSpeed = filteredParams.AverageSpeed; |
sk398 | 2:13a9394ba2e0 | 212 | filteredParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 213 | |
sk398 | 5:4f2a072ed289 | 214 | AvSpeedWiper.pulsewidth_us(1000+(10*currentAverageSpeed)); |
sk398 | 2:13a9394ba2e0 | 215 | } |
sk398 | 2:13a9394ba2e0 | 216 | |
sk398 | 0:f7d6ed1dfe3e | 217 | |
sk398 | 0:f7d6ed1dfe3e | 218 | |
sk398 | 4:b4bcb329a930 | 219 | void Task5_OverspeedLED(void const *arg) |
sk398 | 0:f7d6ed1dfe3e | 220 | { |
sk398 | 7:f09208f9a4f7 | 221 | // PCConn.printf("Task5\r\n"); |
sk398 | 5:4f2a072ed289 | 222 | SpeedMutex.lock(); |
sk398 | 5:4f2a072ed289 | 223 | float currentInstSpeed = speedParams.rawSpeed[speedParams.counter]; |
sk398 | 5:4f2a072ed289 | 224 | SpeedMutex.unlock(); |
sk398 | 4:b4bcb329a930 | 225 | |
sk398 | 4:b4bcb329a930 | 226 | if(currentInstSpeed > 70.0) |
sk398 | 4:b4bcb329a930 | 227 | { |
sk398 | 4:b4bcb329a930 | 228 | OverSpeedInd = HIGH; |
sk398 | 1:cdf851858518 | 229 | } |
sk398 | 1:cdf851858518 | 230 | else |
sk398 | 1:cdf851858518 | 231 | { |
sk398 | 4:b4bcb329a930 | 232 | OverSpeedInd = LOW; |
sk398 | 4:b4bcb329a930 | 233 | } |
sk398 | 4:b4bcb329a930 | 234 | |
sk398 | 4:b4bcb329a930 | 235 | } |
sk398 | 4:b4bcb329a930 | 236 | |
sk398 | 4:b4bcb329a930 | 237 | |
sk398 | 5:4f2a072ed289 | 238 | void Task6_UpdateOdometer(void const *arg) |
sk398 | 6:572b9755f2c1 | 239 | { |
sk398 | 5:4f2a072ed289 | 240 | filteredParamsMutex.lock(); |
sk398 | 5:4f2a072ed289 | 241 | float currentAverageSpeed = filteredParams.AverageSpeed; |
sk398 | 5:4f2a072ed289 | 242 | filteredParamsMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 243 | |
sk398 | 6:572b9755f2c1 | 244 | totalDistance = totalDistance + (currentAverageSpeed*(0.5/3600)); |
sk398 | 6:572b9755f2c1 | 245 | |
sk398 | 5:4f2a072ed289 | 246 | lcd -> cls(); |
sk398 | 5:4f2a072ed289 | 247 | lcd -> locate(0,0); |
sk398 | 5:4f2a072ed289 | 248 | lcd -> printf("Dist: %8.2f",totalDistance); |
sk398 | 5:4f2a072ed289 | 249 | lcd -> locate(1,0); |
sk398 | 5:4f2a072ed289 | 250 | lcd -> printf("Speed: %3.1f mph",currentAverageSpeed); |
sk398 | 5:4f2a072ed289 | 251 | } |
sk398 | 5:4f2a072ed289 | 252 | |
sk398 | 5:4f2a072ed289 | 253 | |
sk398 | 5:4f2a072ed289 | 254 | void Task7_SendToMailQueue(void const *arg) |
sk398 | 5:4f2a072ed289 | 255 | { |
sk398 | 7:f09208f9a4f7 | 256 | filteredParamsMutex.lock(); |
sk398 | 7:f09208f9a4f7 | 257 | float currentAverageSpeed = filteredParams.AverageSpeed; |
sk398 | 7:f09208f9a4f7 | 258 | filteredParamsMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 259 | |
sk398 | 7:f09208f9a4f7 | 260 | rawParamsMutex.lock(); |
sk398 | 7:f09208f9a4f7 | 261 | float currentAccelerometer = rawParams.RawAccelerometer; |
sk398 | 7:f09208f9a4f7 | 262 | float currentBrake = rawParams.RawBraking; |
sk398 | 7:f09208f9a4f7 | 263 | rawParamsMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 264 | |
sk398 | 7:f09208f9a4f7 | 265 | PCDump_t *currentPCDump = Memory_Dump.alloc(); |
sk398 | 7:f09208f9a4f7 | 266 | currentPCDump -> currentAverageSpeed = currentAverageSpeed; |
sk398 | 7:f09208f9a4f7 | 267 | currentPCDump -> currentAccelerometer = currentAccelerometer; |
sk398 | 7:f09208f9a4f7 | 268 | currentPCDump -> currentBraking = currentBrake; |
sk398 | 7:f09208f9a4f7 | 269 | |
sk398 | 7:f09208f9a4f7 | 270 | Memory_Dump.put(currentPCDump); |
sk398 | 7:f09208f9a4f7 | 271 | |
sk398 | 7:f09208f9a4f7 | 272 | MailMutex.lock(); |
sk398 | 7:f09208f9a4f7 | 273 | MailQueueCounter++; |
sk398 | 7:f09208f9a4f7 | 274 | MailMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 275 | } |
sk398 | 5:4f2a072ed289 | 276 | |
sk398 | 5:4f2a072ed289 | 277 | |
sk398 | 5:4f2a072ed289 | 278 | |
sk398 | 5:4f2a072ed289 | 279 | void Task8_DumpSerial(void const *arg) |
sk398 | 5:4f2a072ed289 | 280 | { |
sk398 | 7:f09208f9a4f7 | 281 | MailMutex.lock(); |
sk398 | 7:f09208f9a4f7 | 282 | int currentQueueCounter = MailQueueCounter; |
sk398 | 7:f09208f9a4f7 | 283 | MailMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 284 | |
sk398 | 7:f09208f9a4f7 | 285 | PCConn.printf("Memory Dump\r\n"); |
sk398 | 5:4f2a072ed289 | 286 | |
sk398 | 7:f09208f9a4f7 | 287 | for(int num = 0; num < currentQueueCounter; num++) |
sk398 | 7:f09208f9a4f7 | 288 | { |
sk398 | 7:f09208f9a4f7 | 289 | osEvent evt = Memory_Dump.get(); |
sk398 | 7:f09208f9a4f7 | 290 | if(evt.status == osEventMail) |
sk398 | 7:f09208f9a4f7 | 291 | { |
sk398 | 7:f09208f9a4f7 | 292 | PCDump_t *currentPCDump = (PCDump_t*)evt.value.p; |
sk398 | 7:f09208f9a4f7 | 293 | |
sk398 | 7:f09208f9a4f7 | 294 | PCConn.printf("Av Speed: %f\r\nAcceler: %f\r\nBrake: %f\r\n\r\n", currentPCDump -> currentAverageSpeed, |
sk398 | 7:f09208f9a4f7 | 295 | currentPCDump -> currentAccelerometer, |
sk398 | 7:f09208f9a4f7 | 296 | currentPCDump -> currentBraking); |
sk398 | 7:f09208f9a4f7 | 297 | Memory_Dump.free(currentPCDump); |
sk398 | 7:f09208f9a4f7 | 298 | } |
sk398 | 7:f09208f9a4f7 | 299 | } |
sk398 | 7:f09208f9a4f7 | 300 | |
sk398 | 7:f09208f9a4f7 | 301 | MailMutex.lock(); |
sk398 | 7:f09208f9a4f7 | 302 | MailQueueCounter = 0; |
sk398 | 7:f09208f9a4f7 | 303 | MailMutex.unlock(); |
sk398 | 5:4f2a072ed289 | 304 | } |
sk398 | 5:4f2a072ed289 | 305 | |
sk398 | 5:4f2a072ed289 | 306 | |
sk398 | 5:4f2a072ed289 | 307 | |
sk398 | 4:b4bcb329a930 | 308 | void Task9_ReadSideLight(void const *arg) |
sk398 | 4:b4bcb329a930 | 309 | { |
sk398 | 7:f09208f9a4f7 | 310 | // PCConn.printf("Task9\r\n"); |
sk398 | 4:b4bcb329a930 | 311 | if(SideLightIndicator) |
sk398 | 4:b4bcb329a930 | 312 | { |
sk398 | 4:b4bcb329a930 | 313 | SideLightInd = HIGH; |
sk398 | 4:b4bcb329a930 | 314 | } |
sk398 | 4:b4bcb329a930 | 315 | else |
sk398 | 4:b4bcb329a930 | 316 | { |
sk398 | 4:b4bcb329a930 | 317 | SideLightInd = LOW; |
sk398 | 1:cdf851858518 | 318 | } |
sk398 | 1:cdf851858518 | 319 | } |
sk398 | 0:f7d6ed1dfe3e | 320 | |
sk398 | 2:13a9394ba2e0 | 321 | |
sk398 | 2:13a9394ba2e0 | 322 | |
sk398 | 4:b4bcb329a930 | 323 | void Task10_ReadIndicatorLights(void const *arg) |
sk398 | 0:f7d6ed1dfe3e | 324 | { |
sk398 | 7:f09208f9a4f7 | 325 | // PCConn.printf("Task10\r\n"); |
sk398 | 1:cdf851858518 | 326 | // Left Indicator Only |
sk398 | 1:cdf851858518 | 327 | if(LeftIndicator && !RightIndicator) |
sk398 | 1:cdf851858518 | 328 | { |
sk398 | 2:13a9394ba2e0 | 329 | LeftLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 330 | LeftLightInd.pulsewidth(0.5); |
sk398 | 2:13a9394ba2e0 | 331 | |
sk398 | 2:13a9394ba2e0 | 332 | RightLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 333 | RightLightInd.pulsewidth(0.0); |
sk398 | 1:cdf851858518 | 334 | } |
sk398 | 1:cdf851858518 | 335 | |
sk398 | 1:cdf851858518 | 336 | // Right Indicator Only |
sk398 | 1:cdf851858518 | 337 | else if(!LeftIndicator && RightIndicator) |
sk398 | 0:f7d6ed1dfe3e | 338 | { |
sk398 | 2:13a9394ba2e0 | 339 | LeftLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 340 | LeftLightInd.pulsewidth(0.0); |
sk398 | 2:13a9394ba2e0 | 341 | |
sk398 | 2:13a9394ba2e0 | 342 | RightLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 343 | RightLightInd.pulsewidth(0.5); |
sk398 | 1:cdf851858518 | 344 | } |
sk398 | 1:cdf851858518 | 345 | |
sk398 | 1:cdf851858518 | 346 | // Left and Right Indicators |
sk398 | 1:cdf851858518 | 347 | else if(LeftIndicator && RightIndicator) |
sk398 | 1:cdf851858518 | 348 | { |
sk398 | 2:13a9394ba2e0 | 349 | LeftLightInd.period(0.5); |
sk398 | 2:13a9394ba2e0 | 350 | RightLightInd.period(0.5); |
sk398 | 0:f7d6ed1dfe3e | 351 | |
sk398 | 2:13a9394ba2e0 | 352 | LeftLightInd.pulsewidth(0.25); |
sk398 | 2:13a9394ba2e0 | 353 | RightLightInd.pulsewidth(0.25); |
sk398 | 2:13a9394ba2e0 | 354 | } |
sk398 | 2:13a9394ba2e0 | 355 | |
sk398 | 2:13a9394ba2e0 | 356 | else |
sk398 | 2:13a9394ba2e0 | 357 | { |
sk398 | 2:13a9394ba2e0 | 358 | LeftLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 359 | LeftLightInd.pulsewidth(0.0); |
sk398 | 2:13a9394ba2e0 | 360 | |
sk398 | 2:13a9394ba2e0 | 361 | RightLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 362 | RightLightInd.pulsewidth(0.0); |
sk398 | 0:f7d6ed1dfe3e | 363 | } |
sk398 | 1:cdf851858518 | 364 | } |
sk398 | 3:8192bbde17b3 | 365 | |
sk398 | 3:8192bbde17b3 | 366 | |
sk398 | 3:8192bbde17b3 | 367 | void InitSystem() |
sk398 | 3:8192bbde17b3 | 368 | { |
sk398 | 4:b4bcb329a930 | 369 | AvSpeedWiper.period_ms(20); |
sk398 | 4:b4bcb329a930 | 370 | |
sk398 | 4:b4bcb329a930 | 371 | par_port->write_bit(1,BL_BIT); // turn LCD backlight ON |
sk398 | 3:8192bbde17b3 | 372 | |
sk398 | 3:8192bbde17b3 | 373 | rawParams.EngineState = 0; |
sk398 | 3:8192bbde17b3 | 374 | rawParams.RawAccelerometer = 0.0; |
sk398 | 3:8192bbde17b3 | 375 | rawParams.RawBraking = 0.0; |
sk398 | 4:b4bcb329a930 | 376 | |
sk398 | 5:4f2a072ed289 | 377 | speedParams.counter = 0; |
sk398 | 5:4f2a072ed289 | 378 | speedParams.rawSpeed[0] = 0.0; |
sk398 | 5:4f2a072ed289 | 379 | speedParams.rawSpeed[1] = 0.0; |
sk398 | 5:4f2a072ed289 | 380 | speedParams.rawSpeed[2] = 0.0; |
sk398 | 3:8192bbde17b3 | 381 | |
sk398 | 3:8192bbde17b3 | 382 | filteredParams.AverageSpeed = 0.0; |
sk398 | 6:572b9755f2c1 | 383 | |
sk398 | 6:572b9755f2c1 | 384 | totalDistance = 0.0; |
sk398 | 7:f09208f9a4f7 | 385 | |
sk398 | 7:f09208f9a4f7 | 386 | MailQueueCounter = 0; |
sk398 | 4:b4bcb329a930 | 387 | } |
sk398 | 3:8192bbde17b3 | 388 | |
sk398 | 3:8192bbde17b3 | 389 | |
sk398 | 3:8192bbde17b3 | 390 | |
sk398 | 2:13a9394ba2e0 | 391 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 392 | // Entry Point Thread |
sk398 | 2:13a9394ba2e0 | 393 | // ============================================================================ |
sk398 | 1:cdf851858518 | 394 | |
sk398 | 1:cdf851858518 | 395 | int main() |
sk398 | 1:cdf851858518 | 396 | { |
sk398 | 4:b4bcb329a930 | 397 | par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip |
sk398 | 4:b4bcb329a930 | 398 | lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display |
sk398 | 4:b4bcb329a930 | 399 | |
sk398 | 4:b4bcb329a930 | 400 | PCConn.baud(115200); |
sk398 | 4:b4bcb329a930 | 401 | |
sk398 | 3:8192bbde17b3 | 402 | InitSystem(); |
sk398 | 3:8192bbde17b3 | 403 | |
sk398 | 4:b4bcb329a930 | 404 | RtosTimer CarSim(CarSimulator,osTimerPeriodic); |
sk398 | 2:13a9394ba2e0 | 405 | |
sk398 | 4:b4bcb329a930 | 406 | RtosTimer Task1(Task1_ReadRawData,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 407 | RtosTimer Task2(Task2_ReadEngineState,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 408 | RtosTimer Task3(Task3_CalcAvSpeed,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 409 | RtosTimer Task4(Task4_UpdateRCWiper,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 410 | RtosTimer Task5(Task5_OverspeedLED,osTimerPeriodic); |
sk398 | 5:4f2a072ed289 | 411 | RtosTimer Task6(Task6_UpdateOdometer,osTimerPeriodic); |
sk398 | 5:4f2a072ed289 | 412 | RtosTimer Task7(Task7_SendToMailQueue,osTimerPeriodic); |
sk398 | 5:4f2a072ed289 | 413 | RtosTimer Task8(Task8_DumpSerial,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 414 | RtosTimer Task9(Task9_ReadSideLight,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 415 | RtosTimer Task10(Task10_ReadIndicatorLights,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 416 | |
sk398 | 5:4f2a072ed289 | 417 | CarSim.start(50); // 20Hz |
sk398 | 5:4f2a072ed289 | 418 | Task1.start(100); // 10Hz |
sk398 | 5:4f2a072ed289 | 419 | Task2.start(500); // 2Hz |
sk398 | 5:4f2a072ed289 | 420 | Task3.start(200); // 5Hz |
sk398 | 5:4f2a072ed289 | 421 | Task4.start(1000); // 1Hz |
sk398 | 5:4f2a072ed289 | 422 | Task5.start(2000); // 0.5Hz |
sk398 | 5:4f2a072ed289 | 423 | Task6.start(500); // 2Hz |
sk398 | 5:4f2a072ed289 | 424 | Task7.start(5000); // 0.2Hz |
sk398 | 5:4f2a072ed289 | 425 | Task8.start(20000); // 0.05Hz |
sk398 | 5:4f2a072ed289 | 426 | Task9.start(1000); // 1Hz |
sk398 | 5:4f2a072ed289 | 427 | Task10.start(2000); // 0.5Hz |
sk398 | 1:cdf851858518 | 428 | |
sk398 | 1:cdf851858518 | 429 | Thread::wait(osWaitForever); |
sk398 | 1:cdf851858518 | 430 | |
sk398 | 0:f7d6ed1dfe3e | 431 | |
sk398 | 0:f7d6ed1dfe3e | 432 | } |