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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    October 14th, 2015
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
00008  *          Motor Control Expansion Board: control of 2 motors.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 /* mbed specific header files. */
00043 #include "mbed.h"
00044 
00045 /* Helper header files. */
00046 #include "DevSPI.h"
00047 
00048 /* Component specific header files. */
00049 #include "L6474.h"
00050 
00051 
00052 /* Definitions ---------------------------------------------------------------*/
00053 
00054 /* Number of steps. */
00055 #define STEPS 3200
00056 
00057 /* Delay in milliseconds. */
00058 #define DELAY_1 2000
00059 #define DELAY_2 6000
00060 #define DELAY_3 8000
00061 
00062 /* Speed in pps (Pulses Per Second).
00063    In Full Step mode: 1 pps = 1 step/s).
00064    In 1/N Step Mode:  N pps = 1 step/s). */
00065 #define SPEED_1 2400
00066 #define SPEED_2 1200
00067 
00068 
00069 /* Variables -----------------------------------------------------------------*/
00070 
00071 /* Motor Control Component. */
00072 L6474 *motor1;
00073 L6474 *motor2;
00074 
00075 
00076 /* Main ----------------------------------------------------------------------*/
00077 
00078 int main()
00079 {
00080     /*----- Initialization. -----*/
00081 
00082     /* Initializing SPI bus. */
00083     DevSPI dev_spi(D11, D12, D13);
00084 
00085     /* Initializing Motor Control Components. */
00086     motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
00087     motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
00088     if (motor1->init() != COMPONENT_OK) {
00089         exit(EXIT_FAILURE);
00090     }
00091     if (motor2->init() != COMPONENT_OK) {
00092         exit(EXIT_FAILURE);
00093     }
00094 
00095     /* Printing to the console. */
00096     printf("Motor Control Application Example for 2 Motors\r\n\n");
00097 
00098 
00099     /*----- Moving. -----*/
00100 
00101     /* Printing to the console. */
00102     printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
00103 
00104     /* Moving N steps in the forward direction. */
00105     motor1->move(StepperMotor::FWD, STEPS >> 1);
00106     motor2->move(StepperMotor::FWD, STEPS);
00107 
00108     /* Waiting while the motor is active. */
00109     motor1->wait_while_active();
00110     motor2->wait_while_active();
00111 
00112     /* Getting current position. */
00113     int position1 = motor1->get_position();
00114     int position2 = motor2->get_position();
00115     
00116     /* Printing to the console. */
00117     printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
00118 
00119     /* Waiting 2 seconds. */
00120     wait_ms(DELAY_1);
00121 
00122     
00123     /*----- Moving. -----*/
00124     
00125     /* Printing to the console. */
00126     printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
00127 
00128     
00129     /* Moving N steps in the backward direction. */
00130     motor1->move(StepperMotor::BWD, STEPS >> 1);
00131     motor2->move(StepperMotor::BWD, STEPS);
00132     
00133     /* Waiting while the motor is active. */
00134     motor1->wait_while_active();
00135     motor2->wait_while_active();
00136 
00137     /* Getting current position. */
00138     position1 = motor1->get_position();
00139     position2 = motor2->get_position();
00140     
00141     /* Printing to the console. */
00142     printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
00143     printf("--> Setting Home.\r\n");
00144 
00145     /* Setting the current position to be the home position. */
00146     motor1->set_home();
00147     motor2->set_home();
00148 
00149     /* Waiting 2 seconds. */
00150     wait_ms(DELAY_1);
00151 
00152 
00153     /*----- Going to a specified position. -----*/
00154 
00155     /* Printing to the console. */
00156     printf("--> Going to position: M1 %d, M2 %d.\r\n", STEPS, STEPS >> 1);
00157     
00158     /* Requesting to go to a specified position. */
00159     motor1->go_to(STEPS);
00160     motor2->go_to(STEPS >> 1);
00161     
00162     /* Waiting while the motor is active. */
00163     motor1->wait_while_active();
00164     motor2->wait_while_active();
00165 
00166     /* Getting current position. */
00167     position1 = motor1->get_position();
00168     position2 = motor2->get_position();
00169     
00170     /* Printing to the console. */
00171     printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
00172     
00173     /* Waiting 2 seconds. */
00174     wait_ms(DELAY_1);
00175 
00176     
00177     /*----- Going Home. -----*/
00178 
00179     /* Printing to the console. */
00180     printf("--> Going Home.\r\n");
00181     
00182     /* Requesting to go to home. */
00183     motor1->go_home();
00184     motor2->go_home();
00185     
00186     /* Waiting while the motor is active. */
00187     motor1->wait_while_active();
00188     motor2->wait_while_active();
00189 
00190     /* Getting current position. */
00191     position1 = motor1->get_position();
00192     position2 = motor2->get_position();
00193 
00194     /* Printing to the console. */
00195     printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
00196 
00197     /* Waiting 2 seconds. */
00198     wait_ms(DELAY_1);
00199 
00200 
00201     /*----- Running. -----*/
00202 
00203     /* Printing to the console. */
00204     printf("--> M1 running backward, M2 running forward.\r\n");
00205 
00206     /* Requesting to run backward. */
00207     motor1->run(StepperMotor::BWD);
00208     motor2->run(StepperMotor::FWD);
00209 
00210     /* Waiting until delay has expired. */
00211     wait_ms(DELAY_2);
00212 
00213     /* Getting current speed. */
00214     int speed1 = motor1->get_speed();
00215     int speed2 = motor2->get_speed();
00216 
00217     /* Printing to the console. */
00218     printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
00219 
00220     /*----- Increasing the speed while running. -----*/
00221 
00222     /* Printing to the console. */
00223     printf("--> Increasing the speed while running.\r\n");
00224 
00225     /* Increasing the speed. */
00226     motor1->set_max_speed(SPEED_1);
00227     motor2->set_max_speed(SPEED_1);
00228 
00229     /* Waiting until delay has expired. */
00230     wait_ms(DELAY_2);
00231 
00232     /* Getting current speed. */
00233     speed1 = motor1->get_speed();
00234     speed2 = motor2->get_speed();
00235 
00236     /* Printing to the console. */
00237     printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
00238 
00239 
00240     /*----- Decreasing the speed while running. -----*/
00241 
00242     /* Printing to the console. */
00243     printf("--> Decreasing the speed while running.\r\n");
00244 
00245     /* Decreasing the speed. */
00246     motor1->set_max_speed(SPEED_2);
00247     motor2->set_max_speed(SPEED_2);
00248 
00249     /* Waiting until delay has expired. */
00250     wait_ms(DELAY_3);
00251 
00252     /* Getting current speed. */
00253     speed1 = motor1->get_speed();
00254     speed2 = motor2->get_speed();
00255 
00256     /* Printing to the console. */
00257     printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
00258 
00259 
00260     /*----- Requiring hard-stop while running. -----*/
00261 
00262     /* Printing to the console. */
00263     printf("--> Requiring hard-stop while running.\r\n");
00264 
00265     /* Requesting to immediatly stop. */
00266     motor1->hard_stop();
00267     motor2->hard_stop();
00268 
00269     /* Waiting while the motor is active. */
00270     motor1->wait_while_active();
00271     motor2->wait_while_active();
00272 
00273     /* Waiting 2 seconds. */
00274     wait_ms(DELAY_1);
00275 
00276 
00277     /*----- Infinite Loop. -----*/
00278 
00279     /* Printing to the console. */
00280     printf("--> Infinite Loop...\r\n");
00281 
00282     /* Setting the current position to be the home position. */
00283     motor1->set_home();
00284     motor2->set_home();
00285 
00286     /* Infinite Loop. */
00287     while(true) {
00288         /* Requesting to go to a specified position. */
00289         motor1->go_to(STEPS >> 1);
00290         motor2->go_to(- (STEPS >> 1));
00291 
00292         /* Waiting while the motor is active. */
00293         motor1->wait_while_active();
00294         motor2->wait_while_active();
00295 
00296         /* Requesting to go to a specified position. */
00297         motor1->go_to(- (STEPS >> 1));
00298         motor2->go_to(STEPS >> 1);
00299 
00300         /* Waiting while the motor is active. */
00301         motor1->wait_while_active();
00302         motor2->wait_while_active();
00303     }
00304 }