Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_IHM01A1
Diff: main.cpp
- Revision:
- 1:fbf28f3367aa
- Parent:
- 0:e6a49a092e2a
- Child:
- 2:e12e4df7a486
diff -r e6a49a092e2a -r fbf28f3367aa main.cpp
--- a/main.cpp Wed Oct 14 15:21:49 2015 +0000
+++ b/main.cpp Fri Oct 16 13:51:31 2015 +0000
@@ -60,80 +60,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 ----------------------------------------------------------------------*/
@@ -143,12 +70,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. */
@@ -163,13 +87,13 @@
printf("--> Moving forward %d steps.\r\n", ROUND_ANGLE_STEPS);
/* Moving N steps in the forward direction. */
- l6474->Move(FORWARD, ROUND_ANGLE_STEPS);
+ motor->Move(FORWARD, ROUND_ANGLE_STEPS);
/* 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);
@@ -184,19 +108,19 @@
printf("--> Moving backward %d steps.\r\n", ROUND_ANGLE_STEPS);
/* Moving N steps in the backward direction. */
- l6474->Move(BACKWARD, ROUND_ANGLE_STEPS);
+ motor->Move(BACKWARD, ROUND_ANGLE_STEPS);
/* 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);
/* Setting the current position to be the home position. */
- l6474->SetHome();
+ motor->SetHome();
/* Waiting 2 seconds. */
wait_ms(2000);
@@ -208,13 +132,13 @@
printf("--> Going to position %d.\r\n", ROUND_ANGLE_STEPS >> 1);
/* Requesting to go to a specified position. */
- l6474->GoTo(ROUND_ANGLE_STEPS >> 1);
+ motor->GoTo(ROUND_ANGLE_STEPS >> 1);
/* 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);
@@ -229,13 +153,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);
@@ -250,13 +174,13 @@
printf("--> Moving backward.\r\n");
/* Requesting to run backward. */
- l6474->Run(BACKWARD);
+ motor->Run(BACKWARD);
/* Waiting until delay has expired. */
- WaitForDelay(6000);
+ wait_ms(6000);
/* Getting current speed. */
- int speed = l6474->GetCurrentSpeed();
+ int speed = motor->GetCurrentSpeed();
/* Printing to the console. */
printf(" Speed: %d.\r\n", speed);
@@ -268,13 +192,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(6000);
+ wait_ms(6000);
/* Getting current speed. */
- speed = l6474->GetCurrentSpeed();
+ speed = motor->GetCurrentSpeed();
/* Printing to the console. */
printf(" Speed: %d.\r\n", speed);
@@ -286,13 +210,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(8000);
+ wait_ms(8000);
/* Getting current speed. */
- speed = l6474->GetCurrentSpeed();
+ speed = motor->GetCurrentSpeed();
/* Printing to the console. */
printf(" Speed: %d.\r\n", speed);
@@ -304,10 +228,10 @@
printf("--> Moving forward.\r\n");
/* Requesting to run in forward direction. */
- l6474->Run(FORWARD);
+ motor->Run(FORWARD);
/* Waiting until delay has expired. */
- WaitForDelay(4000);
+ wait_ms(4000);
/*----- Requiring hard-stop while running. -----*/
@@ -316,10 +240,10 @@
printf("--> Requiring hard-stop while running.\r\n");
/* Requesting to immediatly stop. */
- l6474->HardStop();
+ motor->HardStop();
/* Waiting while the motor is active. */
- WaitWhileActive();
+ motor->WaitWhileActive();
/* Waiting 2 seconds. */
wait_ms(2000);
@@ -331,22 +255,22 @@
printf("--> Infinite Loop...\r\n");
/* Setting the current position to be the home position. */
- l6474->SetHome();
+ motor->SetHome();
/* Infinite Loop. */
while(1)
{
/* Requesting to go to a specified position. */
- l6474->GoTo(- ROUND_ANGLE_STEPS >> 2);
+ motor->GoTo(- ROUND_ANGLE_STEPS >> 2);
/* Waiting while the motor is active. */
- WaitWhileActive();
+ motor->WaitWhileActive();
/* Requesting to go to a specified position. */
- l6474->GoTo(ROUND_ANGLE_STEPS >> 2);
+ motor->GoTo(ROUND_ANGLE_STEPS >> 2);
/* Waiting while the motor is active. */
- WaitWhileActive();
+ motor->WaitWhileActive();
}
}
}