Joystick controlando 3 motores

Dependencies:   mbed X_NUCLEO_IHM01A1

main.cpp

Committer:
rebecams
Date:
2019-04-24
Revision:
31:3aae82e6353c
Parent:
29:526970c1d998
Child:
32:74a4c733c11b

File content as of revision 31:3aae82e6353c:

/* Includes ------------------------------------------------------------------*/

/* mbed specific header files. */
#include "mbed.h"

/* Helper header files. */
#include "DevSPI.h"

/* Component specific header files. */
#include "L6474.h"


/* Definitions ---------------------------------------------------------------*/

/* 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). */
#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 -----------------------------------------------------------------*/

/* Motor Control Component. */
L6474 *motor1;
L6474 *motor2;

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);
    if (motor1->init() != COMPONENT_OK) {
        exit(EXIT_FAILURE);
    }
    if (motor2->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)
        
        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. */
            motor1->run(StepperMotor::FWD);
            motor1->wait_while_active();
            }
        else if (y_axis.read()>0.6){
            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);
            motor2->run(StepperMotor::BWD);
            }
        else{
            soft_stop()
            }
    
        /* Getting current position. */
        position1 = motor1->get_position();
        position2 = motor2->get_position();
        
        /* Printing to the console. */
        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
    }
}