2 Motores + Joystick

Dependencies:   X_NUCLEO_IHM01A1 TextLCD

Fork of HelloWorld_IHM01A1_2Motors_mbedOS by ST

Committer:
leogrotti
Date:
Wed May 02 14:59:42 2018 +0000
Revision:
40:0b517b49f70d
Parent:
39:7e30bcc989d3
Child:
41:7f91e949ca88
3 motores + joystick + botao

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. */
Davidroid 24:8cb3c4ad055f 58 #define DELAY_1 2000
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 40:0b517b49f70d 71 DigitalIn button1(D2);
leogrotti 40:0b517b49f70d 72
Brunoporto2702 32:481674c0f90d 73 /* Initialization parameters. */
leogrotti 39:7e30bcc989d3 74 //L6474_init_t init = {
leogrotti 39:7e30bcc989d3 75 // 160, /* Acceleration rate in pps^2. Range: (0..+inf). */
leogrotti 39:7e30bcc989d3 76 // 160, /* Deceleration rate in pps^2. Range: (0..+inf). */
leogrotti 39:7e30bcc989d3 77 // 4000, /* Maximum speed in pps. Range: (30..10000]. */
leogrotti 39:7e30bcc989d3 78 // 1300, /* Minimum speed in pps. Range: [30..10000). */
leogrotti 39:7e30bcc989d3 79 // 1500, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
leogrotti 39:7e30bcc989d3 80 // L6474_OCD_TH_1875mA, /* Overcurrent threshold (OCD_TH register). */
leogrotti 39:7e30bcc989d3 81 // L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
leogrotti 39:7e30bcc989d3 82 // L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
leogrotti 39:7e30bcc989d3 83 // L6474_STEP_SEL_1_2, /* Step selection (STEP_SEL field of STEP_MODE register). */
leogrotti 39:7e30bcc989d3 84 // L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
leogrotti 39:7e30bcc989d3 85 // L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
leogrotti 39:7e30bcc989d3 86 // L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
leogrotti 39:7e30bcc989d3 87 // 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
leogrotti 39:7e30bcc989d3 88 // 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
leogrotti 39:7e30bcc989d3 89 // L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
leogrotti 39:7e30bcc989d3 90 // L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
leogrotti 39:7e30bcc989d3 91 // L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
leogrotti 39:7e30bcc989d3 92 // L6474_ALARM_EN_OVERCURRENT |
leogrotti 39:7e30bcc989d3 93 // L6474_ALARM_EN_THERMAL_SHUTDOWN |
leogrotti 39:7e30bcc989d3 94 // L6474_ALARM_EN_THERMAL_WARNING |
leogrotti 39:7e30bcc989d3 95 // L6474_ALARM_EN_UNDERVOLTAGE |
leogrotti 39:7e30bcc989d3 96 // L6474_ALARM_EN_SW_TURN_ON |
leogrotti 39:7e30bcc989d3 97 // L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
leogrotti 39:7e30bcc989d3 98 //};
Brunoporto2702 32:481674c0f90d 99
Brunoporto2702 32:481674c0f90d 100 Serial pc(USBTX, USBRX);
Brunoporto2702 32:481674c0f90d 101
Brunoporto2702 32:481674c0f90d 102 AnalogIn eixo_X(A0);
Brunoporto2702 32:481674c0f90d 103 AnalogIn eixo_Y(A1);
Brunoporto2702 32:481674c0f90d 104
Brunoporto2702 32:481674c0f90d 105
Brunoporto2702 32:481674c0f90d 106
Brunoporto2702 32:481674c0f90d 107 float x;
Brunoporto2702 32:481674c0f90d 108 float y;
Brunoporto2702 32:481674c0f90d 109 float w;
Brunoporto2702 32:481674c0f90d 110 float u;
Brunoporto2702 32:481674c0f90d 111
leogrotti 38:c3d77ff8168a 112 //float a;
leogrotti 36:0cd4fdbb40af 113
leogrotti 35:a05bef8dd995 114
leogrotti 35:a05bef8dd995 115
leogrotti 34:098efd69c86d 116 unsigned int minspeed = 1300;
Brunoporto2702 32:481674c0f90d 117
leogrotti 34:098efd69c86d 118 //int step = 0x08;
Brunoporto2702 32:481674c0f90d 119
leogrotti 35:a05bef8dd995 120 unsigned int speed1;
leogrotti 35:a05bef8dd995 121 unsigned int speed2;
leogrotti 39:7e30bcc989d3 122 unsigned int speed3;
leogrotti 36:0cd4fdbb40af 123
leogrotti 38:c3d77ff8168a 124 //unsigned int pos3;
Brunoporto2702 32:481674c0f90d 125
Davidroid 0:e6a49a092e2a 126
Davidroid 0:e6a49a092e2a 127 /* Motor Control Component. */
Davidroid 3:02d9ec4f88b2 128 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 129 L6474 *motor2;
leogrotti 39:7e30bcc989d3 130 L6474 *motor3;
Davidroid 0:e6a49a092e2a 131
leogrotti 40:0b517b49f70d 132 void flag_irq_handler(void)
leogrotti 40:0b517b49f70d 133 {
leogrotti 40:0b517b49f70d 134 /* Set ISR flag. */
leogrotti 40:0b517b49f70d 135 motor3->isr_flag = TRUE;
leogrotti 40:0b517b49f70d 136
leogrotti 40:0b517b49f70d 137 /* Get the value of the status register. */
leogrotti 40:0b517b49f70d 138 unsigned int status = motor3->get_status();
leogrotti 40:0b517b49f70d 139
leogrotti 40:0b517b49f70d 140 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
leogrotti 40:0b517b49f70d 141 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
leogrotti 40:0b517b49f70d 142 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
leogrotti 40:0b517b49f70d 143 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
leogrotti 40:0b517b49f70d 144 }
leogrotti 40:0b517b49f70d 145
leogrotti 40:0b517b49f70d 146 /* Reset ISR flag. */
leogrotti 40:0b517b49f70d 147 motor3->isr_flag = FALSE;
leogrotti 40:0b517b49f70d 148 }
leogrotti 40:0b517b49f70d 149
Davidroid 0:e6a49a092e2a 150
Davidroid 0:e6a49a092e2a 151 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 152
Davidroid 0:e6a49a092e2a 153 int main()
Davidroid 0:e6a49a092e2a 154 {
Davidroid 8:cec4c2c03a27 155 /*----- Initialization. -----*/
Davidroid 8:cec4c2c03a27 156
Davidroid 0:e6a49a092e2a 157 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 158 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 159
Davidroid 9:a9e51320aee4 160 /* Initializing Motor Control Components. */
Davidroid 5:a0268a435bb1 161 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 16:810667a9f31f 162 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
leogrotti 39:7e30bcc989d3 163 motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi);
Davidroid 29:526970c1d998 164 if (motor1->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 165 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 166 }
Davidroid 29:526970c1d998 167 if (motor2->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 168 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 169 }
leogrotti 39:7e30bcc989d3 170 if (motor3->init() != COMPONENT_OK) {
leogrotti 39:7e30bcc989d3 171 exit(EXIT_FAILURE);
leogrotti 39:7e30bcc989d3 172 }
Davidroid 0:e6a49a092e2a 173
leogrotti 35:a05bef8dd995 174
Davidroid 8:cec4c2c03a27 175
leogrotti 35:a05bef8dd995 176 /*motor1->set_acceleration(160);
leogrotti 35:a05bef8dd995 177 motor2->set_acceleration(160);
leogrotti 35:a05bef8dd995 178 motor1->set_max_speed(speed1);
leogrotti 35:a05bef8dd995 179 motor2->set_max_speed(speed2);*/
leogrotti 36:0cd4fdbb40af 180
Davidroid 8:cec4c2c03a27 181
leogrotti 36:0cd4fdbb40af 182
leogrotti 36:0cd4fdbb40af 183
leogrotti 36:0cd4fdbb40af 184 //motor1->set_min_speed(1300);
leogrotti 36:0cd4fdbb40af 185 //motor2->set_min_speed(1300);
leogrotti 36:0cd4fdbb40af 186
leogrotti 36:0cd4fdbb40af 187 //motor1->set_parameter(L6474_STEP_MODE,L6474_STEP_SEL_1_2);
leogrotti 36:0cd4fdbb40af 188 //motor2->set_parameter(L6474_STEP_MODE,L6474_STEP_SEL_1_2);
Davidroid 8:cec4c2c03a27 189
leogrotti 39:7e30bcc989d3 190
leogrotti 40:0b517b49f70d 191 /* Attaching and enabling interrupt handlers. */
leogrotti 40:0b517b49f70d 192 motor3->attach_flag_irq(&flag_irq_handler);
leogrotti 40:0b517b49f70d 193 motor3->enable_flag_irq();
leogrotti 39:7e30bcc989d3 194
leogrotti 39:7e30bcc989d3 195
Davidroid 28:3f17a4152bcf 196 while(true) {
Brunoporto2702 32:481674c0f90d 197 u = eixo_X.read();
Brunoporto2702 32:481674c0f90d 198 w = eixo_Y.read();
Brunoporto2702 32:481674c0f90d 199
leogrotti 40:0b517b49f70d 200 if (button1.read() == 0){
leogrotti 40:0b517b49f70d 201
leogrotti 39:7e30bcc989d3 202 motor3->run(StepperMotor::FWD);
leogrotti 40:0b517b49f70d 203 printf("button1 fwd \r\n\n");
leogrotti 39:7e30bcc989d3 204
leogrotti 40:0b517b49f70d 205 }
leogrotti 40:0b517b49f70d 206
leogrotti 40:0b517b49f70d 207 else {
leogrotti 40:0b517b49f70d 208 motor3->run(StepperMotor::BWD);
leogrotti 40:0b517b49f70d 209 printf("button1 bwd \r\n\n");
leogrotti 40:0b517b49f70d 210
leogrotti 40:0b517b49f70d 211 }
leogrotti 35:a05bef8dd995 212
leogrotti 35:a05bef8dd995 213
leogrotti 34:098efd69c86d 214 if (u>0.740) {
leogrotti 34:098efd69c86d 215
Brunoporto2702 32:481674c0f90d 216 motor1->run(StepperMotor::FWD);
leogrotti 39:7e30bcc989d3 217 //wait_ms(DELAY_1);
leogrotti 39:7e30bcc989d3 218
Brunoporto2702 32:481674c0f90d 219 speed1 = motor1->get_speed();
leogrotti 34:098efd69c86d 220
leogrotti 38:c3d77ff8168a 221 printf("speed1 =%d \r\n\n", speed1);
leogrotti 38:c3d77ff8168a 222 printf("U =%f \r\n\n", u);
leogrotti 34:098efd69c86d 223
Brunoporto2702 32:481674c0f90d 224 }
leogrotti 34:098efd69c86d 225 else{ if(u<0.65) {
leogrotti 34:098efd69c86d 226
Brunoporto2702 32:481674c0f90d 227 motor1->run(StepperMotor::BWD);
leogrotti 39:7e30bcc989d3 228 // wait_ms(DELAY_1);
leogrotti 39:7e30bcc989d3 229
Brunoporto2702 32:481674c0f90d 230 speed1 = motor1->get_speed();
leogrotti 34:098efd69c86d 231
leogrotti 38:c3d77ff8168a 232 printf("speed1 =%d \r\n\n", speed1);
leogrotti 35:a05bef8dd995 233
leogrotti 38:c3d77ff8168a 234 printf("U =%f \r\n\n", u);
leogrotti 34:098efd69c86d 235
Brunoporto2702 32:481674c0f90d 236 }
Brunoporto2702 32:481674c0f90d 237 else
Brunoporto2702 32:481674c0f90d 238 motor1->hard_stop();
leogrotti 39:7e30bcc989d3 239 // wait_ms(DELAY_1);
leogrotti 39:7e30bcc989d3 240
leogrotti 35:a05bef8dd995 241 speed1 = 0;
leogrotti 38:c3d77ff8168a 242 printf("speed1 =%d \r\n\n", speed1);
leogrotti 34:098efd69c86d 243
leogrotti 38:c3d77ff8168a 244 printf("U =%f \r\n\n", u);
leogrotti 34:098efd69c86d 245
Brunoporto2702 32:481674c0f90d 246 }
Brunoporto2702 32:481674c0f90d 247
leogrotti 34:098efd69c86d 248 if (w>0.76) {
leogrotti 34:098efd69c86d 249
leogrotti 34:098efd69c86d 250 motor2->run(StepperMotor::BWD);
Brunoporto2702 32:481674c0f90d 251 speed2 = motor2->get_speed();
leogrotti 35:a05bef8dd995 252
leogrotti 38:c3d77ff8168a 253 printf("speed2 =%d \r\n\n", speed2);
leogrotti 38:c3d77ff8168a 254 printf("W =%f \r\n\n", w);
leogrotti 34:098efd69c86d 255
Brunoporto2702 32:481674c0f90d 256 }
leogrotti 34:098efd69c86d 257 else{ if(w<0.67) {
leogrotti 34:098efd69c86d 258
leogrotti 34:098efd69c86d 259
leogrotti 34:098efd69c86d 260 motor2->run(StepperMotor::FWD);
leogrotti 35:a05bef8dd995 261 speed2 = motor2->get_speed();
leogrotti 35:a05bef8dd995 262
leogrotti 38:c3d77ff8168a 263 printf("speed2 =%d \r\n\n", speed2);
leogrotti 38:c3d77ff8168a 264 printf("W =%f \r\n\n", w);
leogrotti 34:098efd69c86d 265
Brunoporto2702 32:481674c0f90d 266 }
Brunoporto2702 32:481674c0f90d 267 else
Brunoporto2702 32:481674c0f90d 268 motor2->hard_stop();
leogrotti 35:a05bef8dd995 269 speed2 = 0;
leogrotti 38:c3d77ff8168a 270 printf("speed2 =%d \r\n\n", speed2);
leogrotti 38:c3d77ff8168a 271 printf("W =%f \r\n\n", w);
leogrotti 34:098efd69c86d 272
Brunoporto2702 32:481674c0f90d 273 }
leogrotti 36:0cd4fdbb40af 274
leogrotti 36:0cd4fdbb40af 275
Davidroid 0:e6a49a092e2a 276 }
Brunoporto2702 32:481674c0f90d 277 }