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

https://os.mbed.com/media/uploads/pratima_hb/system_pic1.jpg

Click here to know more about this Example

Committer:
pratima_hb
Date:
Thu Feb 13 17:33:59 2020 +0000
Revision:
42:faa7b7382f2f
Parent:
40:fc0e002acc8c
Motor code for System example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pratima_hb 39:1270497828ca 1 /**
pratima_hb 39:1270497828ca 2 ******************************************************************************
pratima_hb 39:1270497828ca 3 * @file main.cpp
pratima_hb 39:1270497828ca 4 * @date February 06, 2020
pratima_hb 39:1270497828ca 5 * @brief mbed test application Esmacat Shield working together with
pratima_hb 39:1270497828ca 6 * STMicroelectronics X-NUCLEO-IHM01A1
pratima_hb 39:1270497828ca 7 * Motor Control Expansion Board: control of 1 motor.
pratima_hb 39:1270497828ca 8 * The original code is STMicroelectronics which is modified to integrate EASE.
pratima_hb 39:1270497828ca 9 *The Copyright of STMicroelectronics is retained below.
pratima_hb 39:1270497828ca 10 ******************************************************************************
pratima_hb 39:1270497828ca 11
pratima_hb 39:1270497828ca 12 Copyright (c) 2020 https://www.esmacat.com/
pratima_hb 39:1270497828ca 13
pratima_hb 39:1270497828ca 14 Licensed under the Apache License, Version 2.0 (the "License");
pratima_hb 39:1270497828ca 15 you may not use this file except in compliance with the License.
pratima_hb 39:1270497828ca 16 You may obtain a copy of the License at
pratima_hb 39:1270497828ca 17
pratima_hb 39:1270497828ca 18 http://www.apache.org/licenses/LICENSE-2.0
pratima_hb 39:1270497828ca 19
pratima_hb 39:1270497828ca 20 Unless required by applicable law or agreed to in writing, software
pratima_hb 39:1270497828ca 21 distributed under the License is distributed on an "AS IS" BASIS,
pratima_hb 39:1270497828ca 22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
pratima_hb 39:1270497828ca 23 See the License for the specific language governing permissions and
pratima_hb 39:1270497828ca 24 limitations under the License.
pratima_hb 39:1270497828ca 25
pratima_hb 39:1270497828ca 26 EsmacatShield.h - Library for using EtherCAT Arduino Shield by Esmacat(EASE).
pratima_hb 39:1270497828ca 27 Created by Esmacat, 01/22/2020
pratima_hb 39:1270497828ca 28
pratima_hb 39:1270497828ca 29 *******************************************************************************
pratima_hb 39:1270497828ca 30 * @file EsmacatShield.h
pratima_hb 39:1270497828ca 31 *******************************************************************************
pratima_hb 39:1270497828ca 32 */
pratima_hb 39:1270497828ca 33
pratima_hb 39:1270497828ca 34
Davidroid 0:e6a49a092e2a 35 /**
Davidroid 0:e6a49a092e2a 36 ******************************************************************************
Davidroid 0:e6a49a092e2a 37 * @file main.cpp
Davidroid 21:0189493b15ec 38 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:e6a49a092e2a 39 * @version V1.0.0
Davidroid 0:e6a49a092e2a 40 * @date October 14th, 2015
Davidroid 21:0189493b15ec 41 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
Davidroid 0:e6a49a092e2a 42 * Motor Control Expansion Board: control of 1 motor.
Davidroid 0:e6a49a092e2a 43 ******************************************************************************
Davidroid 0:e6a49a092e2a 44 * @attention
Davidroid 0:e6a49a092e2a 45 *
Davidroid 0:e6a49a092e2a 46 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:e6a49a092e2a 47 *
Davidroid 0:e6a49a092e2a 48 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:e6a49a092e2a 49 * are permitted provided that the following conditions are met:
Davidroid 0:e6a49a092e2a 50 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:e6a49a092e2a 51 * this list of conditions and the following disclaimer.
Davidroid 0:e6a49a092e2a 52 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:e6a49a092e2a 53 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:e6a49a092e2a 54 * and/or other materials provided with the distribution.
Davidroid 0:e6a49a092e2a 55 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:e6a49a092e2a 56 * may be used to endorse or promote products derived from this software
Davidroid 0:e6a49a092e2a 57 * without specific prior written permission.
Davidroid 0:e6a49a092e2a 58 *
Davidroid 0:e6a49a092e2a 59 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:e6a49a092e2a 60 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:e6a49a092e2a 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:e6a49a092e2a 62 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:e6a49a092e2a 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:e6a49a092e2a 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:e6a49a092e2a 65 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:e6a49a092e2a 66 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:e6a49a092e2a 67 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:e6a49a092e2a 68 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:e6a49a092e2a 69 *
Davidroid 0:e6a49a092e2a 70 ******************************************************************************
Davidroid 0:e6a49a092e2a 71 */
Davidroid 0:e6a49a092e2a 72
Davidroid 0:e6a49a092e2a 73
Davidroid 0:e6a49a092e2a 74 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 75
Davidroid 0:e6a49a092e2a 76 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 77 #include "mbed.h"
Davidroid 38:ca71d404823c 78 #include "rtos.h"
Davidroid 0:e6a49a092e2a 79
Davidroid 0:e6a49a092e2a 80 /* Helper header files. */
Davidroid 0:e6a49a092e2a 81 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 82
Davidroid 0:e6a49a092e2a 83 /* Component specific header files. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 84 #include "L6474.h"
Davidroid 0:e6a49a092e2a 85
pratima_hb 40:fc0e002acc8c 86 ///* Esmacat code*/
pratima_hb 39:1270497828ca 87 #include <EsmacatShield.h> //Include Esmacat Library
pratima_hb 39:1270497828ca 88
pratima_hb 39:1270497828ca 89 int16_t ease_read[8]; //EASE 8 registers
pratima_hb 39:1270497828ca 90
pratima_hb 40:fc0e002acc8c 91 ///*D9 Chip select for EASE. Remember to change the jumper on the board*/
pratima_hb 39:1270497828ca 92 DigitalOut selectPin(D9);
pratima_hb 39:1270497828ca 93 SPI spi(D11, D12, D13); // mosi, miso, sclk
pratima_hb 40:fc0e002acc8c 94 ///* Esmacat code*/
Davidroid 0:e6a49a092e2a 95
Davidroid 0:e6a49a092e2a 96 /* Definitions ---------------------------------------------------------------*/
pratima_hb 40:fc0e002acc8c 97 #define STEPS_1 (400 * 8) /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */
Davidroid 26:b0203c2265e5 98 /* Delay in milliseconds. */
Davidroid 26:b0203c2265e5 99 #define DELAY_1 1000
Davidroid 0:e6a49a092e2a 100
Davidroid 0:e6a49a092e2a 101 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 102
Davidroid 19:1cd7f65c155c 103 /* Initialization parameters. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 104 L6474_init_t init = {
Davidroid 29:a80a213c3c94 105 160, /* Acceleration rate in pps^2. Range: (0..+inf). */
Davidroid 29:a80a213c3c94 106 160, /* Deceleration rate in pps^2. Range: (0..+inf). */
Davidroid 29:a80a213c3c94 107 1600, /* Maximum speed in pps. Range: (30..10000]. */
Davidroid 29:a80a213c3c94 108 800, /* Minimum speed in pps. Range: [30..10000). */
Davidroid 19:1cd7f65c155c 109 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 19:1cd7f65c155c 110 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 19:1cd7f65c155c 111 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 19:1cd7f65c155c 112 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
Davidroid 26:b0203c2265e5 113 L6474_STEP_SEL_1_8, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 114 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 115 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 116 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 117 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 118 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 119 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 19:1cd7f65c155c 120 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 19:1cd7f65c155c 121 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 19:1cd7f65c155c 122 L6474_ALARM_EN_OVERCURRENT |
Davidroid 19:1cd7f65c155c 123 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 19:1cd7f65c155c 124 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 19:1cd7f65c155c 125 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 19:1cd7f65c155c 126 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 19:1cd7f65c155c 127 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 19:1cd7f65c155c 128 };
Davidroid 19:1cd7f65c155c 129
Davidroid 0:e6a49a092e2a 130 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 131 L6474 *motor;
Davidroid 0:e6a49a092e2a 132
Davidroid 0:e6a49a092e2a 133
Davidroid 19:1cd7f65c155c 134 /* Functions -----------------------------------------------------------------*/
Davidroid 19:1cd7f65c155c 135
Davidroid 19:1cd7f65c155c 136 /**
Davidroid 25:3c863b420ac5 137 * @brief This is an example of user handler for the flag interrupt.
Davidroid 25:3c863b420ac5 138 * @param None
Davidroid 25:3c863b420ac5 139 * @retval None
Davidroid 25:3c863b420ac5 140 * @note If needed, implement it, and then attach and enable it:
davide.aliprandi@st.com 35:2b44ed4ec7a0 141 * + motor->attach_flag_irq(&flag_irq_handler);
davide.aliprandi@st.com 35:2b44ed4ec7a0 142 * + motor->enable_flag_irq();
Davidroid 25:3c863b420ac5 143 * To disable it:
davide.aliprandi@st.com 35:2b44ed4ec7a0 144 * + motor->disble_flag_irq();
Davidroid 25:3c863b420ac5 145 */
davide.aliprandi@st.com 35:2b44ed4ec7a0 146 void flag_irq_handler(void)
Davidroid 19:1cd7f65c155c 147 {
Davidroid 19:1cd7f65c155c 148 /* Set ISR flag. */
Davidroid 19:1cd7f65c155c 149 motor->isr_flag = TRUE;
Davidroid 19:1cd7f65c155c 150
Davidroid 19:1cd7f65c155c 151 /* Get the value of the status register. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 152 unsigned int status = motor->get_status();
Davidroid 34:543d0d1147d9 153
Davidroid 19:1cd7f65c155c 154 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 19:1cd7f65c155c 155 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
Davidroid 34:543d0d1147d9 156 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
Davidroid 19:1cd7f65c155c 157 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
Davidroid 34:543d0d1147d9 158 }
Davidroid 19:1cd7f65c155c 159
Davidroid 19:1cd7f65c155c 160 /* Reset ISR flag. */
Davidroid 19:1cd7f65c155c 161 motor->isr_flag = FALSE;
Davidroid 19:1cd7f65c155c 162 }
Davidroid 19:1cd7f65c155c 163
Davidroid 19:1cd7f65c155c 164
Davidroid 0:e6a49a092e2a 165 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 166
Davidroid 0:e6a49a092e2a 167 int main()
Davidroid 0:e6a49a092e2a 168 {
pratima_hb 39:1270497828ca 169
Davidroid 6:32166bfc04b0 170 /*----- Initialization. -----*/
Davidroid 6:32166bfc04b0 171
pratima_hb 40:fc0e002acc8c 172 /* Initializing SPI bus for Motor */
pratima_hb 39:1270497828ca 173 DevSPI dev_spi(D11, D12, D13);// SPI device for the motor
pratima_hb 39:1270497828ca 174 dev_spi.setup(8, 3, 3E6); // Set-up SPI device for the motor
pratima_hb 40:fc0e002acc8c 175
pratima_hb 40:fc0e002acc8c 176 /* EASE slave object creation and Initializing SPI bus for EASE */
pratima_hb 39:1270497828ca 177 EsmacatShield slave(spi, selectPin); //Define Chip Selector Pin
pratima_hb 39:1270497828ca 178 slave.setup_spi(); //Setup SPI for EASE
pratima_hb 39:1270497828ca 179
Davidroid 0:e6a49a092e2a 180
Davidroid 11:3e303a25770d 181 /* Initializing Motor Control Component. */
pratima_hb 39:1270497828ca 182 motor = new L6474(D2, D8, D7, D3, D10, dev_spi);//PWM pin is set to D3 as D9 is used by EASE
davide.aliprandi@st.com 35:2b44ed4ec7a0 183 if (motor->init(&init) != COMPONENT_OK) {
Davidroid 17:4830b25fec7f 184 exit(EXIT_FAILURE);
Davidroid 34:543d0d1147d9 185 }
Davidroid 0:e6a49a092e2a 186
Davidroid 19:1cd7f65c155c 187 /* Attaching and enabling interrupt handlers. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 188 motor->attach_flag_irq(&flag_irq_handler);
pratima_hb 40:fc0e002acc8c 189 motor->enable_flag_irq();
Davidroid 6:32166bfc04b0 190
pratima_hb 39:1270497828ca 191 while(1)
pratima_hb 39:1270497828ca 192 {
pratima_hb 39:1270497828ca 193 // This part of the code sets up the Mode 1 for SPI to be used for
pratima_hb 39:1270497828ca 194 // communication with EASE. This is needed because SPI for EASE operates
pratima_hb 39:1270497828ca 195 // in Mode 1 which is different than what the motor shield SPI operates in.
pratima_hb 39:1270497828ca 196
pratima_hb 39:1270497828ca 197 slave.setup_spi(); //Mode 1 set SPI for EASE
pratima_hb 39:1270497828ca 198 wait_us(200);
pratima_hb 39:1270497828ca 199
pratima_hb 39:1270497828ca 200 // get the data from Esmacat Master
pratima_hb 39:1270497828ca 201 slave.get_ecat_registers(ease_read); //read all registers
pratima_hb 40:fc0e002acc8c 202
pratima_hb 39:1270497828ca 203
pratima_hb 39:1270497828ca 204 // This part of the code sets up the Mode 1 for SPI to be used for
pratima_hb 39:1270497828ca 205 // communication with EASE. This is needed because SPI for EASE operates
pratima_hb 39:1270497828ca 206 // in Mode 1 which is different than what the motor shield SPI operates in.
pratima_hb 39:1270497828ca 207 dev_spi.setup(8, 3, 3E6); // Mode 3 set for SPI device for the motor
pratima_hb 39:1270497828ca 208 wait_us(200);
pratima_hb 39:1270497828ca 209
pratima_hb 39:1270497828ca 210 // command the motor to go to a set postion
pratima_hb 40:fc0e002acc8c 211 if (ease_read[0] == 0)
pratima_hb 40:fc0e002acc8c 212 {
pratima_hb 40:fc0e002acc8c 213 printf("Value from EASE %d.\r\n", ease_read[0]);
pratima_hb 40:fc0e002acc8c 214 motor->move(StepperMotor::FWD, STEPS_1);
pratima_hb 40:fc0e002acc8c 215 }
pratima_hb 40:fc0e002acc8c 216 else if(ease_read[0] == 10)
pratima_hb 40:fc0e002acc8c 217 {
pratima_hb 40:fc0e002acc8c 218 printf("Value from EASE %d.\r\n", ease_read[0]);
pratima_hb 40:fc0e002acc8c 219 motor->move(StepperMotor::BWD, STEPS_1);
pratima_hb 40:fc0e002acc8c 220 }
pratima_hb 40:fc0e002acc8c 221
pratima_hb 39:1270497828ca 222 /* Getting current position. */
pratima_hb 39:1270497828ca 223 int position = motor->get_position();
pratima_hb 39:1270497828ca 224
pratima_hb 39:1270497828ca 225 /* Printing to the console. */
pratima_hb 39:1270497828ca 226 printf(" Position: %d.\r\n", position);
pratima_hb 39:1270497828ca 227
pratima_hb 39:1270497828ca 228 /* Waiting. */
pratima_hb 39:1270497828ca 229 wait_ms(DELAY_1);
pratima_hb 39:1270497828ca 230
pratima_hb 39:1270497828ca 231 }
Davidroid 6:32166bfc04b0 232
Davidroid 0:e6a49a092e2a 233 }