Example software of using the mbed-rtos to control a simple vehicle's on board computer

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Committer:
sk398
Date:
Tue Mar 29 21:41:55 2016 +0000
Revision:
3:8192bbde17b3
Parent:
2:13a9394ba2e0
Child:
4:b4bcb329a930
Initialise Function implemented to ensure known starting state.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sk398 0:f7d6ed1dfe3e 1 #include "mbed.h"
sk398 0:f7d6ed1dfe3e 2 #include "rtos.h"
sk398 0:f7d6ed1dfe3e 3
sk398 1:cdf851858518 4 #define TRUE 1
sk398 1:cdf851858518 5 #define FALSE 0
sk398 1:cdf851858518 6
sk398 2:13a9394ba2e0 7 #define HIGH 1
sk398 2:13a9394ba2e0 8 #define LOW 0
sk398 2:13a9394ba2e0 9
sk398 2:13a9394ba2e0 10 // ============================================================================
sk398 2:13a9394ba2e0 11 // MBED Pin Assignments
sk398 2:13a9394ba2e0 12 // ============================================================================
sk398 1:cdf851858518 13 AnalogIn Brake(p19);
sk398 0:f7d6ed1dfe3e 14 AnalogIn Accelerometer(p20);
sk398 0:f7d6ed1dfe3e 15
sk398 1:cdf851858518 16 DigitalIn EngineState(p18);
sk398 1:cdf851858518 17 DigitalIn LeftIndicator(p17);
sk398 1:cdf851858518 18 DigitalIn RightIndicator(p16);
sk398 1:cdf851858518 19 DigitalIn SideLightIndicator(p15);
sk398 1:cdf851858518 20
sk398 1:cdf851858518 21 DigitalOut EngineStateInd(LED1);
sk398 1:cdf851858518 22 DigitalOut SideLightInd(LED2);
sk398 1:cdf851858518 23
sk398 1:cdf851858518 24 PwmOut LeftLightInd(LED3);
sk398 1:cdf851858518 25 PwmOut RightLightInd(LED4);
sk398 2:13a9394ba2e0 26 PwmOut OverSpeedInd(p22);
sk398 2:13a9394ba2e0 27
sk398 2:13a9394ba2e0 28 PwmOut AvSpeedWiper(p21);
sk398 2:13a9394ba2e0 29
sk398 2:13a9394ba2e0 30 // ============================================================================
sk398 2:13a9394ba2e0 31 // Global Data Structure Declerations
sk398 2:13a9394ba2e0 32 // ============================================================================
sk398 2:13a9394ba2e0 33 typedef struct
sk398 2:13a9394ba2e0 34 {
sk398 2:13a9394ba2e0 35 bool EngineState;
sk398 2:13a9394ba2e0 36 float RawAccelerometer;
sk398 2:13a9394ba2e0 37 float RawBraking;
sk398 2:13a9394ba2e0 38 } CarRawParams;
sk398 2:13a9394ba2e0 39
sk398 2:13a9394ba2e0 40 Mutex rawParamsMutex;
sk398 2:13a9394ba2e0 41 CarRawParams rawParams;
sk398 2:13a9394ba2e0 42
sk398 2:13a9394ba2e0 43
sk398 2:13a9394ba2e0 44 typedef struct
sk398 2:13a9394ba2e0 45 {
sk398 2:13a9394ba2e0 46 float rawAcceleration;
sk398 2:13a9394ba2e0 47 float rawSpeed;
sk398 2:13a9394ba2e0 48 } CarProcessedParams;
sk398 2:13a9394ba2e0 49
sk398 2:13a9394ba2e0 50 Mutex processedParamsMutex;
sk398 2:13a9394ba2e0 51 CarProcessedParams processedParams;
sk398 2:13a9394ba2e0 52
sk398 2:13a9394ba2e0 53
sk398 2:13a9394ba2e0 54 typedef struct
sk398 2:13a9394ba2e0 55 {
sk398 2:13a9394ba2e0 56 float AverageSpeed;
sk398 2:13a9394ba2e0 57 } CarFilteredParams;
sk398 2:13a9394ba2e0 58
sk398 2:13a9394ba2e0 59 Mutex filteredParamsMutex;
sk398 2:13a9394ba2e0 60 CarFilteredParams filteredParams;
sk398 2:13a9394ba2e0 61
sk398 2:13a9394ba2e0 62
sk398 2:13a9394ba2e0 63 // ============================================================================
sk398 2:13a9394ba2e0 64 // Car Simulation
sk398 2:13a9394ba2e0 65 // ============================================================================
sk398 2:13a9394ba2e0 66
sk398 2:13a9394ba2e0 67 void CarSimulator(void const *arg)
sk398 2:13a9394ba2e0 68 {
sk398 2:13a9394ba2e0 69 rawParamsMutex.lock();
sk398 2:13a9394ba2e0 70 float currentAccelerometer = rawParams.RawAccelerometer;
sk398 2:13a9394ba2e0 71 float currentBrake = rawParams.RawBraking;
sk398 2:13a9394ba2e0 72 bool currentEngineState = rawParams.EngineState;
sk398 2:13a9394ba2e0 73 rawParamsMutex.unlock();
sk398 2:13a9394ba2e0 74
sk398 2:13a9394ba2e0 75 processedParamsMutex.lock();
sk398 2:13a9394ba2e0 76 float currentRawSpeed = processedParams.rawSpeed;
sk398 2:13a9394ba2e0 77 processedParamsMutex.unlock();
sk398 2:13a9394ba2e0 78
sk398 2:13a9394ba2e0 79 if(currentEngineState)
sk398 2:13a9394ba2e0 80 {
sk398 2:13a9394ba2e0 81 if(currentRawSpeed >= 100)
sk398 2:13a9394ba2e0 82 {
sk398 2:13a9394ba2e0 83 processedParamsMutex.lock();
sk398 2:13a9394ba2e0 84 processedParams.rawSpeed = 100;
sk398 2:13a9394ba2e0 85 processedParamsMutex.unlock();
sk398 2:13a9394ba2e0 86 }
sk398 2:13a9394ba2e0 87 else
sk398 2:13a9394ba2e0 88 {
sk398 2:13a9394ba2e0 89 // Car Sim Model
sk398 2:13a9394ba2e0 90 }
sk398 2:13a9394ba2e0 91 }
sk398 2:13a9394ba2e0 92 }
sk398 2:13a9394ba2e0 93
sk398 2:13a9394ba2e0 94
sk398 2:13a9394ba2e0 95 // ============================================================================
sk398 2:13a9394ba2e0 96 // Control System Tasks
sk398 2:13a9394ba2e0 97 // ============================================================================
sk398 2:13a9394ba2e0 98
sk398 2:13a9394ba2e0 99 void Task1_ReadRawData(void const *arg)
sk398 2:13a9394ba2e0 100 {
sk398 2:13a9394ba2e0 101 rawParamsMutex.lock();
sk398 2:13a9394ba2e0 102 rawParams.RawBraking = Brake.read();
sk398 2:13a9394ba2e0 103 rawParams.RawAccelerometer = Accelerometer.read();
sk398 2:13a9394ba2e0 104 rawParamsMutex.unlock();
sk398 2:13a9394ba2e0 105 }
sk398 2:13a9394ba2e0 106
sk398 2:13a9394ba2e0 107
sk398 2:13a9394ba2e0 108
sk398 2:13a9394ba2e0 109 void Task2_ReadEngineState(void const *arg)
sk398 2:13a9394ba2e0 110 {
sk398 2:13a9394ba2e0 111 rawParamsMutex.lock();
sk398 2:13a9394ba2e0 112 bool currentEngineState = rawParams.EngineState;
sk398 2:13a9394ba2e0 113 rawParamsMutex.unlock();
sk398 2:13a9394ba2e0 114
sk398 2:13a9394ba2e0 115 if(currentEngineState)
sk398 2:13a9394ba2e0 116 {
sk398 2:13a9394ba2e0 117 EngineStateInd = HIGH;
sk398 2:13a9394ba2e0 118 }
sk398 2:13a9394ba2e0 119 else
sk398 2:13a9394ba2e0 120 {
sk398 2:13a9394ba2e0 121 EngineStateInd = LOW;
sk398 2:13a9394ba2e0 122 }
sk398 2:13a9394ba2e0 123 }
sk398 2:13a9394ba2e0 124
sk398 2:13a9394ba2e0 125
sk398 2:13a9394ba2e0 126
sk398 2:13a9394ba2e0 127 void Task3_UpdateRCWiper(void const *arg)
sk398 2:13a9394ba2e0 128 {
sk398 2:13a9394ba2e0 129 filteredParamsMutex.lock();
sk398 2:13a9394ba2e0 130 float currentAverageSpeed = filteredParams.AverageSpeed;
sk398 2:13a9394ba2e0 131 filteredParamsMutex.unlock();
sk398 2:13a9394ba2e0 132
sk398 2:13a9394ba2e0 133 AvSpeedWiper.pulsewidth_ms(2*currentAverageSpeed);
sk398 2:13a9394ba2e0 134 }
sk398 2:13a9394ba2e0 135
sk398 0:f7d6ed1dfe3e 136
sk398 0:f7d6ed1dfe3e 137
sk398 1:cdf851858518 138 void Task8_ReadSideLight(void const *arg)
sk398 0:f7d6ed1dfe3e 139 {
sk398 1:cdf851858518 140 if(SideLightIndicator)
sk398 1:cdf851858518 141 {
sk398 1:cdf851858518 142 SideLightInd = TRUE;
sk398 1:cdf851858518 143 }
sk398 1:cdf851858518 144 else
sk398 1:cdf851858518 145 {
sk398 1:cdf851858518 146 SideLightInd = FALSE;
sk398 1:cdf851858518 147 }
sk398 1:cdf851858518 148 }
sk398 0:f7d6ed1dfe3e 149
sk398 2:13a9394ba2e0 150
sk398 2:13a9394ba2e0 151
sk398 1:cdf851858518 152 void Task9_ReadIndicatorLights(void const *arg)
sk398 0:f7d6ed1dfe3e 153 {
sk398 1:cdf851858518 154 // Left Indicator Only
sk398 1:cdf851858518 155 if(LeftIndicator && !RightIndicator)
sk398 1:cdf851858518 156 {
sk398 2:13a9394ba2e0 157 LeftLightInd.period(1.0);
sk398 2:13a9394ba2e0 158 LeftLightInd.pulsewidth(0.5);
sk398 2:13a9394ba2e0 159
sk398 2:13a9394ba2e0 160 RightLightInd.period(1.0);
sk398 2:13a9394ba2e0 161 RightLightInd.pulsewidth(0.0);
sk398 1:cdf851858518 162 }
sk398 1:cdf851858518 163
sk398 1:cdf851858518 164 // Right Indicator Only
sk398 1:cdf851858518 165 else if(!LeftIndicator && RightIndicator)
sk398 0:f7d6ed1dfe3e 166 {
sk398 2:13a9394ba2e0 167 LeftLightInd.period(1.0);
sk398 2:13a9394ba2e0 168 LeftLightInd.pulsewidth(0.0);
sk398 2:13a9394ba2e0 169
sk398 2:13a9394ba2e0 170 RightLightInd.period(1.0);
sk398 2:13a9394ba2e0 171 RightLightInd.pulsewidth(0.5);
sk398 1:cdf851858518 172 }
sk398 1:cdf851858518 173
sk398 1:cdf851858518 174 // Left and Right Indicators
sk398 1:cdf851858518 175 else if(LeftIndicator && RightIndicator)
sk398 1:cdf851858518 176 {
sk398 2:13a9394ba2e0 177 LeftLightInd.period(0.5);
sk398 2:13a9394ba2e0 178 RightLightInd.period(0.5);
sk398 0:f7d6ed1dfe3e 179
sk398 2:13a9394ba2e0 180 LeftLightInd.pulsewidth(0.25);
sk398 2:13a9394ba2e0 181 RightLightInd.pulsewidth(0.25);
sk398 2:13a9394ba2e0 182 }
sk398 2:13a9394ba2e0 183
sk398 2:13a9394ba2e0 184 else
sk398 2:13a9394ba2e0 185 {
sk398 2:13a9394ba2e0 186 LeftLightInd.period(1.0);
sk398 2:13a9394ba2e0 187 LeftLightInd.pulsewidth(0.0);
sk398 2:13a9394ba2e0 188
sk398 2:13a9394ba2e0 189 RightLightInd.period(1.0);
sk398 2:13a9394ba2e0 190 RightLightInd.pulsewidth(0.0);
sk398 0:f7d6ed1dfe3e 191 }
sk398 1:cdf851858518 192 }
sk398 3:8192bbde17b3 193
sk398 3:8192bbde17b3 194
sk398 3:8192bbde17b3 195 void InitSystem()
sk398 3:8192bbde17b3 196 {
sk398 3:8192bbde17b3 197 AvSpeedWiper.period_ms(20);
sk398 3:8192bbde17b3 198
sk398 3:8192bbde17b3 199
sk398 3:8192bbde17b3 200 typedef struct
sk398 3:8192bbde17b3 201 {
sk398 3:8192bbde17b3 202 float AverageSpeed;
sk398 3:8192bbde17b3 203 } CarFilteredParams;
sk398 3:8192bbde17b3 204
sk398 3:8192bbde17b3 205 rawParams.EngineState = 0;
sk398 3:8192bbde17b3 206 rawParams.RawAccelerometer = 0.0;
sk398 3:8192bbde17b3 207 rawParams.RawBraking = 0.0;
sk398 3:8192bbde17b3 208
sk398 3:8192bbde17b3 209 processedParams.rawAcceleration = 0.0;
sk398 3:8192bbde17b3 210 processedParams.rawSpeed = 0.0;
sk398 3:8192bbde17b3 211
sk398 3:8192bbde17b3 212 filteredParams.AverageSpeed = 0.0;
sk398 3:8192bbde17b3 213
sk398 3:8192bbde17b3 214
sk398 3:8192bbde17b3 215
sk398 2:13a9394ba2e0 216 // ============================================================================
sk398 2:13a9394ba2e0 217 // Entry Point Thread
sk398 2:13a9394ba2e0 218 // ============================================================================
sk398 1:cdf851858518 219
sk398 1:cdf851858518 220 int main()
sk398 1:cdf851858518 221 {
sk398 3:8192bbde17b3 222 InitSystem();
sk398 3:8192bbde17b3 223
sk398 2:13a9394ba2e0 224 RtosTimer CarSim(CarSimulator,osTimerPeriodic); Thread::wait(2);
sk398 2:13a9394ba2e0 225
sk398 2:13a9394ba2e0 226 RtosTimer Task1(Task1_ReadRawData,osTimerPeriodic); Thread::wait(2);
sk398 2:13a9394ba2e0 227 RtosTimer Task2(Task2_ReadEngineState,osTimerPeriodic); Thread::wait(2);
sk398 2:13a9394ba2e0 228 RtosTimer Task3(Task3_UpdateRCWiper,osTimerPeriodic); Thread::wait(2);
sk398 2:13a9394ba2e0 229 RtosTimer Task8(Task8_ReadSideLight,osTimerPeriodic); Thread::wait(2);
sk398 2:13a9394ba2e0 230 RtosTimer Task9(Task9_ReadIndicatorLights,osTimerPeriodic); Thread::wait(2);
sk398 2:13a9394ba2e0 231
sk398 2:13a9394ba2e0 232 CarSim.start(50);
sk398 2:13a9394ba2e0 233
sk398 2:13a9394ba2e0 234 Task1.start(100);
sk398 2:13a9394ba2e0 235 Task2.start(500);
sk398 2:13a9394ba2e0 236 Task3.start(1000);
sk398 2:13a9394ba2e0 237
sk398 1:cdf851858518 238
sk398 1:cdf851858518 239 Task8.start(1000);
sk398 1:cdf851858518 240 Task9.start(2000);
sk398 1:cdf851858518 241
sk398 1:cdf851858518 242 Thread::wait(osWaitForever);
sk398 1:cdf851858518 243
sk398 0:f7d6ed1dfe3e 244
sk398 0:f7d6ed1dfe3e 245 }
sk398 0:f7d6ed1dfe3e 246
sk398 1:cdf851858518 247 //
sk398 1:cdf851858518 248 //
sk398 1:cdf851858518 249 //Mutex readvalues;
sk398 1:cdf851858518 250 //Semaphore read_s(1);
sk398 1:cdf851858518 251 //
sk398 1:cdf851858518 252 //Mail<value_t, 100> mail_box;
sk398 1:cdf851858518 253 //
sk398 1:cdf851858518 254 //
sk398 1:cdf851858518 255 //float readValue = 1.0;
sk398 1:cdf851858518 256 //
sk398 1:cdf851858518 257 //typedef struct
sk398 1:cdf851858518 258 //{
sk398 1:cdf851858518 259 // float AccelerationRaw;
sk398 1:cdf851858518 260 // float BrakeRaw;
sk398 1:cdf851858518 261 //} value_t;
sk398 1:cdf851858518 262 //
sk398 1:cdf851858518 263 //
sk398 1:cdf851858518 264 //void readAnalogPins(void const *args)
sk398 1:cdf851858518 265 //{
sk398 1:cdf851858518 266 // while(1)
sk398 1:cdf851858518 267 // {
sk398 1:cdf851858518 268 // value_t *ReadValue = mail_box.alloc();
sk398 1:cdf851858518 269 //
sk398 1:cdf851858518 270 // ReadValue -> AccelerationrRaw = Accelerometer.read();
sk398 1:cdf851858518 271 // ReadValue -> BrakeRaw = Brake.read();
sk398 1:cdf851858518 272 //
sk398 1:cdf851858518 273 // mail_box.put(ReadValue);
sk398 1:cdf851858518 274 // Thread::wait(100);
sk398 1:cdf851858518 275 // }
sk398 1:cdf851858518 276 //
sk398 1:cdf851858518 277 //}
sk398 1:cdf851858518 278 //
sk398 1:cdf851858518 279 //void sendToPC(void const *args)
sk398 1:cdf851858518 280 //{
sk398 1:cdf851858518 281 // while(1)
sk398 1:cdf851858518 282 // {
sk398 1:cdf851858518 283 // osEvent evt = mail_box.get();
sk398 1:cdf851858518 284 //
sk398 1:cdf851858518 285 // if(evt.status == osEventMail)
sk398 1:cdf851858518 286 // {
sk398 1:cdf851858518 287 // value_t *ReadValue = (value_t*)evt.value.p;
sk398 1:cdf851858518 288 // printf("Value: %1.3f\r\n", ReadValue-> vale);
sk398 1:cdf851858518 289 // mail_box.free(ReadValue);
sk398 1:cdf851858518 290 // }
sk398 1:cdf851858518 291 // Thread::wait(5000);
sk398 1:cdf851858518 292 // }
sk398 1:cdf851858518 293 //
sk398 1:cdf851858518 294 //}
sk398 1:cdf851858518 295 //
sk398 1:cdf851858518 296 //int main() {
sk398 1:cdf851858518 297 //
sk398 1:cdf851858518 298 // Thread thread_1(readData);
sk398 1:cdf851858518 299 // Thread thread_2(sendToPC);
sk398 1:cdf851858518 300 //
sk398 1:cdf851858518 301 // while(1) {
sk398 1:cdf851858518 302 //
sk398 1:cdf851858518 303 // myled = 1;
sk398 1:cdf851858518 304 // wait(0.2);
sk398 1:cdf851858518 305 // myled = 0;
sk398 1:cdf851858518 306 // wait(0.2);
sk398 1:cdf851858518 307 // }
sk398 1:cdf851858518 308 //}