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:
Thu Nov 26 16:15:13 2015 +0000
Revision:
6:ff72e3344fb1
Parent:
5:dfec8da854e5
Child:
7:eec0f9ec9719
+ Updated with the new version of the X_NUCLEO_IHM01A1 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 0:fedf3cf2324a 149 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 150 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:fedf3cf2324a 151
Davidroid 6:ff72e3344fb1 152
Davidroid 6:ff72e3344fb1 153 /*----- Moving forward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 154
Davidroid 6:ff72e3344fb1 155 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 156 printf("--> Moving forward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 157
Davidroid 6:ff72e3344fb1 158 /* Moving 16000 steps in the forward direction. */
Davidroid 6:ff72e3344fb1 159 motor->Move(StepperMotor::FWD, 16000);
Davidroid 6:ff72e3344fb1 160
Davidroid 6:ff72e3344fb1 161 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 162 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 163
Davidroid 6:ff72e3344fb1 164 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 165 wait_ms(2000);
Davidroid 6:ff72e3344fb1 166
Davidroid 6:ff72e3344fb1 167
Davidroid 6:ff72e3344fb1 168 /*----- Moving backward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 169
Davidroid 6:ff72e3344fb1 170 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 171 printf("--> Moving backward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 172
Davidroid 6:ff72e3344fb1 173 /* Moving 16000 steps in the backward direction. */
Davidroid 6:ff72e3344fb1 174 motor->Move(StepperMotor::BWD, 16000);
Davidroid 6:ff72e3344fb1 175
Davidroid 6:ff72e3344fb1 176 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 177 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 178
Davidroid 6:ff72e3344fb1 179 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 180 printf("--> Setting Home.\r\n");
Davidroid 6:ff72e3344fb1 181
Davidroid 6:ff72e3344fb1 182 /* Setting the current position to be the home position. */
Davidroid 6:ff72e3344fb1 183 motor->SetHome();
Davidroid 6:ff72e3344fb1 184
Davidroid 6:ff72e3344fb1 185 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 186 wait_ms(2000);
Davidroid 6:ff72e3344fb1 187
Davidroid 6:ff72e3344fb1 188
Davidroid 6:ff72e3344fb1 189 /*----- Going to a specified position. -----*/
Davidroid 6:ff72e3344fb1 190
Davidroid 6:ff72e3344fb1 191 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 192 printf("--> Going to position -6400.\r\n");
Davidroid 6:ff72e3344fb1 193
Davidroid 6:ff72e3344fb1 194 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 195 motor->GoTo(-6400);
Davidroid 6:ff72e3344fb1 196
Davidroid 6:ff72e3344fb1 197 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 198 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 199
Davidroid 6:ff72e3344fb1 200 /* Getting current position. */
Davidroid 6:ff72e3344fb1 201 int position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 202
Davidroid 6:ff72e3344fb1 203 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 204 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 205
Davidroid 6:ff72e3344fb1 206 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 207 printf("--> Setting a mark.\r\n");
Davidroid 6:ff72e3344fb1 208
Davidroid 6:ff72e3344fb1 209 /* Setting the current position to be the mark position. */
Davidroid 6:ff72e3344fb1 210 motor->SetMark();
Davidroid 6:ff72e3344fb1 211
Davidroid 6:ff72e3344fb1 212 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 213 wait_ms(2000);
Davidroid 6:ff72e3344fb1 214
Davidroid 6:ff72e3344fb1 215
Davidroid 6:ff72e3344fb1 216 /*----- Going Home. -----*/
Davidroid 6:ff72e3344fb1 217
Davidroid 6:ff72e3344fb1 218 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 219 printf("--> Going Home.\r\n");
Davidroid 6:ff72e3344fb1 220
Davidroid 6:ff72e3344fb1 221 /* Requesting to go to home */
Davidroid 6:ff72e3344fb1 222 motor->GoHome();
Davidroid 6:ff72e3344fb1 223
Davidroid 6:ff72e3344fb1 224 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 225 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 226
Davidroid 6:ff72e3344fb1 227 /* Getting current position. */
Davidroid 6:ff72e3344fb1 228 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 229
Davidroid 6:ff72e3344fb1 230 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 231 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 232
Davidroid 6:ff72e3344fb1 233 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 234 wait_ms(2000);
Davidroid 6:ff72e3344fb1 235
Davidroid 6:ff72e3344fb1 236
Davidroid 6:ff72e3344fb1 237 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 238
Davidroid 6:ff72e3344fb1 239 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 240 printf("--> Going to position 6400.\r\n");
Davidroid 6:ff72e3344fb1 241
Davidroid 6:ff72e3344fb1 242 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 243 motor->GoTo(6400);
Davidroid 6:ff72e3344fb1 244
Davidroid 6:ff72e3344fb1 245 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 246 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 247
Davidroid 6:ff72e3344fb1 248 /* Getting current position. */
Davidroid 6:ff72e3344fb1 249 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 250
Davidroid 6:ff72e3344fb1 251 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 252 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 253
Davidroid 6:ff72e3344fb1 254 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 255 wait_ms(2000);
Davidroid 6:ff72e3344fb1 256
Davidroid 6:ff72e3344fb1 257
Davidroid 6:ff72e3344fb1 258 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 6:ff72e3344fb1 259
Davidroid 6:ff72e3344fb1 260 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 261 printf("--> Going to the mark set previously.\r\n");
Davidroid 6:ff72e3344fb1 262
Davidroid 6:ff72e3344fb1 263 /* Requesting to go to mark position. */
Davidroid 6:ff72e3344fb1 264 motor->GoMark();
Davidroid 6:ff72e3344fb1 265
Davidroid 6:ff72e3344fb1 266 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 267 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 268
Davidroid 6:ff72e3344fb1 269 /* Getting current position. */
Davidroid 6:ff72e3344fb1 270 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 271
Davidroid 6:ff72e3344fb1 272 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 273 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 274
Davidroid 6:ff72e3344fb1 275 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 276 wait_ms(2000);
Davidroid 6:ff72e3344fb1 277
Davidroid 6:ff72e3344fb1 278
Davidroid 6:ff72e3344fb1 279 /*----- Running backward. -----*/
Davidroid 6:ff72e3344fb1 280
Davidroid 6:ff72e3344fb1 281 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 282 printf("--> Running backward.\r\n");
Davidroid 6:ff72e3344fb1 283
Davidroid 6:ff72e3344fb1 284 /* Requesting to run backward. */
Davidroid 6:ff72e3344fb1 285 motor->Run(StepperMotor::BWD);
Davidroid 6:ff72e3344fb1 286
Davidroid 6:ff72e3344fb1 287 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 288 wait_ms(5000);
Davidroid 6:ff72e3344fb1 289
Davidroid 6:ff72e3344fb1 290 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 291 int speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 292
Davidroid 6:ff72e3344fb1 293 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 294 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 295
Davidroid 6:ff72e3344fb1 296
Davidroid 6:ff72e3344fb1 297 /*----- Increasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 298
Davidroid 6:ff72e3344fb1 299 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 300 printf("--> Increasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 301
Davidroid 6:ff72e3344fb1 302 /* Increasing speed to 2400 step/s. */
Davidroid 6:ff72e3344fb1 303 motor->SetMaxSpeed(2400);
Davidroid 6:ff72e3344fb1 304
Davidroid 6:ff72e3344fb1 305 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 306 wait_ms(5000);
Davidroid 6:ff72e3344fb1 307
Davidroid 6:ff72e3344fb1 308 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 309 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 310
Davidroid 6:ff72e3344fb1 311 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 312 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 313
Davidroid 6:ff72e3344fb1 314
Davidroid 6:ff72e3344fb1 315 /*----- Decreasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 316
Davidroid 6:ff72e3344fb1 317 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 318 printf("--> Decreasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 319
Davidroid 6:ff72e3344fb1 320 /* Decreasing speed to 1200 step/s. */
Davidroid 6:ff72e3344fb1 321 motor->SetMaxSpeed(1200);
Davidroid 6:ff72e3344fb1 322
Davidroid 6:ff72e3344fb1 323 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 324 wait_ms(5000);
Davidroid 6:ff72e3344fb1 325
Davidroid 6:ff72e3344fb1 326 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 327 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 328
Davidroid 6:ff72e3344fb1 329 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 330 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 331
Davidroid 0:fedf3cf2324a 332
Davidroid 6:ff72e3344fb1 333 /*----- Increasing acceleration while running. -----*/
Davidroid 6:ff72e3344fb1 334
Davidroid 6:ff72e3344fb1 335 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 336 printf("--> Increasing acceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 337
Davidroid 6:ff72e3344fb1 338 /* Increasing acceleration to 480 step/s^2. */
Davidroid 6:ff72e3344fb1 339 motor->SetAcceleration(480);
Davidroid 6:ff72e3344fb1 340
Davidroid 6:ff72e3344fb1 341 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 342 wait_ms(5000);
Davidroid 6:ff72e3344fb1 343
Davidroid 6:ff72e3344fb1 344 /* Increasing speed to 2400 step/s. */
Davidroid 6:ff72e3344fb1 345 motor->SetMaxSpeed(2400);
Davidroid 6:ff72e3344fb1 346
Davidroid 6:ff72e3344fb1 347 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 348 wait_ms(5000);
Davidroid 6:ff72e3344fb1 349
Davidroid 6:ff72e3344fb1 350 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 351 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 352
Davidroid 6:ff72e3344fb1 353 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 354 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 355
Davidroid 6:ff72e3344fb1 356
Davidroid 6:ff72e3344fb1 357 /*----- Increasing deceleration while running. -----*/
Davidroid 6:ff72e3344fb1 358
Davidroid 6:ff72e3344fb1 359 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 360 printf("--> Increasing deceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 361
Davidroid 6:ff72e3344fb1 362 /* Increasing deceleration to 480 step/s^2. */
Davidroid 6:ff72e3344fb1 363 motor->SetDeceleration(480);
Davidroid 6:ff72e3344fb1 364
Davidroid 6:ff72e3344fb1 365 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 366 wait_ms(5000);
Davidroid 6:ff72e3344fb1 367
Davidroid 6:ff72e3344fb1 368 /* Decreasing speed to 1200 step/s. */
Davidroid 6:ff72e3344fb1 369 motor->SetMaxSpeed(1200);
Davidroid 6:ff72e3344fb1 370
Davidroid 6:ff72e3344fb1 371 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 372 wait_ms(5000);
Davidroid 6:ff72e3344fb1 373
Davidroid 6:ff72e3344fb1 374 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 375 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 376
Davidroid 6:ff72e3344fb1 377 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 378 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 379
Davidroid 6:ff72e3344fb1 380
Davidroid 6:ff72e3344fb1 381 /*----- Requesting soft-stop while running. -----*/
Davidroid 6:ff72e3344fb1 382
Davidroid 6:ff72e3344fb1 383 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 384 printf("--> Requesting soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 385
Davidroid 6:ff72e3344fb1 386 /* Requesting soft stop. */
Davidroid 6:ff72e3344fb1 387 motor->SoftStop();
Davidroid 6:ff72e3344fb1 388
Davidroid 6:ff72e3344fb1 389 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 390 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 391
Davidroid 6:ff72e3344fb1 392 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 393 wait_ms(2000);
Davidroid 6:ff72e3344fb1 394
Davidroid 6:ff72e3344fb1 395
Davidroid 6:ff72e3344fb1 396 /*----- Requesting hard-stop while running. -----*/
Davidroid 6:ff72e3344fb1 397
Davidroid 6:ff72e3344fb1 398 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 399 printf("--> Running forward.\r\n");
Davidroid 6:ff72e3344fb1 400
Davidroid 6:ff72e3344fb1 401 /* Requesting to run in forward direction. */
Davidroid 6:ff72e3344fb1 402 motor->Run(StepperMotor::FWD);
Davidroid 6:ff72e3344fb1 403
Davidroid 6:ff72e3344fb1 404 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 405 wait_ms(5000);
Davidroid 6:ff72e3344fb1 406
Davidroid 6:ff72e3344fb1 407 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 408 printf("--> Requesting hard-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 409
Davidroid 6:ff72e3344fb1 410 /* Requesting to immediatly stop. */
Davidroid 6:ff72e3344fb1 411 motor->HardStop();
Davidroid 6:ff72e3344fb1 412
Davidroid 6:ff72e3344fb1 413 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 414 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 415
Davidroid 6:ff72e3344fb1 416 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 417 wait_ms(2000);
Davidroid 6:ff72e3344fb1 418
Davidroid 6:ff72e3344fb1 419
Davidroid 6:ff72e3344fb1 420 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 421
Davidroid 6:ff72e3344fb1 422 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 423 printf("--> Going to position 20000.\r\n");
Davidroid 6:ff72e3344fb1 424
Davidroid 6:ff72e3344fb1 425 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 426 motor->GoTo(20000);
Davidroid 6:ff72e3344fb1 427
Davidroid 6:ff72e3344fb1 428 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 429 wait_ms(5000);
Davidroid 6:ff72e3344fb1 430
Davidroid 6:ff72e3344fb1 431 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 432 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 433
Davidroid 6:ff72e3344fb1 434 /* Requesting to perform a soft stop */
Davidroid 6:ff72e3344fb1 435 motor->SoftStop();
Davidroid 6:ff72e3344fb1 436
Davidroid 6:ff72e3344fb1 437 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 438 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 439
Davidroid 6:ff72e3344fb1 440 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 441 wait_ms(2000);
Davidroid 6:ff72e3344fb1 442
Davidroid 6:ff72e3344fb1 443
Davidroid 6:ff72e3344fb1 444 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 6:ff72e3344fb1 445
Davidroid 6:ff72e3344fb1 446 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 447 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 6:ff72e3344fb1 448
Davidroid 6:ff72e3344fb1 449 /*
Davidroid 6:ff72e3344fb1 450 * Trying to read an inexistent register.
Davidroid 6:ff72e3344fb1 451 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 6:ff72e3344fb1 452 * function called.
Davidroid 6:ff72e3344fb1 453 */
Davidroid 6:ff72e3344fb1 454 motor->GetParameter(0x1F);
Davidroid 6:ff72e3344fb1 455
Davidroid 6:ff72e3344fb1 456 /* Waiting 0.5 seconds. */
Davidroid 6:ff72e3344fb1 457 wait_ms(500);
Davidroid 6:ff72e3344fb1 458
Davidroid 6:ff72e3344fb1 459
Davidroid 6:ff72e3344fb1 460 /*----- Changing step mode to full step mode. -----*/
Davidroid 6:ff72e3344fb1 461
Davidroid 6:ff72e3344fb1 462 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 463 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 6:ff72e3344fb1 464
Davidroid 6:ff72e3344fb1 465 /* Selecting full step mode. */
Davidroid 6:ff72e3344fb1 466 motor->SetStepMode(STEP_MODE_FULL);
Davidroid 6:ff72e3344fb1 467
Davidroid 6:ff72e3344fb1 468 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 6:ff72e3344fb1 469 motor->SetMaxSpeed(100);
Davidroid 6:ff72e3344fb1 470 motor->SetMinSpeed(50);
Davidroid 6:ff72e3344fb1 471 motor->SetAcceleration(10);
Davidroid 6:ff72e3344fb1 472 motor->SetDeceleration(10);
Davidroid 6:ff72e3344fb1 473
Davidroid 6:ff72e3344fb1 474 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 475 motor->GoTo(200);
Davidroid 6:ff72e3344fb1 476
Davidroid 6:ff72e3344fb1 477 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 478 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 479
Davidroid 6:ff72e3344fb1 480 /* Getting current position */
Davidroid 6:ff72e3344fb1 481 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 482
Davidroid 6:ff72e3344fb1 483 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 484 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 485
Davidroid 6:ff72e3344fb1 486 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 487 wait_ms(2000);
Davidroid 6:ff72e3344fb1 488
Davidroid 6:ff72e3344fb1 489
Davidroid 6:ff72e3344fb1 490 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 6:ff72e3344fb1 491
Davidroid 6:ff72e3344fb1 492 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 493 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 6:ff72e3344fb1 494
Davidroid 6:ff72e3344fb1 495 /* Resetting to 1/16 microstepping mode */
Davidroid 6:ff72e3344fb1 496 motor->SetStepMode(STEP_MODE_1_16);
Davidroid 6:ff72e3344fb1 497
Davidroid 6:ff72e3344fb1 498 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 6:ff72e3344fb1 499 motor->SetMaxSpeed(1600);
Davidroid 6:ff72e3344fb1 500 motor->SetMinSpeed(800);
Davidroid 6:ff72e3344fb1 501 motor->SetAcceleration(160);
Davidroid 6:ff72e3344fb1 502 motor->SetDeceleration(160);
Davidroid 6:ff72e3344fb1 503
Davidroid 6:ff72e3344fb1 504
Davidroid 6:ff72e3344fb1 505 /*----- Infinite Loop. -----*/
Davidroid 6:ff72e3344fb1 506
Davidroid 6:ff72e3344fb1 507 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 508 printf("--> Infinite Loop...\r\n");
Davidroid 6:ff72e3344fb1 509
Davidroid 6:ff72e3344fb1 510 /* Infinite Loop. */
Davidroid 6:ff72e3344fb1 511 while(1)
Davidroid 6:ff72e3344fb1 512 {
Davidroid 6:ff72e3344fb1 513 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 514 motor->GoTo(-6400);
Davidroid 1:5171df1bf684 515
Davidroid 0:fedf3cf2324a 516 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 517 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 518
Davidroid 0:fedf3cf2324a 519 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 520 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 521
Davidroid 0:fedf3cf2324a 522 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 523 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 524 }
Davidroid 0:fedf3cf2324a 525 }