Lab 3 Template Code
Dependencies: mbed-rtos ECE4333Lab3
Fork of ECE4333Lab3 by
main.cpp@3:0efaad3251a8, 2016-02-03 (annotated)
- Committer:
- JordanWisdom
- Date:
- Wed Feb 03 23:22:01 2016 +0000
- Revision:
- 3:0efaad3251a8
- Parent:
- 2:f1ce213d3367
- Child:
- 4:434586084857
Added Dr. Diduch's UserInterface code and fixed the errors to get it running.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JordanWisdom | 0:a046f7397436 | 1 | // C.P. Diduch |
JordanWisdom | 0:a046f7397436 | 2 | // EE4333 Robotics Lab-3, January 18, 2014. |
JordanWisdom | 0:a046f7397436 | 3 | // Template for implementation of a PI Speed Control System |
JordanWisdom | 0:a046f7397436 | 4 | #include "rtos.h" |
JordanWisdom | 0:a046f7397436 | 5 | #include "mbed.h" |
JordanWisdom | 0:a046f7397436 | 6 | // Function prototypes |
JordanWisdom | 0:a046f7397436 | 7 | void PiControllerISR(void); |
JordanWisdom | 0:a046f7397436 | 8 | void WdtFaultISR(void); |
JordanWisdom | 0:a046f7397436 | 9 | void ExtCollisionISR(void); |
JordanWisdom | 0:a046f7397436 | 10 | void PiControlThread(void const *argument); |
JordanWisdom | 0:a046f7397436 | 11 | void ExtCollisionThread(void const *argument); |
JordanWisdom | 0:a046f7397436 | 12 | void Watchdog(void const *n); |
JordanWisdom | 3:0efaad3251a8 | 13 | void UserInterface(void); |
JordanWisdom | 1:79e780124b58 | 14 | |
JordanWisdom | 0:a046f7397436 | 15 | // Global variables for interrupt handler |
JordanWisdom | 0:a046f7397436 | 16 | int Position; |
JordanWisdom | 1:79e780124b58 | 17 | |
JordanWisdom | 0:a046f7397436 | 18 | // Processes and threads |
JordanWisdom | 0:a046f7397436 | 19 | int32_t SignalPi, SignalWdt, SignalExtCollision; |
JordanWisdom | 1:79e780124b58 | 20 | |
JordanWisdom | 0:a046f7397436 | 21 | osThreadId PiControl,WdtFault,ExtCollision; |
JordanWisdom | 0:a046f7397436 | 22 | osThreadDef(PiControlThread, osPriorityRealtime, DEFAULT_STACK_SIZE); // Declare Control as a thread/process |
JordanWisdom | 0:a046f7397436 | 23 | osThreadDef(ExtCollisionThread, osPriorityHigh, DEFAULT_STACK_SIZE); // Declare External Collision as a thread/process |
JordanWisdom | 0:a046f7397436 | 24 | osTimerDef(Wdtimer, Watchdog); // Declare a watch dog timer |
JordanWisdom | 3:0efaad3251a8 | 25 | osTimerId OneShot = osTimerCreate(osTimer(Wdtimer), osTimerOnce, (void *)0);//OneShot Timer |
JordanWisdom | 0:a046f7397436 | 26 | // osPriorityIdle = -3, ///< priority: idle (lowest) |
JordanWisdom | 0:a046f7397436 | 27 | // osPriorityLow = -2, ///< priority: low |
JordanWisdom | 0:a046f7397436 | 28 | // osPriorityBelowNormal = -1, ///< priority: below normal |
JordanWisdom | 0:a046f7397436 | 29 | // osPriorityNormal = 0, ///< priority: normal (default) |
JordanWisdom | 0:a046f7397436 | 30 | // osPriorityAboveNormal = +1, ///< priority: above normal |
JordanWisdom | 0:a046f7397436 | 31 | // osPriorityHigh = +2, ///< priority: high |
JordanWisdom | 0:a046f7397436 | 32 | // osPriorityRealtime = +3, ///< priority: realtime (highest) |
JordanWisdom | 1:79e780124b58 | 33 | |
JordanWisdom | 0:a046f7397436 | 34 | // IO Port Configuration |
JordanWisdom | 0:a046f7397436 | 35 | DigitalOut led1(LED1); |
JordanWisdom | 0:a046f7397436 | 36 | DigitalOut led2(LED2); |
JordanWisdom | 0:a046f7397436 | 37 | DigitalOut led3(LED3); |
JordanWisdom | 0:a046f7397436 | 38 | DigitalOut led4(LED4); |
JordanWisdom | 0:a046f7397436 | 39 | Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel |
JordanWisdom | 3:0efaad3251a8 | 40 | Serial BluetoothSerial(p28,p27); |
JordanWisdom | 0:a046f7397436 | 41 | Ticker PeriodicInt; // Declare a timer interrupt: PeriodicInt |
JordanWisdom | 0:a046f7397436 | 42 | InterruptIn Bumper(p8); // External interrupt pin declared as Bumper |
JordanWisdom | 1:79e780124b58 | 43 | |
JordanWisdom | 1:79e780124b58 | 44 | |
JordanWisdom | 0:a046f7397436 | 45 | // ******** Main Thread ******** |
JordanWisdom | 0:a046f7397436 | 46 | int main() { // This thread executes first upon reset or power-on. |
JordanWisdom | 1:79e780124b58 | 47 | char x; |
JordanWisdom | 1:79e780124b58 | 48 | // Attach the address of the ExtCollisionISR to the rising edge of Bumper: |
JordanWisdom | 1:79e780124b58 | 49 | Bumper.rise(&ExtCollisionISR); |
JordanWisdom | 1:79e780124b58 | 50 | |
JordanWisdom | 1:79e780124b58 | 51 | // Start execution of the threads: PiControlThread and ExtCollisionThread: |
JordanWisdom | 1:79e780124b58 | 52 | PiControl = osThreadCreate(osThread(PiControlThread), NULL); |
JordanWisdom | 1:79e780124b58 | 53 | ExtCollision = osThreadCreate(osThread(ExtCollisionThread), NULL); |
JordanWisdom | 1:79e780124b58 | 54 | |
JordanWisdom | 1:79e780124b58 | 55 | // Start the watch dog timer and enable the watch dog interrupt |
JordanWisdom | 1:79e780124b58 | 56 | osTimerId OneShot = osTimerCreate(osTimer(Wdtimer), osTimerOnce, (void *)0); |
JordanWisdom | 1:79e780124b58 | 57 | pc.printf("\r\n RTOS Template Program"); |
JordanWisdom | 1:79e780124b58 | 58 | |
JordanWisdom | 1:79e780124b58 | 59 | // May prompt user for input data here: |
JordanWisdom | 1:79e780124b58 | 60 | // Specify address of the PeriodicInt ISR as PiControllerISR, specify the interval |
JordanWisdom | 1:79e780124b58 | 61 | // in seconds between interrupts, and start interrupt generation: |
JordanWisdom | 1:79e780124b58 | 62 | PeriodicInt.attach(&PiControllerISR, .05); |
JordanWisdom | 1:79e780124b58 | 63 | do { |
JordanWisdom | 1:79e780124b58 | 64 | if (pc.readable()){ |
JordanWisdom | 1:79e780124b58 | 65 | x=pc.getc(); |
JordanWisdom | 1:79e780124b58 | 66 | pc.putc(x); // Echo keyboard entry |
JordanWisdom | 1:79e780124b58 | 67 | // Start or restart the watchdog timer interrupt and set to 2000ms. |
JordanWisdom | 1:79e780124b58 | 68 | osTimerStart(OneShot, 2000); |
JordanWisdom | 1:79e780124b58 | 69 | led3=0; // Clear watch dog led3 status |
JordanWisdom | 1:79e780124b58 | 70 | led4=0; |
JordanWisdom | 1:79e780124b58 | 71 | } |
JordanWisdom | 1:79e780124b58 | 72 | // Display variables at the terminal emulator for logging: |
JordanWisdom | 1:79e780124b58 | 73 | pc.printf("\r\n%6d", Position); // The terminal emulator may be configured to |
JordanWisdom | 1:79e780124b58 | 74 | // store received data to a file |
JordanWisdom | 1:79e780124b58 | 75 | Thread::wait(500); // Go to sleep for 500 ms |
JordanWisdom | 1:79e780124b58 | 76 | } |
JordanWisdom | 1:79e780124b58 | 77 | while(1); |
JordanWisdom | 0:a046f7397436 | 78 | } |
JordanWisdom | 1:79e780124b58 | 79 | |
JordanWisdom | 0:a046f7397436 | 80 | // ******** Control Thread ******** |
JordanWisdom | 0:a046f7397436 | 81 | void PiControlThread(void const *argument) { |
JordanWisdom | 1:79e780124b58 | 82 | while (true) { |
JordanWisdom | 1:79e780124b58 | 83 | osSignalWait(SignalPi, osWaitForever); // Go to sleep until, SignalPi, is received. |
JordanWisdom | 2:f1ce213d3367 | 84 | led2 = !led2; // Alive status - led2 toggles each time PiControlThread is signaled. |
JordanWisdom | 1:79e780124b58 | 85 | Position = Position + 1; |
JordanWisdom | 1:79e780124b58 | 86 | } |
JordanWisdom | 0:a046f7397436 | 87 | } |
JordanWisdom | 1:79e780124b58 | 88 | |
JordanWisdom | 0:a046f7397436 | 89 | // ******** Collision Thread ******** |
JordanWisdom | 0:a046f7397436 | 90 | void ExtCollisionThread(void const *argument) { |
JordanWisdom | 1:79e780124b58 | 91 | while (true) { |
JordanWisdom | 1:79e780124b58 | 92 | // Go to sleep until signal, SignalExtCollision, is received: |
JordanWisdom | 1:79e780124b58 | 93 | osSignalWait(SignalExtCollision, osWaitForever); |
JordanWisdom | 1:79e780124b58 | 94 | led4 = 1; |
JordanWisdom | 1:79e780124b58 | 95 | } |
JordanWisdom | 0:a046f7397436 | 96 | } |
JordanWisdom | 1:79e780124b58 | 97 | |
JordanWisdom | 0:a046f7397436 | 98 | // ******** Watchdog Interrupt Handler ******** |
JordanWisdom | 0:a046f7397436 | 99 | void Watchdog(void const *n) { |
JordanWisdom | 1:79e780124b58 | 100 | led3=1; // led3 is activated when the watchdog timer times out |
JordanWisdom | 0:a046f7397436 | 101 | } |
JordanWisdom | 1:79e780124b58 | 102 | |
JordanWisdom | 0:a046f7397436 | 103 | // ******** Period Timer Interrupt Handler ******** |
JordanWisdom | 0:a046f7397436 | 104 | void PiControllerISR(void) { |
JordanWisdom | 1:79e780124b58 | 105 | // Activate the signal, PiControl, with each periodic timer interrupt. |
JordanWisdom | 1:79e780124b58 | 106 | osSignalSet(PiControl,0x1); |
JordanWisdom | 1:79e780124b58 | 107 | } |
JordanWisdom | 0:a046f7397436 | 108 | |
JordanWisdom | 0:a046f7397436 | 109 | // ******** Collision Interrupt Handler ******** |
JordanWisdom | 0:a046f7397436 | 110 | void ExtCollisionISR(void) { |
JordanWisdom | 1:79e780124b58 | 111 | // Activate the signal, ExtCollision, with each pexternal interrupt. |
JordanWisdom | 1:79e780124b58 | 112 | osSignalSet(ExtCollision,0x1); |
JordanWisdom | 1:79e780124b58 | 113 | } |
JordanWisdom | 3:0efaad3251a8 | 114 | |
JordanWisdom | 3:0efaad3251a8 | 115 | // ******** User Interface ********* |
JordanWisdom | 3:0efaad3251a8 | 116 | void UserInterface(void) { |
JordanWisdom | 3:0efaad3251a8 | 117 | char x; |
JordanWisdom | 3:0efaad3251a8 | 118 | do{ |
JordanWisdom | 3:0efaad3251a8 | 119 | if (pc.readable()) { |
JordanWisdom | 3:0efaad3251a8 | 120 | x = pc.getc(); pc.putc(x); //Echo keyboard entry |
JordanWisdom | 3:0efaad3251a8 | 121 | osTimerStart(OneShot, 2000); // Set WDT interrupt to 2s. |
JordanWisdom | 3:0efaad3251a8 | 122 | led3 = 0; |
JordanWisdom | 3:0efaad3251a8 | 123 | } |
JordanWisdom | 3:0efaad3251a8 | 124 | if (BluetoothSerial.readable()) { |
JordanWisdom | 3:0efaad3251a8 | 125 | x = BluetoothSerial.getc(); |
JordanWisdom | 3:0efaad3251a8 | 126 | if(x == 'w'){ |
JordanWisdom | 3:0efaad3251a8 | 127 | pc.printf("\r\n w pressed"); |
JordanWisdom | 3:0efaad3251a8 | 128 | } |
JordanWisdom | 3:0efaad3251a8 | 129 | // Display variables at the terminal emulator for logging: |
JordanWisdom | 3:0efaad3251a8 | 130 | pc.printf("\r\n blah blah"); |
JordanWisdom | 3:0efaad3251a8 | 131 | Thread::wait(500); // Wait 500 ms |
JordanWisdom | 3:0efaad3251a8 | 132 | } |
JordanWisdom | 3:0efaad3251a8 | 133 | }while(1); |
JordanWisdom | 3:0efaad3251a8 | 134 | } |