Example software for a Cyclic Executive
Dependencies: MCP23017 SDFileSystem WattBob_TextLCD mbed
Diff: main.cpp
- Revision:
- 7:2973bf297f3d
- Parent:
- 6:ceda53939eb8
- Child:
- 8:7f3594882cec
diff -r ceda53939eb8 -r 2973bf297f3d main.cpp --- a/main.cpp Mon Feb 29 11:20:48 2016 +0000 +++ b/main.cpp Wed Mar 02 00:51:17 2016 +0000 @@ -24,69 +24,288 @@ #include "mbed.h" #include "Tasks.h" -#include "MCP23017.h" -#include "WattBob_TextLCD.h" -#define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) -#define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) - -DigitalOut myled(LED1); - +// ============================================================================ +// Task Declerations +// ============================================================================ Task1 task1(p11); // Square wave Measurement -Task2 task2_switch1(p12); // Read digital Output +Task2 task2_switch1(p12); // Read digital Input Task3 task3(p13); // Watchdog Pulse Task4 task4(p15,p16); // Read analog Inputs Task5 task5(p9,p10,0x40); // Output to LCD Display Task6 task6; // Logical checks Task7 task7(p5,p6,p7,p8,"SD","/SD/A2"); // SD Card Write +// ============================================================================ +// Cyclic Executive Objects and Declerations +// ============================================================================ +DigitalOut ErrorLED(LED1); // Error Indicating LED +DigitalOut LenPin(p21); // Pulse Pin +DigitalIn SDRemoval(p18); // Switch state to indicate to remove SD +Timer BusyWait; // Wasted time Timer +Ticker CyclicTicker; // Ticker object to cycle tasks + +// ============================================================================ +// Global Data Parameters used in Cyclic Executive +// ============================================================================ +// Counter to record the number of ticks went through by the Cyclic Executive +int cyclicTicks = 1; + +// Global parameter storing the most up to date value of the return from Task 1 +volatile int task1Frequency; + +// Global parameter storing the most up to date value of the return from Task 2 +volatile int task2SwitchState; + +// Global parameter storing the most up to date value of the return from Task 4 +volatile float task4AnalogChannels[2]; + +// Global parameter storing the most up to date value of the return from Task 6 +volatile int task6ErrorState; + +// Char array to store the concatenated string for output onto the SD Card +char logData[50]; + +int slotCounter = 0; +int taskNum = 2; + +// ============================================================================ +// Cyclic Executive Function Prototypes +// ============================================================================ +void CyclicExec(); + +// ============================================================================ +// Main Execution Program +// ============================================================================ int main() { - - volatile int task1Frequency; - volatile int task2SwitchState; - volatile float task4AnalogChannels[2]; - volatile int errorState; - char logData[50]; - - task1Frequency = task1.ReadFrequency(); -// printf("Task 1 Frequency %d Hz\r\n",task1Frequency); - - task2SwitchState = task2_switch1.digitalInState(); -// printf("Switch 1 State: %d\r\n",task2SwitchState); - - task3.OutputWatchdogPulse(); + + // Attempt to open SD file + // If open failed, do not run Cyclic Exec + if(!task7.openFile("/SD/A2/test.csv","a")) + { + // Start Cyclic Executive + CyclicTicker.attach(&CyclicExec,0.025); // 25ms pulses + + // Keep program running until RESET + while(1) + { + } + } - float *analogReading = task4.returnAnalogReadings(); - task4AnalogChannels[0] = *(analogReading); - task4AnalogChannels[1]= *(analogReading+1); -// printf("Analog Readings:\r\nChannel 1-%f\r\nChannel 2-%f\r\n",task4AnalogChannels[0],task4AnalogChannels[1]); - - errorState = task6.updateErrorCode( task2SwitchState, - task4AnalogChannels[0], - task4AnalogChannels[1] ); - - task5.updateDisplay(task1Frequency, - task2SwitchState, - errorState, - task4AnalogChannels[0], - task4AnalogChannels[1] ); - - if(task7.openFile("/SD/A2/test.csv","a")) - { - printf("File not opened"); - } + // If FIle is not opened, prompt user and show Error on LED else { - int a = sprintf(logData,"Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",task1Frequency, - task2SwitchState,task4AnalogChannels[0],task4AnalogChannels[1]); - task7.writeData(logData); - task7.closeFile(); + // Prompt user about error + printf("File not opened\r\nNot executing Cyclic Executive"); + + // Execute error code on LED + while(1) + { + ErrorLED = 1; + wait(1); + ErrorLED = 0; + wait(1); + } + } +} + +Timer stampTime; + +#define TASK1_TICKS 40 +#define TASK2_TICKS 12 +#define TASK3_TICKS 12 +#define TASK4_TICKS 16 +#define TASK5_TICKS 80 +#define TASK6_TICKS 32 +#define TASK7_TICKS 200 + +#define HIGH 1 + +//void CyclicExec() +//{ +// slotCounter++; +// switch(slotCounter) +// { +// case 1: +// if(cyclicTicks % TASK1_TICKS == 0) +// { +//// printf("T1\r\n"); +// task1Frequency = task1.ReadFrequency(); +// } +// break; +// +// case 2: +// if(cyclicTicks % TASK2_TICKS == 0) +// { +//// printf("T2\r\n"); +// task2SwitchState = task2_switch1.digitalInState(); +// } +// break; +// +// case 3: +// if(cyclicTicks % TASK3_TICKS == 0) +// { +//// printf("T3 ------ \r\n"); +// LenPin = 1; +// task3.OutputWatchdogPulse(); +// LenPin = 0; +// } +// break; +// +// case 4: +// if(cyclicTicks % TASK4_TICKS == 0) +// { +//// printf("T4\r\n"); +// float *analogReading = task4.returnAnalogReadings(); +// task4AnalogChannels[0] = *(analogReading); +// task4AnalogChannels[1]= *(analogReading+1); +// } +// break; +// +// case 5: +// if(cyclicTicks % TASK5_TICKS == 0) +// { +//// printf("T5\r\n"); +// task5.updateDisplay( task1Frequency, +// task2SwitchState, +// task6ErrorState, +// task4AnalogChannels[0], +// task4AnalogChannels[1] ); +// } +// break; +// +// case 10: +// if(cyclicTicks % TASK6_TICKS == 0) +// { +//// printf("T6\r\n"); +// task6ErrorState = task6.updateErrorCode( task2SwitchState, +// task4AnalogChannels[0], +// task4AnalogChannels[1] ); +// } +// break; +// +// case 11: +// if(cyclicTicks % TASK7_TICKS == 0) +// { +//// printf("T7\r\n"); +// int a = sprintf( logData,"Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n", +// task1Frequency, +// task2SwitchState, +// task4AnalogChannels[0], +// task4AnalogChannels[1] ); +// task7.writeData(logData); +// } +// break; +// +// case 12: +// slotCounter = 0; +// cyclicTicks++; +// break; +// } +// if(SDRemoval == HIGH) +// { +// printf("Shutting File"); +// task7.closeFile(); +// } +// +// +//} + +Timer task1t; +Timer task2t; +Timer task3t; +Timer task4t; +Timer task5t; +Timer task6t; + +void CyclicExec() +{ + cyclicTicks++; + if(cyclicTicks % 200 == 0) + { + stampTime.stop(); + printf("T7\r\n"); + int a = sprintf( logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n", + stampTime.read(), + task1Frequency, + task2SwitchState, + task4AnalogChannels[0], + task4AnalogChannels[1] ); + task7.writeData(logData); + stampTime.reset(); + stampTime.start(); + } + + if(cyclicTicks % 80 == 0) + { + task5t.stop(); + printf("T5 %1.2f\r\n",task5t.read()); + task5.updateDisplay( task1Frequency, + task2SwitchState, + task6ErrorState, + task4AnalogChannels[0], + task4AnalogChannels[1] ); + task5t.reset(); + task5t.start(); + } + + if(cyclicTicks % 40 == 0) + { + task1t.stop(); + printf("T1 %d\r\n",task1t.read_ms()); + task1Frequency = task1.ReadFrequency(); + task1t.reset(); + task1t.start(); } - while(1) { - myled = 1; - wait(0.2); - myled = 0; - wait(0.2); + if(cyclicTicks % 32 == 0) + { + task6t.stop(); + printf("T6 %d\r\n",task6t.read_ms()); + task6ErrorState = task6.updateErrorCode( task2SwitchState, + task4AnalogChannels[0], + task4AnalogChannels[1] ); + task6t.reset(); + task6t.start(); + } + + if(cyclicTicks % 16 == 0) + { + task4t.stop(); + printf("T4 %d\r\n",task4t.read_ms()); + float *analogReading = task4.returnAnalogReadings(); + task4AnalogChannels[0] = *(analogReading); + task4AnalogChannels[1]= *(analogReading+1); + task4t.reset(); + task4t.start(); } -} \ No newline at end of file + + if(cyclicTicks & 13 == 0) + { + printf("IM IN TASK 3!!!!!!!!!!!!!!!!!!!!!"); + task3t.stop(); + printf("T3 %d\r\n",task3t.read_ms()); + task3.OutputWatchdogPulse(); + taskNum = 2; + task3t.reset(); + task3t.start(); + } + + if(cyclicTicks % 12 == 0) + { + printf("IM IN TASK 2!!!!!!!!!!!!!!!!!!!!!"); + task2t.stop(); + printf("T2 %d\r\n",task2t.read_ms()); + task2SwitchState = task2_switch1.digitalInState(); + taskNum = 3; + task2t.reset(); + task2t.start(); + } + + if(SDRemoval == HIGH) + { + printf("SD Removed"); + task7.closeFile(); + } +} +