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