Codice per pilotare il motore IHM01A1

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
Davidroid
Date:
Fri Nov 27 11:29:34 2015 +0000
Revision:
11:3e303a25770d
Parent:
10:8f031470ca9f
Child:
12:e218356f35bd
+ Updated with the new version of the library.

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 7:1d8a73b24eba 4 * @author Davide Aliprandi, STMicrolectronics
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 0:e6a49a092e2a 7 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
Davidroid 0:e6a49a092e2a 8 * Motor Control Expansion Board: control of 1 motor.
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 0:e6a49a092e2a 49 #include "l6474_class.h"
Davidroid 0:e6a49a092e2a 50
Davidroid 0:e6a49a092e2a 51
Davidroid 0:e6a49a092e2a 52 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 53
Davidroid 10:8f031470ca9f 54 /* Number of steps to move. */
Davidroid 10:8f031470ca9f 55 #define STEPS 3200
Davidroid 0:e6a49a092e2a 56
Davidroid 0:e6a49a092e2a 57
Davidroid 0:e6a49a092e2a 58 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 59
Davidroid 0:e6a49a092e2a 60 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 61 L6474 *motor;
Davidroid 0:e6a49a092e2a 62
Davidroid 0:e6a49a092e2a 63
Davidroid 0:e6a49a092e2a 64 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 65
Davidroid 0:e6a49a092e2a 66 int main()
Davidroid 0:e6a49a092e2a 67 {
Davidroid 6:32166bfc04b0 68 /*----- Initialization. -----*/
Davidroid 6:32166bfc04b0 69
Davidroid 0:e6a49a092e2a 70 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 71 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 72
Davidroid 11:3e303a25770d 73 /* Initializing Motor Control Component. */
Davidroid 5:8065b587ade0 74 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 1:fbf28f3367aa 75 if (motor->Init(NULL) != COMPONENT_OK)
Davidroid 0:e6a49a092e2a 76 return false;
Davidroid 0:e6a49a092e2a 77
Davidroid 0:e6a49a092e2a 78 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 79 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:e6a49a092e2a 80
Davidroid 6:32166bfc04b0 81
Davidroid 6:32166bfc04b0 82 /*----- Moving. -----*/
Davidroid 6:32166bfc04b0 83
Davidroid 6:32166bfc04b0 84 /* Printing to the console. */
Davidroid 10:8f031470ca9f 85 printf("--> Moving forward %d steps.\r\n", STEPS);
Davidroid 6:32166bfc04b0 86
Davidroid 6:32166bfc04b0 87 /* Moving N steps in the forward direction. */
Davidroid 10:8f031470ca9f 88 motor->Move(StepperMotor::FWD, STEPS);
Davidroid 6:32166bfc04b0 89
Davidroid 6:32166bfc04b0 90 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 91 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 92
Davidroid 6:32166bfc04b0 93 /* Getting current position. */
Davidroid 6:32166bfc04b0 94 int position = motor->GetPosition();
Davidroid 6:32166bfc04b0 95
Davidroid 6:32166bfc04b0 96 /* Printing to the console. */
Davidroid 6:32166bfc04b0 97 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 98
Davidroid 6:32166bfc04b0 99 /* Waiting 2 seconds. */
Davidroid 6:32166bfc04b0 100 wait_ms(2000);
Davidroid 6:32166bfc04b0 101
Davidroid 6:32166bfc04b0 102
Davidroid 6:32166bfc04b0 103 /*----- Moving. -----*/
Davidroid 6:32166bfc04b0 104
Davidroid 6:32166bfc04b0 105 /* Printing to the console. */
Davidroid 10:8f031470ca9f 106 printf("--> Moving backward %d steps.\r\n", STEPS >> 1);
Davidroid 6:32166bfc04b0 107
Davidroid 6:32166bfc04b0 108 /* Moving N steps in the backward direction. */
Davidroid 10:8f031470ca9f 109 motor->Move(StepperMotor::BWD, STEPS >> 1);
Davidroid 6:32166bfc04b0 110
Davidroid 6:32166bfc04b0 111 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 112 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 113
Davidroid 6:32166bfc04b0 114 /* Getting current position. */
Davidroid 6:32166bfc04b0 115 position = motor->GetPosition();
Davidroid 6:32166bfc04b0 116
Davidroid 6:32166bfc04b0 117 /* Printing to the console. */
Davidroid 6:32166bfc04b0 118 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 119 printf("--> Setting Home.\r\n");
Davidroid 6:32166bfc04b0 120
Davidroid 6:32166bfc04b0 121 /* Setting the current position to be the home position. */
Davidroid 6:32166bfc04b0 122 motor->SetHome();
Davidroid 6:32166bfc04b0 123
Davidroid 6:32166bfc04b0 124 /* Waiting 2 seconds. */
Davidroid 6:32166bfc04b0 125 wait_ms(2000);
Davidroid 6:32166bfc04b0 126
Davidroid 6:32166bfc04b0 127
Davidroid 6:32166bfc04b0 128 /*----- Going to a specified position. -----*/
Davidroid 6:32166bfc04b0 129
Davidroid 6:32166bfc04b0 130 /* Printing to the console. */
Davidroid 10:8f031470ca9f 131 printf("--> Going to position %d.\r\n", STEPS >> 1);
Davidroid 6:32166bfc04b0 132
Davidroid 6:32166bfc04b0 133 /* Requesting to go to a specified position. */
Davidroid 10:8f031470ca9f 134 motor->GoTo(STEPS >> 1);
Davidroid 6:32166bfc04b0 135
Davidroid 6:32166bfc04b0 136 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 137 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 138
Davidroid 6:32166bfc04b0 139 /* Getting current position. */
Davidroid 6:32166bfc04b0 140 position = motor->GetPosition();
Davidroid 6:32166bfc04b0 141
Davidroid 6:32166bfc04b0 142 /* Printing to the console. */
Davidroid 6:32166bfc04b0 143 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 144
Davidroid 6:32166bfc04b0 145 /* Waiting 2 seconds. */
Davidroid 6:32166bfc04b0 146 wait_ms(2000);
Davidroid 6:32166bfc04b0 147
Davidroid 6:32166bfc04b0 148
Davidroid 6:32166bfc04b0 149 /*----- Going Home. -----*/
Davidroid 6:32166bfc04b0 150
Davidroid 6:32166bfc04b0 151 /* Printing to the console. */
Davidroid 6:32166bfc04b0 152 printf("--> Going Home.\r\n");
Davidroid 6:32166bfc04b0 153
Davidroid 6:32166bfc04b0 154 /* Requesting to go to home. */
Davidroid 6:32166bfc04b0 155 motor->GoHome();
Davidroid 6:32166bfc04b0 156
Davidroid 6:32166bfc04b0 157 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 158 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 159
Davidroid 6:32166bfc04b0 160 /* Getting current position. */
Davidroid 6:32166bfc04b0 161 position = motor->GetPosition();
Davidroid 6:32166bfc04b0 162
Davidroid 6:32166bfc04b0 163 /* Printing to the console. */
Davidroid 6:32166bfc04b0 164 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 165
Davidroid 6:32166bfc04b0 166 /* Waiting 2 seconds. */
Davidroid 6:32166bfc04b0 167 wait_ms(2000);
Davidroid 6:32166bfc04b0 168
Davidroid 6:32166bfc04b0 169
Davidroid 6:32166bfc04b0 170 /*----- Running. -----*/
Davidroid 6:32166bfc04b0 171
Davidroid 6:32166bfc04b0 172 /* Printing to the console. */
Davidroid 6:32166bfc04b0 173 printf("--> Running backward.\r\n");
Davidroid 6:32166bfc04b0 174
Davidroid 6:32166bfc04b0 175 /* Requesting to run backward. */
Davidroid 6:32166bfc04b0 176 motor->Run(StepperMotor::BWD);
Davidroid 6:32166bfc04b0 177
Davidroid 6:32166bfc04b0 178 /* Waiting until delay has expired. */
Davidroid 6:32166bfc04b0 179 wait_ms(6000);
Davidroid 6:32166bfc04b0 180
Davidroid 6:32166bfc04b0 181 /* Getting current speed. */
Davidroid 6:32166bfc04b0 182 int speed = motor->GetSpeed();
Davidroid 6:32166bfc04b0 183
Davidroid 6:32166bfc04b0 184 /* Printing to the console. */
Davidroid 6:32166bfc04b0 185 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 186
Davidroid 6:32166bfc04b0 187 /*----- Increasing the speed while running. -----*/
Davidroid 6:32166bfc04b0 188
Davidroid 6:32166bfc04b0 189 /* Printing to the console. */
Davidroid 6:32166bfc04b0 190 printf("--> Increasing the speed while running.\r\n");
Davidroid 6:32166bfc04b0 191
Davidroid 6:32166bfc04b0 192 /* Increasing speed to 2400 step/s. */
Davidroid 6:32166bfc04b0 193 motor->SetMaxSpeed(2400);
Davidroid 6:32166bfc04b0 194
Davidroid 6:32166bfc04b0 195 /* Waiting until delay has expired. */
Davidroid 6:32166bfc04b0 196 wait_ms(6000);
Davidroid 6:32166bfc04b0 197
Davidroid 6:32166bfc04b0 198 /* Getting current speed. */
Davidroid 6:32166bfc04b0 199 speed = motor->GetSpeed();
Davidroid 6:32166bfc04b0 200
Davidroid 6:32166bfc04b0 201 /* Printing to the console. */
Davidroid 6:32166bfc04b0 202 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 203
Davidroid 6:32166bfc04b0 204
Davidroid 6:32166bfc04b0 205 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 206
Davidroid 6:32166bfc04b0 207 /* Printing to the console. */
Davidroid 6:32166bfc04b0 208 printf("--> Decreasing the speed while running.\r\n");
Davidroid 6:32166bfc04b0 209
Davidroid 6:32166bfc04b0 210 /* Decreasing speed to 1200 step/s. */
Davidroid 6:32166bfc04b0 211 motor->SetMaxSpeed(1200);
Davidroid 6:32166bfc04b0 212
Davidroid 6:32166bfc04b0 213 /* Waiting until delay has expired. */
Davidroid 6:32166bfc04b0 214 wait_ms(8000);
Davidroid 6:32166bfc04b0 215
Davidroid 6:32166bfc04b0 216 /* Getting current speed. */
Davidroid 6:32166bfc04b0 217 speed = motor->GetSpeed();
Davidroid 6:32166bfc04b0 218
Davidroid 6:32166bfc04b0 219 /* Printing to the console. */
Davidroid 6:32166bfc04b0 220 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 221
Davidroid 6:32166bfc04b0 222
Davidroid 6:32166bfc04b0 223 /*----- Requiring hard-stop while running. -----*/
Davidroid 6:32166bfc04b0 224
Davidroid 6:32166bfc04b0 225 /* Printing to the console. */
Davidroid 6:32166bfc04b0 226 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 6:32166bfc04b0 227
Davidroid 6:32166bfc04b0 228 /* Requesting to immediatly stop. */
Davidroid 6:32166bfc04b0 229 motor->HardStop();
Davidroid 6:32166bfc04b0 230
Davidroid 6:32166bfc04b0 231 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 232 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 233
Davidroid 6:32166bfc04b0 234 /* Waiting 2 seconds. */
Davidroid 6:32166bfc04b0 235 wait_ms(2000);
Davidroid 6:32166bfc04b0 236
Davidroid 6:32166bfc04b0 237
Davidroid 6:32166bfc04b0 238 /*----- Infinite Loop. -----*/
Davidroid 6:32166bfc04b0 239
Davidroid 6:32166bfc04b0 240 /* Printing to the console. */
Davidroid 6:32166bfc04b0 241 printf("--> Infinite Loop...\r\n");
Davidroid 6:32166bfc04b0 242
Davidroid 6:32166bfc04b0 243 /* Setting the current position to be the home position. */
Davidroid 6:32166bfc04b0 244 motor->SetHome();
Davidroid 6:32166bfc04b0 245
Davidroid 6:32166bfc04b0 246 /* Infinite Loop. */
Davidroid 6:32166bfc04b0 247 while (1)
Davidroid 6:32166bfc04b0 248 {
Davidroid 6:32166bfc04b0 249 /* Requesting to go to a specified position. */
Davidroid 10:8f031470ca9f 250 motor->GoTo(STEPS >> 1);
Davidroid 3:fffa53c7aed2 251
Davidroid 0:e6a49a092e2a 252 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 253 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 254
Davidroid 0:e6a49a092e2a 255 /* Requesting to go to a specified position. */
Davidroid 10:8f031470ca9f 256 motor->GoTo(- (STEPS >> 1));
Davidroid 0:e6a49a092e2a 257
Davidroid 0:e6a49a092e2a 258 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 259 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 260 }
Davidroid 0:e6a49a092e2a 261 }