Joystick controlando 3 motores

Dependencies:   mbed X_NUCLEO_IHM01A1

Files at this revision

API Documentation at this revision

Comitter:
Gabiuas
Date:
Tue Apr 30 13:11:04 2019 +0000
Parent:
31:3aae82e6353c
Commit message:
Joystick controlando 3 motores

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Apr 24 14:14:04 2019 +0000
+++ b/main.cpp	Tue Apr 30 13:11:04 2019 +0000
@@ -1,114 +1,118 @@
-/* Includes ------------------------------------------------------------------*/
-
-/* mbed specific header files. */
 #include "mbed.h"
-
-/* Helper header files. */
 #include "DevSPI.h"
-
-/* Component specific header files. */
 #include "L6474.h"
 
-
-/* Definitions ---------------------------------------------------------------*/
+#define VRx A0
+#define VRy A1
+#define VRz A2
+#define SW D5
 
 /* Number of steps. */
 #define STEPS 2400
 
-
 /* Delay in milliseconds. */
 #define DELAY_1 2000
 #define DELAY_2 6000
 #define DELAY_3 8000
 
 /* Speed in pps (Pulses Per Second).
-   In Full Step mode: 1 pps = 1 step/s).
-   In 1/N Step Mode:  N pps = 1 step/s). */
+ In Full Step mode: 1 pps = 1 step/s).
+ In 1/N Step Mode:  N pps = 1 step/s). */
 #define SPEED_1 2400
 #define SPEED_2 1200
 
 /* Joystick */
-
-#define T_MS 500                                 // Define the time between reads
-#define VRx A0                                   // Define the input pin for VRx pin
-#define VRy A1                                   // Define the input pin for VRy pin
-#define SW D5                                    // Define the input pin for SW pin
-
-/* Variables -----------------------------------------------------------------*/
+AnalogIn x_axis(VRx);                       // Create the analog x movement object
+AnalogIn y_axis(VRy);                       // Create the analog y movement object
 
 /* Motor Control Component. */
 L6474 *motor1;
 L6474 *motor2;
+L6474 *motor3;
 
-int main(){   
+int main(){
     /* Initializing SPI bus. */
     DevSPI dev_spi(D11, D12, D13);
-
+    
     /* Initializing Motor Control Components. */
     motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
     motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
+    motor3 = new L6474(D2, D8, D11, D5, D6, dev_spi); // Mudar Entradas
+    
     if (motor1->init() != COMPONENT_OK) {
         exit(EXIT_FAILURE);
     }
     if (motor2->init() != COMPONENT_OK) {
         exit(EXIT_FAILURE);
     }
+    if (motor3->init() != COMPONENT_OK) {
+        exit(EXIT_FAILURE);
+    }
 
-    AnalogIn x_axis(VRx);                       // Create the analog x movement object
-    AnalogIn y_axis(VRy);                       // Create the analog y movement object
-    DigitalIn button(SW, PullUp);               // Create the digital button object and setup internall pull-up resistor
- 
+    
     while(1)
-    {   
+    {
         printf("\n");
-        printf("\n X axis: %f", x_axis.read()); // Show the value of the X axis (0.0 to 1.0)
-//        x = x_axis.read();
-        printf("\n Y axis: %f", y_axis.read()); // Show the value of the Y axis (0.0 to 1.0)
-//        y = y_axis.read();
-        printf("\n Button: %d", button.read()); // Show the button status (0 is pressed, 1 is not pressed)
+        
+        
+        /*----- Movendo Eixo X -----*/
+        
+        printf("\n   X axis: %f", x_axis.read());
         
-        wait_ms(T_MS);                          // Wait time between reads
-        printf("Motor Control Application Example for 2 Motors\r\n\n");
-    
-    
-        /*----- Movendo pra frente -----*/
-
-        /* Printing to the console. */
-        printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
         if (x_axis.read()>0.6){
-            /* Moving N steps in the forward direction. */
+            printf("--> Movendo X frente; Motor 1");
             motor1->run(StepperMotor::FWD);
-            motor1->wait_while_active();
-            }
-        else if (y_axis.read()>0.6){
+        }
+        else if (x_axis.read()<0.4){
+            printf("--> Movendo X tras; Motor 1");
+            motor1->run(StepperMotor::BWD);
+        }
+        else if (0.4<x_axis.read() and x_axis.read()<0.6){
+            printf("--> Parado; motor 1");
+            motor1->soft_stop();
+        }
+        
+        
+        /*----- Movendo Eixo Y -----*/
+        
+        printf("              Y axis: %f", y_axis.read());
+        
+        if (y_axis.read()<0.4){
+            printf("--> Movendo Y frente; Motor 2");
             motor2->run(StepperMotor::FWD);
-            motor2->wait_while_active();
-            }
-    
-        int position1 = motor1->get_position();
-        int position2 = motor2->get_position();
-        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
-    
-        /*----- Movendo pra trás -----*/
-    
-        if (x_axis.read()<0.4){
-            /* Moving N steps in the backward direction. */
-            printf("--> Moving X backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
-            motor1->run(StepperMotor::BWD);
-            }
-        else if (y_axis.read()<0.4){
-            printf("--> Moving Y backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
+        }
+        else if (y_axis.read()>0.6){
+            printf("--> Movendo Y tras; Motor 2");
             motor2->run(StepperMotor::BWD);
-            }
-        else{
-            soft_stop()
-            }
-    
-        /* Getting current position. */
-        position1 = motor1->get_position();
-        position2 = motor2->get_position();
+        }
+        else if (0.4<y_axis.read() and y_axis.read()<0.6){
+            printf("--> Parado; motor 2");
+            motor2->soft_stop();
+        }
+        
+        
+        /*----- Movendo Eixo Z -----*/
+        
+        printf("              Z axis: %f", z_axis.read());
         
-        /* Printing to the console. */
-        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
+        if (z_axis.read()>0.6){
+            printf("--> Movendo Z frente; Motor 3");
+            motor3->run(StepperMotor::FWD);
+        }
+        else if (z_axis.read()<0.4){
+            printf("--> Movendo Z tras; Motor 3");
+            motor3->run(StepperMotor::BWD);
+        }
+        else if (0.4<z_axis.read() and x_axis.read()<0.6){
+            printf("--> Parado; motor 3");
+            motor3->soft_stop();
+        }
+        
+        
+        
+        if (button==0){
+            break;
+        }
+        
     }
-}
\ No newline at end of file
+}