See graph

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Fork of Embedded_Software_Assignment_2 by Steven Kay

Committer:
sk398
Date:
Wed Mar 02 01:03:09 2016 +0000
Revision:
8:7f3594882cec
Parent:
7:2973bf297f3d
Child:
9:46408a8dea0c
All operations now in sync. Small errors with timing however this is tunable;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sk398 4:b85bc0d810e1 1 /* #####################################################################
sk398 4:b85bc0d810e1 2 main.cpp
sk398 4:b85bc0d810e1 3 ---------
sk398 4:b85bc0d810e1 4
sk398 4:b85bc0d810e1 5 Embedded Software - Assignment 2
sk398 4:b85bc0d810e1 6 --------------------------------
sk398 4:b85bc0d810e1 7
sk398 4:b85bc0d810e1 8 Written by: Steven Kay
sk398 4:b85bc0d810e1 9
sk398 4:b85bc0d810e1 10 Date: February 2016
sk398 4:b85bc0d810e1 11
sk398 4:b85bc0d810e1 12 Function: This
sk398 4:b85bc0d810e1 13
sk398 4:b85bc0d810e1 14 Version: 1.0
sk398 4:b85bc0d810e1 15
sk398 4:b85bc0d810e1 16 Version History
sk398 4:b85bc0d810e1 17 ---------------
sk398 4:b85bc0d810e1 18
sk398 4:b85bc0d810e1 19 1.1 rgdfgdfgdfggdfgdg
sk398 4:b85bc0d810e1 20
sk398 4:b85bc0d810e1 21 1.0 gdgddfdddgd
sk398 4:b85bc0d810e1 22
sk398 4:b85bc0d810e1 23 ##################################################################### */
sk398 2:22ebabd78084 24
sk398 0:5989ac10c4d3 25 #include "mbed.h"
sk398 1:221d677fe0d3 26 #include "Tasks.h"
sk398 0:5989ac10c4d3 27
sk398 7:2973bf297f3d 28 // ============================================================================
sk398 7:2973bf297f3d 29 // Task Declerations
sk398 7:2973bf297f3d 30 // ============================================================================
sk398 6:ceda53939eb8 31 Task1 task1(p11); // Square wave Measurement
sk398 7:2973bf297f3d 32 Task2 task2_switch1(p12); // Read digital Input
sk398 6:ceda53939eb8 33 Task3 task3(p13); // Watchdog Pulse
sk398 6:ceda53939eb8 34 Task4 task4(p15,p16); // Read analog Inputs
sk398 6:ceda53939eb8 35 Task5 task5(p9,p10,0x40); // Output to LCD Display
sk398 6:ceda53939eb8 36 Task6 task6; // Logical checks
sk398 6:ceda53939eb8 37 Task7 task7(p5,p6,p7,p8,"SD","/SD/A2"); // SD Card Write
sk398 0:5989ac10c4d3 38
sk398 7:2973bf297f3d 39 // ============================================================================
sk398 7:2973bf297f3d 40 // Cyclic Executive Objects and Declerations
sk398 7:2973bf297f3d 41 // ============================================================================
sk398 7:2973bf297f3d 42 DigitalOut ErrorLED(LED1); // Error Indicating LED
sk398 7:2973bf297f3d 43 DigitalOut LenPin(p21); // Pulse Pin
sk398 7:2973bf297f3d 44 DigitalIn SDRemoval(p18); // Switch state to indicate to remove SD
sk398 7:2973bf297f3d 45 Timer BusyWait; // Wasted time Timer
sk398 7:2973bf297f3d 46 Ticker CyclicTicker; // Ticker object to cycle tasks
sk398 7:2973bf297f3d 47
sk398 7:2973bf297f3d 48 // ============================================================================
sk398 7:2973bf297f3d 49 // Global Data Parameters used in Cyclic Executive
sk398 7:2973bf297f3d 50 // ============================================================================
sk398 7:2973bf297f3d 51 // Counter to record the number of ticks went through by the Cyclic Executive
sk398 7:2973bf297f3d 52 int cyclicTicks = 1;
sk398 7:2973bf297f3d 53
sk398 7:2973bf297f3d 54 // Global parameter storing the most up to date value of the return from Task 1
sk398 7:2973bf297f3d 55 volatile int task1Frequency;
sk398 7:2973bf297f3d 56
sk398 7:2973bf297f3d 57 // Global parameter storing the most up to date value of the return from Task 2
sk398 7:2973bf297f3d 58 volatile int task2SwitchState;
sk398 7:2973bf297f3d 59
sk398 7:2973bf297f3d 60 // Global parameter storing the most up to date value of the return from Task 4
sk398 7:2973bf297f3d 61 volatile float task4AnalogChannels[2];
sk398 7:2973bf297f3d 62
sk398 7:2973bf297f3d 63 // Global parameter storing the most up to date value of the return from Task 6
sk398 7:2973bf297f3d 64 volatile int task6ErrorState;
sk398 7:2973bf297f3d 65
sk398 7:2973bf297f3d 66 // Char array to store the concatenated string for output onto the SD Card
sk398 7:2973bf297f3d 67 char logData[50];
sk398 7:2973bf297f3d 68
sk398 7:2973bf297f3d 69 int slotCounter = 0;
sk398 7:2973bf297f3d 70 int taskNum = 2;
sk398 7:2973bf297f3d 71
sk398 7:2973bf297f3d 72 // ============================================================================
sk398 7:2973bf297f3d 73 // Cyclic Executive Function Prototypes
sk398 7:2973bf297f3d 74 // ============================================================================
sk398 7:2973bf297f3d 75 void CyclicExec();
sk398 7:2973bf297f3d 76
sk398 7:2973bf297f3d 77 // ============================================================================
sk398 7:2973bf297f3d 78 // Main Execution Program
sk398 7:2973bf297f3d 79 // ============================================================================
sk398 0:5989ac10c4d3 80 int main() {
sk398 7:2973bf297f3d 81
sk398 7:2973bf297f3d 82 // Attempt to open SD file
sk398 7:2973bf297f3d 83 // If open failed, do not run Cyclic Exec
sk398 7:2973bf297f3d 84 if(!task7.openFile("/SD/A2/test.csv","a"))
sk398 7:2973bf297f3d 85 {
sk398 7:2973bf297f3d 86 // Start Cyclic Executive
sk398 7:2973bf297f3d 87 CyclicTicker.attach(&CyclicExec,0.025); // 25ms pulses
sk398 7:2973bf297f3d 88
sk398 7:2973bf297f3d 89 // Keep program running until RESET
sk398 7:2973bf297f3d 90 while(1)
sk398 7:2973bf297f3d 91 {
sk398 7:2973bf297f3d 92 }
sk398 7:2973bf297f3d 93 }
sk398 4:b85bc0d810e1 94
sk398 7:2973bf297f3d 95 // If FIle is not opened, prompt user and show Error on LED
sk398 6:ceda53939eb8 96 else
sk398 6:ceda53939eb8 97 {
sk398 7:2973bf297f3d 98 // Prompt user about error
sk398 7:2973bf297f3d 99 printf("File not opened\r\nNot executing Cyclic Executive");
sk398 7:2973bf297f3d 100
sk398 7:2973bf297f3d 101 // Execute error code on LED
sk398 7:2973bf297f3d 102 while(1)
sk398 7:2973bf297f3d 103 {
sk398 7:2973bf297f3d 104 ErrorLED = 1;
sk398 7:2973bf297f3d 105 wait(1);
sk398 7:2973bf297f3d 106 ErrorLED = 0;
sk398 7:2973bf297f3d 107 wait(1);
sk398 7:2973bf297f3d 108 }
sk398 7:2973bf297f3d 109 }
sk398 7:2973bf297f3d 110 }
sk398 7:2973bf297f3d 111
sk398 7:2973bf297f3d 112 Timer stampTime;
sk398 7:2973bf297f3d 113
sk398 7:2973bf297f3d 114 #define TASK1_TICKS 40
sk398 7:2973bf297f3d 115 #define TASK2_TICKS 12
sk398 7:2973bf297f3d 116 #define TASK3_TICKS 12
sk398 7:2973bf297f3d 117 #define TASK4_TICKS 16
sk398 7:2973bf297f3d 118 #define TASK5_TICKS 80
sk398 7:2973bf297f3d 119 #define TASK6_TICKS 32
sk398 7:2973bf297f3d 120 #define TASK7_TICKS 200
sk398 7:2973bf297f3d 121
sk398 7:2973bf297f3d 122 #define HIGH 1
sk398 7:2973bf297f3d 123
sk398 7:2973bf297f3d 124 //void CyclicExec()
sk398 7:2973bf297f3d 125 //{
sk398 7:2973bf297f3d 126 // slotCounter++;
sk398 7:2973bf297f3d 127 // switch(slotCounter)
sk398 7:2973bf297f3d 128 // {
sk398 7:2973bf297f3d 129 // case 1:
sk398 7:2973bf297f3d 130 // if(cyclicTicks % TASK1_TICKS == 0)
sk398 7:2973bf297f3d 131 // {
sk398 7:2973bf297f3d 132 //// printf("T1\r\n");
sk398 7:2973bf297f3d 133 // task1Frequency = task1.ReadFrequency();
sk398 7:2973bf297f3d 134 // }
sk398 7:2973bf297f3d 135 // break;
sk398 7:2973bf297f3d 136 //
sk398 7:2973bf297f3d 137 // case 2:
sk398 7:2973bf297f3d 138 // if(cyclicTicks % TASK2_TICKS == 0)
sk398 7:2973bf297f3d 139 // {
sk398 7:2973bf297f3d 140 //// printf("T2\r\n");
sk398 7:2973bf297f3d 141 // task2SwitchState = task2_switch1.digitalInState();
sk398 7:2973bf297f3d 142 // }
sk398 7:2973bf297f3d 143 // break;
sk398 7:2973bf297f3d 144 //
sk398 7:2973bf297f3d 145 // case 3:
sk398 7:2973bf297f3d 146 // if(cyclicTicks % TASK3_TICKS == 0)
sk398 7:2973bf297f3d 147 // {
sk398 7:2973bf297f3d 148 //// printf("T3 ------ \r\n");
sk398 7:2973bf297f3d 149 // LenPin = 1;
sk398 7:2973bf297f3d 150 // task3.OutputWatchdogPulse();
sk398 7:2973bf297f3d 151 // LenPin = 0;
sk398 7:2973bf297f3d 152 // }
sk398 7:2973bf297f3d 153 // break;
sk398 7:2973bf297f3d 154 //
sk398 7:2973bf297f3d 155 // case 4:
sk398 7:2973bf297f3d 156 // if(cyclicTicks % TASK4_TICKS == 0)
sk398 7:2973bf297f3d 157 // {
sk398 7:2973bf297f3d 158 //// printf("T4\r\n");
sk398 7:2973bf297f3d 159 // float *analogReading = task4.returnAnalogReadings();
sk398 7:2973bf297f3d 160 // task4AnalogChannels[0] = *(analogReading);
sk398 7:2973bf297f3d 161 // task4AnalogChannels[1]= *(analogReading+1);
sk398 7:2973bf297f3d 162 // }
sk398 7:2973bf297f3d 163 // break;
sk398 7:2973bf297f3d 164 //
sk398 7:2973bf297f3d 165 // case 5:
sk398 7:2973bf297f3d 166 // if(cyclicTicks % TASK5_TICKS == 0)
sk398 7:2973bf297f3d 167 // {
sk398 7:2973bf297f3d 168 //// printf("T5\r\n");
sk398 7:2973bf297f3d 169 // task5.updateDisplay( task1Frequency,
sk398 7:2973bf297f3d 170 // task2SwitchState,
sk398 7:2973bf297f3d 171 // task6ErrorState,
sk398 7:2973bf297f3d 172 // task4AnalogChannels[0],
sk398 7:2973bf297f3d 173 // task4AnalogChannels[1] );
sk398 7:2973bf297f3d 174 // }
sk398 7:2973bf297f3d 175 // break;
sk398 7:2973bf297f3d 176 //
sk398 7:2973bf297f3d 177 // case 10:
sk398 7:2973bf297f3d 178 // if(cyclicTicks % TASK6_TICKS == 0)
sk398 7:2973bf297f3d 179 // {
sk398 7:2973bf297f3d 180 //// printf("T6\r\n");
sk398 7:2973bf297f3d 181 // task6ErrorState = task6.updateErrorCode( task2SwitchState,
sk398 7:2973bf297f3d 182 // task4AnalogChannels[0],
sk398 7:2973bf297f3d 183 // task4AnalogChannels[1] );
sk398 7:2973bf297f3d 184 // }
sk398 7:2973bf297f3d 185 // break;
sk398 7:2973bf297f3d 186 //
sk398 7:2973bf297f3d 187 // case 11:
sk398 7:2973bf297f3d 188 // if(cyclicTicks % TASK7_TICKS == 0)
sk398 7:2973bf297f3d 189 // {
sk398 7:2973bf297f3d 190 //// printf("T7\r\n");
sk398 7:2973bf297f3d 191 // int a = sprintf( logData,"Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
sk398 7:2973bf297f3d 192 // task1Frequency,
sk398 7:2973bf297f3d 193 // task2SwitchState,
sk398 7:2973bf297f3d 194 // task4AnalogChannels[0],
sk398 7:2973bf297f3d 195 // task4AnalogChannels[1] );
sk398 7:2973bf297f3d 196 // task7.writeData(logData);
sk398 7:2973bf297f3d 197 // }
sk398 7:2973bf297f3d 198 // break;
sk398 7:2973bf297f3d 199 //
sk398 7:2973bf297f3d 200 // case 12:
sk398 7:2973bf297f3d 201 // slotCounter = 0;
sk398 7:2973bf297f3d 202 // cyclicTicks++;
sk398 7:2973bf297f3d 203 // break;
sk398 7:2973bf297f3d 204 // }
sk398 7:2973bf297f3d 205 // if(SDRemoval == HIGH)
sk398 7:2973bf297f3d 206 // {
sk398 7:2973bf297f3d 207 // printf("Shutting File");
sk398 7:2973bf297f3d 208 // task7.closeFile();
sk398 7:2973bf297f3d 209 // }
sk398 7:2973bf297f3d 210 //
sk398 7:2973bf297f3d 211 //
sk398 7:2973bf297f3d 212 //}
sk398 7:2973bf297f3d 213
sk398 7:2973bf297f3d 214 Timer task1t;
sk398 7:2973bf297f3d 215 Timer task2t;
sk398 7:2973bf297f3d 216 Timer task3t;
sk398 7:2973bf297f3d 217 Timer task4t;
sk398 7:2973bf297f3d 218 Timer task5t;
sk398 7:2973bf297f3d 219 Timer task6t;
sk398 7:2973bf297f3d 220
sk398 7:2973bf297f3d 221 void CyclicExec()
sk398 7:2973bf297f3d 222 {
sk398 7:2973bf297f3d 223 cyclicTicks++;
sk398 7:2973bf297f3d 224 if(cyclicTicks % 200 == 0)
sk398 7:2973bf297f3d 225 {
sk398 7:2973bf297f3d 226 stampTime.stop();
sk398 7:2973bf297f3d 227 printf("T7\r\n");
sk398 7:2973bf297f3d 228 int a = sprintf( logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
sk398 7:2973bf297f3d 229 stampTime.read(),
sk398 7:2973bf297f3d 230 task1Frequency,
sk398 7:2973bf297f3d 231 task2SwitchState,
sk398 7:2973bf297f3d 232 task4AnalogChannels[0],
sk398 7:2973bf297f3d 233 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 234 task7.writeData(logData);
sk398 7:2973bf297f3d 235 stampTime.reset();
sk398 7:2973bf297f3d 236 stampTime.start();
sk398 7:2973bf297f3d 237 }
sk398 7:2973bf297f3d 238
sk398 8:7f3594882cec 239 if(cyclicTicks % 84 == 0)
sk398 7:2973bf297f3d 240 {
sk398 7:2973bf297f3d 241 task5t.stop();
sk398 7:2973bf297f3d 242 printf("T5 %1.2f\r\n",task5t.read());
sk398 7:2973bf297f3d 243 task5.updateDisplay( task1Frequency,
sk398 7:2973bf297f3d 244 task2SwitchState,
sk398 7:2973bf297f3d 245 task6ErrorState,
sk398 7:2973bf297f3d 246 task4AnalogChannels[0],
sk398 7:2973bf297f3d 247 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 248 task5t.reset();
sk398 7:2973bf297f3d 249 task5t.start();
sk398 7:2973bf297f3d 250 }
sk398 7:2973bf297f3d 251
sk398 8:7f3594882cec 252 if(cyclicTicks % 38 == 0)
sk398 7:2973bf297f3d 253 {
sk398 7:2973bf297f3d 254 task1t.stop();
sk398 7:2973bf297f3d 255 printf("T1 %d\r\n",task1t.read_ms());
sk398 7:2973bf297f3d 256 task1Frequency = task1.ReadFrequency();
sk398 7:2973bf297f3d 257 task1t.reset();
sk398 7:2973bf297f3d 258 task1t.start();
sk398 6:ceda53939eb8 259 }
sk398 6:ceda53939eb8 260
sk398 7:2973bf297f3d 261 if(cyclicTicks % 32 == 0)
sk398 7:2973bf297f3d 262 {
sk398 7:2973bf297f3d 263 task6t.stop();
sk398 7:2973bf297f3d 264 printf("T6 %d\r\n",task6t.read_ms());
sk398 7:2973bf297f3d 265 task6ErrorState = task6.updateErrorCode( task2SwitchState,
sk398 7:2973bf297f3d 266 task4AnalogChannels[0],
sk398 7:2973bf297f3d 267 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 268 task6t.reset();
sk398 7:2973bf297f3d 269 task6t.start();
sk398 7:2973bf297f3d 270 }
sk398 7:2973bf297f3d 271
sk398 7:2973bf297f3d 272 if(cyclicTicks % 16 == 0)
sk398 7:2973bf297f3d 273 {
sk398 7:2973bf297f3d 274 task4t.stop();
sk398 7:2973bf297f3d 275 printf("T4 %d\r\n",task4t.read_ms());
sk398 7:2973bf297f3d 276 float *analogReading = task4.returnAnalogReadings();
sk398 7:2973bf297f3d 277 task4AnalogChannels[0] = *(analogReading);
sk398 7:2973bf297f3d 278 task4AnalogChannels[1]= *(analogReading+1);
sk398 7:2973bf297f3d 279 task4t.reset();
sk398 7:2973bf297f3d 280 task4t.start();
sk398 0:5989ac10c4d3 281 }
sk398 7:2973bf297f3d 282
sk398 8:7f3594882cec 283 if(cyclicTicks % 6 == 0)
sk398 7:2973bf297f3d 284 {
sk398 8:7f3594882cec 285 if(taskNum == 2)
sk398 8:7f3594882cec 286 {
sk398 8:7f3594882cec 287 // printf("IM IN TASK 2!!!!!!!!!!!!!!!!!!!!!\r\n");
sk398 8:7f3594882cec 288 task2t.stop();
sk398 8:7f3594882cec 289 printf("T2 %d\r\n",task2t.read_ms());
sk398 8:7f3594882cec 290 task2SwitchState = task2_switch1.digitalInState();
sk398 8:7f3594882cec 291 taskNum = 3;
sk398 8:7f3594882cec 292 task2t.reset();
sk398 8:7f3594882cec 293 task2t.start();
sk398 8:7f3594882cec 294 }
sk398 8:7f3594882cec 295 else if(taskNum == 3)
sk398 8:7f3594882cec 296 {
sk398 8:7f3594882cec 297 // printf("IM IN TASK 3!!!!!!!!!!!!!!!!!!!!!\r\n");
sk398 8:7f3594882cec 298 task3t.stop();
sk398 8:7f3594882cec 299 printf("T3 %d\r\n",task3t.read_ms());
sk398 8:7f3594882cec 300 task3.OutputWatchdogPulse();
sk398 8:7f3594882cec 301 taskNum = 2;
sk398 8:7f3594882cec 302 task3t.reset();
sk398 8:7f3594882cec 303 task3t.start();
sk398 8:7f3594882cec 304 }
sk398 7:2973bf297f3d 305 }
sk398 7:2973bf297f3d 306
sk398 7:2973bf297f3d 307 if(SDRemoval == HIGH)
sk398 7:2973bf297f3d 308 {
sk398 7:2973bf297f3d 309 printf("SD Removed");
sk398 7:2973bf297f3d 310 task7.closeFile();
sk398 7:2973bf297f3d 311 }
sk398 7:2973bf297f3d 312 }
sk398 7:2973bf297f3d 313