Simple program featuring a few API functions usage of the X_NUCLEO_IHM03A1 library.
Dependencies: X_NUCLEO_IHM03A1 mbed
Fork of HelloWorld_IHM03A1 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-IHM03A1 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 /* Helper header files. */ 00045 #include "DevSPI.h" 00046 00047 /* Component specific header files. */ 00048 #include "PowerStep01.h" 00049 00050 /* Variables -----------------------------------------------------------------*/ 00051 00052 /* Initialization parameters of the motor connected to the expansion board. */ 00053 /* Current mode. */ 00054 powerstep01_init_u_t init = 00055 { 00056 /* common parameters */ 00057 .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t 00058 582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2 00059 582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2 00060 488, // Maximum speed in step/s, range 15.25 to 15610 steps/s 00061 0, // Minimum speed in step/s, range 0 to 976.3 steps/s 00062 POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t 00063 244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s 00064 POWERSTEP01_BOOST_MODE_OFF, // Boost of the amplitude square wave, enum powerstep01_BoostMode_t 00065 281.25, // Overcurrent threshold settings via enum powerstep01_OcdTh_t 00066 STEP_MODE_1_16, // Step mode settings via enum motorStepMode_t 00067 POWERSTEP01_SYNC_SEL_DISABLED, // Synch. Mode settings via enum powerstep01_SyncSel_t 00068 (POWERSTEP01_ALARM_EN_OVERCURRENT| 00069 POWERSTEP01_ALARM_EN_THERMAL_SHUTDOWN| 00070 POWERSTEP01_ALARM_EN_THERMAL_WARNING| 00071 POWERSTEP01_ALARM_EN_UVLO| 00072 POWERSTEP01_ALARM_EN_STALL_DETECTION| 00073 POWERSTEP01_ALARM_EN_SW_TURN_ON| 00074 POWERSTEP01_ALARM_EN_WRONG_NPERF_CMD), // Alarm settings via bitmap enum powerstep01_AlarmEn_t 00075 POWERSTEP01_IGATE_64mA, // Gate sink/source current via enum powerstep01_Igate_t 00076 POWERSTEP01_TBOOST_0ns, // Duration of the overboost phase during gate turn-off via enum powerstep01_Tboost_t 00077 POWERSTEP01_TCC_500ns, // Controlled current time via enum powerstep01_Tcc_t 00078 POWERSTEP01_WD_EN_DISABLE, // External clock watchdog, enum powerstep01_WdEn_t 00079 POWERSTEP01_TBLANK_375ns, // Duration of the blanking time via enum powerstep01_TBlank_t 00080 POWERSTEP01_TDT_125ns, // Duration of the dead time via enum powerstep01_Tdt_t 00081 /* current mode parameters */ 00082 328.12, // Hold torque in mV, range from 7.8mV to 1000 mV 00083 328.12, // Running torque in mV, range from 7.8mV to 1000 mV 00084 328.12, // Acceleration torque in mV, range from 7.8mV to 1000 mV 00085 328.12, // Deceleration torque in mV, range from 7.8mV to 1000 mV 00086 POWERSTEP01_TOFF_FAST_8us, //Maximum fast decay time , enum powerstep01_ToffFast_t 00087 POWERSTEP01_FAST_STEP_12us, //Maximum fall step time , enum powerstep01_FastStep_t 00088 3.0, // Minimum on-time in us, range 0.5us to 64us 00089 21.0, // Minimum off-time in us, range 0.5us to 64us 00090 POWERSTEP01_CONFIG_INT_16MHZ_OSCOUT_2MHZ, // Clock setting , enum powerstep01_ConfigOscMgmt_t 00091 POWERSTEP01_CONFIG_SW_HARD_STOP, // External switch hard stop interrupt mode, enum powerstep01_ConfigSwMode_t 00092 POWERSTEP01_CONFIG_TQ_REG_TVAL_USED, // External torque regulation enabling , enum powerstep01_ConfigEnTqReg_t 00093 POWERSTEP01_CONFIG_VS_COMP_DISABLE, // Motor Supply Voltage Compensation enabling , enum powerstep01_ConfigEnVscomp_t 00094 POWERSTEP01_CONFIG_OC_SD_DISABLE, // Over current shutwdown enabling, enum powerstep01_ConfigOcSd_t 00095 POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t 00096 POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t 00097 POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t 00098 POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t 00099 }; 00100 00101 /* Motor Control Component. */ 00102 PowerStep01 *motor; 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(&my_flag_irq_handler); 00112 * + motor->enable_flag_irq(); 00113 * To disable it: 00114 * + motor->DisbleFlagIRQ(); 00115 */ 00116 void my_flag_irq_handler(void) 00117 { 00118 /* Set ISR flag. */ 00119 motor->isrFlag = TRUE; 00120 /* Get the value of the status register. */ 00121 unsigned int statusRegister = motor->get_status(); 00122 printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); 00123 /* Check SW_F flag: if not set, the SW input is opened */ 00124 if ((statusRegister & POWERSTEP01_STATUS_SW_F ) != 0) { 00125 printf(" SW closed (connected to ground).\r\n"); 00126 } 00127 /* Check SW_EN bit */ 00128 if ((statusRegister & POWERSTEP01_STATUS_SW_EVN) == POWERSTEP01_STATUS_SW_EVN) { 00129 printf(" SW turn_on event.\r\n"); 00130 } 00131 /* Check Command Error flag: if set, the command received by SPI can't be */ 00132 /* performed. This occurs for instance when a move command is sent to the */ 00133 /* Powerstep01 while it is already running */ 00134 if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR) == POWERSTEP01_STATUS_CMD_ERROR) { 00135 printf(" Non-performable command detected.\r\n"); 00136 } 00137 /* Check UVLO flag: if not set, there is an undervoltage lock-out */ 00138 if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) { 00139 printf(" undervoltage lock-out.\r\n"); 00140 } 00141 /* Check thermal STATUS flags: if set, the thermal status is not normal */ 00142 if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) { 00143 //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown 00144 printf(" Thermal status: %d.\r\n", (statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11); 00145 } 00146 /* Check OCD flag: if not set, there is an overcurrent detection */ 00147 if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) { 00148 printf(" Overcurrent detection.\r\n"); 00149 } 00150 /* Reset ISR flag. */ 00151 motor->isrFlag = FALSE; 00152 } 00153 00154 /** 00155 * @brief This is an example of error handler. 00156 * @param[in] error Number of the error 00157 * @retval None 00158 * @note If needed, implement it, and then attach it: 00159 * + motor->attach_error_handler(&my_error_handler); 00160 */ 00161 void my_error_handler(uint16_t error) 00162 { 00163 /* Printing to the console. */ 00164 printf("Error %d detected\r\n\n", error); 00165 00166 /* Infinite loop */ 00167 while (true) { 00168 } 00169 } 00170 00171 /* Main ----------------------------------------------------------------------*/ 00172 00173 int main() 00174 { 00175 /* Printing to the console. */ 00176 printf("STARTING MAIN PROGRAM\r\n"); 00177 printf(" Reminder:\r\n"); 00178 printf(" The position unit is in agreement to the step mode.\r\n"); 00179 printf(" The speed, acceleration or deceleration unit\r\n"); 00180 printf(" do not depend on the step mode and the step unit is a full step.\r\n"); 00181 00182 //----- Initialization 00183 /* Initializing SPI bus. */ 00184 DevSPI dev_spi(D11, D12, D13); 00185 00186 /* Initializing Motor Control Component. */ 00187 motor = new PowerStep01(D2, D4, D8, D9, D10, dev_spi); 00188 if (motor->init(&init) != COMPONENT_OK) { 00189 exit(EXIT_FAILURE); 00190 } 00191 00192 /* Attaching and enabling an interrupt handler. */ 00193 motor->attach_flag_irq(&my_flag_irq_handler); 00194 motor->enable_flag_irq(); 00195 00196 /* Attaching an error handler */ 00197 motor->attach_error_handler(&my_error_handler); 00198 00199 /* Printing to the console. */ 00200 printf("Motor Control Application Example for 1 Motor\r\n"); 00201 00202 //----- move of 16000 steps in the FW direction 00203 printf("--> Moving forward 16000 steps.\r\n"); 00204 motor->move(StepperMotor::FWD, 16000); 00205 00206 /* Waiting while the motor is active. */ 00207 motor->wait_while_active(); 00208 00209 /* Wait for 2 seconds */ 00210 wait_ms(2000); 00211 00212 //----- Go to position -6400 00213 printf("--> Go to position -6400 steps.\r\n"); 00214 00215 /* Request device to go to position -6400 */ 00216 motor->go_to(-6400); 00217 00218 /* Wait for the motor ends moving */ 00219 motor->wait_while_active(); 00220 00221 /* Get current position of device and print to the console */ 00222 printf(" Position: %d.\r\n", motor->get_position()); 00223 00224 /* Wait for 2 seconds */ 00225 wait_ms(2000); 00226 00227 //----- Go Home 00228 /* Printing to the console. */ 00229 printf("--> Go to home position.\r\n"); 00230 00231 /* Request device to go to Home */ 00232 motor->go_home(); 00233 00234 /* Wait for the motor ends moving */ 00235 motor->wait_while_active(); 00236 00237 /* Wait for 2 seconds */ 00238 wait_ms(2000); 00239 00240 //----- run the motor BACKWARD at 400 step/s 00241 printf("--> run the motor backward at 400 step/s.\r\n"); 00242 motor->run(StepperMotor::BWD,400); 00243 00244 //----- Get parameter example 00245 /* Wait for device reaches the targeted speed */ 00246 while ((motor->read_status_register() & POWERSTEP01_STATUS_MOT_STATUS) != POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD); 00247 00248 /* Record the reached speed in step/s and print to the console */ 00249 printf(" Reached Speed: %f step/s.\r\n", motor->get_analog_value(POWERSTEP01_SPEED)); 00250 00251 //----- Soft stopped required while running 00252 printf("--> Soft stop requested.\r\n"); 00253 00254 /* Request a soft stop of device and keep the power bridges enabled */ 00255 motor->soft_hiz(); 00256 00257 /* Wait for the motor of device ends moving */ 00258 motor->wait_while_active(); 00259 00260 /* Wait for 2 seconds */ 00261 wait_ms(2000); 00262 00263 /* Infinite Loop. */ 00264 printf("--> Infinite Loop...\r\n"); 00265 while (true) { 00266 /* Request device to go position -6400 */ 00267 motor->go_to(-6400); 00268 00269 /* Waiting while the motor is active. */ 00270 motor->wait_while_active(); 00271 00272 /* Request device to go position 6400 */ 00273 motor->go_to(6400); 00274 00275 /* Waiting while the motor is active. */ 00276 motor->wait_while_active(); 00277 } 00278 } 00279 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 17:00:07 by 1.7.2