Simple program featuring a few API functions usage of the X_NUCLEO_IHM03A1 library.
Dependencies: X_NUCLEO_IHM03A1 mbed
Fork of HelloWorld_IHM03A1 by
This application provides a simple 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.
For the hardware configuration of the expansion board, please refer to the X_NUCLEO_IHM03A1 home web page.
More API usage is available in IHM03A1_ExampleFor1Motor and IHM03A1_ExampleFor3Motors programs.
main.cpp@6:39b4eed48750, 2018-08-09 (annotated)
- Committer:
- Davidroid
- Date:
- Thu Aug 09 15:10:34 2018 +0000
- Revision:
- 6:39b4eed48750
- Parent:
- 5:f5ceb23e3590
Update with the new version of the libary
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucleosam | 0:c44075c08a18 | 1 | /** |
nucleosam | 0:c44075c08a18 | 2 | ****************************************************************************** |
nucleosam | 0:c44075c08a18 | 3 | * @file main.cpp |
nucleosam | 0:c44075c08a18 | 4 | * @author IPC Rennes |
nucleosam | 0:c44075c08a18 | 5 | * @version V1.0.0 |
nucleosam | 0:c44075c08a18 | 6 | * @date April 13th, 2016 |
nucleosam | 0:c44075c08a18 | 7 | * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM03A1 |
nucleosam | 0:c44075c08a18 | 8 | * Motor Control Expansion Board: control of 1 motor. |
nucleosam | 0:c44075c08a18 | 9 | ****************************************************************************** |
nucleosam | 0:c44075c08a18 | 10 | * @attention |
nucleosam | 0:c44075c08a18 | 11 | * |
nucleosam | 0:c44075c08a18 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nucleosam | 0:c44075c08a18 | 13 | * |
nucleosam | 0:c44075c08a18 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
nucleosam | 0:c44075c08a18 | 15 | * are permitted provided that the following conditions are met: |
nucleosam | 0:c44075c08a18 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
nucleosam | 0:c44075c08a18 | 17 | * this list of conditions and the following disclaimer. |
nucleosam | 0:c44075c08a18 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nucleosam | 0:c44075c08a18 | 19 | * this list of conditions and the following disclaimer in the documentation |
nucleosam | 0:c44075c08a18 | 20 | * and/or other materials provided with the distribution. |
nucleosam | 0:c44075c08a18 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nucleosam | 0:c44075c08a18 | 22 | * may be used to endorse or promote products derived from this software |
nucleosam | 0:c44075c08a18 | 23 | * without specific prior written permission. |
nucleosam | 0:c44075c08a18 | 24 | * |
nucleosam | 0:c44075c08a18 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nucleosam | 0:c44075c08a18 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nucleosam | 0:c44075c08a18 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nucleosam | 0:c44075c08a18 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nucleosam | 0:c44075c08a18 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nucleosam | 0:c44075c08a18 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nucleosam | 0:c44075c08a18 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nucleosam | 0:c44075c08a18 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nucleosam | 0:c44075c08a18 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nucleosam | 0:c44075c08a18 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nucleosam | 0:c44075c08a18 | 35 | * |
nucleosam | 0:c44075c08a18 | 36 | ****************************************************************************** |
nucleosam | 0:c44075c08a18 | 37 | */ |
nucleosam | 0:c44075c08a18 | 38 | |
nucleosam | 0:c44075c08a18 | 39 | /* Includes ------------------------------------------------------------------*/ |
nucleosam | 0:c44075c08a18 | 40 | |
nucleosam | 0:c44075c08a18 | 41 | /* mbed specific header files. */ |
nucleosam | 0:c44075c08a18 | 42 | #include "mbed.h" |
nucleosam | 0:c44075c08a18 | 43 | |
nucleosam | 0:c44075c08a18 | 44 | /* Helper header files. */ |
nucleosam | 0:c44075c08a18 | 45 | #include "DevSPI.h" |
nucleosam | 0:c44075c08a18 | 46 | |
nucleosam | 0:c44075c08a18 | 47 | /* Component specific header files. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 48 | #include "PowerStep01.h" |
nucleosam | 0:c44075c08a18 | 49 | |
nucleosam | 0:c44075c08a18 | 50 | /* Variables -----------------------------------------------------------------*/ |
nucleosam | 0:c44075c08a18 | 51 | |
nucleosam | 0:c44075c08a18 | 52 | /* Initialization parameters of the motor connected to the expansion board. */ |
nucleosam | 0:c44075c08a18 | 53 | /* Current mode. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 54 | powerstep01_init_u_t init = |
nucleosam | 0:c44075c08a18 | 55 | { |
nucleosam | 0:c44075c08a18 | 56 | /* common parameters */ |
nucleosam | 0:c44075c08a18 | 57 | .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t |
nucleosam | 0:c44075c08a18 | 58 | 582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2 |
nucleosam | 0:c44075c08a18 | 59 | 582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2 |
nucleosam | 0:c44075c08a18 | 60 | 488, // Maximum speed in step/s, range 15.25 to 15610 steps/s |
nucleosam | 0:c44075c08a18 | 61 | 0, // Minimum speed in step/s, range 0 to 976.3 steps/s |
nucleosam | 0:c44075c08a18 | 62 | POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t |
nucleosam | 0:c44075c08a18 | 63 | 244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s |
nucleosam | 0:c44075c08a18 | 64 | POWERSTEP01_BOOST_MODE_OFF, // Boost of the amplitude square wave, enum powerstep01_BoostMode_t |
nucleosam | 0:c44075c08a18 | 65 | 281.25, // Overcurrent threshold settings via enum powerstep01_OcdTh_t |
nucleosam | 0:c44075c08a18 | 66 | STEP_MODE_1_16, // Step mode settings via enum motorStepMode_t |
nucleosam | 0:c44075c08a18 | 67 | POWERSTEP01_SYNC_SEL_DISABLED, // Synch. Mode settings via enum powerstep01_SyncSel_t |
nucleosam | 0:c44075c08a18 | 68 | (POWERSTEP01_ALARM_EN_OVERCURRENT| |
nucleosam | 0:c44075c08a18 | 69 | POWERSTEP01_ALARM_EN_THERMAL_SHUTDOWN| |
nucleosam | 0:c44075c08a18 | 70 | POWERSTEP01_ALARM_EN_THERMAL_WARNING| |
nucleosam | 0:c44075c08a18 | 71 | POWERSTEP01_ALARM_EN_UVLO| |
nucleosam | 0:c44075c08a18 | 72 | POWERSTEP01_ALARM_EN_STALL_DETECTION| |
nucleosam | 0:c44075c08a18 | 73 | POWERSTEP01_ALARM_EN_SW_TURN_ON| |
nucleosam | 0:c44075c08a18 | 74 | POWERSTEP01_ALARM_EN_WRONG_NPERF_CMD), // Alarm settings via bitmap enum powerstep01_AlarmEn_t |
nucleosam | 0:c44075c08a18 | 75 | POWERSTEP01_IGATE_64mA, // Gate sink/source current via enum powerstep01_Igate_t |
nucleosam | 0:c44075c08a18 | 76 | POWERSTEP01_TBOOST_0ns, // Duration of the overboost phase during gate turn-off via enum powerstep01_Tboost_t |
nucleosam | 0:c44075c08a18 | 77 | POWERSTEP01_TCC_500ns, // Controlled current time via enum powerstep01_Tcc_t |
nucleosam | 0:c44075c08a18 | 78 | POWERSTEP01_WD_EN_DISABLE, // External clock watchdog, enum powerstep01_WdEn_t |
nucleosam | 0:c44075c08a18 | 79 | POWERSTEP01_TBLANK_375ns, // Duration of the blanking time via enum powerstep01_TBlank_t |
nucleosam | 0:c44075c08a18 | 80 | POWERSTEP01_TDT_125ns, // Duration of the dead time via enum powerstep01_Tdt_t |
nucleosam | 0:c44075c08a18 | 81 | /* current mode parameters */ |
nucleosam | 0:c44075c08a18 | 82 | 328.12, // Hold torque in mV, range from 7.8mV to 1000 mV |
nucleosam | 0:c44075c08a18 | 83 | 328.12, // Running torque in mV, range from 7.8mV to 1000 mV |
nucleosam | 0:c44075c08a18 | 84 | 328.12, // Acceleration torque in mV, range from 7.8mV to 1000 mV |
nucleosam | 0:c44075c08a18 | 85 | 328.12, // Deceleration torque in mV, range from 7.8mV to 1000 mV |
nucleosam | 0:c44075c08a18 | 86 | POWERSTEP01_TOFF_FAST_8us, //Maximum fast decay time , enum powerstep01_ToffFast_t |
nucleosam | 0:c44075c08a18 | 87 | POWERSTEP01_FAST_STEP_12us, //Maximum fall step time , enum powerstep01_FastStep_t |
nucleosam | 0:c44075c08a18 | 88 | 3.0, // Minimum on-time in us, range 0.5us to 64us |
nucleosam | 0:c44075c08a18 | 89 | 21.0, // Minimum off-time in us, range 0.5us to 64us |
nucleosam | 0:c44075c08a18 | 90 | POWERSTEP01_CONFIG_INT_16MHZ_OSCOUT_2MHZ, // Clock setting , enum powerstep01_ConfigOscMgmt_t |
nucleosam | 0:c44075c08a18 | 91 | POWERSTEP01_CONFIG_SW_HARD_STOP, // External switch hard stop interrupt mode, enum powerstep01_ConfigSwMode_t |
nucleosam | 0:c44075c08a18 | 92 | POWERSTEP01_CONFIG_TQ_REG_TVAL_USED, // External torque regulation enabling , enum powerstep01_ConfigEnTqReg_t |
nucleosam | 0:c44075c08a18 | 93 | POWERSTEP01_CONFIG_VS_COMP_DISABLE, // Motor Supply Voltage Compensation enabling , enum powerstep01_ConfigEnVscomp_t |
nucleosam | 0:c44075c08a18 | 94 | POWERSTEP01_CONFIG_OC_SD_DISABLE, // Over current shutwdown enabling, enum powerstep01_ConfigOcSd_t |
nucleosam | 0:c44075c08a18 | 95 | POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t |
nucleosam | 0:c44075c08a18 | 96 | POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t |
nucleosam | 0:c44075c08a18 | 97 | POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t |
nucleosam | 0:c44075c08a18 | 98 | POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t |
nucleosam | 0:c44075c08a18 | 99 | }; |
nucleosam | 0:c44075c08a18 | 100 | |
nucleosam | 0:c44075c08a18 | 101 | /* Motor Control Component. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 102 | PowerStep01 *motor; |
nucleosam | 0:c44075c08a18 | 103 | |
nucleosam | 0:c44075c08a18 | 104 | /* Functions -----------------------------------------------------------------*/ |
nucleosam | 0:c44075c08a18 | 105 | |
nucleosam | 0:c44075c08a18 | 106 | /** |
nucleosam | 0:c44075c08a18 | 107 | * @brief This is an example of user handler for the flag interrupt. |
nucleosam | 0:c44075c08a18 | 108 | * @param None |
nucleosam | 0:c44075c08a18 | 109 | * @retval None |
nucleosam | 0:c44075c08a18 | 110 | * @note If needed, implement it, and then attach and enable it: |
davide.aliprandi@st.com | 2:f20ed233a489 | 111 | * + motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 2:f20ed233a489 | 112 | * + motor->enable_flag_irq(); |
nucleosam | 0:c44075c08a18 | 113 | * To disable it: |
nucleosam | 0:c44075c08a18 | 114 | * + motor->DisbleFlagIRQ(); |
nucleosam | 0:c44075c08a18 | 115 | */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 116 | void my_flag_irq_handler(void) |
nucleosam | 0:c44075c08a18 | 117 | { |
nucleosam | 0:c44075c08a18 | 118 | /* Set ISR flag. */ |
nucleosam | 0:c44075c08a18 | 119 | motor->isrFlag = TRUE; |
nucleosam | 0:c44075c08a18 | 120 | /* Get the value of the status register. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 121 | unsigned int statusRegister = motor->get_status(); |
nucleosam | 0:c44075c08a18 | 122 | printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); |
nucleosam | 0:c44075c08a18 | 123 | /* Check SW_F flag: if not set, the SW input is opened */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 124 | if ((statusRegister & POWERSTEP01_STATUS_SW_F ) != 0) { |
davide.aliprandi@st.com | 2:f20ed233a489 | 125 | printf(" SW closed (connected to ground).\r\n"); |
nucleosam | 0:c44075c08a18 | 126 | } |
nucleosam | 0:c44075c08a18 | 127 | /* Check SW_EN bit */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 128 | if ((statusRegister & POWERSTEP01_STATUS_SW_EVN) == POWERSTEP01_STATUS_SW_EVN) { |
davide.aliprandi@st.com | 2:f20ed233a489 | 129 | printf(" SW turn_on event.\r\n"); |
nucleosam | 0:c44075c08a18 | 130 | } |
nucleosam | 0:c44075c08a18 | 131 | /* Check Command Error flag: if set, the command received by SPI can't be */ |
nucleosam | 0:c44075c08a18 | 132 | /* performed. This occurs for instance when a move command is sent to the */ |
nucleosam | 0:c44075c08a18 | 133 | /* Powerstep01 while it is already running */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 134 | if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR) == POWERSTEP01_STATUS_CMD_ERROR) { |
nucleosam | 0:c44075c08a18 | 135 | printf(" Non-performable command detected.\r\n"); |
nucleosam | 0:c44075c08a18 | 136 | } |
nucleosam | 0:c44075c08a18 | 137 | /* Check UVLO flag: if not set, there is an undervoltage lock-out */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 138 | if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) { |
davide.aliprandi@st.com | 2:f20ed233a489 | 139 | printf(" undervoltage lock-out.\r\n"); |
nucleosam | 0:c44075c08a18 | 140 | } |
nucleosam | 0:c44075c08a18 | 141 | /* Check thermal STATUS flags: if set, the thermal status is not normal */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 142 | if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) { |
nucleosam | 0:c44075c08a18 | 143 | //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown |
nucleosam | 0:c44075c08a18 | 144 | printf(" Thermal status: %d.\r\n", (statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11); |
nucleosam | 0:c44075c08a18 | 145 | } |
nucleosam | 0:c44075c08a18 | 146 | /* Check OCD flag: if not set, there is an overcurrent detection */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 147 | if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) { |
nucleosam | 0:c44075c08a18 | 148 | printf(" Overcurrent detection.\r\n"); |
nucleosam | 0:c44075c08a18 | 149 | } |
nucleosam | 0:c44075c08a18 | 150 | /* Reset ISR flag. */ |
nucleosam | 0:c44075c08a18 | 151 | motor->isrFlag = FALSE; |
nucleosam | 0:c44075c08a18 | 152 | } |
nucleosam | 0:c44075c08a18 | 153 | |
nucleosam | 0:c44075c08a18 | 154 | /** |
nucleosam | 0:c44075c08a18 | 155 | * @brief This is an example of error handler. |
nucleosam | 0:c44075c08a18 | 156 | * @param[in] error Number of the error |
nucleosam | 0:c44075c08a18 | 157 | * @retval None |
nucleosam | 0:c44075c08a18 | 158 | * @note If needed, implement it, and then attach it: |
davide.aliprandi@st.com | 2:f20ed233a489 | 159 | * + motor->attach_error_handler(&my_error_handler); |
nucleosam | 0:c44075c08a18 | 160 | */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 161 | void my_error_handler(uint16_t error) |
nucleosam | 0:c44075c08a18 | 162 | { |
nucleosam | 0:c44075c08a18 | 163 | /* Printing to the console. */ |
nucleosam | 0:c44075c08a18 | 164 | printf("Error %d detected\r\n\n", error); |
nucleosam | 0:c44075c08a18 | 165 | |
nucleosam | 0:c44075c08a18 | 166 | /* Infinite loop */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 167 | while (true) { |
nucleosam | 0:c44075c08a18 | 168 | } |
nucleosam | 0:c44075c08a18 | 169 | } |
nucleosam | 0:c44075c08a18 | 170 | |
nucleosam | 0:c44075c08a18 | 171 | /* Main ----------------------------------------------------------------------*/ |
nucleosam | 0:c44075c08a18 | 172 | |
nucleosam | 0:c44075c08a18 | 173 | int main() |
nucleosam | 0:c44075c08a18 | 174 | { |
nucleosam | 0:c44075c08a18 | 175 | /* Printing to the console. */ |
nucleosam | 0:c44075c08a18 | 176 | printf("STARTING MAIN PROGRAM\r\n"); |
nucleosam | 0:c44075c08a18 | 177 | printf(" Reminder:\r\n"); |
nucleosam | 0:c44075c08a18 | 178 | printf(" The position unit is in agreement to the step mode.\r\n"); |
nucleosam | 0:c44075c08a18 | 179 | printf(" The speed, acceleration or deceleration unit\r\n"); |
nucleosam | 0:c44075c08a18 | 180 | printf(" do not depend on the step mode and the step unit is a full step.\r\n"); |
nucleosam | 0:c44075c08a18 | 181 | |
nucleosam | 0:c44075c08a18 | 182 | //----- Initialization |
nucleosam | 0:c44075c08a18 | 183 | /* Initializing SPI bus. */ |
nucleosam | 0:c44075c08a18 | 184 | DevSPI dev_spi(D11, D12, D13); |
nucleosam | 0:c44075c08a18 | 185 | |
nucleosam | 0:c44075c08a18 | 186 | /* Initializing Motor Control Component. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 187 | motor = new PowerStep01(D2, D4, D8, D9, D10, dev_spi); |
davide.aliprandi@st.com | 2:f20ed233a489 | 188 | if (motor->init(&init) != COMPONENT_OK) { |
davide.aliprandi@st.com | 2:f20ed233a489 | 189 | exit(EXIT_FAILURE); |
davide.aliprandi@st.com | 2:f20ed233a489 | 190 | } |
nucleosam | 0:c44075c08a18 | 191 | |
nucleosam | 0:c44075c08a18 | 192 | /* Attaching and enabling an interrupt handler. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 193 | motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 2:f20ed233a489 | 194 | motor->enable_flag_irq(); |
nucleosam | 0:c44075c08a18 | 195 | |
nucleosam | 0:c44075c08a18 | 196 | /* Attaching an error handler */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 197 | motor->attach_error_handler(&my_error_handler); |
nucleosam | 0:c44075c08a18 | 198 | |
nucleosam | 0:c44075c08a18 | 199 | /* Printing to the console. */ |
nucleosam | 0:c44075c08a18 | 200 | printf("Motor Control Application Example for 1 Motor\r\n"); |
nucleosam | 0:c44075c08a18 | 201 | |
davide.aliprandi@st.com | 2:f20ed233a489 | 202 | //----- move of 16000 steps in the FW direction |
nucleosam | 0:c44075c08a18 | 203 | printf("--> Moving forward 16000 steps.\r\n"); |
davide.aliprandi@st.com | 2:f20ed233a489 | 204 | motor->move(StepperMotor::FWD, 16000); |
nucleosam | 0:c44075c08a18 | 205 | |
nucleosam | 0:c44075c08a18 | 206 | /* Waiting while the motor is active. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 207 | motor->wait_while_active(); |
nucleosam | 0:c44075c08a18 | 208 | |
nucleosam | 0:c44075c08a18 | 209 | /* Wait for 2 seconds */ |
nucleosam | 0:c44075c08a18 | 210 | wait_ms(2000); |
nucleosam | 0:c44075c08a18 | 211 | |
nucleosam | 0:c44075c08a18 | 212 | //----- Go to position -6400 |
nucleosam | 0:c44075c08a18 | 213 | printf("--> Go to position -6400 steps.\r\n"); |
nucleosam | 0:c44075c08a18 | 214 | |
nucleosam | 0:c44075c08a18 | 215 | /* Request device to go to position -6400 */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 216 | motor->go_to(-6400); |
nucleosam | 0:c44075c08a18 | 217 | |
nucleosam | 0:c44075c08a18 | 218 | /* Wait for the motor ends moving */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 219 | motor->wait_while_active(); |
nucleosam | 0:c44075c08a18 | 220 | |
nucleosam | 0:c44075c08a18 | 221 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 222 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:c44075c08a18 | 223 | |
nucleosam | 0:c44075c08a18 | 224 | /* Wait for 2 seconds */ |
nucleosam | 0:c44075c08a18 | 225 | wait_ms(2000); |
nucleosam | 0:c44075c08a18 | 226 | |
nucleosam | 0:c44075c08a18 | 227 | //----- Go Home |
nucleosam | 0:c44075c08a18 | 228 | /* Printing to the console. */ |
nucleosam | 0:c44075c08a18 | 229 | printf("--> Go to home position.\r\n"); |
nucleosam | 0:c44075c08a18 | 230 | |
nucleosam | 0:c44075c08a18 | 231 | /* Request device to go to Home */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 232 | motor->go_home(); |
nucleosam | 0:c44075c08a18 | 233 | |
nucleosam | 0:c44075c08a18 | 234 | /* Wait for the motor ends moving */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 235 | motor->wait_while_active(); |
nucleosam | 0:c44075c08a18 | 236 | |
nucleosam | 0:c44075c08a18 | 237 | /* Wait for 2 seconds */ |
nucleosam | 0:c44075c08a18 | 238 | wait_ms(2000); |
nucleosam | 0:c44075c08a18 | 239 | |
davide.aliprandi@st.com | 2:f20ed233a489 | 240 | //----- run the motor BACKWARD at 400 step/s |
davide.aliprandi@st.com | 2:f20ed233a489 | 241 | printf("--> run the motor backward at 400 step/s.\r\n"); |
davide.aliprandi@st.com | 2:f20ed233a489 | 242 | motor->run(StepperMotor::BWD,400); |
nucleosam | 0:c44075c08a18 | 243 | |
nucleosam | 0:c44075c08a18 | 244 | //----- Get parameter example |
nucleosam | 0:c44075c08a18 | 245 | /* Wait for device reaches the targeted speed */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 246 | while ((motor->read_status_register() & POWERSTEP01_STATUS_MOT_STATUS) != POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD); |
nucleosam | 0:c44075c08a18 | 247 | |
nucleosam | 0:c44075c08a18 | 248 | /* Record the reached speed in step/s and print to the console */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 249 | printf(" Reached Speed: %f step/s.\r\n", motor->get_analog_value(POWERSTEP01_SPEED)); |
nucleosam | 0:c44075c08a18 | 250 | |
nucleosam | 0:c44075c08a18 | 251 | //----- Soft stopped required while running |
nucleosam | 0:c44075c08a18 | 252 | printf("--> Soft stop requested.\r\n"); |
nucleosam | 0:c44075c08a18 | 253 | |
nucleosam | 0:c44075c08a18 | 254 | /* Request a soft stop of device and keep the power bridges enabled */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 255 | motor->soft_hiz(); |
nucleosam | 0:c44075c08a18 | 256 | |
nucleosam | 0:c44075c08a18 | 257 | /* Wait for the motor of device ends moving */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 258 | motor->wait_while_active(); |
nucleosam | 0:c44075c08a18 | 259 | |
nucleosam | 0:c44075c08a18 | 260 | /* Wait for 2 seconds */ |
nucleosam | 0:c44075c08a18 | 261 | wait_ms(2000); |
nucleosam | 0:c44075c08a18 | 262 | |
nucleosam | 0:c44075c08a18 | 263 | /* Infinite Loop. */ |
nucleosam | 0:c44075c08a18 | 264 | printf("--> Infinite Loop...\r\n"); |
davide.aliprandi@st.com | 2:f20ed233a489 | 265 | while (true) { |
nucleosam | 0:c44075c08a18 | 266 | /* Request device to go position -6400 */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 267 | motor->go_to(-6400); |
nucleosam | 0:c44075c08a18 | 268 | |
nucleosam | 0:c44075c08a18 | 269 | /* Waiting while the motor is active. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 270 | motor->wait_while_active(); |
nucleosam | 0:c44075c08a18 | 271 | |
nucleosam | 0:c44075c08a18 | 272 | /* Request device to go position 6400 */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 273 | motor->go_to(6400); |
nucleosam | 0:c44075c08a18 | 274 | |
nucleosam | 0:c44075c08a18 | 275 | /* Waiting while the motor is active. */ |
davide.aliprandi@st.com | 2:f20ed233a489 | 276 | motor->wait_while_active(); |
nucleosam | 0:c44075c08a18 | 277 | } |
nucleosam | 0:c44075c08a18 | 278 | } |
nucleosam | 0:c44075c08a18 | 279 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |