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
Diff: main.cpp
- Revision:
- 5:bc710d77d801
- Parent:
- 3:a6e155687c6a
- Child:
- 6:6fb625ef867a
diff -r 5e5f1535e3b5 -r bc710d77d801 main.cpp --- a/main.cpp Tue May 03 08:54:47 2016 +0000 +++ b/main.cpp Fri Mar 24 11:00:38 2017 +0100 @@ -42,7 +42,7 @@ #include "mbed.h" /* Component specific header files. */ -#include "l6208_class.h" +#include "L6208.h" /* Definitions ---------------------------------------------------------------*/ @@ -60,7 +60,7 @@ /* Variables -----------------------------------------------------------------*/ /* Initialization parameters of the motor connected to the expansion board. */ -l6208_Init_t initDeviceParameters = +l6208_init_t init = { 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes 40, //Acceleration current torque in % (from 0 to 100) @@ -86,12 +86,12 @@ * @param None * @retval None * @note If needed, implement it, and then attach and enable it: - * + motor->AttachFlagIRQ(&myFlagIRQHandler); - * + motor->EnableFlagIRQ(); + * + motor->attach_flag_irq(&my_flag_irq_handler); + * + motor->enable_flag_irq(); * To disable it: * + motor->DisbleFlagIRQ(); */ -void myFlagIRQHandler(void) +void my_flag_irq_handler(void) { printf(" WARNING: \"FLAG\" interrupt triggered:\r\n"); motor->Disable(); @@ -103,16 +103,15 @@ * @param[in] error Number of the error * @retval None * @note If needed, implement it, and then attach it: - * + motor->AttachErrorHandler(&myErrorHandler); + * + motor->attach_error_handler(&my_error_handler); */ -void myErrorHandler(uint16_t error) +void my_error_handler(uint16_t error) { /* Printing to the console. */ printf("Error %d detected\r\n\n", error); /* Infinite loop */ - while(1) - { + while (true) { } } @@ -131,179 +130,182 @@ //----- Initialization /* Initializing Motor Control Component. */ motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN); - if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE); + if (motor->init(&init) != COMPONENT_OK) { + exit(EXIT_FAILURE); + } /* Attaching and enabling an interrupt handler. */ - motor->AttachFlagIRQ(&myFlagIRQHandler); - motor->EnableFlagIRQ(); + motor->attach_flag_irq(&my_flag_irq_handler); + motor->enable_flag_irq(); /* Attaching an error handler */ - motor->AttachErrorHandler(&myErrorHandler); + motor->attach_error_handler(&my_error_handler); /* Printing to the console. */ printf("Motor Control Application Example for 1 Motor\r\n"); -//----- Run the motor BACKWARD +//----- run the motor BACKWARD printf("--> Running the motor backward.\r\n"); - motor->Run(StepperMotor::BWD); + motor->run(StepperMotor::BWD); - while (motor->GetStatus()!=STEADY) - { + while (motor->get_status()!=STEADY) { /* Print reached speed to the console in step/s or microsteps/s */ - printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); + printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); wait_ms(50); } - printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); + printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); /* Wait for 1 second */ wait_ms(1000); //----- Decrease speed while running to one quarter of the previous speed - motor->SetMaxSpeed(motor->GetSpeed()>>2); + motor->set_max_speed(motor->get_speed()>>2); /* Wait until the motor starts decelerating */ - while (motor->GetStatus()==STEADY); + while (motor->get_status()==STEADY); /* Wait and print speed while the motor is not steady running */ - while (motor->GetStatus()!=STEADY) - { + while (motor->get_status()!=STEADY) { /* Print reached speed to the console in step/s or microsteps/s */ - printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); + printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); wait_ms(50); } - printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); + printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); /* Wait for 5 seconds */ wait_ms(5000); //----- Soft stop required while running printf("--> Soft stop requested.\r\n"); - motor->SoftStop(); + motor->soft_stop(); /* Wait for the motor of device ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Wait for 2 seconds */ wait_ms(2000); //----- Change step mode to full step mode - motor->SetStepMode(StepperMotor::STEP_MODE_FULL); - printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->GetStepMode()); + motor->set_step_mode(StepperMotor::STEP_MODE_FULL); + 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()); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); /* Set speed, acceleration and deceleration to scale with normal mode */ - motor->SetMaxSpeed(initDeviceParameters.maxSpeedSps>>4); - motor->SetAcceleration(motor->GetAcceleration()>>4); - motor->SetDeceleration(motor->GetDeceleration()>>4); + motor->set_max_speed(init.maxSpeedSps>>4); + motor->set_acceleration(motor->get_acceleration()>>4); + motor->set_deceleration(motor->get_deceleration()>>4); /* Print parameters to the console */ - printf(" Motor Max Speed: %d step/s.\r\n", motor->GetMaxSpeed()); - printf(" Motor Min Speed: %d step/s.\r\n", motor->GetMinSpeed()); - printf(" Motor Acceleration: %d step/s.\r\n", motor->GetAcceleration()); - printf(" Motor Deceleration: %d step/s.\r\n", motor->GetDeceleration()); + printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); + printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); + printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); + printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); -//----- Move of 200 steps in the FW direction +//----- move of 200 steps in the FW direction printf("--> Moving forward 200 steps.\r\n"); - motor->Move(StepperMotor::FWD, 200); + motor->move(StepperMotor::FWD, 200); /* Waiting while the motor is active. */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); /* Disable the power bridges */ motor->Disable(); /* Check that the power bridges are actually disabled */ - if (motor->CheckStatusHw()!=0) printf(" Motor driver disabled.\r\n"); - else printf(" Failed to disable the motor driver.\r\n"); + if (motor->check_status_hw()!=0) { + printf(" Motor driver disabled.\r\n"); + } else { + printf(" Failed to disable the motor driver.\r\n"); + } /* Wait for 2 seconds */ wait_ms(2000); //----- Change step mode to 1/4 microstepping mode - motor->SetStepMode(StepperMotor::STEP_MODE_1_4); - printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->GetStepMode()); + motor->set_step_mode(StepperMotor::STEP_MODE_1_4); + 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()); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); /* Set speed, acceleration and deceleration to scale with microstep mode */ - motor->SetMaxSpeed(motor->GetMaxSpeed()<<4); - motor->SetAcceleration(motor->GetAcceleration()<<4); - motor->SetDeceleration(motor->GetDeceleration()<<4); + motor->set_max_speed(motor->get_max_speed()<<4); + motor->set_acceleration(motor->get_acceleration()<<4); + motor->set_deceleration(motor->get_deceleration()<<4); /* Print parameters to the console */ - printf(" Motor Max Speed: %d step/s.\r\n", motor->GetMaxSpeed()); - printf(" Motor Min Speed: %d step/s.\r\n", motor->GetMinSpeed()); - printf(" Motor Acceleration: %d step/s.\r\n", motor->GetAcceleration()); - printf(" Motor Deceleration: %d step/s.\r\n", motor->GetDeceleration()); + printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); + printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); + printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); + printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); /* Request to go position 800 (quarter steps) */ - motor->GoTo(800); + motor->go_to(800); /* Wait for the motor ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); /* Wait for 2 seconds */ wait_ms(2000); //----- Restore step mode to its initialization value - motor->SetStepMode((StepperMotor::step_mode_t)initDeviceParameters.stepMode); - printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->GetStepMode()); + motor->set_step_mode((StepperMotor::step_mode_t)init.stepMode); + 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()); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); //----- Change decay mode - motor->SetDecayMode(SLOW_DECAY); - printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->GetDecayMode()); + motor->set_decay_mode(SLOW_DECAY); + printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode()); //----- Go to position -6400 printf("--> Go to position -6400 steps.\r\n"); - motor->GoTo(-6400); + motor->go_to(-6400); /* Wait for the motor ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); /* Wait for 2 seconds */ wait_ms(2000); //----- Restore decay mode to its initialization value - motor->SetDecayMode(initDeviceParameters.decayMode); - printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->GetDecayMode()); + motor->set_decay_mode(init.decayMode); + printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode()); //----- Go Home printf("--> Go to home position.\r\n"); - motor->GoHome(); + motor->go_home(); /* Wait for the motor ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Wait for 1 second */ wait_ms(1000); /* Infinite Loop. */ printf("--> Infinite Loop...\r\n"); - while (1) - { + while (true) { /* Request device to go position -3200 */ - motor->GoTo(-3200); + motor->go_to(-3200); /* Waiting while the motor is active. */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Request device to go position 3200 */ - motor->GoTo(3200); + motor->go_to(3200); /* Waiting while the motor is active. */ - motor->WaitWhileActive(); + motor->wait_while_active(); } } + /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/