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:
Fri Nov 27 12:47:16 2015 +0000
Revision:
7:eec0f9ec9719
Parent:
6:ff72e3344fb1
Child:
12:fac478c7ea0b
+ Updated with the new version of the library.

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 6:ff72e3344fb1 4 * @author Davide Aliprandi, STMicrolectronics
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 ******************************************************************************
Davidroid 0:fedf3cf2324a 11 * @attention
Davidroid 0:fedf3cf2324a 12 *
Davidroid 0:fedf3cf2324a 13 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:fedf3cf2324a 14 *
Davidroid 0:fedf3cf2324a 15 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:fedf3cf2324a 16 * are permitted provided that the following conditions are met:
Davidroid 0:fedf3cf2324a 17 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:fedf3cf2324a 18 * this list of conditions and the following disclaimer.
Davidroid 0:fedf3cf2324a 19 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:fedf3cf2324a 20 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:fedf3cf2324a 21 * and/or other materials provided with the distribution.
Davidroid 0:fedf3cf2324a 22 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:fedf3cf2324a 23 * may be used to endorse or promote products derived from this software
Davidroid 0:fedf3cf2324a 24 * without specific prior written permission.
Davidroid 0:fedf3cf2324a 25 *
Davidroid 0:fedf3cf2324a 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:fedf3cf2324a 27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:fedf3cf2324a 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:fedf3cf2324a 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:fedf3cf2324a 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:fedf3cf2324a 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:fedf3cf2324a 32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:fedf3cf2324a 33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:fedf3cf2324a 34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:fedf3cf2324a 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:fedf3cf2324a 36 *
Davidroid 0:fedf3cf2324a 37 ******************************************************************************
Davidroid 0:fedf3cf2324a 38 */
Davidroid 0:fedf3cf2324a 39
Davidroid 0:fedf3cf2324a 40
Davidroid 0:fedf3cf2324a 41 /* Includes ------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 42
Davidroid 0:fedf3cf2324a 43 /* mbed specific header files. */
Davidroid 0:fedf3cf2324a 44 #include "mbed.h"
Davidroid 0:fedf3cf2324a 45
Davidroid 0:fedf3cf2324a 46 /* Helper header files. */
Davidroid 0:fedf3cf2324a 47 #include "DevSPI.h"
Davidroid 0:fedf3cf2324a 48
Davidroid 0:fedf3cf2324a 49 /* Component specific header files. */
Davidroid 0:fedf3cf2324a 50 #include "l6474_class.h"
Davidroid 0:fedf3cf2324a 51
Davidroid 0:fedf3cf2324a 52
Davidroid 0:fedf3cf2324a 53 /* Variables -----------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 54
Davidroid 0:fedf3cf2324a 55 /* Motor Control Component. */
Davidroid 1:5171df1bf684 56 L6474 *motor;
Davidroid 0:fedf3cf2324a 57
Davidroid 0:fedf3cf2324a 58
Davidroid 5:dfec8da854e5 59 /* Functions -----------------------------------------------------------------*/
Davidroid 5:dfec8da854e5 60
Davidroid 5:dfec8da854e5 61 /**
Davidroid 5:dfec8da854e5 62 * @brief This is an example of user handler for the flag interrupt.
Davidroid 5:dfec8da854e5 63 * @param None
Davidroid 5:dfec8da854e5 64 * @retval None
Davidroid 5:dfec8da854e5 65 * @note If needed, implement it, and then attach and enable it:
Davidroid 5:dfec8da854e5 66 * + motor->AttachFlagIRQ(&FlagIRQHandler);
Davidroid 5:dfec8da854e5 67 * + motor->EnableFlagIRQ();
Davidroid 5:dfec8da854e5 68 * To disable it:
Davidroid 5:dfec8da854e5 69 * + motor->DisbleFlagIRQ();
Davidroid 5:dfec8da854e5 70 */
Davidroid 5:dfec8da854e5 71 void FlagIRQHandler(void)
Davidroid 5:dfec8da854e5 72 {
Davidroid 5:dfec8da854e5 73 /* Set ISR flag. */
Davidroid 5:dfec8da854e5 74 motor->isrFlag = TRUE;
Davidroid 5:dfec8da854e5 75
Davidroid 5:dfec8da854e5 76 /* Get the value of the status register. */
Davidroid 5:dfec8da854e5 77 unsigned int status = motor->GetStatus();
Davidroid 5:dfec8da854e5 78
Davidroid 5:dfec8da854e5 79 /* Check HIZ flag: if set, power brigdes are disabled. */
Davidroid 5:dfec8da854e5 80 if ((status & L6474_STATUS_HIZ) == L6474_STATUS_HIZ)
Davidroid 5:dfec8da854e5 81 { /* HIZ state. Action to be customized. */ }
Davidroid 5:dfec8da854e5 82
Davidroid 5:dfec8da854e5 83 /* Check direction. */
Davidroid 5:dfec8da854e5 84 if ((status & L6474_STATUS_DIR) == L6474_STATUS_DIR)
Davidroid 5:dfec8da854e5 85 { /* Forward direction is set. Action to be customized. */ }
Davidroid 5:dfec8da854e5 86 else
Davidroid 5:dfec8da854e5 87 { /* Backward direction is set. Action to be customized. */ }
Davidroid 5:dfec8da854e5 88
Davidroid 5:dfec8da854e5 89 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 5:dfec8da854e5 90 /* This often occures when a command is sent to the L6474 while it is in HIZ state. */
Davidroid 5:dfec8da854e5 91 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD)
Davidroid 5:dfec8da854e5 92 { /* Command received by SPI can't be performed. Action to be customized. */ }
Davidroid 5:dfec8da854e5 93
Davidroid 5:dfec8da854e5 94 /* Check WRONG_CMD flag: if set, the command does not exist. */
Davidroid 5:dfec8da854e5 95 if ((status & L6474_STATUS_WRONG_CMD) == L6474_STATUS_WRONG_CMD)
Davidroid 5:dfec8da854e5 96 { /* The command received by SPI does not exist. Action to be customized. */ }
Davidroid 5:dfec8da854e5 97
Davidroid 5:dfec8da854e5 98 /* Check UVLO flag: if not set, there is an undervoltage lock-out. */
Davidroid 5:dfec8da854e5 99 if ((status & L6474_STATUS_UVLO) == 0)
Davidroid 5:dfec8da854e5 100 { /* Undervoltage lock-out. Action to be customized. */ }
Davidroid 5:dfec8da854e5 101
Davidroid 5:dfec8da854e5 102 /* Check TH_WRN flag: if not set, the thermal warning threshold is reached. */
Davidroid 5:dfec8da854e5 103 if ((status & L6474_STATUS_TH_WRN) == 0)
Davidroid 5:dfec8da854e5 104 { /* Thermal warning threshold is reached. Action to be customized. */ }
Davidroid 5:dfec8da854e5 105
Davidroid 5:dfec8da854e5 106 /* Check TH_SHD flag: if not set, the thermal shut down threshold is reached. */
Davidroid 5:dfec8da854e5 107 if ((status & L6474_STATUS_TH_SD) == 0)
Davidroid 5:dfec8da854e5 108 { /* Thermal shut down threshold is reached. Action to be customized. */ }
Davidroid 5:dfec8da854e5 109
Davidroid 5:dfec8da854e5 110 /* Check OCD flag: if not set, there is an overcurrent detection. */
Davidroid 5:dfec8da854e5 111 if ((status & L6474_STATUS_OCD) == 0)
Davidroid 5:dfec8da854e5 112 { /* Overcurrent detection. Action to be customized. */ }
Davidroid 5:dfec8da854e5 113
Davidroid 5:dfec8da854e5 114 /* Reset ISR flag. */
Davidroid 5:dfec8da854e5 115 motor->isrFlag = FALSE;
Davidroid 5:dfec8da854e5 116 }
Davidroid 5:dfec8da854e5 117
Davidroid 5:dfec8da854e5 118 /**
Davidroid 5:dfec8da854e5 119 * @brief This is an example of user handler for the errors.
Davidroid 5:dfec8da854e5 120 * @param error error-code.
Davidroid 5:dfec8da854e5 121 * @retval None
Davidroid 5:dfec8da854e5 122 * @note If needed, implement it, and then attach it:
Davidroid 5:dfec8da854e5 123 * + motor->AttachErrorHandler(&ErrorHandler);
Davidroid 5:dfec8da854e5 124 */
Davidroid 5:dfec8da854e5 125 void ErrorHandler(uint16_t error)
Davidroid 5:dfec8da854e5 126 {
Davidroid 5:dfec8da854e5 127 /* Printing to the console. */
Davidroid 5:dfec8da854e5 128 printf("Error: %d.\r\n", error);
Davidroid 5:dfec8da854e5 129
Davidroid 5:dfec8da854e5 130 /* Aborting the program. */
Davidroid 5:dfec8da854e5 131 exit(EXIT_FAILURE);
Davidroid 5:dfec8da854e5 132 }
Davidroid 5:dfec8da854e5 133
Davidroid 5:dfec8da854e5 134
Davidroid 0:fedf3cf2324a 135 /* Main ----------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 136
Davidroid 0:fedf3cf2324a 137 int main()
Davidroid 0:fedf3cf2324a 138 {
Davidroid 6:ff72e3344fb1 139 /*----- Initialization. -----*/
Davidroid 6:ff72e3344fb1 140
Davidroid 0:fedf3cf2324a 141 /* Initializing SPI bus. */
Davidroid 0:fedf3cf2324a 142 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:fedf3cf2324a 143
Davidroid 0:fedf3cf2324a 144 /* Initializing Motor Control Component. */
Davidroid 4:43df256f26d9 145 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 1:5171df1bf684 146 if (motor->Init(NULL) != COMPONENT_OK)
Davidroid 0:fedf3cf2324a 147 return false;
Davidroid 0:fedf3cf2324a 148
Davidroid 7:eec0f9ec9719 149 /* Setting 1/16 Microstepping mode. */
Davidroid 7:eec0f9ec9719 150 motor->SetStepMode(STEP_MODE_1_16);
Davidroid 7:eec0f9ec9719 151
Davidroid 0:fedf3cf2324a 152 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 153 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:fedf3cf2324a 154
Davidroid 6:ff72e3344fb1 155
Davidroid 6:ff72e3344fb1 156 /*----- Moving forward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 157
Davidroid 6:ff72e3344fb1 158 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 159 printf("--> Moving forward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 160
Davidroid 6:ff72e3344fb1 161 /* Moving 16000 steps in the forward direction. */
Davidroid 6:ff72e3344fb1 162 motor->Move(StepperMotor::FWD, 16000);
Davidroid 6:ff72e3344fb1 163
Davidroid 6:ff72e3344fb1 164 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 165 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 166
Davidroid 6:ff72e3344fb1 167 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 168 wait_ms(2000);
Davidroid 6:ff72e3344fb1 169
Davidroid 6:ff72e3344fb1 170
Davidroid 6:ff72e3344fb1 171 /*----- Moving backward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 172
Davidroid 6:ff72e3344fb1 173 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 174 printf("--> Moving backward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 175
Davidroid 6:ff72e3344fb1 176 /* Moving 16000 steps in the backward direction. */
Davidroid 6:ff72e3344fb1 177 motor->Move(StepperMotor::BWD, 16000);
Davidroid 6:ff72e3344fb1 178
Davidroid 6:ff72e3344fb1 179 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 180 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 181
Davidroid 6:ff72e3344fb1 182 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 183 printf("--> Setting Home.\r\n");
Davidroid 6:ff72e3344fb1 184
Davidroid 6:ff72e3344fb1 185 /* Setting the current position to be the home position. */
Davidroid 6:ff72e3344fb1 186 motor->SetHome();
Davidroid 6:ff72e3344fb1 187
Davidroid 6:ff72e3344fb1 188 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 189 wait_ms(2000);
Davidroid 6:ff72e3344fb1 190
Davidroid 6:ff72e3344fb1 191
Davidroid 6:ff72e3344fb1 192 /*----- Going to a specified position. -----*/
Davidroid 6:ff72e3344fb1 193
Davidroid 6:ff72e3344fb1 194 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 195 printf("--> Going to position -6400.\r\n");
Davidroid 6:ff72e3344fb1 196
Davidroid 6:ff72e3344fb1 197 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 198 motor->GoTo(-6400);
Davidroid 6:ff72e3344fb1 199
Davidroid 6:ff72e3344fb1 200 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 201 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 202
Davidroid 6:ff72e3344fb1 203 /* Getting current position. */
Davidroid 6:ff72e3344fb1 204 int position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 205
Davidroid 6:ff72e3344fb1 206 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 207 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 208
Davidroid 6:ff72e3344fb1 209 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 210 printf("--> Setting a mark.\r\n");
Davidroid 6:ff72e3344fb1 211
Davidroid 6:ff72e3344fb1 212 /* Setting the current position to be the mark position. */
Davidroid 6:ff72e3344fb1 213 motor->SetMark();
Davidroid 6:ff72e3344fb1 214
Davidroid 6:ff72e3344fb1 215 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 216 wait_ms(2000);
Davidroid 6:ff72e3344fb1 217
Davidroid 6:ff72e3344fb1 218
Davidroid 6:ff72e3344fb1 219 /*----- Going Home. -----*/
Davidroid 6:ff72e3344fb1 220
Davidroid 6:ff72e3344fb1 221 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 222 printf("--> Going Home.\r\n");
Davidroid 6:ff72e3344fb1 223
Davidroid 6:ff72e3344fb1 224 /* Requesting to go to home */
Davidroid 6:ff72e3344fb1 225 motor->GoHome();
Davidroid 6:ff72e3344fb1 226
Davidroid 6:ff72e3344fb1 227 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 228 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 229
Davidroid 6:ff72e3344fb1 230 /* Getting current position. */
Davidroid 6:ff72e3344fb1 231 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 232
Davidroid 6:ff72e3344fb1 233 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 234 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 235
Davidroid 6:ff72e3344fb1 236 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 237 wait_ms(2000);
Davidroid 6:ff72e3344fb1 238
Davidroid 6:ff72e3344fb1 239
Davidroid 6:ff72e3344fb1 240 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 241
Davidroid 6:ff72e3344fb1 242 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 243 printf("--> Going to position 6400.\r\n");
Davidroid 6:ff72e3344fb1 244
Davidroid 6:ff72e3344fb1 245 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 246 motor->GoTo(6400);
Davidroid 6:ff72e3344fb1 247
Davidroid 6:ff72e3344fb1 248 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 249 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 250
Davidroid 6:ff72e3344fb1 251 /* Getting current position. */
Davidroid 6:ff72e3344fb1 252 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 253
Davidroid 6:ff72e3344fb1 254 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 255 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 256
Davidroid 6:ff72e3344fb1 257 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 258 wait_ms(2000);
Davidroid 6:ff72e3344fb1 259
Davidroid 6:ff72e3344fb1 260
Davidroid 6:ff72e3344fb1 261 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 6:ff72e3344fb1 262
Davidroid 6:ff72e3344fb1 263 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 264 printf("--> Going to the mark set previously.\r\n");
Davidroid 6:ff72e3344fb1 265
Davidroid 6:ff72e3344fb1 266 /* Requesting to go to mark position. */
Davidroid 6:ff72e3344fb1 267 motor->GoMark();
Davidroid 6:ff72e3344fb1 268
Davidroid 6:ff72e3344fb1 269 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 270 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 271
Davidroid 6:ff72e3344fb1 272 /* Getting current position. */
Davidroid 6:ff72e3344fb1 273 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 274
Davidroid 6:ff72e3344fb1 275 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 276 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 277
Davidroid 6:ff72e3344fb1 278 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 279 wait_ms(2000);
Davidroid 6:ff72e3344fb1 280
Davidroid 6:ff72e3344fb1 281
Davidroid 6:ff72e3344fb1 282 /*----- Running backward. -----*/
Davidroid 6:ff72e3344fb1 283
Davidroid 6:ff72e3344fb1 284 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 285 printf("--> Running backward.\r\n");
Davidroid 6:ff72e3344fb1 286
Davidroid 6:ff72e3344fb1 287 /* Requesting to run backward. */
Davidroid 6:ff72e3344fb1 288 motor->Run(StepperMotor::BWD);
Davidroid 6:ff72e3344fb1 289
Davidroid 6:ff72e3344fb1 290 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 291 wait_ms(5000);
Davidroid 6:ff72e3344fb1 292
Davidroid 6:ff72e3344fb1 293 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 294 int speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 295
Davidroid 6:ff72e3344fb1 296 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 297 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 298
Davidroid 6:ff72e3344fb1 299
Davidroid 6:ff72e3344fb1 300 /*----- Increasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 301
Davidroid 6:ff72e3344fb1 302 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 303 printf("--> Increasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 304
Davidroid 6:ff72e3344fb1 305 /* Increasing speed to 2400 step/s. */
Davidroid 6:ff72e3344fb1 306 motor->SetMaxSpeed(2400);
Davidroid 6:ff72e3344fb1 307
Davidroid 6:ff72e3344fb1 308 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 309 wait_ms(5000);
Davidroid 6:ff72e3344fb1 310
Davidroid 6:ff72e3344fb1 311 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 312 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 313
Davidroid 6:ff72e3344fb1 314 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 315 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 316
Davidroid 6:ff72e3344fb1 317
Davidroid 6:ff72e3344fb1 318 /*----- Decreasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 319
Davidroid 6:ff72e3344fb1 320 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 321 printf("--> Decreasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 322
Davidroid 6:ff72e3344fb1 323 /* Decreasing speed to 1200 step/s. */
Davidroid 6:ff72e3344fb1 324 motor->SetMaxSpeed(1200);
Davidroid 6:ff72e3344fb1 325
Davidroid 6:ff72e3344fb1 326 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 327 wait_ms(5000);
Davidroid 6:ff72e3344fb1 328
Davidroid 6:ff72e3344fb1 329 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 330 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 331
Davidroid 6:ff72e3344fb1 332 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 333 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 334
Davidroid 0:fedf3cf2324a 335
Davidroid 6:ff72e3344fb1 336 /*----- Increasing acceleration while running. -----*/
Davidroid 6:ff72e3344fb1 337
Davidroid 6:ff72e3344fb1 338 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 339 printf("--> Increasing acceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 340
Davidroid 6:ff72e3344fb1 341 /* Increasing acceleration to 480 step/s^2. */
Davidroid 6:ff72e3344fb1 342 motor->SetAcceleration(480);
Davidroid 6:ff72e3344fb1 343
Davidroid 6:ff72e3344fb1 344 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 345 wait_ms(5000);
Davidroid 6:ff72e3344fb1 346
Davidroid 6:ff72e3344fb1 347 /* Increasing speed to 2400 step/s. */
Davidroid 6:ff72e3344fb1 348 motor->SetMaxSpeed(2400);
Davidroid 6:ff72e3344fb1 349
Davidroid 6:ff72e3344fb1 350 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 351 wait_ms(5000);
Davidroid 6:ff72e3344fb1 352
Davidroid 6:ff72e3344fb1 353 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 354 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 355
Davidroid 6:ff72e3344fb1 356 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 357 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 358
Davidroid 6:ff72e3344fb1 359
Davidroid 6:ff72e3344fb1 360 /*----- Increasing deceleration while running. -----*/
Davidroid 6:ff72e3344fb1 361
Davidroid 6:ff72e3344fb1 362 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 363 printf("--> Increasing deceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 364
Davidroid 6:ff72e3344fb1 365 /* Increasing deceleration to 480 step/s^2. */
Davidroid 6:ff72e3344fb1 366 motor->SetDeceleration(480);
Davidroid 6:ff72e3344fb1 367
Davidroid 6:ff72e3344fb1 368 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 369 wait_ms(5000);
Davidroid 6:ff72e3344fb1 370
Davidroid 6:ff72e3344fb1 371 /* Decreasing speed to 1200 step/s. */
Davidroid 6:ff72e3344fb1 372 motor->SetMaxSpeed(1200);
Davidroid 6:ff72e3344fb1 373
Davidroid 6:ff72e3344fb1 374 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 375 wait_ms(5000);
Davidroid 6:ff72e3344fb1 376
Davidroid 6:ff72e3344fb1 377 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 378 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 379
Davidroid 6:ff72e3344fb1 380 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 381 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 382
Davidroid 6:ff72e3344fb1 383
Davidroid 6:ff72e3344fb1 384 /*----- Requesting soft-stop while running. -----*/
Davidroid 6:ff72e3344fb1 385
Davidroid 6:ff72e3344fb1 386 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 387 printf("--> Requesting soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 388
Davidroid 6:ff72e3344fb1 389 /* Requesting soft stop. */
Davidroid 6:ff72e3344fb1 390 motor->SoftStop();
Davidroid 6:ff72e3344fb1 391
Davidroid 6:ff72e3344fb1 392 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 393 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 394
Davidroid 6:ff72e3344fb1 395 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 396 wait_ms(2000);
Davidroid 6:ff72e3344fb1 397
Davidroid 6:ff72e3344fb1 398
Davidroid 6:ff72e3344fb1 399 /*----- Requesting hard-stop while running. -----*/
Davidroid 6:ff72e3344fb1 400
Davidroid 6:ff72e3344fb1 401 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 402 printf("--> Running forward.\r\n");
Davidroid 6:ff72e3344fb1 403
Davidroid 6:ff72e3344fb1 404 /* Requesting to run in forward direction. */
Davidroid 6:ff72e3344fb1 405 motor->Run(StepperMotor::FWD);
Davidroid 6:ff72e3344fb1 406
Davidroid 6:ff72e3344fb1 407 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 408 wait_ms(5000);
Davidroid 6:ff72e3344fb1 409
Davidroid 6:ff72e3344fb1 410 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 411 printf("--> Requesting hard-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 412
Davidroid 6:ff72e3344fb1 413 /* Requesting to immediatly stop. */
Davidroid 6:ff72e3344fb1 414 motor->HardStop();
Davidroid 6:ff72e3344fb1 415
Davidroid 6:ff72e3344fb1 416 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 417 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 418
Davidroid 6:ff72e3344fb1 419 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 420 wait_ms(2000);
Davidroid 6:ff72e3344fb1 421
Davidroid 6:ff72e3344fb1 422
Davidroid 6:ff72e3344fb1 423 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 424
Davidroid 6:ff72e3344fb1 425 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 426 printf("--> Going to position 20000.\r\n");
Davidroid 6:ff72e3344fb1 427
Davidroid 6:ff72e3344fb1 428 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 429 motor->GoTo(20000);
Davidroid 6:ff72e3344fb1 430
Davidroid 6:ff72e3344fb1 431 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 432 wait_ms(5000);
Davidroid 6:ff72e3344fb1 433
Davidroid 6:ff72e3344fb1 434 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 435 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 436
Davidroid 6:ff72e3344fb1 437 /* Requesting to perform a soft stop */
Davidroid 6:ff72e3344fb1 438 motor->SoftStop();
Davidroid 6:ff72e3344fb1 439
Davidroid 6:ff72e3344fb1 440 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 441 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 442
Davidroid 6:ff72e3344fb1 443 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 444 wait_ms(2000);
Davidroid 6:ff72e3344fb1 445
Davidroid 6:ff72e3344fb1 446
Davidroid 6:ff72e3344fb1 447 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 6:ff72e3344fb1 448
Davidroid 6:ff72e3344fb1 449 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 450 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 6:ff72e3344fb1 451
Davidroid 6:ff72e3344fb1 452 /*
Davidroid 6:ff72e3344fb1 453 * Trying to read an inexistent register.
Davidroid 6:ff72e3344fb1 454 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 6:ff72e3344fb1 455 * function called.
Davidroid 6:ff72e3344fb1 456 */
Davidroid 6:ff72e3344fb1 457 motor->GetParameter(0x1F);
Davidroid 6:ff72e3344fb1 458
Davidroid 6:ff72e3344fb1 459 /* Waiting 0.5 seconds. */
Davidroid 6:ff72e3344fb1 460 wait_ms(500);
Davidroid 6:ff72e3344fb1 461
Davidroid 6:ff72e3344fb1 462
Davidroid 6:ff72e3344fb1 463 /*----- Changing step mode to full step mode. -----*/
Davidroid 6:ff72e3344fb1 464
Davidroid 6:ff72e3344fb1 465 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 466 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 6:ff72e3344fb1 467
Davidroid 6:ff72e3344fb1 468 /* Selecting full step mode. */
Davidroid 6:ff72e3344fb1 469 motor->SetStepMode(STEP_MODE_FULL);
Davidroid 6:ff72e3344fb1 470
Davidroid 6:ff72e3344fb1 471 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 6:ff72e3344fb1 472 motor->SetMaxSpeed(100);
Davidroid 6:ff72e3344fb1 473 motor->SetMinSpeed(50);
Davidroid 6:ff72e3344fb1 474 motor->SetAcceleration(10);
Davidroid 6:ff72e3344fb1 475 motor->SetDeceleration(10);
Davidroid 6:ff72e3344fb1 476
Davidroid 6:ff72e3344fb1 477 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 478 motor->GoTo(200);
Davidroid 6:ff72e3344fb1 479
Davidroid 6:ff72e3344fb1 480 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 481 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 482
Davidroid 6:ff72e3344fb1 483 /* Getting current position */
Davidroid 6:ff72e3344fb1 484 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 485
Davidroid 6:ff72e3344fb1 486 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 487 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 488
Davidroid 6:ff72e3344fb1 489 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 490 wait_ms(2000);
Davidroid 6:ff72e3344fb1 491
Davidroid 6:ff72e3344fb1 492
Davidroid 6:ff72e3344fb1 493 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 6:ff72e3344fb1 494
Davidroid 6:ff72e3344fb1 495 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 496 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 6:ff72e3344fb1 497
Davidroid 6:ff72e3344fb1 498 /* Resetting to 1/16 microstepping mode */
Davidroid 6:ff72e3344fb1 499 motor->SetStepMode(STEP_MODE_1_16);
Davidroid 6:ff72e3344fb1 500
Davidroid 6:ff72e3344fb1 501 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 6:ff72e3344fb1 502 motor->SetMaxSpeed(1600);
Davidroid 6:ff72e3344fb1 503 motor->SetMinSpeed(800);
Davidroid 6:ff72e3344fb1 504 motor->SetAcceleration(160);
Davidroid 6:ff72e3344fb1 505 motor->SetDeceleration(160);
Davidroid 6:ff72e3344fb1 506
Davidroid 6:ff72e3344fb1 507
Davidroid 6:ff72e3344fb1 508 /*----- Infinite Loop. -----*/
Davidroid 6:ff72e3344fb1 509
Davidroid 6:ff72e3344fb1 510 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 511 printf("--> Infinite Loop...\r\n");
Davidroid 6:ff72e3344fb1 512
Davidroid 6:ff72e3344fb1 513 /* Infinite Loop. */
Davidroid 6:ff72e3344fb1 514 while(1)
Davidroid 6:ff72e3344fb1 515 {
Davidroid 6:ff72e3344fb1 516 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 517 motor->GoTo(-6400);
Davidroid 1:5171df1bf684 518
Davidroid 0:fedf3cf2324a 519 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 520 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 521
Davidroid 0:fedf3cf2324a 522 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 523 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 524
Davidroid 0:fedf3cf2324a 525 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 526 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 527 }
Davidroid 0:fedf3cf2324a 528 }