This is an example application demonstrating building an EtherCAT system using Esmacat
Dependencies: EsmacatShield X_NUCLEO_IHM01A1
Basic Information of Esmacat
Information about Esmacat and EASE is provided in the link below. https://os.mbed.com/users/pratima_hb/code/EASE_Example/wiki/Homepage
Information about the hardware needs and setup is provided in the link below. https://os.mbed.com/users/pratima_hb/code/EASE_Example/wiki/Hardware-Setup
Information about the structure of the system and it's software is provided in the link below. https://os.mbed.com/users/pratima_hb/code/EASE_Example/wiki/Software
About this example
This is an example application to demonstrate the ease with which a system, which communicates over EtherCAT, can be build. It measures the RPM of a motor using a proximity sensor.
Following lists the hardware required
- 2 x Mbed boards with Arduino UNO form factor (NUCLEO-F103RB)
- 2 x EASE boards
- 1 x proximity sensor shield (X_NUCLEO_6180XA1)
- 1 x Motor shield (X-NUCLEO-IHM01A1)
- 1 x stepper motor
- 1 x Ethernet POE injector with a 24VDC power supply
- 2 x Ethernet cables
- Keyboard, mouse, and monitor
- PC with Linux/Windows OS installed
- DC power supply for motor
Click here to know more about this Example
Diff: main.cpp
- Revision:
- 1:fbf28f3367aa
- Parent:
- 0:e6a49a092e2a
- Child:
- 2:e12e4df7a486
--- 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(); } } }