2 Motores + Joystick

Dependencies:   X_NUCLEO_IHM01A1 TextLCD

Fork of HelloWorld_IHM01A1_2Motors_mbedOS by ST

Committer:
leogrotti
Date:
Tue May 15 14:23:01 2018 +0000
Revision:
42:a04bff02f231
Parent:
41:7f91e949ca88
Child:
43:d08a3f6b65b5
3 eixos funcionais sem interrupt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /**
Davidroid 0:e6a49a092e2a 2 ******************************************************************************
Davidroid 0:e6a49a092e2a 3 * @file main.cpp
Davidroid 17:aae1446c67f4 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 17:aae1446c67f4 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
Davidroid 3:02d9ec4f88b2 8 * Motor Control Expansion Board: control of 2 motors.
Davidroid 0:e6a49a092e2a 9 ******************************************************************************
Davidroid 0:e6a49a092e2a 10 * @attention
Davidroid 0:e6a49a092e2a 11 *
Davidroid 0:e6a49a092e2a 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:e6a49a092e2a 13 *
Davidroid 0:e6a49a092e2a 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:e6a49a092e2a 15 * are permitted provided that the following conditions are met:
Davidroid 0:e6a49a092e2a 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:e6a49a092e2a 17 * this list of conditions and the following disclaimer.
Davidroid 0:e6a49a092e2a 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:e6a49a092e2a 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:e6a49a092e2a 20 * and/or other materials provided with the distribution.
Davidroid 0:e6a49a092e2a 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:e6a49a092e2a 22 * may be used to endorse or promote products derived from this software
Davidroid 0:e6a49a092e2a 23 * without specific prior written permission.
Davidroid 0:e6a49a092e2a 24 *
Davidroid 0:e6a49a092e2a 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:e6a49a092e2a 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:e6a49a092e2a 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:e6a49a092e2a 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:e6a49a092e2a 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:e6a49a092e2a 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:e6a49a092e2a 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:e6a49a092e2a 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:e6a49a092e2a 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:e6a49a092e2a 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:e6a49a092e2a 35 *
Davidroid 0:e6a49a092e2a 36 ******************************************************************************
Davidroid 0:e6a49a092e2a 37 */
Davidroid 0:e6a49a092e2a 38
Davidroid 0:e6a49a092e2a 39
Davidroid 0:e6a49a092e2a 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 41
Davidroid 0:e6a49a092e2a 42 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 43 #include "mbed.h"
Davidroid 0:e6a49a092e2a 44
Davidroid 0:e6a49a092e2a 45 /* Helper header files. */
Davidroid 0:e6a49a092e2a 46 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 47
Davidroid 0:e6a49a092e2a 48 /* Component specific header files. */
Davidroid 29:526970c1d998 49 #include "L6474.h"
Davidroid 0:e6a49a092e2a 50
Davidroid 0:e6a49a092e2a 51
Davidroid 0:e6a49a092e2a 52 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 53
Davidroid 24:8cb3c4ad055f 54 /* Number of steps. */
Davidroid 9:a9e51320aee4 55 #define STEPS 3200
Davidroid 0:e6a49a092e2a 56
Davidroid 24:8cb3c4ad055f 57 /* Delay in milliseconds. */
leogrotti 42:a04bff02f231 58 #define DELAY_1 20
Davidroid 24:8cb3c4ad055f 59 #define DELAY_2 6000
Davidroid 24:8cb3c4ad055f 60 #define DELAY_3 8000
Davidroid 24:8cb3c4ad055f 61
Davidroid 24:8cb3c4ad055f 62 /* Speed in pps (Pulses Per Second).
Davidroid 24:8cb3c4ad055f 63 In Full Step mode: 1 pps = 1 step/s).
Davidroid 24:8cb3c4ad055f 64 In 1/N Step Mode: N pps = 1 step/s). */
leogrotti 34:098efd69c86d 65 #define SPEED_1 1300
leogrotti 35:a05bef8dd995 66 #define SPEED_2 1300
Davidroid 24:8cb3c4ad055f 67
Davidroid 0:e6a49a092e2a 68
Brunoporto2702 32:481674c0f90d 69
Davidroid 0:e6a49a092e2a 70 /* Variables -----------------------------------------------------------------*/
leogrotti 42:a04bff02f231 71 //InterruptIn sensorX1(PC_9);
leogrotti 42:a04bff02f231 72 //InterruptIn sensorY1(PA_14);
leogrotti 40:0b517b49f70d 73
leogrotti 42:a04bff02f231 74
Brunoporto2702 32:481674c0f90d 75
Brunoporto2702 32:481674c0f90d 76 Serial pc(USBTX, USBRX);
Brunoporto2702 32:481674c0f90d 77
leogrotti 42:a04bff02f231 78 AnalogIn eixo_X(A2);
leogrotti 42:a04bff02f231 79 AnalogIn eixo_Y(A3);
leogrotti 42:a04bff02f231 80 AnalogIn eixo_ZU(A0);
leogrotti 42:a04bff02f231 81 AnalogIn eixo_ZD(A1);
Brunoporto2702 32:481674c0f90d 82
Brunoporto2702 32:481674c0f90d 83
Brunoporto2702 32:481674c0f90d 84
Brunoporto2702 32:481674c0f90d 85 float x;
Brunoporto2702 32:481674c0f90d 86 float y;
Brunoporto2702 32:481674c0f90d 87 float w;
Brunoporto2702 32:481674c0f90d 88 float u;
leogrotti 42:a04bff02f231 89 float zu;
leogrotti 42:a04bff02f231 90 float zd;
Brunoporto2702 32:481674c0f90d 91
leogrotti 42:a04bff02f231 92 int count;
leogrotti 42:a04bff02f231 93
leogrotti 36:0cd4fdbb40af 94
leogrotti 42:a04bff02f231 95 bool flagX1=0;
leogrotti 42:a04bff02f231 96 bool flagY1=0;
leogrotti 35:a05bef8dd995 97
leogrotti 35:a05bef8dd995 98
leogrotti 34:098efd69c86d 99 unsigned int minspeed = 1300;
Brunoporto2702 32:481674c0f90d 100
leogrotti 34:098efd69c86d 101 //int step = 0x08;
Brunoporto2702 32:481674c0f90d 102
leogrotti 35:a05bef8dd995 103 unsigned int speed1;
leogrotti 35:a05bef8dd995 104 unsigned int speed2;
leogrotti 39:7e30bcc989d3 105 unsigned int speed3;
leogrotti 36:0cd4fdbb40af 106
leogrotti 38:c3d77ff8168a 107 //unsigned int pos3;
Brunoporto2702 32:481674c0f90d 108
Davidroid 0:e6a49a092e2a 109
Davidroid 0:e6a49a092e2a 110 /* Motor Control Component. */
Davidroid 3:02d9ec4f88b2 111 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 112 L6474 *motor2;
leogrotti 39:7e30bcc989d3 113 L6474 *motor3;
Davidroid 0:e6a49a092e2a 114
Davidroid 0:e6a49a092e2a 115
Davidroid 0:e6a49a092e2a 116 /* Main ----------------------------------------------------------------------*/
leogrotti 42:a04bff02f231 117 /*
leogrotti 41:7f91e949ca88 118 void released_1()
leogrotti 41:7f91e949ca88 119 {
leogrotti 41:7f91e949ca88 120 motor3->hard_stop();
leogrotti 41:7f91e949ca88 121 printf ("parou \r\n");
leogrotti 41:7f91e949ca88 122
leogrotti 41:7f91e949ca88 123 }
leogrotti 41:7f91e949ca88 124
leogrotti 41:7f91e949ca88 125 void released_2()
leogrotti 41:7f91e949ca88 126 {
leogrotti 41:7f91e949ca88 127 motor3->hard_stop();
leogrotti 41:7f91e949ca88 128 printf ("parou \r\n");
leogrotti 41:7f91e949ca88 129
leogrotti 41:7f91e949ca88 130 }
leogrotti 41:7f91e949ca88 131
leogrotti 42:a04bff02f231 132 void paraX1() {
leogrotti 42:a04bff02f231 133 flagX1 = 1;
leogrotti 42:a04bff02f231 134 motor2->hard_stop();
leogrotti 42:a04bff02f231 135
leogrotti 42:a04bff02f231 136 }
leogrotti 42:a04bff02f231 137
leogrotti 42:a04bff02f231 138 void continuaX1() {
leogrotti 42:a04bff02f231 139 flagX1 = 0;
leogrotti 42:a04bff02f231 140 }
leogrotti 42:a04bff02f231 141
leogrotti 42:a04bff02f231 142
leogrotti 42:a04bff02f231 143 void paraY1() {
leogrotti 42:a04bff02f231 144 flagY1 = 1;
leogrotti 42:a04bff02f231 145 motor1->hard_stop();
leogrotti 42:a04bff02f231 146
leogrotti 42:a04bff02f231 147 }
leogrotti 42:a04bff02f231 148
leogrotti 42:a04bff02f231 149 void continuaY1() {
leogrotti 42:a04bff02f231 150 flagY1 = 0;
leogrotti 42:a04bff02f231 151 }
leogrotti 42:a04bff02f231 152 */
leogrotti 42:a04bff02f231 153
Davidroid 0:e6a49a092e2a 154 int main()
Davidroid 0:e6a49a092e2a 155 {
Davidroid 8:cec4c2c03a27 156 /*----- Initialization. -----*/
Davidroid 8:cec4c2c03a27 157
Davidroid 0:e6a49a092e2a 158 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 159 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 160
Davidroid 9:a9e51320aee4 161 /* Initializing Motor Control Components. */
Davidroid 5:a0268a435bb1 162 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 16:810667a9f31f 163 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
leogrotti 39:7e30bcc989d3 164 motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi);
Davidroid 29:526970c1d998 165 if (motor1->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 166 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 167 }
Davidroid 29:526970c1d998 168 if (motor2->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 169 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 170 }
leogrotti 39:7e30bcc989d3 171 if (motor3->init() != COMPONENT_OK) {
leogrotti 39:7e30bcc989d3 172 exit(EXIT_FAILURE);
leogrotti 39:7e30bcc989d3 173 }
Davidroid 0:e6a49a092e2a 174
leogrotti 35:a05bef8dd995 175
Davidroid 8:cec4c2c03a27 176
Davidroid 8:cec4c2c03a27 177
leogrotti 39:7e30bcc989d3 178
leogrotti 42:a04bff02f231 179 /* Attaching and enabling interrupt handlers.
leogrotti 42:a04bff02f231 180 motor3->attach_flag_irq(&flag_irq_handler3);
leogrotti 40:0b517b49f70d 181 motor3->enable_flag_irq();
leogrotti 42:a04bff02f231 182 motor1->attach_flag_irq(&flag_irq_handler1);
leogrotti 42:a04bff02f231 183 motor1->enable_flag_irq();
leogrotti 42:a04bff02f231 184 motor2->attach_flag_irq(&flag_irq_handler2);
leogrotti 42:a04bff02f231 185 motor2->enable_flag_irq();
leogrotti 42:a04bff02f231 186 */
leogrotti 41:7f91e949ca88 187
leogrotti 42:a04bff02f231 188 //sensorX1.rise(&paraX1);
leogrotti 42:a04bff02f231 189 // sensorX1.fall(&continuaX1);
leogrotti 39:7e30bcc989d3 190
leogrotti 41:7f91e949ca88 191
leogrotti 42:a04bff02f231 192 //sensorY1.rise(&paraY1);
leogrotti 42:a04bff02f231 193 //sensorY1.fall(&continuaY1);
leogrotti 42:a04bff02f231 194
leogrotti 42:a04bff02f231 195
Davidroid 28:3f17a4152bcf 196 while(true) {
leogrotti 42:a04bff02f231 197 count = count +1;
leogrotti 42:a04bff02f231 198 // Leitura analógica
Brunoporto2702 32:481674c0f90d 199 u = eixo_X.read();
Brunoporto2702 32:481674c0f90d 200 w = eixo_Y.read();
leogrotti 42:a04bff02f231 201 zu = eixo_ZU.read();
leogrotti 42:a04bff02f231 202 zd = eixo_ZD.read();
leogrotti 42:a04bff02f231 203 if (count == 10000) {
leogrotti 42:a04bff02f231 204 printf("zu =%f \r\n\n", zu);
leogrotti 42:a04bff02f231 205 printf("zd =%f \r\n\n", zd);
leogrotti 42:a04bff02f231 206 count = 0;
leogrotti 42:a04bff02f231 207 }
leogrotti 41:7f91e949ca88 208
leogrotti 42:a04bff02f231 209 //printf("X =%i \r\n\n", flagX1);
leogrotti 42:a04bff02f231 210 // printf("Y =%i \r\n\n", flagY1);
leogrotti 42:a04bff02f231 211
leogrotti 42:a04bff02f231 212 // Movimentando eixo Z cima caso botão 1 apertado
leogrotti 42:a04bff02f231 213 if (zu >0.7) {
leogrotti 42:a04bff02f231 214 motor3->run(StepperMotor::FWD);
leogrotti 42:a04bff02f231 215 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 216 speed3 = motor3->get_speed();
leogrotti 42:a04bff02f231 217 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 218 }
leogrotti 34:098efd69c86d 219
leogrotti 42:a04bff02f231 220 // Parando eixo Z caso botão 1 e 2 liberado
leogrotti 42:a04bff02f231 221 if (zu< 0.7 && zd < 0.7){
leogrotti 42:a04bff02f231 222 motor3->hard_stop();
leogrotti 42:a04bff02f231 223 // wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 224 speed3 = 0;
leogrotti 42:a04bff02f231 225 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 226 }
leogrotti 42:a04bff02f231 227
leogrotti 42:a04bff02f231 228 // Movimentando eixo Z baixo caso botão 2 apertado
leogrotti 42:a04bff02f231 229 if (zd >0.7) {
leogrotti 42:a04bff02f231 230 motor3->run(StepperMotor::BWD);
leogrotti 42:a04bff02f231 231 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 232 speed3 = motor3->get_speed();
leogrotti 42:a04bff02f231 233 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 234 }
leogrotti 34:098efd69c86d 235
leogrotti 42:a04bff02f231 236 // Parando eixo Z caso botão 1 e 2 liberado
leogrotti 42:a04bff02f231 237 if (zu< 0.7 && zd < 0.7){
leogrotti 42:a04bff02f231 238 motor3->hard_stop();
leogrotti 42:a04bff02f231 239 // wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 240 speed3 = 0;
leogrotti 42:a04bff02f231 241 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 242 }
leogrotti 42:a04bff02f231 243
leogrotti 42:a04bff02f231 244
leogrotti 42:a04bff02f231 245 // Movimentando eixo Y duas direções com joystick
leogrotti 42:a04bff02f231 246 //if (flagY1 == 0 ) {
leogrotti 42:a04bff02f231 247
leogrotti 42:a04bff02f231 248 // Movimentando eixo Y fwd
leogrotti 42:a04bff02f231 249 if (u>0.820) {
leogrotti 42:a04bff02f231 250 motor1->run(StepperMotor::FWD);
leogrotti 42:a04bff02f231 251 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 252 speed1 = motor1->get_speed();
leogrotti 42:a04bff02f231 253 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 254 }
leogrotti 42:a04bff02f231 255 // Movimentando eixo Y bwd
leogrotti 42:a04bff02f231 256 else{ if(u<0.65) {
leogrotti 42:a04bff02f231 257 motor1->run(StepperMotor::BWD);
leogrotti 42:a04bff02f231 258 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 259 speed1 = motor1->get_speed();
leogrotti 42:a04bff02f231 260 // wait_ms(DELAY_1);
Brunoporto2702 32:481674c0f90d 261 }
leogrotti 42:a04bff02f231 262 // parando eixo Y
leogrotti 42:a04bff02f231 263 else
leogrotti 42:a04bff02f231 264 motor1->hard_stop();
leogrotti 42:a04bff02f231 265 //wait_ms(DELAY_1);
leogrotti 35:a05bef8dd995 266 speed1 = 0;
leogrotti 42:a04bff02f231 267 //wait_ms(DELAY_1);
leogrotti 42:a04bff02f231 268 //}
leogrotti 42:a04bff02f231 269 }
leogrotti 42:a04bff02f231 270
leogrotti 42:a04bff02f231 271 // Movimentando eixo X duas direções com joystick
leogrotti 42:a04bff02f231 272 //if (flagX1 == 0) {
leogrotti 42:a04bff02f231 273 // Movimentando eixo Y fwd
leogrotti 42:a04bff02f231 274 if (w>0.80) {
leogrotti 34:098efd69c86d 275 motor2->run(StepperMotor::BWD);
leogrotti 42:a04bff02f231 276 speed2 = motor2->get_speed();
leogrotti 38:c3d77ff8168a 277 printf("speed2 =%d \r\n\n", speed2);
leogrotti 38:c3d77ff8168a 278 printf("W =%f \r\n\n", w);
leogrotti 42:a04bff02f231 279 }
leogrotti 42:a04bff02f231 280 // Movimentando eixo Y Bwd
leogrotti 42:a04bff02f231 281 else{ if(w<0.60) {
leogrotti 34:098efd69c86d 282 motor2->run(StepperMotor::FWD);
leogrotti 42:a04bff02f231 283 speed2 = motor2->get_speed();
leogrotti 38:c3d77ff8168a 284 printf("speed2 =%d \r\n\n", speed2);
leogrotti 38:c3d77ff8168a 285 printf("W =%f \r\n\n", w);
leogrotti 42:a04bff02f231 286 }
leogrotti 42:a04bff02f231 287 // parando eixo X
leogrotti 42:a04bff02f231 288 else
Brunoporto2702 32:481674c0f90d 289 motor2->hard_stop();
leogrotti 35:a05bef8dd995 290 speed2 = 0;
leogrotti 38:c3d77ff8168a 291 printf("speed2 =%d \r\n\n", speed2);
Brunoporto2702 32:481674c0f90d 292 }
Davidroid 0:e6a49a092e2a 293 }
Brunoporto2702 32:481674c0f90d 294 }