Simple code to run steps

Dependencies:   X-NUCLEO-IHM05A1 mbed

Fork of HelloWorld_IHM05A1 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  IPC Rennes
00005  * @version V1.0.0
00006  * @date    April 13th, 2016
00007  * @brief   mbed simple application for the STMicroelectronics X-NUCLEO-IHM05A1
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 "L6208.h"
00046 
00047 #define DELAY_STEPS 1000
00048 #define STEPS_PER 1 
00049 #define NUM_STEPS 400
00050 #define NUM_ROTATION 1
00051 int32_t steps=0;
00052 /* Definitions ---------------------------------------------------------------*/
00053 #ifdef TARGET_NUCLEO_F334R8
00054 #define VREFA_PWM_PIN D11
00055 #define VREFB_PWM_PIN D9
00056 #elif TARGET_NUCLEO_F302R8
00057 #define VREFA_PWM_PIN D11
00058 #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */
00059 #else
00060 #define VREFA_PWM_PIN D3
00061 #define VREFB_PWM_PIN D9
00062 #endif
00063 
00064 /* Variables -----------------------------------------------------------------*/
00065 
00066 /* Initialization parameters of the motor connected to the expansion board. */
00067 l6208_init_t init =
00068 {
00069   100,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
00070   50,              //Acceleration current torque in % (from 0 to 100)
00071   100,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
00072   50,              //Deceleration current torque in % (from 0 to 100)
00073   100,            //Running speed in step/s or (1/16)th step/s for microstep modes
00074   50,              //Running current torque in % (from 0 to 100)
00075   50,              //Holding current torque in % (from 0 to 100)
00076   STEP_MODE_HALF,  //Step mode via enum motorStepMode_t
00077   SLOW_DECAY,      //Decay mode via enum motorDecayMode_t
00078   10,               //Dwelling time in ms
00079   FALSE,           //Automatic HIZ STOP
00080   100000           //VREFA and VREFB PWM frequency (Hz)
00081 };
00082 
00083 /* Motor Control Component. */
00084 L6208 *motor;
00085 
00086 /* Functions -----------------------------------------------------------------*/
00087 
00088 /**
00089  * @brief  This is an example of user handler for the flag interrupt.
00090  * @param  None
00091  * @retval None
00092  * @note   If needed, implement it, and then attach and enable it:
00093  *           + motor->attach_flag_irq(&my_flag_irq_handler);
00094  *           + motor->enable_flag_irq();
00095  *         To disable it:
00096  *           + motor->DisbleFlagIRQ();
00097  */
00098 void my_flag_irq_handler(void)
00099 {
00100   printf("    WARNING: \"FLAG\" interrupt triggered:\r\n");
00101   motor->disable();
00102   printf("    Motor disabled.\r\n\n");
00103 }
00104 
00105 /**
00106  * @brief  This is an example of error handler.
00107  * @param[in] error Number of the error
00108  * @retval None
00109  * @note   If needed, implement it, and then attach it:
00110  *           + motor->attach_error_handler(&my_error_handler);
00111  */
00112 void my_error_handler(uint16_t error)
00113 {
00114   /* Printing to the console. */
00115   printf("Error %d detected\r\n\n", error);
00116   
00117   /* Infinite loop */
00118   while (true) {
00119   }    
00120 }
00121 
00122 /* Main ----------------------------------------------------------------------*/
00123 
00124 int main()
00125 {
00126   /* Printing to the console. */
00127   printf("STARTING MAIN PROGRAM\r\n");
00128   printf("    Reminder:\r\n");
00129   printf("    The position unit is in agreement to the step mode.\r\n");
00130   printf("    The speed, acceleration or deceleration unit depend on the step mode:\r\n");
00131   printf("    - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n");
00132   printf("    - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n");
00133     
00134 //----- Initialization 
00135   /* Initializing Motor Control Component. */
00136   motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
00137   if (motor->init(&init) != COMPONENT_OK) {
00138     exit(EXIT_FAILURE);
00139   }
00140 
00141   /* Attaching and enabling an interrupt handler. */
00142   motor->attach_flag_irq(&my_flag_irq_handler);
00143   motor->enable_flag_irq();
00144     
00145   /* Attaching an error handler */
00146   motor->attach_error_handler(&my_error_handler);
00147 
00148   /* Printing to the console. */
00149   printf("Run motor at 1/16 micro stepping");
00150 
00151   /* Print parameters to the console */  
00152   printf("    Motor Max Speed: %d step/s.\r\n", motor->get_max_speed());
00153   printf("    Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());
00154   printf("    Motor Acceleration: %d step/s.\r\n", motor->get_acceleration());
00155   printf("    Motor Deceleration: %d step/s.\r\n", motor->get_deceleration());
00156   
00157 
00158 //----- Change step mode to full step mode
00159   motor->set_step_mode(StepperMotor::STEP_MODE_HALF);
00160   printf("    Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode());
00161 
00162 ////----- run the motor BACKWARD
00163 //  printf("--> Running the motor backward.\r\n");
00164 //  motor->run(StepperMotor::BWD);
00165 //  
00166 //  
00167 //  // wait untill motor is at const speed
00168 //  while (motor->get_status()!=STEADY) {
00169 //    /* Print reached speed to the console in step/s or microsteps/s */
00170 //    printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
00171 //    wait_ms(50);    
00172 //  }
00173 //  printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
00174 //  
00175 //  // run continious   
00176 //  /* Wait for 1 second */  
00177 //  wait(60);
00178 //  
00179 ////----- Soft stop required while running
00180 //  printf("--> Soft stop requested.\r\n");
00181 //  motor->soft_stop();
00182 //  
00183 //  /* Wait for the motor of device ends moving */  
00184 //  motor->wait_while_active();
00185 
00186 
00187 while (steps<NUM_STEPS*NUM_ROTATION){
00188     //----- move of STEPS_PER steps in the FW direction
00189     
00190     motor->move(StepperMotor::FWD, STEPS_PER);
00191     /* Wait for DELAY_STEPS*/
00192     wait_ms(DELAY_STEPS);
00193     steps=motor->get_position();
00194     printf("--> %d steps.\r\n",steps);
00195   }
00196 }
00197 
00198 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/