Simple program featuring a few API functions usage of the X_NUCLEO_IHM05A1 library.
Dependencies: X-NUCLEO-IHM05A1 mbed
Fork of HelloWorld_IHM05A1 by
main.cpp
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>© 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 00048 /* Definitions ---------------------------------------------------------------*/ 00049 #ifdef TARGET_NUCLEO_F334R8 00050 #define VREFA_PWM_PIN D11 00051 #define VREFB_PWM_PIN D9 00052 #elif TARGET_NUCLEO_F302R8 00053 #define VREFA_PWM_PIN D11 00054 #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */ 00055 #else 00056 #define VREFA_PWM_PIN D3 00057 #define VREFB_PWM_PIN D9 00058 #endif 00059 00060 /* Variables -----------------------------------------------------------------*/ 00061 00062 /* Initialization parameters of the motor connected to the expansion board. */ 00063 l6208_init_t init = 00064 { 00065 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes 00066 40, //Acceleration current torque in % (from 0 to 100) 00067 1500, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes 00068 30, //Deceleration current torque in % (from 0 to 100) 00069 1500, //Running speed in step/s or (1/16)th step/s for microstep modes 00070 50, //Running current torque in % (from 0 to 100) 00071 20, //Holding current torque in % (from 0 to 100) 00072 STEP_MODE_1_16, //Step mode via enum motorStepMode_t 00073 FAST_DECAY, //Decay mode via enum motorDecayMode_t 00074 0, //Dwelling time in ms 00075 FALSE, //Automatic HIZ STOP 00076 100000 //VREFA and VREFB PWM frequency (Hz) 00077 }; 00078 00079 /* Motor Control Component. */ 00080 L6208 *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 unit is in agreement to the step mode.\r\n"); 00126 printf(" The speed, acceleration or deceleration unit depend on the step mode:\r\n"); 00127 printf(" - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n"); 00128 printf(" - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n"); 00129 00130 //----- Initialization 00131 /* Initializing Motor Control Component. */ 00132 motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN); 00133 if (motor->init(&init) != COMPONENT_OK) { 00134 exit(EXIT_FAILURE); 00135 } 00136 00137 /* Attaching and enabling an interrupt handler. */ 00138 motor->attach_flag_irq(&my_flag_irq_handler); 00139 motor->enable_flag_irq(); 00140 00141 /* Attaching an error handler */ 00142 motor->attach_error_handler(&my_error_handler); 00143 00144 /* Printing to the console. */ 00145 printf("Motor Control Application Example for 1 Motor\r\n"); 00146 00147 //----- run the motor BACKWARD 00148 printf("--> Running the motor backward.\r\n"); 00149 motor->run(StepperMotor::BWD); 00150 00151 while (motor->get_status()!=STEADY) { 00152 /* Print reached speed to the console in step/s or microsteps/s */ 00153 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); 00154 wait_ms(50); 00155 } 00156 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); 00157 00158 /* Wait for 1 second */ 00159 wait_ms(1000); 00160 00161 //----- Decrease speed while running to one quarter of the previous speed 00162 motor->set_max_speed(motor->get_speed()>>2); 00163 00164 /* Wait until the motor starts decelerating */ 00165 while (motor->get_status()==STEADY); 00166 /* Wait and print speed while the motor is not steady running */ 00167 while (motor->get_status()!=STEADY) { 00168 /* Print reached speed to the console in step/s or microsteps/s */ 00169 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); 00170 wait_ms(50); 00171 } 00172 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); 00173 00174 /* Wait for 5 seconds */ 00175 wait_ms(5000); 00176 00177 //----- Soft stop required while running 00178 printf("--> Soft stop requested.\r\n"); 00179 motor->soft_stop(); 00180 00181 /* Wait for the motor of device ends moving */ 00182 motor->wait_while_active(); 00183 00184 /* Wait for 2 seconds */ 00185 wait_ms(2000); 00186 00187 //----- Change step mode to full step mode 00188 motor->set_step_mode(StepperMotor::STEP_MODE_FULL); 00189 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()); 00190 00191 /* Get current position of device and print to the console */ 00192 printf(" Position: %d.\r\n", motor->get_position()); 00193 00194 /* Set speed, acceleration and deceleration to scale with normal mode */ 00195 motor->set_max_speed(init.maxSpeedSps>>4); 00196 motor->set_acceleration(motor->get_acceleration()>>4); 00197 motor->set_deceleration(motor->get_deceleration()>>4); 00198 /* Print parameters to the console */ 00199 printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); 00200 printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); 00201 printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); 00202 printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); 00203 00204 //----- move of 200 steps in the FW direction 00205 printf("--> Moving forward 200 steps.\r\n"); 00206 motor->move(StepperMotor::FWD, 200); 00207 00208 /* Waiting while the motor is active. */ 00209 motor->wait_while_active(); 00210 00211 /* Get current position of device and print to the console */ 00212 printf(" Position: %d.\r\n", motor->get_position()); 00213 00214 /* Disable the power bridges */ 00215 motor->disable(); 00216 00217 /* Check that the power bridges are actually disabled */ 00218 if (motor->check_status_hw()!=0) { 00219 printf(" Motor driver disabled.\r\n"); 00220 } else { 00221 printf(" Failed to disable the motor driver.\r\n"); 00222 } 00223 00224 /* Wait for 2 seconds */ 00225 wait_ms(2000); 00226 00227 //----- Change step mode to 1/4 microstepping mode 00228 motor->set_step_mode(StepperMotor::STEP_MODE_1_4); 00229 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()); 00230 00231 /* Get current position of device and print to the console */ 00232 printf(" Position: %d.\r\n", motor->get_position()); 00233 00234 /* Set speed, acceleration and deceleration to scale with microstep mode */ 00235 motor->set_max_speed(motor->get_max_speed()<<4); 00236 motor->set_acceleration(motor->get_acceleration()<<4); 00237 motor->set_deceleration(motor->get_deceleration()<<4); 00238 /* Print parameters to the console */ 00239 printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); 00240 printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); 00241 printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); 00242 printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); 00243 00244 /* Request to go position 800 (quarter steps) */ 00245 motor->go_to(800); 00246 00247 /* Wait for the motor ends moving */ 00248 motor->wait_while_active(); 00249 00250 /* Get current position of device and print to the console */ 00251 printf(" Position: %d.\r\n", motor->get_position()); 00252 00253 /* Wait for 2 seconds */ 00254 wait_ms(2000); 00255 00256 //----- Restore step mode to its initialization value 00257 motor->set_step_mode((StepperMotor::step_mode_t)init.stepMode); 00258 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()); 00259 00260 /* Get current position of device and print to the console */ 00261 printf(" Position: %d.\r\n", motor->get_position()); 00262 00263 //----- Change decay mode 00264 motor->set_decay_mode(SLOW_DECAY); 00265 printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode()); 00266 00267 //----- Go to position -6400 00268 printf("--> Go to position -6400 steps.\r\n"); 00269 motor->go_to(-6400); 00270 00271 /* Wait for the motor ends moving */ 00272 motor->wait_while_active(); 00273 00274 /* Get current position of device and print to the console */ 00275 printf(" Position: %d.\r\n", motor->get_position()); 00276 00277 /* Wait for 2 seconds */ 00278 wait_ms(2000); 00279 00280 //----- Restore decay mode to its initialization value 00281 motor->set_decay_mode(init.decayMode); 00282 printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode()); 00283 00284 //----- Go Home 00285 printf("--> Go to home position.\r\n"); 00286 motor->go_home(); 00287 00288 /* Wait for the motor ends moving */ 00289 motor->wait_while_active(); 00290 00291 /* Wait for 1 second */ 00292 wait_ms(1000); 00293 00294 /* Infinite Loop. */ 00295 printf("--> Infinite Loop...\r\n"); 00296 //while (true) { 00297 int i; 00298 for (i = 0; i < 2; i++) { 00299 /* Request device to go position -3200 */ 00300 motor->go_to(-3200); 00301 00302 /* Waiting while the motor is active. */ 00303 motor->wait_while_active(); 00304 00305 /* Request device to go position 3200 */ 00306 motor->go_to(3200); 00307 00308 /* Waiting while the motor is active. */ 00309 motor->wait_while_active(); 00310 } 00311 } 00312 00313 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Sun Jul 17 2022 15:01:35 by 1.7.2