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 Nov 20 18:10:04 2015 +0000
Revision:
0:5148e9486cf2
Child:
1:9f1974b0960d
mbed test application for the STMicrolectronics X-NUCLEO-IHM02A1 Motor Control Expansion Board: control of 2 motors.

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 0:5148e9486cf2 4 * @author Davide Aliprandi / AST
Davidroid 0:5148e9486cf2 5 * @version V1.0.0
Davidroid 0:5148e9486cf2 6 * @date November 4th, 2015
Davidroid 0:5148e9486cf2 7 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
Davidroid 0:5148e9486cf2 8 * Motor Control Expansion Board: control of 1 motor.
Davidroid 0:5148e9486cf2 9 * This application makes use of a C++ component architecture obtained
Davidroid 0:5148e9486cf2 10 * from the C component architecture through the Stm32CubeTOO tool.
Davidroid 0:5148e9486cf2 11 ******************************************************************************
Davidroid 0:5148e9486cf2 12 * @attention
Davidroid 0:5148e9486cf2 13 *
Davidroid 0:5148e9486cf2 14 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:5148e9486cf2 15 *
Davidroid 0:5148e9486cf2 16 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:5148e9486cf2 17 * are permitted provided that the following conditions are met:
Davidroid 0:5148e9486cf2 18 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:5148e9486cf2 19 * this list of conditions and the following disclaimer.
Davidroid 0:5148e9486cf2 20 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:5148e9486cf2 21 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:5148e9486cf2 22 * and/or other materials provided with the distribution.
Davidroid 0:5148e9486cf2 23 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:5148e9486cf2 24 * may be used to endorse or promote products derived from this software
Davidroid 0:5148e9486cf2 25 * without specific prior written permission.
Davidroid 0:5148e9486cf2 26 *
Davidroid 0:5148e9486cf2 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:5148e9486cf2 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:5148e9486cf2 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:5148e9486cf2 30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:5148e9486cf2 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:5148e9486cf2 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:5148e9486cf2 33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:5148e9486cf2 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:5148e9486cf2 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:5148e9486cf2 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:5148e9486cf2 37 *
Davidroid 0:5148e9486cf2 38 ******************************************************************************
Davidroid 0:5148e9486cf2 39 */
Davidroid 0:5148e9486cf2 40
Davidroid 0:5148e9486cf2 41
Davidroid 0:5148e9486cf2 42 /* Includes ------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 43
Davidroid 0:5148e9486cf2 44 /* mbed specific header files. */
Davidroid 0:5148e9486cf2 45 #include "mbed.h"
Davidroid 0:5148e9486cf2 46
Davidroid 0:5148e9486cf2 47 /* Helper header files. */
Davidroid 0:5148e9486cf2 48 #include "DevSPI.h"
Davidroid 0:5148e9486cf2 49
Davidroid 0:5148e9486cf2 50 /* Expansion Board specific header files. */
Davidroid 0:5148e9486cf2 51 #include "x_nucleo_ihm02a1_class.h"
Davidroid 0:5148e9486cf2 52
Davidroid 0:5148e9486cf2 53
Davidroid 0:5148e9486cf2 54 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 55
Davidroid 0:5148e9486cf2 56 /* Number of movements per revolution. */
Davidroid 0:5148e9486cf2 57 #define MPR_1 4
Davidroid 0:5148e9486cf2 58 #define MPR_2 8
Davidroid 0:5148e9486cf2 59
Davidroid 0:5148e9486cf2 60 /* Delay in milliseconds. */
Davidroid 0:5148e9486cf2 61 #define DELAY_1 500
Davidroid 0:5148e9486cf2 62 #define DELAY_2 2000
Davidroid 0:5148e9486cf2 63 #define DELAY_3 5000
Davidroid 0:5148e9486cf2 64
Davidroid 0:5148e9486cf2 65
Davidroid 0:5148e9486cf2 66 /* Variables -----------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 67
Davidroid 0:5148e9486cf2 68 /* Motor Control Expansion Board. */
Davidroid 0:5148e9486cf2 69 X_NUCLEO_IHM02A1 *x_nucleo_ihm02a1;
Davidroid 0:5148e9486cf2 70
Davidroid 0:5148e9486cf2 71
Davidroid 0:5148e9486cf2 72 /* Main ----------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 73
Davidroid 0:5148e9486cf2 74 int main()
Davidroid 0:5148e9486cf2 75 {
Davidroid 0:5148e9486cf2 76 /* Led. */
Davidroid 0:5148e9486cf2 77 DigitalOut led(LED1);
Davidroid 0:5148e9486cf2 78
Davidroid 0:5148e9486cf2 79 /* Initializing SPI bus. */
Davidroid 0:5148e9486cf2 80 DevSPI dev_spi(D11, D12, D3);
Davidroid 0:5148e9486cf2 81
Davidroid 0:5148e9486cf2 82 /* Initializing Motor Control Expansion Board. */
Davidroid 0:5148e9486cf2 83 x_nucleo_ihm02a1 = X_NUCLEO_IHM02A1::Instance(A4, D4, A2, &dev_spi);
Davidroid 0:5148e9486cf2 84
Davidroid 0:5148e9486cf2 85 /* Building a list of motors. */
Davidroid 0:5148e9486cf2 86 L6470 *motors[L6470DAISYCHAINSIZE] = {x_nucleo_ihm02a1->l6470_0, x_nucleo_ihm02a1->l6470_1};
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 0:5148e9486cf2 91 /* Main Loop. */
Davidroid 0:5148e9486cf2 92 while(true)
Davidroid 0:5148e9486cf2 93 {
Davidroid 0:5148e9486cf2 94 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 0:5148e9486cf2 95
Davidroid 0:5148e9486cf2 96 /* Printing to the console. */
Davidroid 0:5148e9486cf2 97 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 0:5148e9486cf2 98
Davidroid 0:5148e9486cf2 99 /* Doing a full revolution on each motor, one after the other. */
Davidroid 0:5148e9486cf2 100 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 101 for (int i = 0; i < MPR_1; i++)
Davidroid 0:5148e9486cf2 102 {
Davidroid 0:5148e9486cf2 103 /* Computing the number of steps. */
Davidroid 0:5148e9486cf2 104 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 0:5148e9486cf2 105
Davidroid 0:5148e9486cf2 106 /* Moving. */
Davidroid 0:5148e9486cf2 107 motors[m]->Move(StepperMotor::FWD, steps);
Davidroid 0:5148e9486cf2 108
Davidroid 0:5148e9486cf2 109 /* Waiting while active. */
Davidroid 0:5148e9486cf2 110 motors[m]->WaitWhileActive();
Davidroid 0:5148e9486cf2 111
Davidroid 0:5148e9486cf2 112 /* Waiting. */
Davidroid 0:5148e9486cf2 113 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 114 }
Davidroid 0:5148e9486cf2 115
Davidroid 0:5148e9486cf2 116 /* Waiting. */
Davidroid 0:5148e9486cf2 117 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 118
Davidroid 0:5148e9486cf2 119
Davidroid 0:5148e9486cf2 120 /*----- Running together for a certain amount of time. -----*/
Davidroid 0:5148e9486cf2 121
Davidroid 0:5148e9486cf2 122 /* Printing to the console. */
Davidroid 0:5148e9486cf2 123 printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 124
Davidroid 0:5148e9486cf2 125 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 0:5148e9486cf2 126 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 127 motors[m]->PrepareRun(StepperMotor::BWD, 10000);
Davidroid 0:5148e9486cf2 128
Davidroid 0:5148e9486cf2 129 /* Performing the action on each motor at the same time. */
Davidroid 0:5148e9486cf2 130 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 131 motors[m]->PerformAction();
Davidroid 0:5148e9486cf2 132
Davidroid 0:5148e9486cf2 133 /* Waiting. */
Davidroid 0:5148e9486cf2 134 wait_ms(DELAY_3);
Davidroid 0:5148e9486cf2 135
Davidroid 0:5148e9486cf2 136
Davidroid 0:5148e9486cf2 137 /*----- Hard Stop. -----*/
Davidroid 0:5148e9486cf2 138
Davidroid 0:5148e9486cf2 139 /* Printing to the console. */
Davidroid 0:5148e9486cf2 140 printf("--> Hard Stop.\r\n");
Davidroid 0:5148e9486cf2 141
Davidroid 0:5148e9486cf2 142 /* Preparing each motor to perform a hard stop. */
Davidroid 0:5148e9486cf2 143 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 144 motors[m]->PrepareHardStop();
Davidroid 0:5148e9486cf2 145
Davidroid 0:5148e9486cf2 146 /* Performing the action on each motor at the same time. */
Davidroid 0:5148e9486cf2 147 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 148 motors[m]->PerformAction();
Davidroid 0:5148e9486cf2 149
Davidroid 0:5148e9486cf2 150 /* Waiting. */
Davidroid 0:5148e9486cf2 151 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 152
Davidroid 0:5148e9486cf2 153
Davidroid 0:5148e9486cf2 154 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 0:5148e9486cf2 155
Davidroid 0:5148e9486cf2 156 /* Printing to the console. */
Davidroid 0:5148e9486cf2 157 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 0:5148e9486cf2 158
Davidroid 0:5148e9486cf2 159 /* Doing a full revolution on each motor, one after the other. */
Davidroid 0:5148e9486cf2 160 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 161 for (int i = 0; i < MPR_2; i++)
Davidroid 0:5148e9486cf2 162 {
Davidroid 0:5148e9486cf2 163 /* Computing the number of steps. */
Davidroid 0:5148e9486cf2 164 int steps = (int) (((int) X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].fullstepsperrevolution * pow(2.0f, X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].step_sel)) / MPR_2);
Davidroid 0:5148e9486cf2 165
Davidroid 0:5148e9486cf2 166 /* Moving. */
Davidroid 0:5148e9486cf2 167 motors[m]->Move(StepperMotor::FWD, steps);
Davidroid 0:5148e9486cf2 168
Davidroid 0:5148e9486cf2 169 /* Waiting while active. */
Davidroid 0:5148e9486cf2 170 motors[m]->WaitWhileActive();
Davidroid 0:5148e9486cf2 171
Davidroid 0:5148e9486cf2 172 /* Waiting. */
Davidroid 0:5148e9486cf2 173 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 174 }
Davidroid 0:5148e9486cf2 175
Davidroid 0:5148e9486cf2 176 /* Waiting. */
Davidroid 0:5148e9486cf2 177 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 178
Davidroid 0:5148e9486cf2 179
Davidroid 0:5148e9486cf2 180 /*----- High Impedance State. -----*/
Davidroid 0:5148e9486cf2 181
Davidroid 0:5148e9486cf2 182 /* Printing to the console. */
Davidroid 0:5148e9486cf2 183 printf("--> High Impedance State.\r\n");
Davidroid 0:5148e9486cf2 184
Davidroid 0:5148e9486cf2 185 /* Preparing each motor to set High Impedance State. */
Davidroid 0:5148e9486cf2 186 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 187 motors[m]->PrepareHardHiZ();
Davidroid 0:5148e9486cf2 188
Davidroid 0:5148e9486cf2 189 /* Performing the action on each motor at the same time. */
Davidroid 0:5148e9486cf2 190 for (int m = 0; m < 2; m++)
Davidroid 0:5148e9486cf2 191 motors[m]->PerformAction();
Davidroid 0:5148e9486cf2 192
Davidroid 0:5148e9486cf2 193 /* Waiting. */
Davidroid 0:5148e9486cf2 194 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 195
Davidroid 0:5148e9486cf2 196
Davidroid 0:5148e9486cf2 197 /*----- Led Blinking. -----*/
Davidroid 0:5148e9486cf2 198
Davidroid 0:5148e9486cf2 199 /* Led Blinking. */
Davidroid 0:5148e9486cf2 200 led = 1;
Davidroid 0:5148e9486cf2 201 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 202 led = 0;
Davidroid 0:5148e9486cf2 203 }
Davidroid 0:5148e9486cf2 204 }