Lab 3 Template Code
Dependencies: mbed-rtos ECE4333Lab3
Fork of ECE4333Lab3 by
main.cpp@4:434586084857, 2016-02-05 (annotated)
- Committer:
- JordanWisdom
- Date:
- Fri Feb 05 16:45:01 2016 +0000
- Revision:
- 4:434586084857
- Parent:
- 3:0efaad3251a8
PAIN
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 | 4:434586084857 | 4 | #include "InterruptIn.h" |
JordanWisdom | 0:a046f7397436 | 5 | #include "rtos.h" |
JordanWisdom | 0:a046f7397436 | 6 | #include "mbed.h" |
JordanWisdom | 4:434586084857 | 7 | #include "Serial.h" |
JordanWisdom | 4:434586084857 | 8 | |
JordanWisdom | 0:a046f7397436 | 9 | // Function prototypes |
JordanWisdom | 0:a046f7397436 | 10 | void PiControllerISR(void); |
JordanWisdom | 0:a046f7397436 | 11 | void WdtFaultISR(void); |
JordanWisdom | 0:a046f7397436 | 12 | void ExtCollisionISR(void); |
JordanWisdom | 0:a046f7397436 | 13 | void PiControlThread(void const *argument); |
JordanWisdom | 0:a046f7397436 | 14 | void ExtCollisionThread(void const *argument); |
JordanWisdom | 0:a046f7397436 | 15 | void Watchdog(void const *n); |
JordanWisdom | 3:0efaad3251a8 | 16 | void UserInterface(void); |
JordanWisdom | 1:79e780124b58 | 17 | |
JordanWisdom | 0:a046f7397436 | 18 | // Global variables for interrupt handler |
JordanWisdom | 0:a046f7397436 | 19 | int Position; |
JordanWisdom | 4:434586084857 | 20 | signed int u; //On Time Int |
JordanWisdom | 1:79e780124b58 | 21 | |
JordanWisdom | 0:a046f7397436 | 22 | // Processes and threads |
JordanWisdom | 4:434586084857 | 23 | int32_t SignalPi, SignalWdt, SignalExtCollision; //Semaphores |
JordanWisdom | 1:79e780124b58 | 24 | |
JordanWisdom | 0:a046f7397436 | 25 | osThreadId PiControl,WdtFault,ExtCollision; |
JordanWisdom | 0:a046f7397436 | 26 | osThreadDef(PiControlThread, osPriorityRealtime, DEFAULT_STACK_SIZE); // Declare Control as a thread/process |
JordanWisdom | 0:a046f7397436 | 27 | osThreadDef(ExtCollisionThread, osPriorityHigh, DEFAULT_STACK_SIZE); // Declare External Collision as a thread/process |
JordanWisdom | 0:a046f7397436 | 28 | osTimerDef(Wdtimer, Watchdog); // Declare a watch dog timer |
JordanWisdom | 4:434586084857 | 29 | //typedef enum{ |
JordanWisdom | 0:a046f7397436 | 30 | // osPriorityIdle = -3, ///< priority: idle (lowest) |
JordanWisdom | 0:a046f7397436 | 31 | // osPriorityLow = -2, ///< priority: low |
JordanWisdom | 0:a046f7397436 | 32 | // osPriorityBelowNormal = -1, ///< priority: below normal |
JordanWisdom | 0:a046f7397436 | 33 | // osPriorityNormal = 0, ///< priority: normal (default) |
JordanWisdom | 0:a046f7397436 | 34 | // osPriorityAboveNormal = +1, ///< priority: above normal |
JordanWisdom | 0:a046f7397436 | 35 | // osPriorityHigh = +2, ///< priority: high |
JordanWisdom | 0:a046f7397436 | 36 | // osPriorityRealtime = +3, ///< priority: realtime (highest) |
JordanWisdom | 4:434586084857 | 37 | //}osPriority; |
JordanWisdom | 1:79e780124b58 | 38 | |
JordanWisdom | 0:a046f7397436 | 39 | // IO Port Configuration |
JordanWisdom | 4:434586084857 | 40 | // Digital |
JordanWisdom | 0:a046f7397436 | 41 | DigitalOut led1(LED1); |
JordanWisdom | 0:a046f7397436 | 42 | DigitalOut led2(LED2); |
JordanWisdom | 0:a046f7397436 | 43 | DigitalOut led3(LED3); |
JordanWisdom | 0:a046f7397436 | 44 | DigitalOut led4(LED4); |
JordanWisdom | 4:434586084857 | 45 | |
JordanWisdom | 4:434586084857 | 46 | DigitalOut BR1(p9); //Brake 1 |
JordanWisdom | 4:434586084857 | 47 | DigitalOut DIR1(p10); //Direction 1 |
JordanWisdom | 4:434586084857 | 48 | |
JordanWisdom | 4:434586084857 | 49 | //PWM |
JordanWisdom | 4:434586084857 | 50 | PwmOut PW1(p22); |
JordanWisdom | 4:434586084857 | 51 | |
JordanWisdom | 4:434586084857 | 52 | //Serial |
JordanWisdom | 0:a046f7397436 | 53 | Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel |
JordanWisdom | 3:0efaad3251a8 | 54 | Serial BluetoothSerial(p28,p27); |
JordanWisdom | 4:434586084857 | 55 | |
JordanWisdom | 4:434586084857 | 56 | //Interrupts |
JordanWisdom | 0:a046f7397436 | 57 | InterruptIn Bumper(p8); // External interrupt pin declared as Bumper |
JordanWisdom | 4:434586084857 | 58 | Ticker PeriodicInt; // Declare a timer interrupt: PeriodicInt |
JordanWisdom | 1:79e780124b58 | 59 | |
JordanWisdom | 4:434586084857 | 60 | signed DisplayMenu(){ |
JordanWisdom | 4:434586084857 | 61 | signed x = 0; |
JordanWisdom | 4:434586084857 | 62 | char Key[7] = "+00000"; |
JordanWisdom | 4:434586084857 | 63 | printf("\n\rEnter a Pulse Width in microSeconds (max +/-30000, include leading zeroes):"); |
JordanWisdom | 4:434586084857 | 64 | pc.gets(Key,7); |
JordanWisdom | 4:434586084857 | 65 | x = strtol(Key,NULL,10); |
JordanWisdom | 4:434586084857 | 66 | printf("\n\r Selected Pulse Width = %d us", x); |
JordanWisdom | 4:434586084857 | 67 | //pc.printf("\r\n%6d", Position); // The terminal emulator may be configured to |
JordanWisdom | 4:434586084857 | 68 | // store received data to a file |
JordanWisdom | 4:434586084857 | 69 | Thread::wait(100); // Go to sleep for 500 ms |
JordanWisdom | 4:434586084857 | 70 | return x; |
JordanWisdom | 4:434586084857 | 71 | } |
JordanWisdom | 1:79e780124b58 | 72 | |
JordanWisdom | 0:a046f7397436 | 73 | // ******** Main Thread ******** |
JordanWisdom | 0:a046f7397436 | 74 | int main() { // This thread executes first upon reset or power-on. |
JordanWisdom | 4:434586084857 | 75 | PW1.period_us(20); |
JordanWisdom | 4:434586084857 | 76 | BR1.write(1); |
JordanWisdom | 4:434586084857 | 77 | DIR1.write(1); |
JordanWisdom | 4:434586084857 | 78 | |
JordanWisdom | 1:79e780124b58 | 79 | // Attach the address of the ExtCollisionISR to the rising edge of Bumper: |
JordanWisdom | 1:79e780124b58 | 80 | Bumper.rise(&ExtCollisionISR); |
JordanWisdom | 1:79e780124b58 | 81 | |
JordanWisdom | 1:79e780124b58 | 82 | // Start execution of the threads: PiControlThread and ExtCollisionThread: |
JordanWisdom | 1:79e780124b58 | 83 | PiControl = osThreadCreate(osThread(PiControlThread), NULL); |
JordanWisdom | 1:79e780124b58 | 84 | ExtCollision = osThreadCreate(osThread(ExtCollisionThread), NULL); |
JordanWisdom | 1:79e780124b58 | 85 | |
JordanWisdom | 1:79e780124b58 | 86 | // Start the watch dog timer and enable the watch dog interrupt |
JordanWisdom | 1:79e780124b58 | 87 | osTimerId OneShot = osTimerCreate(osTimer(Wdtimer), osTimerOnce, (void *)0); |
JordanWisdom | 1:79e780124b58 | 88 | pc.printf("\r\n RTOS Template Program"); |
JordanWisdom | 1:79e780124b58 | 89 | |
JordanWisdom | 1:79e780124b58 | 90 | // May prompt user for input data here: |
JordanWisdom | 1:79e780124b58 | 91 | // Specify address of the PeriodicInt ISR as PiControllerISR, specify the interval |
JordanWisdom | 1:79e780124b58 | 92 | // in seconds between interrupts, and start interrupt generation: |
JordanWisdom | 1:79e780124b58 | 93 | PeriodicInt.attach(&PiControllerISR, .05); |
JordanWisdom | 4:434586084857 | 94 | do { |
JordanWisdom | 4:434586084857 | 95 | u = DisplayMenu(); |
JordanWisdom | 4:434586084857 | 96 | printf("\n\r Memory Pulse Width: %d", u); |
JordanWisdom | 1:79e780124b58 | 97 | } |
JordanWisdom | 1:79e780124b58 | 98 | while(1); |
JordanWisdom | 4:434586084857 | 99 | |
JordanWisdom | 0:a046f7397436 | 100 | } |
JordanWisdom | 1:79e780124b58 | 101 | |
JordanWisdom | 4:434586084857 | 102 | |
JordanWisdom | 0:a046f7397436 | 103 | // ******** Control Thread ******** |
JordanWisdom | 0:a046f7397436 | 104 | void PiControlThread(void const *argument) { |
JordanWisdom | 1:79e780124b58 | 105 | while (true) { |
JordanWisdom | 1:79e780124b58 | 106 | osSignalWait(SignalPi, osWaitForever); // Go to sleep until, SignalPi, is received. |
JordanWisdom | 4:434586084857 | 107 | //led2 = !led2; // Alive status - led2 toggles each time PiControlThread is signaled. |
JordanWisdom | 4:434586084857 | 108 | if(u >= 0) |
JordanWisdom | 4:434586084857 | 109 | { |
JordanWisdom | 4:434586084857 | 110 | if(u>=131071) u = 131071; //Overflow protection |
JordanWisdom | 4:434586084857 | 111 | PW1.pulsewidth_us(u); |
JordanWisdom | 4:434586084857 | 112 | DIR1.write(1); |
JordanWisdom | 4:434586084857 | 113 | } |
JordanWisdom | 4:434586084857 | 114 | else if(u < 0) |
JordanWisdom | 4:434586084857 | 115 | { |
JordanWisdom | 4:434586084857 | 116 | if(u<-131071) u = -131071; //Overflow protection |
JordanWisdom | 4:434586084857 | 117 | PW1.pulsewidth_us(u); |
JordanWisdom | 4:434586084857 | 118 | DIR1.write(0); |
JordanWisdom | 4:434586084857 | 119 | } |
JordanWisdom | 1:79e780124b58 | 120 | Position = Position + 1; |
JordanWisdom | 4:434586084857 | 121 | printf("\n\r Memory Pulse Width = %d", u); |
JordanWisdom | 1:79e780124b58 | 122 | } |
JordanWisdom | 0:a046f7397436 | 123 | } |
JordanWisdom | 1:79e780124b58 | 124 | |
JordanWisdom | 0:a046f7397436 | 125 | // ******** Collision Thread ******** |
JordanWisdom | 0:a046f7397436 | 126 | void ExtCollisionThread(void const *argument) { |
JordanWisdom | 1:79e780124b58 | 127 | while (true) { |
JordanWisdom | 1:79e780124b58 | 128 | // Go to sleep until signal, SignalExtCollision, is received: |
JordanWisdom | 1:79e780124b58 | 129 | osSignalWait(SignalExtCollision, osWaitForever); |
JordanWisdom | 4:434586084857 | 130 | led4 = 0; |
JordanWisdom | 1:79e780124b58 | 131 | } |
JordanWisdom | 0:a046f7397436 | 132 | } |
JordanWisdom | 1:79e780124b58 | 133 | |
JordanWisdom | 0:a046f7397436 | 134 | // ******** Watchdog Interrupt Handler ******** |
JordanWisdom | 0:a046f7397436 | 135 | void Watchdog(void const *n) { |
JordanWisdom | 4:434586084857 | 136 | led3=0; // led3 is activated when the watchdog timer times out |
JordanWisdom | 0:a046f7397436 | 137 | } |
JordanWisdom | 1:79e780124b58 | 138 | |
JordanWisdom | 0:a046f7397436 | 139 | // ******** Period Timer Interrupt Handler ******** |
JordanWisdom | 0:a046f7397436 | 140 | void PiControllerISR(void) { |
JordanWisdom | 1:79e780124b58 | 141 | // Activate the signal, PiControl, with each periodic timer interrupt. |
JordanWisdom | 4:434586084857 | 142 | //osSignalSet(PiControl,0x1); |
JordanWisdom | 1:79e780124b58 | 143 | } |
JordanWisdom | 0:a046f7397436 | 144 | |
JordanWisdom | 0:a046f7397436 | 145 | // ******** Collision Interrupt Handler ******** |
JordanWisdom | 0:a046f7397436 | 146 | void ExtCollisionISR(void) { |
JordanWisdom | 1:79e780124b58 | 147 | // Activate the signal, ExtCollision, with each pexternal interrupt. |
JordanWisdom | 4:434586084857 | 148 | //osSignalSet(ExtCollision,0x1); |
JordanWisdom | 1:79e780124b58 | 149 | } |
JordanWisdom | 3:0efaad3251a8 | 150 | |
JordanWisdom | 4:434586084857 | 151 | /*/ ******** User Interface ********* |
JordanWisdom | 3:0efaad3251a8 | 152 | void UserInterface(void) { |
JordanWisdom | 3:0efaad3251a8 | 153 | char x; |
JordanWisdom | 3:0efaad3251a8 | 154 | do{ |
JordanWisdom | 3:0efaad3251a8 | 155 | if (pc.readable()) { |
JordanWisdom | 3:0efaad3251a8 | 156 | x = pc.getc(); pc.putc(x); //Echo keyboard entry |
JordanWisdom | 3:0efaad3251a8 | 157 | osTimerStart(OneShot, 2000); // Set WDT interrupt to 2s. |
JordanWisdom | 3:0efaad3251a8 | 158 | led3 = 0; |
JordanWisdom | 3:0efaad3251a8 | 159 | } |
JordanWisdom | 4:434586084857 | 160 | (BluetoothSerial.readable()) { |
JordanWisdom | 3:0efaad3251a8 | 161 | x = BluetoothSerial.getc(); |
JordanWisdom | 4:434586084857 | 162 | (x == 'w'){ |
JordanWisdom | 3:0efaad3251a8 | 163 | pc.printf("\r\n w pressed"); |
JordanWisdom | 3:0efaad3251a8 | 164 | } |
JordanWisdom | 3:0efaad3251a8 | 165 | // Display variables at the terminal emulator for logging: |
JordanWisdom | 3:0efaad3251a8 | 166 | pc.printf("\r\n blah blah"); |
JordanWisdom | 3:0efaad3251a8 | 167 | Thread::wait(500); // Wait 500 ms |
JordanWisdom | 3:0efaad3251a8 | 168 | } |
JordanWisdom | 4:434586084857 | 169 | }while(1); |
JordanWisdom | 4:434586084857 | 170 | }*/ |