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:
- 39:1270497828ca
- Parent:
- 38:ca71d404823c
- Child:
- 40:fc0e002acc8c
- Child:
- 41:74e394ae8b5f
--- a/main.cpp Mon Jul 03 11:54:28 2017 +0000 +++ b/main.cpp Thu Feb 06 22:44:58 2020 +0000 @@ -1,3 +1,37 @@ +/** + ****************************************************************************** + * @file main.cpp + * @date February 06, 2020 + * @brief mbed test application Esmacat Shield working together with + * STMicroelectronics X-NUCLEO-IHM01A1 + * Motor Control Expansion Board: control of 1 motor. + * The original code is STMicroelectronics which is modified to integrate EASE. + *The Copyright of STMicroelectronics is retained below. + ****************************************************************************** + + Copyright (c) 2020 https://www.esmacat.com/ + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + EsmacatShield.h - Library for using EtherCAT Arduino Shield by Esmacat(EASE). + Created by Esmacat, 01/22/2020 + +******************************************************************************* +* @file EsmacatShield.h +******************************************************************************* +*/ + + /** ****************************************************************************** * @file main.cpp @@ -49,24 +83,19 @@ /* Component specific header files. */ #include "L6474.h" +/* Esmacat code*/ +#include <EsmacatShield.h> //Include Esmacat Library + +int16_t ease_read[8]; //EASE 8 registers + +/*D9 Chip select for EASE. Remember to change the jumper on the board*/ +DigitalOut selectPin(D9); +SPI spi(D11, D12, D13); // mosi, miso, sclk +/* Esmacat code*/ /* Definitions ---------------------------------------------------------------*/ - -/* Number of steps. */ -#define STEPS_1 (400 * 8) /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */ - /* Delay in milliseconds. */ #define DELAY_1 1000 -#define DELAY_2 2000 -#define DELAY_3 6000 -#define DELAY_4 8000 - -/* Speed in pps (Pulses Per Second). - In Full Step mode: 1 pps = 1 step/s). - In 1/N Step Mode: N pps = 1 step/s). */ -#define SPEED_1 2400 -#define SPEED_2 1200 - /* Variables -----------------------------------------------------------------*/ @@ -136,13 +165,21 @@ int main() { + /*----- Initialization of EASE -----*/ + + /*----- Initialization. -----*/ /* Initializing SPI bus. */ - DevSPI dev_spi(D11, D12, D13); + DevSPI dev_spi(D11, D12, D13);// SPI device for the motor + dev_spi.setup(8, 3, 3E6); // Set-up SPI device for the motor + + EsmacatShield slave(spi, selectPin); //Define Chip Selector Pin + slave.setup_spi(); //Setup SPI for EASE + /* Initializing Motor Control Component. */ - motor = new L6474(D2, D8, D7, D9, D10, dev_spi); + motor = new L6474(D2, D8, D7, D3, D10, dev_spi);//PWM pin is set to D3 as D9 is used by EASE if (motor->init(&init) != COMPONENT_OK) { exit(EXIT_FAILURE); } @@ -150,218 +187,46 @@ /* Attaching and enabling interrupt handlers. */ motor->attach_flag_irq(&flag_irq_handler); motor->enable_flag_irq(); - - /* Printing to the console. */ - printf("Motor Control Application Example for 1 Motor\r\n\n"); - - - /*----- Moving. -----*/ - - /* Printing to the console. */ - printf("--> Moving forward %d steps.\r\n", STEPS_1); - - /* Moving N steps in the forward direction. */ - motor->move(StepperMotor::FWD, STEPS_1); - - /* Waiting while the motor is active. */ - motor->wait_while_active(); - - /* Getting current position. */ - int position = motor->get_position(); - /* Printing to the console. */ - printf(" Position: %d.\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - - - /*----- Changing the motor setting. -----*/ - - /* Printing to the console. */ - printf("--> Setting Torque Regulation Current to 500[mA].\r\n"); - - /* Increasing the torque regulation current to 500[mA]. */ - motor->set_parameter(L6474_TVAL, 500); - - /* Printing to the console. */ - printf("--> Doubling the microsteps.\r\n"); - /* Doubling the microsteps. */ - if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_1_16)) { - printf(" Step Mode not allowed.\r\n"); - } - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Setting Home.\r\n"); - - /* Setting the current position to be the home position. */ - motor->set_home(); - - /* Getting current position. */ - position = motor->get_position(); - - /* Printing to the console. */ - printf(" Position: %d.\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_2); - - - /*----- Moving. -----*/ - - /* Printing to the console. */ - printf("--> Moving backward %d steps.\r\n", STEPS_1); - - /* Moving N steps in the backward direction. */ - motor->move(StepperMotor::BWD, STEPS_1); - - /* Waiting while the motor is active. */ - motor->wait_while_active(); - - /* Getting current position. */ - position = motor->get_position(); - - /* Printing to the console. */ - printf(" Position: %d.\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - /*----- Going to a specified position. -----*/ - - /* Printing to the console. */ - printf("--> Going to position %d.\r\n", STEPS_1); - - /* Requesting to go to a specified position. */ - motor->go_to(STEPS_1); - - /* Waiting while the motor is active. */ - motor->wait_while_active(); - - /* Getting current position. */ - position = motor->get_position(); - - /* Printing to the console. */ - printf(" Position: %d.\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_2); - - - /*----- Going Home. -----*/ - - /* Printing to the console. */ - printf("--> Going Home.\r\n"); - - /* Requesting to go to home. */ - motor->go_home(); + while(1) + { + // This part of the code sets up the Mode 1 for SPI to be used for + // communication with EASE. This is needed because SPI for EASE operates + // in Mode 1 which is different than what the motor shield SPI operates in. + + slave.setup_spi(); //Mode 1 set SPI for EASE + wait_us(200); + + // get the data from Esmacat Master + slave.get_ecat_registers(ease_read); //read all registers + + wait_us(1000); + /* Printing to the console. */ + printf("--> Moving to position : %d\r\n", ease_read[0]); + + + // This part of the code sets up the Mode 1 for SPI to be used for + // communication with EASE. This is needed because SPI for EASE operates + // in Mode 1 which is different than what the motor shield SPI operates in. + dev_spi.setup(8, 3, 3E6); // Mode 3 set for SPI device for the motor + wait_us(200); + + // command the motor to go to a set postion + motor->go_to(ease_read[0]); + - /* Waiting while the motor is active. */ - motor->wait_while_active(); - - /* Getting current position. */ - position = motor->get_position(); - - /* Printing to the console. */ - printf(" Position: %d.\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_2); - - - /*----- Running. -----*/ - - /* Printing to the console. */ - printf("--> Running backward for %d seconds.\r\n", DELAY_3 / 1000); - - /* Requesting to run backward. */ - motor->run(StepperMotor::BWD); - - /* Waiting. */ - wait_ms(DELAY_3); - - /* Getting current speed. */ - int speed = motor->get_speed(); - - /* Printing to the console. */ - printf(" Speed: %d.\r\n", speed); - - /*----- Increasing the speed while running. -----*/ - - /* Printing to the console. */ - printf("--> Increasing the speed while running again for %d seconds.\r\n", DELAY_3 / 1000); - - /* Increasing the speed. */ - motor->set_max_speed(SPEED_1); + /* Getting current position. */ + int position = motor->get_position(); + + /* Printing to the console. */ + printf(" Position: %d.\r\n", position); + + /* Waiting. */ + wait_ms(DELAY_1); + + } - /* Waiting. */ - wait_ms(DELAY_3); - - /* Getting current speed. */ - speed = motor->get_speed(); - - /* Printing to the console. */ - printf(" Speed: %d.\r\n", speed); - - - /*----- Decreasing the speed while running. -----*/ - - /* Printing to the console. */ - printf("--> Decreasing the speed while running again for %d seconds.\r\n", DELAY_4 / 1000); - - /* Decreasing the speed. */ - motor->set_max_speed(SPEED_2); - - /* Waiting. */ - wait_ms(DELAY_4); - - /* Getting current speed. */ - speed = motor->get_speed(); - - /* Printing to the console. */ - printf(" Speed: %d.\r\n", speed); - - - /*----- Hard Stop. -----*/ - - /* Printing to the console. */ - printf("--> Hard Stop.\r\n"); - - /* Requesting to immediatly stop. */ - motor->hard_stop(); - - /* Waiting while the motor is active. */ - motor->wait_while_active(); - - /* Waiting. */ - wait_ms(DELAY_2); - - - /*----- Infinite Loop. -----*/ - - /* Printing to the console. */ - printf("--> Infinite Loop...\r\n"); - - /* Setting the current position to be the home position. */ - motor->set_home(); - - /* Infinite Loop. */ - while (true) { - /* Requesting to go to a specified position. */ - motor->go_to(STEPS_1 >> 1); - - /* Waiting while the motor is active. */ - motor->wait_while_active(); - - /* Requesting to go to a specified position. */ - motor->go_to(- (STEPS_1 >> 1)); - - /* Waiting while the motor is active. */ - motor->wait_while_active(); - } }