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:
Fri Dec 04 13:57:42 2015 +0000
Revision:
5:3b8e19bbf386
Parent:
3:fd280b953f77
Child:
9:f35fbeedb8f4
+ Some comments removed from the code and added to the web-page.

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 2:41eeee48951b 4 * @author Davide Aliprandi, STMicrolectronics
Davidroid 0:5148e9486cf2 5 * @version V1.0.0
Davidroid 0:5148e9486cf2 6 * @date November 4th, 2015
Davidroid 1:9f1974b0960d 7 * @brief mbed test application for the STMicrolectronics 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 1:9f1974b0960d 58 #define STEPS_1 100000
Davidroid 1:9f1974b0960d 59 #define STEPS_2 200000
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 0:5148e9486cf2 72
Davidroid 0:5148e9486cf2 73 /* Main ----------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 74
Davidroid 0:5148e9486cf2 75 int main()
Davidroid 0:5148e9486cf2 76 {
Davidroid 1:9f1974b0960d 77 /*----- Initialization. -----*/
Davidroid 1:9f1974b0960d 78
Davidroid 2:41eeee48951b 79 /* Initializing SPI bus. */
Davidroid 3:fd280b953f77 80 DevSPI dev_spi(D11, D12, D3);
Davidroid 0:5148e9486cf2 81
Davidroid 5:3b8e19bbf386 82 /* Initializing Motor Control Expansion Board. */
Davidroid 2:41eeee48951b 83 x_nucleo_ihm02a1 = new X_NUCLEO_IHM02A1(A4, A5, D4, A2, &dev_spi);
Davidroid 0:5148e9486cf2 84
Davidroid 1:9f1974b0960d 85 /* Building a list of motor control components. */
Davidroid 1:9f1974b0960d 86 L6470 **motors = x_nucleo_ihm02a1->GetComponents();
Davidroid 0:5148e9486cf2 87
Davidroid 0:5148e9486cf2 88 /* Printing to the console. */
Davidroid 0:5148e9486cf2 89 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:5148e9486cf2 90
Davidroid 1:9f1974b0960d 91
Davidroid 1:9f1974b0960d 92 /*----- Setting home and marke positions, getting positions, and going to positions. -----*/
Davidroid 1:9f1974b0960d 93
Davidroid 1:9f1974b0960d 94 /* Printing to the console. */
Davidroid 1:9f1974b0960d 95 printf("--> Setting home position.\r\n");
Davidroid 1:9f1974b0960d 96
Davidroid 1:9f1974b0960d 97 /* Setting the home position. */
Davidroid 1:9f1974b0960d 98 motors[0]->SetHome();
Davidroid 1:9f1974b0960d 99
Davidroid 1:9f1974b0960d 100 /* Waiting. */
Davidroid 1:9f1974b0960d 101 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 102
Davidroid 1:9f1974b0960d 103 /* Getting the current position. */
Davidroid 1:9f1974b0960d 104 int position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 105
Davidroid 1:9f1974b0960d 106 /* Printing to the console. */
Davidroid 1:9f1974b0960d 107 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 108
Davidroid 1:9f1974b0960d 109 /* Waiting. */
Davidroid 1:9f1974b0960d 110 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 111
Davidroid 1:9f1974b0960d 112 /* Printing to the console. */
Davidroid 1:9f1974b0960d 113 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 1:9f1974b0960d 114
Davidroid 1:9f1974b0960d 115 /* Moving. */
Davidroid 1:9f1974b0960d 116 motors[0]->Move(StepperMotor::FWD, STEPS_1);
Davidroid 0:5148e9486cf2 117
Davidroid 1:9f1974b0960d 118 /* Waiting while active. */
Davidroid 1:9f1974b0960d 119 motors[0]->WaitWhileActive();
Davidroid 1:9f1974b0960d 120
Davidroid 1:9f1974b0960d 121 /* Getting the current position. */
Davidroid 1:9f1974b0960d 122 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 123
Davidroid 1:9f1974b0960d 124 /* Printing to the console. */
Davidroid 1:9f1974b0960d 125 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 126
Davidroid 1:9f1974b0960d 127 /* Printing to the console. */
Davidroid 1:9f1974b0960d 128 printf("--> Marking the current position.\r\n");
Davidroid 0:5148e9486cf2 129
Davidroid 1:9f1974b0960d 130 /* Marking the current position. */
Davidroid 1:9f1974b0960d 131 motors[0]->SetMark();
Davidroid 1:9f1974b0960d 132
Davidroid 1:9f1974b0960d 133 /* Waiting. */
Davidroid 1:9f1974b0960d 134 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 135
Davidroid 1:9f1974b0960d 136 /* Printing to the console. */
Davidroid 1:9f1974b0960d 137 printf("--> Moving backward %d steps.\r\n", STEPS_2);
Davidroid 1:9f1974b0960d 138
Davidroid 1:9f1974b0960d 139 /* Moving. */
Davidroid 1:9f1974b0960d 140 motors[0]->Move(StepperMotor::BWD, STEPS_2);
Davidroid 1:9f1974b0960d 141
Davidroid 1:9f1974b0960d 142 /* Waiting while active. */
Davidroid 1:9f1974b0960d 143 motors[0]->WaitWhileActive();
Davidroid 0:5148e9486cf2 144
Davidroid 1:9f1974b0960d 145 /* Waiting. */
Davidroid 1:9f1974b0960d 146 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 147
Davidroid 1:9f1974b0960d 148 /* Getting the current position. */
Davidroid 1:9f1974b0960d 149 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 150
Davidroid 1:9f1974b0960d 151 /* Printing to the console. */
Davidroid 1:9f1974b0960d 152 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 153
Davidroid 1:9f1974b0960d 154 /* Waiting. */
Davidroid 1:9f1974b0960d 155 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 156
Davidroid 1:9f1974b0960d 157 /* Printing to the console. */
Davidroid 1:9f1974b0960d 158 printf("--> Going to marked position.\r\n");
Davidroid 0:5148e9486cf2 159
Davidroid 1:9f1974b0960d 160 /* Going to marked position. */
Davidroid 1:9f1974b0960d 161 motors[0]->GoMark();
Davidroid 1:9f1974b0960d 162
Davidroid 1:9f1974b0960d 163 /* Waiting while active. */
Davidroid 1:9f1974b0960d 164 motors[0]->WaitWhileActive();
Davidroid 0:5148e9486cf2 165
Davidroid 1:9f1974b0960d 166 /* Waiting. */
Davidroid 1:9f1974b0960d 167 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 168
Davidroid 1:9f1974b0960d 169 /* Getting the current position. */
Davidroid 1:9f1974b0960d 170 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 171
Davidroid 1:9f1974b0960d 172 /* Printing to the console. */
Davidroid 1:9f1974b0960d 173 printf("--> Getting the current position: %d\r\n", position);
Davidroid 0:5148e9486cf2 174
Davidroid 1:9f1974b0960d 175 /* Waiting. */
Davidroid 1:9f1974b0960d 176 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 177
Davidroid 1:9f1974b0960d 178 /* Printing to the console. */
Davidroid 1:9f1974b0960d 179 printf("--> Going to home position.\r\n");
Davidroid 0:5148e9486cf2 180
Davidroid 1:9f1974b0960d 181 /* Going to home position. */
Davidroid 1:9f1974b0960d 182 motors[0]->GoHome();
Davidroid 1:9f1974b0960d 183
Davidroid 1:9f1974b0960d 184 /* Waiting while active. */
Davidroid 1:9f1974b0960d 185 motors[0]->WaitWhileActive();
Davidroid 1:9f1974b0960d 186
Davidroid 1:9f1974b0960d 187 /* Waiting. */
Davidroid 1:9f1974b0960d 188 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 189
Davidroid 1:9f1974b0960d 190 /* Getting the current position. */
Davidroid 1:9f1974b0960d 191 position = motors[0]->GetPosition();
Davidroid 1:9f1974b0960d 192
Davidroid 1:9f1974b0960d 193 /* Printing to the console. */
Davidroid 1:9f1974b0960d 194 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 195
Davidroid 1:9f1974b0960d 196 /* Waiting. */
Davidroid 1:9f1974b0960d 197 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 198
Davidroid 0:5148e9486cf2 199
Davidroid 1:9f1974b0960d 200 /*----- Running together for a certain amount of time. -----*/
Davidroid 1:9f1974b0960d 201
Davidroid 1:9f1974b0960d 202 /* Printing to the console. */
Davidroid 1:9f1974b0960d 203 printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 204
Davidroid 1:9f1974b0960d 205 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 1:9f1974b0960d 206 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 207 motors[m]->PrepareRun(StepperMotor::BWD, 400);
Davidroid 0:5148e9486cf2 208
Davidroid 1:9f1974b0960d 209 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 210 x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 211
Davidroid 1:9f1974b0960d 212 /* Waiting. */
Davidroid 1:9f1974b0960d 213 wait_ms(DELAY_3);
Davidroid 1:9f1974b0960d 214
Davidroid 1:9f1974b0960d 215
Davidroid 1:9f1974b0960d 216 /*----- Increasing the speed while running. -----*/
Davidroid 0:5148e9486cf2 217
Davidroid 1:9f1974b0960d 218 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 1:9f1974b0960d 219 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 220 motors[m]->PrepareGetSpeed();
Davidroid 0:5148e9486cf2 221
Davidroid 1:9f1974b0960d 222 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 223 uint32_t* results = x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 224
Davidroid 1:9f1974b0960d 225 /* Printing to the console. */
Davidroid 1:9f1974b0960d 226 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 227
Davidroid 1:9f1974b0960d 228 /* Printing to the console. */
Davidroid 1:9f1974b0960d 229 printf("--> Doublig the speed while running.\r\n");
Davidroid 0:5148e9486cf2 230
Davidroid 1:9f1974b0960d 231 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 1:9f1974b0960d 232 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 233 motors[m]->PrepareRun(StepperMotor::BWD, results[m] << 1);
Davidroid 1:9f1974b0960d 234
Davidroid 1:9f1974b0960d 235 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 236 results = x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 237
Davidroid 1:9f1974b0960d 238 /* Waiting. */
Davidroid 1:9f1974b0960d 239 wait_ms(DELAY_3);
Davidroid 0:5148e9486cf2 240
Davidroid 1:9f1974b0960d 241 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 1:9f1974b0960d 242 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 243 motors[m]->PrepareGetSpeed();
Davidroid 0:5148e9486cf2 244
Davidroid 1:9f1974b0960d 245 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 246 results = x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 247
Davidroid 1:9f1974b0960d 248 /* Printing to the console. */
Davidroid 1:9f1974b0960d 249 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 250
Davidroid 1:9f1974b0960d 251 /* Waiting. */
Davidroid 1:9f1974b0960d 252 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 253
Davidroid 0:5148e9486cf2 254
Davidroid 1:9f1974b0960d 255 /*----- Hard Stop. -----*/
Davidroid 0:5148e9486cf2 256
Davidroid 1:9f1974b0960d 257 /* Printing to the console. */
Davidroid 1:9f1974b0960d 258 printf("--> Hard Stop.\r\n");
Davidroid 0:5148e9486cf2 259
Davidroid 1:9f1974b0960d 260 /* Preparing each motor to perform a hard stop. */
Davidroid 1:9f1974b0960d 261 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 262 motors[m]->PrepareHardStop();
Davidroid 0:5148e9486cf2 263
Davidroid 1:9f1974b0960d 264 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 265 x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 0:5148e9486cf2 266
Davidroid 1:9f1974b0960d 267 /* Waiting. */
Davidroid 1:9f1974b0960d 268 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 269
Davidroid 0:5148e9486cf2 270
Davidroid 1:9f1974b0960d 271 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 1:9f1974b0960d 272
Davidroid 1:9f1974b0960d 273 /* Printing to the console. */
Davidroid 1:9f1974b0960d 274 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 1:9f1974b0960d 275
Davidroid 1:9f1974b0960d 276 /* Doing a full revolution on each motor, one after the other. */
Davidroid 1:9f1974b0960d 277 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 278 for (int i = 0; i < MPR_1; i++)
Davidroid 1:9f1974b0960d 279 {
Davidroid 1:9f1974b0960d 280 /* Computing the number of steps. */
Davidroid 1:9f1974b0960d 281 int steps = (int) (((int) X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].fullstepsperrevolution * pow(2.0f, X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].step_sel)) / MPR_1);
Davidroid 1:9f1974b0960d 282
Davidroid 1:9f1974b0960d 283 /* Moving. */
Davidroid 1:9f1974b0960d 284 motors[m]->Move(StepperMotor::FWD, steps);
Davidroid 1:9f1974b0960d 285
Davidroid 1:9f1974b0960d 286 /* Waiting while active. */
Davidroid 1:9f1974b0960d 287 motors[m]->WaitWhileActive();
Davidroid 1:9f1974b0960d 288
Davidroid 1:9f1974b0960d 289 /* Waiting. */
Davidroid 1:9f1974b0960d 290 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 291 }
Davidroid 1:9f1974b0960d 292
Davidroid 1:9f1974b0960d 293 /* Waiting. */
Davidroid 1:9f1974b0960d 294 wait_ms(DELAY_2);
Davidroid 1:9f1974b0960d 295
Davidroid 1:9f1974b0960d 296
Davidroid 1:9f1974b0960d 297 /*----- High Impedance State. -----*/
Davidroid 1:9f1974b0960d 298
Davidroid 1:9f1974b0960d 299 /* Printing to the console. */
Davidroid 1:9f1974b0960d 300 printf("--> High Impedance State.\r\n");
Davidroid 1:9f1974b0960d 301
Davidroid 1:9f1974b0960d 302 /* Preparing each motor to set High Impedance State. */
Davidroid 1:9f1974b0960d 303 for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
Davidroid 1:9f1974b0960d 304 motors[m]->PrepareHardHiZ();
Davidroid 1:9f1974b0960d 305
Davidroid 1:9f1974b0960d 306 /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 307 x_nucleo_ihm02a1->PerformPreparedActions();
Davidroid 1:9f1974b0960d 308
Davidroid 1:9f1974b0960d 309 /* Waiting. */
Davidroid 1:9f1974b0960d 310 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 311 }