Codice per pilotare il motore IHM01A1

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
Davidroid
Date:
Fri Apr 08 12:48:30 2016 +0000
Revision:
27:e09aa231c16d
Parent:
26:b0203c2265e5
Child:
29:a80a213c3c94
+ Added a SetHome() after changing the step mode.; + Updated with the new version of the library.

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 0:e6a49a092e2a 44
Davidroid 0:e6a49a092e2a 45 /* Helper header files. */
Davidroid 0:e6a49a092e2a 46 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 47
Davidroid 0:e6a49a092e2a 48 /* Component specific header files. */
Davidroid 0:e6a49a092e2a 49 #include "l6474_class.h"
Davidroid 0:e6a49a092e2a 50
Davidroid 0:e6a49a092e2a 51
Davidroid 0:e6a49a092e2a 52 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 53
Davidroid 26:b0203c2265e5 54 /* Number of steps. */
Davidroid 26:b0203c2265e5 55 #define STEPS_1 (400 * 8) /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */
Davidroid 26:b0203c2265e5 56
Davidroid 26:b0203c2265e5 57 /* Delay in milliseconds. */
Davidroid 26:b0203c2265e5 58 #define DELAY_1 1000
Davidroid 26:b0203c2265e5 59 #define DELAY_2 2000
Davidroid 26:b0203c2265e5 60 #define DELAY_3 6000
Davidroid 26:b0203c2265e5 61 #define DELAY_4 8000
Davidroid 26:b0203c2265e5 62
Davidroid 26:b0203c2265e5 63 /* Speed in step/s. */
Davidroid 26:b0203c2265e5 64 #define SPEED_1 2400
Davidroid 26:b0203c2265e5 65 #define SPEED_2 1200
Davidroid 0:e6a49a092e2a 66
Davidroid 0:e6a49a092e2a 67
Davidroid 0:e6a49a092e2a 68 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 69
Davidroid 19:1cd7f65c155c 70 /* Initialization parameters. */
Davidroid 23:54503e363f67 71 L6474_Init_t init =
Davidroid 19:1cd7f65c155c 72 {
Davidroid 19:1cd7f65c155c 73 160, /* Acceleration rate in step/s2. Range: (0..+inf). */
Davidroid 19:1cd7f65c155c 74 160, /* Deceleration rate in step/s2. Range: (0..+inf). */
Davidroid 19:1cd7f65c155c 75 1600, /* Maximum speed in step/s. Range: (30..10000]. */
Davidroid 19:1cd7f65c155c 76 800, /* Minimum speed in step/s. Range: [30..10000). */
Davidroid 19:1cd7f65c155c 77 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 19:1cd7f65c155c 78 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 19:1cd7f65c155c 79 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 19:1cd7f65c155c 80 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
Davidroid 26:b0203c2265e5 81 L6474_STEP_SEL_1_8, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 82 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 83 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 84 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 85 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 86 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 87 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 19:1cd7f65c155c 88 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 19:1cd7f65c155c 89 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 19:1cd7f65c155c 90 L6474_ALARM_EN_OVERCURRENT |
Davidroid 19:1cd7f65c155c 91 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 19:1cd7f65c155c 92 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 19:1cd7f65c155c 93 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 19:1cd7f65c155c 94 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 19:1cd7f65c155c 95 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 19:1cd7f65c155c 96 };
Davidroid 19:1cd7f65c155c 97
Davidroid 0:e6a49a092e2a 98 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 99 L6474 *motor;
Davidroid 0:e6a49a092e2a 100
Davidroid 0:e6a49a092e2a 101
Davidroid 19:1cd7f65c155c 102 /* Functions -----------------------------------------------------------------*/
Davidroid 19:1cd7f65c155c 103
Davidroid 19:1cd7f65c155c 104 /**
Davidroid 25:3c863b420ac5 105 * @brief This is an example of user handler for the flag interrupt.
Davidroid 25:3c863b420ac5 106 * @param None
Davidroid 25:3c863b420ac5 107 * @retval None
Davidroid 25:3c863b420ac5 108 * @note If needed, implement it, and then attach and enable it:
Davidroid 25:3c863b420ac5 109 * + motor->AttachFlagIRQ(&FlagIRQHandler);
Davidroid 25:3c863b420ac5 110 * + motor->EnableFlagIRQ();
Davidroid 25:3c863b420ac5 111 * To disable it:
Davidroid 25:3c863b420ac5 112 * + motor->DisbleFlagIRQ();
Davidroid 25:3c863b420ac5 113 */
Davidroid 19:1cd7f65c155c 114 void FlagIRQHandler(void)
Davidroid 19:1cd7f65c155c 115 {
Davidroid 19:1cd7f65c155c 116 /* Set ISR flag. */
Davidroid 19:1cd7f65c155c 117 motor->isr_flag = TRUE;
Davidroid 19:1cd7f65c155c 118
Davidroid 19:1cd7f65c155c 119 /* Get the value of the status register. */
Davidroid 19:1cd7f65c155c 120 unsigned int status = motor->GetStatus();
Davidroid 19:1cd7f65c155c 121
Davidroid 19:1cd7f65c155c 122 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 19:1cd7f65c155c 123 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
Davidroid 19:1cd7f65c155c 124 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD)
Davidroid 19:1cd7f65c155c 125 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
Davidroid 19:1cd7f65c155c 126
Davidroid 19:1cd7f65c155c 127 /* Reset ISR flag. */
Davidroid 19:1cd7f65c155c 128 motor->isr_flag = FALSE;
Davidroid 19:1cd7f65c155c 129 }
Davidroid 19:1cd7f65c155c 130
Davidroid 19:1cd7f65c155c 131
Davidroid 0:e6a49a092e2a 132 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 133
Davidroid 0:e6a49a092e2a 134 int main()
Davidroid 0:e6a49a092e2a 135 {
Davidroid 6:32166bfc04b0 136 /*----- Initialization. -----*/
Davidroid 6:32166bfc04b0 137
Davidroid 0:e6a49a092e2a 138 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 139 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 140
Davidroid 11:3e303a25770d 141 /* Initializing Motor Control Component. */
Davidroid 5:8065b587ade0 142 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 19:1cd7f65c155c 143 if (motor->Init(&init) != COMPONENT_OK)
Davidroid 17:4830b25fec7f 144 exit(EXIT_FAILURE);
Davidroid 0:e6a49a092e2a 145
Davidroid 19:1cd7f65c155c 146 /* Attaching and enabling interrupt handlers. */
Davidroid 19:1cd7f65c155c 147 motor->AttachFlagIRQ(&FlagIRQHandler);
Davidroid 19:1cd7f65c155c 148 motor->EnableFlagIRQ();
Davidroid 19:1cd7f65c155c 149
Davidroid 0:e6a49a092e2a 150 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 151 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:e6a49a092e2a 152
Davidroid 6:32166bfc04b0 153
Davidroid 6:32166bfc04b0 154 /*----- Moving. -----*/
Davidroid 6:32166bfc04b0 155
Davidroid 6:32166bfc04b0 156 /* Printing to the console. */
Davidroid 26:b0203c2265e5 157 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 6:32166bfc04b0 158
Davidroid 6:32166bfc04b0 159 /* Moving N steps in the forward direction. */
Davidroid 26:b0203c2265e5 160 motor->Move(StepperMotor::FWD, STEPS_1);
Davidroid 6:32166bfc04b0 161
Davidroid 6:32166bfc04b0 162 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 163 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 164
Davidroid 6:32166bfc04b0 165 /* Getting current position. */
Davidroid 6:32166bfc04b0 166 int position = motor->GetPosition();
Davidroid 6:32166bfc04b0 167
Davidroid 6:32166bfc04b0 168 /* Printing to the console. */
Davidroid 6:32166bfc04b0 169 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 170
Davidroid 26:b0203c2265e5 171 /* Waiting. */
Davidroid 26:b0203c2265e5 172 wait_ms(DELAY_1);
Davidroid 6:32166bfc04b0 173
Davidroid 19:1cd7f65c155c 174
Davidroid 26:b0203c2265e5 175 /*----- Changing the motor setting. -----*/
Davidroid 19:1cd7f65c155c 176
Davidroid 19:1cd7f65c155c 177 /* Printing to the console. */
Davidroid 27:e09aa231c16d 178 printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
Davidroid 27:e09aa231c16d 179
Davidroid 27:e09aa231c16d 180 /* Increasing the torque regulation current to 500[mA]. */
Davidroid 27:e09aa231c16d 181 motor->SetParameter(L6474_TVAL, 500);
Davidroid 27:e09aa231c16d 182
Davidroid 27:e09aa231c16d 183 /* Printing to the console. */
Davidroid 26:b0203c2265e5 184 printf("--> Doubling the microsteps.\r\n");
Davidroid 19:1cd7f65c155c 185
Davidroid 26:b0203c2265e5 186 /* Doubling the microsteps. */
Davidroid 26:b0203c2265e5 187 if (!motor->SetStepMode((StepperMotor::step_mode_t) STEP_MODE_1_16))
Davidroid 26:b0203c2265e5 188 printf(" Step Mode not allowed.\r\n");
Davidroid 19:1cd7f65c155c 189
Davidroid 26:b0203c2265e5 190 /* Waiting. */
Davidroid 26:b0203c2265e5 191 wait_ms(DELAY_1);
Davidroid 19:1cd7f65c155c 192
Davidroid 27:e09aa231c16d 193 /* Printing to the console. */
Davidroid 27:e09aa231c16d 194 printf("--> Setting Home.\r\n");
Davidroid 27:e09aa231c16d 195
Davidroid 27:e09aa231c16d 196 /* Setting the current position to be the home position. */
Davidroid 27:e09aa231c16d 197 motor->SetHome();
Davidroid 27:e09aa231c16d 198
Davidroid 27:e09aa231c16d 199 /* Getting current position. */
Davidroid 27:e09aa231c16d 200 position = motor->GetPosition();
Davidroid 27:e09aa231c16d 201
Davidroid 27:e09aa231c16d 202 /* Printing to the console. */
Davidroid 27:e09aa231c16d 203 printf(" Position: %d.\r\n", position);
Davidroid 27:e09aa231c16d 204
Davidroid 27:e09aa231c16d 205 /* Waiting. */
Davidroid 27:e09aa231c16d 206 wait_ms(DELAY_2);
Davidroid 27:e09aa231c16d 207
Davidroid 19:1cd7f65c155c 208
Davidroid 6:32166bfc04b0 209 /*----- Moving. -----*/
Davidroid 6:32166bfc04b0 210
Davidroid 6:32166bfc04b0 211 /* Printing to the console. */
Davidroid 26:b0203c2265e5 212 printf("--> Moving backward %d steps.\r\n", STEPS_1);
Davidroid 6:32166bfc04b0 213
Davidroid 6:32166bfc04b0 214 /* Moving N steps in the backward direction. */
Davidroid 26:b0203c2265e5 215 motor->Move(StepperMotor::BWD, STEPS_1);
Davidroid 6:32166bfc04b0 216
Davidroid 6:32166bfc04b0 217 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 218 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 219
Davidroid 6:32166bfc04b0 220 /* Getting current position. */
Davidroid 6:32166bfc04b0 221 position = motor->GetPosition();
Davidroid 6:32166bfc04b0 222
Davidroid 6:32166bfc04b0 223 /* Printing to the console. */
Davidroid 6:32166bfc04b0 224 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 225
Davidroid 26:b0203c2265e5 226 /* Waiting. */
Davidroid 26:b0203c2265e5 227 wait_ms(DELAY_1);
Davidroid 6:32166bfc04b0 228
Davidroid 6:32166bfc04b0 229
Davidroid 6:32166bfc04b0 230 /*----- Going to a specified position. -----*/
Davidroid 6:32166bfc04b0 231
Davidroid 6:32166bfc04b0 232 /* Printing to the console. */
Davidroid 26:b0203c2265e5 233 printf("--> Going to position %d.\r\n", STEPS_1);
Davidroid 6:32166bfc04b0 234
Davidroid 6:32166bfc04b0 235 /* Requesting to go to a specified position. */
Davidroid 26:b0203c2265e5 236 motor->GoTo(STEPS_1);
Davidroid 6:32166bfc04b0 237
Davidroid 6:32166bfc04b0 238 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 239 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 240
Davidroid 6:32166bfc04b0 241 /* Getting current position. */
Davidroid 6:32166bfc04b0 242 position = motor->GetPosition();
Davidroid 6:32166bfc04b0 243
Davidroid 6:32166bfc04b0 244 /* Printing to the console. */
Davidroid 6:32166bfc04b0 245 printf(" Position: %d.\r\n", position);
Davidroid 6:32166bfc04b0 246
Davidroid 26:b0203c2265e5 247 /* Waiting. */
Davidroid 26:b0203c2265e5 248 wait_ms(DELAY_2);
Davidroid 6:32166bfc04b0 249
Davidroid 6:32166bfc04b0 250
Davidroid 6:32166bfc04b0 251 /*----- Going Home. -----*/
Davidroid 6:32166bfc04b0 252
Davidroid 6:32166bfc04b0 253 /* Printing to the console. */
Davidroid 6:32166bfc04b0 254 printf("--> Going Home.\r\n");
Davidroid 6:32166bfc04b0 255
Davidroid 6:32166bfc04b0 256 /* Requesting to go to home. */
Davidroid 6:32166bfc04b0 257 motor->GoHome();
Davidroid 6:32166bfc04b0 258
Davidroid 6:32166bfc04b0 259 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 260 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 261
Davidroid 6:32166bfc04b0 262 /* Getting current position. */
Davidroid 6:32166bfc04b0 263 position = motor->GetPosition();
Davidroid 6:32166bfc04b0 264
Davidroid 6:32166bfc04b0 265 /* Printing to the console. */
Davidroid 6:32166bfc04b0 266 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 267
Davidroid 26:b0203c2265e5 268 /* Waiting. */
Davidroid 26:b0203c2265e5 269 wait_ms(DELAY_2);
Davidroid 6:32166bfc04b0 270
Davidroid 6:32166bfc04b0 271
Davidroid 6:32166bfc04b0 272 /*----- Running. -----*/
Davidroid 6:32166bfc04b0 273
Davidroid 6:32166bfc04b0 274 /* Printing to the console. */
Davidroid 26:b0203c2265e5 275 printf("--> Running backward for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 6:32166bfc04b0 276
Davidroid 6:32166bfc04b0 277 /* Requesting to run backward. */
Davidroid 6:32166bfc04b0 278 motor->Run(StepperMotor::BWD);
Davidroid 6:32166bfc04b0 279
Davidroid 26:b0203c2265e5 280 /* Waiting. */
Davidroid 26:b0203c2265e5 281 wait_ms(DELAY_3);
Davidroid 6:32166bfc04b0 282
Davidroid 6:32166bfc04b0 283 /* Getting current speed. */
Davidroid 6:32166bfc04b0 284 int speed = motor->GetSpeed();
Davidroid 6:32166bfc04b0 285
Davidroid 6:32166bfc04b0 286 /* Printing to the console. */
Davidroid 6:32166bfc04b0 287 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 288
Davidroid 6:32166bfc04b0 289 /*----- Increasing the speed while running. -----*/
Davidroid 6:32166bfc04b0 290
Davidroid 6:32166bfc04b0 291 /* Printing to the console. */
Davidroid 26:b0203c2265e5 292 printf("--> Increasing the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 6:32166bfc04b0 293
Davidroid 26:b0203c2265e5 294 /* Increasing the speed. */
Davidroid 26:b0203c2265e5 295 motor->SetMaxSpeed(SPEED_1);
Davidroid 6:32166bfc04b0 296
Davidroid 26:b0203c2265e5 297 /* Waiting. */
Davidroid 26:b0203c2265e5 298 wait_ms(DELAY_3);
Davidroid 6:32166bfc04b0 299
Davidroid 6:32166bfc04b0 300 /* Getting current speed. */
Davidroid 6:32166bfc04b0 301 speed = motor->GetSpeed();
Davidroid 6:32166bfc04b0 302
Davidroid 6:32166bfc04b0 303 /* Printing to the console. */
Davidroid 6:32166bfc04b0 304 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 305
Davidroid 6:32166bfc04b0 306
Davidroid 6:32166bfc04b0 307 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 308
Davidroid 6:32166bfc04b0 309 /* Printing to the console. */
Davidroid 26:b0203c2265e5 310 printf("--> Decreasing the speed while running again for %d seconds.\r\n", DELAY_4 / 1000);
Davidroid 6:32166bfc04b0 311
Davidroid 26:b0203c2265e5 312 /* Decreasing the speed. */
Davidroid 26:b0203c2265e5 313 motor->SetMaxSpeed(SPEED_2);
Davidroid 6:32166bfc04b0 314
Davidroid 26:b0203c2265e5 315 /* Waiting. */
Davidroid 26:b0203c2265e5 316 wait_ms(DELAY_4);
Davidroid 6:32166bfc04b0 317
Davidroid 6:32166bfc04b0 318 /* Getting current speed. */
Davidroid 6:32166bfc04b0 319 speed = motor->GetSpeed();
Davidroid 6:32166bfc04b0 320
Davidroid 6:32166bfc04b0 321 /* Printing to the console. */
Davidroid 6:32166bfc04b0 322 printf(" Speed: %d.\r\n", speed);
Davidroid 6:32166bfc04b0 323
Davidroid 6:32166bfc04b0 324
Davidroid 26:b0203c2265e5 325 /*----- Hard Stop. -----*/
Davidroid 6:32166bfc04b0 326
Davidroid 6:32166bfc04b0 327 /* Printing to the console. */
Davidroid 26:b0203c2265e5 328 printf("--> Hard Stop.\r\n");
Davidroid 6:32166bfc04b0 329
Davidroid 6:32166bfc04b0 330 /* Requesting to immediatly stop. */
Davidroid 6:32166bfc04b0 331 motor->HardStop();
Davidroid 6:32166bfc04b0 332
Davidroid 6:32166bfc04b0 333 /* Waiting while the motor is active. */
Davidroid 6:32166bfc04b0 334 motor->WaitWhileActive();
Davidroid 6:32166bfc04b0 335
Davidroid 26:b0203c2265e5 336 /* Waiting. */
Davidroid 26:b0203c2265e5 337 wait_ms(DELAY_2);
Davidroid 6:32166bfc04b0 338
Davidroid 6:32166bfc04b0 339
Davidroid 6:32166bfc04b0 340 /*----- Infinite Loop. -----*/
Davidroid 6:32166bfc04b0 341
Davidroid 6:32166bfc04b0 342 /* Printing to the console. */
Davidroid 6:32166bfc04b0 343 printf("--> Infinite Loop...\r\n");
Davidroid 6:32166bfc04b0 344
Davidroid 6:32166bfc04b0 345 /* Setting the current position to be the home position. */
Davidroid 6:32166bfc04b0 346 motor->SetHome();
Davidroid 6:32166bfc04b0 347
Davidroid 6:32166bfc04b0 348 /* Infinite Loop. */
Davidroid 6:32166bfc04b0 349 while (1)
Davidroid 6:32166bfc04b0 350 {
Davidroid 6:32166bfc04b0 351 /* Requesting to go to a specified position. */
Davidroid 26:b0203c2265e5 352 motor->GoTo(STEPS_1 >> 1);
Davidroid 3:fffa53c7aed2 353
Davidroid 0:e6a49a092e2a 354 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 355 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 356
Davidroid 0:e6a49a092e2a 357 /* Requesting to go to a specified position. */
Davidroid 26:b0203c2265e5 358 motor->GoTo(- (STEPS_1 >> 1));
Davidroid 0:e6a49a092e2a 359
Davidroid 0:e6a49a092e2a 360 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 361 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 362 }
Davidroid 0:e6a49a092e2a 363 }