1

Dependencies:   X-NUCLEO-IHM05A1

Example code to use L6208 stepper motor driver with nucleo evaluation shield

Explanation: Run a test to determine the maximum speed of the motor at given current and acceleration Run the motor in position mode with microstepping and slow decay

Committer:
gidiana
Date:
Sat Dec 01 12:31:22 2018 +0000
Revision:
9:16f134d983bb
Parent:
8:baedd289a8cc
Child:
10:9d908db05638
1

Who changed what in which revision?

UserRevisionLine numberNew 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>&copy; 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
gidiana 9:16f134d983bb 66 50, //Acceleration current torque in % (from 0 to 100)
gidiana 9:16f134d983bb 67 1000, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
gidiana 9:16f134d983bb 68 50, //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)
gidiana 9:16f134d983bb 71 30, //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 {
gidiana 9:16f134d983bb 122
nucleosam 0:36aa6787d4f9 123 //----- Initialization
nucleosam 0:36aa6787d4f9 124 /* Initializing Motor Control Component. */
nucleosam 3:a6e155687c6a 125 motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
davide.aliprandi@st.com 5:bc710d77d801 126 if (motor->init(&init) != COMPONENT_OK) {
davide.aliprandi@st.com 5:bc710d77d801 127 exit(EXIT_FAILURE);
davide.aliprandi@st.com 5:bc710d77d801 128 }
nucleosam 0:36aa6787d4f9 129
nucleosam 0:36aa6787d4f9 130 /* Attaching and enabling an interrupt handler. */
davide.aliprandi@st.com 5:bc710d77d801 131 motor->attach_flag_irq(&my_flag_irq_handler);
davide.aliprandi@st.com 5:bc710d77d801 132 motor->enable_flag_irq();
nucleosam 0:36aa6787d4f9 133
nucleosam 0:36aa6787d4f9 134 /* Attaching an error handler */
davide.aliprandi@st.com 5:bc710d77d801 135 motor->attach_error_handler(&my_error_handler);
gidiana 9:16f134d983bb 136 //----- run the motor BACKWARD
nucleosam 0:36aa6787d4f9 137 printf("--> Running the motor backward.\r\n");
davide.aliprandi@st.com 5:bc710d77d801 138 motor->run(StepperMotor::BWD);
nucleosam 0:36aa6787d4f9 139
davide.aliprandi@st.com 5:bc710d77d801 140 while (motor->get_status()!=STEADY) {
nucleosam 0:36aa6787d4f9 141 /* Print reached speed to the console in step/s or microsteps/s */
davide.aliprandi@st.com 5:bc710d77d801 142 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 2:c5f455885df7 143 wait_ms(50);
nucleosam 0:36aa6787d4f9 144 }
davide.aliprandi@st.com 5:bc710d77d801 145 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 0:36aa6787d4f9 146
nucleosam 0:36aa6787d4f9 147 /* Wait for 1 second */
nucleosam 0:36aa6787d4f9 148 wait_ms(1000);
nucleosam 0:36aa6787d4f9 149
nucleosam 0:36aa6787d4f9 150 //----- Decrease speed while running to one quarter of the previous speed
davide.aliprandi@st.com 5:bc710d77d801 151 motor->set_max_speed(motor->get_speed()>>2);
nucleosam 0:36aa6787d4f9 152
nucleosam 0:36aa6787d4f9 153 /* Wait until the motor starts decelerating */
davide.aliprandi@st.com 5:bc710d77d801 154 while (motor->get_status()==STEADY);
nucleosam 0:36aa6787d4f9 155 /* Wait and print speed while the motor is not steady running */
davide.aliprandi@st.com 5:bc710d77d801 156 while (motor->get_status()!=STEADY) {
nucleosam 0:36aa6787d4f9 157 /* Print reached speed to the console in step/s or microsteps/s */
davide.aliprandi@st.com 5:bc710d77d801 158 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 2:c5f455885df7 159 wait_ms(50);
nucleosam 0:36aa6787d4f9 160 }
davide.aliprandi@st.com 5:bc710d77d801 161 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 0:36aa6787d4f9 162
nucleosam 0:36aa6787d4f9 163 /* Wait for 5 seconds */
nucleosam 0:36aa6787d4f9 164 wait_ms(5000);
nucleosam 0:36aa6787d4f9 165
nucleosam 0:36aa6787d4f9 166 //----- Change step mode to 1/4 microstepping mode
davide.aliprandi@st.com 5:bc710d77d801 167 motor->set_step_mode(StepperMotor::STEP_MODE_1_4);
nucleosam 0:36aa6787d4f9 168
nucleosam 0:36aa6787d4f9 169 /* Set speed, acceleration and deceleration to scale with microstep mode */
davide.aliprandi@st.com 5:bc710d77d801 170 motor->set_max_speed(motor->get_max_speed()<<4);
davide.aliprandi@st.com 5:bc710d77d801 171 motor->set_acceleration(motor->get_acceleration()<<4);
davide.aliprandi@st.com 5:bc710d77d801 172 motor->set_deceleration(motor->get_deceleration()<<4);
nucleosam 0:36aa6787d4f9 173
nucleosam 0:36aa6787d4f9 174 /* Request to go position 800 (quarter steps) */
gidiana 9:16f134d983bb 175 motor->go_to(9000);
nucleosam 0:36aa6787d4f9 176
nucleosam 0:36aa6787d4f9 177 /* Wait for the motor ends moving */
davide.aliprandi@st.com 5:bc710d77d801 178 motor->wait_while_active();
gidiana 9:16f134d983bb 179 motor->go_to(-9000);
nucleosam 0:36aa6787d4f9 180
nucleosam 0:36aa6787d4f9 181 /* Wait for the motor ends moving */
davide.aliprandi@st.com 5:bc710d77d801 182 motor->wait_while_active();
nucleosam 0:36aa6787d4f9 183 /* Wait for 2 seconds */
nucleosam 0:36aa6787d4f9 184 wait_ms(2000);
gidiana 9:16f134d983bb 185 int i;
gidiana 9:16f134d983bb 186 for (i = 0; i < 4; i++) {
nucleosam 0:36aa6787d4f9 187 /* Request device to go position -3200 */
gidiana 9:16f134d983bb 188 motor->go_to(3200);
nucleosam 0:36aa6787d4f9 189
nucleosam 0:36aa6787d4f9 190 /* Waiting while the motor is active. */
davide.aliprandi@st.com 5:bc710d77d801 191 motor->wait_while_active();
nucleosam 0:36aa6787d4f9 192
nucleosam 0:36aa6787d4f9 193 /* Request device to go position 3200 */
gidiana 9:16f134d983bb 194 motor->go_to(-3200);
nucleosam 0:36aa6787d4f9 195
nucleosam 0:36aa6787d4f9 196 /* Waiting while the motor is active. */
davide.aliprandi@st.com 5:bc710d77d801 197 motor->wait_while_active();
nucleosam 0:36aa6787d4f9 198 }
gidiana 9:16f134d983bb 199 wait_ms(5000);
gidiana 9:16f134d983bb 200 motor->disable();
nucleosam 0:36aa6787d4f9 201 }
davide.aliprandi@st.com 5:bc710d77d801 202
gidiana 9:16f134d983bb 203