Esmacat
Published © GPL3+

Connect Touch LCD & Motor Shield w/ EASE Tutorial

Use an EtherCAT master to communicate with 2 EtherCAT Arduino Shield by Esmacat(EASE) on Arduino to control motors using Touch shield input.

IntermediateProtip2 hours2,618

Things used in this project

Hardware components

Esmacat Master S
(Optional Esmacat Master)
×1
Esmacat Master C
(Optional Esmacat Master)
×1
Arduino UNO
Arduino UNO
×2
EtherCAT Arduino Shield by Esmacat (EASE)
×2
Ethernet Cable, Cat6a
Ethernet Cable, Cat6a
×3
Power over Ethernet Injector
×1
DC Adapter
To supply power to the Power over Ethernet Injector.
×1
Touch LCD Shield 2.4"
×1
Adafruit Motor Shield
Adafruit Motor Shield V2 to control the motors
×1
Servo Motor
(Optional to test the code). Any Servo motor can be used. This specific motor has been used in this tutorial.
×1
DC Motor
(Optional Motor to test the code). Any dc motor can be used. This specific motor has been used in this tutorial.
×1
Stepper Motor
(Optional Motor to test the code). Any stepper motor can be used. This specific motor has been used in this tutorial.
×1
Power Source
Any power source like a lipo battery or battery eliminator to power the motors.
×1

Software apps and online services

Visual Studio 2017
Microsoft Visual Studio 2017
(Optional) IDE for building C++ Applications in Windows.
QT creator
(Optional) IDE for building C++ Applications in Linux.
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Hardware Setup Schematic

Schematic of the connection used in the tutorial

Physical Hardware Connection schematic

Hardware setup after final connections

Code

Arduino with EASE and Touch Shield Code

Arduino
/*
  Motors Control using LCD Touch Shield
  
  This sketch does the following:
  1) Gets user input for choice of motor from another Arduino connected to the EASE master.
  2) Depending on the motor chosen, it does the following
      2.1) If the motor chosen is a DC Motor, using the touch input for the up and down buttons, 
           speed of the motor attached to the second Arduino can be controlled.
           The current speed of the motor is printed on the LCD.
      2.2) If the motor chosen is a Servo Motor, using the touch input for the up and down buttons,
           position of the servo attached to the second Arduino can be controlled.
           The current position of the motor is printed on the LCD.
      2.3) If the motor chosen is a Stepper Motor, using the touch input for the up and down buttons,
           speed of the motor attached to the second Arduino can be controlled.
           The current speed of the motor is printed on the LCD.            

  created 14 Jan 2020
  by Harmonic Binonics Inc. (https://www.harmonicbionics.com/).
     Esmacat (https://www.esmacat.com/). 
*/

/*
    Motor Select Options
    |--------------------------------------------------------|
    |   Motor Select Integer Value   |    Motor Controlled   |
    |--------------------------------------------------------|
    |               0                |       Servo Motor     |
    |               1                |         DC  Moror     |
    |               2                |     Stepper Motor     |
    |--------------------------------------------------------|
*/

// Include the necessary libraries
# include <Adafruit_TFTLCD.h> 
# include <Adafruit_GFX.h>    
# include <TouchScreen.h>
# include <Esmacatshield.h>   //Include the Esmacat Arduino Library

// define the LCD Pins
# define LCD_CS A3     
# define LCD_CD A2 
# define LCD_WR A1     
# define LCD_RD A0 
# define LCD_RESET A4 

// Calibrate the values from the LCD
// These values may vary depending on the board used
# define TS_MINX 160 
# define TS_MINY 76
# define TS_MAXX 915 
# define TS_MAXY 877

// Define the pins used for the touch screen
// Note: This may vary depending of the board used.
# define YP A3 
# define XM A2
# define YM 7  
# define XP 6

// Define human readable colour values
# define BLACK   0x0000
# define BLUE    0x001F
# define RED     0xF800 
# define GREEN   0x07E0
# define CYAN    0x07FF 
# define MAGENTA 0xF81F
# define YELLOW  0xFFE0 
# define WHITE   0xFFFF

// Define the min max pressure values sensed by the touch screen
# define MINPRESSURE 10 
# define MAXPRESSURE 1000

# define ARDUINO_UNO_SLAVE_SELECT 10   // The chip selector pin used in Arduino Uno is 10

Adafruit_TFTLCD touch_lcd(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);
Esmacatshield ease_with_touch_lcd(ARDUINO_UNO_SLAVE_SELECT);      // Create a slave object and specify the Chip Selector Pin

// Define the required variables
boolean first_page = true, motor_control_page = false;
boolean servo_page = false, dc_page = false, stepper_page = false;

int motor_select;
int servo_position = 0, dc_pwm = 40, stepper_speed = 0;
int ease_registers[8];      // EASE 8 registers

// Start page method with required UI
void start_page()
{
  touch_lcd.setRotation(1);
  touch_lcd.fillScreen(WHITE);
  touch_lcd.drawRect(0,0,319,240,YELLOW);
  
  touch_lcd.setCursor(30,40);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("EASE LCD Touch Screen");
  
  touch_lcd.setCursor(27,80);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("Motor Control Tutorial\n\n        by Esmacat");
  
  touch_lcd.setCursor(20,150);
  touch_lcd.setTextColor(BLUE);
  touch_lcd.setTextSize(2);
  touch_lcd.print("@ Harmonic Bionics Inc.");
  
  touch_lcd.fillRect(30,180, 250, 40, RED);
  touch_lcd.drawRect(30,180,250,40,BLACK);
  touch_lcd.setCursor(40,190);
  touch_lcd.setTextColor(WHITE);
  touch_lcd.setTextSize(3);
  touch_lcd.print("Begin Program");
}

// UI for the Motor Control Page
void motor_control()
{
  touch_lcd.fillScreen(WHITE);
  touch_lcd.drawRect(0,0,319,240,YELLOW);
  touch_lcd.fillRect(35,25, 250, 40, BLUE);
  touch_lcd.setCursor(60,30);
  touch_lcd.setTextColor(WHITE);
  touch_lcd.setTextSize(3);
  touch_lcd.print("Servo Motor");

  touch_lcd.fillRect(35,75, 250, 40, RED);
  touch_lcd.setCursor(86,80);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("DC Motor");

  touch_lcd.fillRect(35,125, 250, 40, GREEN);
  touch_lcd.setCursor(45,130);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("Stepper Motor");

  touch_lcd.fillRect(115,190, 80, 40, MAGENTA);
  touch_lcd.setCursor(120,195);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("EXIT");
}

// UI specific for Servo Motor Control page
void servo_motor_page()
{
  touch_lcd.fillScreen(WHITE);
  touch_lcd.drawRect(0,0,319,240,YELLOW);
  touch_lcd.setCursor(40,20);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("Servo Motor Control");

  touch_lcd.fillRect(35,65, 50, 40, GREEN);
  touch_lcd.setCursor(50,75);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("+");

  touch_lcd.fillRect(35,115, 50, 40, RED);
  touch_lcd.setCursor(50,125);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("-");

  touch_lcd.fillRect(120,60, 170, 100, YELLOW);
  touch_lcd.setCursor(170,105);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(4);
  touch_lcd.print(servo_position);  

  touch_lcd.setCursor(125,70);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("Current Angle");  

  touch_lcd.fillRect(115,190, 80, 40, MAGENTA);
  touch_lcd.setCursor(120,195);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("BACK");
}

// UI specific for DC Motor Control page
void dc_motor_page()
{
  touch_lcd.fillScreen(WHITE);
  touch_lcd.drawRect(0,0,319,240,YELLOW);
  touch_lcd.setCursor(50,20);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("DC Motor Control");

  touch_lcd.fillRect(35,65, 50, 40, GREEN);
  touch_lcd.setCursor(50,75);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("+");

  touch_lcd.fillRect(35,115, 50, 40, RED);
  touch_lcd.setCursor(50,125);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("-");

  touch_lcd.fillRect(120,60, 170, 100, YELLOW);
  touch_lcd.setCursor(180,105);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(4);
  touch_lcd.print(dc_pwm);  

  touch_lcd.setCursor(135,70);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("Current PWM");  

  touch_lcd.fillRect(115,190, 80, 40, MAGENTA);
  touch_lcd.setCursor(120,195);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("BACK");
}

// UI specific for Stepper Motor Control page
void stepper_motor_page()
{
  touch_lcd.fillScreen(WHITE);
  touch_lcd.drawRect(0,0,319,240,YELLOW);
  touch_lcd.setCursor(40,20);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("Stepper Motor Control");

  touch_lcd.fillRect(35,65, 50, 40, GREEN);
  touch_lcd.setCursor(50,75);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("+");

  touch_lcd.fillRect(35,115, 50, 40, RED);
  touch_lcd.setCursor(50,125);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("-");

  touch_lcd.fillRect(120,60, 170, 100, YELLOW);
  touch_lcd.setCursor(190,105);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(4);
  touch_lcd.print(stepper_speed);  

  touch_lcd.setCursor(125,70);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(2);
  touch_lcd.print("Current Speed");  

  touch_lcd.fillRect(115,190, 80, 40, MAGENTA);
  touch_lcd.setCursor(120,195);
  touch_lcd.setTextColor(BLACK);
  touch_lcd.setTextSize(3);
  touch_lcd.print("BACK");
}

// Setup function
void setup()
{
  Serial.begin(9600);   // Begin Serial Communication
  touch_lcd.reset();
  touch_lcd.begin(0x9341);   // Begin the LCD Touch Shield with the identifier
  start_page();
  ease_with_touch_lcd.start_spi();      // Start SPI for EASE
}

void loop()
{
  TSPoint p = ts.getPoint();

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE)  // If the touch pressure is significant
  {
    // Mapt the raw values to pixel values of the Screen
    p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
    p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240);  
    
    if(p.x>172 && p.x<179 && p.y>6 && p.y<194 && first_page)   // Begin program button
    {
      first_page = false;
      motor_control_page = true;
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      motor_control();
    }  
    
    else if(p.x>=197 && p.x<=202 && p.y>7 && p.y<196 && motor_control_page)   // Servo Motor Button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      servo_page = true;
      motor_control_page = false;
      servo_motor_page();
    }  

    else if(p.x>=188 && p.x<=194 && p.y>9 && p.y<196 && motor_control_page)   // DC Motor Button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      dc_page = true;
      motor_control_page = false;
      dc_motor_page();
    }

    else if(p.x>=183 && p.x<=187 && p.y>8 && p.y<195 && motor_control_page)   // Stepper Motor Button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      stepper_page = true;
      motor_control_page = false;
      stepper_motor_page();
    }

    else if(p.x>170 && p.x<177 && p.y>69 && p.y<128 && motor_control_page)   // Exit button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      first_page = true;
      motor_control_page = false;
      start_page();
    }

    else if(p.x>170 && p.x<177 && p.y>69 && p.y<128 && (servo_page || dc_page || stepper_page))   // Back button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      motor_control_page = true;
      servo_page = false;
      dc_page = false;
      stepper_page = false;
      motor_control();
    }

    else if(p.x>= 194 && p.x<=200 && p.y>= 8 && p.y<= 40 && (servo_page || dc_page || stepper_page))   // Increase button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      
      touch_lcd.fillRect(160,85, 130, 75, YELLOW);
      touch_lcd.setCursor(170,105);
      touch_lcd.setTextColor(BLACK);
      touch_lcd.setTextSize(4);
      
      if(servo_page)
      {
        servo_position += 1;
        motor_select = 0;
        touch_lcd.print(servo_position);
        ease_with_touch_lcd.write_reg_value(0,motor_select);   // Write value into the EASE registers (register number, data) 
        ease_with_touch_lcd.write_reg_value(1,servo_position);  // Write value into the EASE registers (register number, data) 
      }
      else if (dc_page)
      {
        dc_pwm += 1;
        motor_select = 1;
        touch_lcd.print(dc_pwm);
        ease_with_touch_lcd.write_reg_value(0,motor_select);   // Write value into the EASE registers (register number, data) 
        ease_with_touch_lcd.write_reg_value(2,dc_pwm);   // Write value into the EASE registers (register number, data) 
      }
      else if (stepper_page) 
      {
        stepper_speed += 1;
        motor_select = 2;
        touch_lcd.print(stepper_speed);
        ease_with_touch_lcd.write_reg_value(0,motor_select);   // Write value into the EASE registers (register number, data) 
        ease_with_touch_lcd.write_reg_value(3,stepper_speed);   // Write value into the EASE registers (register number, data) 
      }
    }

    else if(p.x>= 187 && p.x<=192 && p.y>= 9 && p.y<= 41 && (servo_page || dc_page || stepper_page))   // Decrease Button
    {
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      touch_lcd.fillRect(160,85, 130, 75, YELLOW);
      touch_lcd.setCursor(170,105);
      touch_lcd.setTextColor(BLACK);
      touch_lcd.setTextSize(4);
      
      if(servo_page)
      {
        motor_select = 0;
        servo_position -= 1;
        touch_lcd.print(servo_position);
        ease_with_touch_lcd.write_reg_value(0,motor_select);   // Write value into the EASE registers (register number, data) 
        ease_with_touch_lcd.write_reg_value(1,servo_position);   // Write value into the EASE registers (register number, data) 
      }
      else if (dc_page)
      {
        dc_pwm -= 1;
        motor_select = 1;
        touch_lcd.print(dc_pwm);
        ease_with_touch_lcd.write_reg_value(0,motor_select);   // Write value into the EASE registers (register number, data) 
        ease_with_touch_lcd.write_reg_value(2,dc_pwm);   // Write value into the EASE registers (register number, data) 
      }
      else if (stepper_page) 
      {
        motor_select = 2;
        stepper_speed -= 1;
        touch_lcd.print(stepper_speed);
        ease_with_touch_lcd.write_reg_value(0,motor_select);   // Write value into the EASE registers (register number, data) 
        ease_with_touch_lcd.write_reg_value(3,stepper_speed);   // Write value into the EASE registers (register number, data) 
      } 
      Serial.print(motor_select);Serial.print(servo_position);Serial.print(" ");Serial.print(dc_pwm);Serial.print(" ");Serial.println(stepper_speed);   // To print data onto the Serial Monitor for debugging
    }
  }
}

Arduino with EASE and Motor Shield Code

Arduino
/*
  Motors Control

  This sketch does the following:
  1) Gets update for the speed from another Arduino connected to the EASE master.
  2) Depending on the motor chosen, it does the following
      2.1) If the motor chosen is a DC Motor, the motor is set to run at the set speed.
           The current speed of the motor is printed on the LCD.
      2.2) If the motor chosen is a Servo Motor, the motor is set at the desired position.
           The current position of the motor is printed on the LCD.
      2.3) If the motor chosen is a Stepper Motor, the motor is set to run at the set speed.          

  created 14 Jan 2020
  by Harmonic Binonics Inc. (https://www.harmonicbionics.com/). 
  
*/

/*
    Motor Select Options
    |--------------------------------------------------------|
    |   Motor Select Integer Value   |    Motor Controlled   |
    |--------------------------------------------------------|
    |               0                |       Servo Motor     |
    |               1                |         DC  Moror     |
    |               2                |     Stepper Motor     |
    |--------------------------------------------------------|
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>   // Include the Adafruit Motor Shield Library

#include <Servo.h>   // Include the Arduino Servo Library
#include <Esmacatshield.h>      //Include the Esmacat Arduino Library

// Define the Pin Numbers as Macros
# define servo_control_pin 9   // Servo is connected to Servo port #1 on the Motor Shield
# define Serial_baud_rate 9600
# define ARDUINO_UNO_SLAVE_SELECT 10      // The chip selector pin for Arduino Uno is 10 

// Create objects for various motor classes and EASE
Esmacatshield ease_with_motor(ARDUINO_UNO_SLAVE_SELECT);      // Create a slave object and specify the Chip Selector Pin

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// Create an object for Stepper Motor
Adafruit_StepperMotor *stepper_motor = AFMS.getStepper(200, 2);   // Connect a stepper motor with 200 steps per revolution
                                                                  // (1.8 degree) to motor port #2 (M3 and M4)
// Create an object for DC Motor
Adafruit_DCMotor *dc_motor = AFMS.getMotor(2);   // Connect a DC motor to motor port #2 (M2)

// Create an object for the Servo Motor Class
Servo servo_2;   // Servo is connected to Servo port #2 on the Motor Shield so the name servo_2 is used

// Required variables Initialization
int motor_select;
int servo_angle = 0;   // Variable to store the input angle
int stepper_speed = 0;   // Variable to store stepper motor speed
int dc_speed = 0;   // Variable to store dc motor speed
int ease_registers[8];      // EASE 8 registers

void setup() 
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  AFMS.begin();  // create with the default frequency 1.6KHz
  
  stepper_motor->setSpeed(10);  // 10 rpm 
  
  dc_motor->setSpeed(0);   // Set the initial Speed of DC Motor to 0

  servo_2.attach(servo_control_pin);   // Servo 1 on Adafruit Motor Shield is connected
                                       // to Pin 10 of the Arduino board
  ease_with_motor.start_spi();      // Start SPI for EASE
}

void loop() 
{
  ease_with_motor.get_ecat_registers(ease_registers);   // Update EASE registers with new data
  motor_select = ease_registers[0];
  servo_angle = ease_registers[1];   // Actual Servo position
  dc_speed = ease_registers[2];   // Actual dc motor speed
  stepper_speed = ease_registers[3];   // Actual Stepper Motor Speed
  Serial.println(motor_select);
  if(motor_select == 0)
  {
    servo_2.write(servo_angle);
    Serial.print("Servo Angle ");
    Serial.println(servo_angle);
  }
  
  if(motor_select == 1)
  {
    if(dc_speed > 0)
    {
      dc_motor->setSpeed(dc_speed);
      dc_motor->run(FORWARD);
    }
    else
    {
      dc_motor->setSpeed(dc_speed);
      dc_motor->run(BACKWARD);
    }
    Serial.print("DC Speed");
    Serial.println(dc_speed);
  }

  if(motor_select == 2)
  {
    if(stepper_speed > 0)
    {
      stepper_motor->step(stepper_speed,FORWARD,SINGLE);
    }

    else
    {
      stepper_motor->step(stepper_speed,BACKWARD,SINGLE);
    }
    Serial.print("Stepper Speed");
    Serial.println(stepper_speed);
   }
}

ease_touch_lcd_motor_control/my_app.cpp

C/C++
Source code of the application class
/** @file
 * @brief This file contains the definition of the functions associated with the user-defined
 * application for the Esmacat slave project */
 /*****************************************************************************************
 * INCLUDES
 ****************************************************************************************/
#include "my_app.h"

/*****************************************************************************************
 * FUNCTIONS
 ****************************************************************************************/
/**
 * @brief Identifies the actual Esmacat slave sequence in the EtherCAT communication chain.
 */
void my_app::assign_slave_sequence(){
    // tell the master what type of slave is at which point in the chain
    assign_esmacat_slave_index(&ease_motor_shield, 1);  //<-- The Motor Shield is connected in number 0
    assign_esmacat_slave_index(&ease_touch_lcd_shield, 0);   //<-- The LCD Shield is connected in number 1
}

/**
 * @brief Configure your Esmacat slave.
 * Link Esmacat slave object with the actual Esmacat slave in the EtherCAT communication chain.
 * Functions beginning with 'configure_slave' must only be executed in this function
 */
void my_app::configure_slaves(){
    // add initialization code here
    // Functions starting with "configure_slave" work only in configure_slaves() function
}

/** @brief Initialization that needs to happen on the first iteration of the loop
 */
void my_app::init()
{ 
    // Touch LCD Shield Initialization
    ease_touch_lcd_shield.set_output_variable_0_OUT_GEN_INT0(0);  // Initialize data into register 0 corresponding to the slave object
    ease_touch_lcd_shield.set_output_variable_1_OUT_GEN_INT1(0); // Initialize data into register 1 corresponding to the slave object
    ease_touch_lcd_shield.set_output_variable_2_OUT_GEN_INT2(0); // Initialize data into register 2 corresponding to the slave object
    ease_touch_lcd_shield.set_output_variable_3_OUT_GEN_INT3(0); // Initialize data into register 3 corresponding to the slave object

    // Motor Shield Initialization
    ease_motor_shield.set_output_variable_0_OUT_GEN_INT0(0); // Initialize data into register 0 corresponding to the slave object
    ease_motor_shield.set_output_variable_1_OUT_GEN_INT1(0); // Initialize data into register 1 corresponding to the slave object
    ease_motor_shield.set_output_variable_2_OUT_GEN_INT2(0); // Initialize data into register 2 corresponding to the slave object
    ease_motor_shield.set_output_variable_3_OUT_GEN_INT3(0); // Initialize data into register 3 corresponding to the slave object

    // Intialisation of the variables
    motor_select = 0;
    servo_postion = 0;
    dc_speed = 0;
    stepper_speed = 0;
    prev_dc = 0;
    prev_servo = 0;
    prev_stepper = 0;
}
/*
   EASE Touch LCD Shield Register Mapping
   |--------------------------------------------------|
   |   Register Number   |           Mapping          |
   |--------------------------------------------------|
   |         0           |         Motor Select       |
   |         1           |         Servo Angle        |
   |         2           |       DC Motor Speed       |
   |         3           |     Stepper motor Speed    |
   |--------------------------------------------------|
*/

/*
   EASE Motor Shield Register Mapping
   |--------------------------------------------------|
   |   Register Number   |           Mapping          |
   |--------------------------------------------------|
   |         0           |         Motor Select       |
   |         1           |         Servo Angle        |
   |         2           |       DC Motor Speed       |
   |         3           |     Stepper motor Speed    |
   |--------------------------------------------------|
/**

 * @brief Executes functions at the defined loop rate
 */
void my_app::loop()
{
    // add functions below that are to be executed at the loop rate
    motor_select = ease_touch_lcd_shield.get_input_variable_0_IN_GEN_INT0();   // Read data from register 0 corresponding to the slave object

    ease_motor_shield.set_output_variable_0_OUT_GEN_INT0(motor_select);   // Write data into register 0 corresponding to the slave object

    if (motor_select == 0)   // Servo Motor Selected
    {
        servo_postion = ease_touch_lcd_shield.get_input_variable_1_IN_GEN_INT1();    // Read data from register 1 corresponding to the slave object
        ease_motor_shield.set_output_variable_1_OUT_GEN_INT1(servo_postion);   // Write data into register 1 corresponding to the slave object
        if (servo_postion != prev_servo) cout << "Servo at " << servo_postion << " angle \n";
    }

    else if (motor_select == 1)   // DC Motor Selected
    {
        dc_speed = ease_touch_lcd_shield.get_input_variable_2_IN_GEN_INT2();   // Read data from register 2 corresponding to the slave object
        ease_motor_shield.set_output_variable_2_OUT_GEN_INT2(dc_speed);   // Write data into register 2 corresponding to the slave object
        if (dc_speed != prev_dc) cout << "DC PWM at " << dc_speed << endl;
    }

    else if (motor_select == 2)   // Stepper Motor Selected
    {
        stepper_speed = ease_touch_lcd_shield.get_input_variable_3_IN_GEN_INT3();   // Read data from register 3 corresponding to the slave object
        ease_motor_shield.set_output_variable_3_OUT_GEN_INT3(stepper_speed);   // Write data into register 3 corresponding to the slave object
        if (stepper_speed != prev_stepper) cout << "Stepper speed at " << stepper_speed << " rpm \n";
    }
    prev_servo = servo_postion;
    prev_dc = dc_speed;
    prev_stepper = stepper_speed;
}

ease_touch_lcd_motor_control/main.cpp

C/C++
Software to be run on the EtheCAT master, main.cpp file
/** @file
 *  @brief This file contains the template for the main program for the Esmacat slave
 *  project */
/*****************************************************************************************
 * INCLUDES
 ****************************************************************************************/
#include <iostream>
#include "my_app.h"

/*****************************************************************************************
 * FUNCTIONS
 ****************************************************************************************/
/**
 * @brief Initializes the execution of the Ethercat communication and
 *        primary real-time loop for your application for the desired
 *        slave
 * @return
 */

int main()
{
    //this is defined in my_app.cpp and my_app.h
    my_app app;

    // if you already know the ethernet adapter name for EtherCAT, uncomment and use the line below
    //    app.set_ethercat_adapter_name(" WRITE YOUR ETHERNET ADAPTER NAME");

    // If the name is not known, select through the terminal an ethernet adapter (the slave)
    // you'd like to communicate with over EtherCAT
    app.set_ethercat_adapter_name_through_terminal();

    // start the esmacat application customized for your slave
    app.start();

    //the application runs as long as the esmacat master and slave are in communication
    while (app.is_esmacat_master_closed() == FALSE );
    return 0;
}

ease_touch_lcd_motor_control/my_app.h

C/C++
Header files for the application class
/** @file
 * @brief This file contains the declaration of the class associated with the user-defined
 * application for the Esmacat slave project */

#ifndef MY_APP_H
#define MY_APP_H

/*****************************************************************************************
 * INCLUDES
 ****************************************************************************************/
#include <iostream>
#include "application.h"
//Include the header file for the Esmacat slave you plan to use for e.g. Analog Input slave
//#include "esmacat_analog_input.h"
#include "ethercat_arduino_shield_by_esmacat.h"
using namespace std;

/*****************************************************************************************
 * CLASSES
 ****************************************************************************************/
/**
 * @brief Description of your custom application class
 *
 * Your custom application class my_app inherits the class 'esmacat_application'
 * Write functions to override the parent functions of 'esmacat_application'
 * Declare an object of your slave class (e.g. ecat_ai)
 * Declare any other variables you might want to add
 * Define the constructor for initialization
 */
class my_app : public esmacat_application
{
private:
    void assign_slave_sequence(); /** identify sequence of slaves and their types*/
    void configure_slaves(); /** setup the slave*/
    void init(); /** code to be executed in the first iteration of the loop */
    void loop(); /** control loop*/

    /** < create a Esmacat slave object for the EASE with Touch LCD shield */
    esmacat_ethercat_arduino_shield_by_esmacat ease_touch_lcd_shield;  

    /** < create a Esmacat slave object for the EASE with Motor shield */
    esmacat_ethercat_arduino_shield_by_esmacat ease_motor_shield;   
    
    // Declaring the required variables
    uint32_t servo_postion;
    uint32_t dc_speed;
    uint32_t stepper_speed;
    uint32_t motor_select;
    uint32_t prev_servo, prev_dc, prev_stepper;

public:
    /** A constructor- sets initial values for class members */
    my_app() {}
};

#endif // MY_APP_H

ease_touch_lcd_motor_coontrol/CMakeLists.txt

C/C++
CMake Lists file of the project
file(GLOB MY_PROJECT_HEADERS *.h)
file(GLOB MY_PROJECT_SOURCES *.cpp *.c)

add_executable(ease_touch_lcd_motor_control ${MY_PROJECT_SOURCES} ${MY_PROJECT_HEADERS})

target_link_libraries(ease_touch_lcd_motor_control ethercat_driver esmacat)

install(TARGETS ease_touch_lcd_motor_control DESTINATION ./bin)

Esmacat EtherCAT Master Software

EtherCAT Arduino Shield by Esmacat (EASE) Arduino Library

Credits

Esmacat

Esmacat

11 projects • 15 followers
Esmacat is an easy yet powerful EtherCAT solution for robotics. Our EtherCAT tech allow users to run EtherCAT applications in minutes!

Comments