Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_IHM01A1
Fork of HelloWorld_IHM01A1 by
main.cpp
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 1 motor. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© 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 #include "rtos.h" 00045 00046 /* Helper header files. */ 00047 #include "DevSPI.h" 00048 00049 /* Component specific header files. */ 00050 #include "L6474.h" 00051 00052 00053 /* Definitions ---------------------------------------------------------------*/ 00054 00055 /* Number of steps. */ 00056 #define STEPS_1 (400 * 8) /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */ 00057 00058 /* Delay in milliseconds. */ 00059 #define DELAY_1 1000 00060 #define DELAY_2 2000 00061 #define DELAY_3 6000 00062 #define DELAY_4 8000 00063 00064 /* Speed in pps (Pulses Per Second). 00065 In Full Step mode: 1 pps = 1 step/s). 00066 In 1/N Step Mode: N pps = 1 step/s). */ 00067 #define SPEED_1 2400 00068 #define SPEED_2 1200 00069 00070 00071 /* Variables -----------------------------------------------------------------*/ 00072 00073 /* Initialization parameters. */ 00074 L6474_init_t init = { 00075 160, /* Acceleration rate in pps^2. Range: (0..+inf). */ 00076 160, /* Deceleration rate in pps^2. Range: (0..+inf). */ 00077 1600, /* Maximum speed in pps. Range: (30..10000]. */ 00078 800, /* Minimum speed in pps. Range: [30..10000). */ 00079 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ 00080 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */ 00081 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ 00082 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ 00083 L6474_STEP_SEL_1_8, /* Step selection (STEP_SEL field of STEP_MODE register). */ 00084 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ 00085 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ 00086 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ 00087 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ 00088 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ 00089 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ 00090 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ 00091 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ 00092 L6474_ALARM_EN_OVERCURRENT | 00093 L6474_ALARM_EN_THERMAL_SHUTDOWN | 00094 L6474_ALARM_EN_THERMAL_WARNING | 00095 L6474_ALARM_EN_UNDERVOLTAGE | 00096 L6474_ALARM_EN_SW_TURN_ON | 00097 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ 00098 }; 00099 00100 /* Motor Control Component. */ 00101 L6474 *motor; 00102 00103 00104 /* Functions -----------------------------------------------------------------*/ 00105 00106 /** 00107 * @brief This is an example of user handler for the flag interrupt. 00108 * @param None 00109 * @retval None 00110 * @note If needed, implement it, and then attach and enable it: 00111 * + motor->attach_flag_irq(&flag_irq_handler); 00112 * + motor->enable_flag_irq(); 00113 * To disable it: 00114 * + motor->disble_flag_irq(); 00115 */ 00116 void flag_irq_handler(void) 00117 { 00118 /* Set ISR flag. */ 00119 motor->isr_flag = TRUE; 00120 00121 /* Get the value of the status register. */ 00122 unsigned int status = motor->get_status(); 00123 00124 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ 00125 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */ 00126 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) { 00127 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n"); 00128 } 00129 00130 /* Reset ISR flag. */ 00131 motor->isr_flag = FALSE; 00132 } 00133 00134 00135 /* Main ----------------------------------------------------------------------*/ 00136 00137 int main() 00138 { 00139 /*----- Initialization. -----*/ 00140 00141 /* Initializing SPI bus. */ 00142 DevSPI dev_spi(D11, D12, D13); 00143 00144 /* Initializing Motor Control Component. */ 00145 motor = new L6474(D2, D8, D7, D9, D10, dev_spi); 00146 if (motor->init(&init) != COMPONENT_OK) { 00147 exit(EXIT_FAILURE); 00148 } 00149 00150 /* Attaching and enabling interrupt handlers. */ 00151 motor->attach_flag_irq(&flag_irq_handler); 00152 motor->enable_flag_irq(); 00153 00154 /* Printing to the console. */ 00155 printf("Motor Control Application Example for 1 Motor\r\n\n"); 00156 00157 00158 /*----- Moving. -----*/ 00159 00160 /* Printing to the console. */ 00161 printf("--> Moving forward %d steps.\r\n", STEPS_1); 00162 00163 /* Moving N steps in the forward direction. */ 00164 motor->move(StepperMotor::FWD, STEPS_1); 00165 00166 /* Waiting while the motor is active. */ 00167 motor->wait_while_active(); 00168 00169 /* Getting current position. */ 00170 int position = motor->get_position(); 00171 00172 /* Printing to the console. */ 00173 printf(" Position: %d.\r\n", position); 00174 00175 /* Waiting. */ 00176 wait_ms(DELAY_1); 00177 00178 00179 /*----- Changing the motor setting. -----*/ 00180 00181 /* Printing to the console. */ 00182 printf("--> Setting Torque Regulation Current to 500[mA].\r\n"); 00183 00184 /* Increasing the torque regulation current to 500[mA]. */ 00185 motor->set_parameter(L6474_TVAL, 500); 00186 00187 /* Printing to the console. */ 00188 printf("--> Doubling the microsteps.\r\n"); 00189 00190 /* Doubling the microsteps. */ 00191 if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_1_16)) { 00192 printf(" Step Mode not allowed.\r\n"); 00193 } 00194 00195 /* Waiting. */ 00196 wait_ms(DELAY_1); 00197 00198 /* Printing to the console. */ 00199 printf("--> Setting Home.\r\n"); 00200 00201 /* Setting the current position to be the home position. */ 00202 motor->set_home(); 00203 00204 /* Getting current position. */ 00205 position = motor->get_position(); 00206 00207 /* Printing to the console. */ 00208 printf(" Position: %d.\r\n", position); 00209 00210 /* Waiting. */ 00211 wait_ms(DELAY_2); 00212 00213 00214 /*----- Moving. -----*/ 00215 00216 /* Printing to the console. */ 00217 printf("--> Moving backward %d steps.\r\n", STEPS_1); 00218 00219 /* Moving N steps in the backward direction. */ 00220 motor->move(StepperMotor::BWD, STEPS_1); 00221 00222 /* Waiting while the motor is active. */ 00223 motor->wait_while_active(); 00224 00225 /* Getting current position. */ 00226 position = motor->get_position(); 00227 00228 /* Printing to the console. */ 00229 printf(" Position: %d.\r\n", position); 00230 00231 /* Waiting. */ 00232 wait_ms(DELAY_1); 00233 00234 00235 /*----- Going to a specified position. -----*/ 00236 00237 /* Printing to the console. */ 00238 printf("--> Going to position %d.\r\n", STEPS_1); 00239 00240 /* Requesting to go to a specified position. */ 00241 motor->go_to(STEPS_1); 00242 00243 /* Waiting while the motor is active. */ 00244 motor->wait_while_active(); 00245 00246 /* Getting current position. */ 00247 position = motor->get_position(); 00248 00249 /* Printing to the console. */ 00250 printf(" Position: %d.\r\n", position); 00251 00252 /* Waiting. */ 00253 wait_ms(DELAY_2); 00254 00255 00256 /*----- Going Home. -----*/ 00257 00258 /* Printing to the console. */ 00259 printf("--> Going Home.\r\n"); 00260 00261 /* Requesting to go to home. */ 00262 motor->go_home(); 00263 00264 /* Waiting while the motor is active. */ 00265 motor->wait_while_active(); 00266 00267 /* Getting current position. */ 00268 position = motor->get_position(); 00269 00270 /* Printing to the console. */ 00271 printf(" Position: %d.\r\n", position); 00272 00273 /* Waiting. */ 00274 wait_ms(DELAY_2); 00275 00276 00277 /*----- Running. -----*/ 00278 00279 /* Printing to the console. */ 00280 printf("--> Running backward for %d seconds.\r\n", DELAY_3 / 1000); 00281 00282 /* Requesting to run backward. */ 00283 motor->run(StepperMotor::BWD); 00284 00285 /* Waiting. */ 00286 wait_ms(DELAY_3); 00287 00288 /* Getting current speed. */ 00289 int speed = motor->get_speed(); 00290 00291 /* Printing to the console. */ 00292 printf(" Speed: %d.\r\n", speed); 00293 00294 /*----- Increasing the speed while running. -----*/ 00295 00296 /* Printing to the console. */ 00297 printf("--> Increasing the speed while running again for %d seconds.\r\n", DELAY_3 / 1000); 00298 00299 /* Increasing the speed. */ 00300 motor->set_max_speed(SPEED_1); 00301 00302 /* Waiting. */ 00303 wait_ms(DELAY_3); 00304 00305 /* Getting current speed. */ 00306 speed = motor->get_speed(); 00307 00308 /* Printing to the console. */ 00309 printf(" Speed: %d.\r\n", speed); 00310 00311 00312 /*----- Decreasing the speed while running. -----*/ 00313 00314 /* Printing to the console. */ 00315 printf("--> Decreasing the speed while running again for %d seconds.\r\n", DELAY_4 / 1000); 00316 00317 /* Decreasing the speed. */ 00318 motor->set_max_speed(SPEED_2); 00319 00320 /* Waiting. */ 00321 wait_ms(DELAY_4); 00322 00323 /* Getting current speed. */ 00324 speed = motor->get_speed(); 00325 00326 /* Printing to the console. */ 00327 printf(" Speed: %d.\r\n", speed); 00328 00329 00330 /*----- Hard Stop. -----*/ 00331 00332 /* Printing to the console. */ 00333 printf("--> Hard Stop.\r\n"); 00334 00335 /* Requesting to immediatly stop. */ 00336 motor->hard_stop(); 00337 00338 /* Waiting while the motor is active. */ 00339 motor->wait_while_active(); 00340 00341 /* Waiting. */ 00342 wait_ms(DELAY_2); 00343 00344 00345 /*----- Infinite Loop. -----*/ 00346 00347 /* Printing to the console. */ 00348 printf("--> Infinite Loop...\r\n"); 00349 00350 /* Setting the current position to be the home position. */ 00351 motor->set_home(); 00352 00353 /* Infinite Loop. */ 00354 while (true) { 00355 /* Requesting to go to a specified position. */ 00356 motor->go_to(STEPS_1 >> 1); 00357 00358 /* Waiting while the motor is active. */ 00359 motor->wait_while_active(); 00360 00361 /* Requesting to go to a specified position. */ 00362 motor->go_to(- (STEPS_1 >> 1)); 00363 00364 /* Waiting while the motor is active. */ 00365 motor->wait_while_active(); 00366 } 00367 }
Generated on Sat Jul 16 2022 03:12:15 by
1.7.2
