See graph

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Fork of Embedded_Software_Assignment_2 by Steven Kay

Committer:
xouf2114
Date:
Tue Mar 14 14:02:28 2017 +0000
Revision:
14:f11c30a93736
Parent:
13:4b595e5d443f
Child:
15:c1cf53d91a85
define task_ticks

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
xouf2114 14:f11c30a93736 125 #define TASK8_TICKS 100
sk398 7:2973bf297f3d 126
sk398 7:2973bf297f3d 127 void CyclicExec()
sk398 7:2973bf297f3d 128 {
sk398 10:c0531edf4850 129 // When called, increment cyclicTicks
sk398 7:2973bf297f3d 130 cyclicTicks++;
sk398 10:c0531edf4850 131
sk398 10:c0531edf4850 132 // Run every 1 second (should be 40 ticks, but tuned to 38 through testing)
sk398 10:c0531edf4850 133 if(cyclicTicks % 38 == 0)
xouf2114 13:4b595e5d443f 134 {
sk398 10:c0531edf4850 135 // ---------- Can be removed ---------
sk398 10:c0531edf4850 136 task1t.stop();
sk398 10:c0531edf4850 137 printf("T1 %d\r\n",task1t.read_ms());
sk398 10:c0531edf4850 138 // -----------------------------------
sk398 10:c0531edf4850 139
sk398 10:c0531edf4850 140 // Run Task1
sk398 10:c0531edf4850 141 LenPin = 1;
sk398 10:c0531edf4850 142 task1Frequency = task1.ReadFrequency();
sk398 10:c0531edf4850 143 LenPin = 0;
sk398 10:c0531edf4850 144
sk398 10:c0531edf4850 145 // ---------- Can be removed ---------
sk398 10:c0531edf4850 146 task1t.reset();
sk398 10:c0531edf4850 147 task1t.start();
sk398 10:c0531edf4850 148 // -----------------------------------
sk398 10:c0531edf4850 149 }
sk398 10:c0531edf4850 150
sk398 10:c0531edf4850 151 // Run every 300ms (should be 12 ticks, but logic dictates double the frequency)
sk398 10:c0531edf4850 152 if(cyclicTicks % 6 == 0)
sk398 10:c0531edf4850 153 {
sk398 10:c0531edf4850 154 // If flag taskNum is assigned to run Task2
sk398 10:c0531edf4850 155 if(taskNum == 2)
sk398 10:c0531edf4850 156 {
sk398 10:c0531edf4850 157
sk398 10:c0531edf4850 158 // ---------- Can be removed ---------
sk398 10:c0531edf4850 159 task2t.stop();
sk398 10:c0531edf4850 160 printf("T2 %d\r\n",task2t.read_ms());
sk398 10:c0531edf4850 161 // -----------------------------------
sk398 10:c0531edf4850 162
sk398 10:c0531edf4850 163 // Run Task 2
sk398 10:c0531edf4850 164 task2SwitchState = task2_switch1.digitalInState();
sk398 10:c0531edf4850 165
sk398 10:c0531edf4850 166 // Set flag to run Task 3 on next iteration
sk398 10:c0531edf4850 167 taskNum = 3;
sk398 10:c0531edf4850 168
sk398 10:c0531edf4850 169 // ---------- Can be removed ---------
sk398 10:c0531edf4850 170 task2t.reset();
sk398 10:c0531edf4850 171 task2t.start();
sk398 10:c0531edf4850 172 // -----------------------------------
sk398 10:c0531edf4850 173 }
sk398 10:c0531edf4850 174
sk398 10:c0531edf4850 175 // If flag taskNum is assigned to run Task3
sk398 10:c0531edf4850 176 else if(taskNum == 3)
sk398 10:c0531edf4850 177 {
sk398 10:c0531edf4850 178 // ---------- Can be removed ---------
sk398 10:c0531edf4850 179 task3t.stop();
sk398 10:c0531edf4850 180 printf("T3 %d\r\n",task3t.read_ms());
sk398 10:c0531edf4850 181 // -----------------------------------
sk398 10:c0531edf4850 182
sk398 10:c0531edf4850 183
sk398 10:c0531edf4850 184 // Run Task3
sk398 10:c0531edf4850 185 task3.OutputWatchdogPulse();
sk398 10:c0531edf4850 186
sk398 10:c0531edf4850 187 // Set flag to run Task2 on next iteration
sk398 10:c0531edf4850 188 taskNum = 2;
sk398 10:c0531edf4850 189
sk398 10:c0531edf4850 190 // ---------- Can be removed ---------
sk398 10:c0531edf4850 191 task3t.reset();
sk398 10:c0531edf4850 192 task3t.start();
sk398 10:c0531edf4850 193 // -----------------------------------
sk398 10:c0531edf4850 194 }
sk398 10:c0531edf4850 195 }
sk398 10:c0531edf4850 196
sk398 10:c0531edf4850 197 // Run every 400ms (16 ticks)
sk398 10:c0531edf4850 198 if(cyclicTicks % 16 == 0)
sk398 10:c0531edf4850 199 {
sk398 10:c0531edf4850 200 // ---------- Can be removed ---------
sk398 10:c0531edf4850 201 task4t.stop();
sk398 10:c0531edf4850 202 printf("T4 %d\r\n",task4t.read_ms());
sk398 10:c0531edf4850 203 // -----------------------------------
sk398 10:c0531edf4850 204
sk398 10:c0531edf4850 205 // Run Task4
sk398 10:c0531edf4850 206 float *analogReading = task4.returnAnalogReadings();
sk398 10:c0531edf4850 207 task4AnalogChannels[0] = *(analogReading);
sk398 10:c0531edf4850 208 task4AnalogChannels[1]= *(analogReading+1);
sk398 10:c0531edf4850 209
sk398 10:c0531edf4850 210 // ---------- Can be removed ---------
sk398 10:c0531edf4850 211 task4t.reset();
sk398 10:c0531edf4850 212 task4t.start();
sk398 10:c0531edf4850 213 // -----------------------------------
sk398 10:c0531edf4850 214 }
sk398 10:c0531edf4850 215
sk398 10:c0531edf4850 216 // Run every 2 seconds (should be 80 ticks, but tuned to 84 through testing)
sk398 10:c0531edf4850 217 if(cyclicTicks % 84 == 0)
sk398 10:c0531edf4850 218 {
sk398 10:c0531edf4850 219 // ---------- Can be removed ---------
sk398 10:c0531edf4850 220 task5t.stop();
sk398 10:c0531edf4850 221 printf("T5 %1.2f\r\n",task5t.read());
sk398 10:c0531edf4850 222 // -----------------------------------
sk398 10:c0531edf4850 223
sk398 10:c0531edf4850 224 // Run Task5
sk398 10:c0531edf4850 225 task5.updateDisplay( task1Frequency,
sk398 10:c0531edf4850 226 task2SwitchState,
sk398 10:c0531edf4850 227 task6ErrorState,
sk398 10:c0531edf4850 228 task4AnalogChannels[0],
sk398 10:c0531edf4850 229 task4AnalogChannels[1] );
sk398 10:c0531edf4850 230
sk398 10:c0531edf4850 231 // ---------- Can be removed ---------
sk398 10:c0531edf4850 232 task5t.reset();
sk398 10:c0531edf4850 233 task5t.start();
sk398 10:c0531edf4850 234 // -----------------------------------
sk398 10:c0531edf4850 235 }
sk398 10:c0531edf4850 236
sk398 10:c0531edf4850 237 // Run every 800ms (32 ticks)
sk398 10:c0531edf4850 238 if(cyclicTicks % 32 == 0)
sk398 10:c0531edf4850 239 {
sk398 10:c0531edf4850 240 // ---------- Can be removed ---------
sk398 10:c0531edf4850 241 task6t.stop();
sk398 10:c0531edf4850 242 printf("T6 %d\r\n",task6t.read_ms());
sk398 10:c0531edf4850 243 // -----------------------------------
sk398 10:c0531edf4850 244
sk398 10:c0531edf4850 245 // Run Task6
sk398 10:c0531edf4850 246 task6ErrorState = task6.updateErrorCode( task2SwitchState,
sk398 10:c0531edf4850 247 task4AnalogChannels[0],
sk398 10:c0531edf4850 248 task4AnalogChannels[1] );
sk398 10:c0531edf4850 249
sk398 10:c0531edf4850 250 // ---------- Can be removed ---------
sk398 10:c0531edf4850 251 task6t.reset();
sk398 10:c0531edf4850 252 task6t.start();
sk398 10:c0531edf4850 253 // -----------------------------------
sk398 10:c0531edf4850 254 }
sk398 10:c0531edf4850 255
sk398 10:c0531edf4850 256 // Run every 5 seconds (200 ticks)
sk398 7:2973bf297f3d 257 if(cyclicTicks % 200 == 0)
sk398 7:2973bf297f3d 258 {
sk398 10:c0531edf4850 259 // ---------- Can be removed ---------
sk398 10:c0531edf4850 260 printf("T7\r\n");
sk398 10:c0531edf4850 261 // -----------------------------------
sk398 10:c0531edf4850 262
sk398 10:c0531edf4850 263 // Run Task7
sk398 7:2973bf297f3d 264 stampTime.stop();
sk398 7:2973bf297f3d 265 int a = sprintf( logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
sk398 7:2973bf297f3d 266 stampTime.read(),
sk398 7:2973bf297f3d 267 task1Frequency,
sk398 7:2973bf297f3d 268 task2SwitchState,
sk398 7:2973bf297f3d 269 task4AnalogChannels[0],
sk398 7:2973bf297f3d 270 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 271 task7.writeData(logData);
sk398 10:c0531edf4850 272
sk398 10:c0531edf4850 273 // ---------- Can be removed ---------
sk398 7:2973bf297f3d 274 stampTime.reset();
sk398 7:2973bf297f3d 275 stampTime.start();
sk398 10:c0531edf4850 276 // -----------------------------------
sk398 7:2973bf297f3d 277 }
sk398 7:2973bf297f3d 278
sk398 10:c0531edf4850 279 // If SDRemoval Input is high, close FILE and detach the Ticker
sk398 7:2973bf297f3d 280 if(SDRemoval == HIGH)
sk398 7:2973bf297f3d 281 {
sk398 7:2973bf297f3d 282 printf("SD Removed");
sk398 7:2973bf297f3d 283 task7.closeFile();
sk398 10:c0531edf4850 284 CyclicTicker.detach();
sk398 7:2973bf297f3d 285 }
sk398 7:2973bf297f3d 286 }
sk398 7:2973bf297f3d 287