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_2Motors by ST

Motor Control with the X-NUCLEO-IHM01A1 Expansion Board

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

Committer:
Davidroid
Date:
Wed Jan 13 15:12:12 2016 +0000
Revision:
16:810667a9f31f
Parent:
14:fcd452b03d1a
Child:
17:aae1446c67f4
+ Updated with the new version of the library.; + Order of initialization instructions corrected.

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 7:141f878e9590 4 * @author Davide Aliprandi, STMicrolectronics
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 0:e6a49a092e2a 7 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
Davidroid 3:02d9ec4f88b2 8 * Motor Control Expansion Board: control of 2 motors.
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 9:a9e51320aee4 54 /* Number of steps to move. */
Davidroid 9:a9e51320aee4 55 #define STEPS 3200
Davidroid 0:e6a49a092e2a 56
Davidroid 0:e6a49a092e2a 57
Davidroid 0:e6a49a092e2a 58 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 59
Davidroid 0:e6a49a092e2a 60 /* Motor Control Component. */
Davidroid 3:02d9ec4f88b2 61 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 62 L6474 *motor2;
Davidroid 0:e6a49a092e2a 63
Davidroid 0:e6a49a092e2a 64
Davidroid 0:e6a49a092e2a 65 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 66
Davidroid 0:e6a49a092e2a 67 int main()
Davidroid 0:e6a49a092e2a 68 {
Davidroid 8:cec4c2c03a27 69 /*----- Initialization. -----*/
Davidroid 8:cec4c2c03a27 70
Davidroid 0:e6a49a092e2a 71 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 72 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 73
Davidroid 9:a9e51320aee4 74 /* Initializing Motor Control Components. */
Davidroid 5:a0268a435bb1 75 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 16:810667a9f31f 76 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
Davidroid 14:fcd452b03d1a 77 if (motor1->Init() != COMPONENT_OK)
Davidroid 14:fcd452b03d1a 78 exit(EXIT_FAILURE);
Davidroid 14:fcd452b03d1a 79 if (motor2->Init() != COMPONENT_OK)
Davidroid 14:fcd452b03d1a 80 exit(EXIT_FAILURE);
Davidroid 0:e6a49a092e2a 81
Davidroid 0:e6a49a092e2a 82 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 83 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:e6a49a092e2a 84
Davidroid 8:cec4c2c03a27 85
Davidroid 8:cec4c2c03a27 86 /*----- Moving. -----*/
Davidroid 8:cec4c2c03a27 87
Davidroid 8:cec4c2c03a27 88 /* Printing to the console. */
Davidroid 9:a9e51320aee4 89 printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
Davidroid 8:cec4c2c03a27 90
Davidroid 8:cec4c2c03a27 91 /* Moving N steps in the forward direction. */
Davidroid 9:a9e51320aee4 92 motor1->Move(StepperMotor::FWD, STEPS >> 1);
Davidroid 9:a9e51320aee4 93 motor2->Move(StepperMotor::FWD, STEPS);
Davidroid 8:cec4c2c03a27 94
Davidroid 8:cec4c2c03a27 95 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 96 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 97 motor2->WaitWhileActive();
Davidroid 8:cec4c2c03a27 98
Davidroid 8:cec4c2c03a27 99 /* Getting current position. */
Davidroid 8:cec4c2c03a27 100 int position1 = motor1->GetPosition();
Davidroid 8:cec4c2c03a27 101 int position2 = motor2->GetPosition();
Davidroid 8:cec4c2c03a27 102
Davidroid 8:cec4c2c03a27 103 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 104 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 105
Davidroid 8:cec4c2c03a27 106 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 107 wait_ms(2000);
Davidroid 8:cec4c2c03a27 108
Davidroid 8:cec4c2c03a27 109
Davidroid 8:cec4c2c03a27 110 /*----- Moving. -----*/
Davidroid 8:cec4c2c03a27 111
Davidroid 8:cec4c2c03a27 112 /* Printing to the console. */
Davidroid 9:a9e51320aee4 113 printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
Davidroid 8:cec4c2c03a27 114
Davidroid 8:cec4c2c03a27 115
Davidroid 8:cec4c2c03a27 116 /* Moving N steps in the backward direction. */
Davidroid 9:a9e51320aee4 117 motor1->Move(StepperMotor::BWD, STEPS >> 1);
Davidroid 9:a9e51320aee4 118 motor2->Move(StepperMotor::BWD, STEPS);
Davidroid 8:cec4c2c03a27 119
Davidroid 8:cec4c2c03a27 120 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 121 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 122 motor2->WaitWhileActive();
Davidroid 8:cec4c2c03a27 123
Davidroid 8:cec4c2c03a27 124 /* Getting current position. */
Davidroid 8:cec4c2c03a27 125 position1 = motor1->GetPosition();
Davidroid 8:cec4c2c03a27 126 position2 = motor2->GetPosition();
Davidroid 8:cec4c2c03a27 127
Davidroid 8:cec4c2c03a27 128 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 129 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 130 printf("--> Setting Home.\r\n");
Davidroid 8:cec4c2c03a27 131
Davidroid 8:cec4c2c03a27 132 /* Setting the current position to be the home position. */
Davidroid 8:cec4c2c03a27 133 motor1->SetHome();
Davidroid 8:cec4c2c03a27 134 motor2->SetHome();
Davidroid 8:cec4c2c03a27 135
Davidroid 8:cec4c2c03a27 136 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 137 wait_ms(2000);
Davidroid 8:cec4c2c03a27 138
Davidroid 8:cec4c2c03a27 139
Davidroid 8:cec4c2c03a27 140 /*----- Going to a specified position. -----*/
Davidroid 8:cec4c2c03a27 141
Davidroid 8:cec4c2c03a27 142 /* Printing to the console. */
Davidroid 9:a9e51320aee4 143 printf("--> Going to position: M1 %d, M2 %d.\r\n", STEPS, STEPS >> 1);
Davidroid 8:cec4c2c03a27 144
Davidroid 8:cec4c2c03a27 145 /* Requesting to go to a specified position. */
Davidroid 9:a9e51320aee4 146 motor1->GoTo(STEPS);
Davidroid 9:a9e51320aee4 147 motor2->GoTo(STEPS >> 1);
Davidroid 8:cec4c2c03a27 148
Davidroid 8:cec4c2c03a27 149 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 150 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 151 motor2->WaitWhileActive();
Davidroid 8:cec4c2c03a27 152
Davidroid 8:cec4c2c03a27 153 /* Getting current position. */
Davidroid 8:cec4c2c03a27 154 position1 = motor1->GetPosition();
Davidroid 8:cec4c2c03a27 155 position2 = motor2->GetPosition();
Davidroid 8:cec4c2c03a27 156
Davidroid 8:cec4c2c03a27 157 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 158 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 159
Davidroid 8:cec4c2c03a27 160 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 161 wait_ms(2000);
Davidroid 8:cec4c2c03a27 162
Davidroid 8:cec4c2c03a27 163
Davidroid 8:cec4c2c03a27 164 /*----- Going Home. -----*/
Davidroid 8:cec4c2c03a27 165
Davidroid 8:cec4c2c03a27 166 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 167 printf("--> Going Home.\r\n");
Davidroid 8:cec4c2c03a27 168
Davidroid 8:cec4c2c03a27 169 /* Requesting to go to home. */
Davidroid 8:cec4c2c03a27 170 motor1->GoHome();
Davidroid 8:cec4c2c03a27 171 motor2->GoHome();
Davidroid 8:cec4c2c03a27 172
Davidroid 8:cec4c2c03a27 173 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 174 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 175 motor2->WaitWhileActive();
Davidroid 8:cec4c2c03a27 176
Davidroid 8:cec4c2c03a27 177 /* Getting current position. */
Davidroid 8:cec4c2c03a27 178 position1 = motor1->GetPosition();
Davidroid 8:cec4c2c03a27 179 position2 = motor2->GetPosition();
Davidroid 0:e6a49a092e2a 180
Davidroid 8:cec4c2c03a27 181 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 182 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 8:cec4c2c03a27 183
Davidroid 8:cec4c2c03a27 184 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 185 wait_ms(2000);
Davidroid 8:cec4c2c03a27 186
Davidroid 8:cec4c2c03a27 187
Davidroid 8:cec4c2c03a27 188 /*----- Running. -----*/
Davidroid 8:cec4c2c03a27 189
Davidroid 8:cec4c2c03a27 190 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 191 printf("--> M1 running backward, M2 running forward.\r\n");
Davidroid 8:cec4c2c03a27 192
Davidroid 8:cec4c2c03a27 193 /* Requesting to run backward. */
Davidroid 8:cec4c2c03a27 194 motor1->Run(StepperMotor::BWD);
Davidroid 8:cec4c2c03a27 195 motor2->Run(StepperMotor::FWD);
Davidroid 8:cec4c2c03a27 196
Davidroid 8:cec4c2c03a27 197 /* Waiting until delay has expired. */
Davidroid 8:cec4c2c03a27 198 wait_ms(6000);
Davidroid 8:cec4c2c03a27 199
Davidroid 8:cec4c2c03a27 200 /* Getting current speed. */
Davidroid 8:cec4c2c03a27 201 int speed1 = motor1->GetSpeed();
Davidroid 8:cec4c2c03a27 202 int speed2 = motor2->GetSpeed();
Davidroid 8:cec4c2c03a27 203
Davidroid 8:cec4c2c03a27 204 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 205 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 8:cec4c2c03a27 206
Davidroid 8:cec4c2c03a27 207 /*----- Increasing the speed while running. -----*/
Davidroid 8:cec4c2c03a27 208
Davidroid 8:cec4c2c03a27 209 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 210 printf("--> Increasing the speed while running.\r\n");
Davidroid 8:cec4c2c03a27 211
Davidroid 8:cec4c2c03a27 212 /* Increasing speed to 2400 step/s. */
Davidroid 8:cec4c2c03a27 213 motor1->SetMaxSpeed(2400);
Davidroid 8:cec4c2c03a27 214 motor2->SetMaxSpeed(2400);
Davidroid 8:cec4c2c03a27 215
Davidroid 8:cec4c2c03a27 216 /* Waiting until delay has expired. */
Davidroid 8:cec4c2c03a27 217 wait_ms(6000);
Davidroid 8:cec4c2c03a27 218
Davidroid 8:cec4c2c03a27 219 /* Getting current speed. */
Davidroid 8:cec4c2c03a27 220 speed1 = motor1->GetSpeed();
Davidroid 8:cec4c2c03a27 221 speed2 = motor2->GetSpeed();
Davidroid 8:cec4c2c03a27 222
Davidroid 8:cec4c2c03a27 223 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 224 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 8:cec4c2c03a27 225
Davidroid 8:cec4c2c03a27 226
Davidroid 8:cec4c2c03a27 227 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 228
Davidroid 8:cec4c2c03a27 229 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 230 printf("--> Decreasing the speed while running.\r\n");
Davidroid 8:cec4c2c03a27 231
Davidroid 8:cec4c2c03a27 232 /* Decreasing speed to 1200 step/s. */
Davidroid 8:cec4c2c03a27 233 motor1->SetMaxSpeed(1200);
Davidroid 8:cec4c2c03a27 234 motor2->SetMaxSpeed(1200);
Davidroid 8:cec4c2c03a27 235
Davidroid 8:cec4c2c03a27 236 /* Waiting until delay has expired. */
Davidroid 8:cec4c2c03a27 237 wait_ms(8000);
Davidroid 8:cec4c2c03a27 238
Davidroid 8:cec4c2c03a27 239 /* Getting current speed. */
Davidroid 8:cec4c2c03a27 240 speed1 = motor1->GetSpeed();
Davidroid 8:cec4c2c03a27 241 speed2 = motor2->GetSpeed();
Davidroid 8:cec4c2c03a27 242
Davidroid 8:cec4c2c03a27 243 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 244 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 8:cec4c2c03a27 245
Davidroid 8:cec4c2c03a27 246
Davidroid 8:cec4c2c03a27 247 /*----- Requiring hard-stop while running. -----*/
Davidroid 8:cec4c2c03a27 248
Davidroid 8:cec4c2c03a27 249 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 250 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 8:cec4c2c03a27 251
Davidroid 8:cec4c2c03a27 252 /* Requesting to immediatly stop. */
Davidroid 8:cec4c2c03a27 253 motor1->HardStop();
Davidroid 8:cec4c2c03a27 254 motor2->HardStop();
Davidroid 8:cec4c2c03a27 255
Davidroid 8:cec4c2c03a27 256 /* Waiting while the motor is active. */
Davidroid 8:cec4c2c03a27 257 motor1->WaitWhileActive();
Davidroid 8:cec4c2c03a27 258 motor2->WaitWhileActive();
Davidroid 8:cec4c2c03a27 259
Davidroid 8:cec4c2c03a27 260 /* Waiting 2 seconds. */
Davidroid 8:cec4c2c03a27 261 wait_ms(2000);
Davidroid 8:cec4c2c03a27 262
Davidroid 8:cec4c2c03a27 263
Davidroid 8:cec4c2c03a27 264 /*----- Infinite Loop. -----*/
Davidroid 8:cec4c2c03a27 265
Davidroid 8:cec4c2c03a27 266 /* Printing to the console. */
Davidroid 8:cec4c2c03a27 267 printf("--> Infinite Loop...\r\n");
Davidroid 8:cec4c2c03a27 268
Davidroid 8:cec4c2c03a27 269 /* Setting the current position to be the home position. */
Davidroid 8:cec4c2c03a27 270 motor1->SetHome();
Davidroid 8:cec4c2c03a27 271 motor2->SetHome();
Davidroid 8:cec4c2c03a27 272
Davidroid 8:cec4c2c03a27 273 /* Infinite Loop. */
Davidroid 8:cec4c2c03a27 274 while(1)
Davidroid 8:cec4c2c03a27 275 {
Davidroid 8:cec4c2c03a27 276 /* Requesting to go to a specified position. */
Davidroid 9:a9e51320aee4 277 motor1->GoTo(STEPS >> 1);
Davidroid 9:a9e51320aee4 278 motor2->GoTo(- (STEPS >> 1));
Davidroid 3:02d9ec4f88b2 279
Davidroid 0:e6a49a092e2a 280 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 281 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 282 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 283
Davidroid 0:e6a49a092e2a 284 /* Requesting to go to a specified position. */
Davidroid 9:a9e51320aee4 285 motor1->GoTo(- (STEPS >> 1));
Davidroid 9:a9e51320aee4 286 motor2->GoTo(STEPS >> 1);
Davidroid 0:e6a49a092e2a 287
Davidroid 0:e6a49a092e2a 288 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 289 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 290 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 291 }
Davidroid 0:e6a49a092e2a 292 }