MATLAB Examples

EtherCAT® Communication - Motor Position Control with an Accelnet™ Drive and Beckhoff® Analog IO Devices

This example shows how to control the position of a motor through an EtherCAT network.



For this example, the EtherCAT network consists of the target computer as EtherCAT Master device and 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 a 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 the EtherCAT OUT port of the Accelnet™ drive to the EtherCAT IN port of the Beckhoff® EK1100 coupler.
  3. Assemble the Beckhoff® EK1100 coupler and the Beckoff® IO terminals EL3062 and EL4002.
  4. Connect a motor to the Accelnet™ Drive.
  5. Connect a variable power supply to the Input port 1 of the EL3062 terminal.
  6. Make sure the Accelnet™ drive and the Beckhoff® terminals are supplied with a 24-volt power source.
  7. 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: xpcEthercatPositionControl. This model creates a sine wave, and modulates it by multiplying by the value of the signal present at the first input port of Terminal EL3062. The modulated signal is sent as motor position command to the drive (see Figure 1).

% 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','CopleyMotorPositionConfig.xml'), '.', 'f' );

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

Figure 1: EtherCAT model for controlling the position of a motor through an analog input terminal.

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 CopleyMotorPositionConfig.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, Terminal EK1100, EL3062 and EL4002 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 variables to be accessed as signals from the Beckhoff® IO terminals and the Process Data Objects (PDOs) variables to be accessed from the Accelnet™ drive.
  3. Define at least one cyclic task, select a task execution rate and associate the selected IO and PDO variables to the task. You only need to select one variable from each PDO to make every variable in that PDO accessible.
  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.
  5. Close or disconnect configurator from the EtherCAT network or you may get interference between Simulink Real-Time and configurator.

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™ drive from Copley Controls and Terminals EK1100, EL3062 and EL4002 from Beckhoff®.

For this example, five receive PDO variables are defined in the configuration file and three are used in the three EtherCAT PDO Transmit blocks: Control Word, Modes of Operation, and Profile Target Position. 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's operating mode. The constant value 8 is given as input to the block to set the mode of the drive to 'Cyclic Synchronous Position mode' (refer to the CANOpen manual from Copley Controls for details on supported modes of operation). The Profile Target Position PDO variable serves to set the desired position. In this example, the position command given as input to the block is a sine wave modulated by the signal read at the first input channel of Terminal EL3062.

Transmit PDO variables are also defined in the configuration file and two are used in the two EtherCAT PDO Receive blocks: 'Actual Motor Position' for the drive and 'Channel 1.Value' for the EL3062 terminal. The Actual Motor Position PDO variable indicates the current value of the motor 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
 pause(20); % Let the model run for 20 seconds
### Starting Simulink Real-Time build procedure for model: xpcEthercatPositionControl
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: xpcEthercatPositionControl
### Looking for target: TargetPC2
### Download model onto target: TargetPC2

Display the Target Computer Scopes

Take a snapshot of the target computer video display. The position command for the motor is varied following the modulated sine wave. The motor turns alternatively in one direction and its opposite. By varying the amplitude of the voltage at Input port 1 of Terminal EL3062 between 0 and 10 volts, the amplitude by which the position of the motor changes increases or decreases in same proportion.

  • 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 PDOs received from and sent to the Drive. Those are the position command (sent to the Drive by the Master) and the actual motor position (sent to the Master by the Drive). As expected, these two signals coincide, after the small offset introduced by the transmission delay.
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