
Controle do sentido dos Eixos X e Y pelo Joystick
Dependencies: mbed X_NUCLEO_IHM01A1
main.cpp@32:06c343675a3e, 2019-04-24 (annotated)
- Committer:
- rebecams
- Date:
- Wed Apr 24 18:58:10 2019 +0000
- Revision:
- 32:06c343675a3e
- Parent:
- 31:3aae82e6353c
joystick para eixos X+ X- Y+ Y-
Who changed what in which revision?
User | Revision | Line number | New 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 | 32:06c343675a3e | 68 | //the button status (0 is pressed, 1 is not pressed) |
Davidroid | 8:cec4c2c03a27 | 69 | |
rebecams | 31:3aae82e6353c | 70 | /*----- Movendo pra frente -----*/ |
Davidroid | 8:cec4c2c03a27 | 71 | |
rebecams | 31:3aae82e6353c | 72 | /* Printing to the console. */ |
rebecams | 31:3aae82e6353c | 73 | if (x_axis.read()>0.6){ |
rebecams | 31:3aae82e6353c | 74 | /* Moving N steps in the forward direction. */ |
rebecams | 32:06c343675a3e | 75 | printf("--> Moving forward Motor 1"); |
rebecams | 31:3aae82e6353c | 76 | motor1->run(StepperMotor::FWD); |
rebecams | 31:3aae82e6353c | 77 | } |
rebecams | 32:06c343675a3e | 78 | if (y_axis.read()>0.6){ |
rebecams | 32:06c343675a3e | 79 | printf("--> Moving forward Motor 2"); |
rebecams | 31:3aae82e6353c | 80 | motor2->run(StepperMotor::FWD); |
rebecams | 31:3aae82e6353c | 81 | } |
rebecams | 31:3aae82e6353c | 82 | |
rebecams | 31:3aae82e6353c | 83 | int position1 = motor1->get_position(); |
rebecams | 31:3aae82e6353c | 84 | int position2 = motor2->get_position(); |
rebecams | 32:06c343675a3e | 85 | //printf(" Position: Motor 1 %d, Motor 2 %d.\r\n", position1, position2); |
rebecams | 31:3aae82e6353c | 86 | |
rebecams | 31:3aae82e6353c | 87 | /*----- Movendo pra trás -----*/ |
rebecams | 31:3aae82e6353c | 88 | if (x_axis.read()<0.4){ |
rebecams | 31:3aae82e6353c | 89 | /* Moving N steps in the backward direction. */ |
rebecams | 32:06c343675a3e | 90 | printf("--> Moving X backward Motor 1"); |
rebecams | 31:3aae82e6353c | 91 | motor1->run(StepperMotor::BWD); |
rebecams | 31:3aae82e6353c | 92 | } |
rebecams | 32:06c343675a3e | 93 | if (y_axis.read()<0.4){ |
rebecams | 32:06c343675a3e | 94 | printf("--> Moving Y backward Motor 2"); |
rebecams | 31:3aae82e6353c | 95 | motor2->run(StepperMotor::BWD); |
rebecams | 31:3aae82e6353c | 96 | } |
rebecams | 32:06c343675a3e | 97 | if (0.4<x_axis.read() and x_axis.read()<0.6){ |
rebecams | 32:06c343675a3e | 98 | printf("--> Parado motor 1"); |
rebecams | 32:06c343675a3e | 99 | motor1->soft_stop(); |
rebecams | 31:3aae82e6353c | 100 | } |
rebecams | 32:06c343675a3e | 101 | if (0.4<y_axis.read() and y_axis.read()<0.6){ |
rebecams | 32:06c343675a3e | 102 | printf("--> Parado motor 2"); |
rebecams | 32:06c343675a3e | 103 | motor2->soft_stop(); |
rebecams | 32:06c343675a3e | 104 | } |
Davidroid | 0:e6a49a092e2a | 105 | } |
rebecams | 31:3aae82e6353c | 106 | } |