Xavier Gouesnard / Mbed 2 deprecated Embedded_Software_Assignment_2

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Fork of Embedded_Software_Assignment_2 by Steven Kay

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