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 Apr 07 16:47:30 2016 +0000
Revision:
20:bec1eba352a5
Parent:
15:4a1684b7252b
Child:
22:ce8a202d67ac
+ 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 15:4a1684b7252b 7 * @brief mbed test application for the STMicroelectronics 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 20:bec1eba352a5 55 /* Initialization parameters. */
Davidroid 20:bec1eba352a5 56 L6474_Init_t init =
Davidroid 20:bec1eba352a5 57 {
Davidroid 20:bec1eba352a5 58 160, /* Acceleration rate in step/s2. Range: (0..+inf). */
Davidroid 20:bec1eba352a5 59 160, /* Deceleration rate in step/s2. Range: (0..+inf). */
Davidroid 20:bec1eba352a5 60 1600, /* Maximum speed in step/s. Range: (30..10000]. */
Davidroid 20:bec1eba352a5 61 800, /* Minimum speed in step/s. Range: [30..10000). */
Davidroid 20:bec1eba352a5 62 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 20:bec1eba352a5 63 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 20:bec1eba352a5 64 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 20:bec1eba352a5 65 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
Davidroid 20:bec1eba352a5 66 L6474_STEP_SEL_1_16, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 20:bec1eba352a5 67 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 20:bec1eba352a5 68 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 20:bec1eba352a5 69 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 20:bec1eba352a5 70 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 20:bec1eba352a5 71 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 20:bec1eba352a5 72 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 20:bec1eba352a5 73 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 20:bec1eba352a5 74 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 20:bec1eba352a5 75 L6474_ALARM_EN_OVERCURRENT |
Davidroid 20:bec1eba352a5 76 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 20:bec1eba352a5 77 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 20:bec1eba352a5 78 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 20:bec1eba352a5 79 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 20:bec1eba352a5 80 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 20:bec1eba352a5 81 };
Davidroid 20:bec1eba352a5 82
Davidroid 0:fedf3cf2324a 83 /* Motor Control Component. */
Davidroid 1:5171df1bf684 84 L6474 *motor;
Davidroid 0:fedf3cf2324a 85
Davidroid 0:fedf3cf2324a 86
Davidroid 5:dfec8da854e5 87 /* Functions -----------------------------------------------------------------*/
Davidroid 5:dfec8da854e5 88
Davidroid 5:dfec8da854e5 89 /**
Davidroid 5:dfec8da854e5 90 * @brief This is an example of user handler for the flag interrupt.
Davidroid 5:dfec8da854e5 91 * @param None
Davidroid 5:dfec8da854e5 92 * @retval None
Davidroid 5:dfec8da854e5 93 * @note If needed, implement it, and then attach and enable it:
Davidroid 5:dfec8da854e5 94 * + motor->AttachFlagIRQ(&FlagIRQHandler);
Davidroid 5:dfec8da854e5 95 * + motor->EnableFlagIRQ();
Davidroid 5:dfec8da854e5 96 * To disable it:
Davidroid 5:dfec8da854e5 97 * + motor->DisbleFlagIRQ();
Davidroid 5:dfec8da854e5 98 */
Davidroid 5:dfec8da854e5 99 void FlagIRQHandler(void)
Davidroid 5:dfec8da854e5 100 {
Davidroid 5:dfec8da854e5 101 /* Set ISR flag. */
Davidroid 14:7264b1f6afec 102 motor->isr_flag = TRUE;
Davidroid 5:dfec8da854e5 103
Davidroid 5:dfec8da854e5 104 /* Get the value of the status register. */
Davidroid 5:dfec8da854e5 105 unsigned int status = motor->GetStatus();
Davidroid 5:dfec8da854e5 106
Davidroid 5:dfec8da854e5 107 /* Check HIZ flag: if set, power brigdes are disabled. */
Davidroid 5:dfec8da854e5 108 if ((status & L6474_STATUS_HIZ) == L6474_STATUS_HIZ)
Davidroid 5:dfec8da854e5 109 { /* HIZ state. Action to be customized. */ }
Davidroid 5:dfec8da854e5 110
Davidroid 5:dfec8da854e5 111 /* Check direction. */
Davidroid 5:dfec8da854e5 112 if ((status & L6474_STATUS_DIR) == L6474_STATUS_DIR)
Davidroid 5:dfec8da854e5 113 { /* Forward direction is set. Action to be customized. */ }
Davidroid 5:dfec8da854e5 114 else
Davidroid 5:dfec8da854e5 115 { /* Backward direction is set. Action to be customized. */ }
Davidroid 5:dfec8da854e5 116
Davidroid 5:dfec8da854e5 117 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 14:7264b1f6afec 118 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
Davidroid 5:dfec8da854e5 119 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD)
Davidroid 5:dfec8da854e5 120 { /* Command received by SPI can't be performed. Action to be customized. */ }
Davidroid 14:7264b1f6afec 121
Davidroid 5:dfec8da854e5 122 /* Check WRONG_CMD flag: if set, the command does not exist. */
Davidroid 5:dfec8da854e5 123 if ((status & L6474_STATUS_WRONG_CMD) == L6474_STATUS_WRONG_CMD)
Davidroid 5:dfec8da854e5 124 { /* The command received by SPI does not exist. Action to be customized. */ }
Davidroid 5:dfec8da854e5 125
Davidroid 5:dfec8da854e5 126 /* Check UVLO flag: if not set, there is an undervoltage lock-out. */
Davidroid 5:dfec8da854e5 127 if ((status & L6474_STATUS_UVLO) == 0)
Davidroid 5:dfec8da854e5 128 { /* Undervoltage lock-out. Action to be customized. */ }
Davidroid 5:dfec8da854e5 129
Davidroid 5:dfec8da854e5 130 /* Check TH_WRN flag: if not set, the thermal warning threshold is reached. */
Davidroid 5:dfec8da854e5 131 if ((status & L6474_STATUS_TH_WRN) == 0)
Davidroid 5:dfec8da854e5 132 { /* Thermal warning threshold is reached. Action to be customized. */ }
Davidroid 5:dfec8da854e5 133
Davidroid 5:dfec8da854e5 134 /* Check TH_SHD flag: if not set, the thermal shut down threshold is reached. */
Davidroid 5:dfec8da854e5 135 if ((status & L6474_STATUS_TH_SD) == 0)
Davidroid 5:dfec8da854e5 136 { /* Thermal shut down threshold is reached. Action to be customized. */ }
Davidroid 5:dfec8da854e5 137
Davidroid 5:dfec8da854e5 138 /* Check OCD flag: if not set, there is an overcurrent detection. */
Davidroid 5:dfec8da854e5 139 if ((status & L6474_STATUS_OCD) == 0)
Davidroid 5:dfec8da854e5 140 { /* Overcurrent detection. Action to be customized. */ }
Davidroid 5:dfec8da854e5 141
Davidroid 5:dfec8da854e5 142 /* Reset ISR flag. */
Davidroid 14:7264b1f6afec 143 motor->isr_flag = FALSE;
Davidroid 5:dfec8da854e5 144 }
Davidroid 5:dfec8da854e5 145
Davidroid 5:dfec8da854e5 146 /**
Davidroid 5:dfec8da854e5 147 * @brief This is an example of user handler for the errors.
Davidroid 5:dfec8da854e5 148 * @param error error-code.
Davidroid 5:dfec8da854e5 149 * @retval None
Davidroid 5:dfec8da854e5 150 * @note If needed, implement it, and then attach it:
Davidroid 5:dfec8da854e5 151 * + motor->AttachErrorHandler(&ErrorHandler);
Davidroid 5:dfec8da854e5 152 */
Davidroid 5:dfec8da854e5 153 void ErrorHandler(uint16_t error)
Davidroid 5:dfec8da854e5 154 {
Davidroid 5:dfec8da854e5 155 /* Printing to the console. */
Davidroid 5:dfec8da854e5 156 printf("Error: %d.\r\n", error);
Davidroid 5:dfec8da854e5 157
Davidroid 5:dfec8da854e5 158 /* Aborting the program. */
Davidroid 5:dfec8da854e5 159 exit(EXIT_FAILURE);
Davidroid 5:dfec8da854e5 160 }
Davidroid 5:dfec8da854e5 161
Davidroid 5:dfec8da854e5 162
Davidroid 0:fedf3cf2324a 163 /* Main ----------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 164
Davidroid 0:fedf3cf2324a 165 int main()
Davidroid 0:fedf3cf2324a 166 {
Davidroid 6:ff72e3344fb1 167 /*----- Initialization. -----*/
Davidroid 6:ff72e3344fb1 168
Davidroid 0:fedf3cf2324a 169 /* Initializing SPI bus. */
Davidroid 0:fedf3cf2324a 170 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:fedf3cf2324a 171
Davidroid 0:fedf3cf2324a 172 /* Initializing Motor Control Component. */
Davidroid 4:43df256f26d9 173 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 20:bec1eba352a5 174 if (motor->Init(&init) != COMPONENT_OK)
Davidroid 12:fac478c7ea0b 175 exit(EXIT_FAILURE);
Davidroid 0:fedf3cf2324a 176
Davidroid 0:fedf3cf2324a 177 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 178 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:fedf3cf2324a 179
Davidroid 6:ff72e3344fb1 180
Davidroid 6:ff72e3344fb1 181 /*----- Moving forward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 182
Davidroid 6:ff72e3344fb1 183 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 184 printf("--> Moving forward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 185
Davidroid 6:ff72e3344fb1 186 /* Moving 16000 steps in the forward direction. */
Davidroid 6:ff72e3344fb1 187 motor->Move(StepperMotor::FWD, 16000);
Davidroid 6:ff72e3344fb1 188
Davidroid 6:ff72e3344fb1 189 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 190 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 191
Davidroid 6:ff72e3344fb1 192 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 193 wait_ms(2000);
Davidroid 6:ff72e3344fb1 194
Davidroid 6:ff72e3344fb1 195
Davidroid 6:ff72e3344fb1 196 /*----- Moving backward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 197
Davidroid 6:ff72e3344fb1 198 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 199 printf("--> Moving backward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 200
Davidroid 6:ff72e3344fb1 201 /* Moving 16000 steps in the backward direction. */
Davidroid 6:ff72e3344fb1 202 motor->Move(StepperMotor::BWD, 16000);
Davidroid 6:ff72e3344fb1 203
Davidroid 6:ff72e3344fb1 204 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 205 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 206
Davidroid 6:ff72e3344fb1 207 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 208 printf("--> Setting Home.\r\n");
Davidroid 6:ff72e3344fb1 209
Davidroid 6:ff72e3344fb1 210 /* Setting the current position to be the home position. */
Davidroid 6:ff72e3344fb1 211 motor->SetHome();
Davidroid 6:ff72e3344fb1 212
Davidroid 6:ff72e3344fb1 213 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 214 wait_ms(2000);
Davidroid 6:ff72e3344fb1 215
Davidroid 6:ff72e3344fb1 216
Davidroid 6:ff72e3344fb1 217 /*----- Going to a specified position. -----*/
Davidroid 6:ff72e3344fb1 218
Davidroid 6:ff72e3344fb1 219 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 220 printf("--> Going to position -6400.\r\n");
Davidroid 6:ff72e3344fb1 221
Davidroid 6:ff72e3344fb1 222 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 223 motor->GoTo(-6400);
Davidroid 6:ff72e3344fb1 224
Davidroid 6:ff72e3344fb1 225 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 226 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 227
Davidroid 6:ff72e3344fb1 228 /* Getting current position. */
Davidroid 6:ff72e3344fb1 229 int position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 230
Davidroid 6:ff72e3344fb1 231 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 232 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 233
Davidroid 6:ff72e3344fb1 234 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 235 printf("--> Setting a mark.\r\n");
Davidroid 6:ff72e3344fb1 236
Davidroid 6:ff72e3344fb1 237 /* Setting the current position to be the mark position. */
Davidroid 6:ff72e3344fb1 238 motor->SetMark();
Davidroid 6:ff72e3344fb1 239
Davidroid 6:ff72e3344fb1 240 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 241 wait_ms(2000);
Davidroid 6:ff72e3344fb1 242
Davidroid 6:ff72e3344fb1 243
Davidroid 6:ff72e3344fb1 244 /*----- Going Home. -----*/
Davidroid 6:ff72e3344fb1 245
Davidroid 6:ff72e3344fb1 246 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 247 printf("--> Going Home.\r\n");
Davidroid 6:ff72e3344fb1 248
Davidroid 6:ff72e3344fb1 249 /* Requesting to go to home */
Davidroid 6:ff72e3344fb1 250 motor->GoHome();
Davidroid 6:ff72e3344fb1 251
Davidroid 6:ff72e3344fb1 252 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 253 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 254
Davidroid 6:ff72e3344fb1 255 /* Getting current position. */
Davidroid 6:ff72e3344fb1 256 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 257
Davidroid 6:ff72e3344fb1 258 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 259 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 260
Davidroid 6:ff72e3344fb1 261 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 262 wait_ms(2000);
Davidroid 6:ff72e3344fb1 263
Davidroid 6:ff72e3344fb1 264
Davidroid 6:ff72e3344fb1 265 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 266
Davidroid 6:ff72e3344fb1 267 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 268 printf("--> Going to position 6400.\r\n");
Davidroid 6:ff72e3344fb1 269
Davidroid 6:ff72e3344fb1 270 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 271 motor->GoTo(6400);
Davidroid 6:ff72e3344fb1 272
Davidroid 6:ff72e3344fb1 273 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 274 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 275
Davidroid 6:ff72e3344fb1 276 /* Getting current position. */
Davidroid 6:ff72e3344fb1 277 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 278
Davidroid 6:ff72e3344fb1 279 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 280 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 281
Davidroid 6:ff72e3344fb1 282 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 283 wait_ms(2000);
Davidroid 6:ff72e3344fb1 284
Davidroid 6:ff72e3344fb1 285
Davidroid 6:ff72e3344fb1 286 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 6:ff72e3344fb1 287
Davidroid 6:ff72e3344fb1 288 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 289 printf("--> Going to the mark set previously.\r\n");
Davidroid 6:ff72e3344fb1 290
Davidroid 6:ff72e3344fb1 291 /* Requesting to go to mark position. */
Davidroid 6:ff72e3344fb1 292 motor->GoMark();
Davidroid 6:ff72e3344fb1 293
Davidroid 6:ff72e3344fb1 294 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 295 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 296
Davidroid 6:ff72e3344fb1 297 /* Getting current position. */
Davidroid 6:ff72e3344fb1 298 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 299
Davidroid 6:ff72e3344fb1 300 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 301 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 302
Davidroid 6:ff72e3344fb1 303 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 304 wait_ms(2000);
Davidroid 6:ff72e3344fb1 305
Davidroid 6:ff72e3344fb1 306
Davidroid 6:ff72e3344fb1 307 /*----- Running backward. -----*/
Davidroid 6:ff72e3344fb1 308
Davidroid 6:ff72e3344fb1 309 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 310 printf("--> Running backward.\r\n");
Davidroid 6:ff72e3344fb1 311
Davidroid 6:ff72e3344fb1 312 /* Requesting to run backward. */
Davidroid 6:ff72e3344fb1 313 motor->Run(StepperMotor::BWD);
Davidroid 6:ff72e3344fb1 314
Davidroid 6:ff72e3344fb1 315 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 316 wait_ms(5000);
Davidroid 6:ff72e3344fb1 317
Davidroid 6:ff72e3344fb1 318 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 319 int speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 320
Davidroid 6:ff72e3344fb1 321 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 322 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 323
Davidroid 6:ff72e3344fb1 324
Davidroid 6:ff72e3344fb1 325 /*----- Increasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 326
Davidroid 6:ff72e3344fb1 327 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 328 printf("--> Increasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 329
Davidroid 6:ff72e3344fb1 330 /* Increasing speed to 2400 step/s. */
Davidroid 6:ff72e3344fb1 331 motor->SetMaxSpeed(2400);
Davidroid 6:ff72e3344fb1 332
Davidroid 6:ff72e3344fb1 333 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 334 wait_ms(5000);
Davidroid 6:ff72e3344fb1 335
Davidroid 6:ff72e3344fb1 336 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 337 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 338
Davidroid 6:ff72e3344fb1 339 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 340 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 341
Davidroid 6:ff72e3344fb1 342
Davidroid 6:ff72e3344fb1 343 /*----- Decreasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 344
Davidroid 6:ff72e3344fb1 345 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 346 printf("--> Decreasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 347
Davidroid 6:ff72e3344fb1 348 /* Decreasing speed to 1200 step/s. */
Davidroid 6:ff72e3344fb1 349 motor->SetMaxSpeed(1200);
Davidroid 6:ff72e3344fb1 350
Davidroid 6:ff72e3344fb1 351 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 352 wait_ms(5000);
Davidroid 6:ff72e3344fb1 353
Davidroid 6:ff72e3344fb1 354 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 355 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 356
Davidroid 6:ff72e3344fb1 357 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 358 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 359
Davidroid 0:fedf3cf2324a 360
Davidroid 6:ff72e3344fb1 361 /*----- Increasing acceleration while running. -----*/
Davidroid 6:ff72e3344fb1 362
Davidroid 6:ff72e3344fb1 363 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 364 printf("--> Increasing acceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 365
Davidroid 6:ff72e3344fb1 366 /* Increasing acceleration to 480 step/s^2. */
Davidroid 6:ff72e3344fb1 367 motor->SetAcceleration(480);
Davidroid 6:ff72e3344fb1 368
Davidroid 6:ff72e3344fb1 369 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 370 wait_ms(5000);
Davidroid 6:ff72e3344fb1 371
Davidroid 6:ff72e3344fb1 372 /* Increasing speed to 2400 step/s. */
Davidroid 6:ff72e3344fb1 373 motor->SetMaxSpeed(2400);
Davidroid 6:ff72e3344fb1 374
Davidroid 6:ff72e3344fb1 375 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 376 wait_ms(5000);
Davidroid 6:ff72e3344fb1 377
Davidroid 6:ff72e3344fb1 378 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 379 speed = motor->GetSpeed();
Davidroid 6:ff72e3344fb1 380
Davidroid 6:ff72e3344fb1 381 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 382 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 383
Davidroid 6:ff72e3344fb1 384
Davidroid 6:ff72e3344fb1 385 /*----- Increasing deceleration while running. -----*/
Davidroid 6:ff72e3344fb1 386
Davidroid 6:ff72e3344fb1 387 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 388 printf("--> Increasing deceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 389
Davidroid 6:ff72e3344fb1 390 /* Increasing deceleration to 480 step/s^2. */
Davidroid 6:ff72e3344fb1 391 motor->SetDeceleration(480);
Davidroid 6:ff72e3344fb1 392
Davidroid 6:ff72e3344fb1 393 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 394 wait_ms(5000);
Davidroid 6:ff72e3344fb1 395
Davidroid 6:ff72e3344fb1 396 /* Decreasing speed to 1200 step/s. */
Davidroid 6:ff72e3344fb1 397 motor->SetMaxSpeed(1200);
Davidroid 6:ff72e3344fb1 398
Davidroid 6:ff72e3344fb1 399 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 400 wait_ms(5000);
Davidroid 6:ff72e3344fb1 401
Davidroid 6:ff72e3344fb1 402 /* Getting current speed. */
Davidroid 6:ff72e3344fb1 403 speed = motor->GetSpeed();
Davidroid 0:fedf3cf2324a 404
Davidroid 6:ff72e3344fb1 405 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 406 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 407
Davidroid 6:ff72e3344fb1 408
Davidroid 6:ff72e3344fb1 409 /*----- Requesting soft-stop while running. -----*/
Davidroid 6:ff72e3344fb1 410
Davidroid 6:ff72e3344fb1 411 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 412 printf("--> Requesting soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 413
Davidroid 6:ff72e3344fb1 414 /* Requesting soft stop. */
Davidroid 6:ff72e3344fb1 415 motor->SoftStop();
Davidroid 6:ff72e3344fb1 416
Davidroid 6:ff72e3344fb1 417 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 418 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 419
Davidroid 6:ff72e3344fb1 420 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 421 wait_ms(2000);
Davidroid 6:ff72e3344fb1 422
Davidroid 6:ff72e3344fb1 423
Davidroid 6:ff72e3344fb1 424 /*----- Requesting hard-stop while running. -----*/
Davidroid 6:ff72e3344fb1 425
Davidroid 6:ff72e3344fb1 426 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 427 printf("--> Running forward.\r\n");
Davidroid 6:ff72e3344fb1 428
Davidroid 6:ff72e3344fb1 429 /* Requesting to run in forward direction. */
Davidroid 6:ff72e3344fb1 430 motor->Run(StepperMotor::FWD);
Davidroid 6:ff72e3344fb1 431
Davidroid 6:ff72e3344fb1 432 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 433 wait_ms(5000);
Davidroid 6:ff72e3344fb1 434
Davidroid 6:ff72e3344fb1 435 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 436 printf("--> Requesting hard-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 437
Davidroid 6:ff72e3344fb1 438 /* Requesting to immediatly stop. */
Davidroid 6:ff72e3344fb1 439 motor->HardStop();
Davidroid 6:ff72e3344fb1 440
Davidroid 6:ff72e3344fb1 441 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 442 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 443
Davidroid 6:ff72e3344fb1 444 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 445 wait_ms(2000);
Davidroid 6:ff72e3344fb1 446
Davidroid 6:ff72e3344fb1 447
Davidroid 6:ff72e3344fb1 448 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 449
Davidroid 6:ff72e3344fb1 450 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 451 printf("--> Going to position 20000.\r\n");
Davidroid 6:ff72e3344fb1 452
Davidroid 6:ff72e3344fb1 453 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 454 motor->GoTo(20000);
Davidroid 6:ff72e3344fb1 455
Davidroid 6:ff72e3344fb1 456 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 457 wait_ms(5000);
Davidroid 6:ff72e3344fb1 458
Davidroid 6:ff72e3344fb1 459 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 460 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 461
Davidroid 6:ff72e3344fb1 462 /* Requesting to perform a soft stop */
Davidroid 6:ff72e3344fb1 463 motor->SoftStop();
Davidroid 6:ff72e3344fb1 464
Davidroid 6:ff72e3344fb1 465 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 466 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 467
Davidroid 6:ff72e3344fb1 468 /* Waiting 2 seconds. */
Davidroid 20:bec1eba352a5 469 wait_ms(2000);
Davidroid 6:ff72e3344fb1 470
Davidroid 6:ff72e3344fb1 471
Davidroid 6:ff72e3344fb1 472 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 6:ff72e3344fb1 473
Davidroid 6:ff72e3344fb1 474 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 475 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 6:ff72e3344fb1 476
Davidroid 6:ff72e3344fb1 477 /*
Davidroid 6:ff72e3344fb1 478 * Trying to read an inexistent register.
Davidroid 6:ff72e3344fb1 479 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 6:ff72e3344fb1 480 * function called.
Davidroid 6:ff72e3344fb1 481 */
Davidroid 6:ff72e3344fb1 482 motor->GetParameter(0x1F);
Davidroid 6:ff72e3344fb1 483
Davidroid 6:ff72e3344fb1 484 /* Waiting 0.5 seconds. */
Davidroid 6:ff72e3344fb1 485 wait_ms(500);
Davidroid 6:ff72e3344fb1 486
Davidroid 6:ff72e3344fb1 487
Davidroid 6:ff72e3344fb1 488 /*----- Changing step mode to full step mode. -----*/
Davidroid 6:ff72e3344fb1 489
Davidroid 6:ff72e3344fb1 490 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 491 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 6:ff72e3344fb1 492
Davidroid 6:ff72e3344fb1 493 /* Selecting full step mode. */
Davidroid 20:bec1eba352a5 494 if (!motor->SetStepMode((StepperMotor::step_mode_t) STEP_MODE_FULL))
Davidroid 20:bec1eba352a5 495 printf(" Step Mode not allowed.\r\n");
Davidroid 6:ff72e3344fb1 496
Davidroid 6:ff72e3344fb1 497 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 6:ff72e3344fb1 498 motor->SetMaxSpeed(100);
Davidroid 6:ff72e3344fb1 499 motor->SetMinSpeed(50);
Davidroid 6:ff72e3344fb1 500 motor->SetAcceleration(10);
Davidroid 6:ff72e3344fb1 501 motor->SetDeceleration(10);
Davidroid 6:ff72e3344fb1 502
Davidroid 6:ff72e3344fb1 503 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 504 motor->GoTo(200);
Davidroid 6:ff72e3344fb1 505
Davidroid 6:ff72e3344fb1 506 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 507 motor->WaitWhileActive();
Davidroid 6:ff72e3344fb1 508
Davidroid 6:ff72e3344fb1 509 /* Getting current position */
Davidroid 6:ff72e3344fb1 510 position = motor->GetPosition();
Davidroid 6:ff72e3344fb1 511
Davidroid 6:ff72e3344fb1 512 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 513 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 514
Davidroid 6:ff72e3344fb1 515 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 516 wait_ms(2000);
Davidroid 6:ff72e3344fb1 517
Davidroid 6:ff72e3344fb1 518
Davidroid 6:ff72e3344fb1 519 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 6:ff72e3344fb1 520
Davidroid 6:ff72e3344fb1 521 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 522 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 6:ff72e3344fb1 523
Davidroid 6:ff72e3344fb1 524 /* Resetting to 1/16 microstepping mode */
Davidroid 20:bec1eba352a5 525 if (!motor->SetStepMode((StepperMotor::step_mode_t) STEP_MODE_1_16))
Davidroid 20:bec1eba352a5 526 printf(" Step Mode not allowed.\r\n");
Davidroid 6:ff72e3344fb1 527
Davidroid 6:ff72e3344fb1 528 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 6:ff72e3344fb1 529 motor->SetMaxSpeed(1600);
Davidroid 6:ff72e3344fb1 530 motor->SetMinSpeed(800);
Davidroid 6:ff72e3344fb1 531 motor->SetAcceleration(160);
Davidroid 6:ff72e3344fb1 532 motor->SetDeceleration(160);
Davidroid 6:ff72e3344fb1 533
Davidroid 6:ff72e3344fb1 534
Davidroid 6:ff72e3344fb1 535 /*----- Infinite Loop. -----*/
Davidroid 6:ff72e3344fb1 536
Davidroid 6:ff72e3344fb1 537 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 538 printf("--> Infinite Loop...\r\n");
Davidroid 6:ff72e3344fb1 539
Davidroid 6:ff72e3344fb1 540 /* Infinite Loop. */
Davidroid 6:ff72e3344fb1 541 while(1)
Davidroid 6:ff72e3344fb1 542 {
Davidroid 6:ff72e3344fb1 543 /* Requesting to go to a specified position. */
Davidroid 6:ff72e3344fb1 544 motor->GoTo(-6400);
Davidroid 1:5171df1bf684 545
Davidroid 0:fedf3cf2324a 546 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 547 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 548
Davidroid 0:fedf3cf2324a 549 /* Requesting to go to a specified position. */
Davidroid 1:5171df1bf684 550 motor->GoTo(6400);
Davidroid 0:fedf3cf2324a 551
Davidroid 0:fedf3cf2324a 552 /* Waiting while the motor is active. */
Davidroid 1:5171df1bf684 553 motor->WaitWhileActive();
Davidroid 0:fedf3cf2324a 554 }
Davidroid 0:fedf3cf2324a 555 }