Jordan Kinden / ECE4333Lab3

Dependencies:   mbed-rtos ECE4333Lab3

Fork of ECE4333Lab3 by ECE 4333

Committer:
JordanWisdom
Date:
Wed Feb 03 17:30:19 2016 +0000
Revision:
0:a046f7397436
Child:
1:79e780124b58
Dr. Diduch's Template (Working)

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