Jordan Kinden / ECE4333Lab3

Dependencies:   mbed-rtos ECE4333Lab3

Fork of ECE4333Lab3 by ECE 4333

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // C.P. Diduch
00002 // EE4333 Robotics Lab-3, January 18, 2014.
00003 // Template for implementation of a PI Speed Control System
00004 #include "InterruptIn.h"
00005 #include "rtos.h"
00006 #include "mbed.h"
00007 #include "Serial.h"
00008 
00009 // Function prototypes
00010 void PiControllerISR(void);
00011 void WdtFaultISR(void);
00012 void ExtCollisionISR(void);
00013 void PiControlThread(void const *argument);
00014 void ExtCollisionThread(void const *argument);
00015 void Watchdog(void const *n);
00016 void UserInterface(void);
00017 
00018 // Global variables for interrupt handler
00019 int Position;
00020 signed int u; //On Time Int
00021 
00022 // Processes and threads
00023 int32_t SignalPi, SignalWdt, SignalExtCollision; //Semaphores
00024 
00025 osThreadId PiControl,WdtFault,ExtCollision;
00026 osThreadDef(PiControlThread, osPriorityRealtime, DEFAULT_STACK_SIZE); // Declare Control as a thread/process
00027 osThreadDef(ExtCollisionThread, osPriorityHigh, DEFAULT_STACK_SIZE); // Declare External Collision as a thread/process
00028 osTimerDef(Wdtimer, Watchdog); // Declare a watch dog timer
00029 //typedef enum{
00030 // osPriorityIdle = -3, ///< priority: idle (lowest)
00031 // osPriorityLow = -2, ///< priority: low
00032 // osPriorityBelowNormal = -1, ///< priority: below normal
00033 // osPriorityNormal = 0, ///< priority: normal (default)
00034 // osPriorityAboveNormal = +1, ///< priority: above normal
00035 // osPriorityHigh = +2, ///< priority: high
00036 // osPriorityRealtime = +3, ///< priority: realtime (highest)
00037 //}osPriority;
00038 
00039 // IO Port Configuration
00040 // Digital
00041 DigitalOut led1(LED1);
00042 DigitalOut led2(LED2);
00043 DigitalOut led3(LED3);
00044 DigitalOut led4(LED4);
00045 
00046 DigitalOut BR1(p9); //Brake 1
00047 DigitalOut DIR1(p10); //Direction 1
00048 
00049 //PWM
00050 PwmOut PW1(p22);
00051 
00052 //Serial
00053 Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel
00054 Serial BluetoothSerial(p28,p27);
00055 
00056 //Interrupts
00057 InterruptIn Bumper(p8); // External interrupt pin declared as Bumper
00058 Ticker PeriodicInt; // Declare a timer interrupt: PeriodicInt
00059 
00060 signed DisplayMenu(){
00061 signed x = 0;
00062 char Key[7] = "+00000";
00063         printf("\n\rEnter a Pulse Width in microSeconds (max +/-30000, include leading zeroes):");
00064         pc.gets(Key,7);
00065         x = strtol(Key,NULL,10);
00066         printf("\n\r Selected Pulse Width = %d us", x);
00067         //pc.printf("\r\n%6d", Position); // The terminal emulator may be configured to
00068         // store received data to a file
00069         Thread::wait(100); // Go to sleep for 500 ms
00070         return x;
00071 }
00072 
00073 // ******** Main Thread ********
00074 int main() { // This thread executes first upon reset or power-on.
00075     PW1.period_us(20);
00076     BR1.write(1);
00077     DIR1.write(1);
00078     
00079     // Attach the address of the ExtCollisionISR to the rising edge of Bumper:
00080     Bumper.rise(&ExtCollisionISR);
00081     
00082     // Start execution of the threads: PiControlThread and ExtCollisionThread:
00083     PiControl = osThreadCreate(osThread(PiControlThread), NULL);
00084     ExtCollision = osThreadCreate(osThread(ExtCollisionThread), NULL);
00085     
00086     // Start the watch dog timer and enable the watch dog interrupt
00087     osTimerId OneShot = osTimerCreate(osTimer(Wdtimer), osTimerOnce, (void *)0);
00088     pc.printf("\r\n RTOS Template Program");
00089     
00090     // May prompt user for input data here:
00091     // Specify address of the PeriodicInt ISR as PiControllerISR, specify the interval
00092     // in seconds between interrupts, and start interrupt generation:
00093     PeriodicInt.attach(&PiControllerISR, .05);
00094     do {  
00095         u = DisplayMenu();
00096         printf("\n\r Memory Pulse Width: %d", u);
00097     }
00098     while(1);
00099     
00100 }
00101 
00102 
00103 // ******** Control Thread ********
00104 void PiControlThread(void const *argument) {
00105     while (true) {
00106         osSignalWait(SignalPi, osWaitForever); // Go to sleep until, SignalPi, is received.
00107         //led2 = !led2; // Alive status - led2 toggles each time PiControlThread is signaled.
00108         if(u >= 0)
00109         {
00110             if(u>=131071) u = 131071; //Overflow protection
00111             PW1.pulsewidth_us(u);
00112             DIR1.write(1);
00113             }
00114         else if(u < 0)
00115         {
00116             if(u<-131071) u = -131071; //Overflow protection
00117             PW1.pulsewidth_us(u);
00118             DIR1.write(0);
00119             }
00120         Position = Position + 1;
00121         printf("\n\r Memory Pulse Width = %d", u);    
00122     }
00123 }
00124 
00125 // ******** Collision Thread ********
00126 void ExtCollisionThread(void const *argument) {
00127     while (true) {
00128         // Go to sleep until signal, SignalExtCollision, is received:
00129         osSignalWait(SignalExtCollision, osWaitForever);
00130         led4 = 0;
00131     }
00132 }
00133 
00134 // ******** Watchdog Interrupt Handler ********
00135 void Watchdog(void const *n) {
00136     led3=0; // led3 is activated when the watchdog timer times out
00137 }
00138 
00139 // ******** Period Timer Interrupt Handler ********
00140 void PiControllerISR(void) {
00141     // Activate the signal, PiControl, with each periodic timer interrupt.
00142     //osSignalSet(PiControl,0x1); 
00143 }
00144 
00145 // ******** Collision Interrupt Handler ********
00146 void ExtCollisionISR(void) {
00147     // Activate the signal, ExtCollision, with each pexternal interrupt.
00148     //osSignalSet(ExtCollision,0x1);
00149 }
00150 
00151 /*/ ******** User Interface *********
00152 void UserInterface(void) {
00153     char x;
00154     do{
00155         if (pc.readable()) {
00156             x = pc.getc(); pc.putc(x); //Echo keyboard entry
00157             osTimerStart(OneShot, 2000); // Set WDT interrupt to 2s.
00158             led3 = 0;
00159         }
00160          (BluetoothSerial.readable()) {
00161             x = BluetoothSerial.getc();
00162             (x == 'w'){ 
00163                 pc.printf("\r\n w pressed");
00164             }
00165             // Display variables at the terminal emulator for logging:
00166             pc.printf("\r\n blah blah");
00167             Thread::wait(500); // Wait 500 ms
00168             }
00169     }while(1);
00170 }*/