Test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.

Dependencies:   X_NUCLEO_IHM01A1 mbed

Fork of MotorControl_IHM01A1 by ST Expansion SW Team

Motor Control with the X-NUCLEO-IHM01A1 Expansion Board

This application provides a more complex example of usage of the X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.
It shows how to use one stepper motor connected to the board, with an example of ISR and error handler, as well as an almost complete coverage of the available APIs, e.g.:

  • moving the rotor to a specific position;
  • moving the rotor for a certain amount of time;
  • setting the speed value;
  • setting the direction of rotation;
  • changing the stepper motor mode;
  • soft/hard stopping the rotor;
  • powering off the power bridge.
Committer:
Davidroid
Date:
Fri Nov 13 13:00:23 2015 +0000
Revision:
2:d956260240b0
Parent:
1:5171df1bf684
Child:
3:82f4c46cccd0
Adaptation to the "StepperMotor" abstract class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:fedf3cf2324a 1 /**
Davidroid 0:fedf3cf2324a 2 ******************************************************************************
Davidroid 0:fedf3cf2324a 3 * @file main.cpp
Davidroid 0:fedf3cf2324a 4 * @author Davide Aliprandi / AST
Davidroid 0:fedf3cf2324a 5 * @version V1.0.0
Davidroid 0:fedf3cf2324a 6 * @date October 14th, 2015
Davidroid 0:fedf3cf2324a 7 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
Davidroid 1:5171df1bf684 8 * Motor Control Expansion Board: control of 1 motor showing the usage
Davidroid 1:5171df1bf684 9 * of all the related APIs.
Davidroid 0:fedf3cf2324a 10 * This application makes use of a C++ component architecture obtained
Davidroid 0:fedf3cf2324a 11 * from the C component architecture through the Stm32CubeTOO tool.
Davidroid 0:fedf3cf2324a 12 ******************************************************************************
Davidroid 0:fedf3cf2324a 13 * @attention
Davidroid 0:fedf3cf2324a 14 *
Davidroid 0:fedf3cf2324a 15 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:fedf3cf2324a 16 *
Davidroid 0:fedf3cf2324a 17 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:fedf3cf2324a 18 * are permitted provided that the following conditions are met:
Davidroid 0:fedf3cf2324a 19 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:fedf3cf2324a 20 * this list of conditions and the following disclaimer.
Davidroid 0:fedf3cf2324a 21 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:fedf3cf2324a 22 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:fedf3cf2324a 23 * and/or other materials provided with the distribution.
Davidroid 0:fedf3cf2324a 24 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:fedf3cf2324a 25 * may be used to endorse or promote products derived from this software
Davidroid 0:fedf3cf2324a 26 * without specific prior written permission.
Davidroid 0:fedf3cf2324a 27 *
Davidroid 0:fedf3cf2324a 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:fedf3cf2324a 29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:fedf3cf2324a 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:fedf3cf2324a 31 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:fedf3cf2324a 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:fedf3cf2324a 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:fedf3cf2324a 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:fedf3cf2324a 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:fedf3cf2324a 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:fedf3cf2324a 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:fedf3cf2324a 38 *
Davidroid 0:fedf3cf2324a 39 ******************************************************************************
Davidroid 0:fedf3cf2324a 40 */
Davidroid 0:fedf3cf2324a 41
Davidroid 0:fedf3cf2324a 42
Davidroid 0:fedf3cf2324a 43 /* Includes ------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 44
Davidroid 0:fedf3cf2324a 45 /* mbed specific header files. */
Davidroid 0:fedf3cf2324a 46 #include "mbed.h"
Davidroid 0:fedf3cf2324a 47
Davidroid 0:fedf3cf2324a 48 /* Helper header files. */
Davidroid 0:fedf3cf2324a 49 #include "DevSPI.h"
Davidroid 0:fedf3cf2324a 50
Davidroid 0:fedf3cf2324a 51 /* Component specific header files. */
Davidroid 0:fedf3cf2324a 52 #include "l6474_class.h"
Davidroid 0:fedf3cf2324a 53
Davidroid 0:fedf3cf2324a 54
Davidroid 0:fedf3cf2324a 55 /* Variables -----------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 56
Davidroid 0:fedf3cf2324a 57 /* Motor Control Component. */
Davidroid 1:5171df1bf684 58 L6474 *motor;
Davidroid 0:fedf3cf2324a 59
Davidroid 0:fedf3cf2324a 60
Davidroid 0:fedf3cf2324a 61 /* Main ----------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 62
Davidroid 0:fedf3cf2324a 63 int main()
Davidroid 0:fedf3cf2324a 64 {
Davidroid 0:fedf3cf2324a 65 /* Initializing SPI bus. */
Davidroid 0:fedf3cf2324a 66 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:fedf3cf2324a 67
Davidroid 0:fedf3cf2324a 68 /* Initializing Motor Control Component. */
Davidroid 1:5171df1bf684 69 motor = new L6474(D8, D7, D9, D10, dev_spi);
Davidroid 1:5171df1bf684 70 if (motor->Init(NULL) != COMPONENT_OK)
Davidroid 0:fedf3cf2324a 71 return false;
Davidroid 0:fedf3cf2324a 72
Davidroid 0:fedf3cf2324a 73 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 74 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:fedf3cf2324a 75
Davidroid 0:fedf3cf2324a 76 /* Main Loop. */
Davidroid 0:fedf3cf2324a 77 while(true)
Davidroid 0:fedf3cf2324a 78 {
Davidroid 0:fedf3cf2324a 79 /*----- Moving forward 16000 steps. -----*/
Davidroid 0:fedf3cf2324a 80
Davidroid 0:fedf3cf2324a 81 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 82 printf("--> Moving forward 16000 steps.\r\n");
Davidroid 0:fedf3cf2324a 83
Davidroid 0:fedf3cf2324a 84 /* Moving 16000 steps in the forward direction. */
Davidroid 2:d956260240b0 85 motor->Move(StepperMotor::CW, 16000);
Davidroid 0:fedf3cf2324a 86
Davidroid 0:fedf3cf2324a 87 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 88 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 89
Davidroid 0:fedf3cf2324a 90 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 91 wait_ms(2000);
Davidroid 0:fedf3cf2324a 92
Davidroid 0:fedf3cf2324a 93
Davidroid 0:fedf3cf2324a 94 /*----- Moving backward 16000 steps. -----*/
Davidroid 0:fedf3cf2324a 95
Davidroid 0:fedf3cf2324a 96 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 97 printf("--> Moving backward 16000 steps.\r\n");
Davidroid 0:fedf3cf2324a 98
Davidroid 0:fedf3cf2324a 99 /* Moving 16000 steps in the backward direction. */
Davidroid 2:d956260240b0 100 motor->Move(StepperMotor::CCW, 16000);
Davidroid 1:5171df1bf684 101
Davidroid 0:fedf3cf2324a 102 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 103 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 104
Davidroid 0:fedf3cf2324a 105 /* Setting the current position to be the home position. */
Davidroid 1:5171df1bf684 106 motor->SetHome();
Davidroid 0:fedf3cf2324a 107
Davidroid 0:fedf3cf2324a 108 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 109 wait_ms(2000);
Davidroid 0:fedf3cf2324a 110
Davidroid 0:fedf3cf2324a 111
Davidroid 0:fedf3cf2324a 112 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 113
Davidroid 0:fedf3cf2324a 114 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 115 printf("--> Going to position -6400.\r\n");
Davidroid 0:fedf3cf2324a 116
Davidroid 0:fedf3cf2324a 117 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 118 motor->GoTo(-6400);
Davidroid 0:fedf3cf2324a 119
Davidroid 0:fedf3cf2324a 120 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 121 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 122
Davidroid 0:fedf3cf2324a 123 /* Getting current position. */
Davidroid 1:5171df1bf684 124 int position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 125
Davidroid 0:fedf3cf2324a 126 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 127 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 128
Davidroid 0:fedf3cf2324a 129 /* Setting the current position to be the mark position. */
Davidroid 1:5171df1bf684 130 motor->SetMark();
Davidroid 0:fedf3cf2324a 131
Davidroid 0:fedf3cf2324a 132 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 133 wait_ms(2000);
Davidroid 0:fedf3cf2324a 134
Davidroid 0:fedf3cf2324a 135
Davidroid 0:fedf3cf2324a 136 /*----- Going Home. -----*/
Davidroid 0:fedf3cf2324a 137
Davidroid 0:fedf3cf2324a 138 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 139 printf("--> Going Home.\r\n");
Davidroid 0:fedf3cf2324a 140
Davidroid 0:fedf3cf2324a 141 /* Requesting to go to home */
Davidroid 1:5171df1bf684 142 motor->GoHome();
Davidroid 0:fedf3cf2324a 143
Davidroid 0:fedf3cf2324a 144 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 145 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 146
Davidroid 0:fedf3cf2324a 147 /* Getting current position. */
Davidroid 1:5171df1bf684 148 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 149
Davidroid 0:fedf3cf2324a 150 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 151 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 152
Davidroid 0:fedf3cf2324a 153 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 154 wait_ms(2000);
Davidroid 0:fedf3cf2324a 155
Davidroid 0:fedf3cf2324a 156
Davidroid 0:fedf3cf2324a 157 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 158
Davidroid 0:fedf3cf2324a 159 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 160 printf("--> Going to position 6400.\r\n");
Davidroid 0:fedf3cf2324a 161
Davidroid 0:fedf3cf2324a 162 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 163 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 164
Davidroid 0:fedf3cf2324a 165 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 166 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 167
Davidroid 0:fedf3cf2324a 168 /* Getting current position. */
Davidroid 1:5171df1bf684 169 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 170
Davidroid 0:fedf3cf2324a 171 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 172 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 173
Davidroid 0:fedf3cf2324a 174 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 175 wait_ms(2000);
Davidroid 0:fedf3cf2324a 176
Davidroid 0:fedf3cf2324a 177
Davidroid 0:fedf3cf2324a 178 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 0:fedf3cf2324a 179
Davidroid 0:fedf3cf2324a 180 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 181 printf("--> Going to mark which was set previously after going to -6400.\r\n");
Davidroid 0:fedf3cf2324a 182
Davidroid 0:fedf3cf2324a 183 /* Requesting to go to mark position. */
Davidroid 1:5171df1bf684 184 motor->GoMark();
Davidroid 0:fedf3cf2324a 185
Davidroid 0:fedf3cf2324a 186 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 187 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 188
Davidroid 0:fedf3cf2324a 189 /* Getting current position. */
Davidroid 1:5171df1bf684 190 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 191
Davidroid 0:fedf3cf2324a 192 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 193 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 194
Davidroid 0:fedf3cf2324a 195 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 196 wait_ms(2000);
Davidroid 0:fedf3cf2324a 197
Davidroid 0:fedf3cf2324a 198
Davidroid 0:fedf3cf2324a 199 /*----- Moving backward. -----*/
Davidroid 0:fedf3cf2324a 200
Davidroid 0:fedf3cf2324a 201 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 202 printf("--> Moving backward.\r\n");
Davidroid 0:fedf3cf2324a 203
Davidroid 0:fedf3cf2324a 204 /* Requesting to run backward. */
Davidroid 2:d956260240b0 205 motor->Run(StepperMotor::CCW);
Davidroid 0:fedf3cf2324a 206
Davidroid 0:fedf3cf2324a 207 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 208 wait_ms(5000);
Davidroid 0:fedf3cf2324a 209
Davidroid 0:fedf3cf2324a 210 /* Getting current speed. */
Davidroid 2:d956260240b0 211 int speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 212
Davidroid 0:fedf3cf2324a 213 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 214 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 215
Davidroid 0:fedf3cf2324a 216
Davidroid 0:fedf3cf2324a 217 /*----- Increasing the speed while running. -----*/
Davidroid 0:fedf3cf2324a 218
Davidroid 0:fedf3cf2324a 219 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 220 printf("--> Increasing the speed while running.\r\n");
Davidroid 0:fedf3cf2324a 221
Davidroid 0:fedf3cf2324a 222 /* Increasing speed to 2400 step/s. */
Davidroid 1:5171df1bf684 223 motor->SetMaxSpeed(2400);
Davidroid 0:fedf3cf2324a 224
Davidroid 0:fedf3cf2324a 225 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 226 wait_ms(5000);
Davidroid 0:fedf3cf2324a 227
Davidroid 0:fedf3cf2324a 228 /* Getting current speed. */
Davidroid 2:d956260240b0 229 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 230
Davidroid 0:fedf3cf2324a 231 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 232 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 233
Davidroid 0:fedf3cf2324a 234
Davidroid 0:fedf3cf2324a 235 /*----- Decreasing the speed while running. -----*/
Davidroid 0:fedf3cf2324a 236
Davidroid 0:fedf3cf2324a 237 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 238 printf("--> Decreasing the speed while running.\r\n");
Davidroid 0:fedf3cf2324a 239
Davidroid 0:fedf3cf2324a 240 /* Decreasing speed to 1200 step/s. */
Davidroid 1:5171df1bf684 241 motor->SetMaxSpeed(1200);
Davidroid 0:fedf3cf2324a 242
Davidroid 0:fedf3cf2324a 243 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 244 wait_ms(5000);
Davidroid 0:fedf3cf2324a 245
Davidroid 0:fedf3cf2324a 246 /* Getting current speed. */
Davidroid 2:d956260240b0 247 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 248
Davidroid 0:fedf3cf2324a 249 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 250 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 251
Davidroid 0:fedf3cf2324a 252
Davidroid 0:fedf3cf2324a 253 /*----- Increasing acceleration while running. -----*/
Davidroid 0:fedf3cf2324a 254
Davidroid 0:fedf3cf2324a 255 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 256 printf("--> Increasing acceleration while running.\r\n");
Davidroid 0:fedf3cf2324a 257
Davidroid 0:fedf3cf2324a 258 /* Increasing acceleration to 480 step/s^2. */
Davidroid 1:5171df1bf684 259 motor->SetAcceleration(480);
Davidroid 0:fedf3cf2324a 260
Davidroid 0:fedf3cf2324a 261 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 262 wait_ms(5000);
Davidroid 0:fedf3cf2324a 263
Davidroid 0:fedf3cf2324a 264 /* Increasing speed to 2400 step/s. */
Davidroid 1:5171df1bf684 265 motor->SetMaxSpeed(2400);
Davidroid 0:fedf3cf2324a 266
Davidroid 0:fedf3cf2324a 267 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 268 wait_ms(5000);
Davidroid 0:fedf3cf2324a 269
Davidroid 0:fedf3cf2324a 270 /* Getting current speed. */
Davidroid 2:d956260240b0 271 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 272
Davidroid 0:fedf3cf2324a 273 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 274 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 275
Davidroid 0:fedf3cf2324a 276
Davidroid 0:fedf3cf2324a 277 /*----- Increasing deceleration while running. -----*/
Davidroid 0:fedf3cf2324a 278
Davidroid 0:fedf3cf2324a 279 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 280 printf("--> Increasing deceleration while running.\r\n");
Davidroid 0:fedf3cf2324a 281
Davidroid 0:fedf3cf2324a 282 /* Increasing deceleration to 480 step/s^2. */
Davidroid 1:5171df1bf684 283 motor->SetDeceleration(480);
Davidroid 0:fedf3cf2324a 284
Davidroid 0:fedf3cf2324a 285 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 286 wait_ms(5000);
Davidroid 0:fedf3cf2324a 287
Davidroid 0:fedf3cf2324a 288 /* Decreasing speed to 1200 step/s. */
Davidroid 1:5171df1bf684 289 motor->SetMaxSpeed(1200);
Davidroid 0:fedf3cf2324a 290
Davidroid 0:fedf3cf2324a 291 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 292 wait_ms(5000);
Davidroid 0:fedf3cf2324a 293
Davidroid 0:fedf3cf2324a 294 /* Getting current speed. */
Davidroid 2:d956260240b0 295 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 296
Davidroid 0:fedf3cf2324a 297 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 298 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 299
Davidroid 0:fedf3cf2324a 300
Davidroid 0:fedf3cf2324a 301 /*----- Requiring soft-stop while running. -----*/
Davidroid 0:fedf3cf2324a 302
Davidroid 0:fedf3cf2324a 303 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 304 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 0:fedf3cf2324a 305
Davidroid 0:fedf3cf2324a 306 /* Requesting soft stop. */
Davidroid 1:5171df1bf684 307 motor->SoftStop();
Davidroid 0:fedf3cf2324a 308
Davidroid 0:fedf3cf2324a 309 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 310 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 311
Davidroid 0:fedf3cf2324a 312 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 313 wait_ms(2000);
Davidroid 0:fedf3cf2324a 314
Davidroid 0:fedf3cf2324a 315
Davidroid 0:fedf3cf2324a 316 /*----- Requiring hard-stop while running. -----*/
Davidroid 0:fedf3cf2324a 317
Davidroid 0:fedf3cf2324a 318 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 319 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 0:fedf3cf2324a 320
Davidroid 0:fedf3cf2324a 321 /* Requesting to run in forward direction. */
Davidroid 2:d956260240b0 322 motor->Run(StepperMotor::CW);
Davidroid 0:fedf3cf2324a 323
Davidroid 0:fedf3cf2324a 324 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 325 wait_ms(5000);
Davidroid 0:fedf3cf2324a 326
Davidroid 0:fedf3cf2324a 327 /* Requesting to immediatly stop. */
Davidroid 1:5171df1bf684 328 motor->HardStop();
Davidroid 0:fedf3cf2324a 329
Davidroid 0:fedf3cf2324a 330 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 331 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 332
Davidroid 0:fedf3cf2324a 333 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 334 wait_ms(2000);
Davidroid 0:fedf3cf2324a 335
Davidroid 0:fedf3cf2324a 336
Davidroid 0:fedf3cf2324a 337 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 338
Davidroid 0:fedf3cf2324a 339 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 340 printf("--> GOTO stopped by soft-stop.\r\n");
Davidroid 0:fedf3cf2324a 341
Davidroid 0:fedf3cf2324a 342 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 343 motor->GoTo(20000);
Davidroid 0:fedf3cf2324a 344
Davidroid 0:fedf3cf2324a 345 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 346 wait_ms(5000);
Davidroid 0:fedf3cf2324a 347
Davidroid 0:fedf3cf2324a 348 /* Requesting to perform a soft stop */
Davidroid 1:5171df1bf684 349 motor->SoftStop();
Davidroid 0:fedf3cf2324a 350
Davidroid 0:fedf3cf2324a 351 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 352 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 353
Davidroid 0:fedf3cf2324a 354 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 355 wait_ms(2000);
Davidroid 0:fedf3cf2324a 356
Davidroid 0:fedf3cf2324a 357
Davidroid 0:fedf3cf2324a 358 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 0:fedf3cf2324a 359
Davidroid 0:fedf3cf2324a 360 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 361 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 0:fedf3cf2324a 362
Davidroid 0:fedf3cf2324a 363 /*
Davidroid 0:fedf3cf2324a 364 * Trying to read an inexistent register.
Davidroid 0:fedf3cf2324a 365 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 0:fedf3cf2324a 366 * function called.
Davidroid 0:fedf3cf2324a 367 */
Davidroid 2:d956260240b0 368 motor->GetParameter(0x1F);
Davidroid 0:fedf3cf2324a 369
Davidroid 0:fedf3cf2324a 370 /* Waiting 0.5 seconds. */
Davidroid 0:fedf3cf2324a 371 wait_ms(500);
Davidroid 0:fedf3cf2324a 372
Davidroid 0:fedf3cf2324a 373
Davidroid 0:fedf3cf2324a 374 /*----- Changing step mode to full step mode. -----*/
Davidroid 0:fedf3cf2324a 375
Davidroid 0:fedf3cf2324a 376 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 377 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 0:fedf3cf2324a 378
Davidroid 0:fedf3cf2324a 379 /* Selecting full step mode. */
Davidroid 1:5171df1bf684 380 motor->SelectStepMode(STEP_MODE_FULL);
Davidroid 0:fedf3cf2324a 381
Davidroid 0:fedf3cf2324a 382 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 1:5171df1bf684 383 motor->SetMaxSpeed(100);
Davidroid 1:5171df1bf684 384 motor->SetMinSpeed(50);
Davidroid 1:5171df1bf684 385 motor->SetAcceleration(10);
Davidroid 1:5171df1bf684 386 motor->SetDeceleration(10);
Davidroid 0:fedf3cf2324a 387
Davidroid 0:fedf3cf2324a 388 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 389 motor->GoTo(200);
Davidroid 0:fedf3cf2324a 390
Davidroid 0:fedf3cf2324a 391 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 392 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 393
Davidroid 0:fedf3cf2324a 394 /* Getting current position */
Davidroid 1:5171df1bf684 395 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 396
Davidroid 0:fedf3cf2324a 397 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 398 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 399
Davidroid 0:fedf3cf2324a 400 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 401 wait_ms(2000);
Davidroid 0:fedf3cf2324a 402
Davidroid 0:fedf3cf2324a 403
Davidroid 0:fedf3cf2324a 404 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 0:fedf3cf2324a 405
Davidroid 0:fedf3cf2324a 406 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 407 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 0:fedf3cf2324a 408
Davidroid 0:fedf3cf2324a 409 /* Resetting to 1/16 microstepping mode */
Davidroid 1:5171df1bf684 410 motor->SelectStepMode(STEP_MODE_1_16);
Davidroid 0:fedf3cf2324a 411
Davidroid 0:fedf3cf2324a 412 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 1:5171df1bf684 413 motor->SetMaxSpeed(1600);
Davidroid 1:5171df1bf684 414 motor->SetMinSpeed(800);
Davidroid 1:5171df1bf684 415 motor->SetAcceleration(160);
Davidroid 1:5171df1bf684 416 motor->SetDeceleration(160);
Davidroid 0:fedf3cf2324a 417
Davidroid 0:fedf3cf2324a 418
Davidroid 0:fedf3cf2324a 419 /*----- Infinite Loop. -----*/
Davidroid 0:fedf3cf2324a 420
Davidroid 0:fedf3cf2324a 421 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 422 printf("--> Infinite Loop...\r\n");
Davidroid 0:fedf3cf2324a 423
Davidroid 0:fedf3cf2324a 424 /* Infinite Loop. */
Davidroid 0:fedf3cf2324a 425 while(1)
Davidroid 0:fedf3cf2324a 426 {
Davidroid 0:fedf3cf2324a 427 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 428 motor->GoTo(-6400);
Davidroid 0:fedf3cf2324a 429
Davidroid 0:fedf3cf2324a 430 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 431 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 432
Davidroid 0:fedf3cf2324a 433 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 434 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 435
Davidroid 0:fedf3cf2324a 436 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 437 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 438 }
Davidroid 0:fedf3cf2324a 439 }
Davidroid 0:fedf3cf2324a 440 }