EtherCAT® Communication - Motor Velocity Control with Accelnet™ Drive

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


For this example, the EtherCAT network consists of the target computer as EtherCAT Master device and an Accelnet™ AEP 180-18 drive from Copley Controls as EtherCAT Slave device. A supported brushless or brush motor must be connected to the drive. An example of motor that can be used is the SM231BE-NFLN from PARKER. In order to run this example, a dedicated network card must be installed and available on the target computer to be used for the EtherCAT communication, 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 the Accelnet™ drive is supplied with a 24-volt power supply.

  4. Build and download the model onto the target.

For a worked-out example of configuring the EtherCAT network, configuring the EtherCAT master node model, and building and running the real-time application, see the Simulink Real-Time EtherCAT documentation.

Open the model

Click here to open the model: xpcEthercatVelocityControl. 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.
copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','CopleyMotorVelocityConfig.xml'), '.', 'f' );

% Open the model.
mdl = 'xpcEthercatVelocityControl';
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if isempty( strcmp(systems, mdl ) )
  mdlOpen = 1;

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. You can obtain these values by using the command tg.getPCIInfo('all') at the Matlab command line on the development computer.

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. Here is an overview of the process for creating the configuration file in the EtherCAT configurator. For more information, see the Simulink Real-Time EtherCAT documentation.

  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 it was created for (i.e. the network discovered in step 1 above in the configuration file creation process). 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 four bits to 1 in order 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 indicates the current values of the motor velocity and position as read in the drive. Make sure the required transmit and receive PDO variables are selected in the blocks as illustrated in Figure 1 before running the example (you may need to refresh them).

Build, Download and Run the model

% Build the model and download to the target computer.
set_param(mdl,'RTWVerbose','off'); % Configure for a non-Verbose build.
rtwbuild(mdl);                     % Build and download application.

% Run the model
### Starting Simulink Real-Time build procedure for model: xpcEthercatVelocityControl
### Generated code for 'xpcEthercatVelocityControl' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: xpcEthercatVelocityControl
### Looking for target: TargetPC2
### Download model onto target: TargetPC2

The velocity command for the motor is a low frequency sine wave. The actual velocity read back from the controller is delayed by 1 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)

tg.viewTargetScreen; % Take a snapshot of the target scopes

Stop the model

%Stop the model

Close the model

% Close the model if we opened it.
if (mdlOpen)

% EOF xpcethercataiodemo.m
Was this topic helpful?