funzionamento un motore

Dependencies:   X_NUCLEO_IHM01A1 mbed Motori_ultrasuoni_prova

Fork of HelloWorld_IHM01A1_2Motors by ST

Committer:
Davidroid
Date:
Fri Mar 10 14:23:19 2017 +0000
Revision:
29:526970c1d998
Parent:
28:3f17a4152bcf
Child:
30:f3d8978c68d4
Aligned to ARM mbed coding style.

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). */
Davidroid 24:8cb3c4ad055f 65 #define SPEED_1 2400
Davidroid 24:8cb3c4ad055f 66 #define SPEED_2 1200
Davidroid 24:8cb3c4ad055f 67
Davidroid 0:e6a49a092e2a 68
Davidroid 0:e6a49a092e2a 69 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 70
Davidroid 0:e6a49a092e2a 71 /* Motor Control Component. */
Davidroid 3:02d9ec4f88b2 72 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 73 L6474 *motor2;
Davidroid 0:e6a49a092e2a 74
Davidroid 0:e6a49a092e2a 75
Davidroid 0:e6a49a092e2a 76 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 77
Davidroid 0:e6a49a092e2a 78 int main()
Davidroid 0:e6a49a092e2a 79 {
Davidroid 8:cec4c2c03a27 80 /*----- Initialization. -----*/
Davidroid 8:cec4c2c03a27 81
Davidroid 0:e6a49a092e2a 82 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 83 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 84
Davidroid 9:a9e51320aee4 85 /* Initializing Motor Control Components. */
Davidroid 5:a0268a435bb1 86 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 16:810667a9f31f 87 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
Davidroid 29:526970c1d998 88 if (motor1->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 89 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 90 }
Davidroid 29:526970c1d998 91 if (motor2->init() != COMPONENT_OK) {
Davidroid 14:fcd452b03d1a 92 exit(EXIT_FAILURE);
Davidroid 28:3f17a4152bcf 93 }
Davidroid 0:e6a49a092e2a 94
Davidroid 0:e6a49a092e2a 95 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 96 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:e6a49a092e2a 97
Davidroid 8:cec4c2c03a27 98
Davidroid 8:cec4c2c03a27 99 /*----- Moving. -----*/
Davidroid 8:cec4c2c03a27 100
Davidroid 8:cec4c2c03a27 101 /* Printing to the console. */
Davidroid 9:a9e51320aee4 102 printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
Davidroid 8:cec4c2c03a27 103
Davidroid 8:cec4c2c03a27 104 /* Moving N steps in the forward direction. */
Davidroid 29:526970c1d998 105 motor1->move(StepperMotor::FWD, STEPS >> 1);
Davidroid 29:526970c1d998 106 motor2->move(StepperMotor::FWD, STEPS);
Davidroid 8:cec4c2c03a27 107
Davidroid 8:cec4c2c03a27 108 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 109 motor1->wait_while_active();
Davidroid 29:526970c1d998 110 motor2->wait_while_active();
Davidroid 8:cec4c2c03a27 111
Davidroid 8:cec4c2c03a27 112 /* Getting current position. */
Davidroid 29:526970c1d998 113 int position1 = motor1->get_position();
Davidroid 29:526970c1d998 114 int position2 = motor2->get_position();
Davidroid 8:cec4c2c03a27 115
Davidroid 8:cec4c2c03a27 116 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 117 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 118
Davidroid 8:cec4c2c03a27 119 /* Waiting 2 seconds. */
Davidroid 24:8cb3c4ad055f 120 wait_ms(DELAY_1);
Davidroid 8:cec4c2c03a27 121
Davidroid 8:cec4c2c03a27 122
Davidroid 8:cec4c2c03a27 123 /*----- Moving. -----*/
Davidroid 8:cec4c2c03a27 124
Davidroid 8:cec4c2c03a27 125 /* Printing to the console. */
Davidroid 9:a9e51320aee4 126 printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
Davidroid 8:cec4c2c03a27 127
Davidroid 8:cec4c2c03a27 128
Davidroid 8:cec4c2c03a27 129 /* Moving N steps in the backward direction. */
Davidroid 29:526970c1d998 130 motor1->move(StepperMotor::BWD, STEPS >> 1);
Davidroid 29:526970c1d998 131 motor2->move(StepperMotor::BWD, STEPS);
Davidroid 8:cec4c2c03a27 132
Davidroid 8:cec4c2c03a27 133 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 134 motor1->wait_while_active();
Davidroid 29:526970c1d998 135 motor2->wait_while_active();
Davidroid 8:cec4c2c03a27 136
Davidroid 8:cec4c2c03a27 137 /* Getting current position. */
Davidroid 29:526970c1d998 138 position1 = motor1->get_position();
Davidroid 29:526970c1d998 139 position2 = motor2->get_position();
Davidroid 8:cec4c2c03a27 140
Davidroid 8:cec4c2c03a27 141 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 142 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 143 printf("--> Setting Home.\r\n");
Davidroid 8:cec4c2c03a27 144
Davidroid 8:cec4c2c03a27 145 /* Setting the current position to be the home position. */
Davidroid 29:526970c1d998 146 motor1->set_home();
Davidroid 29:526970c1d998 147 motor2->set_home();
Davidroid 8:cec4c2c03a27 148
Davidroid 8:cec4c2c03a27 149 /* Waiting 2 seconds. */
Davidroid 24:8cb3c4ad055f 150 wait_ms(DELAY_1);
Davidroid 8:cec4c2c03a27 151
Davidroid 8:cec4c2c03a27 152
Davidroid 8:cec4c2c03a27 153 /*----- Going to a specified position. -----*/
Davidroid 8:cec4c2c03a27 154
Davidroid 8:cec4c2c03a27 155 /* Printing to the console. */
Davidroid 9:a9e51320aee4 156 printf("--> Going to position: M1 %d, M2 %d.\r\n", STEPS, STEPS >> 1);
Davidroid 8:cec4c2c03a27 157
Davidroid 8:cec4c2c03a27 158 /* Requesting to go to a specified position. */
Davidroid 29:526970c1d998 159 motor1->go_to(STEPS);
Davidroid 29:526970c1d998 160 motor2->go_to(STEPS >> 1);
Davidroid 8:cec4c2c03a27 161
Davidroid 8:cec4c2c03a27 162 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 163 motor1->wait_while_active();
Davidroid 29:526970c1d998 164 motor2->wait_while_active();
Davidroid 8:cec4c2c03a27 165
Davidroid 8:cec4c2c03a27 166 /* Getting current position. */
Davidroid 29:526970c1d998 167 position1 = motor1->get_position();
Davidroid 29:526970c1d998 168 position2 = motor2->get_position();
Davidroid 8:cec4c2c03a27 169
Davidroid 8:cec4c2c03a27 170 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 171 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 172
Davidroid 8:cec4c2c03a27 173 /* Waiting 2 seconds. */
Davidroid 24:8cb3c4ad055f 174 wait_ms(DELAY_1);
Davidroid 8:cec4c2c03a27 175
Davidroid 8:cec4c2c03a27 176
Davidroid 8:cec4c2c03a27 177 /*----- Going Home. -----*/
Davidroid 8:cec4c2c03a27 178
Davidroid 8:cec4c2c03a27 179 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 180 printf("--> Going Home.\r\n");
Davidroid 8:cec4c2c03a27 181
Davidroid 8:cec4c2c03a27 182 /* Requesting to go to home. */
Davidroid 29:526970c1d998 183 motor1->go_home();
Davidroid 29:526970c1d998 184 motor2->go_home();
Davidroid 8:cec4c2c03a27 185
Davidroid 8:cec4c2c03a27 186 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 187 motor1->wait_while_active();
Davidroid 29:526970c1d998 188 motor2->wait_while_active();
Davidroid 8:cec4c2c03a27 189
Davidroid 8:cec4c2c03a27 190 /* Getting current position. */
Davidroid 29:526970c1d998 191 position1 = motor1->get_position();
Davidroid 29:526970c1d998 192 position2 = motor2->get_position();
Davidroid 0:e6a49a092e2a 193
Davidroid 8:cec4c2c03a27 194 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 195 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 196
Davidroid 8:cec4c2c03a27 197 /* Waiting 2 seconds. */
Davidroid 24:8cb3c4ad055f 198 wait_ms(DELAY_1);
Davidroid 8:cec4c2c03a27 199
Davidroid 8:cec4c2c03a27 200
Davidroid 8:cec4c2c03a27 201 /*----- Running. -----*/
Davidroid 8:cec4c2c03a27 202
Davidroid 8:cec4c2c03a27 203 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 204 printf("--> M1 running backward, M2 running forward.\r\n");
Davidroid 8:cec4c2c03a27 205
Davidroid 8:cec4c2c03a27 206 /* Requesting to run backward. */
Davidroid 29:526970c1d998 207 motor1->run(StepperMotor::BWD);
Davidroid 29:526970c1d998 208 motor2->run(StepperMotor::FWD);
Davidroid 8:cec4c2c03a27 209
Davidroid 8:cec4c2c03a27 210 /* Waiting until delay has expired. */
Davidroid 24:8cb3c4ad055f 211 wait_ms(DELAY_2);
Davidroid 8:cec4c2c03a27 212
Davidroid 8:cec4c2c03a27 213 /* Getting current speed. */
Davidroid 29:526970c1d998 214 int speed1 = motor1->get_speed();
Davidroid 29:526970c1d998 215 int speed2 = motor2->get_speed();
Davidroid 8:cec4c2c03a27 216
Davidroid 8:cec4c2c03a27 217 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 218 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 8:cec4c2c03a27 219
Davidroid 8:cec4c2c03a27 220 /*----- Increasing the speed while running. -----*/
Davidroid 8:cec4c2c03a27 221
Davidroid 8:cec4c2c03a27 222 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 223 printf("--> Increasing the speed while running.\r\n");
Davidroid 8:cec4c2c03a27 224
Davidroid 24:8cb3c4ad055f 225 /* Increasing the speed. */
Davidroid 29:526970c1d998 226 motor1->set_max_speed(SPEED_1);
Davidroid 29:526970c1d998 227 motor2->set_max_speed(SPEED_1);
Davidroid 8:cec4c2c03a27 228
Davidroid 8:cec4c2c03a27 229 /* Waiting until delay has expired. */
Davidroid 24:8cb3c4ad055f 230 wait_ms(DELAY_2);
Davidroid 8:cec4c2c03a27 231
Davidroid 8:cec4c2c03a27 232 /* Getting current speed. */
Davidroid 29:526970c1d998 233 speed1 = motor1->get_speed();
Davidroid 29:526970c1d998 234 speed2 = motor2->get_speed();
Davidroid 8:cec4c2c03a27 235
Davidroid 8:cec4c2c03a27 236 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 237 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 8:cec4c2c03a27 238
Davidroid 8:cec4c2c03a27 239
Davidroid 8:cec4c2c03a27 240 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 241
Davidroid 8:cec4c2c03a27 242 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 243 printf("--> Decreasing the speed while running.\r\n");
Davidroid 8:cec4c2c03a27 244
Davidroid 24:8cb3c4ad055f 245 /* Decreasing the speed. */
Davidroid 29:526970c1d998 246 motor1->set_max_speed(SPEED_2);
Davidroid 29:526970c1d998 247 motor2->set_max_speed(SPEED_2);
Davidroid 8:cec4c2c03a27 248
Davidroid 8:cec4c2c03a27 249 /* Waiting until delay has expired. */
Davidroid 24:8cb3c4ad055f 250 wait_ms(DELAY_3);
Davidroid 8:cec4c2c03a27 251
Davidroid 8:cec4c2c03a27 252 /* Getting current speed. */
Davidroid 29:526970c1d998 253 speed1 = motor1->get_speed();
Davidroid 29:526970c1d998 254 speed2 = motor2->get_speed();
Davidroid 8:cec4c2c03a27 255
Davidroid 8:cec4c2c03a27 256 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 257 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 8:cec4c2c03a27 258
Davidroid 8:cec4c2c03a27 259
Davidroid 8:cec4c2c03a27 260 /*----- Requiring hard-stop while running. -----*/
Davidroid 8:cec4c2c03a27 261
Davidroid 8:cec4c2c03a27 262 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 263 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 8:cec4c2c03a27 264
Davidroid 8:cec4c2c03a27 265 /* Requesting to immediatly stop. */
Davidroid 29:526970c1d998 266 motor1->hard_stop();
Davidroid 29:526970c1d998 267 motor2->hard_stop();
Davidroid 8:cec4c2c03a27 268
Davidroid 8:cec4c2c03a27 269 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 270 motor1->wait_while_active();
Davidroid 29:526970c1d998 271 motor2->wait_while_active();
Davidroid 8:cec4c2c03a27 272
Davidroid 8:cec4c2c03a27 273 /* Waiting 2 seconds. */
Davidroid 24:8cb3c4ad055f 274 wait_ms(DELAY_1);
Davidroid 8:cec4c2c03a27 275
Davidroid 8:cec4c2c03a27 276
Davidroid 8:cec4c2c03a27 277 /*----- Infinite Loop. -----*/
Davidroid 8:cec4c2c03a27 278
Davidroid 8:cec4c2c03a27 279 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 280 printf("--> Infinite Loop...\r\n");
Davidroid 8:cec4c2c03a27 281
Davidroid 8:cec4c2c03a27 282 /* Setting the current position to be the home position. */
Davidroid 29:526970c1d998 283 motor1->set_home();
Davidroid 29:526970c1d998 284 motor2->set_home();
Davidroid 8:cec4c2c03a27 285
Davidroid 8:cec4c2c03a27 286 /* Infinite Loop. */
Davidroid 28:3f17a4152bcf 287 while(true) {
Davidroid 8:cec4c2c03a27 288 /* Requesting to go to a specified position. */
Davidroid 29:526970c1d998 289 motor1->go_to(STEPS >> 1);
Davidroid 29:526970c1d998 290 motor2->go_to(- (STEPS >> 1));
Davidroid 3:02d9ec4f88b2 291
Davidroid 0:e6a49a092e2a 292 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 293 motor1->wait_while_active();
Davidroid 29:526970c1d998 294 motor2->wait_while_active();
Davidroid 0:e6a49a092e2a 295
Davidroid 0:e6a49a092e2a 296 /* Requesting to go to a specified position. */
Davidroid 29:526970c1d998 297 motor1->go_to(- (STEPS >> 1));
Davidroid 29:526970c1d998 298 motor2->go_to(STEPS >> 1);
Davidroid 0:e6a49a092e2a 299
Davidroid 0:e6a49a092e2a 300 /* Waiting while the motor is active. */
Davidroid 29:526970c1d998 301 motor1->wait_while_active();
Davidroid 29:526970c1d998 302 motor2->wait_while_active();
Davidroid 0:e6a49a092e2a 303 }
Davidroid 0:e6a49a092e2a 304 }