Test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.
Dependencies: X_NUCLEO_IHM01A1
Fork of MotorControl_IHM01A1 by
Motor Control with the X-NUCLEO-IHM01A1 Expansion Board
This application provides a more complex example of usage of the X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.
It shows how to use one stepper motor connected to the board, with an example of ISR and error handler, as well as an almost complete coverage of the available APIs, e.g.:
- moving the rotor to a specific position;
- moving the rotor for a certain amount of time;
- setting the speed value;
- setting the direction of rotation;
- changing the stepper motor mode;
- soft/hard stopping the rotor;
- powering off the power bridge.
Diff: main.cpp
- Revision:
- 1:5171df1bf684
- Parent:
- 0:fedf3cf2324a
- Child:
- 2:d956260240b0
diff -r fedf3cf2324a -r 5171df1bf684 main.cpp --- a/main.cpp Wed Oct 14 15:29:42 2015 +0000 +++ b/main.cpp Fri Oct 16 13:43:01 2015 +0000 @@ -5,7 +5,8 @@ * @version V1.0.0 * @date October 14th, 2015 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1 - * Motor Control Expansion Board: control of 1 motor. + * Motor Control Expansion Board: control of 1 motor showing the usage + * of all the related APIs. * This application makes use of a C++ component architecture obtained * from the C component architecture through the Stm32CubeTOO tool. ****************************************************************************** @@ -54,80 +55,7 @@ /* Variables -----------------------------------------------------------------*/ /* Motor Control Component. */ -L6474 *l6474; - -/* Flag to identify whenever a PWM pulse has finished. */ -volatile int pwm_pulse_finished_flag; - -/* Flag to identify whenever the desired delay has expired. */ -volatile int delay_expired_flag; - - -/* Functions -----------------------------------------------------------------*/ - -/* - * @brief PWM callback. - * @param None - * @retval None - */ -void PWMCallback(void) -{ - pwm_pulse_finished_flag = 1; -} - -/* - * @brief Delay callback. - * @param None - * @retval None - */ -void DelayCallback() -{ - delay_expired_flag = 1; -} - -/* - * @brief Waiting until PWM pulse has finished. - * @param None - * @retval None - */ -void WaitForPWMPulse(void) -{ - /* Waiting until PWM flag is set. */ - while (pwm_pulse_finished_flag == 0); - - /* Resetting PWM flag. */ - pwm_pulse_finished_flag = 0; - - /* Setting the device state machine. */ - if (l6474->GetDeviceState() != INACTIVE) - l6474->StepClockHandler(); -} - -/* - * @brief Waiting while the motor is active. - * @param None - * @retval None - */ -void WaitWhileActive(void) -{ - while (l6474->GetDeviceState() != INACTIVE) - WaitForPWMPulse(); -} - -/* - * @brief Waiting until delay has expired. - * @param delay delay in milliseconds. - * @retval None - */ -void WaitForDelay(int delay) -{ - Timeout timeout; - timeout.attach(&DelayCallback, delay / 1E3); - - delay_expired_flag = 0; - while (delay_expired_flag == 0) - WaitForPWMPulse(); -} +L6474 *motor; /* Main ----------------------------------------------------------------------*/ @@ -137,12 +65,9 @@ /* Initializing SPI bus. */ DevSPI dev_spi(D11, D12, D13); - /* Resetting Timer PWM flag. */ - pwm_pulse_finished_flag = 0; - /* Initializing Motor Control Component. */ - l6474 = new L6474(D8, D7, D9, D10, dev_spi); - if (l6474->Init(NULL) != COMPONENT_OK) + motor = new L6474(D8, D7, D9, D10, dev_spi); + if (motor->Init(NULL) != COMPONENT_OK) return false; /* Printing to the console. */ @@ -157,10 +82,10 @@ printf("--> Moving forward 16000 steps.\r\n"); /* Moving 16000 steps in the forward direction. */ - l6474->Move(FORWARD, 16000); + motor->Move(FORWARD, 16000); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Waiting 2 seconds. */ wait_ms(2000); @@ -172,13 +97,13 @@ printf("--> Moving backward 16000 steps.\r\n"); /* Moving 16000 steps in the backward direction. */ - l6474->Move(BACKWARD, 16000); - + motor->Move(BACKWARD, 16000); + /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Setting the current position to be the home position. */ - l6474->SetHome(); + motor->SetHome(); /* Waiting 2 seconds. */ wait_ms(2000); @@ -190,19 +115,19 @@ printf("--> Going to position -6400.\r\n"); /* Requesting to go to a specified position. */ - l6474->GoTo(-6400); + motor->GoTo(-6400); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Getting current position. */ - int position = l6474->GetPosition(); + int position = motor->GetPosition(); /* Printing to the console. */ printf(" Position: %d.\r\n", position); /* Setting the current position to be the mark position. */ - l6474->SetMark(); + motor->SetMark(); /* Waiting 2 seconds. */ wait_ms(2000); @@ -214,13 +139,13 @@ printf("--> Going Home.\r\n"); /* Requesting to go to home */ - l6474->GoHome(); + motor->GoHome(); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Getting current position. */ - position = l6474->GetPosition(); + position = motor->GetPosition(); /* Printing to the console. */ printf(" Position: %d.\r\n", position); @@ -235,13 +160,13 @@ printf("--> Going to position 6400.\r\n"); /* Requesting to go to a specified position. */ - l6474->GoTo(6400); + motor->GoTo(6400); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Getting current position. */ - position = l6474->GetPosition(); + position = motor->GetPosition(); /* Printing to the console. */ printf(" Position: %d.\r\n", position); @@ -256,13 +181,13 @@ printf("--> Going to mark which was set previously after going to -6400.\r\n"); /* Requesting to go to mark position. */ - l6474->GoMark(); + motor->GoMark(); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Getting current position. */ - position = l6474->GetPosition(); + position = motor->GetPosition(); /* Printing to the console. */ printf(" Position: %d.\r\n", position); @@ -277,13 +202,13 @@ printf("--> Moving backward.\r\n"); /* Requesting to run backward. */ - l6474->Run(BACKWARD); + motor->Run(BACKWARD); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Getting current speed. */ - int speed = l6474->GetCurrentSpeed(); + int speed = motor->GetCurrentSpeed(); /* Printing to the console. */ printf(" Speed: %d.\r\n", speed); @@ -295,13 +220,13 @@ printf("--> Increasing the speed while running.\r\n"); /* Increasing speed to 2400 step/s. */ - l6474->SetMaxSpeed(2400); + motor->SetMaxSpeed(2400); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Getting current speed. */ - speed = l6474->GetCurrentSpeed(); + speed = motor->GetCurrentSpeed(); /* Printing to the console. */ printf(" Speed: %d.\r\n", speed); @@ -313,13 +238,13 @@ printf("--> Decreasing the speed while running.\r\n"); /* Decreasing speed to 1200 step/s. */ - l6474->SetMaxSpeed(1200); + motor->SetMaxSpeed(1200); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Getting current speed. */ - speed = l6474->GetCurrentSpeed(); + speed = motor->GetCurrentSpeed(); /* Printing to the console. */ printf(" Speed: %d.\r\n", speed); @@ -331,19 +256,19 @@ printf("--> Increasing acceleration while running.\r\n"); /* Increasing acceleration to 480 step/s^2. */ - l6474->SetAcceleration(480); + motor->SetAcceleration(480); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Increasing speed to 2400 step/s. */ - l6474->SetMaxSpeed(2400); + motor->SetMaxSpeed(2400); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Getting current speed. */ - speed = l6474->GetCurrentSpeed(); + speed = motor->GetCurrentSpeed(); /* Printing to the console. */ printf(" Speed: %d.\r\n", speed); @@ -355,19 +280,19 @@ printf("--> Increasing deceleration while running.\r\n"); /* Increasing deceleration to 480 step/s^2. */ - l6474->SetDeceleration(480); + motor->SetDeceleration(480); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Decreasing speed to 1200 step/s. */ - l6474->SetMaxSpeed(1200); + motor->SetMaxSpeed(1200); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Getting current speed. */ - speed = l6474->GetCurrentSpeed(); + speed = motor->GetCurrentSpeed(); /* Printing to the console. */ printf(" Speed: %d.\r\n", speed); @@ -379,10 +304,10 @@ printf("--> Requiring soft-stop while running.\r\n"); /* Requesting soft stop. */ - l6474->SoftStop(); + motor->SoftStop(); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Waiting 2 seconds. */ wait_ms(2000); @@ -394,16 +319,16 @@ printf("--> Requiring hard-stop while running.\r\n"); /* Requesting to run in forward direction. */ - l6474->Run(FORWARD); + motor->Run(FORWARD); /* Waiting until delay has expired. */ - WaitForDelay(5000); + wait_ms(5000); /* Requesting to immediatly stop. */ - l6474->HardStop(); + motor->HardStop(); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Waiting 2 seconds. */ wait_ms(2000); @@ -415,16 +340,16 @@ printf("--> GOTO stopped by soft-stop.\r\n"); /* Requesting to go to a specified position. */ - l6474->GoTo(20000); + motor->GoTo(20000); /* Waiting while the motor is active. */ - WaitForDelay(5000); + wait_ms(5000); /* Requesting to perform a soft stop */ - l6474->SoftStop(); + motor->SoftStop(); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Waiting 2 seconds. */ wait_ms(2000); @@ -440,7 +365,7 @@ * The flag interrupt should be raised and the "MyFlagInterruptHandler" * function called. */ - l6474->CmdGetParam(0x1F); + motor->CmdGetParam(0x1F); /* Waiting 0.5 seconds. */ wait_ms(500); @@ -452,22 +377,22 @@ printf("--> Changing step mode to full step mode.\r\n"); /* Selecting full step mode. */ - l6474->SelectStepMode(STEP_MODE_FULL); + motor->SelectStepMode(STEP_MODE_FULL); /* Setting speed and acceleration to be consistent with full step mode. */ - l6474->SetMaxSpeed(100); - l6474->SetMinSpeed(50); - l6474->SetAcceleration(10); - l6474->SetDeceleration(10); + motor->SetMaxSpeed(100); + motor->SetMinSpeed(50); + motor->SetAcceleration(10); + motor->SetDeceleration(10); /* Requesting to go to a specified position. */ - l6474->GoTo(200); + motor->GoTo(200); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Getting current position */ - position = l6474->GetPosition(); + position = motor->GetPosition(); /* Printing to the console. */ printf(" Position: %d.\r\n", position); @@ -482,13 +407,13 @@ printf("--> Restoring 1/16 microstepping mode.\r\n"); /* Resetting to 1/16 microstepping mode */ - l6474->SelectStepMode(STEP_MODE_1_16); + motor->SelectStepMode(STEP_MODE_1_16); /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/ - l6474->SetMaxSpeed(1600); - l6474->SetMinSpeed(800); - l6474->SetAcceleration(160); - l6474->SetDeceleration(160); + motor->SetMaxSpeed(1600); + motor->SetMinSpeed(800); + motor->SetAcceleration(160); + motor->SetDeceleration(160); /*----- Infinite Loop. -----*/ @@ -500,16 +425,16 @@ while(1) { /* Requesting to go to a specified position. */ - l6474->GoTo(-6400); + motor->GoTo(-6400); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); /* Requesting to go to a specified position. */ - l6474->GoTo(6400); + motor->GoTo(6400); /* Waiting while the motor is active. */ - WaitWhileActive(); + motor->WaitWhileActive(); } } }