Simple test application for the STMicroelectronics X-NUCLEO-IHM02A1 Stepper Motor Control Expansion Board.

Dependencies:   X_NUCLEO_IHM02A1 mbed

Fork of HelloWorld_IHM02A1 by ST Expansion SW Team

Motor Control with the X-NUCLEO-IHM02A1 Expansion Board

This application provides a simple example of usage of the X-NUCLEO-IHM02A1 Stepper Motor Control Expansion Board.
It shows how to use two stepper motors connected in daisy chain configuration to the board, moving the rotors to specific positions, with given speed values, direction of rotations, etc.

Committer:
Davidroid
Date:
Wed Mar 01 17:53:04 2017 +0000
Revision:
22:e81ccf73bc5d
Parent:
18:591a007effc2
Child:
23:073b26366d03
Fitting mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:5148e9486cf2 1 /**
Davidroid 0:5148e9486cf2 2 ******************************************************************************
Davidroid 0:5148e9486cf2 3 * @file main.cpp
Davidroid 12:5be6dd48b94a 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:5148e9486cf2 5 * @version V1.0.0
Davidroid 0:5148e9486cf2 6 * @date November 4th, 2015
Davidroid 12:5be6dd48b94a 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM02A1
Davidroid 1:9f1974b0960d 8 * Motor Control Expansion Board: control of 2 motors.
Davidroid 0:5148e9486cf2 9 ******************************************************************************
Davidroid 0:5148e9486cf2 10 * @attention
Davidroid 0:5148e9486cf2 11 *
Davidroid 0:5148e9486cf2 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:5148e9486cf2 13 *
Davidroid 0:5148e9486cf2 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:5148e9486cf2 15 * are permitted provided that the following conditions are met:
Davidroid 0:5148e9486cf2 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:5148e9486cf2 17 * this list of conditions and the following disclaimer.
Davidroid 0:5148e9486cf2 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:5148e9486cf2 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:5148e9486cf2 20 * and/or other materials provided with the distribution.
Davidroid 0:5148e9486cf2 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:5148e9486cf2 22 * may be used to endorse or promote products derived from this software
Davidroid 0:5148e9486cf2 23 * without specific prior written permission.
Davidroid 0:5148e9486cf2 24 *
Davidroid 0:5148e9486cf2 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:5148e9486cf2 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:5148e9486cf2 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:5148e9486cf2 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:5148e9486cf2 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:5148e9486cf2 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:5148e9486cf2 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:5148e9486cf2 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:5148e9486cf2 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:5148e9486cf2 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:5148e9486cf2 35 *
Davidroid 0:5148e9486cf2 36 ******************************************************************************
Davidroid 0:5148e9486cf2 37 */
Davidroid 0:5148e9486cf2 38
Davidroid 0:5148e9486cf2 39
Davidroid 0:5148e9486cf2 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 41
Davidroid 0:5148e9486cf2 42 /* mbed specific header files. */
Davidroid 0:5148e9486cf2 43 #include "mbed.h"
Davidroid 0:5148e9486cf2 44
Davidroid 0:5148e9486cf2 45 /* Helper header files. */
Davidroid 0:5148e9486cf2 46 #include "DevSPI.h"
Davidroid 0:5148e9486cf2 47
Davidroid 0:5148e9486cf2 48 /* Expansion Board specific header files. */
Davidroid 0:5148e9486cf2 49 #include "x_nucleo_ihm02a1_class.h"
Davidroid 0:5148e9486cf2 50
Davidroid 0:5148e9486cf2 51
Davidroid 0:5148e9486cf2 52 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 53
Davidroid 0:5148e9486cf2 54 /* Number of movements per revolution. */
Davidroid 0:5148e9486cf2 55 #define MPR_1 4
Davidroid 1:9f1974b0960d 56
Davidroid 1:9f1974b0960d 57 /* Number of steps. */
Davidroid 18:591a007effc2 58 #define STEPS_1 (400 * 128) /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
Davidroid 18:591a007effc2 59 #define STEPS_2 (STEPS_1 * 2)
Davidroid 0:5148e9486cf2 60
Davidroid 0:5148e9486cf2 61 /* Delay in milliseconds. */
Davidroid 1:9f1974b0960d 62 #define DELAY_1 1000
Davidroid 0:5148e9486cf2 63 #define DELAY_2 2000
Davidroid 0:5148e9486cf2 64 #define DELAY_3 5000
Davidroid 0:5148e9486cf2 65
Davidroid 0:5148e9486cf2 66
Davidroid 0:5148e9486cf2 67 /* Variables -----------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 68
Davidroid 0:5148e9486cf2 69 /* Motor Control Expansion Board. */
Davidroid 0:5148e9486cf2 70 X_NUCLEO_IHM02A1 *x_nucleo_ihm02a1;
Davidroid 0:5148e9486cf2 71
Davidroid 9:f35fbeedb8f4 72 /* Initialization parameters of the motors connected to the expansion board. */
Davidroid 22:e81ccf73bc5d 73 L6470_Init_t init[L6470DAISYCHAINSIZE] = {
Davidroid 9:f35fbeedb8f4 74 /* First Motor. */
Davidroid 9:f35fbeedb8f4 75 {
Davidroid 18:591a007effc2 76 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 77 400, /* Min number of steps per revolution for the motor. */
Davidroid 18:591a007effc2 78 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 79 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 80 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 81 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 82 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 83 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 84 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 85 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 86 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 87 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 88 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 89 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 90 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 91 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 92 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 93 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 94 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 95 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 96 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 97 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 98 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 99 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 100 },
Davidroid 9:f35fbeedb8f4 101
Davidroid 9:f35fbeedb8f4 102 /* Second Motor. */
Davidroid 9:f35fbeedb8f4 103 {
Davidroid 18:591a007effc2 104 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 105 400, /* Min number of steps per revolution for the motor. */
Davidroid 18:591a007effc2 106 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 107 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 108 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 109 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 110 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 111 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 112 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 113 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 114 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 115 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 116 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 117 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 118 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 119 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 120 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 121 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 122 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 123 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 124 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 125 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 126 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 127 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 128 }
Davidroid 9:f35fbeedb8f4 129 };
Davidroid 9:f35fbeedb8f4 130
Davidroid 0:5148e9486cf2 131
Davidroid 0:5148e9486cf2 132 /* Main ----------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 133
Davidroid 0:5148e9486cf2 134 int main()
Davidroid 0:5148e9486cf2 135 {
Davidroid 1:9f1974b0960d 136 /*----- Initialization. -----*/
Davidroid 1:9f1974b0960d 137
Davidroid 2:41eeee48951b 138 /* Initializing SPI bus. */
Davidroid 3:fd280b953f77 139 DevSPI dev_spi(D11, D12, D3);
Davidroid 0:5148e9486cf2 140
Davidroid 5:3b8e19bbf386 141 /* Initializing Motor Control Expansion Board. */
Davidroid 9:f35fbeedb8f4 142 x_nucleo_ihm02a1 = new X_NUCLEO_IHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);
Davidroid 0:5148e9486cf2 143
Davidroid 1:9f1974b0960d 144 /* Building a list of motor control components. */
Davidroid 1:9f1974b0960d 145 L6470 **motors = x_nucleo_ihm02a1->GetComponents();
Davidroid 0:5148e9486cf2 146
Davidroid 0:5148e9486cf2 147 /* Printing to the console. */
Davidroid 0:5148e9486cf2 148 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:5148e9486cf2 149
Davidroid 1:9f1974b0960d 150
Davidroid 1:9f1974b0960d 151 /*----- Setting home and marke positions, getting positions, and going to positions. -----*/
Davidroid 1:9f1974b0960d 152
Davidroid 1:9f1974b0960d 153 /* Printing to the console. */
Davidroid 1:9f1974b0960d 154 printf("--> Setting home position.\r\n");
Davidroid 1:9f1974b0960d 155
Davidroid 1:9f1974b0960d 156 /* Setting the home position. */
Davidroid 1:9f1974b0960d 157 motors[0]->SetHome();
Davidroid 1:9f1974b0960d 158
Davidroid 1:9f1974b0960d 159 /* Waiting. */
Davidroid 1:9f1974b0960d 160 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 161
Davidroid 1:9f1974b0960d 162 /* Getting the current position. */
Davidroid 1:9f1974b0960d 163 int position = motors[0]->GetPosition();
Davidroid 18:591a007effc2 164
Davidroid 1:9f1974b0960d 165 /* Printing to the console. */
Davidroid 1:9f1974b0960d 166 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 167
Davidroid 1:9f1974b0960d 168 /* Waiting. */
Davidroid 1:9f1974b0960d 169 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 170
Davidroid 1:9f1974b0960d 171 /* Printing to the console. */
Davidroid 1:9f1974b0960d 172 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 1:9f1974b0960d 173
Davidroid 1:9f1974b0960d 174 /* Moving. */
Davidroid 1:9f1974b0960d 175 motors[0]->Move(StepperMotor::FWD, STEPS_1);
Davidroid 0:5148e9486cf2 176
Davidroid 1:9f1974b0960d 177 /* Waiting while active. */
Davidroid 1:9f1974b0960d 178 motors[0]->WaitWhileActive();
Davidroid 1:9f1974b0960d 179
Davidroid 1:9f1974b0960d 180 /* Getting the current position. */
Davidroid 1:9f1974b0960d 181 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 182
Davidroid 1:9f1974b0960d 183 /* Printing to the console. */
Davidroid 1:9f1974b0960d 184 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 185
Davidroid 1:9f1974b0960d 186 /* Printing to the console. */
Davidroid 1:9f1974b0960d 187 printf("--> Marking the current position.\r\n");
Davidroid 0:5148e9486cf2 188
Davidroid 1:9f1974b0960d 189 /* Marking the current position. */
Davidroid 1:9f1974b0960d 190 motors[0]->SetMark();
Davidroid 1:9f1974b0960d 191
Davidroid 1:9f1974b0960d 192 /* Waiting. */
Davidroid 1:9f1974b0960d 193 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 194
Davidroid 1:9f1974b0960d 195 /* Printing to the console. */
Davidroid 1:9f1974b0960d 196 printf("--> Moving backward %d steps.\r\n", STEPS_2);
Davidroid 1:9f1974b0960d 197
Davidroid 1:9f1974b0960d 198 /* Moving. */
Davidroid 1:9f1974b0960d 199 motors[0]->Move(StepperMotor::BWD, STEPS_2);
Davidroid 1:9f1974b0960d 200
Davidroid 1:9f1974b0960d 201 /* Waiting while active. */
Davidroid 1:9f1974b0960d 202 motors[0]->WaitWhileActive();
Davidroid 0:5148e9486cf2 203
Davidroid 1:9f1974b0960d 204 /* Waiting. */
Davidroid 1:9f1974b0960d 205 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 206
Davidroid 1:9f1974b0960d 207 /* Getting the current position. */
Davidroid 1:9f1974b0960d 208 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 209
Davidroid 1:9f1974b0960d 210 /* Printing to the console. */
Davidroid 1:9f1974b0960d 211 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 212
Davidroid 1:9f1974b0960d 213 /* Waiting. */
Davidroid 1:9f1974b0960d 214 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 215
Davidroid 1:9f1974b0960d 216 /* Printing to the console. */
Davidroid 1:9f1974b0960d 217 printf("--> Going to marked position.\r\n");
Davidroid 0:5148e9486cf2 218
Davidroid 1:9f1974b0960d 219 /* Going to marked position. */
Davidroid 1:9f1974b0960d 220 motors[0]->GoMark();
Davidroid 1:9f1974b0960d 221
Davidroid 1:9f1974b0960d 222 /* Waiting while active. */
Davidroid 1:9f1974b0960d 223 motors[0]->WaitWhileActive();
Davidroid 0:5148e9486cf2 224
Davidroid 1:9f1974b0960d 225 /* Waiting. */
Davidroid 1:9f1974b0960d 226 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 227
Davidroid 1:9f1974b0960d 228 /* Getting the current position. */
Davidroid 1:9f1974b0960d 229 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 230
Davidroid 1:9f1974b0960d 231 /* Printing to the console. */
Davidroid 1:9f1974b0960d 232 printf("--> Getting the current position: %d\r\n", position);
Davidroid 0:5148e9486cf2 233
Davidroid 1:9f1974b0960d 234 /* Waiting. */
Davidroid 1:9f1974b0960d 235 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 236
Davidroid 1:9f1974b0960d 237 /* Printing to the console. */
Davidroid 1:9f1974b0960d 238 printf("--> Going to home position.\r\n");
Davidroid 0:5148e9486cf2 239
Davidroid 1:9f1974b0960d 240 /* Going to home position. */
Davidroid 1:9f1974b0960d 241 motors[0]->GoHome();
Davidroid 1:9f1974b0960d 242
Davidroid 1:9f1974b0960d 243 /* Waiting while active. */
Davidroid 1:9f1974b0960d 244 motors[0]->WaitWhileActive();
Davidroid 1:9f1974b0960d 245
Davidroid 1:9f1974b0960d 246 /* Waiting. */
Davidroid 1:9f1974b0960d 247 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 248
Davidroid 1:9f1974b0960d 249 /* Getting the current position. */
Davidroid 1:9f1974b0960d 250 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 251
Davidroid 1:9f1974b0960d 252 /* Printing to the console. */
Davidroid 1:9f1974b0960d 253 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 254
Davidroid 1:9f1974b0960d 255 /* Waiting. */
Davidroid 18:591a007effc2 256 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 257
Davidroid 18:591a007effc2 258 /* Printing to the console. */
Davidroid 18:591a007effc2 259 printf("--> Halving the microsteps.\r\n");
Davidroid 18:591a007effc2 260
Davidroid 18:591a007effc2 261 /* Halving the microsteps. */
Davidroid 18:591a007effc2 262 init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel - 1 : init[0].step_sel);
Davidroid 22:e81ccf73bc5d 263 if (!motors[0]->SetStepMode((StepperMotor::step_mode_t) init[0].step_sel)) {
Davidroid 18:591a007effc2 264 printf(" Step Mode not allowed.\r\n");
Davidroid 22:e81ccf73bc5d 265 }
Davidroid 18:591a007effc2 266
Davidroid 18:591a007effc2 267 /* Waiting. */
Davidroid 18:591a007effc2 268 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 269
Davidroid 18:591a007effc2 270 /* Printing to the console. */
Davidroid 18:591a007effc2 271 printf("--> Setting home position.\r\n");
Davidroid 18:591a007effc2 272
Davidroid 18:591a007effc2 273 /* Setting the home position. */
Davidroid 18:591a007effc2 274 motors[0]->SetHome();
Davidroid 18:591a007effc2 275
Davidroid 18:591a007effc2 276 /* Waiting. */
Davidroid 18:591a007effc2 277 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 278
Davidroid 18:591a007effc2 279 /* Getting the current position. */
Davidroid 18:591a007effc2 280 position = motors[0]->GetPosition();
Davidroid 18:591a007effc2 281
Davidroid 18:591a007effc2 282 /* Printing to the console. */
Davidroid 18:591a007effc2 283 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 284
Davidroid 18:591a007effc2 285 /* Waiting. */
Davidroid 18:591a007effc2 286 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 287
Davidroid 18:591a007effc2 288 /* Printing to the console. */
Davidroid 18:591a007effc2 289 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 18:591a007effc2 290
Davidroid 18:591a007effc2 291 /* Moving. */
Davidroid 18:591a007effc2 292 motors[0]->Move(StepperMotor::FWD, STEPS_1);
Davidroid 18:591a007effc2 293
Davidroid 18:591a007effc2 294 /* Waiting while active. */
Davidroid 18:591a007effc2 295 motors[0]->WaitWhileActive();
Davidroid 18:591a007effc2 296
Davidroid 18:591a007effc2 297 /* Getting the current position. */
Davidroid 18:591a007effc2 298 position = motors[0]->GetPosition();
Davidroid 18:591a007effc2 299
Davidroid 18:591a007effc2 300 /* Printing to the console. */
Davidroid 18:591a007effc2 301 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 302
Davidroid 18:591a007effc2 303 /* Printing to the console. */
Davidroid 18:591a007effc2 304 printf("--> Marking the current position.\r\n");
Davidroid 18:591a007effc2 305
Davidroid 18:591a007effc2 306 /* Marking the current position. */
Davidroid 18:591a007effc2 307 motors[0]->SetMark();
Davidroid 18:591a007effc2 308
Davidroid 18:591a007effc2 309 /* Waiting. */
Davidroid 1:9f1974b0960d 310 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 311
Davidroid 0:5148e9486cf2 312
Davidroid 1:9f1974b0960d 313 /*----- Running together for a certain amount of time. -----*/
Davidroid 1:9f1974b0960d 314
Davidroid 1:9f1974b0960d 315 /* Printing to the console. */
Davidroid 1:9f1974b0960d 316 printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 317
Davidroid 1:9f1974b0960d 318 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 319 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 1:9f1974b0960d 320 motors[m]->PrepareRun(StepperMotor::BWD, 400);
Davidroid 22:e81ccf73bc5d 321 }
Davidroid 0:5148e9486cf2 322
Davidroid 1:9f1974b0960d 323 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 324 x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 325
Davidroid 1:9f1974b0960d 326 /* Waiting. */
Davidroid 1:9f1974b0960d 327 wait_ms(DELAY_3);
Davidroid 1:9f1974b0960d 328
Davidroid 1:9f1974b0960d 329
Davidroid 1:9f1974b0960d 330 /*----- Increasing the speed while running. -----*/
Davidroid 0:5148e9486cf2 331
Davidroid 1:9f1974b0960d 332 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 333 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 1:9f1974b0960d 334 motors[m]->PrepareGetSpeed();
Davidroid 22:e81ccf73bc5d 335 }
Davidroid 0:5148e9486cf2 336
Davidroid 1:9f1974b0960d 337 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 338 uint32_t* results = x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 339
Davidroid 1:9f1974b0960d 340 /* Printing to the console. */
Davidroid 1:9f1974b0960d 341 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 342
Davidroid 1:9f1974b0960d 343 /* Printing to the console. */
Davidroid 18:591a007effc2 344 printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 345
Davidroid 1:9f1974b0960d 346 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 347 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 1:9f1974b0960d 348 motors[m]->PrepareRun(StepperMotor::BWD, results[m] << 1);
Davidroid 22:e81ccf73bc5d 349 }
Davidroid 1:9f1974b0960d 350
Davidroid 1:9f1974b0960d 351 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 352 results = x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 353
Davidroid 1:9f1974b0960d 354 /* Waiting. */
Davidroid 1:9f1974b0960d 355 wait_ms(DELAY_3);
Davidroid 0:5148e9486cf2 356
Davidroid 1:9f1974b0960d 357 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 358 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 1:9f1974b0960d 359 motors[m]->PrepareGetSpeed();
Davidroid 22:e81ccf73bc5d 360 }
Davidroid 0:5148e9486cf2 361
Davidroid 1:9f1974b0960d 362 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 363 results = x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 364
Davidroid 1:9f1974b0960d 365 /* Printing to the console. */
Davidroid 1:9f1974b0960d 366 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 367
Davidroid 1:9f1974b0960d 368 /* Waiting. */
Davidroid 1:9f1974b0960d 369 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 370
Davidroid 0:5148e9486cf2 371
Davidroid 1:9f1974b0960d 372 /*----- Hard Stop. -----*/
Davidroid 0:5148e9486cf2 373
Davidroid 1:9f1974b0960d 374 /* Printing to the console. */
Davidroid 1:9f1974b0960d 375 printf("--> Hard Stop.\r\n");
Davidroid 0:5148e9486cf2 376
Davidroid 1:9f1974b0960d 377 /* Preparing each motor to perform a hard stop. */
Davidroid 22:e81ccf73bc5d 378 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 1:9f1974b0960d 379 motors[m]->PrepareHardStop();
Davidroid 22:e81ccf73bc5d 380 }
Davidroid 0:5148e9486cf2 381
Davidroid 1:9f1974b0960d 382 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 383 x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 384
Davidroid 1:9f1974b0960d 385 /* Waiting. */
Davidroid 1:9f1974b0960d 386 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 387
Davidroid 0:5148e9486cf2 388
Davidroid 1:9f1974b0960d 389 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 1:9f1974b0960d 390
Davidroid 1:9f1974b0960d 391 /* Printing to the console. */
Davidroid 1:9f1974b0960d 392 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 1:9f1974b0960d 393
Davidroid 1:9f1974b0960d 394 /* Doing a full revolution on each motor, one after the other. */
Davidroid 22:e81ccf73bc5d 395 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 22:e81ccf73bc5d 396 for (int i = 0; i < MPR_1; i++) {
Davidroid 1:9f1974b0960d 397 /* Computing the number of steps. */
Davidroid 9:f35fbeedb8f4 398 int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1);
Davidroid 1:9f1974b0960d 399
Davidroid 1:9f1974b0960d 400 /* Moving. */
Davidroid 1:9f1974b0960d 401 motors[m]->Move(StepperMotor::FWD, steps);
Davidroid 1:9f1974b0960d 402
Davidroid 1:9f1974b0960d 403 /* Waiting while active. */
Davidroid 1:9f1974b0960d 404 motors[m]->WaitWhileActive();
Davidroid 1:9f1974b0960d 405
Davidroid 1:9f1974b0960d 406 /* Waiting. */
Davidroid 1:9f1974b0960d 407 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 408 }
Davidroid 22:e81ccf73bc5d 409 }
Davidroid 1:9f1974b0960d 410
Davidroid 1:9f1974b0960d 411 /* Waiting. */
Davidroid 1:9f1974b0960d 412 wait_ms(DELAY_2);
Davidroid 1:9f1974b0960d 413
Davidroid 1:9f1974b0960d 414
Davidroid 1:9f1974b0960d 415 /*----- High Impedance State. -----*/
Davidroid 1:9f1974b0960d 416
Davidroid 1:9f1974b0960d 417 /* Printing to the console. */
Davidroid 1:9f1974b0960d 418 printf("--> High Impedance State.\r\n");
Davidroid 1:9f1974b0960d 419
Davidroid 1:9f1974b0960d 420 /* Preparing each motor to set High Impedance State. */
Davidroid 22:e81ccf73bc5d 421 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 1:9f1974b0960d 422 motors[m]->PrepareHardHiZ();
Davidroid 22:e81ccf73bc5d 423 }
Davidroid 1:9f1974b0960d 424
Davidroid 1:9f1974b0960d 425 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 426 x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 1:9f1974b0960d 427
Davidroid 1:9f1974b0960d 428 /* Waiting. */
Davidroid 1:9f1974b0960d 429 wait_ms(DELAY_2);
Davidroid 22:e81ccf73bc5d 430 }