Esmacat
Published © GPL3+

Raspberry Pi with Codesys PLC UI -Arduino controlling Motors

Use a Raspberry Pi with Codesys to control motors attached to an Arduino Uno using the EtherCAT Arduino Shield by Esmacat (EASE).

IntermediateProtip2 hours751

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
EtherCAT Master
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
EtherCAT Arduino Shield by Esmacat (EASE)
×1
Ethernet Cable, Cat6a
Ethernet Cable, Cat6a
×2
Power over Ethernet (POE) Injector
×1
DC Adapter
To supply power to the PoE Injector
×1
Adafruit Motor Shield
×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
Raspberry Pi Power Supply
×1

Story

Read more

Schematics

Physical Hardware Connection schematic

hardware setup after final connections

Hardware Setup Schematic

Schematic of the connection used in the tutorial

Code

EASE_codesys_motor_control

Arduino
Code to be uploaded to Arduino board with EASE & Motor Shield
/*
  Motors Control

  This sketch does the following:
  1) Controls 3 different motors attached to the Motor Shield.
  2) Depending on the motor selected as input form the Master,
      2.1) If the motor selected is a Servo Motor, the motor is set at the desired position.
      2.2) If the motor selected is a DC Motor, the motor is set to run at a particular speed 
           by modifying the pwm control input to the motor.
      2.3) If the motor is selected is a Stepper Motor, the motor is set to run at a particular
           speed.        
           
  created 4 Feb 2020
  by Harmonic Binonics Inc. (https://www.harmonicbionics.com/). 
  
*/

#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 ARDUINO_UNO_SLAVE_SELECT 10      // The chip selector pin used in 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

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

// 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 #1 (M2)

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

// Required variables Initialization

int operation_state = 0;   // Variable to choose which motor to control
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() 
{
  AFMS.begin();  // create with the default frequency 1.6KHz
  
  stepper_motor->setSpeed(10);  // 10 rpm random initialization of speed
  
  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 all the EASE registers with new values

  operation_state = ease_registers[0];   // Motor to control
  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

  if(operation_state)   // Operate motors only if main switch is ON
  {
    servo_2.write(servo_angle);
    if(dc_speed > 0)
    {
      dc_motor->setSpeed(dc_speed);
      dc_motor->run(FORWARD);
    }
    else
    {
      dc_motor->setSpeed(dc_speed);
      dc_motor->run(BACKWARD);
    }
    if(stepper_speed > 0)
    {
      stepper_motor->step(stepper_speed,FORWARD,SINGLE);
    }

    else
    {
      stepper_motor->step(stepper_speed,BACKWARD,SINGLE);
    }
  }
  
  else
  {
    dc_speed = 0;
    stepper_speed = 0;
    servo_angle = 0;
  }
}

Codesys Master Code

C/C++
Binary file of the Codesys Motor Control project
No preview (download only).

EtherCAT Arduino Shield by Esmacat (EASE) Arduino Library

Esmacat Arduino Library

Credits

Esmacat

Esmacat

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

Comments