Motion control example for 1 motor.

Dependencies:   X_NUCLEO_IHM03A1 mbed

Fork of IHM03A1_ExampleFor1Motor by ST Expansion SW Team

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.

Committer:
nucleosam
Date:
Wed Apr 13 08:53:45 2016 +0000
Revision:
2:7e8485b5d57d
Parent:
1:6b179be4937e
Child:
3:465d357bbce3
Comments updated with the correct name of functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucleosam 0:cd62cd5b2c77 1 /**
nucleosam 0:cd62cd5b2c77 2 ******************************************************************************
nucleosam 0:cd62cd5b2c77 3 * @file main.cpp
nucleosam 0:cd62cd5b2c77 4 * @author IPC Rennes
nucleosam 0:cd62cd5b2c77 5 * @version V1.0.0
nucleosam 0:cd62cd5b2c77 6 * @date March 18th, 2016
nucleosam 0:cd62cd5b2c77 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM03A1
nucleosam 0:cd62cd5b2c77 8 * Motor Control Expansion Board: control of 1 motor.
nucleosam 0:cd62cd5b2c77 9 ******************************************************************************
nucleosam 0:cd62cd5b2c77 10 * @attention
nucleosam 0:cd62cd5b2c77 11 *
nucleosam 0:cd62cd5b2c77 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
nucleosam 0:cd62cd5b2c77 13 *
nucleosam 0:cd62cd5b2c77 14 * Redistribution and use in source and binary forms, with or without modification,
nucleosam 0:cd62cd5b2c77 15 * are permitted provided that the following conditions are met:
nucleosam 0:cd62cd5b2c77 16 * 1. Redistributions of source code must retain the above copyright notice,
nucleosam 0:cd62cd5b2c77 17 * this list of conditions and the following disclaimer.
nucleosam 0:cd62cd5b2c77 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
nucleosam 0:cd62cd5b2c77 19 * this list of conditions and the following disclaimer in the documentation
nucleosam 0:cd62cd5b2c77 20 * and/or other materials provided with the distribution.
nucleosam 0:cd62cd5b2c77 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nucleosam 0:cd62cd5b2c77 22 * may be used to endorse or promote products derived from this software
nucleosam 0:cd62cd5b2c77 23 * without specific prior written permission.
nucleosam 0:cd62cd5b2c77 24 *
nucleosam 0:cd62cd5b2c77 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nucleosam 0:cd62cd5b2c77 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nucleosam 0:cd62cd5b2c77 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nucleosam 0:cd62cd5b2c77 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nucleosam 0:cd62cd5b2c77 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nucleosam 0:cd62cd5b2c77 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nucleosam 0:cd62cd5b2c77 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nucleosam 0:cd62cd5b2c77 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nucleosam 0:cd62cd5b2c77 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nucleosam 0:cd62cd5b2c77 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nucleosam 0:cd62cd5b2c77 35 *
nucleosam 0:cd62cd5b2c77 36 ******************************************************************************
nucleosam 0:cd62cd5b2c77 37 */
nucleosam 0:cd62cd5b2c77 38
nucleosam 0:cd62cd5b2c77 39
nucleosam 0:cd62cd5b2c77 40 /* Includes ------------------------------------------------------------------*/
nucleosam 0:cd62cd5b2c77 41
nucleosam 0:cd62cd5b2c77 42 /* mbed specific header files. */
nucleosam 0:cd62cd5b2c77 43 #include "mbed.h"
nucleosam 0:cd62cd5b2c77 44
nucleosam 0:cd62cd5b2c77 45 /* Helper header files. */
nucleosam 0:cd62cd5b2c77 46 #include "DevSPI.h"
nucleosam 0:cd62cd5b2c77 47
nucleosam 0:cd62cd5b2c77 48 /* Component specific header files. */
nucleosam 0:cd62cd5b2c77 49 #include "powerstep01_class.h"
nucleosam 0:cd62cd5b2c77 50
nucleosam 0:cd62cd5b2c77 51 /* Variables -----------------------------------------------------------------*/
nucleosam 0:cd62cd5b2c77 52
nucleosam 0:cd62cd5b2c77 53 /* Initialization parameters of the motor connected to the expansion board. */
nucleosam 0:cd62cd5b2c77 54 /* Current mode. */
nucleosam 0:cd62cd5b2c77 55 powerstep01_Init_u_t initDeviceParameters =
nucleosam 0:cd62cd5b2c77 56 {
nucleosam 0:cd62cd5b2c77 57 /* common parameters */
nucleosam 0:cd62cd5b2c77 58 .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t
nucleosam 0:cd62cd5b2c77 59 582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2
nucleosam 0:cd62cd5b2c77 60 582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2
nucleosam 0:cd62cd5b2c77 61 488, // Maximum speed in step/s, range 15.25 to 15610 steps/s
nucleosam 0:cd62cd5b2c77 62 0, // Minimum speed in step/s, range 0 to 976.3 steps/s
nucleosam 0:cd62cd5b2c77 63 POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t
nucleosam 0:cd62cd5b2c77 64 244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s
nucleosam 0:cd62cd5b2c77 65 POWERSTEP01_BOOST_MODE_OFF, // Boost of the amplitude square wave, enum powerstep01_BoostMode_t
nucleosam 0:cd62cd5b2c77 66 281.25, // Overcurrent threshold settings via enum powerstep01_OcdTh_t
nucleosam 0:cd62cd5b2c77 67 STEP_MODE_1_16, // Step mode settings via enum motorStepMode_t
nucleosam 0:cd62cd5b2c77 68 POWERSTEP01_SYNC_SEL_DISABLED, // Synch. Mode settings via enum powerstep01_SyncSel_t
nucleosam 0:cd62cd5b2c77 69 (POWERSTEP01_ALARM_EN_OVERCURRENT|
nucleosam 0:cd62cd5b2c77 70 POWERSTEP01_ALARM_EN_THERMAL_SHUTDOWN|
nucleosam 0:cd62cd5b2c77 71 POWERSTEP01_ALARM_EN_THERMAL_WARNING|
nucleosam 0:cd62cd5b2c77 72 POWERSTEP01_ALARM_EN_UVLO|
nucleosam 0:cd62cd5b2c77 73 POWERSTEP01_ALARM_EN_STALL_DETECTION|
nucleosam 0:cd62cd5b2c77 74 POWERSTEP01_ALARM_EN_SW_TURN_ON|
nucleosam 0:cd62cd5b2c77 75 POWERSTEP01_ALARM_EN_WRONG_NPERF_CMD), // Alarm settings via bitmap enum powerstep01_AlarmEn_t
nucleosam 0:cd62cd5b2c77 76 POWERSTEP01_IGATE_64mA, // Gate sink/source current via enum powerstep01_Igate_t
nucleosam 0:cd62cd5b2c77 77 POWERSTEP01_TBOOST_0ns, // Duration of the overboost phase during gate turn-off via enum powerstep01_Tboost_t
nucleosam 0:cd62cd5b2c77 78 POWERSTEP01_TCC_500ns, // Controlled current time via enum powerstep01_Tcc_t
nucleosam 0:cd62cd5b2c77 79 POWERSTEP01_WD_EN_DISABLE, // External clock watchdog, enum powerstep01_WdEn_t
nucleosam 0:cd62cd5b2c77 80 POWERSTEP01_TBLANK_375ns, // Duration of the blanking time via enum powerstep01_TBlank_t
nucleosam 0:cd62cd5b2c77 81 POWERSTEP01_TDT_125ns, // Duration of the dead time via enum powerstep01_Tdt_t
nucleosam 0:cd62cd5b2c77 82 /* current mode parameters */
nucleosam 0:cd62cd5b2c77 83 328.12, // Hold torque in mV, range from 7.8mV to 1000 mV
nucleosam 0:cd62cd5b2c77 84 328.12, // Running torque in mV, range from 7.8mV to 1000 mV
nucleosam 0:cd62cd5b2c77 85 328.12, // Acceleration torque in mV, range from 7.8mV to 1000 mV
nucleosam 0:cd62cd5b2c77 86 328.12, // Deceleration torque in mV, range from 7.8mV to 1000 mV
nucleosam 0:cd62cd5b2c77 87 POWERSTEP01_TOFF_FAST_8us, //Maximum fast decay time , enum powerstep01_ToffFast_t
nucleosam 0:cd62cd5b2c77 88 POWERSTEP01_FAST_STEP_12us, //Maximum fall step time , enum powerstep01_FastStep_t
nucleosam 0:cd62cd5b2c77 89 3.0, // Minimum on-time in us, range 0.5us to 64us
nucleosam 0:cd62cd5b2c77 90 21.0, // Minimum off-time in us, range 0.5us to 64us
nucleosam 0:cd62cd5b2c77 91 POWERSTEP01_CONFIG_INT_16MHZ_OSCOUT_2MHZ, // Clock setting , enum powerstep01_ConfigOscMgmt_t
nucleosam 0:cd62cd5b2c77 92 POWERSTEP01_CONFIG_SW_HARD_STOP, // External switch hard stop interrupt mode, enum powerstep01_ConfigSwMode_t
nucleosam 0:cd62cd5b2c77 93 POWERSTEP01_CONFIG_TQ_REG_TVAL_USED, // External torque regulation enabling , enum powerstep01_ConfigEnTqReg_t
nucleosam 0:cd62cd5b2c77 94 POWERSTEP01_CONFIG_VS_COMP_DISABLE, // Motor Supply Voltage Compensation enabling , enum powerstep01_ConfigEnVscomp_t
nucleosam 0:cd62cd5b2c77 95 POWERSTEP01_CONFIG_OC_SD_DISABLE, // Over current shutwdown enabling, enum powerstep01_ConfigOcSd_t
nucleosam 0:cd62cd5b2c77 96 POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t
nucleosam 0:cd62cd5b2c77 97 POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t
nucleosam 0:cd62cd5b2c77 98 POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t
nucleosam 2:7e8485b5d57d 99 POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t
nucleosam 0:cd62cd5b2c77 100 };
nucleosam 0:cd62cd5b2c77 101
nucleosam 0:cd62cd5b2c77 102 /* Motor Control Component. */
nucleosam 0:cd62cd5b2c77 103 POWERSTEP01 *motor;
nucleosam 0:cd62cd5b2c77 104
nucleosam 0:cd62cd5b2c77 105
nucleosam 0:cd62cd5b2c77 106 /* Functions -----------------------------------------------------------------*/
nucleosam 0:cd62cd5b2c77 107
nucleosam 0:cd62cd5b2c77 108 /**
nucleosam 0:cd62cd5b2c77 109 * @brief This is an example of user handler for the flag interrupt.
nucleosam 0:cd62cd5b2c77 110 * @param None
nucleosam 0:cd62cd5b2c77 111 * @retval None
nucleosam 0:cd62cd5b2c77 112 * @note If needed, implement it, and then attach and enable it:
nucleosam 2:7e8485b5d57d 113 * + motor->AttachFlagIRQ(&myFlagIRQHandler);
nucleosam 0:cd62cd5b2c77 114 * + motor->EnableFlagIRQ();
nucleosam 0:cd62cd5b2c77 115 * To disable it:
nucleosam 0:cd62cd5b2c77 116 * + motor->DisbleFlagIRQ();
nucleosam 0:cd62cd5b2c77 117 */
nucleosam 0:cd62cd5b2c77 118 void myFlagIRQHandler(void)
nucleosam 0:cd62cd5b2c77 119 {
nucleosam 0:cd62cd5b2c77 120 /* Set ISR flag. */
nucleosam 0:cd62cd5b2c77 121 motor->isrFlag = TRUE;
nucleosam 0:cd62cd5b2c77 122
nucleosam 0:cd62cd5b2c77 123 /* Get the value of the status register. */
nucleosam 0:cd62cd5b2c77 124 unsigned int statusRegister = motor->GetStatus();
nucleosam 2:7e8485b5d57d 125
nucleosam 0:cd62cd5b2c77 126 /* Check HIZ flag: if set, power brigdes are disabled */
nucleosam 0:cd62cd5b2c77 127 if ((statusRegister & POWERSTEP01_STATUS_HIZ)==POWERSTEP01_STATUS_HIZ)
nucleosam 0:cd62cd5b2c77 128 {
nucleosam 0:cd62cd5b2c77 129 // HIZ state
nucleosam 2:7e8485b5d57d 130 printf(" HiZ state.\r\n");
nucleosam 0:cd62cd5b2c77 131 }
nucleosam 0:cd62cd5b2c77 132 /* Check BUSY flag: if not set, a command is under execution */
nucleosam 0:cd62cd5b2c77 133 if ((statusRegister & POWERSTEP01_STATUS_BUSY)==0)
nucleosam 0:cd62cd5b2c77 134 {
nucleosam 0:cd62cd5b2c77 135 // BUSY
nucleosam 2:7e8485b5d57d 136 printf(" Busy.\r\n");
nucleosam 0:cd62cd5b2c77 137 }
nucleosam 0:cd62cd5b2c77 138 /* Check SW_F flag: if not set, the SW input is opened */
nucleosam 2:7e8485b5d57d 139 if ((statusRegister & POWERSTEP01_STATUS_SW_F )!=0)
nucleosam 0:cd62cd5b2c77 140 {
nucleosam 2:7e8485b5d57d 141 // SW closed (connected to ground)
nucleosam 2:7e8485b5d57d 142 printf(" SW closed (connected to ground).\r\n");
nucleosam 2:7e8485b5d57d 143 }
nucleosam 2:7e8485b5d57d 144 /* Check SW_EN bit */
nucleosam 2:7e8485b5d57d 145 if ((statusRegister & POWERSTEP01_STATUS_SW_EVN)==
nucleosam 2:7e8485b5d57d 146 POWERSTEP01_STATUS_SW_EVN)
nucleosam 0:cd62cd5b2c77 147 {
nucleosam 2:7e8485b5d57d 148 // SW turn_on event
nucleosam 2:7e8485b5d57d 149 printf(" SW turn_on event.\r\n");
nucleosam 0:cd62cd5b2c77 150 }
nucleosam 0:cd62cd5b2c77 151 if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)==
nucleosam 0:cd62cd5b2c77 152 POWERSTEP01_STATUS_MOT_STATUS_STOPPED)
nucleosam 0:cd62cd5b2c77 153 {
nucleosam 0:cd62cd5b2c77 154 // MOTOR STOPPED
nucleosam 2:7e8485b5d57d 155 printf(" Stopped.\r\n");
nucleosam 0:cd62cd5b2c77 156 }
nucleosam 2:7e8485b5d57d 157 else
nucleosam 0:cd62cd5b2c77 158 {
nucleosam 2:7e8485b5d57d 159 if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)==
nucleosam 2:7e8485b5d57d 160 POWERSTEP01_STATUS_MOT_STATUS_ACCELERATION)
nucleosam 2:7e8485b5d57d 161 {
nucleosam 2:7e8485b5d57d 162 // MOTOR ACCELERATION
nucleosam 2:7e8485b5d57d 163 printf(" Accelerating ");
nucleosam 2:7e8485b5d57d 164 }
nucleosam 2:7e8485b5d57d 165 else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)==
nucleosam 2:7e8485b5d57d 166 POWERSTEP01_STATUS_MOT_STATUS_DECELERATION)
nucleosam 2:7e8485b5d57d 167 {
nucleosam 2:7e8485b5d57d 168 // MOTOR DECELERATION
nucleosam 2:7e8485b5d57d 169 printf(" Decelerating ");
nucleosam 2:7e8485b5d57d 170 }
nucleosam 2:7e8485b5d57d 171 else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)==
nucleosam 2:7e8485b5d57d 172 POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD)
nucleosam 2:7e8485b5d57d 173 {
nucleosam 2:7e8485b5d57d 174 // MOTOR RUNNING AT CONSTANT SPEED
nucleosam 2:7e8485b5d57d 175 printf(" Steady running ");
nucleosam 2:7e8485b5d57d 176 }
nucleosam 2:7e8485b5d57d 177 /* Check direction bit */
nucleosam 2:7e8485b5d57d 178 if ((statusRegister & POWERSTEP01_STATUS_DIR)==0)
nucleosam 2:7e8485b5d57d 179 {
nucleosam 2:7e8485b5d57d 180 // StepperMotor::BWD
nucleosam 2:7e8485b5d57d 181 printf(" in backward direction.\r\n");
nucleosam 2:7e8485b5d57d 182 }
nucleosam 2:7e8485b5d57d 183 else
nucleosam 2:7e8485b5d57d 184 {
nucleosam 2:7e8485b5d57d 185 // StepperMotor::FWD
nucleosam 2:7e8485b5d57d 186 printf(" in forward direction.\r\n");
nucleosam 2:7e8485b5d57d 187 }
nucleosam 2:7e8485b5d57d 188 }
nucleosam 0:cd62cd5b2c77 189 /* Check Command Error flag: if set, the command received by SPI can't be */
nucleosam 0:cd62cd5b2c77 190 /* performed. This occurs for instance when a move command is sent to the */
nucleosam 0:cd62cd5b2c77 191 /* Powerstep01 while it is already running */
nucleosam 0:cd62cd5b2c77 192 if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR)==
nucleosam 0:cd62cd5b2c77 193 POWERSTEP01_STATUS_CMD_ERROR)
nucleosam 0:cd62cd5b2c77 194 {
nucleosam 0:cd62cd5b2c77 195 // Command Error
nucleosam 2:7e8485b5d57d 196 printf(" Non-performable command detected.\r\n");
nucleosam 0:cd62cd5b2c77 197 }
nucleosam 0:cd62cd5b2c77 198 /* Check Step mode clock flag: if set, the device is working in step clock mode */
nucleosam 0:cd62cd5b2c77 199 if ((statusRegister & POWERSTEP01_STATUS_STCK_MOD)==
nucleosam 0:cd62cd5b2c77 200 POWERSTEP01_STATUS_STCK_MOD)
nucleosam 0:cd62cd5b2c77 201 {
nucleosam 0:cd62cd5b2c77 202 //Step clock mode enabled
nucleosam 2:7e8485b5d57d 203 printf(" Step clock mode enabled.\r\n");
nucleosam 0:cd62cd5b2c77 204 }
nucleosam 0:cd62cd5b2c77 205 /* Check UVLO flag: if not set, there is an undervoltage lock-out */
nucleosam 0:cd62cd5b2c77 206 if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0)
nucleosam 0:cd62cd5b2c77 207 {
nucleosam 2:7e8485b5d57d 208 //Undervoltage lock-out
nucleosam 2:7e8485b5d57d 209 printf(" undervoltage lock-out.\r\n");
nucleosam 0:cd62cd5b2c77 210 }
nucleosam 0:cd62cd5b2c77 211 /* Check UVLO ADC flag: if not set, there is an ADC undervoltage lock-out */
nucleosam 0:cd62cd5b2c77 212 if ((statusRegister & POWERSTEP01_STATUS_UVLO_ADC)==0)
nucleosam 0:cd62cd5b2c77 213 {
nucleosam 2:7e8485b5d57d 214 //ADC undervoltage lock-out
nucleosam 2:7e8485b5d57d 215 printf(" ADC undervoltage lock-out:\r\n");
nucleosam 2:7e8485b5d57d 216 printf(" Expected with default IHM03A1 HW configuration.\r\n");
nucleosam 0:cd62cd5b2c77 217 }
nucleosam 0:cd62cd5b2c77 218 /* Check thermal STATUS flags: if set, the thermal status is not normal */
nucleosam 0:cd62cd5b2c77 219 if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0)
nucleosam 0:cd62cd5b2c77 220 {
nucleosam 0:cd62cd5b2c77 221 //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown
nucleosam 2:7e8485b5d57d 222 if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==1)
nucleosam 2:7e8485b5d57d 223 {
nucleosam 2:7e8485b5d57d 224 printf(" Thermal status - Warning.\r\n");
nucleosam 2:7e8485b5d57d 225 }
nucleosam 2:7e8485b5d57d 226 else if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==2)
nucleosam 2:7e8485b5d57d 227 {
nucleosam 2:7e8485b5d57d 228 printf(" Thermal status - Bridge shutdown.\r\n");
nucleosam 2:7e8485b5d57d 229 }
nucleosam 2:7e8485b5d57d 230 else if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==3)
nucleosam 2:7e8485b5d57d 231 {
nucleosam 2:7e8485b5d57d 232 printf(" Thermal status - Device shutdown.\r\n");
nucleosam 2:7e8485b5d57d 233 }
nucleosam 0:cd62cd5b2c77 234 }
nucleosam 0:cd62cd5b2c77 235 /* Check OCD flag: if not set, there is an overcurrent detection */
nucleosam 0:cd62cd5b2c77 236 if ((statusRegister & POWERSTEP01_STATUS_OCD)==0)
nucleosam 0:cd62cd5b2c77 237 {
nucleosam 2:7e8485b5d57d 238 //Overcurrent detection
nucleosam 2:7e8485b5d57d 239 printf(" Overcurrent detection.\r\n");
nucleosam 0:cd62cd5b2c77 240 }
nucleosam 0:cd62cd5b2c77 241 /* Check STALL_A flag: if not set, there is a Stall condition on bridge A */
nucleosam 0:cd62cd5b2c77 242 if ((statusRegister & POWERSTEP01_STATUS_STALL_A)==0)
nucleosam 0:cd62cd5b2c77 243 {
nucleosam 2:7e8485b5d57d 244 //Bridge A stalled
nucleosam 2:7e8485b5d57d 245 printf(" Bridge A stalled.\r\n");
nucleosam 0:cd62cd5b2c77 246 }
nucleosam 0:cd62cd5b2c77 247 /* Check STALL_B flag: if not set, there is a Stall condition on bridge B */
nucleosam 0:cd62cd5b2c77 248 if ((statusRegister & POWERSTEP01_STATUS_STALL_B)==0)
nucleosam 0:cd62cd5b2c77 249 {
nucleosam 2:7e8485b5d57d 250 //Bridge B stalled
nucleosam 2:7e8485b5d57d 251 printf(" Bridge B stalled.\r\n");
nucleosam 2:7e8485b5d57d 252 }
nucleosam 2:7e8485b5d57d 253
nucleosam 0:cd62cd5b2c77 254 /* Reset ISR flag. */
nucleosam 0:cd62cd5b2c77 255 motor->isrFlag = FALSE;
nucleosam 0:cd62cd5b2c77 256 }
nucleosam 0:cd62cd5b2c77 257
nucleosam 0:cd62cd5b2c77 258 /**
nucleosam 0:cd62cd5b2c77 259 * @brief This is an example of user handler for the busy interrupt.
nucleosam 0:cd62cd5b2c77 260 * @param None
nucleosam 0:cd62cd5b2c77 261 * @retval None
nucleosam 0:cd62cd5b2c77 262 * @note If needed, implement it, and then attach and enable it:
nucleosam 2:7e8485b5d57d 263 * + motor->AttachBusyIRQ(&myBusyIRQHandler);
nucleosam 0:cd62cd5b2c77 264 * + motor->EnableBusyIRQ();
nucleosam 0:cd62cd5b2c77 265 * To disable it:
nucleosam 0:cd62cd5b2c77 266 * + motor->DisbleBusyIRQ();
nucleosam 0:cd62cd5b2c77 267 */
nucleosam 0:cd62cd5b2c77 268 void myBusyIRQHandler(void)
nucleosam 0:cd62cd5b2c77 269 {
nucleosam 0:cd62cd5b2c77 270 /* Set ISR flag. */
nucleosam 0:cd62cd5b2c77 271 motor->isrFlag = TRUE;
nucleosam 0:cd62cd5b2c77 272
nucleosam 0:cd62cd5b2c77 273 if (motor->CheckBusyHw())
nucleosam 0:cd62cd5b2c77 274 {
nucleosam 0:cd62cd5b2c77 275 /* Busy pin is low, so at list one Powerstep01 chip is busy */
nucleosam 0:cd62cd5b2c77 276 /* To be customized (for example Switch on a LED) */
nucleosam 0:cd62cd5b2c77 277 }
nucleosam 0:cd62cd5b2c77 278 else
nucleosam 0:cd62cd5b2c77 279 {
nucleosam 0:cd62cd5b2c77 280 /* To be customized (for example Switch off a LED) */
nucleosam 0:cd62cd5b2c77 281 }
nucleosam 0:cd62cd5b2c77 282
nucleosam 0:cd62cd5b2c77 283 /* Reset ISR flag. */
nucleosam 0:cd62cd5b2c77 284 motor->isrFlag = FALSE;
nucleosam 0:cd62cd5b2c77 285 }
nucleosam 0:cd62cd5b2c77 286
nucleosam 0:cd62cd5b2c77 287 /**
nucleosam 0:cd62cd5b2c77 288 * @brief This is an example of error handler.
nucleosam 0:cd62cd5b2c77 289 * @param[in] error Number of the error
nucleosam 0:cd62cd5b2c77 290 * @retval None
nucleosam 2:7e8485b5d57d 291 * @note If needed, implement it, and then attach it:
nucleosam 2:7e8485b5d57d 292 * + motor->AttachErrorHandler(&myErrorHandler);
nucleosam 0:cd62cd5b2c77 293 */
nucleosam 0:cd62cd5b2c77 294 void myErrorHandler(uint16_t error)
nucleosam 0:cd62cd5b2c77 295 {
nucleosam 0:cd62cd5b2c77 296 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 297 printf("Error %d detected\r\n\n", error);
nucleosam 0:cd62cd5b2c77 298
nucleosam 0:cd62cd5b2c77 299 /* Infinite loop */
nucleosam 0:cd62cd5b2c77 300 while(1)
nucleosam 0:cd62cd5b2c77 301 {
nucleosam 0:cd62cd5b2c77 302 }
nucleosam 0:cd62cd5b2c77 303 }
nucleosam 0:cd62cd5b2c77 304
nucleosam 0:cd62cd5b2c77 305 /* Main ----------------------------------------------------------------------*/
nucleosam 0:cd62cd5b2c77 306
nucleosam 0:cd62cd5b2c77 307 int main()
nucleosam 0:cd62cd5b2c77 308 {
nucleosam 0:cd62cd5b2c77 309 int32_t pos;
nucleosam 0:cd62cd5b2c77 310 uint32_t unsignedIntegerValue;
nucleosam 0:cd62cd5b2c77 311 float floatValue;
nucleosam 0:cd62cd5b2c77 312
nucleosam 0:cd62cd5b2c77 313 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 314 printf("STARTING MAIN PROGRAM\r\n");
nucleosam 0:cd62cd5b2c77 315 printf(" Reminder:\r\n");
nucleosam 0:cd62cd5b2c77 316 printf(" The position unit is in agreement to the step mode.\r\n");
nucleosam 0:cd62cd5b2c77 317 printf(" The speed, acceleration or deceleration unit\r\n");
nucleosam 0:cd62cd5b2c77 318 printf(" do not depend on the step mode and the step unit is a full step.\r\n");
nucleosam 0:cd62cd5b2c77 319
nucleosam 0:cd62cd5b2c77 320 //----- Initialization
nucleosam 0:cd62cd5b2c77 321 /* Initializing SPI bus. */
nucleosam 0:cd62cd5b2c77 322 DevSPI dev_spi(D11, D12, D13);
nucleosam 0:cd62cd5b2c77 323
nucleosam 0:cd62cd5b2c77 324 /* Initializing Motor Control Component. */
nucleosam 0:cd62cd5b2c77 325 motor = new POWERSTEP01(D2, D4, D8, D9, D10, dev_spi);
nucleosam 0:cd62cd5b2c77 326 if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE);
nucleosam 0:cd62cd5b2c77 327
nucleosam 0:cd62cd5b2c77 328 /* Attaching and enabling interrupt handlers. */
nucleosam 0:cd62cd5b2c77 329 motor->AttachFlagIRQ(&myFlagIRQHandler);
nucleosam 0:cd62cd5b2c77 330 motor->EnableFlagIRQ();
nucleosam 0:cd62cd5b2c77 331 motor->AttachBusyIRQ(&myBusyIRQHandler);
nucleosam 0:cd62cd5b2c77 332 motor->EnableBusyIRQ();
nucleosam 0:cd62cd5b2c77 333
nucleosam 0:cd62cd5b2c77 334 /* Attaching an error handler */
nucleosam 0:cd62cd5b2c77 335 motor->AttachErrorHandler(&myErrorHandler);
nucleosam 0:cd62cd5b2c77 336
nucleosam 0:cd62cd5b2c77 337 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 338 printf("Motor Control Application Example for 1 Motor\r\n");
nucleosam 0:cd62cd5b2c77 339
nucleosam 0:cd62cd5b2c77 340 //----- Move of 16000 steps in the FW direction
nucleosam 0:cd62cd5b2c77 341 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 342 printf("--> Moving forward 16000 steps.\r\n");
nucleosam 0:cd62cd5b2c77 343
nucleosam 0:cd62cd5b2c77 344 /* Move 16000 steps in the FORWARD direction*/
nucleosam 0:cd62cd5b2c77 345 motor->Move(StepperMotor::FWD, 16000);
nucleosam 0:cd62cd5b2c77 346
nucleosam 0:cd62cd5b2c77 347 /* Waiting while the motor is active. */
nucleosam 0:cd62cd5b2c77 348 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 349
nucleosam 0:cd62cd5b2c77 350 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 351 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 352
nucleosam 0:cd62cd5b2c77 353 //----- Move of 16000 steps in the BW direction
nucleosam 0:cd62cd5b2c77 354 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 355 printf("--> Moving backward 16000 steps.\r\n");
nucleosam 0:cd62cd5b2c77 356
nucleosam 0:cd62cd5b2c77 357 /* Move 16000 steps in the BACKWARD direction*/
nucleosam 0:cd62cd5b2c77 358 motor->Move(StepperMotor::BWD, 16000);
nucleosam 0:cd62cd5b2c77 359
nucleosam 0:cd62cd5b2c77 360 /* Waiting while the motor is active. */
nucleosam 0:cd62cd5b2c77 361 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 362
nucleosam 0:cd62cd5b2c77 363 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 364 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 365
nucleosam 0:cd62cd5b2c77 366 //----- Go to position -6400
nucleosam 0:cd62cd5b2c77 367 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 368 printf("--> Go to position -6400 steps.\r\n");
nucleosam 0:cd62cd5b2c77 369
nucleosam 0:cd62cd5b2c77 370 /* Request device to go to position -6400 */
nucleosam 0:cd62cd5b2c77 371 motor->GoTo(-6400);
nucleosam 0:cd62cd5b2c77 372
nucleosam 0:cd62cd5b2c77 373 /* Wait for the motor ends moving */
nucleosam 0:cd62cd5b2c77 374 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 375
nucleosam 0:cd62cd5b2c77 376 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 377 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 378 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 379
nucleosam 0:cd62cd5b2c77 380 /* Mark the current position and print to the console */
nucleosam 0:cd62cd5b2c77 381 motor->SetMark();
nucleosam 0:cd62cd5b2c77 382 printf(" Position marked: -6400.\r\n");
nucleosam 0:cd62cd5b2c77 383
nucleosam 0:cd62cd5b2c77 384 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 385 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 386
nucleosam 0:cd62cd5b2c77 387 //----- Go Home
nucleosam 0:cd62cd5b2c77 388 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 389 printf("--> Go to home position.\r\n");
nucleosam 0:cd62cd5b2c77 390
nucleosam 0:cd62cd5b2c77 391 /* Request device to go to Home */
nucleosam 0:cd62cd5b2c77 392 motor->GoHome();
nucleosam 0:cd62cd5b2c77 393 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 394
nucleosam 0:cd62cd5b2c77 395 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 396 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 397 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 398
nucleosam 0:cd62cd5b2c77 399 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 400 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 401
nucleosam 0:cd62cd5b2c77 402 //----- Go to position 6400
nucleosam 0:cd62cd5b2c77 403 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 404 printf("--> Go to position 6400 steps.\r\n");
nucleosam 0:cd62cd5b2c77 405
nucleosam 0:cd62cd5b2c77 406 /* Request device to go to position 6400 */
nucleosam 0:cd62cd5b2c77 407 motor->GoTo(6400);
nucleosam 0:cd62cd5b2c77 408
nucleosam 0:cd62cd5b2c77 409 /* Wait for the motor of device ends moving */
nucleosam 0:cd62cd5b2c77 410 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 411
nucleosam 0:cd62cd5b2c77 412 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 413 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 414 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 415
nucleosam 0:cd62cd5b2c77 416 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 417 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 418
nucleosam 0:cd62cd5b2c77 419 //----- Go Mark which was set previously after go to -6400
nucleosam 0:cd62cd5b2c77 420 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 421 printf("--> Go to the marked position.\r\n");
nucleosam 0:cd62cd5b2c77 422
nucleosam 0:cd62cd5b2c77 423 /* Request device to go to Mark position */
nucleosam 0:cd62cd5b2c77 424 motor->GoMark();
nucleosam 0:cd62cd5b2c77 425
nucleosam 0:cd62cd5b2c77 426 /* Wait for the motor of device ends moving */
nucleosam 0:cd62cd5b2c77 427 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 428
nucleosam 0:cd62cd5b2c77 429 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 430 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 431 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 432
nucleosam 0:cd62cd5b2c77 433 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 434 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 435
nucleosam 0:cd62cd5b2c77 436 //----- Run the motor BACKWARD
nucleosam 0:cd62cd5b2c77 437 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 438 printf("--> Run the motor backward at 400 step/s.\r\n");
nucleosam 0:cd62cd5b2c77 439
nucleosam 0:cd62cd5b2c77 440 /* Request device to run BACKWARD at 400 step/s */
nucleosam 0:cd62cd5b2c77 441 motor->Run(StepperMotor::BWD,400);
nucleosam 0:cd62cd5b2c77 442
nucleosam 0:cd62cd5b2c77 443 //----- Get parameter example
nucleosam 0:cd62cd5b2c77 444 /* Wait for device reaches the targeted speed */
nucleosam 0:cd62cd5b2c77 445 while((motor->ReadStatusRegister() & POWERSTEP01_STATUS_MOT_STATUS)!=
nucleosam 0:cd62cd5b2c77 446 POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD);
nucleosam 0:cd62cd5b2c77 447
nucleosam 0:cd62cd5b2c77 448 /* Record the reached speed in step/s */
nucleosam 0:cd62cd5b2c77 449 floatValue = motor->GetAnalogValue(POWERSTEP01_SPEED);
nucleosam 0:cd62cd5b2c77 450 /* Print reached speed to the console in step/s */
nucleosam 0:cd62cd5b2c77 451 printf(" Reached Speed: %f step/s.\r\n", floatValue);
nucleosam 0:cd62cd5b2c77 452
nucleosam 0:cd62cd5b2c77 453 /* Record the reached speed in step/s rounded to integer */
nucleosam 0:cd62cd5b2c77 454 unsignedIntegerValue = motor->GetSpeed();
nucleosam 0:cd62cd5b2c77 455 /* Print reached speed to the console in step/s */
nucleosam 0:cd62cd5b2c77 456 printf(" Reached Speed: %d step/s.\r\n", unsignedIntegerValue);
nucleosam 0:cd62cd5b2c77 457
nucleosam 0:cd62cd5b2c77 458 /* Record the reached speed in step/tick */
nucleosam 1:6b179be4937e 459 unsignedIntegerValue = motor->GetRawParameter(POWERSTEP01_SPEED);
nucleosam 0:cd62cd5b2c77 460 /* Print reached speed to the console in step/tick */
nucleosam 0:cd62cd5b2c77 461 printf(" Reached Speed: %d step/tick.\r\n", unsignedIntegerValue);
nucleosam 0:cd62cd5b2c77 462
nucleosam 0:cd62cd5b2c77 463 //----- Soft stopped required while running
nucleosam 0:cd62cd5b2c77 464 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 465 printf("--> Soft stop requested.\r\n");
nucleosam 0:cd62cd5b2c77 466
nucleosam 0:cd62cd5b2c77 467 /* Request a soft stop of device and keep the power bridges enabled */
nucleosam 0:cd62cd5b2c77 468 motor->SoftHiZ();
nucleosam 0:cd62cd5b2c77 469
nucleosam 0:cd62cd5b2c77 470 /* Wait for the motor of device ends moving */
nucleosam 0:cd62cd5b2c77 471 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 472
nucleosam 0:cd62cd5b2c77 473 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 474 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 475
nucleosam 0:cd62cd5b2c77 476 //----- Run stopped by hardstop
nucleosam 0:cd62cd5b2c77 477 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 478 printf("--> Run the motor forward at 300 step/s.\r\n");
nucleosam 0:cd62cd5b2c77 479
nucleosam 0:cd62cd5b2c77 480 /* Request device to run in FORWARD direction at 300 step/s */
nucleosam 0:cd62cd5b2c77 481 motor->Run(StepperMotor::FWD,300);
nucleosam 0:cd62cd5b2c77 482 wait_ms(5000);
nucleosam 0:cd62cd5b2c77 483
nucleosam 0:cd62cd5b2c77 484 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 485 printf("--> Hard stop requested.\r\n");
nucleosam 0:cd62cd5b2c77 486
nucleosam 0:cd62cd5b2c77 487 /* Request device to immediatly stop */
nucleosam 0:cd62cd5b2c77 488 motor->HardStop();
nucleosam 0:cd62cd5b2c77 489 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 490
nucleosam 0:cd62cd5b2c77 491 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 492 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 493
nucleosam 0:cd62cd5b2c77 494 //----- GOTO stopped by soft stop
nucleosam 0:cd62cd5b2c77 495 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 496 printf("--> Go to position 200000 steps.\r\n");
nucleosam 0:cd62cd5b2c77 497
nucleosam 0:cd62cd5b2c77 498 /* Request device to go to position 200000 */
nucleosam 0:cd62cd5b2c77 499 motor->GoTo(200000);
nucleosam 0:cd62cd5b2c77 500 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 501
nucleosam 0:cd62cd5b2c77 502 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 503 printf("--> Soft stop requested.\r\n");
nucleosam 0:cd62cd5b2c77 504
nucleosam 0:cd62cd5b2c77 505 /* Request device to perform a soft stop */
nucleosam 0:cd62cd5b2c77 506 motor->SoftStop();
nucleosam 0:cd62cd5b2c77 507 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 508
nucleosam 0:cd62cd5b2c77 509 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 510 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 511 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 512
nucleosam 0:cd62cd5b2c77 513 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 514 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 515
nucleosam 0:cd62cd5b2c77 516 //----- Read inexistent register to test MyFlagInterruptHandler
nucleosam 0:cd62cd5b2c77 517 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 518 printf("--> Try to read an inexistent register.\r\n");
nucleosam 0:cd62cd5b2c77 519
nucleosam 0:cd62cd5b2c77 520 /* Try to read an inexistent register */
nucleosam 0:cd62cd5b2c77 521 /* the flag interrupt should be raised */
nucleosam 0:cd62cd5b2c77 522 /* and the MyFlagInterruptHandler function called */
nucleosam 1:6b179be4937e 523 motor->GetRawParameter(0x1F);
nucleosam 0:cd62cd5b2c77 524
nucleosam 0:cd62cd5b2c77 525 //----- Put the bridges in high impedance
nucleosam 0:cd62cd5b2c77 526 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 527 printf("--> Request disabling of device power bridges.\r\n");
nucleosam 0:cd62cd5b2c77 528
nucleosam 0:cd62cd5b2c77 529 /* Request disabling of device power bridges */
nucleosam 0:cd62cd5b2c77 530 motor->HardHiZ();
nucleosam 0:cd62cd5b2c77 531 wait_ms(5000);
nucleosam 0:cd62cd5b2c77 532
nucleosam 0:cd62cd5b2c77 533 //----- Step clock mode example
nucleosam 0:cd62cd5b2c77 534 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 535 printf("--> Enable Step Clock Mode of the Powerstep01, enabling power bridges.\r\n");
nucleosam 0:cd62cd5b2c77 536
nucleosam 0:cd62cd5b2c77 537 /* Enable Step Clock Mode of the Powerstep01, enabling power bridges */
nucleosam 0:cd62cd5b2c77 538 motor->StepClockModeEnable(StepperMotor::FWD);
nucleosam 0:cd62cd5b2c77 539
nucleosam 0:cd62cd5b2c77 540 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 541 printf("--> Start step clock.\r\n");
nucleosam 0:cd62cd5b2c77 542
nucleosam 0:cd62cd5b2c77 543 /* Enable the step clock at 333 Hz */
nucleosam 0:cd62cd5b2c77 544 motor->StepClockStart(333);
nucleosam 0:cd62cd5b2c77 545
nucleosam 0:cd62cd5b2c77 546 /* Let the motor runs for 5 second at 333 step/s */
nucleosam 0:cd62cd5b2c77 547 wait_ms(5000);
nucleosam 0:cd62cd5b2c77 548
nucleosam 0:cd62cd5b2c77 549 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 550 printf("--> Stop step clock.\r\n");
nucleosam 0:cd62cd5b2c77 551
nucleosam 0:cd62cd5b2c77 552 /* Stop the step clock */
nucleosam 0:cd62cd5b2c77 553 motor->StepClockStop();
nucleosam 0:cd62cd5b2c77 554
nucleosam 0:cd62cd5b2c77 555 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 556 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 557
nucleosam 0:cd62cd5b2c77 558 //----- Set parameter example
nucleosam 0:cd62cd5b2c77 559 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 560 printf("--> Set POWERSTEP01_MAX_SPEED parameter to 3.\r\n");
nucleosam 0:cd62cd5b2c77 561 printf(" This is corresponding to 45.75 full step/s.\r\n");
nucleosam 0:cd62cd5b2c77 562
nucleosam 0:cd62cd5b2c77 563 /* Change the maximum speed to 3*2^18 step/tick */
nucleosam 1:6b179be4937e 564 motor->SetRawParameter(POWERSTEP01_MAX_SPEED, 3);
nucleosam 0:cd62cd5b2c77 565
nucleosam 0:cd62cd5b2c77 566 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 567 printf("--> Move the motor 3200 steps in forward direction.\r\n");
nucleosam 0:cd62cd5b2c77 568
nucleosam 0:cd62cd5b2c77 569 /* Request device to move 3200 microsteps */
nucleosam 0:cd62cd5b2c77 570 motor->Move(StepperMotor::FWD,3200);
nucleosam 0:cd62cd5b2c77 571
nucleosam 0:cd62cd5b2c77 572 /* Wait for the motor ends moving */
nucleosam 0:cd62cd5b2c77 573 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 574
nucleosam 0:cd62cd5b2c77 575 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 576 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 577
nucleosam 0:cd62cd5b2c77 578 //----- Change step mode to full step mode
nucleosam 0:cd62cd5b2c77 579 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 580 printf("--> Select full step mode.\r\n");
nucleosam 0:cd62cd5b2c77 581
nucleosam 0:cd62cd5b2c77 582 /* Select full step mode for device */
nucleosam 1:6b179be4937e 583 motor->SetStepMode(StepperMotor::STEP_MODE_FULL);
nucleosam 0:cd62cd5b2c77 584
nucleosam 0:cd62cd5b2c77 585 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 586 printf("--> Set the device position to POWERSTEP01_MIN_POSITION + 199 (-2^21+199).\r\n");
nucleosam 0:cd62cd5b2c77 587
nucleosam 0:cd62cd5b2c77 588 /* Set the device position to POWERSTEP01_MIN_POSITION + 199 */
nucleosam 1:6b179be4937e 589 motor->SetRawParameter(POWERSTEP01_ABS_POS,(uint32_t)(POWERSTEP01_MIN_POSITION+199));
nucleosam 0:cd62cd5b2c77 590
nucleosam 0:cd62cd5b2c77 591 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 592 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 593 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 594
nucleosam 0:cd62cd5b2c77 595 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 596 printf("--> Go to POWERSTEP01_MAX_POSITION (2^21-1) using the shortest path.\r\n");
nucleosam 0:cd62cd5b2c77 597
nucleosam 0:cd62cd5b2c77 598 /* Request device to go to the POWERSTEP01_MAX_POSITION using the shortest path */
nucleosam 0:cd62cd5b2c77 599 motor->GoTo(POWERSTEP01_MAX_POSITION);
nucleosam 0:cd62cd5b2c77 600
nucleosam 0:cd62cd5b2c77 601 /* Wait for the motor of device ends moving */
nucleosam 0:cd62cd5b2c77 602 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 603
nucleosam 0:cd62cd5b2c77 604 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 605 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 606 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 607
nucleosam 0:cd62cd5b2c77 608 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 609 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 610
nucleosam 0:cd62cd5b2c77 611 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 612 printf("--> Set the device position to POWERSTEP01_MIN_POSITION + 199 (-2^21+199).\r\n");
nucleosam 0:cd62cd5b2c77 613
nucleosam 0:cd62cd5b2c77 614 /* Set the device position to POWERSTEP01_MIN_POSITION + 199 */
nucleosam 1:6b179be4937e 615 motor->SetRawParameter(POWERSTEP01_ABS_POS,(uint32_t)(POWERSTEP01_MIN_POSITION+199));
nucleosam 0:cd62cd5b2c77 616
nucleosam 0:cd62cd5b2c77 617 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 618 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 619 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 620
nucleosam 0:cd62cd5b2c77 621 //----- GoTo_DIR example
nucleosam 0:cd62cd5b2c77 622 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 623 printf("--> Go to the POWERSTEP01_MAX_POSITION using backward direction.\r\n");
nucleosam 0:cd62cd5b2c77 624
nucleosam 0:cd62cd5b2c77 625 /* Request device to go to the POWERSTEP01_MAX_POSITION using backward direction */
nucleosam 0:cd62cd5b2c77 626 motor->GoTo(StepperMotor::BWD,POWERSTEP01_MAX_POSITION);
nucleosam 0:cd62cd5b2c77 627
nucleosam 0:cd62cd5b2c77 628 /* Wait for the motor of device ends moving */
nucleosam 0:cd62cd5b2c77 629 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 630
nucleosam 0:cd62cd5b2c77 631 /* Get current position of device and print to the console */
nucleosam 0:cd62cd5b2c77 632 pos = motor->GetPosition();
nucleosam 0:cd62cd5b2c77 633 printf(" Position: %d.\r\n", pos);
nucleosam 0:cd62cd5b2c77 634
nucleosam 0:cd62cd5b2c77 635 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 636 printf("--> Restore device initial max speed.\r\n");
nucleosam 0:cd62cd5b2c77 637
nucleosam 0:cd62cd5b2c77 638 /* Restore device initial max speed */
nucleosam 0:cd62cd5b2c77 639 motor->SetAnalogValue(POWERSTEP01_MAX_SPEED, POWERSTEP01_CONF_PARAM_MAX_SPEED_DEVICE_0);
nucleosam 0:cd62cd5b2c77 640
nucleosam 0:cd62cd5b2c77 641 /* Get the maximum speed in step/s */
nucleosam 0:cd62cd5b2c77 642 floatValue = motor->GetAnalogValue(POWERSTEP01_MAX_SPEED);
nucleosam 0:cd62cd5b2c77 643 /* Print maximum speed to the console in step/s */
nucleosam 0:cd62cd5b2c77 644 printf(" Maximum Speed: %f step/s.\r\n", floatValue);
nucleosam 0:cd62cd5b2c77 645
nucleosam 0:cd62cd5b2c77 646 //----- Restore initial microstepping mode
nucleosam 0:cd62cd5b2c77 647 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 648 printf("--> Restore device initial step mode.\r\n");
nucleosam 0:cd62cd5b2c77 649
nucleosam 0:cd62cd5b2c77 650 /* Reset device to its initial microstepping mode */
nucleosam 1:6b179be4937e 651 motor->SetStepMode((StepperMotor::step_mode_t)POWERSTEP01_CONF_PARAM_STEP_MODE_DEVICE_0);
nucleosam 0:cd62cd5b2c77 652
nucleosam 0:cd62cd5b2c77 653 /* Wait for 2 seconds */
nucleosam 0:cd62cd5b2c77 654 wait_ms(2000);
nucleosam 0:cd62cd5b2c77 655
nucleosam 0:cd62cd5b2c77 656 /* Printing to the console. */
nucleosam 0:cd62cd5b2c77 657 printf("--> Infinite Loop...\r\n");
nucleosam 0:cd62cd5b2c77 658 /* Infinite Loop. */
nucleosam 0:cd62cd5b2c77 659 while (1)
nucleosam 0:cd62cd5b2c77 660 {
nucleosam 0:cd62cd5b2c77 661 /* Request device to go position -6400 */
nucleosam 0:cd62cd5b2c77 662 motor->GoTo(-6400);
nucleosam 0:cd62cd5b2c77 663
nucleosam 0:cd62cd5b2c77 664 /* Waiting while the motor is active. */
nucleosam 0:cd62cd5b2c77 665 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 666
nucleosam 0:cd62cd5b2c77 667 /* Request device to go position 6400 */
nucleosam 0:cd62cd5b2c77 668 motor->GoTo(6400);
nucleosam 0:cd62cd5b2c77 669
nucleosam 0:cd62cd5b2c77 670 /* Waiting while the motor is active. */
nucleosam 0:cd62cd5b2c77 671 motor->WaitWhileActive();
nucleosam 0:cd62cd5b2c77 672 }
nucleosam 0:cd62cd5b2c77 673 }
nucleosam 0:cd62cd5b2c77 674 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/