Stepper motor control for KYPHOS rig
Dependencies: mbed X_NUCLEO_IHM03A1 HX711
main.cpp
00001 #include "mbed.h" 00002 #include "DevSPI.h" 00003 00004 #include "PowerStep01.h" 00005 00006 powerstep01_init_u_t init = { 00007 /* common parameters */ 00008 .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t 00009 582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2 00010 582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2 00011 48, // Maximum speed in step/s, range 15.25 to 15610 steps/s 00012 0, // Minimum speed in step/s, range 0 to 976.3 steps/s 00013 POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t 00014 244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s 00015 POWERSTEP01_BOOST_MODE_OFF, // Boost of the amplitude square wave, enum powerstep01_BoostMode_t 00016 281.25, // Overcurrent threshold settings via enum powerstep01_OcdTh_t 00017 STEP_MODE_1_16, // Step mode settings via enum motorStepMode_t 00018 POWERSTEP01_SYNC_SEL_DISABLED, // Synch. Mode settings via enum powerstep01_SyncSel_t 00019 (POWERSTEP01_ALARM_EN_OVERCURRENT| 00020 POWERSTEP01_ALARM_EN_THERMAL_SHUTDOWN| 00021 POWERSTEP01_ALARM_EN_THERMAL_WARNING| 00022 POWERSTEP01_ALARM_EN_UVLO| 00023 POWERSTEP01_ALARM_EN_STALL_DETECTION| 00024 POWERSTEP01_ALARM_EN_SW_TURN_ON| 00025 POWERSTEP01_ALARM_EN_WRONG_NPERF_CMD), // Alarm settings via bitmap enum powerstep01_AlarmEn_t 00026 POWERSTEP01_IGATE_64mA, // Gate sink/source current via enum powerstep01_Igate_t 00027 POWERSTEP01_TBOOST_0ns, // Duration of the overboost phase during gate turn-off via enum powerstep01_Tboost_t 00028 POWERSTEP01_TCC_500ns, // Controlled current time via enum powerstep01_Tcc_t 00029 POWERSTEP01_WD_EN_DISABLE, // External clock watchdog, enum powerstep01_WdEn_t 00030 POWERSTEP01_TBLANK_375ns, // Duration of the blanking time via enum powerstep01_TBlank_t 00031 POWERSTEP01_TDT_125ns, // Duration of the dead time via enum powerstep01_Tdt_t 00032 /* current mode parameters */ 00033 328.12, // Hold torque in mV, range from 7.8mV to 1000 mV 00034 328.12, // Running torque in mV, range from 7.8mV to 1000 mV 00035 328.12, // Acceleration torque in mV, range from 7.8mV to 1000 mV 00036 328.12, // Deceleration torque in mV, range from 7.8mV to 1000 mV 00037 POWERSTEP01_TOFF_FAST_8us, //Maximum fast decay time , enum powerstep01_ToffFast_t 00038 POWERSTEP01_FAST_STEP_12us, //Maximum fall step time , enum powerstep01_FastStep_t 00039 3.0, // Minimum on-time in us, range 0.5us to 64us 00040 21.0, // Minimum off-time in us, range 0.5us to 64us 00041 POWERSTEP01_CONFIG_INT_16MHZ_OSCOUT_2MHZ, // Clock setting , enum powerstep01_ConfigOscMgmt_t 00042 POWERSTEP01_CONFIG_SW_HARD_STOP, // External switch hard stop interrupt mode, enum powerstep01_ConfigSwMode_t 00043 POWERSTEP01_CONFIG_TQ_REG_TVAL_USED, // External torque regulation enabling , enum powerstep01_ConfigEnTqReg_t 00044 POWERSTEP01_CONFIG_VS_COMP_DISABLE, // Motor Supply Voltage Compensation enabling , enum powerstep01_ConfigEnVscomp_t 00045 POWERSTEP01_CONFIG_OC_SD_DISABLE, // Over current shutwdown enabling, enum powerstep01_ConfigOcSd_t 00046 POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t 00047 POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t 00048 POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t 00049 POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t 00050 }; 00051 00052 DigitalIn button(USER_BUTTON); 00053 PowerStep01 *motor; 00054 00055 void flag_irq_handler(void) 00056 { 00057 /* Set ISR flag. */ 00058 motor->isrFlag = TRUE; 00059 /* Get the value of the status register. */ 00060 unsigned int statusRegister = motor->get_status(); 00061 printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); 00062 /* Check SW_F flag: if not set, the SW input is opened */ 00063 if ((statusRegister & POWERSTEP01_STATUS_SW_F ) != 0) { 00064 printf(" SW closed (connected to ground).\r\n"); 00065 } 00066 /* Check SW_EN bit */ 00067 if ((statusRegister & POWERSTEP01_STATUS_SW_EVN) == POWERSTEP01_STATUS_SW_EVN) { 00068 printf(" SW turn_on event.\r\n"); 00069 } 00070 /* Check Command Error flag: if set, the command received by SPI can't be */ 00071 /* performed. This occurs for instance when a move command is sent to the */ 00072 /* Powerstep01 while it is already running */ 00073 if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR) == POWERSTEP01_STATUS_CMD_ERROR) { 00074 printf(" Non-performable command detected.\r\n"); 00075 } 00076 /* Check UVLO flag: if not set, there is an undervoltage lock-out */ 00077 if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) { 00078 printf(" undervoltage lock-out.\r\n"); 00079 } 00080 /* Check thermal STATUS flags: if set, the thermal status is not normal */ 00081 if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) { 00082 //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown 00083 printf(" Thermal status: %d.\r\n", (statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11); 00084 } 00085 /* Check OCD flag: if not set, there is an overcurrent detection */ 00086 if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) { 00087 printf(" Overcurrent detection.\r\n"); 00088 } 00089 /* Reset ISR flag. */ 00090 motor->isrFlag = FALSE; 00091 } 00092 00093 /** 00094 * @brief This is an example of error handler. 00095 * @param[in] error Number of the error 00096 * @retval None 00097 * @note If needed, implement it, and then attach it: 00098 * + motor->attach_error_handler(&my_error_handler); 00099 */ 00100 void error_handler(uint16_t error) 00101 { 00102 printf("Error %d detected\r\n\n", error); 00103 while (true); 00104 } 00105 00106 /* Main ----------------------------------------------------------------------*/ 00107 00108 int main() 00109 { 00110 printf("STARTING MAIN PROGRAM\r\n"); 00111 00112 /* Initializing SPI bus. */ 00113 DevSPI dev_spi(D11, D12, D13); 00114 00115 /* Initializing Motor Control Component. */ 00116 motor = new PowerStep01(D2, D4, D8, D9, D10, dev_spi); 00117 if (motor->init(&init) != COMPONENT_OK) { 00118 exit(EXIT_FAILURE); 00119 } 00120 00121 /* Attaching and enabling an interrupt handler. */ 00122 motor->attach_flag_irq(&flag_irq_handler); 00123 motor->enable_flag_irq(); 00124 00125 /* Attaching an error handler */ 00126 motor->attach_error_handler(&error_handler); 00127 00128 printf("Press button to start motor control.\r\n"); 00129 while(button); 00130 00131 printf("--> Going to start position.\r\n"); 00132 motor->move(StepperMotor::FWD, 2000); 00133 motor->wait_while_active(); 00134 00135 while(button) { 00136 //----- Go to position 7680 00137 printf("--> Go to position -2000 steps.\r\n"); 00138 /* Request device to go to position 7680 */ 00139 motor->go_to(-2000); 00140 /* Wait for the motor ends moving */ 00141 motor->wait_while_active(); 00142 printf("--> Go to position 2000 steps.\r\n"); 00143 motor->go_to(2000); 00144 /* Wait for the motor ends moving */ 00145 motor->wait_while_active(); 00146 } 00147 00148 //----- Go Home 00149 /* Printing to the console. */ 00150 printf("--> Go to home position.\r\n"); 00151 00152 /* Request device to go to Home */ 00153 motor->go_home(); 00154 motor->wait_while_active(); 00155 /* Request a soft stop of device and keep the power bridges enabled */ 00156 motor->soft_hiz(); 00157 /* Wait for the motor of device ends moving */ 00158 motor->wait_while_active(); 00159 }
Generated on Sat Jul 16 2022 18:46:26 by 1.7.2