Controle do sentido dos Eixos X e Y pelo Joystick

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
rebecams
Date:
Wed Apr 24 14:14:04 2019 +0000
Revision:
31:3aae82e6353c
Parent:
29:526970c1d998
Child:
32:06c343675a3e
Aula 16 - Joystick pra controlar o sentido dos Eixos X e Y com biblioteca do XNucleo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 2
Davidroid 0:e6a49a092e2a 3 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 4 #include "mbed.h"
Davidroid 0:e6a49a092e2a 5
Davidroid 0:e6a49a092e2a 6 /* Helper header files. */
Davidroid 0:e6a49a092e2a 7 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 8
Davidroid 0:e6a49a092e2a 9 /* Component specific header files. */
Davidroid 29:526970c1d998 10 #include "L6474.h"
Davidroid 0:e6a49a092e2a 11
Davidroid 0:e6a49a092e2a 12
Davidroid 0:e6a49a092e2a 13 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 14
Davidroid 24:8cb3c4ad055f 15 /* Number of steps. */
rebecams 31:3aae82e6353c 16 #define STEPS 2400
rebecams 31:3aae82e6353c 17
Davidroid 0:e6a49a092e2a 18
Davidroid 24:8cb3c4ad055f 19 /* Delay in milliseconds. */
Davidroid 24:8cb3c4ad055f 20 #define DELAY_1 2000
Davidroid 24:8cb3c4ad055f 21 #define DELAY_2 6000
Davidroid 24:8cb3c4ad055f 22 #define DELAY_3 8000
Davidroid 24:8cb3c4ad055f 23
Davidroid 24:8cb3c4ad055f 24 /* Speed in pps (Pulses Per Second).
Davidroid 24:8cb3c4ad055f 25 In Full Step mode: 1 pps = 1 step/s).
Davidroid 24:8cb3c4ad055f 26 In 1/N Step Mode: N pps = 1 step/s). */
Davidroid 24:8cb3c4ad055f 27 #define SPEED_1 2400
Davidroid 24:8cb3c4ad055f 28 #define SPEED_2 1200
Davidroid 24:8cb3c4ad055f 29
rebecams 31:3aae82e6353c 30 /* Joystick */
rebecams 31:3aae82e6353c 31
rebecams 31:3aae82e6353c 32 #define T_MS 500 // Define the time between reads
rebecams 31:3aae82e6353c 33 #define VRx A0 // Define the input pin for VRx pin
rebecams 31:3aae82e6353c 34 #define VRy A1 // Define the input pin for VRy pin
rebecams 31:3aae82e6353c 35 #define SW D5 // Define the input pin for SW pin
Davidroid 0:e6a49a092e2a 36
Davidroid 0:e6a49a092e2a 37 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 38
Davidroid 0:e6a49a092e2a 39 /* Motor Control Component. */
Davidroid 3:02d9ec4f88b2 40 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 41 L6474 *motor2;
Davidroid 0:e6a49a092e2a 42
rebecams 31:3aae82e6353c 43 int main(){
Davidroid 0:e6a49a092e2a 44 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 45 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 46
Davidroid 9:a9e51320aee4 47 /* Initializing Motor Control Components. */
Davidroid 5:a0268a435bb1 48 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 16:810667a9f31f 49 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
Davidroid 29:526970c1d998 50 if (motor1->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 51 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 52 }
Davidroid 29:526970c1d998 53 if (motor2->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 54 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 55 }
Davidroid 0:e6a49a092e2a 56
rebecams 31:3aae82e6353c 57 AnalogIn x_axis(VRx); // Create the analog x movement object
rebecams 31:3aae82e6353c 58 AnalogIn y_axis(VRy); // Create the analog y movement object
rebecams 31:3aae82e6353c 59 DigitalIn button(SW, PullUp); // Create the digital button object and setup internall pull-up resistor
rebecams 31:3aae82e6353c 60
rebecams 31:3aae82e6353c 61 while(1)
rebecams 31:3aae82e6353c 62 {
rebecams 31:3aae82e6353c 63 printf("\n");
rebecams 31:3aae82e6353c 64 printf("\n X axis: %f", x_axis.read()); // Show the value of the X axis (0.0 to 1.0)
rebecams 31:3aae82e6353c 65 // x = x_axis.read();
rebecams 31:3aae82e6353c 66 printf("\n Y axis: %f", y_axis.read()); // Show the value of the Y axis (0.0 to 1.0)
rebecams 31:3aae82e6353c 67 // y = y_axis.read();
rebecams 31:3aae82e6353c 68 printf("\n Button: %d", button.read()); // Show the button status (0 is pressed, 1 is not pressed)
rebecams 31:3aae82e6353c 69
rebecams 31:3aae82e6353c 70 wait_ms(T_MS); // Wait time between reads
rebecams 31:3aae82e6353c 71 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 8:cec4c2c03a27 72
Davidroid 8:cec4c2c03a27 73
rebecams 31:3aae82e6353c 74 /*----- Movendo pra frente -----*/
Davidroid 8:cec4c2c03a27 75
rebecams 31:3aae82e6353c 76 /* Printing to the console. */
rebecams 31:3aae82e6353c 77 printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
rebecams 31:3aae82e6353c 78 if (x_axis.read()>0.6){
rebecams 31:3aae82e6353c 79 /* Moving N steps in the forward direction. */
rebecams 31:3aae82e6353c 80 motor1->run(StepperMotor::FWD);
rebecams 31:3aae82e6353c 81 motor1->wait_while_active();
rebecams 31:3aae82e6353c 82 }
rebecams 31:3aae82e6353c 83 else if (y_axis.read()>0.6){
rebecams 31:3aae82e6353c 84 motor2->run(StepperMotor::FWD);
rebecams 31:3aae82e6353c 85 motor2->wait_while_active();
rebecams 31:3aae82e6353c 86 }
rebecams 31:3aae82e6353c 87
rebecams 31:3aae82e6353c 88 int position1 = motor1->get_position();
rebecams 31:3aae82e6353c 89 int position2 = motor2->get_position();
rebecams 31:3aae82e6353c 90 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
rebecams 31:3aae82e6353c 91
rebecams 31:3aae82e6353c 92 /*----- Movendo pra trás -----*/
rebecams 31:3aae82e6353c 93
rebecams 31:3aae82e6353c 94 if (x_axis.read()<0.4){
rebecams 31:3aae82e6353c 95 /* Moving N steps in the backward direction. */
rebecams 31:3aae82e6353c 96 printf("--> Moving X backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
rebecams 31:3aae82e6353c 97 motor1->run(StepperMotor::BWD);
rebecams 31:3aae82e6353c 98 }
rebecams 31:3aae82e6353c 99 else if (y_axis.read()<0.4){
rebecams 31:3aae82e6353c 100 printf("--> Moving Y backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
rebecams 31:3aae82e6353c 101 motor2->run(StepperMotor::BWD);
rebecams 31:3aae82e6353c 102 }
rebecams 31:3aae82e6353c 103 else{
rebecams 31:3aae82e6353c 104 soft_stop()
rebecams 31:3aae82e6353c 105 }
rebecams 31:3aae82e6353c 106
rebecams 31:3aae82e6353c 107 /* Getting current position. */
rebecams 31:3aae82e6353c 108 position1 = motor1->get_position();
rebecams 31:3aae82e6353c 109 position2 = motor2->get_position();
rebecams 31:3aae82e6353c 110
rebecams 31:3aae82e6353c 111 /* Printing to the console. */
rebecams 31:3aae82e6353c 112 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 0:e6a49a092e2a 113 }
rebecams 31:3aae82e6353c 114 }