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@4:b4bcb329a930, 2016-03-30 (annotated)
- Committer:
- sk398
- Date:
- Wed Mar 30 02:23:10 2016 +0000
- Revision:
- 4:b4bcb329a930
- Parent:
- 3:8192bbde17b3
- Child:
- 5:4f2a072ed289
Car Sim not working - going to replace Queue and Mailpool with array system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:f7d6ed1dfe3e | 1 | #include "mbed.h" |
sk398 | 0:f7d6ed1dfe3e | 2 | #include "rtos.h" |
sk398 | 4:b4bcb329a930 | 3 | #include "MCP23017.h" |
sk398 | 4:b4bcb329a930 | 4 | #include "WattBob_TextLCD.h" |
sk398 | 4:b4bcb329a930 | 5 | |
sk398 | 4:b4bcb329a930 | 6 | #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) |
sk398 | 4:b4bcb329a930 | 7 | |
sk398 | 4:b4bcb329a930 | 8 | #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) |
sk398 | 4:b4bcb329a930 | 9 | |
sk398 | 4:b4bcb329a930 | 10 | MCP23017 *par_port; |
sk398 | 4:b4bcb329a930 | 11 | WattBob_TextLCD *lcd; |
sk398 | 0:f7d6ed1dfe3e | 12 | |
sk398 | 1:cdf851858518 | 13 | #define TRUE 1 |
sk398 | 1:cdf851858518 | 14 | #define FALSE 0 |
sk398 | 1:cdf851858518 | 15 | |
sk398 | 2:13a9394ba2e0 | 16 | #define HIGH 1 |
sk398 | 2:13a9394ba2e0 | 17 | #define LOW 0 |
sk398 | 2:13a9394ba2e0 | 18 | |
sk398 | 2:13a9394ba2e0 | 19 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 20 | // MBED Pin Assignments |
sk398 | 2:13a9394ba2e0 | 21 | // ============================================================================ |
sk398 | 1:cdf851858518 | 22 | AnalogIn Brake(p19); |
sk398 | 0:f7d6ed1dfe3e | 23 | AnalogIn Accelerometer(p20); |
sk398 | 0:f7d6ed1dfe3e | 24 | |
sk398 | 1:cdf851858518 | 25 | DigitalIn EngineState(p18); |
sk398 | 1:cdf851858518 | 26 | DigitalIn LeftIndicator(p17); |
sk398 | 1:cdf851858518 | 27 | DigitalIn RightIndicator(p16); |
sk398 | 1:cdf851858518 | 28 | DigitalIn SideLightIndicator(p15); |
sk398 | 1:cdf851858518 | 29 | |
sk398 | 1:cdf851858518 | 30 | DigitalOut EngineStateInd(LED1); |
sk398 | 1:cdf851858518 | 31 | DigitalOut SideLightInd(LED2); |
sk398 | 1:cdf851858518 | 32 | |
sk398 | 1:cdf851858518 | 33 | PwmOut LeftLightInd(LED3); |
sk398 | 1:cdf851858518 | 34 | PwmOut RightLightInd(LED4); |
sk398 | 2:13a9394ba2e0 | 35 | PwmOut OverSpeedInd(p22); |
sk398 | 2:13a9394ba2e0 | 36 | |
sk398 | 2:13a9394ba2e0 | 37 | PwmOut AvSpeedWiper(p21); |
sk398 | 2:13a9394ba2e0 | 38 | |
sk398 | 4:b4bcb329a930 | 39 | Serial PCConn(USBTX,USBRX); |
sk398 | 4:b4bcb329a930 | 40 | |
sk398 | 4:b4bcb329a930 | 41 | |
sk398 | 2:13a9394ba2e0 | 42 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 43 | // Global Data Structure Declerations |
sk398 | 2:13a9394ba2e0 | 44 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 45 | typedef struct |
sk398 | 2:13a9394ba2e0 | 46 | { |
sk398 | 2:13a9394ba2e0 | 47 | bool EngineState; |
sk398 | 2:13a9394ba2e0 | 48 | float RawAccelerometer; |
sk398 | 2:13a9394ba2e0 | 49 | float RawBraking; |
sk398 | 2:13a9394ba2e0 | 50 | } CarRawParams; |
sk398 | 2:13a9394ba2e0 | 51 | |
sk398 | 2:13a9394ba2e0 | 52 | Mutex rawParamsMutex; |
sk398 | 2:13a9394ba2e0 | 53 | CarRawParams rawParams; |
sk398 | 2:13a9394ba2e0 | 54 | |
sk398 | 2:13a9394ba2e0 | 55 | |
sk398 | 2:13a9394ba2e0 | 56 | typedef struct |
sk398 | 2:13a9394ba2e0 | 57 | { |
sk398 | 4:b4bcb329a930 | 58 | float rawSpeed[4]; |
sk398 | 2:13a9394ba2e0 | 59 | } CarProcessedParams; |
sk398 | 2:13a9394ba2e0 | 60 | |
sk398 | 4:b4bcb329a930 | 61 | |
sk398 | 4:b4bcb329a930 | 62 | Mutex lastCalculatedrawSpeedMutex; |
sk398 | 2:13a9394ba2e0 | 63 | |
sk398 | 2:13a9394ba2e0 | 64 | |
sk398 | 2:13a9394ba2e0 | 65 | typedef struct |
sk398 | 2:13a9394ba2e0 | 66 | { |
sk398 | 2:13a9394ba2e0 | 67 | float AverageSpeed; |
sk398 | 2:13a9394ba2e0 | 68 | } CarFilteredParams; |
sk398 | 2:13a9394ba2e0 | 69 | |
sk398 | 2:13a9394ba2e0 | 70 | Mutex filteredParamsMutex; |
sk398 | 2:13a9394ba2e0 | 71 | CarFilteredParams filteredParams; |
sk398 | 2:13a9394ba2e0 | 72 | |
sk398 | 2:13a9394ba2e0 | 73 | |
sk398 | 4:b4bcb329a930 | 74 | |
sk398 | 2:13a9394ba2e0 | 75 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 76 | // Car Simulation |
sk398 | 2:13a9394ba2e0 | 77 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 78 | |
sk398 | 2:13a9394ba2e0 | 79 | void CarSimulator(void const *arg) |
sk398 | 2:13a9394ba2e0 | 80 | { |
sk398 | 4:b4bcb329a930 | 81 | PCConn.printf("CarSim\r\n"); |
sk398 | 2:13a9394ba2e0 | 82 | rawParamsMutex.lock(); |
sk398 | 2:13a9394ba2e0 | 83 | float currentAccelerometer = rawParams.RawAccelerometer; |
sk398 | 4:b4bcb329a930 | 84 | // PCConn.printf("%f\r\n",currentAccelerometer); |
sk398 | 2:13a9394ba2e0 | 85 | float currentBrake = rawParams.RawBraking; |
sk398 | 2:13a9394ba2e0 | 86 | bool currentEngineState = rawParams.EngineState; |
sk398 | 2:13a9394ba2e0 | 87 | rawParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 88 | |
sk398 | 4:b4bcb329a930 | 89 | lastCalculatedrawSpeedMutex.lock(); |
sk398 | 4:b4bcb329a930 | 90 | float currentRawSpeed = lastCalculatedrawSpeed; |
sk398 | 4:b4bcb329a930 | 91 | lastCalculatedrawSpeedMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 92 | |
sk398 | 2:13a9394ba2e0 | 93 | if(currentEngineState) |
sk398 | 2:13a9394ba2e0 | 94 | { |
sk398 | 2:13a9394ba2e0 | 95 | if(currentRawSpeed >= 100) |
sk398 | 2:13a9394ba2e0 | 96 | { |
sk398 | 4:b4bcb329a930 | 97 | CarProcessedParams *pr\ocessedParams = mpool.alloc(); |
sk398 | 4:b4bcb329a930 | 98 | processedParams -> rawSpeed = 100.0; |
sk398 | 4:b4bcb329a930 | 99 | AvSpeedQueue.put(processedParams); |
sk398 | 4:b4bcb329a930 | 100 | |
sk398 | 4:b4bcb329a930 | 101 | lastCalculatedrawSpeedMutex.lock(); |
sk398 | 4:b4bcb329a930 | 102 | lastCalculatedrawSpeed = 100.0; |
sk398 | 4:b4bcb329a930 | 103 | lastCalculatedrawSpeedMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 104 | } |
sk398 | 2:13a9394ba2e0 | 105 | else |
sk398 | 2:13a9394ba2e0 | 106 | { |
sk398 | 4:b4bcb329a930 | 107 | // Calculate speed in m/s, convert to mph |
sk398 | 4:b4bcb329a930 | 108 | float newSpeed = (currentRawSpeed + ((7*(currentAccelerometer - currentBrake))*0.05)); |
sk398 | 4:b4bcb329a930 | 109 | |
sk398 | 4:b4bcb329a930 | 110 | CarProcessedParams *processedParams = mpool.alloc(); |
sk398 | 4:b4bcb329a930 | 111 | processedParams -> rawSpeed = newSpeed; |
sk398 | 4:b4bcb329a930 | 112 | AvSpeedQueue.put(processedParams); |
sk398 | 4:b4bcb329a930 | 113 | |
sk398 | 4:b4bcb329a930 | 114 | lastCalculatedrawSpeedMutex.lock(); |
sk398 | 4:b4bcb329a930 | 115 | lastCalculatedrawSpeed = newSpeed; |
sk398 | 4:b4bcb329a930 | 116 | lastCalculatedrawSpeedMutex.unlock(); |
sk398 | 4:b4bcb329a930 | 117 | |
sk398 | 2:13a9394ba2e0 | 118 | } |
sk398 | 2:13a9394ba2e0 | 119 | } |
sk398 | 4:b4bcb329a930 | 120 | // If Engine State == OFF, assume Car is stationary |
sk398 | 4:b4bcb329a930 | 121 | else |
sk398 | 4:b4bcb329a930 | 122 | { |
sk398 | 4:b4bcb329a930 | 123 | CarProcessedParams *processedParams = mpool.alloc(); |
sk398 | 4:b4bcb329a930 | 124 | processedParams -> rawSpeed = 0.0; |
sk398 | 4:b4bcb329a930 | 125 | AvSpeedQueue.put(processedParams); |
sk398 | 4:b4bcb329a930 | 126 | |
sk398 | 4:b4bcb329a930 | 127 | lastCalculatedrawSpeedMutex.lock(); |
sk398 | 4:b4bcb329a930 | 128 | lastCalculatedrawSpeed = 0.0; |
sk398 | 4:b4bcb329a930 | 129 | lastCalculatedrawSpeedMutex.unlock(); |
sk398 | 4:b4bcb329a930 | 130 | } |
sk398 | 4:b4bcb329a930 | 131 | |
sk398 | 4:b4bcb329a930 | 132 | PCConn.printf("%f\r\n",lastCalculatedrawSpeed); |
sk398 | 2:13a9394ba2e0 | 133 | } |
sk398 | 2:13a9394ba2e0 | 134 | |
sk398 | 2:13a9394ba2e0 | 135 | |
sk398 | 2:13a9394ba2e0 | 136 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 137 | // Control System Tasks |
sk398 | 2:13a9394ba2e0 | 138 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 139 | |
sk398 | 2:13a9394ba2e0 | 140 | void Task1_ReadRawData(void const *arg) |
sk398 | 2:13a9394ba2e0 | 141 | { |
sk398 | 4:b4bcb329a930 | 142 | PCConn.printf("Task1\r\n"); |
sk398 | 2:13a9394ba2e0 | 143 | rawParamsMutex.lock(); |
sk398 | 2:13a9394ba2e0 | 144 | rawParams.RawBraking = Brake.read(); |
sk398 | 4:b4bcb329a930 | 145 | // PCConn.printf("%f\r\n",rawParams.RawBraking); |
sk398 | 2:13a9394ba2e0 | 146 | rawParams.RawAccelerometer = Accelerometer.read(); |
sk398 | 4:b4bcb329a930 | 147 | // PCConn.printf("%f\r\n",rawParams.RawAccelerometer); |
sk398 | 2:13a9394ba2e0 | 148 | rawParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 149 | } |
sk398 | 2:13a9394ba2e0 | 150 | |
sk398 | 2:13a9394ba2e0 | 151 | |
sk398 | 2:13a9394ba2e0 | 152 | |
sk398 | 2:13a9394ba2e0 | 153 | void Task2_ReadEngineState(void const *arg) |
sk398 | 2:13a9394ba2e0 | 154 | { |
sk398 | 4:b4bcb329a930 | 155 | PCConn.printf("Task2\r\n"); |
sk398 | 4:b4bcb329a930 | 156 | bool currentEngineState = EngineState.read(); |
sk398 | 2:13a9394ba2e0 | 157 | rawParamsMutex.lock(); |
sk398 | 4:b4bcb329a930 | 158 | rawParams.EngineState = currentEngineState; |
sk398 | 2:13a9394ba2e0 | 159 | rawParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 160 | |
sk398 | 2:13a9394ba2e0 | 161 | if(currentEngineState) |
sk398 | 2:13a9394ba2e0 | 162 | { |
sk398 | 2:13a9394ba2e0 | 163 | EngineStateInd = HIGH; |
sk398 | 2:13a9394ba2e0 | 164 | } |
sk398 | 2:13a9394ba2e0 | 165 | else |
sk398 | 2:13a9394ba2e0 | 166 | { |
sk398 | 2:13a9394ba2e0 | 167 | EngineStateInd = LOW; |
sk398 | 2:13a9394ba2e0 | 168 | } |
sk398 | 2:13a9394ba2e0 | 169 | } |
sk398 | 2:13a9394ba2e0 | 170 | |
sk398 | 4:b4bcb329a930 | 171 | void Task3_CalcAvSpeed(void const *arg) |
sk398 | 4:b4bcb329a930 | 172 | { |
sk398 | 4:b4bcb329a930 | 173 | PCConn.printf("Task3\r\n"); |
sk398 | 4:b4bcb329a930 | 174 | float speedTotal = 0.0; |
sk398 | 4:b4bcb329a930 | 175 | for(int num = 0; num < 3; num++) |
sk398 | 4:b4bcb329a930 | 176 | { |
sk398 | 4:b4bcb329a930 | 177 | osEvent evt = AvSpeedQueue.get(); |
sk398 | 4:b4bcb329a930 | 178 | if(evt.status == osEventMessage) |
sk398 | 4:b4bcb329a930 | 179 | { |
sk398 | 4:b4bcb329a930 | 180 | CarProcessedParams *processedParams = (CarProcessedParams*)evt.value.p; |
sk398 | 4:b4bcb329a930 | 181 | speedTotal = speedTotal + processedParams->rawSpeed; |
sk398 | 4:b4bcb329a930 | 182 | PCConn.printf("Total: %f\r\n",speedTotal); |
sk398 | 4:b4bcb329a930 | 183 | mpool.free(processedParams); |
sk398 | 4:b4bcb329a930 | 184 | } |
sk398 | 4:b4bcb329a930 | 185 | } |
sk398 | 2:13a9394ba2e0 | 186 | |
sk398 | 4:b4bcb329a930 | 187 | filteredParamsMutex.lock(); |
sk398 | 4:b4bcb329a930 | 188 | filteredParams.AverageSpeed = (speedTotal/3); |
sk398 | 4:b4bcb329a930 | 189 | filteredParamsMutex.unlock(); |
sk398 | 4:b4bcb329a930 | 190 | } |
sk398 | 4:b4bcb329a930 | 191 | |
sk398 | 4:b4bcb329a930 | 192 | void Task4_UpdateRCWiper(void const *arg) |
sk398 | 2:13a9394ba2e0 | 193 | { |
sk398 | 4:b4bcb329a930 | 194 | PCConn.printf("Task4\r\n"); |
sk398 | 2:13a9394ba2e0 | 195 | filteredParamsMutex.lock(); |
sk398 | 2:13a9394ba2e0 | 196 | float currentAverageSpeed = filteredParams.AverageSpeed; |
sk398 | 2:13a9394ba2e0 | 197 | filteredParamsMutex.unlock(); |
sk398 | 2:13a9394ba2e0 | 198 | |
sk398 | 4:b4bcb329a930 | 199 | AvSpeedWiper.pulsewidth_us(2*currentAverageSpeed); |
sk398 | 2:13a9394ba2e0 | 200 | } |
sk398 | 2:13a9394ba2e0 | 201 | |
sk398 | 0:f7d6ed1dfe3e | 202 | |
sk398 | 0:f7d6ed1dfe3e | 203 | |
sk398 | 4:b4bcb329a930 | 204 | void Task5_OverspeedLED(void const *arg) |
sk398 | 0:f7d6ed1dfe3e | 205 | { |
sk398 | 4:b4bcb329a930 | 206 | PCConn.printf("Task5\r\n"); |
sk398 | 4:b4bcb329a930 | 207 | float currentInstSpeed = 0.0; |
sk398 | 4:b4bcb329a930 | 208 | osEvent evt = AvSpeedQueue.get(); |
sk398 | 4:b4bcb329a930 | 209 | |
sk398 | 4:b4bcb329a930 | 210 | if(evt.status == osEventMessage) |
sk398 | 1:cdf851858518 | 211 | { |
sk398 | 4:b4bcb329a930 | 212 | CarProcessedParams *processedParams = (CarProcessedParams*)evt.value.p; |
sk398 | 4:b4bcb329a930 | 213 | currentInstSpeed = processedParams->rawSpeed; |
sk398 | 4:b4bcb329a930 | 214 | } |
sk398 | 4:b4bcb329a930 | 215 | |
sk398 | 4:b4bcb329a930 | 216 | if(currentInstSpeed > 70.0) |
sk398 | 4:b4bcb329a930 | 217 | { |
sk398 | 4:b4bcb329a930 | 218 | OverSpeedInd = HIGH; |
sk398 | 1:cdf851858518 | 219 | } |
sk398 | 1:cdf851858518 | 220 | else |
sk398 | 1:cdf851858518 | 221 | { |
sk398 | 4:b4bcb329a930 | 222 | OverSpeedInd = LOW; |
sk398 | 4:b4bcb329a930 | 223 | } |
sk398 | 4:b4bcb329a930 | 224 | |
sk398 | 4:b4bcb329a930 | 225 | } |
sk398 | 4:b4bcb329a930 | 226 | |
sk398 | 4:b4bcb329a930 | 227 | |
sk398 | 4:b4bcb329a930 | 228 | void Task9_ReadSideLight(void const *arg) |
sk398 | 4:b4bcb329a930 | 229 | { |
sk398 | 4:b4bcb329a930 | 230 | PCConn.printf("Task9\r\n"); |
sk398 | 4:b4bcb329a930 | 231 | if(SideLightIndicator) |
sk398 | 4:b4bcb329a930 | 232 | { |
sk398 | 4:b4bcb329a930 | 233 | SideLightInd = HIGH; |
sk398 | 4:b4bcb329a930 | 234 | } |
sk398 | 4:b4bcb329a930 | 235 | else |
sk398 | 4:b4bcb329a930 | 236 | { |
sk398 | 4:b4bcb329a930 | 237 | SideLightInd = LOW; |
sk398 | 1:cdf851858518 | 238 | } |
sk398 | 1:cdf851858518 | 239 | } |
sk398 | 0:f7d6ed1dfe3e | 240 | |
sk398 | 2:13a9394ba2e0 | 241 | |
sk398 | 2:13a9394ba2e0 | 242 | |
sk398 | 4:b4bcb329a930 | 243 | void Task10_ReadIndicatorLights(void const *arg) |
sk398 | 0:f7d6ed1dfe3e | 244 | { |
sk398 | 4:b4bcb329a930 | 245 | PCConn.printf("Task10\r\n"); |
sk398 | 1:cdf851858518 | 246 | // Left Indicator Only |
sk398 | 1:cdf851858518 | 247 | if(LeftIndicator && !RightIndicator) |
sk398 | 1:cdf851858518 | 248 | { |
sk398 | 2:13a9394ba2e0 | 249 | LeftLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 250 | LeftLightInd.pulsewidth(0.5); |
sk398 | 2:13a9394ba2e0 | 251 | |
sk398 | 2:13a9394ba2e0 | 252 | RightLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 253 | RightLightInd.pulsewidth(0.0); |
sk398 | 1:cdf851858518 | 254 | } |
sk398 | 1:cdf851858518 | 255 | |
sk398 | 1:cdf851858518 | 256 | // Right Indicator Only |
sk398 | 1:cdf851858518 | 257 | else if(!LeftIndicator && RightIndicator) |
sk398 | 0:f7d6ed1dfe3e | 258 | { |
sk398 | 2:13a9394ba2e0 | 259 | LeftLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 260 | LeftLightInd.pulsewidth(0.0); |
sk398 | 2:13a9394ba2e0 | 261 | |
sk398 | 2:13a9394ba2e0 | 262 | RightLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 263 | RightLightInd.pulsewidth(0.5); |
sk398 | 1:cdf851858518 | 264 | } |
sk398 | 1:cdf851858518 | 265 | |
sk398 | 1:cdf851858518 | 266 | // Left and Right Indicators |
sk398 | 1:cdf851858518 | 267 | else if(LeftIndicator && RightIndicator) |
sk398 | 1:cdf851858518 | 268 | { |
sk398 | 2:13a9394ba2e0 | 269 | LeftLightInd.period(0.5); |
sk398 | 2:13a9394ba2e0 | 270 | RightLightInd.period(0.5); |
sk398 | 0:f7d6ed1dfe3e | 271 | |
sk398 | 2:13a9394ba2e0 | 272 | LeftLightInd.pulsewidth(0.25); |
sk398 | 2:13a9394ba2e0 | 273 | RightLightInd.pulsewidth(0.25); |
sk398 | 2:13a9394ba2e0 | 274 | } |
sk398 | 2:13a9394ba2e0 | 275 | |
sk398 | 2:13a9394ba2e0 | 276 | else |
sk398 | 2:13a9394ba2e0 | 277 | { |
sk398 | 2:13a9394ba2e0 | 278 | LeftLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 279 | LeftLightInd.pulsewidth(0.0); |
sk398 | 2:13a9394ba2e0 | 280 | |
sk398 | 2:13a9394ba2e0 | 281 | RightLightInd.period(1.0); |
sk398 | 2:13a9394ba2e0 | 282 | RightLightInd.pulsewidth(0.0); |
sk398 | 0:f7d6ed1dfe3e | 283 | } |
sk398 | 1:cdf851858518 | 284 | } |
sk398 | 3:8192bbde17b3 | 285 | |
sk398 | 3:8192bbde17b3 | 286 | |
sk398 | 3:8192bbde17b3 | 287 | void InitSystem() |
sk398 | 3:8192bbde17b3 | 288 | { |
sk398 | 4:b4bcb329a930 | 289 | AvSpeedWiper.period_ms(20); |
sk398 | 4:b4bcb329a930 | 290 | |
sk398 | 4:b4bcb329a930 | 291 | par_port->write_bit(1,BL_BIT); // turn LCD backlight ON |
sk398 | 3:8192bbde17b3 | 292 | |
sk398 | 3:8192bbde17b3 | 293 | rawParams.EngineState = 0; |
sk398 | 3:8192bbde17b3 | 294 | rawParams.RawAccelerometer = 0.0; |
sk398 | 3:8192bbde17b3 | 295 | rawParams.RawBraking = 0.0; |
sk398 | 4:b4bcb329a930 | 296 | |
sk398 | 4:b4bcb329a930 | 297 | lastCalculatedrawSpeed = 0.0; |
sk398 | 3:8192bbde17b3 | 298 | |
sk398 | 3:8192bbde17b3 | 299 | filteredParams.AverageSpeed = 0.0; |
sk398 | 4:b4bcb329a930 | 300 | } |
sk398 | 3:8192bbde17b3 | 301 | |
sk398 | 3:8192bbde17b3 | 302 | |
sk398 | 3:8192bbde17b3 | 303 | |
sk398 | 2:13a9394ba2e0 | 304 | // ============================================================================ |
sk398 | 2:13a9394ba2e0 | 305 | // Entry Point Thread |
sk398 | 2:13a9394ba2e0 | 306 | // ============================================================================ |
sk398 | 1:cdf851858518 | 307 | |
sk398 | 1:cdf851858518 | 308 | int main() |
sk398 | 1:cdf851858518 | 309 | { |
sk398 | 4:b4bcb329a930 | 310 | par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip |
sk398 | 4:b4bcb329a930 | 311 | lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display |
sk398 | 4:b4bcb329a930 | 312 | |
sk398 | 4:b4bcb329a930 | 313 | PCConn.baud(115200); |
sk398 | 4:b4bcb329a930 | 314 | |
sk398 | 3:8192bbde17b3 | 315 | InitSystem(); |
sk398 | 3:8192bbde17b3 | 316 | |
sk398 | 4:b4bcb329a930 | 317 | RtosTimer CarSim(CarSimulator,osTimerPeriodic); |
sk398 | 2:13a9394ba2e0 | 318 | |
sk398 | 4:b4bcb329a930 | 319 | RtosTimer Task1(Task1_ReadRawData,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 320 | RtosTimer Task2(Task2_ReadEngineState,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 321 | RtosTimer Task3(Task3_CalcAvSpeed,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 322 | RtosTimer Task4(Task4_UpdateRCWiper,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 323 | RtosTimer Task5(Task5_OverspeedLED,osTimerPeriodic); |
sk398 | 2:13a9394ba2e0 | 324 | |
sk398 | 2:13a9394ba2e0 | 325 | |
sk398 | 1:cdf851858518 | 326 | |
sk398 | 4:b4bcb329a930 | 327 | RtosTimer Task9(Task9_ReadSideLight,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 328 | RtosTimer Task10(Task10_ReadIndicatorLights,osTimerPeriodic); |
sk398 | 4:b4bcb329a930 | 329 | |
sk398 | 4:b4bcb329a930 | 330 | CarSim.start(50); // 20Hz |
sk398 | 4:b4bcb329a930 | 331 | |
sk398 | 4:b4bcb329a930 | 332 | Task1.start(100); // 10Hz |
sk398 | 4:b4bcb329a930 | 333 | Task2.start(500); // 2Hz |
sk398 | 4:b4bcb329a930 | 334 | Task3.start(200); // 5Hz |
sk398 | 4:b4bcb329a930 | 335 | Task4.start(1000); // 1Hz |
sk398 | 4:b4bcb329a930 | 336 | |
sk398 | 4:b4bcb329a930 | 337 | |
sk398 | 4:b4bcb329a930 | 338 | Task9.start(1000); // 1Hz |
sk398 | 4:b4bcb329a930 | 339 | Task10.start(2000);// 0.5Hz |
sk398 | 1:cdf851858518 | 340 | |
sk398 | 1:cdf851858518 | 341 | Thread::wait(osWaitForever); |
sk398 | 1:cdf851858518 | 342 | |
sk398 | 0:f7d6ed1dfe3e | 343 | |
sk398 | 0:f7d6ed1dfe3e | 344 | } |
sk398 | 0:f7d6ed1dfe3e | 345 | |
sk398 | 1:cdf851858518 | 346 | // |
sk398 | 1:cdf851858518 | 347 | // |
sk398 | 1:cdf851858518 | 348 | //Mutex readvalues; |
sk398 | 1:cdf851858518 | 349 | //Semaphore read_s(1); |
sk398 | 1:cdf851858518 | 350 | // |
sk398 | 1:cdf851858518 | 351 | //Mail<value_t, 100> mail_box; |
sk398 | 1:cdf851858518 | 352 | // |
sk398 | 1:cdf851858518 | 353 | // |
sk398 | 1:cdf851858518 | 354 | //float readValue = 1.0; |
sk398 | 1:cdf851858518 | 355 | // |
sk398 | 1:cdf851858518 | 356 | //typedef struct |
sk398 | 1:cdf851858518 | 357 | //{ |
sk398 | 1:cdf851858518 | 358 | // float AccelerationRaw; |
sk398 | 1:cdf851858518 | 359 | // float BrakeRaw; |
sk398 | 1:cdf851858518 | 360 | //} value_t; |
sk398 | 1:cdf851858518 | 361 | // |
sk398 | 1:cdf851858518 | 362 | // |
sk398 | 1:cdf851858518 | 363 | //void readAnalogPins(void const *args) |
sk398 | 1:cdf851858518 | 364 | //{ |
sk398 | 1:cdf851858518 | 365 | // while(1) |
sk398 | 1:cdf851858518 | 366 | // { |
sk398 | 1:cdf851858518 | 367 | // value_t *ReadValue = mail_box.alloc(); |
sk398 | 1:cdf851858518 | 368 | // |
sk398 | 1:cdf851858518 | 369 | // ReadValue -> AccelerationrRaw = Accelerometer.read(); |
sk398 | 1:cdf851858518 | 370 | // ReadValue -> BrakeRaw = Brake.read(); |
sk398 | 1:cdf851858518 | 371 | // |
sk398 | 1:cdf851858518 | 372 | // mail_box.put(ReadValue); |
sk398 | 1:cdf851858518 | 373 | // Thread::wait(100); |
sk398 | 1:cdf851858518 | 374 | // } |
sk398 | 1:cdf851858518 | 375 | // |
sk398 | 1:cdf851858518 | 376 | //} |
sk398 | 1:cdf851858518 | 377 | // |
sk398 | 1:cdf851858518 | 378 | //void sendToPC(void const *args) |
sk398 | 1:cdf851858518 | 379 | //{ |
sk398 | 1:cdf851858518 | 380 | // while(1) |
sk398 | 1:cdf851858518 | 381 | // { |
sk398 | 1:cdf851858518 | 382 | // osEvent evt = mail_box.get(); |
sk398 | 1:cdf851858518 | 383 | // |
sk398 | 1:cdf851858518 | 384 | // if(evt.status == osEventMail) |
sk398 | 1:cdf851858518 | 385 | // { |
sk398 | 1:cdf851858518 | 386 | // value_t *ReadValue = (value_t*)evt.value.p; |
sk398 | 1:cdf851858518 | 387 | // printf("Value: %1.3f\r\n", ReadValue-> vale); |
sk398 | 1:cdf851858518 | 388 | // mail_box.free(ReadValue); |
sk398 | 1:cdf851858518 | 389 | // } |
sk398 | 1:cdf851858518 | 390 | // Thread::wait(5000); |
sk398 | 1:cdf851858518 | 391 | // } |
sk398 | 1:cdf851858518 | 392 | // |
sk398 | 1:cdf851858518 | 393 | //} |
sk398 | 1:cdf851858518 | 394 | // |
sk398 | 1:cdf851858518 | 395 | //int main() { |
sk398 | 1:cdf851858518 | 396 | // |
sk398 | 1:cdf851858518 | 397 | // Thread thread_1(readData); |
sk398 | 1:cdf851858518 | 398 | // Thread thread_2(sendToPC); |
sk398 | 1:cdf851858518 | 399 | // |
sk398 | 1:cdf851858518 | 400 | // while(1) { |
sk398 | 1:cdf851858518 | 401 | // |
sk398 | 1:cdf851858518 | 402 | // myled = 1; |
sk398 | 1:cdf851858518 | 403 | // wait(0.2); |
sk398 | 1:cdf851858518 | 404 | // myled = 0; |
sk398 | 1:cdf851858518 | 405 | // wait(0.2); |
sk398 | 1:cdf851858518 | 406 | // } |
sk398 | 1:cdf851858518 | 407 | //} |