Simple program featuring a few API functions usage of the X_NUCLEO_IHM05A1 library.
Dependencies: X-NUCLEO-IHM05A1 mbed
Fork of HelloWorld_IHM05A1 by
This application provides a simple example of usage of the X-NUCLEO-IHM05A1 Bipolar Stepper Motor Control Expansion Board.
It shows how to use a stepper motor connected to the board by:
- Running the motor;
- Monitoring the speed and the motor state;
- Setting/Getting the speed;
- Setting/Getting the step mode;
- Setting/Getting the acceleration and deceleration;
- Moving a defined number of steps or microsteps;
- Setting/Getting the direction;
- Setting/Getting the decay mode.
For the hardware configuration of the expansion board, please refer to the X_NUCLEO_IHM05A1 library web page.
main.cpp@8:baedd289a8cc, 2017-07-28 (annotated)
- Committer:
- Davidroid
- Date:
- Fri Jul 28 13:07:13 2017 +0000
- Revision:
- 8:baedd289a8cc
- Parent:
- 6:6fb625ef867a
Updating with new libraries.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucleosam | 0:36aa6787d4f9 | 1 | /** |
nucleosam | 0:36aa6787d4f9 | 2 | ****************************************************************************** |
nucleosam | 0:36aa6787d4f9 | 3 | * @file main.cpp |
nucleosam | 0:36aa6787d4f9 | 4 | * @author IPC Rennes |
nucleosam | 0:36aa6787d4f9 | 5 | * @version V1.0.0 |
nucleosam | 0:36aa6787d4f9 | 6 | * @date April 13th, 2016 |
nucleosam | 2:c5f455885df7 | 7 | * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM05A1 |
nucleosam | 0:36aa6787d4f9 | 8 | * Motor Control Expansion Board: control of 1 motor. |
nucleosam | 0:36aa6787d4f9 | 9 | ****************************************************************************** |
nucleosam | 0:36aa6787d4f9 | 10 | * @attention |
nucleosam | 0:36aa6787d4f9 | 11 | * |
nucleosam | 0:36aa6787d4f9 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nucleosam | 0:36aa6787d4f9 | 13 | * |
nucleosam | 0:36aa6787d4f9 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
nucleosam | 0:36aa6787d4f9 | 15 | * are permitted provided that the following conditions are met: |
nucleosam | 0:36aa6787d4f9 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
nucleosam | 0:36aa6787d4f9 | 17 | * this list of conditions and the following disclaimer. |
nucleosam | 0:36aa6787d4f9 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nucleosam | 0:36aa6787d4f9 | 19 | * this list of conditions and the following disclaimer in the documentation |
nucleosam | 0:36aa6787d4f9 | 20 | * and/or other materials provided with the distribution. |
nucleosam | 0:36aa6787d4f9 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nucleosam | 0:36aa6787d4f9 | 22 | * may be used to endorse or promote products derived from this software |
nucleosam | 0:36aa6787d4f9 | 23 | * without specific prior written permission. |
nucleosam | 0:36aa6787d4f9 | 24 | * |
nucleosam | 0:36aa6787d4f9 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nucleosam | 0:36aa6787d4f9 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nucleosam | 0:36aa6787d4f9 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nucleosam | 0:36aa6787d4f9 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nucleosam | 0:36aa6787d4f9 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nucleosam | 0:36aa6787d4f9 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nucleosam | 0:36aa6787d4f9 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nucleosam | 0:36aa6787d4f9 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nucleosam | 0:36aa6787d4f9 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nucleosam | 0:36aa6787d4f9 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nucleosam | 0:36aa6787d4f9 | 35 | * |
nucleosam | 0:36aa6787d4f9 | 36 | ****************************************************************************** |
nucleosam | 0:36aa6787d4f9 | 37 | */ |
nucleosam | 0:36aa6787d4f9 | 38 | |
nucleosam | 0:36aa6787d4f9 | 39 | /* Includes ------------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 40 | |
nucleosam | 0:36aa6787d4f9 | 41 | /* mbed specific header files. */ |
nucleosam | 0:36aa6787d4f9 | 42 | #include "mbed.h" |
nucleosam | 0:36aa6787d4f9 | 43 | |
nucleosam | 0:36aa6787d4f9 | 44 | /* Component specific header files. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 45 | #include "L6208.h" |
nucleosam | 0:36aa6787d4f9 | 46 | |
nucleosam | 0:36aa6787d4f9 | 47 | |
nucleosam | 0:36aa6787d4f9 | 48 | /* Definitions ---------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 49 | #ifdef TARGET_NUCLEO_F334R8 |
nucleosam | 0:36aa6787d4f9 | 50 | #define VREFA_PWM_PIN D11 |
nucleosam | 3:a6e155687c6a | 51 | #define VREFB_PWM_PIN D9 |
nucleosam | 3:a6e155687c6a | 52 | #elif TARGET_NUCLEO_F302R8 |
nucleosam | 3:a6e155687c6a | 53 | #define VREFA_PWM_PIN D11 |
nucleosam | 3:a6e155687c6a | 54 | #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */ |
nucleosam | 0:36aa6787d4f9 | 55 | #else |
nucleosam | 0:36aa6787d4f9 | 56 | #define VREFA_PWM_PIN D3 |
nucleosam | 3:a6e155687c6a | 57 | #define VREFB_PWM_PIN D9 |
nucleosam | 0:36aa6787d4f9 | 58 | #endif |
nucleosam | 0:36aa6787d4f9 | 59 | |
nucleosam | 0:36aa6787d4f9 | 60 | /* Variables -----------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 61 | |
nucleosam | 0:36aa6787d4f9 | 62 | /* Initialization parameters of the motor connected to the expansion board. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 63 | l6208_init_t init = |
nucleosam | 0:36aa6787d4f9 | 64 | { |
nucleosam | 0:36aa6787d4f9 | 65 | 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes |
nucleosam | 0:36aa6787d4f9 | 66 | 40, //Acceleration current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 67 | 1500, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes |
nucleosam | 0:36aa6787d4f9 | 68 | 30, //Deceleration current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 69 | 1500, //Running speed in step/s or (1/16)th step/s for microstep modes |
nucleosam | 0:36aa6787d4f9 | 70 | 50, //Running current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 71 | 20, //Holding current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 72 | STEP_MODE_1_16, //Step mode via enum motorStepMode_t |
nucleosam | 0:36aa6787d4f9 | 73 | FAST_DECAY, //Decay mode via enum motorDecayMode_t |
nucleosam | 0:36aa6787d4f9 | 74 | 0, //Dwelling time in ms |
nucleosam | 0:36aa6787d4f9 | 75 | FALSE, //Automatic HIZ STOP |
nucleosam | 0:36aa6787d4f9 | 76 | 100000 //VREFA and VREFB PWM frequency (Hz) |
nucleosam | 0:36aa6787d4f9 | 77 | }; |
nucleosam | 0:36aa6787d4f9 | 78 | |
nucleosam | 0:36aa6787d4f9 | 79 | /* Motor Control Component. */ |
nucleosam | 0:36aa6787d4f9 | 80 | L6208 *motor; |
nucleosam | 0:36aa6787d4f9 | 81 | |
nucleosam | 0:36aa6787d4f9 | 82 | /* Functions -----------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 83 | |
nucleosam | 0:36aa6787d4f9 | 84 | /** |
nucleosam | 0:36aa6787d4f9 | 85 | * @brief This is an example of user handler for the flag interrupt. |
nucleosam | 0:36aa6787d4f9 | 86 | * @param None |
nucleosam | 0:36aa6787d4f9 | 87 | * @retval None |
nucleosam | 0:36aa6787d4f9 | 88 | * @note If needed, implement it, and then attach and enable it: |
davide.aliprandi@st.com | 5:bc710d77d801 | 89 | * + motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 5:bc710d77d801 | 90 | * + motor->enable_flag_irq(); |
nucleosam | 0:36aa6787d4f9 | 91 | * To disable it: |
nucleosam | 0:36aa6787d4f9 | 92 | * + motor->DisbleFlagIRQ(); |
nucleosam | 0:36aa6787d4f9 | 93 | */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 94 | void my_flag_irq_handler(void) |
nucleosam | 0:36aa6787d4f9 | 95 | { |
nucleosam | 0:36aa6787d4f9 | 96 | printf(" WARNING: \"FLAG\" interrupt triggered:\r\n"); |
Davidroid | 6:6fb625ef867a | 97 | motor->disable(); |
nucleosam | 0:36aa6787d4f9 | 98 | printf(" Motor disabled.\r\n\n"); |
nucleosam | 0:36aa6787d4f9 | 99 | } |
nucleosam | 0:36aa6787d4f9 | 100 | |
nucleosam | 0:36aa6787d4f9 | 101 | /** |
nucleosam | 0:36aa6787d4f9 | 102 | * @brief This is an example of error handler. |
nucleosam | 0:36aa6787d4f9 | 103 | * @param[in] error Number of the error |
nucleosam | 0:36aa6787d4f9 | 104 | * @retval None |
nucleosam | 0:36aa6787d4f9 | 105 | * @note If needed, implement it, and then attach it: |
davide.aliprandi@st.com | 5:bc710d77d801 | 106 | * + motor->attach_error_handler(&my_error_handler); |
nucleosam | 0:36aa6787d4f9 | 107 | */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 108 | void my_error_handler(uint16_t error) |
nucleosam | 0:36aa6787d4f9 | 109 | { |
nucleosam | 0:36aa6787d4f9 | 110 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 111 | printf("Error %d detected\r\n\n", error); |
nucleosam | 0:36aa6787d4f9 | 112 | |
nucleosam | 0:36aa6787d4f9 | 113 | /* Infinite loop */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 114 | while (true) { |
nucleosam | 0:36aa6787d4f9 | 115 | } |
nucleosam | 0:36aa6787d4f9 | 116 | } |
nucleosam | 0:36aa6787d4f9 | 117 | |
nucleosam | 0:36aa6787d4f9 | 118 | /* Main ----------------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 119 | |
nucleosam | 0:36aa6787d4f9 | 120 | int main() |
nucleosam | 0:36aa6787d4f9 | 121 | { |
nucleosam | 0:36aa6787d4f9 | 122 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 123 | printf("STARTING MAIN PROGRAM\r\n"); |
nucleosam | 0:36aa6787d4f9 | 124 | printf(" Reminder:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 125 | printf(" The position unit is in agreement to the step mode.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 126 | printf(" The speed, acceleration or deceleration unit depend on the step mode:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 127 | printf(" - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 128 | printf(" - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 129 | |
nucleosam | 0:36aa6787d4f9 | 130 | //----- Initialization |
nucleosam | 0:36aa6787d4f9 | 131 | /* Initializing Motor Control Component. */ |
nucleosam | 3:a6e155687c6a | 132 | motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN); |
davide.aliprandi@st.com | 5:bc710d77d801 | 133 | if (motor->init(&init) != COMPONENT_OK) { |
davide.aliprandi@st.com | 5:bc710d77d801 | 134 | exit(EXIT_FAILURE); |
davide.aliprandi@st.com | 5:bc710d77d801 | 135 | } |
nucleosam | 0:36aa6787d4f9 | 136 | |
nucleosam | 0:36aa6787d4f9 | 137 | /* Attaching and enabling an interrupt handler. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 138 | motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 5:bc710d77d801 | 139 | motor->enable_flag_irq(); |
nucleosam | 0:36aa6787d4f9 | 140 | |
nucleosam | 0:36aa6787d4f9 | 141 | /* Attaching an error handler */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 142 | motor->attach_error_handler(&my_error_handler); |
nucleosam | 0:36aa6787d4f9 | 143 | |
nucleosam | 0:36aa6787d4f9 | 144 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 145 | printf("Motor Control Application Example for 1 Motor\r\n"); |
nucleosam | 0:36aa6787d4f9 | 146 | |
davide.aliprandi@st.com | 5:bc710d77d801 | 147 | //----- run the motor BACKWARD |
nucleosam | 0:36aa6787d4f9 | 148 | printf("--> Running the motor backward.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 149 | motor->run(StepperMotor::BWD); |
nucleosam | 0:36aa6787d4f9 | 150 | |
davide.aliprandi@st.com | 5:bc710d77d801 | 151 | while (motor->get_status()!=STEADY) { |
nucleosam | 0:36aa6787d4f9 | 152 | /* Print reached speed to the console in step/s or microsteps/s */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 153 | printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); |
nucleosam | 2:c5f455885df7 | 154 | wait_ms(50); |
nucleosam | 0:36aa6787d4f9 | 155 | } |
davide.aliprandi@st.com | 5:bc710d77d801 | 156 | printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); |
nucleosam | 0:36aa6787d4f9 | 157 | |
nucleosam | 0:36aa6787d4f9 | 158 | /* Wait for 1 second */ |
nucleosam | 0:36aa6787d4f9 | 159 | wait_ms(1000); |
nucleosam | 0:36aa6787d4f9 | 160 | |
nucleosam | 0:36aa6787d4f9 | 161 | //----- Decrease speed while running to one quarter of the previous speed |
davide.aliprandi@st.com | 5:bc710d77d801 | 162 | motor->set_max_speed(motor->get_speed()>>2); |
nucleosam | 0:36aa6787d4f9 | 163 | |
nucleosam | 0:36aa6787d4f9 | 164 | /* Wait until the motor starts decelerating */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 165 | while (motor->get_status()==STEADY); |
nucleosam | 0:36aa6787d4f9 | 166 | /* Wait and print speed while the motor is not steady running */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 167 | while (motor->get_status()!=STEADY) { |
nucleosam | 0:36aa6787d4f9 | 168 | /* Print reached speed to the console in step/s or microsteps/s */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 169 | printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); |
nucleosam | 2:c5f455885df7 | 170 | wait_ms(50); |
nucleosam | 0:36aa6787d4f9 | 171 | } |
davide.aliprandi@st.com | 5:bc710d77d801 | 172 | printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); |
nucleosam | 0:36aa6787d4f9 | 173 | |
nucleosam | 0:36aa6787d4f9 | 174 | /* Wait for 5 seconds */ |
nucleosam | 0:36aa6787d4f9 | 175 | wait_ms(5000); |
nucleosam | 0:36aa6787d4f9 | 176 | |
nucleosam | 0:36aa6787d4f9 | 177 | //----- Soft stop required while running |
nucleosam | 0:36aa6787d4f9 | 178 | printf("--> Soft stop requested.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 179 | motor->soft_stop(); |
nucleosam | 0:36aa6787d4f9 | 180 | |
nucleosam | 0:36aa6787d4f9 | 181 | /* Wait for the motor of device ends moving */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 182 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 183 | |
nucleosam | 0:36aa6787d4f9 | 184 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 185 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 186 | |
nucleosam | 0:36aa6787d4f9 | 187 | //----- Change step mode to full step mode |
davide.aliprandi@st.com | 5:bc710d77d801 | 188 | motor->set_step_mode(StepperMotor::STEP_MODE_FULL); |
davide.aliprandi@st.com | 5:bc710d77d801 | 189 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode()); |
nucleosam | 0:36aa6787d4f9 | 190 | |
nucleosam | 0:36aa6787d4f9 | 191 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 192 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:36aa6787d4f9 | 193 | |
nucleosam | 0:36aa6787d4f9 | 194 | /* Set speed, acceleration and deceleration to scale with normal mode */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 195 | motor->set_max_speed(init.maxSpeedSps>>4); |
davide.aliprandi@st.com | 5:bc710d77d801 | 196 | motor->set_acceleration(motor->get_acceleration()>>4); |
davide.aliprandi@st.com | 5:bc710d77d801 | 197 | motor->set_deceleration(motor->get_deceleration()>>4); |
nucleosam | 0:36aa6787d4f9 | 198 | /* Print parameters to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 199 | printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 200 | printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 201 | printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 202 | printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); |
nucleosam | 0:36aa6787d4f9 | 203 | |
davide.aliprandi@st.com | 5:bc710d77d801 | 204 | //----- move of 200 steps in the FW direction |
nucleosam | 0:36aa6787d4f9 | 205 | printf("--> Moving forward 200 steps.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 206 | motor->move(StepperMotor::FWD, 200); |
nucleosam | 0:36aa6787d4f9 | 207 | |
nucleosam | 0:36aa6787d4f9 | 208 | /* Waiting while the motor is active. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 209 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 210 | |
nucleosam | 0:36aa6787d4f9 | 211 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 212 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:36aa6787d4f9 | 213 | |
nucleosam | 0:36aa6787d4f9 | 214 | /* Disable the power bridges */ |
Davidroid | 6:6fb625ef867a | 215 | motor->disable(); |
nucleosam | 0:36aa6787d4f9 | 216 | |
nucleosam | 0:36aa6787d4f9 | 217 | /* Check that the power bridges are actually disabled */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 218 | if (motor->check_status_hw()!=0) { |
davide.aliprandi@st.com | 5:bc710d77d801 | 219 | printf(" Motor driver disabled.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 220 | } else { |
davide.aliprandi@st.com | 5:bc710d77d801 | 221 | printf(" Failed to disable the motor driver.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 222 | } |
nucleosam | 0:36aa6787d4f9 | 223 | |
nucleosam | 0:36aa6787d4f9 | 224 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 225 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 226 | |
nucleosam | 0:36aa6787d4f9 | 227 | //----- Change step mode to 1/4 microstepping mode |
davide.aliprandi@st.com | 5:bc710d77d801 | 228 | motor->set_step_mode(StepperMotor::STEP_MODE_1_4); |
davide.aliprandi@st.com | 5:bc710d77d801 | 229 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode()); |
nucleosam | 0:36aa6787d4f9 | 230 | |
nucleosam | 0:36aa6787d4f9 | 231 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 232 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:36aa6787d4f9 | 233 | |
nucleosam | 0:36aa6787d4f9 | 234 | /* Set speed, acceleration and deceleration to scale with microstep mode */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 235 | motor->set_max_speed(motor->get_max_speed()<<4); |
davide.aliprandi@st.com | 5:bc710d77d801 | 236 | motor->set_acceleration(motor->get_acceleration()<<4); |
davide.aliprandi@st.com | 5:bc710d77d801 | 237 | motor->set_deceleration(motor->get_deceleration()<<4); |
nucleosam | 0:36aa6787d4f9 | 238 | /* Print parameters to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 239 | printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 240 | printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 241 | printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 242 | printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); |
nucleosam | 0:36aa6787d4f9 | 243 | |
nucleosam | 0:36aa6787d4f9 | 244 | /* Request to go position 800 (quarter steps) */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 245 | motor->go_to(800); |
nucleosam | 0:36aa6787d4f9 | 246 | |
nucleosam | 0:36aa6787d4f9 | 247 | /* Wait for the motor ends moving */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 248 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 249 | |
nucleosam | 0:36aa6787d4f9 | 250 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 251 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:36aa6787d4f9 | 252 | |
nucleosam | 0:36aa6787d4f9 | 253 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 254 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 255 | |
nucleosam | 0:36aa6787d4f9 | 256 | //----- Restore step mode to its initialization value |
davide.aliprandi@st.com | 5:bc710d77d801 | 257 | motor->set_step_mode((StepperMotor::step_mode_t)init.stepMode); |
davide.aliprandi@st.com | 5:bc710d77d801 | 258 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode()); |
nucleosam | 0:36aa6787d4f9 | 259 | |
nucleosam | 0:36aa6787d4f9 | 260 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 261 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:36aa6787d4f9 | 262 | |
nucleosam | 0:36aa6787d4f9 | 263 | //----- Change decay mode |
davide.aliprandi@st.com | 5:bc710d77d801 | 264 | motor->set_decay_mode(SLOW_DECAY); |
davide.aliprandi@st.com | 5:bc710d77d801 | 265 | printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode()); |
nucleosam | 0:36aa6787d4f9 | 266 | |
nucleosam | 0:36aa6787d4f9 | 267 | //----- Go to position -6400 |
nucleosam | 0:36aa6787d4f9 | 268 | printf("--> Go to position -6400 steps.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 269 | motor->go_to(-6400); |
nucleosam | 0:36aa6787d4f9 | 270 | |
nucleosam | 0:36aa6787d4f9 | 271 | /* Wait for the motor ends moving */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 272 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 273 | |
nucleosam | 0:36aa6787d4f9 | 274 | /* Get current position of device and print to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 275 | printf(" Position: %d.\r\n", motor->get_position()); |
nucleosam | 0:36aa6787d4f9 | 276 | |
nucleosam | 0:36aa6787d4f9 | 277 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 278 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 279 | |
nucleosam | 0:36aa6787d4f9 | 280 | //----- Restore decay mode to its initialization value |
davide.aliprandi@st.com | 5:bc710d77d801 | 281 | motor->set_decay_mode(init.decayMode); |
davide.aliprandi@st.com | 5:bc710d77d801 | 282 | printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode()); |
nucleosam | 0:36aa6787d4f9 | 283 | |
nucleosam | 0:36aa6787d4f9 | 284 | //----- Go Home |
nucleosam | 0:36aa6787d4f9 | 285 | printf("--> Go to home position.\r\n"); |
davide.aliprandi@st.com | 5:bc710d77d801 | 286 | motor->go_home(); |
nucleosam | 0:36aa6787d4f9 | 287 | |
nucleosam | 0:36aa6787d4f9 | 288 | /* Wait for the motor ends moving */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 289 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 290 | |
nucleosam | 0:36aa6787d4f9 | 291 | /* Wait for 1 second */ |
nucleosam | 0:36aa6787d4f9 | 292 | wait_ms(1000); |
nucleosam | 0:36aa6787d4f9 | 293 | |
nucleosam | 0:36aa6787d4f9 | 294 | /* Infinite Loop. */ |
nucleosam | 0:36aa6787d4f9 | 295 | printf("--> Infinite Loop...\r\n"); |
Davidroid | 8:baedd289a8cc | 296 | //while (true) { |
Davidroid | 8:baedd289a8cc | 297 | int i; |
Davidroid | 8:baedd289a8cc | 298 | for (i = 0; i < 2; i++) { |
nucleosam | 0:36aa6787d4f9 | 299 | /* Request device to go position -3200 */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 300 | motor->go_to(-3200); |
nucleosam | 0:36aa6787d4f9 | 301 | |
nucleosam | 0:36aa6787d4f9 | 302 | /* Waiting while the motor is active. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 303 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 304 | |
nucleosam | 0:36aa6787d4f9 | 305 | /* Request device to go position 3200 */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 306 | motor->go_to(3200); |
nucleosam | 0:36aa6787d4f9 | 307 | |
nucleosam | 0:36aa6787d4f9 | 308 | /* Waiting while the motor is active. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 309 | motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 310 | } |
nucleosam | 0:36aa6787d4f9 | 311 | } |
davide.aliprandi@st.com | 5:bc710d77d801 | 312 | |
nucleosam | 0:36aa6787d4f9 | 313 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |