EtherCAT® Communication - Motor Velocity Control with Accelnet™ Drive

This example shows how to control the velocity of a motor using EtherCAT communication.


To run this example, you need an EtherCAT network that consists of the target computer as EtherCAT Master device and an Accelnet™ AEP 180-18 drive from Copley Controls as EtherCAT Slave device. Connect a supported brushless or brush motor to the drive. An example motor that works with this example is the SM231BE-NFLN from PARKER.

This example requires a dedicated network card that is installed and is available on the target computer. Use the dedicated card for the EtherCAT communication. The dedicated card is in addition to the card used for the Ethernet link between the development and target computers.

To test this model:

  1. Connect the network port of the dedicated card in the target computer to the EtherCAT IN port of the Accelnet™ drive.

  2. Connect a motor to the Accelnet™ drive.

  3. Make sure that the Accelnet™ drive is supplied with a 24-volt power supply.

  4. Build and download the model onto the target.

For a complete example that configures the EtherCAT network, configures the EtherCAT master node model, and builds then runs the real-time application, see the Simulink Real-Time EtherCAT documentation.

Open the Model

To open the model, in the Command Window, type:


This model sends a varying velocity command to the drive.

The EtherCAT initialization block requires that the configuration ENI file is present in the current folder. Copy the example configuration file from the example folder to the current folder. Then, open the model.

copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','CopleyMotorVelocityConfig.xml'), '.', 'f' );
copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcEthercatVelocityControl.slx'), '.', 'f' );
mdl = 'xpcEthercatVelocityControl';
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if isempty( strcmp(systems, mdl ) )
  mdlOpen = 1;
  set_param('xpcEthercatVelocityControl/EtherCAT Init ','pci_bus','5','pci_slot','0','pci_function','0')

Figure 1: EtherCAT model for motor velocity control.

Configure the Model

Open the mask for the EtherCAT Init block and provide the required values for the PCI bus and slot numbers for the network card being used for EtherCAT communication. To get these values, in the Command Window type tg.getPCIInfo('ethernet'). An example command to set configuration parameters for the EtherCAT Init block is:

set_param('xpcEthercatVelocityControl/EtherCAT Init ','pci_bus','5','pci_slot','0','pci_function','0')

Describe Network with Configurator

Using a third-party EtherCAT configurator that you install on a development computer, generate an EtherCAT configuration file CopleyMotorVelocityConfig.xml.

This file describes the network to the master. For more information, see the Simulink Real-Time EtherCAT documentation.

An overview of the process for creating the configuration file in the EtherCAT configurator is:

  1. Connect the EtherCAT network (consisting of the Accelnet™ drive in this example) to the computer where the EtherCAT configurator is installed and scan the network to discover the connected slave devices.

  2. Select the transmit and receive Process Data Object (PDO) variables to be accessed from the Accelnet™ drive.

  3. Define at least one cyclic task and associate the selected PDO variables to the task.

  4. Export the configuration file into an XML file. Make sure the name of the XML file is different from the name of your Simulink® model.

Each EtherCAT configuration file is specific to the exact network setup for which it was created (for example, the network discovered in step 1 of configuration file creation). The configuration file provided for this example is valid if and only if the EtherCAT network consists of an Accelnet™ AEP drive from Copley Controls.

For this example, three receive PDO variables are defined and used in the three EtherCAT PDO Transmit blocks: Control Word, Modes of Operation and Target Velocity.

  • The Control Word PDO variable serves to control the state of the drive. The constant value 15 is given as input to the block to set the first 4 bits to 1 to enable the drive. Refer to the CANOpen manual from Copley Controls for details on the bits mapping of this variable.

  • The Modes of Operation PDO variable serves to set the drive operating mode. The constant value 3 is given as input to the block to set the mode of the drive to 'Profile Velocity mode'. Refer to the CANOpen manual from Copley Controls for details on supported modes of operation.

  • The Target Velocity PDO variable serves to set the desired velocity. In this example, the velocity command at the input of the block can be tuned through the gain block.

Three transmit PDO variables are also defined in the configuration file and used in the three EtherCAT PDO Receive blocks: Status Word, Actual Motor Velocity, and Actual Motor Position.

  • The Status Word PDO variable indicates the current state of the drive.

  • The Actual Motor Velocity and Actual Motor Position PDO variables indicate the current values of the motor velocity and position as read in the drive.

Make sure that the required transmit and receive PDO variables are selected in the blocks as illustrated in Figure 1 before running the example. (You could need to refresh these variables.)

Build, Download, and Run the Model

Build the model and download to the target computer. Then, run the model.

tg = slrt('TargetPC1');
### Starting Simulink Real-Time build procedure for model: xpcEthercatVelocityControl
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: xpcEthercatVelocityControl
### Created MLDATX ..\xpcEthercatVelocityControl.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

The velocity command for the motor is a low frequency sine wave. The actual velocity read back from the controller is delayed by one sample time and the actual position is out of phase by 90 degrees from the velocity, as expected.


Display the Target Computer Scopes

Take a snapshot of the target computer video display.

  • Scope 1 displays the outputs of the Ethercat Init block. See the documentation of this block for the meaning of the displayed values.

  • Scope 2 displays the PDO variables received and transmitted to the drive, once the drive initializes and the state goes to Op state (=8)

To take a snapshot of the target scopes, type:


Stop and Close the Model

When the example completes its run, stop and close the model.

if (mdlOpen)