Simple program featuring a few API functions usage of the X_NUCLEO_IHM06A1 library.

Dependencies:   X_NUCLEO_IHM06A1 mbed

Fork of HelloWorld_IHM06A1 by ST Expansion SW Team

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  IPC Rennes
00005  * @version V1.0.0
00006  * @date    May 26th, 2016
00007  * @brief   mbed simple application for the STMicroelectronics X-NUCLEO-IHM06A1
00008  *          Motor Control Expansion Board: control of 1 motor.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 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 /* Includes ------------------------------------------------------------------*/
00040 
00041 /* mbed specific header files. */
00042 #include "mbed.h"
00043 
00044 /* Component specific header files. */
00045 #include "STSpin220.h"
00046 
00047 /* Definitions ---------------------------------------------------------------*/
00048 #ifdef TARGET_NUCLEO_F302R8
00049 #define PWM_REF_PIN D11 /* HW mandatory patch: bridge manually D9 with D10 */
00050 #else
00051 #define PWM_REF_PIN D9
00052 #endif
00053 /* Uncomment the line below to enable the step clock monitoring duration */
00054 //#define STEP_CLOCK_HANDLER_DURATION_MONITORING
00055 #ifdef STEP_CLOCK_HANDLER_DURATION_MONITORING
00056 #define MONITORING_PIN D15
00057 #endif
00058 
00059 /* Variables -----------------------------------------------------------------*/
00060 
00061 /* Initialization parameters of the motor connected to the expansion board. */
00062 STSpin220_init_t init =
00063 {
00064   10000,           //Acceleration rate in pulse/s2 (must be greater than 0)
00065   10000,           //Deceleration rate in pulse/s2 (must be greater than 0)
00066   10000,           //Running speed in pulse/s (8 pulse/s < Maximum speed <= 10 000 pulse/s )
00067   400,             //Minimum speed in pulse/s (8 pulse/s <= Minimum speed < 10 000 pulse/s)
00068   20,              //Acceleration current torque in % (from 0 to 100)
00069   15,              //Deceleration current torque in % (from 0 to 100)
00070   10,              //Running current torque in % (from 0 to 100)
00071   25,              //Holding current torque in % (from 0 to 100)
00072   TRUE,            //Torque boost speed enable
00073   200,             //Torque boost speed threshold in fullstep/s
00074   STEP_MODE_1_32,  //Step mode via enum motorStepMode_t  
00075   HIZ_MODE,        //Automatic HIZ STOP
00076   100000           //REF frequency (Hz)
00077 };
00078 
00079 /* Motor Control Component. */
00080 STSpin220 *motor;
00081 
00082 /* Functions -----------------------------------------------------------------*/
00083 
00084 /**
00085  * @brief  This is an example of user handler for the flag interrupt.
00086  * @param  None
00087  * @retval None
00088  * @note   If needed, implement it, and then attach and enable it:
00089  *           + motor->attach_flag_irq(&my_flag_irq_handler);
00090  *           + motor->enable_flag_irq();
00091  *         To disable it:
00092  *           + motor->DisbleFlagIRQ();
00093  */
00094 void my_flag_irq_handler(void)
00095 {
00096   printf("    WARNING: \"FLAG\" interrupt triggered:\r\n");
00097   motor->disable();
00098   printf("    Motor disabled.\r\n\n");
00099 }
00100 
00101 /**
00102  * @brief  This is an example of error handler.
00103  * @param[in] error Number of the error
00104  * @retval None
00105  * @note   If needed, implement it, and then attach it:
00106  *           + motor->attach_error_handler(&my_error_handler);
00107  */
00108 void my_error_handler(uint16_t error)
00109 {
00110   /* Printing to the console. */
00111   printf("Error %d detected\r\n\n", error);
00112   
00113   /* Infinite loop */
00114   while (true) {
00115   }    
00116 }
00117 
00118 /* Main ----------------------------------------------------------------------*/
00119 
00120 int main()
00121 {
00122   /* Printing to the console. */
00123   printf("STARTING MAIN PROGRAM\r\n");
00124   printf("    Reminder:\r\n");
00125   printf("    The position, speed, acceleration and deceleration units\r\n");
00126   printf("    are in agreement to the step mode.\r\n");
00127   printf("    In example if the step mode is 1/32th,\r\n");
00128   printf("    the position is in 1/32th step,\r\n");
00129   printf("    the speed is in 1/32th step/s,\r\n");
00130   printf("    the acceleration and the deceleration are in 1/32th step/s^2.\r\n");
00131     
00132 //----- Initialization 
00133   /* Initializing Motor Control Component. */
00134 #ifdef STEP_CLOCK_HANDLER_DURATION_MONITORING
00135   motor = new STSpin220(D2, D8, D7, D5, D10, D3, PWM_REF_PIN, MONITORING_PIN);
00136 #else
00137   motor = new STSpin220(D2, D8, D7, D5, D10, D3, PWM_REF_PIN);
00138 #endif
00139 
00140   if (motor->init(&init) != COMPONENT_OK) {
00141     exit(EXIT_FAILURE);
00142   }
00143 
00144   /* Attaching and enabling an interrupt handler. */
00145   motor->attach_flag_irq(&my_flag_irq_handler);
00146   motor->enable_flag_irq();
00147     
00148   /* Attaching an error handler */
00149   motor->attach_error_handler(&my_error_handler);
00150 
00151   /* Printing to the console. */
00152   printf("Motor Control Application Example for 1 Motor\r\n");
00153   
00154 //----- Get the step mode after initialization 
00155   StepperMotor::step_mode_t myStepMode = motor->get_step_mode();
00156 
00157 //----- run the motor BACKWARD
00158   printf("--> Running the motor backward.\r\n");
00159   motor->run(StepperMotor::BWD);
00160   
00161   while (motor->get_status()!=STEADY)
00162   {
00163     /* Print reached speed to the console in step/s or microsteps/s */
00164     printf("    Reached Speed: %d step/s.\r\n", motor->get_speed());
00165     wait_ms(50);    
00166   }
00167   printf("    Reached Speed: %d step/s.\r\n", motor->get_speed());
00168      
00169   /* Wait for 1 second */  
00170   wait_ms(1000);
00171   
00172 //----- Decrease speed while running to one quarter of the previous speed
00173   printf("    Motor init step mode: %d\r\n", myStepMode);
00174   int currentStepMode = motor->get_step_mode();
00175   printf("    Motor current step mode: %d\r\n", currentStepMode);
00176   int newSpeed = (motor->get_speed()<<(myStepMode-currentStepMode))>>2;
00177   printf("    Set motor max speed to: %d step/s.\r\n", newSpeed);
00178   if (!motor->set_max_speed(newSpeed)) {
00179     printf("    Failed: target speed below min speed.\r\n");
00180     if (motor->set_max_speed(motor->get_min_speed())) {
00181       printf("    Motor max speed set to min speed: %d step/s.\r\n", motor->get_min_speed());
00182     } else {
00183       printf("    Failed: check all speed and boost setting.\r\n");
00184     }
00185   }
00186   
00187   /* Wait until the motor starts decelerating */
00188   while (motor->get_status()==STEADY);
00189   /* Wait and print speed while the motor is not steady running */
00190   while (motor->get_status()!=STEADY) {
00191     /* Print reached speed to the console in step/s or microsteps/s */
00192     printf("    Reached Speed: %d step/s.\r\n", motor->get_speed());
00193     wait_ms(50);    
00194   }
00195   printf("    Reached Speed: %d step/s.\r\n", motor->get_speed());  
00196 
00197   /* Wait for 5 seconds */  
00198   wait_ms(5000);
00199   
00200 //----- Soft stop required while running
00201   printf("--> Soft stop requested.\r\n");
00202   motor->soft_stop();
00203   
00204   /* Wait for the motor of device ends moving */  
00205   motor->wait_while_active();
00206 
00207   /* Wait for 2 seconds */
00208   wait_ms(2000);
00209 
00210 //----- Change step mode to full step mode
00211   motor->set_step_mode(StepperMotor::STEP_MODE_FULL);
00212   printf("    Motor step mode: %d\r\n", motor->get_step_mode());
00213   printf("    0:FS      1:1/2     2:1/4\r\n    3:1/8     4:1/16    5:1/32\r\n");
00214   printf("    6:1/64    7:1/128   8:1/256\r\n");
00215     
00216   /* Get current position of device and print to the console */
00217   printf("    Position: %d.\r\n", motor->get_position());
00218   
00219   /* Set speed, acceleration and deceleration to scale with full step mode */
00220   motor->set_min_speed(init.minSpeed>>myStepMode);  
00221   motor->set_max_speed(init.maxSpeed>>myStepMode);
00222   motor->set_acceleration(motor->get_acceleration()>>myStepMode);
00223   motor->set_deceleration(motor->get_deceleration()>>myStepMode);
00224   /* Print parameters to the console */
00225   printf("    Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());  
00226   printf("    Motor Max Speed: %d step/s.\r\n", motor->get_max_speed());
00227   printf("    Motor Acceleration: %d step/s.\r\n", motor->get_acceleration());
00228   printf("    Motor Deceleration: %d step/s.\r\n", motor->get_deceleration());
00229   
00230 //----- move of 200 steps in the FW direction
00231   printf("--> Moving forward 200 steps.\r\n");
00232   motor->move(StepperMotor::FWD, 200);
00233   
00234   /* Waiting while the motor is active. */
00235   motor->wait_while_active();
00236   
00237   /* Get current position of device and print to the console */
00238   printf("    Position: %d.\r\n", motor->get_position());
00239   
00240   /* Disable the power bridges */
00241   motor->disable();
00242   
00243   /* Check that the power bridges are actually disabled */
00244   if (motor->check_status_hw()!=0) {
00245     printf("    Motor driver disabled.\r\n");
00246   } else {
00247     printf("    Failed to disable the motor driver.\r\n");
00248   }
00249     
00250   /* Wait for 2 seconds */  
00251   wait_ms(2000);
00252   
00253 //----- Restore step mode to its initialization value
00254   motor->set_step_mode((StepperMotor::step_mode_t)init.stepMode);
00255   printf("    Motor step mode: %d\r\n", motor->get_step_mode());
00256   printf("    0:FS      1:1/2     2:1/4\r\n    3:1/8     4:1/16    5:1/32\r\n");
00257   printf("    6:1/64    7:1/128   8:1/256\r\n");
00258 
00259   /* Set speed, acceleration and deceleration to scale with microstep mode */
00260   motor->set_max_speed(motor->get_max_speed()<<myStepMode);
00261   motor->set_min_speed(motor->get_min_speed()<<myStepMode);
00262   motor->set_acceleration(motor->get_acceleration()<<myStepMode);
00263   motor->set_deceleration(motor->get_deceleration()<<myStepMode);
00264   /* Print parameters to the console */  
00265   printf("    Motor Max Speed: %d step/s.\r\n", motor->get_max_speed());
00266   printf("    Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());
00267   printf("    Motor Acceleration: %d step/s.\r\n", motor->get_acceleration());
00268   printf("    Motor Deceleration: %d step/s.\r\n", motor->get_deceleration());
00269 
00270   /* Get current position of device and print to the console */
00271   printf("    Position: %d.\r\n", motor->get_position());
00272   
00273 //----- Change torque
00274   motor->set_torque(ACC_TORQUE,30);
00275   motor->set_torque(DEC_TORQUE,20);
00276   printf("    Motor acceleration and deceleration torque changed to: %d and %d.\r\n", motor->get_torque(ACC_TORQUE), motor->get_torque(DEC_TORQUE));   
00277   
00278 //----- Go to position -6400
00279   printf("--> Go to position -6400 steps.\r\n");
00280   motor->go_to(-6400);
00281   
00282   /* Wait for the motor ends moving */
00283   motor->wait_while_active();
00284 
00285   /* Get current position of device and print to the console */
00286   printf("    Position: %d.\r\n", motor->get_position());
00287   
00288   /* Wait for 2 seconds */  
00289   wait_ms(2000);
00290   
00291 //----- Go Home
00292   printf("--> Go to home position.\r\n");
00293   motor->go_home();
00294  
00295   /* Wait for the motor ends moving */
00296   motor->wait_while_active();
00297   
00298   /* Wait for 1 second */
00299   wait_ms(1000);
00300   
00301   /* Infinite Loop. */
00302   printf("--> Infinite Loop...\r\n");
00303   while (true) {
00304     /* Request device to go position -3200 */
00305     motor->go_to(-3200);
00306     /* Waiting while the motor is active. */
00307     motor->wait_while_active();
00308     /* Request device to go position 3200 */
00309     motor->go_to(3200);
00310     /* Waiting while the motor is active. */
00311     motor->wait_while_active();
00312     wait_ms(500);
00313   }
00314 }
00315 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/