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