Stepper motor control for KYPHOS rig
Dependencies: mbed X_NUCLEO_IHM03A1 HX711
main.cpp@0:1cb05bc2807c, 2020-02-11 (annotated)
- Committer:
- paullj
- Date:
- Tue Feb 11 16:02:22 2020 +0000
- Revision:
- 0:1cb05bc2807c
- Child:
- 1:5822a95491d3
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
paullj | 0:1cb05bc2807c | 1 | #include "mbed.h" |
paullj | 0:1cb05bc2807c | 2 | #include "PowerStep01.h" |
paullj | 0:1cb05bc2807c | 3 | |
paullj | 0:1cb05bc2807c | 4 | powerstep01_init_u_t init = { |
paullj | 0:1cb05bc2807c | 5 | /* common parameters */ |
paullj | 0:1cb05bc2807c | 6 | .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t |
paullj | 0:1cb05bc2807c | 7 | 582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2 |
paullj | 0:1cb05bc2807c | 8 | 582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2 |
paullj | 0:1cb05bc2807c | 9 | 488, // Maximum speed in step/s, range 15.25 to 15610 steps/s |
paullj | 0:1cb05bc2807c | 10 | 0, // Minimum speed in step/s, range 0 to 976.3 steps/s |
paullj | 0:1cb05bc2807c | 11 | POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t |
paullj | 0:1cb05bc2807c | 12 | 244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s |
paullj | 0:1cb05bc2807c | 13 | POWERSTEP01_BOOST_MODE_OFF, // Boost of the amplitude square wave, enum powerstep01_BoostMode_t |
paullj | 0:1cb05bc2807c | 14 | 281.25, // Overcurrent threshold settings via enum powerstep01_OcdTh_t |
paullj | 0:1cb05bc2807c | 15 | STEP_MODE_1_16, // Step mode settings via enum motorStepMode_t |
paullj | 0:1cb05bc2807c | 16 | POWERSTEP01_SYNC_SEL_DISABLED, // Synch. Mode settings via enum powerstep01_SyncSel_t |
paullj | 0:1cb05bc2807c | 17 | (POWERSTEP01_ALARM_EN_OVERCURRENT| |
paullj | 0:1cb05bc2807c | 18 | POWERSTEP01_ALARM_EN_THERMAL_SHUTDOWN| |
paullj | 0:1cb05bc2807c | 19 | POWERSTEP01_ALARM_EN_THERMAL_WARNING| |
paullj | 0:1cb05bc2807c | 20 | POWERSTEP01_ALARM_EN_UVLO| |
paullj | 0:1cb05bc2807c | 21 | POWERSTEP01_ALARM_EN_STALL_DETECTION| |
paullj | 0:1cb05bc2807c | 22 | POWERSTEP01_ALARM_EN_SW_TURN_ON| |
paullj | 0:1cb05bc2807c | 23 | POWERSTEP01_ALARM_EN_WRONG_NPERF_CMD), // Alarm settings via bitmap enum powerstep01_AlarmEn_t |
paullj | 0:1cb05bc2807c | 24 | POWERSTEP01_IGATE_64mA, // Gate sink/source current via enum powerstep01_Igate_t |
paullj | 0:1cb05bc2807c | 25 | POWERSTEP01_TBOOST_0ns, // Duration of the overboost phase during gate turn-off via enum powerstep01_Tboost_t |
paullj | 0:1cb05bc2807c | 26 | POWERSTEP01_TCC_500ns, // Controlled current time via enum powerstep01_Tcc_t |
paullj | 0:1cb05bc2807c | 27 | POWERSTEP01_WD_EN_DISABLE, // External clock watchdog, enum powerstep01_WdEn_t |
paullj | 0:1cb05bc2807c | 28 | POWERSTEP01_TBLANK_375ns, // Duration of the blanking time via enum powerstep01_TBlank_t |
paullj | 0:1cb05bc2807c | 29 | POWERSTEP01_TDT_125ns, // Duration of the dead time via enum powerstep01_Tdt_t |
paullj | 0:1cb05bc2807c | 30 | /* current mode parameters */ |
paullj | 0:1cb05bc2807c | 31 | 328.12, // Hold torque in mV, range from 7.8mV to 1000 mV |
paullj | 0:1cb05bc2807c | 32 | 328.12, // Running torque in mV, range from 7.8mV to 1000 mV |
paullj | 0:1cb05bc2807c | 33 | 328.12, // Acceleration torque in mV, range from 7.8mV to 1000 mV |
paullj | 0:1cb05bc2807c | 34 | 328.12, // Deceleration torque in mV, range from 7.8mV to 1000 mV |
paullj | 0:1cb05bc2807c | 35 | POWERSTEP01_TOFF_FAST_8us, //Maximum fast decay time , enum powerstep01_ToffFast_t |
paullj | 0:1cb05bc2807c | 36 | POWERSTEP01_FAST_STEP_12us, //Maximum fall step time , enum powerstep01_FastStep_t |
paullj | 0:1cb05bc2807c | 37 | 3.0, // Minimum on-time in us, range 0.5us to 64us |
paullj | 0:1cb05bc2807c | 38 | 21.0, // Minimum off-time in us, range 0.5us to 64us |
paullj | 0:1cb05bc2807c | 39 | POWERSTEP01_CONFIG_INT_16MHZ_OSCOUT_2MHZ, // Clock setting , enum powerstep01_ConfigOscMgmt_t |
paullj | 0:1cb05bc2807c | 40 | POWERSTEP01_CONFIG_SW_HARD_STOP, // External switch hard stop interrupt mode, enum powerstep01_ConfigSwMode_t |
paullj | 0:1cb05bc2807c | 41 | POWERSTEP01_CONFIG_TQ_REG_TVAL_USED, // External torque regulation enabling , enum powerstep01_ConfigEnTqReg_t |
paullj | 0:1cb05bc2807c | 42 | POWERSTEP01_CONFIG_VS_COMP_DISABLE, // Motor Supply Voltage Compensation enabling , enum powerstep01_ConfigEnVscomp_t |
paullj | 0:1cb05bc2807c | 43 | POWERSTEP01_CONFIG_OC_SD_DISABLE, // Over current shutwdown enabling, enum powerstep01_ConfigOcSd_t |
paullj | 0:1cb05bc2807c | 44 | POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t |
paullj | 0:1cb05bc2807c | 45 | POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t |
paullj | 0:1cb05bc2807c | 46 | POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t |
paullj | 0:1cb05bc2807c | 47 | POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t |
paullj | 0:1cb05bc2807c | 48 | }; |
paullj | 0:1cb05bc2807c | 49 | |
paullj | 0:1cb05bc2807c | 50 | /* Motor Control Component. */ |
paullj | 0:1cb05bc2807c | 51 | PowerStep01 *motor; |
paullj | 0:1cb05bc2807c | 52 | |
paullj | 0:1cb05bc2807c | 53 | void flag_irq_handler(void) |
paullj | 0:1cb05bc2807c | 54 | { |
paullj | 0:1cb05bc2807c | 55 | /* Set ISR flag. */ |
paullj | 0:1cb05bc2807c | 56 | motor->isrFlag = TRUE; |
paullj | 0:1cb05bc2807c | 57 | /* Get the value of the status register. */ |
paullj | 0:1cb05bc2807c | 58 | unsigned int statusRegister = motor->get_status(); |
paullj | 0:1cb05bc2807c | 59 | printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); |
paullj | 0:1cb05bc2807c | 60 | /* Check SW_F flag: if not set, the SW input is opened */ |
paullj | 0:1cb05bc2807c | 61 | if ((statusRegister & POWERSTEP01_STATUS_SW_F ) != 0) { |
paullj | 0:1cb05bc2807c | 62 | printf(" SW closed (connected to ground).\r\n"); |
paullj | 0:1cb05bc2807c | 63 | } |
paullj | 0:1cb05bc2807c | 64 | /* Check SW_EN bit */ |
paullj | 0:1cb05bc2807c | 65 | if ((statusRegister & POWERSTEP01_STATUS_SW_EVN) == POWERSTEP01_STATUS_SW_EVN) { |
paullj | 0:1cb05bc2807c | 66 | printf(" SW turn_on event.\r\n"); |
paullj | 0:1cb05bc2807c | 67 | } |
paullj | 0:1cb05bc2807c | 68 | /* Check Command Error flag: if set, the command received by SPI can't be */ |
paullj | 0:1cb05bc2807c | 69 | /* performed. This occurs for instance when a move command is sent to the */ |
paullj | 0:1cb05bc2807c | 70 | /* Powerstep01 while it is already running */ |
paullj | 0:1cb05bc2807c | 71 | if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR) == POWERSTEP01_STATUS_CMD_ERROR) { |
paullj | 0:1cb05bc2807c | 72 | printf(" Non-performable command detected.\r\n"); |
paullj | 0:1cb05bc2807c | 73 | } |
paullj | 0:1cb05bc2807c | 74 | /* Check UVLO flag: if not set, there is an undervoltage lock-out */ |
paullj | 0:1cb05bc2807c | 75 | if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) { |
paullj | 0:1cb05bc2807c | 76 | printf(" undervoltage lock-out.\r\n"); |
paullj | 0:1cb05bc2807c | 77 | } |
paullj | 0:1cb05bc2807c | 78 | /* Check thermal STATUS flags: if set, the thermal status is not normal */ |
paullj | 0:1cb05bc2807c | 79 | if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) { |
paullj | 0:1cb05bc2807c | 80 | //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown |
paullj | 0:1cb05bc2807c | 81 | printf(" Thermal status: %d.\r\n", (statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11); |
paullj | 0:1cb05bc2807c | 82 | } |
paullj | 0:1cb05bc2807c | 83 | /* Check OCD flag: if not set, there is an overcurrent detection */ |
paullj | 0:1cb05bc2807c | 84 | if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) { |
paullj | 0:1cb05bc2807c | 85 | printf(" Overcurrent detection.\r\n"); |
paullj | 0:1cb05bc2807c | 86 | } |
paullj | 0:1cb05bc2807c | 87 | /* Reset ISR flag. */ |
paullj | 0:1cb05bc2807c | 88 | motor->isrFlag = FALSE; |
paullj | 0:1cb05bc2807c | 89 | } |
paullj | 0:1cb05bc2807c | 90 | |
paullj | 0:1cb05bc2807c | 91 | /** |
paullj | 0:1cb05bc2807c | 92 | * @brief This is an example of error handler. |
paullj | 0:1cb05bc2807c | 93 | * @param[in] error Number of the error |
paullj | 0:1cb05bc2807c | 94 | * @retval None |
paullj | 0:1cb05bc2807c | 95 | * @note If needed, implement it, and then attach it: |
paullj | 0:1cb05bc2807c | 96 | * + motor->attach_error_handler(&my_error_handler); |
paullj | 0:1cb05bc2807c | 97 | */ |
paullj | 0:1cb05bc2807c | 98 | void error_handler(uint16_t error) |
paullj | 0:1cb05bc2807c | 99 | { |
paullj | 0:1cb05bc2807c | 100 | /* Printing to the console. */ |
paullj | 0:1cb05bc2807c | 101 | printf("Error %d detected\r\n\n", error); |
paullj | 0:1cb05bc2807c | 102 | |
paullj | 0:1cb05bc2807c | 103 | /* Infinite loop */ |
paullj | 0:1cb05bc2807c | 104 | while (true) { |
paullj | 0:1cb05bc2807c | 105 | } |
paullj | 0:1cb05bc2807c | 106 | } |
paullj | 0:1cb05bc2807c | 107 | |
paullj | 0:1cb05bc2807c | 108 | /* Main ----------------------------------------------------------------------*/ |
paullj | 0:1cb05bc2807c | 109 | |
paullj | 0:1cb05bc2807c | 110 | int main() |
paullj | 0:1cb05bc2807c | 111 | { |
paullj | 0:1cb05bc2807c | 112 | /* Printing to the console. */ |
paullj | 0:1cb05bc2807c | 113 | printf("STARTING MAIN PROGRAM\r\n"); |
paullj | 0:1cb05bc2807c | 114 | |
paullj | 0:1cb05bc2807c | 115 | //----- Initialization |
paullj | 0:1cb05bc2807c | 116 | /* Initializing SPI bus. */ |
paullj | 0:1cb05bc2807c | 117 | DevSPI dev_spi(D11, D12, D13); |
paullj | 0:1cb05bc2807c | 118 | |
paullj | 0:1cb05bc2807c | 119 | /* Initializing Motor Control Component. */ |
paullj | 0:1cb05bc2807c | 120 | motor = new PowerStep01(D2, D4, D8, D9, D10, dev_spi); |
paullj | 0:1cb05bc2807c | 121 | if (motor->init(&init) != COMPONENT_OK) { |
paullj | 0:1cb05bc2807c | 122 | exit(EXIT_FAILURE); |
paullj | 0:1cb05bc2807c | 123 | } |
paullj | 0:1cb05bc2807c | 124 | |
paullj | 0:1cb05bc2807c | 125 | /* Attaching and enabling an interrupt handler. */ |
paullj | 0:1cb05bc2807c | 126 | motor->attach_flag_irq(&flag_irq_handler); |
paullj | 0:1cb05bc2807c | 127 | motor->enable_flag_irq(); |
paullj | 0:1cb05bc2807c | 128 | |
paullj | 0:1cb05bc2807c | 129 | /* Attaching an error handler */ |
paullj | 0:1cb05bc2807c | 130 | motor->attach_error_handler(&error_handler); |
paullj | 0:1cb05bc2807c | 131 | |
paullj | 0:1cb05bc2807c | 132 | /* Printing to the console. */ |
paullj | 0:1cb05bc2807c | 133 | printf("Motor Control Application Example for 1 Motor\r\n"); |
paullj | 0:1cb05bc2807c | 134 | |
paullj | 0:1cb05bc2807c | 135 | //----- move of 16000 steps in the FW direction |
paullj | 0:1cb05bc2807c | 136 | printf("--> Moving forward 480 steps.\r\n"); |
paullj | 0:1cb05bc2807c | 137 | motor->move(StepperMotor::FWD, 480); |
paullj | 0:1cb05bc2807c | 138 | |
paullj | 0:1cb05bc2807c | 139 | /* Waiting while the motor is active. */ |
paullj | 0:1cb05bc2807c | 140 | motor->wait_while_active(); |
paullj | 0:1cb05bc2807c | 141 | |
paullj | 0:1cb05bc2807c | 142 | /* Wait for 2 seconds */ |
paullj | 0:1cb05bc2807c | 143 | ThisThread::sleep_for(2*1000); |
paullj | 0:1cb05bc2807c | 144 | //----- Go to position -480 |
paullj | 0:1cb05bc2807c | 145 | printf("--> Go to position -480 steps.\r\n"); |
paullj | 0:1cb05bc2807c | 146 | |
paullj | 0:1cb05bc2807c | 147 | /* Request device to go to position -480 */ |
paullj | 0:1cb05bc2807c | 148 | motor->go_to(-480); |
paullj | 0:1cb05bc2807c | 149 | |
paullj | 0:1cb05bc2807c | 150 | /* Wait for the motor ends moving */ |
paullj | 0:1cb05bc2807c | 151 | motor->wait_while_active(); |
paullj | 0:1cb05bc2807c | 152 | |
paullj | 0:1cb05bc2807c | 153 | /* Get current position of device and print to the console */ |
paullj | 0:1cb05bc2807c | 154 | printf(" Position: %d.\r\n", motor->get_position()); |
paullj | 0:1cb05bc2807c | 155 | |
paullj | 0:1cb05bc2807c | 156 | /* Wait for 2 seconds */ |
paullj | 0:1cb05bc2807c | 157 | ThisThread::sleep_for(2*1000); |
paullj | 0:1cb05bc2807c | 158 | |
paullj | 0:1cb05bc2807c | 159 | //----- Go Home |
paullj | 0:1cb05bc2807c | 160 | /* Printing to the console. */ |
paullj | 0:1cb05bc2807c | 161 | printf("--> Go to home position.\r\n"); |
paullj | 0:1cb05bc2807c | 162 | |
paullj | 0:1cb05bc2807c | 163 | /* Request device to go to Home */ |
paullj | 0:1cb05bc2807c | 164 | motor->go_home(); |
paullj | 0:1cb05bc2807c | 165 | |
paullj | 0:1cb05bc2807c | 166 | /* Wait for the motor ends moving */ |
paullj | 0:1cb05bc2807c | 167 | motor->wait_while_active(); |
paullj | 0:1cb05bc2807c | 168 | |
paullj | 0:1cb05bc2807c | 169 | /* Wait for 2 seconds */ |
paullj | 0:1cb05bc2807c | 170 | ThisThread::sleep_for(2*1000); |
paullj | 0:1cb05bc2807c | 171 | |
paullj | 0:1cb05bc2807c | 172 | //----- run the motor BACKWARD at 120 step/s |
paullj | 0:1cb05bc2807c | 173 | printf("--> run the motor backward at 120 step/s.\r\n"); |
paullj | 0:1cb05bc2807c | 174 | motor->run(StepperMotor::BWD, 120); |
paullj | 0:1cb05bc2807c | 175 | |
paullj | 0:1cb05bc2807c | 176 | //----- Get parameter example |
paullj | 0:1cb05bc2807c | 177 | /* Wait for device reaches the targeted speed */ |
paullj | 0:1cb05bc2807c | 178 | while ((motor->read_status_register() & POWERSTEP01_STATUS_MOT_STATUS) != POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD); |
paullj | 0:1cb05bc2807c | 179 | |
paullj | 0:1cb05bc2807c | 180 | /* Record the reached speed in step/s and print to the console */ |
paullj | 0:1cb05bc2807c | 181 | printf(" Reached Speed: %f step/s.\r\n", motor->get_analog_value(POWERSTEP01_SPEED)); |
paullj | 0:1cb05bc2807c | 182 | |
paullj | 0:1cb05bc2807c | 183 | //----- Soft stopped required while running |
paullj | 0:1cb05bc2807c | 184 | printf("--> Soft stop requested.\r\n"); |
paullj | 0:1cb05bc2807c | 185 | |
paullj | 0:1cb05bc2807c | 186 | /* Request a soft stop of device and keep the power bridges enabled */ |
paullj | 0:1cb05bc2807c | 187 | motor->soft_hiz(); |
paullj | 0:1cb05bc2807c | 188 | |
paullj | 0:1cb05bc2807c | 189 | /* Wait for the motor of device ends moving */ |
paullj | 0:1cb05bc2807c | 190 | motor->wait_while_active(); |
paullj | 0:1cb05bc2807c | 191 | } |