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

Dependencies:   X_NUCLEO_IHM01A1

Fork of MotorControl_IHM01A1 by ST

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:
Wed Nov 18 19:13:32 2015 +0000
Revision:
3:82f4c46cccd0
Parent:
2:d956260240b0
Child:
4:43df256f26d9
+ Program updated to reflect the modifications 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 3:82f4c46cccd0 85 motor->Move(StepperMotor::FWD, 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 3:82f4c46cccd0 100 motor->Move(StepperMotor::BWD, 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 3:82f4c46cccd0 105 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 106 printf("--> Setting Home.\r\n");
Davidroid 3:82f4c46cccd0 107
Davidroid 0:fedf3cf2324a 108 /* Setting the current position to be the home position. */
Davidroid 1:5171df1bf684 109 motor->SetHome();
Davidroid 0:fedf3cf2324a 110
Davidroid 0:fedf3cf2324a 111 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 112 wait_ms(2000);
Davidroid 0:fedf3cf2324a 113
Davidroid 0:fedf3cf2324a 114
Davidroid 0:fedf3cf2324a 115 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 116
Davidroid 0:fedf3cf2324a 117 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 118 printf("--> Going to position -6400.\r\n");
Davidroid 0:fedf3cf2324a 119
Davidroid 0:fedf3cf2324a 120 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 121 motor->GoTo(-6400);
Davidroid 0:fedf3cf2324a 122
Davidroid 0:fedf3cf2324a 123 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 124 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 125
Davidroid 0:fedf3cf2324a 126 /* Getting current position. */
Davidroid 1:5171df1bf684 127 int position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 128
Davidroid 0:fedf3cf2324a 129 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 130 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 131
Davidroid 3:82f4c46cccd0 132 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 133 printf("--> Setting a mark.\r\n");
Davidroid 3:82f4c46cccd0 134
Davidroid 0:fedf3cf2324a 135 /* Setting the current position to be the mark position. */
Davidroid 1:5171df1bf684 136 motor->SetMark();
Davidroid 0:fedf3cf2324a 137
Davidroid 0:fedf3cf2324a 138 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 139 wait_ms(2000);
Davidroid 0:fedf3cf2324a 140
Davidroid 0:fedf3cf2324a 141
Davidroid 0:fedf3cf2324a 142 /*----- Going Home. -----*/
Davidroid 0:fedf3cf2324a 143
Davidroid 0:fedf3cf2324a 144 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 145 printf("--> Going Home.\r\n");
Davidroid 0:fedf3cf2324a 146
Davidroid 0:fedf3cf2324a 147 /* Requesting to go to home */
Davidroid 1:5171df1bf684 148 motor->GoHome();
Davidroid 0:fedf3cf2324a 149
Davidroid 0:fedf3cf2324a 150 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 151 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 152
Davidroid 0:fedf3cf2324a 153 /* Getting current position. */
Davidroid 1:5171df1bf684 154 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 155
Davidroid 0:fedf3cf2324a 156 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 157 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 158
Davidroid 0:fedf3cf2324a 159 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 160 wait_ms(2000);
Davidroid 0:fedf3cf2324a 161
Davidroid 0:fedf3cf2324a 162
Davidroid 0:fedf3cf2324a 163 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 164
Davidroid 0:fedf3cf2324a 165 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 166 printf("--> Going to position 6400.\r\n");
Davidroid 0:fedf3cf2324a 167
Davidroid 0:fedf3cf2324a 168 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 169 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 170
Davidroid 0:fedf3cf2324a 171 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 172 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 173
Davidroid 0:fedf3cf2324a 174 /* Getting current position. */
Davidroid 1:5171df1bf684 175 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 176
Davidroid 0:fedf3cf2324a 177 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 178 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 179
Davidroid 0:fedf3cf2324a 180 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 181 wait_ms(2000);
Davidroid 0:fedf3cf2324a 182
Davidroid 0:fedf3cf2324a 183
Davidroid 0:fedf3cf2324a 184 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 0:fedf3cf2324a 185
Davidroid 0:fedf3cf2324a 186 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 187 printf("--> Going to the mark set previously.\r\n");
Davidroid 0:fedf3cf2324a 188
Davidroid 0:fedf3cf2324a 189 /* Requesting to go to mark position. */
Davidroid 1:5171df1bf684 190 motor->GoMark();
Davidroid 0:fedf3cf2324a 191
Davidroid 0:fedf3cf2324a 192 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 193 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 194
Davidroid 0:fedf3cf2324a 195 /* Getting current position. */
Davidroid 1:5171df1bf684 196 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 197
Davidroid 0:fedf3cf2324a 198 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 199 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 200
Davidroid 0:fedf3cf2324a 201 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 202 wait_ms(2000);
Davidroid 0:fedf3cf2324a 203
Davidroid 0:fedf3cf2324a 204
Davidroid 3:82f4c46cccd0 205 /*----- Running backward. -----*/
Davidroid 0:fedf3cf2324a 206
Davidroid 0:fedf3cf2324a 207 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 208 printf("--> Running backward.\r\n");
Davidroid 0:fedf3cf2324a 209
Davidroid 0:fedf3cf2324a 210 /* Requesting to run backward. */
Davidroid 3:82f4c46cccd0 211 motor->Run(StepperMotor::BWD);
Davidroid 0:fedf3cf2324a 212
Davidroid 0:fedf3cf2324a 213 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 214 wait_ms(5000);
Davidroid 0:fedf3cf2324a 215
Davidroid 0:fedf3cf2324a 216 /* Getting current speed. */
Davidroid 2:d956260240b0 217 int speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 218
Davidroid 0:fedf3cf2324a 219 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 220 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 221
Davidroid 0:fedf3cf2324a 222
Davidroid 0:fedf3cf2324a 223 /*----- Increasing the speed while running. -----*/
Davidroid 0:fedf3cf2324a 224
Davidroid 0:fedf3cf2324a 225 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 226 printf("--> Increasing the speed while running.\r\n");
Davidroid 0:fedf3cf2324a 227
Davidroid 0:fedf3cf2324a 228 /* Increasing speed to 2400 step/s. */
Davidroid 1:5171df1bf684 229 motor->SetMaxSpeed(2400);
Davidroid 0:fedf3cf2324a 230
Davidroid 0:fedf3cf2324a 231 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 232 wait_ms(5000);
Davidroid 0:fedf3cf2324a 233
Davidroid 0:fedf3cf2324a 234 /* Getting current speed. */
Davidroid 2:d956260240b0 235 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 236
Davidroid 0:fedf3cf2324a 237 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 238 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 239
Davidroid 0:fedf3cf2324a 240
Davidroid 0:fedf3cf2324a 241 /*----- Decreasing the speed while running. -----*/
Davidroid 0:fedf3cf2324a 242
Davidroid 0:fedf3cf2324a 243 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 244 printf("--> Decreasing the speed while running.\r\n");
Davidroid 0:fedf3cf2324a 245
Davidroid 0:fedf3cf2324a 246 /* Decreasing speed to 1200 step/s. */
Davidroid 1:5171df1bf684 247 motor->SetMaxSpeed(1200);
Davidroid 0:fedf3cf2324a 248
Davidroid 0:fedf3cf2324a 249 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 250 wait_ms(5000);
Davidroid 0:fedf3cf2324a 251
Davidroid 0:fedf3cf2324a 252 /* Getting current speed. */
Davidroid 2:d956260240b0 253 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 254
Davidroid 0:fedf3cf2324a 255 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 256 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 257
Davidroid 0:fedf3cf2324a 258
Davidroid 0:fedf3cf2324a 259 /*----- Increasing acceleration while running. -----*/
Davidroid 0:fedf3cf2324a 260
Davidroid 0:fedf3cf2324a 261 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 262 printf("--> Increasing acceleration while running.\r\n");
Davidroid 0:fedf3cf2324a 263
Davidroid 0:fedf3cf2324a 264 /* Increasing acceleration to 480 step/s^2. */
Davidroid 1:5171df1bf684 265 motor->SetAcceleration(480);
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 /* Increasing speed to 2400 step/s. */
Davidroid 1:5171df1bf684 271 motor->SetMaxSpeed(2400);
Davidroid 0:fedf3cf2324a 272
Davidroid 0:fedf3cf2324a 273 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 274 wait_ms(5000);
Davidroid 0:fedf3cf2324a 275
Davidroid 0:fedf3cf2324a 276 /* Getting current speed. */
Davidroid 2:d956260240b0 277 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 278
Davidroid 0:fedf3cf2324a 279 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 280 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 281
Davidroid 0:fedf3cf2324a 282
Davidroid 0:fedf3cf2324a 283 /*----- Increasing deceleration while running. -----*/
Davidroid 0:fedf3cf2324a 284
Davidroid 0:fedf3cf2324a 285 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 286 printf("--> Increasing deceleration while running.\r\n");
Davidroid 0:fedf3cf2324a 287
Davidroid 0:fedf3cf2324a 288 /* Increasing deceleration to 480 step/s^2. */
Davidroid 1:5171df1bf684 289 motor->SetDeceleration(480);
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 /* Decreasing speed to 1200 step/s. */
Davidroid 1:5171df1bf684 295 motor->SetMaxSpeed(1200);
Davidroid 0:fedf3cf2324a 296
Davidroid 0:fedf3cf2324a 297 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 298 wait_ms(5000);
Davidroid 0:fedf3cf2324a 299
Davidroid 0:fedf3cf2324a 300 /* Getting current speed. */
Davidroid 2:d956260240b0 301 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 302
Davidroid 0:fedf3cf2324a 303 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 304 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 305
Davidroid 0:fedf3cf2324a 306
Davidroid 3:82f4c46cccd0 307 /*----- Requesting soft-stop while running. -----*/
Davidroid 0:fedf3cf2324a 308
Davidroid 0:fedf3cf2324a 309 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 310 printf("--> Requesting soft-stop while running.\r\n");
Davidroid 0:fedf3cf2324a 311
Davidroid 0:fedf3cf2324a 312 /* Requesting soft stop. */
Davidroid 1:5171df1bf684 313 motor->SoftStop();
Davidroid 0:fedf3cf2324a 314
Davidroid 0:fedf3cf2324a 315 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 316 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 317
Davidroid 0:fedf3cf2324a 318 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 319 wait_ms(2000);
Davidroid 0:fedf3cf2324a 320
Davidroid 0:fedf3cf2324a 321
Davidroid 3:82f4c46cccd0 322 /*----- Requesting hard-stop while running. -----*/
Davidroid 0:fedf3cf2324a 323
Davidroid 0:fedf3cf2324a 324 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 325 printf("--> Running forward.\r\n");
Davidroid 3:82f4c46cccd0 326
Davidroid 0:fedf3cf2324a 327 /* Requesting to run in forward direction. */
Davidroid 3:82f4c46cccd0 328 motor->Run(StepperMotor::FWD);
Davidroid 0:fedf3cf2324a 329
Davidroid 0:fedf3cf2324a 330 /* Waiting until delay has expired. */
Davidroid 1:5171df1bf684 331 wait_ms(5000);
Davidroid 3:82f4c46cccd0 332
Davidroid 3:82f4c46cccd0 333 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 334 printf("--> Requesting hard-stop while running.\r\n");
Davidroid 3:82f4c46cccd0 335
Davidroid 0:fedf3cf2324a 336 /* Requesting to immediatly stop. */
Davidroid 1:5171df1bf684 337 motor->HardStop();
Davidroid 0:fedf3cf2324a 338
Davidroid 0:fedf3cf2324a 339 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 340 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 341
Davidroid 0:fedf3cf2324a 342 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 343 wait_ms(2000);
Davidroid 0:fedf3cf2324a 344
Davidroid 0:fedf3cf2324a 345
Davidroid 0:fedf3cf2324a 346 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 347
Davidroid 0:fedf3cf2324a 348 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 349 printf("--> Going to position 20000.\r\n");
Davidroid 3:82f4c46cccd0 350
Davidroid 0:fedf3cf2324a 351 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 352 motor->GoTo(20000);
Davidroid 0:fedf3cf2324a 353
Davidroid 0:fedf3cf2324a 354 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 355 wait_ms(5000);
Davidroid 0:fedf3cf2324a 356
Davidroid 3:82f4c46cccd0 357 /* Printing to the console. */
Davidroid 3:82f4c46cccd0 358 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 3:82f4c46cccd0 359
Davidroid 0:fedf3cf2324a 360 /* Requesting to perform a soft stop */
Davidroid 1:5171df1bf684 361 motor->SoftStop();
Davidroid 0:fedf3cf2324a 362
Davidroid 0:fedf3cf2324a 363 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 364 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 365
Davidroid 0:fedf3cf2324a 366 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 367 wait_ms(2000);
Davidroid 0:fedf3cf2324a 368
Davidroid 0:fedf3cf2324a 369
Davidroid 0:fedf3cf2324a 370 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 0:fedf3cf2324a 371
Davidroid 0:fedf3cf2324a 372 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 373 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 0:fedf3cf2324a 374
Davidroid 0:fedf3cf2324a 375 /*
Davidroid 0:fedf3cf2324a 376 * Trying to read an inexistent register.
Davidroid 0:fedf3cf2324a 377 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 0:fedf3cf2324a 378 * function called.
Davidroid 0:fedf3cf2324a 379 */
Davidroid 2:d956260240b0 380 motor->GetParameter(0x1F);
Davidroid 0:fedf3cf2324a 381
Davidroid 0:fedf3cf2324a 382 /* Waiting 0.5 seconds. */
Davidroid 0:fedf3cf2324a 383 wait_ms(500);
Davidroid 0:fedf3cf2324a 384
Davidroid 0:fedf3cf2324a 385
Davidroid 0:fedf3cf2324a 386 /*----- Changing step mode to full step mode. -----*/
Davidroid 0:fedf3cf2324a 387
Davidroid 0:fedf3cf2324a 388 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 389 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 0:fedf3cf2324a 390
Davidroid 0:fedf3cf2324a 391 /* Selecting full step mode. */
Davidroid 1:5171df1bf684 392 motor->SelectStepMode(STEP_MODE_FULL);
Davidroid 0:fedf3cf2324a 393
Davidroid 0:fedf3cf2324a 394 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 1:5171df1bf684 395 motor->SetMaxSpeed(100);
Davidroid 1:5171df1bf684 396 motor->SetMinSpeed(50);
Davidroid 1:5171df1bf684 397 motor->SetAcceleration(10);
Davidroid 1:5171df1bf684 398 motor->SetDeceleration(10);
Davidroid 0:fedf3cf2324a 399
Davidroid 0:fedf3cf2324a 400 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 401 motor->GoTo(200);
Davidroid 0:fedf3cf2324a 402
Davidroid 0:fedf3cf2324a 403 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 404 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 405
Davidroid 0:fedf3cf2324a 406 /* Getting current position */
Davidroid 1:5171df1bf684 407 position = motor->GetPosition();
Davidroid 0:fedf3cf2324a 408
Davidroid 0:fedf3cf2324a 409 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 410 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 411
Davidroid 0:fedf3cf2324a 412 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 413 wait_ms(2000);
Davidroid 0:fedf3cf2324a 414
Davidroid 0:fedf3cf2324a 415
Davidroid 0:fedf3cf2324a 416 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 0:fedf3cf2324a 417
Davidroid 0:fedf3cf2324a 418 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 419 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 0:fedf3cf2324a 420
Davidroid 0:fedf3cf2324a 421 /* Resetting to 1/16 microstepping mode */
Davidroid 1:5171df1bf684 422 motor->SelectStepMode(STEP_MODE_1_16);
Davidroid 0:fedf3cf2324a 423
Davidroid 0:fedf3cf2324a 424 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 1:5171df1bf684 425 motor->SetMaxSpeed(1600);
Davidroid 1:5171df1bf684 426 motor->SetMinSpeed(800);
Davidroid 1:5171df1bf684 427 motor->SetAcceleration(160);
Davidroid 1:5171df1bf684 428 motor->SetDeceleration(160);
Davidroid 0:fedf3cf2324a 429
Davidroid 0:fedf3cf2324a 430
Davidroid 0:fedf3cf2324a 431 /*----- Infinite Loop. -----*/
Davidroid 0:fedf3cf2324a 432
Davidroid 0:fedf3cf2324a 433 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 434 printf("--> Infinite Loop...\r\n");
Davidroid 0:fedf3cf2324a 435
Davidroid 0:fedf3cf2324a 436 /* Infinite Loop. */
Davidroid 0:fedf3cf2324a 437 while(1)
Davidroid 0:fedf3cf2324a 438 {
Davidroid 0:fedf3cf2324a 439 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 440 motor->GoTo(-6400);
Davidroid 0:fedf3cf2324a 441
Davidroid 0:fedf3cf2324a 442 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 443 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 444
Davidroid 0:fedf3cf2324a 445 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 446 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 447
Davidroid 0:fedf3cf2324a 448 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 449 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 450 }
Davidroid 0:fedf3cf2324a 451 }
Davidroid 0:fedf3cf2324a 452 }