Jordan Kinden / ECE4333Lab3

Dependencies:   mbed-rtos ECE4333Lab3

Fork of ECE4333Lab3 by ECE 4333

Committer:
JordanWisdom
Date:
Wed Feb 03 17:41:51 2016 +0000
Revision:
1:79e780124b58
Parent:
0:a046f7397436
Child:
2:f1ce213d3367
Made it pretty for Adam.

Who changed what in which revision?

UserRevisionLine numberNew 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 1:79e780124b58 13
JordanWisdom 0:a046f7397436 14 // Global variables for interrupt handler
JordanWisdom 0:a046f7397436 15 int Position;
JordanWisdom 1:79e780124b58 16
JordanWisdom 0:a046f7397436 17 // Processes and threads
JordanWisdom 0:a046f7397436 18 int32_t SignalPi, SignalWdt, SignalExtCollision;
JordanWisdom 1:79e780124b58 19
JordanWisdom 0:a046f7397436 20 osThreadId PiControl,WdtFault,ExtCollision;
JordanWisdom 0:a046f7397436 21 osThreadDef(PiControlThread, osPriorityRealtime, DEFAULT_STACK_SIZE); // Declare Control as a thread/process
JordanWisdom 0:a046f7397436 22 osThreadDef(ExtCollisionThread, osPriorityHigh, DEFAULT_STACK_SIZE); // Declare External Collision as a thread/process
JordanWisdom 0:a046f7397436 23 osTimerDef(Wdtimer, Watchdog); // Declare a watch dog timer
JordanWisdom 0:a046f7397436 24 // osPriorityIdle = -3, ///< priority: idle (lowest)
JordanWisdom 0:a046f7397436 25 // osPriorityLow = -2, ///< priority: low
JordanWisdom 0:a046f7397436 26 // osPriorityBelowNormal = -1, ///< priority: below normal
JordanWisdom 0:a046f7397436 27 // osPriorityNormal = 0, ///< priority: normal (default)
JordanWisdom 0:a046f7397436 28 // osPriorityAboveNormal = +1, ///< priority: above normal
JordanWisdom 0:a046f7397436 29 // osPriorityHigh = +2, ///< priority: high
JordanWisdom 0:a046f7397436 30 // osPriorityRealtime = +3, ///< priority: realtime (highest)
JordanWisdom 1:79e780124b58 31
JordanWisdom 0:a046f7397436 32 // IO Port Configuration
JordanWisdom 0:a046f7397436 33 DigitalOut led1(LED1);
JordanWisdom 0:a046f7397436 34 DigitalOut led2(LED2);
JordanWisdom 0:a046f7397436 35 DigitalOut led3(LED3);
JordanWisdom 0:a046f7397436 36 DigitalOut led4(LED4);
JordanWisdom 0:a046f7397436 37 Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel
JordanWisdom 0:a046f7397436 38 Ticker PeriodicInt; // Declare a timer interrupt: PeriodicInt
JordanWisdom 0:a046f7397436 39 InterruptIn Bumper(p8); // External interrupt pin declared as Bumper
JordanWisdom 1:79e780124b58 40
JordanWisdom 1:79e780124b58 41
JordanWisdom 0:a046f7397436 42 // ******** Main Thread ********
JordanWisdom 0:a046f7397436 43 int main() { // This thread executes first upon reset or power-on.
JordanWisdom 1:79e780124b58 44 char x;
JordanWisdom 1:79e780124b58 45 // Attach the address of the ExtCollisionISR to the rising edge of Bumper:
JordanWisdom 1:79e780124b58 46 Bumper.rise(&ExtCollisionISR);
JordanWisdom 1:79e780124b58 47
JordanWisdom 1:79e780124b58 48 // Start execution of the threads: PiControlThread and ExtCollisionThread:
JordanWisdom 1:79e780124b58 49 PiControl = osThreadCreate(osThread(PiControlThread), NULL);
JordanWisdom 1:79e780124b58 50 ExtCollision = osThreadCreate(osThread(ExtCollisionThread), NULL);
JordanWisdom 1:79e780124b58 51
JordanWisdom 1:79e780124b58 52 // Start the watch dog timer and enable the watch dog interrupt
JordanWisdom 1:79e780124b58 53 osTimerId OneShot = osTimerCreate(osTimer(Wdtimer), osTimerOnce, (void *)0);
JordanWisdom 1:79e780124b58 54 pc.printf("\r\n RTOS Template Program");
JordanWisdom 1:79e780124b58 55
JordanWisdom 1:79e780124b58 56 // May prompt user for input data here:
JordanWisdom 1:79e780124b58 57 // Specify address of the PeriodicInt ISR as PiControllerISR, specify the interval
JordanWisdom 1:79e780124b58 58 // in seconds between interrupts, and start interrupt generation:
JordanWisdom 1:79e780124b58 59 PeriodicInt.attach(&PiControllerISR, .05);
JordanWisdom 1:79e780124b58 60 do {
JordanWisdom 1:79e780124b58 61 if (pc.readable()){
JordanWisdom 1:79e780124b58 62 x=pc.getc();
JordanWisdom 1:79e780124b58 63 pc.putc(x); // Echo keyboard entry
JordanWisdom 1:79e780124b58 64 // Start or restart the watchdog timer interrupt and set to 2000ms.
JordanWisdom 1:79e780124b58 65 osTimerStart(OneShot, 2000);
JordanWisdom 1:79e780124b58 66 led3=0; // Clear watch dog led3 status
JordanWisdom 1:79e780124b58 67 led4=0;
JordanWisdom 1:79e780124b58 68 }
JordanWisdom 1:79e780124b58 69 // Display variables at the terminal emulator for logging:
JordanWisdom 1:79e780124b58 70 pc.printf("\r\n%6d", Position); // The terminal emulator may be configured to
JordanWisdom 1:79e780124b58 71 // store received data to a file
JordanWisdom 1:79e780124b58 72 Thread::wait(500); // Go to sleep for 500 ms
JordanWisdom 1:79e780124b58 73 }
JordanWisdom 1:79e780124b58 74 while(1);
JordanWisdom 0:a046f7397436 75 }
JordanWisdom 1:79e780124b58 76
JordanWisdom 0:a046f7397436 77 // ******** Control Thread ********
JordanWisdom 0:a046f7397436 78 void PiControlThread(void const *argument) {
JordanWisdom 1:79e780124b58 79 while (true) {
JordanWisdom 1:79e780124b58 80 osSignalWait(SignalPi, osWaitForever); // Go to sleep until, SignalPi, is received.
JordanWisdom 1:79e780124b58 81 led2= !led2; // Alive status - led2 toggles each time PiControlThread is signaled.
JordanWisdom 1:79e780124b58 82 Position = Position + 1;
JordanWisdom 1:79e780124b58 83 }
JordanWisdom 0:a046f7397436 84 }
JordanWisdom 1:79e780124b58 85
JordanWisdom 0:a046f7397436 86 // ******** Collision Thread ********
JordanWisdom 0:a046f7397436 87 void ExtCollisionThread(void const *argument) {
JordanWisdom 1:79e780124b58 88 while (true) {
JordanWisdom 1:79e780124b58 89 // Go to sleep until signal, SignalExtCollision, is received:
JordanWisdom 1:79e780124b58 90 osSignalWait(SignalExtCollision, osWaitForever);
JordanWisdom 1:79e780124b58 91 led4 = 1;
JordanWisdom 1:79e780124b58 92 }
JordanWisdom 0:a046f7397436 93 }
JordanWisdom 1:79e780124b58 94
JordanWisdom 0:a046f7397436 95 // ******** Watchdog Interrupt Handler ********
JordanWisdom 0:a046f7397436 96 void Watchdog(void const *n) {
JordanWisdom 1:79e780124b58 97 led3=1; // led3 is activated when the watchdog timer times out
JordanWisdom 0:a046f7397436 98 }
JordanWisdom 1:79e780124b58 99
JordanWisdom 0:a046f7397436 100 // ******** Period Timer Interrupt Handler ********
JordanWisdom 0:a046f7397436 101 void PiControllerISR(void) {
JordanWisdom 1:79e780124b58 102 // Activate the signal, PiControl, with each periodic timer interrupt.
JordanWisdom 1:79e780124b58 103 osSignalSet(PiControl,0x1);
JordanWisdom 1:79e780124b58 104 }
JordanWisdom 0:a046f7397436 105
JordanWisdom 0:a046f7397436 106 // ******** Collision Interrupt Handler ********
JordanWisdom 0:a046f7397436 107 void ExtCollisionISR(void) {
JordanWisdom 1:79e780124b58 108 // Activate the signal, ExtCollision, with each pexternal interrupt.
JordanWisdom 1:79e780124b58 109 osSignalSet(ExtCollision,0x1);
JordanWisdom 1:79e780124b58 110 }