Engine control program with 3 engines Needs to make a case for each simultaneous engine setting, because of the WAIT after each number of bits have been sent.

Dependencies:   mbed-rtos mbed PinDetect

Fork of FinalMotorControl by Robot Bachelor

Revision:
23:ad08a8eabc24
Parent:
22:f6e328f7bd28
Child:
24:24c91a6ae6ba
--- a/main.cpp	Thu May 05 09:24:43 2016 +0000
+++ b/main.cpp	Wed May 11 18:44:06 2016 +0000
@@ -1,7 +1,9 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "PinDetect.h"
 
 Serial pc(USBTX, USBRX); // tx, rx
+PinDetect pb(A5);
 
 //Analog Pins
 //First engine
@@ -21,9 +23,11 @@
 DigitalOut IN12(D11);
 
 
-static int fart = 1300;
+static int fart = 1500;
 static char m_cmd = 'x';
-
+static bool e1 = true;
+static bool e2 = true;
+static bool e3 = true;
 
 //Methods to set pins to control direction.
 void stepAllRight()
@@ -311,6 +315,31 @@
     wait_us(fart);
 }
 
+
+void step1()
+{
+    //engine 1
+    IN1=1;
+    IN2=0;
+    IN3=0;
+    IN4=1;
+}
+
+void step2()
+{
+    //engine 1
+    IN1=0;
+    IN2=1;
+    IN3=0;
+    IN4=1;
+}
+
+void wait()
+{
+    wait_us(fart);
+    
+}
+
 //Method to release all engines.
 void stopAll()
 {
@@ -358,6 +387,8 @@
 
 
 
+
+
 void input(void const *args)
 {
     while(true)
@@ -368,21 +399,63 @@
         
         }
          
-        Thread::wait(20);
+        Thread::wait(100);
         //pc.printf("%d", steps);
+        
     }
 }
 
+//SPECIFY WHICH BUTTON IS PRESSED. NEED ONE CALLBACK FOR EACH BUTTON.
+void pb_hit_callback (void) 
+{
+    m_cmd = 'x';
+    /*
+    
+    
+    */
+    
+
+}
+
+
 int main()
 {
+        pb.mode(PullUp);
+        pb.attach_deasserted(&pb_hit_callback);
+        pb.setSampleFrequency();
+
         //Thread 1 has constant feed from usb
         Thread t1(input);
         //static char global_direction;
         printf("Started");
+
         
         /*
         
         BOOT UP AND SYNCHRONIZE
+        while (true)
+        {
+            should move one engine at a time. If not, alot of work has to be done in if/else statements as well as new engine methods.. 
+            while(moving bottom part 180 degrees to one side  / steps < 0)
+            {
+                step bottom engine in one direction.
+                callback will fire and stop the engine
+            }
+            while(moving middle part 180 degrees to one side  / steps < 0)
+            {
+                step middle engine in one direction
+                callback fires and stops the engine
+            }  
+            while(moving middle part 180 degrees to one side  / steps < 0)
+            {
+                step middle engine in one direction
+                callback fires and stops the engine
+            }
+            //Break the while loop (FALSE)
+            break;
+            
+            
+        }
         
         */
         
@@ -390,6 +463,20 @@
         
         while (true) 
         {   
+        
+            if (m_cmd == 't')
+            {
+                int steps = 50;
+                while (steps <= 50)
+                {
+                    step1();
+                    wait();
+                }
+                
+            }
+        
+        
+        
             if(m_cmd == 'a')
             {
                 stepAllRight();
@@ -443,41 +530,74 @@
             if(m_cmd == '7')
             {
                 
-                stopE1();
-                stepEngine1Left();
+                
+                stepEngine3Left();
             }
             else if (m_cmd == '9')
             {
                 
-                stopE1();
-                stepEngine1Right();
+                
+                stepEngine3Right();
             }
             //ENGINE 2
             else if (m_cmd == '4')
             {
                 
-                stopE2();
+                
                 stepEngine2Left();
             }
             else if (m_cmd == '6')
             {
                 
-                stopE2();
+               
                 stepEngine2Right();
             }
             //ENGINE 3
             else if (m_cmd == '1')
             {
                 
-                stopE3();
-                stepEngine3Left();
+                
+                stepEngine1Left();
             }
             else if (m_cmd == '3')
             {
                 
-                stopE3();
-                stepEngine3Right();
+                
+                stepEngine1Right();
             }
+            
+            else if (m_cmd == 'y')
+            {
+                int a = 121;
+                int b = 60;
+                int c = 60;
+                
+                while (a >= 0)
+                {
+                    stepEngine1Left();
+                    a--;
+                }
+                while (b >= 0)
+                {
+                    stepEngine2Right();
+                    b--;
+                }
+                while (c >= 0)
+                {
+                    stepEngine3Left();
+                    c--;
+                }
+                
+                m_cmd = 'x';
+
+            }
+            /*
+            new_pb = pb;
+            if (new_pb == '1')
+            {
+                m_cmd ='x';
+            }
+            */