See graph

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Fork of Embedded_Software_Assignment_2 by Steven Kay

Committer:
xouf2114
Date:
Tue Mar 14 02:36:32 2017 +0000
Revision:
11:1069d300847b
Parent:
10:c0531edf4850
Child:
13:4b595e5d443f
change of SD library

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
xouf2114 11:1069d300847b 8 Written by: Xavier Gouesnard
sk398 4:b85bc0d810e1 9
xouf2114 11:1069d300847b 10 Date: March 2017
sk398 4:b85bc0d810e1 11
sk398 10:c0531edf4850 12 Function: This is the main runner containing the Cyclic executive
xouf2114 11:1069d300847b 13 There are 8 defined tasks and several Auxillary components
sk398 10:c0531edf4850 14 which are logically ran periodically at their required time
sk398 10:c0531edf4850 15 by a Cyclic Executive sequencer.
sk398 10:c0531edf4850 16 Ticks, or slots, to this Cyclic Executive are provided by
sk398 10:c0531edf4850 17 a ticker ever 25ms, and then using logical expressions, the
sk398 10:c0531edf4850 18 correct task is initiated and allocated the required time.
sk398 4:b85bc0d810e1 19
sk398 4:b85bc0d810e1 20 ##################################################################### */
sk398 2:22ebabd78084 21
sk398 0:5989ac10c4d3 22 #include "mbed.h"
sk398 1:221d677fe0d3 23 #include "Tasks.h"
sk398 0:5989ac10c4d3 24
sk398 7:2973bf297f3d 25 // ============================================================================
sk398 7:2973bf297f3d 26 // Task Declerations
sk398 7:2973bf297f3d 27 // ============================================================================
sk398 6:ceda53939eb8 28 Task1 task1(p11); // Square wave Measurement
sk398 7:2973bf297f3d 29 Task2 task2_switch1(p12); // Read digital Input
sk398 6:ceda53939eb8 30 Task3 task3(p13); // Watchdog Pulse
sk398 6:ceda53939eb8 31 Task4 task4(p15,p16); // Read analog Inputs
sk398 6:ceda53939eb8 32 Task5 task5(p9,p10,0x40); // Output to LCD Display
sk398 6:ceda53939eb8 33 Task6 task6; // Logical checks
sk398 6:ceda53939eb8 34 Task7 task7(p5,p6,p7,p8,"SD","/SD/A2"); // SD Card Write
xouf2114 11:1069d300847b 35 //Task8 task8; // Shutdown Switch
sk398 0:5989ac10c4d3 36
sk398 7:2973bf297f3d 37 // ============================================================================
sk398 7:2973bf297f3d 38 // Cyclic Executive Objects and Declerations
sk398 7:2973bf297f3d 39 // ============================================================================
sk398 7:2973bf297f3d 40 DigitalOut ErrorLED(LED1); // Error Indicating LED
sk398 10:c0531edf4850 41 DigitalOut LenPin(p25); // Pulse Pin
sk398 10:c0531edf4850 42 DigitalIn SDRemoval(p18); // Switch state to indicate remove SD
sk398 7:2973bf297f3d 43 Ticker CyclicTicker; // Ticker object to cycle tasks
sk398 10:c0531edf4850 44 Timer stampTime; // Timer to stamp the time between logs
sk398 10:c0531edf4850 45
sk398 10:c0531edf4850 46 // NOTE THE FOLLOWING ARE NOT ESSENTIAL TO THE OPERATION OF THIS CYCLIC EXECUTIVE
sk398 10:c0531edf4850 47 Timer task1t; // Timer to calculate task1 timing
sk398 10:c0531edf4850 48 Timer task2t; // Timer to calculate task2 timing
sk398 10:c0531edf4850 49 Timer task3t; // Timer to calculate task3 timing
sk398 10:c0531edf4850 50 Timer task4t; // Timer to calculate task4 timing
sk398 10:c0531edf4850 51 Timer task5t; // Timer to calculate task5 timing
sk398 10:c0531edf4850 52 Timer task6t; // Timer to calculate task6 timing
sk398 7:2973bf297f3d 53
sk398 7:2973bf297f3d 54 // ============================================================================
sk398 7:2973bf297f3d 55 // Global Data Parameters used in Cyclic Executive
sk398 7:2973bf297f3d 56 // ============================================================================
sk398 7:2973bf297f3d 57 // Counter to record the number of ticks went through by the Cyclic Executive
sk398 7:2973bf297f3d 58 int cyclicTicks = 1;
sk398 7:2973bf297f3d 59
sk398 10:c0531edf4850 60 // Flag variable to switch priority between Tasks 2 and 3
sk398 10:c0531edf4850 61 int taskNum = 2;
sk398 10:c0531edf4850 62
sk398 7:2973bf297f3d 63 // Global parameter storing the most up to date value of the return from Task 1
sk398 7:2973bf297f3d 64 volatile int task1Frequency;
sk398 7:2973bf297f3d 65
sk398 7:2973bf297f3d 66 // Global parameter storing the most up to date value of the return from Task 2
sk398 7:2973bf297f3d 67 volatile int task2SwitchState;
sk398 7:2973bf297f3d 68
sk398 7:2973bf297f3d 69 // Global parameter storing the most up to date value of the return from Task 4
sk398 7:2973bf297f3d 70 volatile float task4AnalogChannels[2];
sk398 7:2973bf297f3d 71
sk398 7:2973bf297f3d 72 // Global parameter storing the most up to date value of the return from Task 6
sk398 7:2973bf297f3d 73 volatile int task6ErrorState;
sk398 7:2973bf297f3d 74
sk398 7:2973bf297f3d 75 // Char array to store the concatenated string for output onto the SD Card
sk398 7:2973bf297f3d 76 char logData[50];
sk398 7:2973bf297f3d 77
sk398 7:2973bf297f3d 78 // ============================================================================
sk398 7:2973bf297f3d 79 // Cyclic Executive Function Prototypes
sk398 7:2973bf297f3d 80 // ============================================================================
sk398 7:2973bf297f3d 81 void CyclicExec();
sk398 7:2973bf297f3d 82
sk398 7:2973bf297f3d 83 // ============================================================================
sk398 7:2973bf297f3d 84 // Main Execution Program
sk398 7:2973bf297f3d 85 // ============================================================================
sk398 0:5989ac10c4d3 86 int main() {
sk398 10:c0531edf4850 87
sk398 7:2973bf297f3d 88 // Attempt to open SD file
sk398 7:2973bf297f3d 89 // If open failed, do not run Cyclic Exec
sk398 7:2973bf297f3d 90 if(!task7.openFile("/SD/A2/test.csv","a"))
sk398 7:2973bf297f3d 91 {
sk398 7:2973bf297f3d 92 // Start Cyclic Executive
sk398 7:2973bf297f3d 93 CyclicTicker.attach(&CyclicExec,0.025); // 25ms pulses
sk398 7:2973bf297f3d 94
sk398 7:2973bf297f3d 95 // Keep program running until RESET
sk398 7:2973bf297f3d 96 while(1)
sk398 7:2973bf297f3d 97 {
sk398 7:2973bf297f3d 98 }
sk398 7:2973bf297f3d 99 }
sk398 4:b85bc0d810e1 100
sk398 7:2973bf297f3d 101 // If FIle is not opened, prompt user and show Error on LED
sk398 6:ceda53939eb8 102 else
sk398 6:ceda53939eb8 103 {
sk398 7:2973bf297f3d 104 // Prompt user about error
sk398 7:2973bf297f3d 105 printf("File not opened\r\nNot executing Cyclic Executive");
sk398 7:2973bf297f3d 106
sk398 7:2973bf297f3d 107 // Execute error code on LED
sk398 7:2973bf297f3d 108 while(1)
sk398 7:2973bf297f3d 109 {
sk398 7:2973bf297f3d 110 ErrorLED = 1;
sk398 7:2973bf297f3d 111 wait(1);
sk398 7:2973bf297f3d 112 ErrorLED = 0;
sk398 7:2973bf297f3d 113 wait(1);
sk398 7:2973bf297f3d 114 }
sk398 7:2973bf297f3d 115 }
sk398 7:2973bf297f3d 116 }
sk398 7:2973bf297f3d 117
sk398 7:2973bf297f3d 118 #define TASK1_TICKS 40
sk398 7:2973bf297f3d 119 #define TASK2_TICKS 12
sk398 7:2973bf297f3d 120 #define TASK3_TICKS 12
sk398 7:2973bf297f3d 121 #define TASK4_TICKS 16
sk398 7:2973bf297f3d 122 #define TASK5_TICKS 80
sk398 7:2973bf297f3d 123 #define TASK6_TICKS 32
sk398 7:2973bf297f3d 124 #define TASK7_TICKS 200
sk398 7:2973bf297f3d 125
sk398 7:2973bf297f3d 126 void CyclicExec()
sk398 7:2973bf297f3d 127 {
sk398 10:c0531edf4850 128 // When called, increment cyclicTicks
sk398 7:2973bf297f3d 129 cyclicTicks++;
sk398 10:c0531edf4850 130
sk398 10:c0531edf4850 131 // Run every 1 second (should be 40 ticks, but tuned to 38 through testing)
sk398 10:c0531edf4850 132 if(cyclicTicks % 38 == 0)
sk398 10:c0531edf4850 133 {
sk398 10:c0531edf4850 134 // ---------- Can be removed ---------
sk398 10:c0531edf4850 135 task1t.stop();
sk398 10:c0531edf4850 136 printf("T1 %d\r\n",task1t.read_ms());
sk398 10:c0531edf4850 137 // -----------------------------------
sk398 10:c0531edf4850 138
sk398 10:c0531edf4850 139 // Run Task1
sk398 10:c0531edf4850 140 LenPin = 1;
sk398 10:c0531edf4850 141 task1Frequency = task1.ReadFrequency();
sk398 10:c0531edf4850 142 LenPin = 0;
sk398 10:c0531edf4850 143
sk398 10:c0531edf4850 144 // ---------- Can be removed ---------
sk398 10:c0531edf4850 145 task1t.reset();
sk398 10:c0531edf4850 146 task1t.start();
sk398 10:c0531edf4850 147 // -----------------------------------
sk398 10:c0531edf4850 148 }
sk398 10:c0531edf4850 149
sk398 10:c0531edf4850 150 // Run every 300ms (should be 12 ticks, but logic dictates double the frequency)
sk398 10:c0531edf4850 151 if(cyclicTicks % 6 == 0)
sk398 10:c0531edf4850 152 {
sk398 10:c0531edf4850 153 // If flag taskNum is assigned to run Task2
sk398 10:c0531edf4850 154 if(taskNum == 2)
sk398 10:c0531edf4850 155 {
sk398 10:c0531edf4850 156
sk398 10:c0531edf4850 157 // ---------- Can be removed ---------
sk398 10:c0531edf4850 158 task2t.stop();
sk398 10:c0531edf4850 159 printf("T2 %d\r\n",task2t.read_ms());
sk398 10:c0531edf4850 160 // -----------------------------------
sk398 10:c0531edf4850 161
sk398 10:c0531edf4850 162 // Run Task 2
sk398 10:c0531edf4850 163 task2SwitchState = task2_switch1.digitalInState();
sk398 10:c0531edf4850 164
sk398 10:c0531edf4850 165 // Set flag to run Task 3 on next iteration
sk398 10:c0531edf4850 166 taskNum = 3;
sk398 10:c0531edf4850 167
sk398 10:c0531edf4850 168 // ---------- Can be removed ---------
sk398 10:c0531edf4850 169 task2t.reset();
sk398 10:c0531edf4850 170 task2t.start();
sk398 10:c0531edf4850 171 // -----------------------------------
sk398 10:c0531edf4850 172 }
sk398 10:c0531edf4850 173
sk398 10:c0531edf4850 174 // If flag taskNum is assigned to run Task3
sk398 10:c0531edf4850 175 else if(taskNum == 3)
sk398 10:c0531edf4850 176 {
sk398 10:c0531edf4850 177 // ---------- Can be removed ---------
sk398 10:c0531edf4850 178 task3t.stop();
sk398 10:c0531edf4850 179 printf("T3 %d\r\n",task3t.read_ms());
sk398 10:c0531edf4850 180 // -----------------------------------
sk398 10:c0531edf4850 181
sk398 10:c0531edf4850 182
sk398 10:c0531edf4850 183 // Run Task3
sk398 10:c0531edf4850 184 task3.OutputWatchdogPulse();
sk398 10:c0531edf4850 185
sk398 10:c0531edf4850 186 // Set flag to run Task2 on next iteration
sk398 10:c0531edf4850 187 taskNum = 2;
sk398 10:c0531edf4850 188
sk398 10:c0531edf4850 189 // ---------- Can be removed ---------
sk398 10:c0531edf4850 190 task3t.reset();
sk398 10:c0531edf4850 191 task3t.start();
sk398 10:c0531edf4850 192 // -----------------------------------
sk398 10:c0531edf4850 193 }
sk398 10:c0531edf4850 194 }
sk398 10:c0531edf4850 195
sk398 10:c0531edf4850 196 // Run every 400ms (16 ticks)
sk398 10:c0531edf4850 197 if(cyclicTicks % 16 == 0)
sk398 10:c0531edf4850 198 {
sk398 10:c0531edf4850 199 // ---------- Can be removed ---------
sk398 10:c0531edf4850 200 task4t.stop();
sk398 10:c0531edf4850 201 printf("T4 %d\r\n",task4t.read_ms());
sk398 10:c0531edf4850 202 // -----------------------------------
sk398 10:c0531edf4850 203
sk398 10:c0531edf4850 204 // Run Task4
sk398 10:c0531edf4850 205 float *analogReading = task4.returnAnalogReadings();
sk398 10:c0531edf4850 206 task4AnalogChannels[0] = *(analogReading);
sk398 10:c0531edf4850 207 task4AnalogChannels[1]= *(analogReading+1);
sk398 10:c0531edf4850 208
sk398 10:c0531edf4850 209 // ---------- Can be removed ---------
sk398 10:c0531edf4850 210 task4t.reset();
sk398 10:c0531edf4850 211 task4t.start();
sk398 10:c0531edf4850 212 // -----------------------------------
sk398 10:c0531edf4850 213 }
sk398 10:c0531edf4850 214
sk398 10:c0531edf4850 215 // Run every 2 seconds (should be 80 ticks, but tuned to 84 through testing)
sk398 10:c0531edf4850 216 if(cyclicTicks % 84 == 0)
sk398 10:c0531edf4850 217 {
sk398 10:c0531edf4850 218 // ---------- Can be removed ---------
sk398 10:c0531edf4850 219 task5t.stop();
sk398 10:c0531edf4850 220 printf("T5 %1.2f\r\n",task5t.read());
sk398 10:c0531edf4850 221 // -----------------------------------
sk398 10:c0531edf4850 222
sk398 10:c0531edf4850 223 // Run Task5
sk398 10:c0531edf4850 224 task5.updateDisplay( task1Frequency,
sk398 10:c0531edf4850 225 task2SwitchState,
sk398 10:c0531edf4850 226 task6ErrorState,
sk398 10:c0531edf4850 227 task4AnalogChannels[0],
sk398 10:c0531edf4850 228 task4AnalogChannels[1] );
sk398 10:c0531edf4850 229
sk398 10:c0531edf4850 230 // ---------- Can be removed ---------
sk398 10:c0531edf4850 231 task5t.reset();
sk398 10:c0531edf4850 232 task5t.start();
sk398 10:c0531edf4850 233 // -----------------------------------
sk398 10:c0531edf4850 234 }
sk398 10:c0531edf4850 235
sk398 10:c0531edf4850 236 // Run every 800ms (32 ticks)
sk398 10:c0531edf4850 237 if(cyclicTicks % 32 == 0)
sk398 10:c0531edf4850 238 {
sk398 10:c0531edf4850 239 // ---------- Can be removed ---------
sk398 10:c0531edf4850 240 task6t.stop();
sk398 10:c0531edf4850 241 printf("T6 %d\r\n",task6t.read_ms());
sk398 10:c0531edf4850 242 // -----------------------------------
sk398 10:c0531edf4850 243
sk398 10:c0531edf4850 244 // Run Task6
sk398 10:c0531edf4850 245 task6ErrorState = task6.updateErrorCode( task2SwitchState,
sk398 10:c0531edf4850 246 task4AnalogChannels[0],
sk398 10:c0531edf4850 247 task4AnalogChannels[1] );
sk398 10:c0531edf4850 248
sk398 10:c0531edf4850 249 // ---------- Can be removed ---------
sk398 10:c0531edf4850 250 task6t.reset();
sk398 10:c0531edf4850 251 task6t.start();
sk398 10:c0531edf4850 252 // -----------------------------------
sk398 10:c0531edf4850 253 }
sk398 10:c0531edf4850 254
sk398 10:c0531edf4850 255 // Run every 5 seconds (200 ticks)
sk398 7:2973bf297f3d 256 if(cyclicTicks % 200 == 0)
sk398 7:2973bf297f3d 257 {
sk398 10:c0531edf4850 258 // ---------- Can be removed ---------
sk398 10:c0531edf4850 259 printf("T7\r\n");
sk398 10:c0531edf4850 260 // -----------------------------------
sk398 10:c0531edf4850 261
sk398 10:c0531edf4850 262 // Run Task7
sk398 7:2973bf297f3d 263 stampTime.stop();
sk398 7:2973bf297f3d 264 int a = sprintf( logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
sk398 7:2973bf297f3d 265 stampTime.read(),
sk398 7:2973bf297f3d 266 task1Frequency,
sk398 7:2973bf297f3d 267 task2SwitchState,
sk398 7:2973bf297f3d 268 task4AnalogChannels[0],
sk398 7:2973bf297f3d 269 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 270 task7.writeData(logData);
sk398 10:c0531edf4850 271
sk398 10:c0531edf4850 272 // ---------- Can be removed ---------
sk398 7:2973bf297f3d 273 stampTime.reset();
sk398 7:2973bf297f3d 274 stampTime.start();
sk398 10:c0531edf4850 275 // -----------------------------------
sk398 7:2973bf297f3d 276 }
sk398 7:2973bf297f3d 277
sk398 10:c0531edf4850 278 // If SDRemoval Input is high, close FILE and detach the Ticker
sk398 7:2973bf297f3d 279 if(SDRemoval == HIGH)
sk398 7:2973bf297f3d 280 {
sk398 7:2973bf297f3d 281 printf("SD Removed");
sk398 7:2973bf297f3d 282 task7.closeFile();
sk398 10:c0531edf4850 283 CyclicTicker.detach();
sk398 7:2973bf297f3d 284 }
sk398 7:2973bf297f3d 285 }
sk398 7:2973bf297f3d 286