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:35:22 2018 +0000
Revision:
10:9d908db05638
Parent:
9:16f134d983bb
Child:
13:08617f604d55
2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucleosam 0:36aa6787d4f9 1
nucleosam 0:36aa6787d4f9 2 /* Includes ------------------------------------------------------------------*/
nucleosam 0:36aa6787d4f9 3
nucleosam 0:36aa6787d4f9 4 /* mbed specific header files. */
nucleosam 0:36aa6787d4f9 5 #include "mbed.h"
nucleosam 0:36aa6787d4f9 6
nucleosam 0:36aa6787d4f9 7 /* Component specific header files. */
davide.aliprandi@st.com 5:bc710d77d801 8 #include "L6208.h"
nucleosam 0:36aa6787d4f9 9
nucleosam 0:36aa6787d4f9 10
nucleosam 0:36aa6787d4f9 11 /* Definitions ---------------------------------------------------------------*/
nucleosam 0:36aa6787d4f9 12 #ifdef TARGET_NUCLEO_F334R8
nucleosam 0:36aa6787d4f9 13 #define VREFA_PWM_PIN D11
nucleosam 3:a6e155687c6a 14 #define VREFB_PWM_PIN D9
nucleosam 3:a6e155687c6a 15 #elif TARGET_NUCLEO_F302R8
nucleosam 3:a6e155687c6a 16 #define VREFA_PWM_PIN D11
nucleosam 3:a6e155687c6a 17 #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */
nucleosam 0:36aa6787d4f9 18 #else
nucleosam 0:36aa6787d4f9 19 #define VREFA_PWM_PIN D3
nucleosam 3:a6e155687c6a 20 #define VREFB_PWM_PIN D9
nucleosam 0:36aa6787d4f9 21 #endif
nucleosam 0:36aa6787d4f9 22
nucleosam 0:36aa6787d4f9 23 /* Variables -----------------------------------------------------------------*/
nucleosam 0:36aa6787d4f9 24
nucleosam 0:36aa6787d4f9 25 /* Initialization parameters of the motor connected to the expansion board. */
davide.aliprandi@st.com 5:bc710d77d801 26 l6208_init_t init =
nucleosam 0:36aa6787d4f9 27 {
nucleosam 0:36aa6787d4f9 28 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
gidiana 9:16f134d983bb 29 50, //Acceleration current torque in % (from 0 to 100)
gidiana 9:16f134d983bb 30 1000, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
gidiana 9:16f134d983bb 31 50, //Deceleration current torque in % (from 0 to 100)
nucleosam 0:36aa6787d4f9 32 1500, //Running speed in step/s or (1/16)th step/s for microstep modes
nucleosam 0:36aa6787d4f9 33 50, //Running current torque in % (from 0 to 100)
gidiana 9:16f134d983bb 34 30, //Holding current torque in % (from 0 to 100)
nucleosam 0:36aa6787d4f9 35 STEP_MODE_1_16, //Step mode via enum motorStepMode_t
nucleosam 0:36aa6787d4f9 36 FAST_DECAY, //Decay mode via enum motorDecayMode_t
nucleosam 0:36aa6787d4f9 37 0, //Dwelling time in ms
nucleosam 0:36aa6787d4f9 38 FALSE, //Automatic HIZ STOP
nucleosam 0:36aa6787d4f9 39 100000 //VREFA and VREFB PWM frequency (Hz)
nucleosam 0:36aa6787d4f9 40 };
nucleosam 0:36aa6787d4f9 41
nucleosam 0:36aa6787d4f9 42 /* Motor Control Component. */
nucleosam 0:36aa6787d4f9 43 L6208 *motor;
nucleosam 0:36aa6787d4f9 44
nucleosam 0:36aa6787d4f9 45 /* Functions -----------------------------------------------------------------*/
nucleosam 0:36aa6787d4f9 46
nucleosam 0:36aa6787d4f9 47 /**
nucleosam 0:36aa6787d4f9 48 * @brief This is an example of user handler for the flag interrupt.
nucleosam 0:36aa6787d4f9 49 * @param None
nucleosam 0:36aa6787d4f9 50 * @retval None
nucleosam 0:36aa6787d4f9 51 * @note If needed, implement it, and then attach and enable it:
davide.aliprandi@st.com 5:bc710d77d801 52 * + motor->attach_flag_irq(&my_flag_irq_handler);
davide.aliprandi@st.com 5:bc710d77d801 53 * + motor->enable_flag_irq();
nucleosam 0:36aa6787d4f9 54 * To disable it:
nucleosam 0:36aa6787d4f9 55 * + motor->DisbleFlagIRQ();
nucleosam 0:36aa6787d4f9 56 */
davide.aliprandi@st.com 5:bc710d77d801 57 void my_flag_irq_handler(void)
nucleosam 0:36aa6787d4f9 58 {
nucleosam 0:36aa6787d4f9 59 printf(" WARNING: \"FLAG\" interrupt triggered:\r\n");
Davidroid 6:6fb625ef867a 60 motor->disable();
nucleosam 0:36aa6787d4f9 61 printf(" Motor disabled.\r\n\n");
nucleosam 0:36aa6787d4f9 62 }
nucleosam 0:36aa6787d4f9 63
nucleosam 0:36aa6787d4f9 64 /**
nucleosam 0:36aa6787d4f9 65 * @brief This is an example of error handler.
nucleosam 0:36aa6787d4f9 66 * @param[in] error Number of the error
nucleosam 0:36aa6787d4f9 67 * @retval None
nucleosam 0:36aa6787d4f9 68 * @note If needed, implement it, and then attach it:
davide.aliprandi@st.com 5:bc710d77d801 69 * + motor->attach_error_handler(&my_error_handler);
nucleosam 0:36aa6787d4f9 70 */
davide.aliprandi@st.com 5:bc710d77d801 71 void my_error_handler(uint16_t error)
nucleosam 0:36aa6787d4f9 72 {
nucleosam 0:36aa6787d4f9 73 /* Printing to the console. */
nucleosam 0:36aa6787d4f9 74 printf("Error %d detected\r\n\n", error);
nucleosam 0:36aa6787d4f9 75
nucleosam 0:36aa6787d4f9 76 /* Infinite loop */
davide.aliprandi@st.com 5:bc710d77d801 77 while (true) {
nucleosam 0:36aa6787d4f9 78 }
nucleosam 0:36aa6787d4f9 79 }
nucleosam 0:36aa6787d4f9 80
nucleosam 0:36aa6787d4f9 81 /* Main ----------------------------------------------------------------------*/
nucleosam 0:36aa6787d4f9 82
nucleosam 0:36aa6787d4f9 83 int main()
nucleosam 0:36aa6787d4f9 84 {
gidiana 9:16f134d983bb 85
nucleosam 0:36aa6787d4f9 86 //----- Initialization
nucleosam 0:36aa6787d4f9 87 /* Initializing Motor Control Component. */
nucleosam 3:a6e155687c6a 88 motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
davide.aliprandi@st.com 5:bc710d77d801 89 if (motor->init(&init) != COMPONENT_OK) {
davide.aliprandi@st.com 5:bc710d77d801 90 exit(EXIT_FAILURE);
davide.aliprandi@st.com 5:bc710d77d801 91 }
nucleosam 0:36aa6787d4f9 92
nucleosam 0:36aa6787d4f9 93 /* Attaching and enabling an interrupt handler. */
davide.aliprandi@st.com 5:bc710d77d801 94 motor->attach_flag_irq(&my_flag_irq_handler);
davide.aliprandi@st.com 5:bc710d77d801 95 motor->enable_flag_irq();
nucleosam 0:36aa6787d4f9 96
nucleosam 0:36aa6787d4f9 97 /* Attaching an error handler */
davide.aliprandi@st.com 5:bc710d77d801 98 motor->attach_error_handler(&my_error_handler);
gidiana 9:16f134d983bb 99 //----- run the motor BACKWARD
nucleosam 0:36aa6787d4f9 100 printf("--> Running the motor backward.\r\n");
davide.aliprandi@st.com 5:bc710d77d801 101 motor->run(StepperMotor::BWD);
nucleosam 0:36aa6787d4f9 102
davide.aliprandi@st.com 5:bc710d77d801 103 while (motor->get_status()!=STEADY) {
nucleosam 0:36aa6787d4f9 104 /* Print reached speed to the console in step/s or microsteps/s */
davide.aliprandi@st.com 5:bc710d77d801 105 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 2:c5f455885df7 106 wait_ms(50);
nucleosam 0:36aa6787d4f9 107 }
davide.aliprandi@st.com 5:bc710d77d801 108 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 0:36aa6787d4f9 109
nucleosam 0:36aa6787d4f9 110 /* Wait for 1 second */
nucleosam 0:36aa6787d4f9 111 wait_ms(1000);
nucleosam 0:36aa6787d4f9 112
nucleosam 0:36aa6787d4f9 113 //----- Decrease speed while running to one quarter of the previous speed
davide.aliprandi@st.com 5:bc710d77d801 114 motor->set_max_speed(motor->get_speed()>>2);
nucleosam 0:36aa6787d4f9 115
nucleosam 0:36aa6787d4f9 116 /* Wait until the motor starts decelerating */
davide.aliprandi@st.com 5:bc710d77d801 117 while (motor->get_status()==STEADY);
nucleosam 0:36aa6787d4f9 118 /* Wait and print speed while the motor is not steady running */
davide.aliprandi@st.com 5:bc710d77d801 119 while (motor->get_status()!=STEADY) {
nucleosam 0:36aa6787d4f9 120 /* Print reached speed to the console in step/s or microsteps/s */
davide.aliprandi@st.com 5:bc710d77d801 121 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 2:c5f455885df7 122 wait_ms(50);
nucleosam 0:36aa6787d4f9 123 }
davide.aliprandi@st.com 5:bc710d77d801 124 printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed());
nucleosam 0:36aa6787d4f9 125
nucleosam 0:36aa6787d4f9 126 /* Wait for 5 seconds */
nucleosam 0:36aa6787d4f9 127 wait_ms(5000);
nucleosam 0:36aa6787d4f9 128
nucleosam 0:36aa6787d4f9 129 //----- Change step mode to 1/4 microstepping mode
davide.aliprandi@st.com 5:bc710d77d801 130 motor->set_step_mode(StepperMotor::STEP_MODE_1_4);
nucleosam 0:36aa6787d4f9 131
nucleosam 0:36aa6787d4f9 132 /* Set speed, acceleration and deceleration to scale with microstep mode */
davide.aliprandi@st.com 5:bc710d77d801 133 motor->set_max_speed(motor->get_max_speed()<<4);
davide.aliprandi@st.com 5:bc710d77d801 134 motor->set_acceleration(motor->get_acceleration()<<4);
davide.aliprandi@st.com 5:bc710d77d801 135 motor->set_deceleration(motor->get_deceleration()<<4);
nucleosam 0:36aa6787d4f9 136
nucleosam 0:36aa6787d4f9 137 /* Request to go position 800 (quarter steps) */
gidiana 9:16f134d983bb 138 motor->go_to(9000);
nucleosam 0:36aa6787d4f9 139
nucleosam 0:36aa6787d4f9 140 /* Wait for the motor ends moving */
davide.aliprandi@st.com 5:bc710d77d801 141 motor->wait_while_active();
gidiana 9:16f134d983bb 142 motor->go_to(-9000);
nucleosam 0:36aa6787d4f9 143
nucleosam 0:36aa6787d4f9 144 /* Wait for the motor ends moving */
davide.aliprandi@st.com 5:bc710d77d801 145 motor->wait_while_active();
nucleosam 0:36aa6787d4f9 146 /* Wait for 2 seconds */
nucleosam 0:36aa6787d4f9 147 wait_ms(2000);
gidiana 9:16f134d983bb 148 int i;
gidiana 9:16f134d983bb 149 for (i = 0; i < 4; i++) {
nucleosam 0:36aa6787d4f9 150 /* Request device to go position -3200 */
gidiana 9:16f134d983bb 151 motor->go_to(3200);
nucleosam 0:36aa6787d4f9 152
nucleosam 0:36aa6787d4f9 153 /* Waiting while the motor is active. */
davide.aliprandi@st.com 5:bc710d77d801 154 motor->wait_while_active();
nucleosam 0:36aa6787d4f9 155
nucleosam 0:36aa6787d4f9 156 /* Request device to go position 3200 */
gidiana 9:16f134d983bb 157 motor->go_to(-3200);
nucleosam 0:36aa6787d4f9 158
nucleosam 0:36aa6787d4f9 159 /* Waiting while the motor is active. */
davide.aliprandi@st.com 5:bc710d77d801 160 motor->wait_while_active();
nucleosam 0:36aa6787d4f9 161 }
gidiana 9:16f134d983bb 162 wait_ms(5000);
gidiana 9:16f134d983bb 163 motor->disable();
nucleosam 0:36aa6787d4f9 164 }
davide.aliprandi@st.com 5:bc710d77d801 165
gidiana 9:16f134d983bb 166