PlayBack

Dependencies:   TPixy-Interface

Fork of ManualControlFinal by ECE4333 - 2018 - Ahmed & Brandon

Revision:
1:3e9684e81312
Parent:
0:a355e511bc5d
Child:
2:ca2a7430739b
--- a/ui.cpp	Thu Feb 01 03:58:00 2018 +0000
+++ b/ui.cpp	Sat Feb 03 00:05:08 2018 +0000
@@ -8,14 +8,21 @@
 Mutex setpoint_mutex;
 
 // speed
-int setpoint;
+int setpoint = 0;
 
 // variable to store character recieved from terminal
 char x;
 
-extern uint16_t dPosition, dTime;
+extern int16_t dPosition, dTime;
+
+extern float Ki; 
+extern float Kp;
 
-uint16_t position;
+extern int vel;
+
+int16_t position;
+
+extern int32_t e;
 
 /******************************************************************************
             A function to test blutooth communication
@@ -40,63 +47,146 @@
 ******************************************************************************/
 void displayStartupMsg()
 {
-    pc.printf("\r\n************************************");
-    pc.printf("\r\n**** DC Motor Control using PWM ****");
-    pc.printf("\r\n************************************");
-    pc.printf("\r\n-Enter r to reset the watchdog timer");
-    pc.printf("\r\n-press w to increase motor speed");
-    pc.printf("\r\n-press s to decrease motor speed");
+    bluetooth.printf("\r\n************************************");
+    bluetooth.printf("\r\n**** DC Motor Control using PWM ****");
+    bluetooth.printf("\r\n************************************");
+    bluetooth.printf("\r\n-Enter r to reset the watchdog timer");
+    bluetooth.printf("\r\n-press w to increase motor speed");
+    bluetooth.printf("\r\n-press s to decrease motor speed");
 }
 
 
 /******************************************************************************
-                            Console interface
+                           User interface
 ******************************************************************************/
 void consoleUI(void)
 {
-    if (pc.readable()) {
-        x = pc.getc();
-
+    if (bluetooth.readable()) {
+        x = bluetooth.getc();
+        
         // if input from console is the letter 'r'
         if(x == 'r') {
             // reset watchdog timer
             WatchdogReset();
-            pc.printf("\r\nWatchdog has been reset");
+            bluetooth.printf("\r\nWatchdog has been reset");
         }
 
         // if w is pressed increase the speed
         // by incrementing u
         else if(x == 'w') {
-            // convert to integer
-            if(setpoint<=10000) {
-                setpoint_mutex.lock();
-                setpoint = setpoint + SPEED_STEP;
-                setpoint_mutex.unlock();
+            setpoint_mutex.lock();
+            if ( setpoint < 560 ) 
+            {
+                //setpoint = setpoint + SPEED_STEP;
+                setpoint = 100;
             }
+            setpoint_mutex.unlock();
+
             // display speed
-            pc.printf("\r\n %5d", setpoint);
+            bluetooth.printf("\r\n %5d", setpoint);
         }
 
         // if s is pressed decrease the speed
         // by decrementing u
         else if(x == 's') {
-            // convert to integer
-            if(setpoint>-10000) {
-                setpoint_mutex.lock();
-                setpoint = setpoint - SPEED_STEP;
-                setpoint_mutex.unlock();
+
+            setpoint_mutex.lock();
+            if (setpoint > -560) 
+            {
+                setpoint = -100;
+                //setpoint = setpoint - SPEED_STEP;
             }
+                
+            setpoint_mutex.unlock();
+
             // display speed
-            pc.printf("\r\n %5d", setpoint);
+            bluetooth.printf("\r\n %5d", setpoint);
         }
 
         // error wrong input
         else {
-            pc.printf("\r\nwrong input please enter \'w\' to increase the speed, \'s\' to reduce it or move in the opposite direction and \'r\' to reset the watchdog");
+            bluetooth.printf("\r\nwrong input please enter \'w\' to increase the speed, \'s\' to reduce it or move in the opposite direction and \'r\' to reset the watchdog");
         }
     }
     position += dPosition;
-    pc.printf("\r\nposition: %d, dPosition: %d, dTime: %d", position, dPosition, dTime);
+    bluetooth.printf("\r\nPos: %d, dP: %d, dT: %d, Kp: %f, Ki: %f, vel: %d, e: %d", position, dPosition, dTime, Kp, Ki, vel, e);
     
 }
 
+/******************************************************************************
+                           User interface 2
+******************************************************************************/
+
+void consoleUI2(void)
+{
+     
+    if (bluetooth.readable()) {
+        x = bluetooth.getc();
+        
+        // if input from console is the letter 'r'
+        if(x == 'r') {
+            // reset watchdog timer
+            WatchdogReset();
+            setpoint = 0;
+            bluetooth.printf("\r\nWatchdog has been reset");
+        }
+
+        // if w is pressed increase the speed
+        // by incrementing u
+        else if(x == 'w') {
+            setpoint_mutex.lock();
+            if ( setpoint < 703 ) 
+            {
+                //setpoint = setpoint + SPEED_STEP;
+                setpoint = 200;
+            }
+            setpoint_mutex.unlock();
+
+            // display speed
+            bluetooth.printf("\r\n %5d", setpoint);
+        }
+
+        // if s is pressed decrease the speed
+        // by decrementing u
+        else if(x == 's') {
+
+            setpoint_mutex.lock();
+            if (setpoint > -703) 
+            {
+                setpoint = -200;
+                //setpoint = setpoint - SPEED_STEP;
+            }
+                
+            setpoint_mutex.unlock();
+
+            // display speed
+            bluetooth.printf("\r\n %5d", setpoint);
+        }
+        else if (x=='e')
+        {
+            Ki = Ki + 0.005;
+        }
+        else if (x=='d')
+        {
+            Ki = Ki - 0.005;
+        }
+        else if (x=='t')
+        {
+            Kp = Kp + 0.05;
+        }
+        else if (x=='g')
+        {
+            Kp = Kp - 0.05;    
+        }
+        
+        // error wrong input
+        else {
+            bluetooth.printf("\r\nwrong input please enter \'w\' to increase the speed, \'s\' to reduce it or move in the opposite direction and \'r\' to reset the watchdog");
+        }
+    }   
+    
+    position += dPosition;
+    //bluetooth.printf("\r\nPos: %d, dP: %d, dT: %d, Kp: %f, Ki: %f, vel: %d, e: %d", position, dPosition, dTime, Kp, Ki, vel, e);
+    bluetooth.printf("\r\nKp: %f, Ki: %f, vel: %d, e: %d", Kp, Ki, vel, e);
+    
+}