Simple test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board, built against mbed OS.

Dependencies:   X_NUCLEO_IHM01A1

Fork of HelloWorld_IHM01A1 by ST

This application provides a simple 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, moving the rotor to a specific position, with a given speed value, direction of rotation, etc.

Committer:
Davidroid
Date:
Mon Jul 03 11:54:28 2017 +0000
Revision:
38:ca71d404823c
Parent:
35:2b44ed4ec7a0
mbed OS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /**
Davidroid 0:e6a49a092e2a 2 ******************************************************************************
Davidroid 0:e6a49a092e2a 3 * @file main.cpp
Davidroid 21:0189493b15ec 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 21:0189493b15ec 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
Davidroid 0:e6a49a092e2a 8 * Motor Control Expansion Board: control of 1 motor.
Davidroid 0:e6a49a092e2a 9 ******************************************************************************
Davidroid 0:e6a49a092e2a 10 * @attention
Davidroid 0:e6a49a092e2a 11 *
Davidroid 0:e6a49a092e2a 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:e6a49a092e2a 13 *
Davidroid 0:e6a49a092e2a 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:e6a49a092e2a 15 * are permitted provided that the following conditions are met:
Davidroid 0:e6a49a092e2a 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:e6a49a092e2a 17 * this list of conditions and the following disclaimer.
Davidroid 0:e6a49a092e2a 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:e6a49a092e2a 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:e6a49a092e2a 20 * and/or other materials provided with the distribution.
Davidroid 0:e6a49a092e2a 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:e6a49a092e2a 22 * may be used to endorse or promote products derived from this software
Davidroid 0:e6a49a092e2a 23 * without specific prior written permission.
Davidroid 0:e6a49a092e2a 24 *
Davidroid 0:e6a49a092e2a 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:e6a49a092e2a 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:e6a49a092e2a 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:e6a49a092e2a 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:e6a49a092e2a 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:e6a49a092e2a 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:e6a49a092e2a 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:e6a49a092e2a 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:e6a49a092e2a 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:e6a49a092e2a 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:e6a49a092e2a 35 *
Davidroid 0:e6a49a092e2a 36 ******************************************************************************
Davidroid 0:e6a49a092e2a 37 */
Davidroid 0:e6a49a092e2a 38
Davidroid 0:e6a49a092e2a 39
Davidroid 0:e6a49a092e2a 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 41
Davidroid 0:e6a49a092e2a 42 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 43 #include "mbed.h"
Davidroid 38:ca71d404823c 44 #include "rtos.h"
Davidroid 0:e6a49a092e2a 45
Davidroid 0:e6a49a092e2a 46 /* Helper header files. */
Davidroid 0:e6a49a092e2a 47 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 48
Davidroid 0:e6a49a092e2a 49 /* Component specific header files. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 50 #include "L6474.h"
Davidroid 0:e6a49a092e2a 51
Davidroid 0:e6a49a092e2a 52
Davidroid 0:e6a49a092e2a 53 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 54
Davidroid 26:b0203c2265e5 55 /* Number of steps. */
Davidroid 26:b0203c2265e5 56 #define STEPS_1 (400 * 8) /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */
Davidroid 26:b0203c2265e5 57
Davidroid 26:b0203c2265e5 58 /* Delay in milliseconds. */
Davidroid 26:b0203c2265e5 59 #define DELAY_1 1000
Davidroid 26:b0203c2265e5 60 #define DELAY_2 2000
Davidroid 26:b0203c2265e5 61 #define DELAY_3 6000
Davidroid 26:b0203c2265e5 62 #define DELAY_4 8000
Davidroid 26:b0203c2265e5 63
Davidroid 29:a80a213c3c94 64 /* Speed in pps (Pulses Per Second).
Davidroid 29:a80a213c3c94 65 In Full Step mode: 1 pps = 1 step/s).
Davidroid 29:a80a213c3c94 66 In 1/N Step Mode: N pps = 1 step/s). */
Davidroid 26:b0203c2265e5 67 #define SPEED_1 2400
Davidroid 26:b0203c2265e5 68 #define SPEED_2 1200
Davidroid 0:e6a49a092e2a 69
Davidroid 0:e6a49a092e2a 70
Davidroid 0:e6a49a092e2a 71 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 72
Davidroid 19:1cd7f65c155c 73 /* Initialization parameters. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 74 L6474_init_t init = {
Davidroid 29:a80a213c3c94 75 160, /* Acceleration rate in pps^2. Range: (0..+inf). */
Davidroid 29:a80a213c3c94 76 160, /* Deceleration rate in pps^2. Range: (0..+inf). */
Davidroid 29:a80a213c3c94 77 1600, /* Maximum speed in pps. Range: (30..10000]. */
Davidroid 29:a80a213c3c94 78 800, /* Minimum speed in pps. Range: [30..10000). */
Davidroid 19:1cd7f65c155c 79 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 19:1cd7f65c155c 80 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 19:1cd7f65c155c 81 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 19:1cd7f65c155c 82 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
Davidroid 26:b0203c2265e5 83 L6474_STEP_SEL_1_8, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 84 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 85 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 86 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 87 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 88 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 89 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 19:1cd7f65c155c 90 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 19:1cd7f65c155c 91 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 19:1cd7f65c155c 92 L6474_ALARM_EN_OVERCURRENT |
Davidroid 19:1cd7f65c155c 93 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 19:1cd7f65c155c 94 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 19:1cd7f65c155c 95 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 19:1cd7f65c155c 96 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 19:1cd7f65c155c 97 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 19:1cd7f65c155c 98 };
Davidroid 19:1cd7f65c155c 99
Davidroid 0:e6a49a092e2a 100 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 101 L6474 *motor;
Davidroid 0:e6a49a092e2a 102
Davidroid 0:e6a49a092e2a 103
Davidroid 19:1cd7f65c155c 104 /* Functions -----------------------------------------------------------------*/
Davidroid 19:1cd7f65c155c 105
Davidroid 19:1cd7f65c155c 106 /**
Davidroid 25:3c863b420ac5 107 * @brief This is an example of user handler for the flag interrupt.
Davidroid 25:3c863b420ac5 108 * @param None
Davidroid 25:3c863b420ac5 109 * @retval None
Davidroid 25:3c863b420ac5 110 * @note If needed, implement it, and then attach and enable it:
davide.aliprandi@st.com 35:2b44ed4ec7a0 111 * + motor->attach_flag_irq(&flag_irq_handler);
davide.aliprandi@st.com 35:2b44ed4ec7a0 112 * + motor->enable_flag_irq();
Davidroid 25:3c863b420ac5 113 * To disable it:
davide.aliprandi@st.com 35:2b44ed4ec7a0 114 * + motor->disble_flag_irq();
Davidroid 25:3c863b420ac5 115 */
davide.aliprandi@st.com 35:2b44ed4ec7a0 116 void flag_irq_handler(void)
Davidroid 19:1cd7f65c155c 117 {
Davidroid 19:1cd7f65c155c 118 /* Set ISR flag. */
Davidroid 19:1cd7f65c155c 119 motor->isr_flag = TRUE;
Davidroid 19:1cd7f65c155c 120
Davidroid 19:1cd7f65c155c 121 /* Get the value of the status register. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 122 unsigned int status = motor->get_status();
Davidroid 34:543d0d1147d9 123
Davidroid 19:1cd7f65c155c 124 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 19:1cd7f65c155c 125 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
Davidroid 34:543d0d1147d9 126 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
Davidroid 19:1cd7f65c155c 127 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
Davidroid 34:543d0d1147d9 128 }
Davidroid 19:1cd7f65c155c 129
Davidroid 19:1cd7f65c155c 130 /* Reset ISR flag. */
Davidroid 19:1cd7f65c155c 131 motor->isr_flag = FALSE;
Davidroid 19:1cd7f65c155c 132 }
Davidroid 19:1cd7f65c155c 133
Davidroid 19:1cd7f65c155c 134
Davidroid 0:e6a49a092e2a 135 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 136
Davidroid 0:e6a49a092e2a 137 int main()
Davidroid 0:e6a49a092e2a 138 {
Davidroid 6:32166bfc04b0 139 /*----- Initialization. -----*/
Davidroid 6:32166bfc04b0 140
Davidroid 0:e6a49a092e2a 141 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 142 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 143
Davidroid 11:3e303a25770d 144 /* Initializing Motor Control Component. */
Davidroid 5:8065b587ade0 145 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
davide.aliprandi@st.com 35:2b44ed4ec7a0 146 if (motor->init(&init) != COMPONENT_OK) {
Davidroid 17:4830b25fec7f 147 exit(EXIT_FAILURE);
Davidroid 34:543d0d1147d9 148 }
Davidroid 0:e6a49a092e2a 149
Davidroid 19:1cd7f65c155c 150 /* Attaching and enabling interrupt handlers. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 151 motor->attach_flag_irq(&flag_irq_handler);
davide.aliprandi@st.com 35:2b44ed4ec7a0 152 motor->enable_flag_irq();
Davidroid 19:1cd7f65c155c 153
Davidroid 0:e6a49a092e2a 154 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 155 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:e6a49a092e2a 156
Davidroid 6:32166bfc04b0 157
Davidroid 6:32166bfc04b0 158 /*----- Moving. -----*/
Davidroid 6:32166bfc04b0 159
Davidroid 6:32166bfc04b0 160 /* Printing to the console. */
Davidroid 26:b0203c2265e5 161 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 6:32166bfc04b0 162
Davidroid 6:32166bfc04b0 163 /* Moving N steps in the forward direction. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 164 motor->move(StepperMotor::FWD, STEPS_1);
Davidroid 6:32166bfc04b0 165
Davidroid 6:32166bfc04b0 166 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 167 motor->wait_while_active();
Davidroid 6:32166bfc04b0 168
Davidroid 6:32166bfc04b0 169 /* Getting current position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 170 int position = motor->get_position();
Davidroid 6:32166bfc04b0 171
Davidroid 6:32166bfc04b0 172 /* Printing to the console. */
Davidroid 6:32166bfc04b0 173 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 174
Davidroid 26:b0203c2265e5 175 /* Waiting. */
Davidroid 26:b0203c2265e5 176 wait_ms(DELAY_1);
Davidroid 6:32166bfc04b0 177
Davidroid 19:1cd7f65c155c 178
Davidroid 26:b0203c2265e5 179 /*----- Changing the motor setting. -----*/
Davidroid 19:1cd7f65c155c 180
Davidroid 19:1cd7f65c155c 181 /* Printing to the console. */
Davidroid 27:e09aa231c16d 182 printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
Davidroid 27:e09aa231c16d 183
Davidroid 27:e09aa231c16d 184 /* Increasing the torque regulation current to 500[mA]. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 185 motor->set_parameter(L6474_TVAL, 500);
Davidroid 27:e09aa231c16d 186
Davidroid 27:e09aa231c16d 187 /* Printing to the console. */
Davidroid 26:b0203c2265e5 188 printf("--> Doubling the microsteps.\r\n");
Davidroid 19:1cd7f65c155c 189
Davidroid 26:b0203c2265e5 190 /* Doubling the microsteps. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 191 if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_1_16)) {
Davidroid 26:b0203c2265e5 192 printf(" Step Mode not allowed.\r\n");
Davidroid 34:543d0d1147d9 193 }
Davidroid 19:1cd7f65c155c 194
Davidroid 26:b0203c2265e5 195 /* Waiting. */
Davidroid 26:b0203c2265e5 196 wait_ms(DELAY_1);
Davidroid 19:1cd7f65c155c 197
Davidroid 27:e09aa231c16d 198 /* Printing to the console. */
Davidroid 27:e09aa231c16d 199 printf("--> Setting Home.\r\n");
Davidroid 27:e09aa231c16d 200
Davidroid 27:e09aa231c16d 201 /* Setting the current position to be the home position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 202 motor->set_home();
Davidroid 27:e09aa231c16d 203
Davidroid 27:e09aa231c16d 204 /* Getting current position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 205 position = motor->get_position();
Davidroid 27:e09aa231c16d 206
Davidroid 27:e09aa231c16d 207 /* Printing to the console. */
Davidroid 27:e09aa231c16d 208 printf(" Position: %d.\r\n", position);
Davidroid 27:e09aa231c16d 209
Davidroid 27:e09aa231c16d 210 /* Waiting. */
Davidroid 27:e09aa231c16d 211 wait_ms(DELAY_2);
Davidroid 27:e09aa231c16d 212
Davidroid 19:1cd7f65c155c 213
Davidroid 6:32166bfc04b0 214 /*----- Moving. -----*/
Davidroid 6:32166bfc04b0 215
Davidroid 6:32166bfc04b0 216 /* Printing to the console. */
Davidroid 26:b0203c2265e5 217 printf("--> Moving backward %d steps.\r\n", STEPS_1);
Davidroid 6:32166bfc04b0 218
Davidroid 6:32166bfc04b0 219 /* Moving N steps in the backward direction. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 220 motor->move(StepperMotor::BWD, STEPS_1);
Davidroid 6:32166bfc04b0 221
Davidroid 6:32166bfc04b0 222 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 223 motor->wait_while_active();
Davidroid 6:32166bfc04b0 224
Davidroid 6:32166bfc04b0 225 /* Getting current position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 226 position = motor->get_position();
Davidroid 6:32166bfc04b0 227
Davidroid 6:32166bfc04b0 228 /* Printing to the console. */
Davidroid 6:32166bfc04b0 229 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 230
Davidroid 26:b0203c2265e5 231 /* Waiting. */
Davidroid 26:b0203c2265e5 232 wait_ms(DELAY_1);
Davidroid 6:32166bfc04b0 233
Davidroid 6:32166bfc04b0 234
Davidroid 6:32166bfc04b0 235 /*----- Going to a specified position. -----*/
Davidroid 6:32166bfc04b0 236
Davidroid 6:32166bfc04b0 237 /* Printing to the console. */
Davidroid 26:b0203c2265e5 238 printf("--> Going to position %d.\r\n", STEPS_1);
Davidroid 6:32166bfc04b0 239
Davidroid 6:32166bfc04b0 240 /* Requesting to go to a specified position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 241 motor->go_to(STEPS_1);
Davidroid 6:32166bfc04b0 242
Davidroid 6:32166bfc04b0 243 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 244 motor->wait_while_active();
Davidroid 6:32166bfc04b0 245
Davidroid 6:32166bfc04b0 246 /* Getting current position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 247 position = motor->get_position();
Davidroid 6:32166bfc04b0 248
Davidroid 6:32166bfc04b0 249 /* Printing to the console. */
Davidroid 6:32166bfc04b0 250 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 251
Davidroid 26:b0203c2265e5 252 /* Waiting. */
Davidroid 26:b0203c2265e5 253 wait_ms(DELAY_2);
Davidroid 6:32166bfc04b0 254
Davidroid 6:32166bfc04b0 255
Davidroid 6:32166bfc04b0 256 /*----- Going Home. -----*/
Davidroid 6:32166bfc04b0 257
Davidroid 6:32166bfc04b0 258 /* Printing to the console. */
Davidroid 6:32166bfc04b0 259 printf("--> Going Home.\r\n");
Davidroid 6:32166bfc04b0 260
Davidroid 6:32166bfc04b0 261 /* Requesting to go to home. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 262 motor->go_home();
Davidroid 6:32166bfc04b0 263
Davidroid 6:32166bfc04b0 264 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 265 motor->wait_while_active();
Davidroid 6:32166bfc04b0 266
Davidroid 6:32166bfc04b0 267 /* Getting current position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 268 position = motor->get_position();
Davidroid 6:32166bfc04b0 269
Davidroid 6:32166bfc04b0 270 /* Printing to the console. */
Davidroid 6:32166bfc04b0 271 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 272
Davidroid 26:b0203c2265e5 273 /* Waiting. */
Davidroid 26:b0203c2265e5 274 wait_ms(DELAY_2);
Davidroid 6:32166bfc04b0 275
Davidroid 6:32166bfc04b0 276
Davidroid 6:32166bfc04b0 277 /*----- Running. -----*/
Davidroid 6:32166bfc04b0 278
Davidroid 6:32166bfc04b0 279 /* Printing to the console. */
Davidroid 26:b0203c2265e5 280 printf("--> Running backward for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 6:32166bfc04b0 281
Davidroid 6:32166bfc04b0 282 /* Requesting to run backward. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 283 motor->run(StepperMotor::BWD);
Davidroid 6:32166bfc04b0 284
Davidroid 26:b0203c2265e5 285 /* Waiting. */
Davidroid 26:b0203c2265e5 286 wait_ms(DELAY_3);
Davidroid 6:32166bfc04b0 287
Davidroid 6:32166bfc04b0 288 /* Getting current speed. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 289 int speed = motor->get_speed();
Davidroid 6:32166bfc04b0 290
Davidroid 6:32166bfc04b0 291 /* Printing to the console. */
Davidroid 6:32166bfc04b0 292 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 293
Davidroid 6:32166bfc04b0 294 /*----- Increasing the speed while running. -----*/
Davidroid 6:32166bfc04b0 295
Davidroid 6:32166bfc04b0 296 /* Printing to the console. */
Davidroid 26:b0203c2265e5 297 printf("--> Increasing the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 6:32166bfc04b0 298
Davidroid 26:b0203c2265e5 299 /* Increasing the speed. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 300 motor->set_max_speed(SPEED_1);
Davidroid 6:32166bfc04b0 301
Davidroid 26:b0203c2265e5 302 /* Waiting. */
Davidroid 26:b0203c2265e5 303 wait_ms(DELAY_3);
Davidroid 6:32166bfc04b0 304
Davidroid 6:32166bfc04b0 305 /* Getting current speed. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 306 speed = motor->get_speed();
Davidroid 6:32166bfc04b0 307
Davidroid 6:32166bfc04b0 308 /* Printing to the console. */
Davidroid 6:32166bfc04b0 309 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 310
Davidroid 6:32166bfc04b0 311
Davidroid 6:32166bfc04b0 312 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 313
Davidroid 6:32166bfc04b0 314 /* Printing to the console. */
Davidroid 26:b0203c2265e5 315 printf("--> Decreasing the speed while running again for %d seconds.\r\n", DELAY_4 / 1000);
Davidroid 6:32166bfc04b0 316
Davidroid 26:b0203c2265e5 317 /* Decreasing the speed. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 318 motor->set_max_speed(SPEED_2);
Davidroid 6:32166bfc04b0 319
Davidroid 26:b0203c2265e5 320 /* Waiting. */
Davidroid 26:b0203c2265e5 321 wait_ms(DELAY_4);
Davidroid 6:32166bfc04b0 322
Davidroid 6:32166bfc04b0 323 /* Getting current speed. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 324 speed = motor->get_speed();
Davidroid 6:32166bfc04b0 325
Davidroid 6:32166bfc04b0 326 /* Printing to the console. */
Davidroid 6:32166bfc04b0 327 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 328
Davidroid 6:32166bfc04b0 329
Davidroid 26:b0203c2265e5 330 /*----- Hard Stop. -----*/
Davidroid 6:32166bfc04b0 331
Davidroid 6:32166bfc04b0 332 /* Printing to the console. */
Davidroid 26:b0203c2265e5 333 printf("--> Hard Stop.\r\n");
Davidroid 6:32166bfc04b0 334
Davidroid 6:32166bfc04b0 335 /* Requesting to immediatly stop. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 336 motor->hard_stop();
Davidroid 6:32166bfc04b0 337
Davidroid 6:32166bfc04b0 338 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 339 motor->wait_while_active();
Davidroid 6:32166bfc04b0 340
Davidroid 26:b0203c2265e5 341 /* Waiting. */
Davidroid 26:b0203c2265e5 342 wait_ms(DELAY_2);
Davidroid 6:32166bfc04b0 343
Davidroid 6:32166bfc04b0 344
Davidroid 6:32166bfc04b0 345 /*----- Infinite Loop. -----*/
Davidroid 6:32166bfc04b0 346
Davidroid 6:32166bfc04b0 347 /* Printing to the console. */
Davidroid 6:32166bfc04b0 348 printf("--> Infinite Loop...\r\n");
Davidroid 6:32166bfc04b0 349
Davidroid 6:32166bfc04b0 350 /* Setting the current position to be the home position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 351 motor->set_home();
Davidroid 6:32166bfc04b0 352
Davidroid 6:32166bfc04b0 353 /* Infinite Loop. */
Davidroid 34:543d0d1147d9 354 while (true) {
Davidroid 6:32166bfc04b0 355 /* Requesting to go to a specified position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 356 motor->go_to(STEPS_1 >> 1);
Davidroid 3:fffa53c7aed2 357
Davidroid 0:e6a49a092e2a 358 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 359 motor->wait_while_active();
Davidroid 0:e6a49a092e2a 360
Davidroid 0:e6a49a092e2a 361 /* Requesting to go to a specified position. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 362 motor->go_to(- (STEPS_1 >> 1));
Davidroid 0:e6a49a092e2a 363
Davidroid 0:e6a49a092e2a 364 /* Waiting while the motor is active. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 365 motor->wait_while_active();
Davidroid 0:e6a49a092e2a 366 }
Davidroid 0:e6a49a092e2a 367 }