Steven Kay / Mbed 2 deprecated Embedded_Software_Assignment_2

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* #####################################################################
00002                                main.cpp
00003                                ---------
00004                 
00005                      Embedded Software - Assignment 2
00006                      --------------------------------
00007  
00008  Written by:        Steven Kay
00009  
00010  Date:              February 2016
00011  
00012  Function:          This is the main runner containing the Cyclic executive
00013                     There are 7 defined tasks and several Auxillary components
00014                     which are logically ran periodically at their required time
00015                     by a Cyclic Executive sequencer.
00016                     Ticks, or slots, to this Cyclic Executive are provided by
00017                     a ticker ever 25ms, and then using logical expressions, the
00018                     correct task is initiated and allocated the required time.
00019  
00020  ##################################################################### */
00021 
00022 #include "mbed.h"
00023 #include "Tasks.h"
00024 
00025 // ============================================================================
00026 // Task Declerations
00027 // ============================================================================
00028 Task1 task1(p11);                               // Square wave Measurement
00029 Task2 task2_switch1(p12);                       // Read digital Input
00030 Task3 task3(p13);                               // Watchdog Pulse
00031 Task4 task4(p15,p16);                           // Read analog Inputs
00032 Task5 task5(p9,p10,0x40);                       // Output to LCD Display
00033 Task6 task6;                                    // Logical checks
00034 Task7 task7(p5,p6,p7,p8,"SD","/SD/A2");         // SD Card Write
00035 
00036 // ============================================================================
00037 // Cyclic Executive Objects and Declerations
00038 // ============================================================================
00039 DigitalOut ErrorLED(LED1);                      // Error Indicating LED
00040 DigitalOut LenPin(p25);                         // Pulse Pin
00041 DigitalIn SDRemoval(p18);                       // Switch state to indicate remove SD
00042 Ticker CyclicTicker;                            // Ticker object to cycle tasks
00043 Timer stampTime;                                // Timer to stamp the time between logs
00044 
00045 // NOTE THE FOLLOWING ARE NOT ESSENTIAL TO THE OPERATION OF THIS CYCLIC EXECUTIVE
00046 Timer task1t;                                   // Timer to calculate task1 timing
00047 Timer task2t;                                   // Timer to calculate task2 timing
00048 Timer task3t;                                   // Timer to calculate task3 timing
00049 Timer task4t;                                   // Timer to calculate task4 timing
00050 Timer task5t;                                   // Timer to calculate task5 timing
00051 Timer task6t;                                   // Timer to calculate task6 timing
00052 
00053 // ============================================================================
00054 // Global Data Parameters used in Cyclic Executive
00055 // ============================================================================
00056 // Counter to record the number of ticks went through by the Cyclic Executive
00057 int cyclicTicks = 1;
00058 
00059 // Flag variable to switch priority between Tasks 2 and 3
00060 int taskNum = 2;
00061 
00062 // Global parameter storing the most up to date value of the return from Task 1
00063 volatile int task1Frequency;
00064 
00065 // Global parameter storing the most up to date value of the return from Task 2
00066 volatile int task2SwitchState;
00067 
00068 // Global parameter storing the most up to date value of the return from Task 4
00069 volatile float task4AnalogChannels[2];
00070 
00071 // Global parameter storing the most up to date value of the return from Task 6
00072 volatile int task6ErrorState;
00073 
00074 // Char array to store the concatenated string for output onto the SD Card
00075 char logData[50];
00076 
00077 // ============================================================================
00078 // Cyclic Executive Function Prototypes
00079 // ============================================================================
00080 void CyclicExec();
00081 
00082 // ============================================================================
00083 // Main Execution Program
00084 // ============================================================================
00085 int main() {
00086     
00087     // Attempt to open SD file
00088     // If open failed, do not run Cyclic Exec
00089     if(!task7.openFile("/SD/A2/test.csv","a"))
00090     {    
00091         // Start Cyclic Executive 
00092         CyclicTicker.attach(&CyclicExec,0.025); // 25ms pulses
00093         
00094         // Keep program running until RESET
00095         while(1)
00096         {
00097         }
00098     }
00099     
00100     // If FIle is not opened, prompt user and show Error on LED
00101     else
00102     {
00103         // Prompt user about error
00104         printf("File not opened\r\nNot executing Cyclic Executive");
00105         
00106         // Execute error code on LED
00107         while(1)
00108         {
00109             ErrorLED = 1;
00110             wait(1);
00111             ErrorLED = 0;
00112             wait(1);
00113         }
00114     }
00115 }
00116 
00117 #define TASK1_TICKS 40
00118 #define TASK2_TICKS 12
00119 #define TASK3_TICKS 12
00120 #define TASK4_TICKS 16
00121 #define TASK5_TICKS 80
00122 #define TASK6_TICKS 32
00123 #define TASK7_TICKS 200
00124 
00125 void CyclicExec()
00126 {
00127     // When called, increment cyclicTicks
00128     cyclicTicks++;
00129     
00130     // Run every 1 second (should be 40 ticks, but tuned to 38 through testing)
00131     if(cyclicTicks % 38 == 0)
00132     {
00133         // ---------- Can be removed ---------
00134         task1t.stop();
00135         printf("T1 %d\r\n",task1t.read_ms());
00136         // -----------------------------------
00137 
00138         // Run Task1
00139         LenPin = 1;
00140         task1Frequency = task1.ReadFrequency();
00141         LenPin = 0;
00142         
00143         // ---------- Can be removed ---------
00144         task1t.reset();
00145         task1t.start();
00146         // -----------------------------------
00147     }
00148     
00149     // Run every 300ms (should be 12 ticks, but logic dictates double the frequency)
00150     if(cyclicTicks % 6 == 0)
00151     {
00152         // If flag taskNum is assigned to run Task2
00153         if(taskNum == 2)
00154         {
00155             
00156             // ---------- Can be removed ---------
00157             task2t.stop();
00158             printf("T2 %d\r\n",task2t.read_ms());
00159             // -----------------------------------
00160 
00161             // Run Task 2
00162             task2SwitchState = task2_switch1.digitalInState();
00163             
00164             // Set flag to run Task 3 on next iteration
00165             taskNum = 3;
00166             
00167             // ---------- Can be removed ---------
00168             task2t.reset();
00169             task2t.start();
00170             // -----------------------------------
00171         }
00172         
00173         // If flag taskNum is assigned to run Task3
00174         else if(taskNum == 3)
00175         {
00176             // ---------- Can be removed ---------
00177             task3t.stop();
00178             printf("T3 %d\r\n",task3t.read_ms());
00179             // -----------------------------------
00180 
00181             
00182             // Run Task3
00183             task3.OutputWatchdogPulse();
00184             
00185             // Set flag to run Task2 on next iteration
00186             taskNum = 2;
00187             
00188             // ---------- Can be removed ---------
00189             task3t.reset();
00190             task3t.start();
00191             // -----------------------------------
00192         }
00193     }
00194     
00195     // Run every 400ms (16 ticks)
00196     if(cyclicTicks % 16 == 0)
00197     {
00198         // ---------- Can be removed ---------
00199         task4t.stop();
00200         printf("T4 %d\r\n",task4t.read_ms());
00201         // -----------------------------------
00202         
00203         // Run Task4
00204         float *analogReading = task4.returnAnalogReadings();
00205         task4AnalogChannels[0] = *(analogReading);
00206         task4AnalogChannels[1]= *(analogReading+1);
00207         
00208         // ---------- Can be removed ---------
00209         task4t.reset();
00210         task4t.start();
00211         // -----------------------------------
00212     }
00213     
00214     // Run every 2 seconds (should be 80 ticks, but tuned to 84 through testing)
00215     if(cyclicTicks % 84 == 0)
00216     {
00217         // ---------- Can be removed ---------
00218         task5t.stop();
00219         printf("T5 %1.2f\r\n",task5t.read());
00220         // -----------------------------------
00221         
00222         // Run Task5
00223         task5.updateDisplay(    task1Frequency,
00224                                 task2SwitchState,
00225                                 task6ErrorState,
00226                                 task4AnalogChannels[0],
00227                                 task4AnalogChannels[1]  );
00228         
00229         // ---------- Can be removed ---------
00230         task5t.reset();
00231         task5t.start();  
00232         // -----------------------------------                 
00233     }
00234     
00235     // Run every 800ms (32 ticks)
00236     if(cyclicTicks % 32 == 0)
00237     {
00238         // ---------- Can be removed ---------
00239         task6t.stop();
00240         printf("T6 %d\r\n",task6t.read_ms());
00241         // -----------------------------------
00242 
00243         // Run Task6
00244         task6ErrorState = task6.updateErrorCode(    task2SwitchState,
00245                                                     task4AnalogChannels[0],
00246                                                     task4AnalogChannels[1]  );
00247         
00248         // ---------- Can be removed ---------
00249         task6t.reset();
00250         task6t.start();
00251         // -----------------------------------
00252     }
00253     
00254     // Run every 5 seconds (200 ticks)
00255     if(cyclicTicks % 200 == 0)
00256     {
00257         // ---------- Can be removed ---------
00258         printf("T7\r\n");
00259         // -----------------------------------
00260         
00261         // Run Task7
00262         stampTime.stop();
00263         int a = sprintf(    logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
00264                             stampTime.read(),
00265                             task1Frequency,
00266                             task2SwitchState,
00267                             task4AnalogChannels[0],
00268                             task4AnalogChannels[1]  );
00269        task7.writeData(logData);
00270        
00271         // ---------- Can be removed ---------
00272        stampTime.reset();
00273        stampTime.start();
00274         // -----------------------------------
00275     }
00276     
00277     // If SDRemoval Input is high, close FILE and detach the Ticker
00278     if(SDRemoval == HIGH)
00279     {
00280         printf("SD Removed");
00281         task7.closeFile();
00282         CyclicTicker.detach();
00283     }
00284 }
00285