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 Mar 10 14:50:46 2017 +0000
Revision:
26:7a58cede28f7
Parent:
25:205b3d09e64e
Aligned to ARM mbed coding style.

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 26:7a58cede28f7 50 #include "L6474.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 26:7a58cede28f7 56 L6474_init_t init = {
Davidroid 22:ce8a202d67ac 57 160, /* Acceleration rate in pps^2. Range: (0..+inf). */
Davidroid 22:ce8a202d67ac 58 160, /* Deceleration rate in pps^2. Range: (0..+inf). */
Davidroid 22:ce8a202d67ac 59 1600, /* Maximum speed in pps. Range: (30..10000]. */
Davidroid 22:ce8a202d67ac 60 800, /* Minimum speed in pps. Range: [30..10000). */
Davidroid 20:bec1eba352a5 61 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 20:bec1eba352a5 62 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 20:bec1eba352a5 63 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 20:bec1eba352a5 64 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
Davidroid 20:bec1eba352a5 65 L6474_STEP_SEL_1_16, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 20:bec1eba352a5 66 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 20:bec1eba352a5 67 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 20:bec1eba352a5 68 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 20:bec1eba352a5 69 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 20:bec1eba352a5 70 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 20:bec1eba352a5 71 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 20:bec1eba352a5 72 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 20:bec1eba352a5 73 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 20:bec1eba352a5 74 L6474_ALARM_EN_OVERCURRENT |
Davidroid 20:bec1eba352a5 75 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 20:bec1eba352a5 76 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 20:bec1eba352a5 77 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 20:bec1eba352a5 78 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 20:bec1eba352a5 79 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 20:bec1eba352a5 80 };
Davidroid 20:bec1eba352a5 81
Davidroid 0:fedf3cf2324a 82 /* Motor Control Component. */
Davidroid 1:5171df1bf684 83 L6474 *motor;
Davidroid 0:fedf3cf2324a 84
Davidroid 0:fedf3cf2324a 85
Davidroid 5:dfec8da854e5 86 /* Functions -----------------------------------------------------------------*/
Davidroid 5:dfec8da854e5 87
Davidroid 5:dfec8da854e5 88 /**
Davidroid 5:dfec8da854e5 89 * @brief This is an example of user handler for the flag interrupt.
Davidroid 25:205b3d09e64e 90 * Empty parts can be implemented by the user upon needs.
Davidroid 25:205b3d09e64e 91 * @param None.
Davidroid 25:205b3d09e64e 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 26:7a58cede28f7 105 unsigned int status = motor->get_status();
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 26:7a58cede28f7 174 if (motor->init(&init) != COMPONENT_OK) {
Davidroid 12:fac478c7ea0b 175 exit(EXIT_FAILURE);
Davidroid 25:205b3d09e64e 176 }
Davidroid 25:205b3d09e64e 177
Davidroid 25:205b3d09e64e 178 /* Attaching and enabling the user handler for the flag interrupt. */
Davidroid 26:7a58cede28f7 179 motor->attach_flag_irq(&FlagIRQHandler);
Davidroid 26:7a58cede28f7 180 motor->enable_flag_irq();
Davidroid 0:fedf3cf2324a 181
Davidroid 0:fedf3cf2324a 182 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 183 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:fedf3cf2324a 184
Davidroid 6:ff72e3344fb1 185
Davidroid 6:ff72e3344fb1 186 /*----- Moving forward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 187
Davidroid 6:ff72e3344fb1 188 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 189 printf("--> Moving forward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 190
Davidroid 6:ff72e3344fb1 191 /* Moving 16000 steps in the forward direction. */
Davidroid 26:7a58cede28f7 192 motor->move(StepperMotor::FWD, 16000);
Davidroid 6:ff72e3344fb1 193
Davidroid 6:ff72e3344fb1 194 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 195 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 196
Davidroid 6:ff72e3344fb1 197 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 198 wait_ms(2000);
Davidroid 6:ff72e3344fb1 199
Davidroid 6:ff72e3344fb1 200
Davidroid 6:ff72e3344fb1 201 /*----- Moving backward 16000 steps. -----*/
Davidroid 6:ff72e3344fb1 202
Davidroid 6:ff72e3344fb1 203 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 204 printf("--> Moving backward 16000 steps.\r\n");
Davidroid 6:ff72e3344fb1 205
Davidroid 6:ff72e3344fb1 206 /* Moving 16000 steps in the backward direction. */
Davidroid 26:7a58cede28f7 207 motor->move(StepperMotor::BWD, 16000);
Davidroid 6:ff72e3344fb1 208
Davidroid 6:ff72e3344fb1 209 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 210 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 211
Davidroid 6:ff72e3344fb1 212 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 213 printf("--> Setting Home.\r\n");
Davidroid 6:ff72e3344fb1 214
Davidroid 6:ff72e3344fb1 215 /* Setting the current position to be the home position. */
Davidroid 26:7a58cede28f7 216 motor->set_home();
Davidroid 6:ff72e3344fb1 217
Davidroid 6:ff72e3344fb1 218 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 219 wait_ms(2000);
Davidroid 6:ff72e3344fb1 220
Davidroid 6:ff72e3344fb1 221
Davidroid 6:ff72e3344fb1 222 /*----- Going to a specified position. -----*/
Davidroid 6:ff72e3344fb1 223
Davidroid 6:ff72e3344fb1 224 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 225 printf("--> Going to position -6400.\r\n");
Davidroid 6:ff72e3344fb1 226
Davidroid 6:ff72e3344fb1 227 /* Requesting to go to a specified position. */
Davidroid 26:7a58cede28f7 228 motor->go_to(-6400);
Davidroid 6:ff72e3344fb1 229
Davidroid 6:ff72e3344fb1 230 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 231 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 232
Davidroid 6:ff72e3344fb1 233 /* Getting current position. */
Davidroid 26:7a58cede28f7 234 int position = motor->get_position();
Davidroid 6:ff72e3344fb1 235
Davidroid 6:ff72e3344fb1 236 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 237 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 238
Davidroid 6:ff72e3344fb1 239 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 240 printf("--> Setting a mark.\r\n");
Davidroid 6:ff72e3344fb1 241
Davidroid 6:ff72e3344fb1 242 /* Setting the current position to be the mark position. */
Davidroid 26:7a58cede28f7 243 motor->set_mark();
Davidroid 6:ff72e3344fb1 244
Davidroid 6:ff72e3344fb1 245 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 246 wait_ms(2000);
Davidroid 6:ff72e3344fb1 247
Davidroid 6:ff72e3344fb1 248
Davidroid 6:ff72e3344fb1 249 /*----- Going Home. -----*/
Davidroid 6:ff72e3344fb1 250
Davidroid 6:ff72e3344fb1 251 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 252 printf("--> Going Home.\r\n");
Davidroid 6:ff72e3344fb1 253
Davidroid 6:ff72e3344fb1 254 /* Requesting to go to home */
Davidroid 26:7a58cede28f7 255 motor->go_home();
Davidroid 6:ff72e3344fb1 256
Davidroid 6:ff72e3344fb1 257 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 258 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 259
Davidroid 6:ff72e3344fb1 260 /* Getting current position. */
Davidroid 26:7a58cede28f7 261 position = motor->get_position();
Davidroid 6:ff72e3344fb1 262
Davidroid 6:ff72e3344fb1 263 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 264 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 265
Davidroid 6:ff72e3344fb1 266 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 267 wait_ms(2000);
Davidroid 6:ff72e3344fb1 268
Davidroid 6:ff72e3344fb1 269
Davidroid 6:ff72e3344fb1 270 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 271
Davidroid 6:ff72e3344fb1 272 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 273 printf("--> Going to position 6400.\r\n");
Davidroid 6:ff72e3344fb1 274
Davidroid 6:ff72e3344fb1 275 /* Requesting to go to a specified position. */
Davidroid 26:7a58cede28f7 276 motor->go_to(6400);
Davidroid 6:ff72e3344fb1 277
Davidroid 6:ff72e3344fb1 278 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 279 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 280
Davidroid 6:ff72e3344fb1 281 /* Getting current position. */
Davidroid 26:7a58cede28f7 282 position = motor->get_position();
Davidroid 6:ff72e3344fb1 283
Davidroid 6:ff72e3344fb1 284 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 285 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 286
Davidroid 6:ff72e3344fb1 287 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 288 wait_ms(2000);
Davidroid 6:ff72e3344fb1 289
Davidroid 6:ff72e3344fb1 290
Davidroid 6:ff72e3344fb1 291 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 6:ff72e3344fb1 292
Davidroid 6:ff72e3344fb1 293 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 294 printf("--> Going to the mark set previously.\r\n");
Davidroid 6:ff72e3344fb1 295
Davidroid 6:ff72e3344fb1 296 /* Requesting to go to mark position. */
Davidroid 26:7a58cede28f7 297 motor->go_mark();
Davidroid 6:ff72e3344fb1 298
Davidroid 6:ff72e3344fb1 299 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 300 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 301
Davidroid 6:ff72e3344fb1 302 /* Getting current position. */
Davidroid 26:7a58cede28f7 303 position = motor->get_position();
Davidroid 6:ff72e3344fb1 304
Davidroid 6:ff72e3344fb1 305 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 306 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 307
Davidroid 6:ff72e3344fb1 308 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 309 wait_ms(2000);
Davidroid 6:ff72e3344fb1 310
Davidroid 6:ff72e3344fb1 311
Davidroid 6:ff72e3344fb1 312 /*----- Running backward. -----*/
Davidroid 6:ff72e3344fb1 313
Davidroid 6:ff72e3344fb1 314 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 315 printf("--> Running backward.\r\n");
Davidroid 6:ff72e3344fb1 316
Davidroid 6:ff72e3344fb1 317 /* Requesting to run backward. */
Davidroid 26:7a58cede28f7 318 motor->run(StepperMotor::BWD);
Davidroid 6:ff72e3344fb1 319
Davidroid 6:ff72e3344fb1 320 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 321 wait_ms(5000);
Davidroid 6:ff72e3344fb1 322
Davidroid 6:ff72e3344fb1 323 /* Getting current speed. */
Davidroid 26:7a58cede28f7 324 int speed = motor->get_speed();
Davidroid 6:ff72e3344fb1 325
Davidroid 6:ff72e3344fb1 326 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 327 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 328
Davidroid 6:ff72e3344fb1 329
Davidroid 6:ff72e3344fb1 330 /*----- Increasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 331
Davidroid 6:ff72e3344fb1 332 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 333 printf("--> Increasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 334
Davidroid 22:ce8a202d67ac 335 /* Increasing the speed. */
Davidroid 26:7a58cede28f7 336 motor->set_max_speed(2400);
Davidroid 6:ff72e3344fb1 337
Davidroid 6:ff72e3344fb1 338 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 339 wait_ms(5000);
Davidroid 6:ff72e3344fb1 340
Davidroid 6:ff72e3344fb1 341 /* Getting current speed. */
Davidroid 26:7a58cede28f7 342 speed = motor->get_speed();
Davidroid 6:ff72e3344fb1 343
Davidroid 6:ff72e3344fb1 344 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 345 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 346
Davidroid 6:ff72e3344fb1 347
Davidroid 6:ff72e3344fb1 348 /*----- Decreasing the speed while running. -----*/
Davidroid 6:ff72e3344fb1 349
Davidroid 6:ff72e3344fb1 350 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 351 printf("--> Decreasing the speed while running.\r\n");
Davidroid 6:ff72e3344fb1 352
Davidroid 22:ce8a202d67ac 353 /* Decreasing the speed. */
Davidroid 26:7a58cede28f7 354 motor->set_max_speed(1200);
Davidroid 6:ff72e3344fb1 355
Davidroid 6:ff72e3344fb1 356 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 357 wait_ms(5000);
Davidroid 6:ff72e3344fb1 358
Davidroid 6:ff72e3344fb1 359 /* Getting current speed. */
Davidroid 26:7a58cede28f7 360 speed = motor->get_speed();
Davidroid 6:ff72e3344fb1 361
Davidroid 6:ff72e3344fb1 362 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 363 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 364
Davidroid 0:fedf3cf2324a 365
Davidroid 6:ff72e3344fb1 366 /*----- Increasing acceleration while running. -----*/
Davidroid 6:ff72e3344fb1 367
Davidroid 6:ff72e3344fb1 368 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 369 printf("--> Increasing acceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 370
Davidroid 22:ce8a202d67ac 371 /* Increasing the acceleration. */
Davidroid 26:7a58cede28f7 372 motor->set_acceleration(480);
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 22:ce8a202d67ac 377 /* Increasing the speed. */
Davidroid 26:7a58cede28f7 378 motor->set_max_speed(2400);
Davidroid 6:ff72e3344fb1 379
Davidroid 6:ff72e3344fb1 380 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 381 wait_ms(5000);
Davidroid 6:ff72e3344fb1 382
Davidroid 6:ff72e3344fb1 383 /* Getting current speed. */
Davidroid 26:7a58cede28f7 384 speed = motor->get_speed();
Davidroid 6:ff72e3344fb1 385
Davidroid 6:ff72e3344fb1 386 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 387 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 388
Davidroid 6:ff72e3344fb1 389
Davidroid 6:ff72e3344fb1 390 /*----- Increasing deceleration while running. -----*/
Davidroid 6:ff72e3344fb1 391
Davidroid 6:ff72e3344fb1 392 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 393 printf("--> Increasing deceleration while running.\r\n");
Davidroid 6:ff72e3344fb1 394
Davidroid 22:ce8a202d67ac 395 /* Increasing the deceleration. */
Davidroid 26:7a58cede28f7 396 motor->set_deceleration(480);
Davidroid 6:ff72e3344fb1 397
Davidroid 6:ff72e3344fb1 398 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 399 wait_ms(5000);
Davidroid 6:ff72e3344fb1 400
Davidroid 22:ce8a202d67ac 401 /* Decreasing the speed. */
Davidroid 26:7a58cede28f7 402 motor->set_max_speed(1200);
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 /* Getting current speed. */
Davidroid 26:7a58cede28f7 408 speed = motor->get_speed();
Davidroid 0:fedf3cf2324a 409
Davidroid 6:ff72e3344fb1 410 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 411 printf(" Speed: %d.\r\n", speed);
Davidroid 6:ff72e3344fb1 412
Davidroid 6:ff72e3344fb1 413
Davidroid 6:ff72e3344fb1 414 /*----- Requesting soft-stop while running. -----*/
Davidroid 6:ff72e3344fb1 415
Davidroid 6:ff72e3344fb1 416 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 417 printf("--> Requesting soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 418
Davidroid 6:ff72e3344fb1 419 /* Requesting soft stop. */
Davidroid 26:7a58cede28f7 420 motor->soft_stop();
Davidroid 6:ff72e3344fb1 421
Davidroid 6:ff72e3344fb1 422 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 423 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 424
Davidroid 6:ff72e3344fb1 425 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 426 wait_ms(2000);
Davidroid 6:ff72e3344fb1 427
Davidroid 6:ff72e3344fb1 428
Davidroid 6:ff72e3344fb1 429 /*----- Requesting hard-stop while running. -----*/
Davidroid 6:ff72e3344fb1 430
Davidroid 6:ff72e3344fb1 431 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 432 printf("--> Running forward.\r\n");
Davidroid 6:ff72e3344fb1 433
Davidroid 6:ff72e3344fb1 434 /* Requesting to run in forward direction. */
Davidroid 26:7a58cede28f7 435 motor->run(StepperMotor::FWD);
Davidroid 6:ff72e3344fb1 436
Davidroid 6:ff72e3344fb1 437 /* Waiting until delay has expired. */
Davidroid 6:ff72e3344fb1 438 wait_ms(5000);
Davidroid 6:ff72e3344fb1 439
Davidroid 6:ff72e3344fb1 440 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 441 printf("--> Requesting hard-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 442
Davidroid 6:ff72e3344fb1 443 /* Requesting to immediatly stop. */
Davidroid 26:7a58cede28f7 444 motor->hard_stop();
Davidroid 6:ff72e3344fb1 445
Davidroid 6:ff72e3344fb1 446 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 447 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 448
Davidroid 6:ff72e3344fb1 449 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 450 wait_ms(2000);
Davidroid 6:ff72e3344fb1 451
Davidroid 6:ff72e3344fb1 452
Davidroid 6:ff72e3344fb1 453 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 454
Davidroid 6:ff72e3344fb1 455 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 456 printf("--> Going to position 20000.\r\n");
Davidroid 6:ff72e3344fb1 457
Davidroid 6:ff72e3344fb1 458 /* Requesting to go to a specified position. */
Davidroid 26:7a58cede28f7 459 motor->go_to(20000);
Davidroid 6:ff72e3344fb1 460
Davidroid 6:ff72e3344fb1 461 /* Waiting while the motor is active. */
Davidroid 6:ff72e3344fb1 462 wait_ms(5000);
Davidroid 6:ff72e3344fb1 463
Davidroid 6:ff72e3344fb1 464 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 465 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 6:ff72e3344fb1 466
Davidroid 6:ff72e3344fb1 467 /* Requesting to perform a soft stop */
Davidroid 26:7a58cede28f7 468 motor->soft_stop();
Davidroid 6:ff72e3344fb1 469
Davidroid 6:ff72e3344fb1 470 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 471 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 472
Davidroid 6:ff72e3344fb1 473 /* Waiting 2 seconds. */
Davidroid 20:bec1eba352a5 474 wait_ms(2000);
Davidroid 6:ff72e3344fb1 475
Davidroid 6:ff72e3344fb1 476
Davidroid 6:ff72e3344fb1 477 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 6:ff72e3344fb1 478
Davidroid 6:ff72e3344fb1 479 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 480 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 6:ff72e3344fb1 481
Davidroid 6:ff72e3344fb1 482 /*
Davidroid 6:ff72e3344fb1 483 * Trying to read an inexistent register.
Davidroid 6:ff72e3344fb1 484 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 6:ff72e3344fb1 485 * function called.
Davidroid 6:ff72e3344fb1 486 */
Davidroid 26:7a58cede28f7 487 motor->get_parameter(0x1F);
Davidroid 6:ff72e3344fb1 488
Davidroid 6:ff72e3344fb1 489 /* Waiting 0.5 seconds. */
Davidroid 6:ff72e3344fb1 490 wait_ms(500);
Davidroid 6:ff72e3344fb1 491
Davidroid 6:ff72e3344fb1 492
Davidroid 6:ff72e3344fb1 493 /*----- Changing step mode to full step mode. -----*/
Davidroid 6:ff72e3344fb1 494
Davidroid 6:ff72e3344fb1 495 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 496 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 6:ff72e3344fb1 497
Davidroid 6:ff72e3344fb1 498 /* Selecting full step mode. */
Davidroid 26:7a58cede28f7 499 if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_FULL)) {
Davidroid 20:bec1eba352a5 500 printf(" Step Mode not allowed.\r\n");
Davidroid 25:205b3d09e64e 501 }
Davidroid 6:ff72e3344fb1 502
Davidroid 6:ff72e3344fb1 503 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 26:7a58cede28f7 504 motor->set_max_speed(100);
Davidroid 26:7a58cede28f7 505 motor->set_min_speed(50);
Davidroid 26:7a58cede28f7 506 motor->set_acceleration(10);
Davidroid 26:7a58cede28f7 507 motor->set_deceleration(10);
Davidroid 6:ff72e3344fb1 508
Davidroid 6:ff72e3344fb1 509 /* Requesting to go to a specified position. */
Davidroid 26:7a58cede28f7 510 motor->go_to(200);
Davidroid 6:ff72e3344fb1 511
Davidroid 6:ff72e3344fb1 512 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 513 motor->wait_while_active();
Davidroid 6:ff72e3344fb1 514
Davidroid 6:ff72e3344fb1 515 /* Getting current position */
Davidroid 26:7a58cede28f7 516 position = motor->get_position();
Davidroid 6:ff72e3344fb1 517
Davidroid 6:ff72e3344fb1 518 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 519 printf(" Position: %d.\r\n", position);
Davidroid 6:ff72e3344fb1 520
Davidroid 6:ff72e3344fb1 521 /* Waiting 2 seconds. */
Davidroid 6:ff72e3344fb1 522 wait_ms(2000);
Davidroid 6:ff72e3344fb1 523
Davidroid 6:ff72e3344fb1 524
Davidroid 6:ff72e3344fb1 525 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 6:ff72e3344fb1 526
Davidroid 6:ff72e3344fb1 527 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 528 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 6:ff72e3344fb1 529
Davidroid 6:ff72e3344fb1 530 /* Resetting to 1/16 microstepping mode */
Davidroid 26:7a58cede28f7 531 if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_1_16)) {
Davidroid 20:bec1eba352a5 532 printf(" Step Mode not allowed.\r\n");
Davidroid 25:205b3d09e64e 533 }
Davidroid 6:ff72e3344fb1 534
Davidroid 6:ff72e3344fb1 535 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 26:7a58cede28f7 536 motor->set_max_speed(1600);
Davidroid 26:7a58cede28f7 537 motor->set_min_speed(800);
Davidroid 26:7a58cede28f7 538 motor->set_acceleration(160);
Davidroid 26:7a58cede28f7 539 motor->set_deceleration(160);
Davidroid 6:ff72e3344fb1 540
Davidroid 6:ff72e3344fb1 541
Davidroid 6:ff72e3344fb1 542 /*----- Infinite Loop. -----*/
Davidroid 6:ff72e3344fb1 543
Davidroid 6:ff72e3344fb1 544 /* Printing to the console. */
Davidroid 6:ff72e3344fb1 545 printf("--> Infinite Loop...\r\n");
Davidroid 6:ff72e3344fb1 546
Davidroid 6:ff72e3344fb1 547 /* Infinite Loop. */
Davidroid 25:205b3d09e64e 548 while (true) {
Davidroid 6:ff72e3344fb1 549 /* Requesting to go to a specified position. */
Davidroid 26:7a58cede28f7 550 motor->go_to(-6400);
Davidroid 1:5171df1bf684 551
Davidroid 0:fedf3cf2324a 552 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 553 motor->wait_while_active();
Davidroid 0:fedf3cf2324a 554
Davidroid 0:fedf3cf2324a 555 /* Requesting to go to a specified position. */
Davidroid 26:7a58cede28f7 556 motor->go_to(6400);
Davidroid 0:fedf3cf2324a 557
Davidroid 0:fedf3cf2324a 558 /* Waiting while the motor is active. */
Davidroid 26:7a58cede28f7 559 motor->wait_while_active();
Davidroid 0:fedf3cf2324a 560 }
Davidroid 0:fedf3cf2324a 561 }