Simple program featuring a few API functions usage of the X_NUCLEO_IHM06A1 library.
Dependencies: X_NUCLEO_IHM06A1 mbed
Fork of HelloWorld_IHM06A1 by
This application provides a simple example of usage of the X-NUCLEO-IHM06A1 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;
- Disabling the power bridges;
- Setting/Getting the torque;
- Going to home position
It also shows how to monitor the step clock handler duration in order to evaluate the maximum achievable motor speed for a given MCU.
For the hardware configuration of the expansion board, please refer to the X_NUCLEO_IHM06A1 library web page.
Diff: main.cpp
- Revision:
- 4:0e50964ff610
- Parent:
- 0:9ebe3081c641
- Child:
- 5:8c0802d9a345
diff -r f9e259625e54 -r 0e50964ff610 main.cpp
--- a/main.cpp Fri Jul 29 08:54:36 2016 +0000
+++ b/main.cpp Fri Mar 24 11:00:53 2017 +0100
@@ -42,7 +42,7 @@
#include "mbed.h"
/* Component specific header files. */
-#include "stspin220_class.h"
+#include "STSpin220.h"
/* Definitions ---------------------------------------------------------------*/
#ifdef TARGET_NUCLEO_F302R8
@@ -59,7 +59,7 @@
/* Variables -----------------------------------------------------------------*/
/* Initialization parameters of the motor connected to the expansion board. */
-Stspin220_Init_t initDeviceParameters =
+Stspin220_init_t init =
{
10000, //Acceleration rate in pulse/s2 (must be greater than 0)
10000, //Deceleration rate in pulse/s2 (must be greater than 0)
@@ -77,7 +77,7 @@
};
/* Motor Control Component. */
-STSPIN220 *motor;
+STSpin220 *motor;
/* Functions -----------------------------------------------------------------*/
@@ -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) {
}
}
@@ -133,180 +132,183 @@
//----- Initialization
/* Initializing Motor Control Component. */
#ifdef STEP_CLOCK_HANDLER_DURATION_MONITORING
- motor = new STSPIN220(D2, D8, D7, D5, D10, D3, PWM_REF_PIN, MONITORING_PIN);
+ motor = new STSpin220(D2, D8, D7, D5, D10, D3, PWM_REF_PIN, MONITORING_PIN);
#else
- motor = new STSPIN220(D2, D8, D7, D5, D10, D3, PWM_REF_PIN);
+ motor = new STSpin220(D2, D8, D7, D5, D10, D3, PWM_REF_PIN);
#endif
- 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");
//----- Get the step mode after initialization
- StepperMotor::step_mode_t myStepMode = motor->GetStepMode();
+ StepperMotor::step_mode_t myStepMode = motor->get_step_mode();
-//----- 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 step/s.\r\n", motor->GetSpeed());
+ printf(" Reached Speed: %d step/s.\r\n", motor->get_speed());
wait_ms(50);
}
- printf(" Reached Speed: %d step/s.\r\n", motor->GetSpeed());
+ printf(" Reached Speed: %d step/s.\r\n", motor->get_speed());
/* Wait for 1 second */
wait_ms(1000);
//----- Decrease speed while running to one quarter of the previous speed
printf(" Motor init step mode: %d\r\n", myStepMode);
- int currentStepMode = motor->GetStepMode();
+ int currentStepMode = motor->get_step_mode();
printf(" Motor current step mode: %d\r\n", currentStepMode);
- int newSpeed = (motor->GetSpeed()<<(myStepMode-currentStepMode))>>2;
+ int newSpeed = (motor->get_speed()<<(myStepMode-currentStepMode))>>2;
printf(" Set motor max speed to: %d step/s.\r\n", newSpeed);
- if (!motor->SetMaxSpeed(newSpeed))
- {
+ if (!motor->set_max_speed(newSpeed)) {
printf(" Failed: target speed below min speed.\r\n");
- if (motor->SetMaxSpeed(motor->GetMinSpeed()))
- printf(" Motor max speed set to min speed: %d step/s.\r\n", motor->GetMinSpeed());
- else
+ if (motor->set_max_speed(motor->get_min_speed())) {
+ printf(" Motor max speed set to min speed: %d step/s.\r\n", motor->get_min_speed());
+ } else {
printf(" Failed: check all speed and boost setting.\r\n");
+ }
}
/* 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 step/s.\r\n", motor->GetSpeed());
+ printf(" Reached Speed: %d step/s.\r\n", motor->get_speed());
wait_ms(50);
}
- printf(" Reached Speed: %d step/s.\r\n", motor->GetSpeed());
+ printf(" Reached Speed: %d step/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\r\n", motor->GetStepMode());
+ motor->set_step_mode(StepperMotor::STEP_MODE_FULL);
+ printf(" Motor step mode: %d\r\n", motor->get_step_mode());
printf(" 0:FS 1:1/2 2:1/4\r\n 3:1/8 4:1/16 5:1/32\r\n");
printf(" 6:1/64 7:1/128 8:1/256\r\n");
/* 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 full step mode */
- motor->SetMinSpeed(initDeviceParameters.minSpeed>>myStepMode);
- motor->SetMaxSpeed(initDeviceParameters.maxSpeed>>myStepMode);
- motor->SetAcceleration(motor->GetAcceleration()>>myStepMode);
- motor->SetDeceleration(motor->GetDeceleration()>>myStepMode);
+ motor->set_min_speed(init.minSpeed>>myStepMode);
+ motor->set_max_speed(init.maxSpeed>>myStepMode);
+ motor->set_acceleration(motor->get_acceleration()>>myStepMode);
+ motor->set_deceleration(motor->get_deceleration()>>myStepMode);
/* Print parameters to the console */
- printf(" Motor Min Speed: %d step/s.\r\n", motor->GetMinSpeed());
- printf(" Motor Max Speed: %d step/s.\r\n", motor->GetMaxSpeed());
- printf(" Motor Acceleration: %d step/s.\r\n", motor->GetAcceleration());
- printf(" Motor Deceleration: %d step/s.\r\n", motor->GetDeceleration());
+ printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());
+ printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_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);
//----- Restore step mode to its initialization value
- motor->SetStepMode((StepperMotor::step_mode_t)initDeviceParameters.stepMode);
- printf(" Motor step mode: %d\r\n", motor->GetStepMode());
+ motor->set_step_mode((StepperMotor::step_mode_t)init.stepMode);
+ printf(" Motor step mode: %d\r\n", motor->get_step_mode());
printf(" 0:FS 1:1/2 2:1/4\r\n 3:1/8 4:1/16 5:1/32\r\n");
printf(" 6:1/64 7:1/128 8:1/256\r\n");
/* Set speed, acceleration and deceleration to scale with microstep mode */
- motor->SetMaxSpeed(motor->GetMaxSpeed()<<myStepMode);
- motor->SetMinSpeed(motor->GetMinSpeed()<<myStepMode);
- motor->SetAcceleration(motor->GetAcceleration()<<myStepMode);
- motor->SetDeceleration(motor->GetDeceleration()<<myStepMode);
+ motor->set_max_speed(motor->get_max_speed()<<myStepMode);
+ motor->set_min_speed(motor->get_min_speed()<<myStepMode);
+ motor->set_acceleration(motor->get_acceleration()<<myStepMode);
+ motor->set_deceleration(motor->get_deceleration()<<myStepMode);
/* 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());
/* 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 torque
- motor->SetTorque(ACC_TORQUE,30);
- motor->SetTorque(DEC_TORQUE,20);
- printf(" Motor acceleration and deceleration torque changed to: %d and %d.\r\n", motor->GetTorque(ACC_TORQUE), motor->GetTorque(DEC_TORQUE));
+ motor->set_torque(ACC_TORQUE,30);
+ motor->set_torque(DEC_TORQUE,20);
+ printf(" Motor acceleration and deceleration torque changed to: %d and %d.\r\n", motor->get_torque(ACC_TORQUE), motor->get_torque(DEC_TORQUE));
//----- 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);
//----- 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();
wait_ms(500);
}
}

X-NUCLEO-IHM06A1 Low Voltage Stepper Motor Driver