Motion control example for 1 motor.
Dependencies: X_NUCLEO_IHM03A1 mbed
Fork of IHM03A1_ExampleFor1Motor by
This application provides an example of usage of the X-NUCLEO-IHM03A1 High Power Stepper Motor Control Expansion Board.
It shows how to use one stepper motor connected to the board by:
- moving the rotor a specific number of steps or to a specific position, with a given speed value, direction of rotation;
- monitoring the motor status;
- handling an interrupt triggered by the motor driver;
- getting and setting a motor driver parameter.
- etc.
For the hardware configuration of the expansion board, please refer to the X_NUCLEO_IHM03A1 home web page.
Diff: main.cpp
- Revision:
- 2:7e8485b5d57d
- Parent:
- 1:6b179be4937e
- Child:
- 3:465d357bbce3
--- a/main.cpp Thu Apr 07 16:32:55 2016 +0000 +++ b/main.cpp Wed Apr 13 08:53:45 2016 +0000 @@ -96,7 +96,7 @@ POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t - POWERSTEP01_CONFIG_PRED_DISABLE, // Predictive current enabling , enum powerstep01_ConfigPredEn_t + POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t }; /* Motor Control Component. */ @@ -110,7 +110,7 @@ * @param None * @retval None * @note If needed, implement it, and then attach and enable it: - * + motor->AttachFlagIRQ(&FlagIRQHandler); + * + motor->AttachFlagIRQ(&myFlagIRQHandler); * + motor->EnableFlagIRQ(); * To disable it: * + motor->DisbleFlagIRQ(); @@ -122,61 +122,70 @@ /* Get the value of the status register. */ unsigned int statusRegister = motor->GetStatus(); - + /* Check HIZ flag: if set, power brigdes are disabled */ if ((statusRegister & POWERSTEP01_STATUS_HIZ)==POWERSTEP01_STATUS_HIZ) { // HIZ state - printf(" WARNING: \"FLAG\" interrupt triggered. HiZ state.\r\n"); + printf(" HiZ state.\r\n"); } /* Check BUSY flag: if not set, a command is under execution */ if ((statusRegister & POWERSTEP01_STATUS_BUSY)==0) { // BUSY + printf(" Busy.\r\n"); } /* Check SW_F flag: if not set, the SW input is opened */ - if ((statusRegister & POWERSTEP01_STATUS_SW_F )==0) - { - // SW OPEN - } - else - { - // SW CLOSED - } - /* Check SW_EN bit */ - if ((statusRegister & POWERSTEP01_STATUS_SW_EVN)==POWERSTEP01_STATUS_SW_EVN) + if ((statusRegister & POWERSTEP01_STATUS_SW_F )!=0) { - // switch turn_on event - } - /* Check direction bit */ - if ((statusRegister & POWERSTEP01_STATUS_DIR)==0) + // SW closed (connected to ground) + printf(" SW closed (connected to ground).\r\n"); + } + /* Check SW_EN bit */ + if ((statusRegister & POWERSTEP01_STATUS_SW_EVN)== + POWERSTEP01_STATUS_SW_EVN) { - // BACKWARD - } - else - { - // FORWARD + // SW turn_on event + printf(" SW turn_on event.\r\n"); } if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== POWERSTEP01_STATUS_MOT_STATUS_STOPPED) { // MOTOR STOPPED + printf(" Stopped.\r\n"); } - else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== - POWERSTEP01_STATUS_MOT_STATUS_ACCELERATION) + else { - // MOTOR ACCELERATION - } - else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== - POWERSTEP01_STATUS_MOT_STATUS_DECELERATION) - { - // MOTOR DECELERATION - } - else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== - POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD) - { - // MOTOR RUNNING AT CONSTANT SPEED - } + if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== + POWERSTEP01_STATUS_MOT_STATUS_ACCELERATION) + { + // MOTOR ACCELERATION + printf(" Accelerating "); + } + else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== + POWERSTEP01_STATUS_MOT_STATUS_DECELERATION) + { + // MOTOR DECELERATION + printf(" Decelerating "); + } + else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== + POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD) + { + // MOTOR RUNNING AT CONSTANT SPEED + printf(" Steady running "); + } + /* Check direction bit */ + if ((statusRegister & POWERSTEP01_STATUS_DIR)==0) + { + // StepperMotor::BWD + printf(" in backward direction.\r\n"); + } + else + { + // StepperMotor::FWD + printf(" in forward direction.\r\n"); + } + } /* Check Command Error flag: if set, the command received by SPI can't be */ /* performed. This occurs for instance when a move command is sent to the */ /* Powerstep01 while it is already running */ @@ -184,45 +193,64 @@ POWERSTEP01_STATUS_CMD_ERROR) { // Command Error - printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected.\r\n"); + printf(" Non-performable command detected.\r\n"); } /* Check Step mode clock flag: if set, the device is working in step clock mode */ if ((statusRegister & POWERSTEP01_STATUS_STCK_MOD)== POWERSTEP01_STATUS_STCK_MOD) { //Step clock mode enabled + printf(" Step clock mode enabled.\r\n"); } /* Check UVLO flag: if not set, there is an undervoltage lock-out */ if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) { - //undervoltage lock-out + //Undervoltage lock-out + printf(" undervoltage lock-out.\r\n"); } /* Check UVLO ADC flag: if not set, there is an ADC undervoltage lock-out */ if ((statusRegister & POWERSTEP01_STATUS_UVLO_ADC)==0) { - //ADC undervoltage lock-out + //ADC undervoltage lock-out + printf(" ADC undervoltage lock-out:\r\n"); + printf(" Expected with default IHM03A1 HW configuration.\r\n"); } /* Check thermal STATUS flags: if set, the thermal status is not normal */ if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) { //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown + if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==1) + { + printf(" Thermal status - Warning.\r\n"); + } + else if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==2) + { + printf(" Thermal status - Bridge shutdown.\r\n"); + } + else if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==3) + { + printf(" Thermal status - Device shutdown.\r\n"); + } } /* Check OCD flag: if not set, there is an overcurrent detection */ if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) { - //overcurrent detection + //Overcurrent detection + printf(" Overcurrent detection.\r\n"); } /* Check STALL_A flag: if not set, there is a Stall condition on bridge A */ if ((statusRegister & POWERSTEP01_STATUS_STALL_A)==0) { - //overcurrent detection + //Bridge A stalled + printf(" Bridge A stalled.\r\n"); } /* Check STALL_B flag: if not set, there is a Stall condition on bridge B */ if ((statusRegister & POWERSTEP01_STATUS_STALL_B)==0) { - //overcurrent detection - } - + //Bridge B stalled + printf(" Bridge B stalled.\r\n"); + } + /* Reset ISR flag. */ motor->isrFlag = FALSE; } @@ -232,7 +260,7 @@ * @param None * @retval None * @note If needed, implement it, and then attach and enable it: - * + motor->AttachBusyIRQ(&FlagIRQHandler); + * + motor->AttachBusyIRQ(&myBusyIRQHandler); * + motor->EnableBusyIRQ(); * To disable it: * + motor->DisbleBusyIRQ(); @@ -260,6 +288,8 @@ * @brief This is an example of error handler. * @param[in] error Number of the error * @retval None + * @note If needed, implement it, and then attach it: + * + motor->AttachErrorHandler(&myErrorHandler); */ void myErrorHandler(uint16_t error) {